Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Fix ccagt to coco ps masks sizes (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnv1 authored Apr 11, 2022
1 parent 52ba460 commit 770e489
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
23 changes: 18 additions & 5 deletions CCAgT_utils/converters/CCAgT.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,17 +474,16 @@ def to_PS_COCO(self, categories_infos: CategoriesInfos, out_dir: str, split_by_s

print(
f'Start compute generate panoptic annotations and masks for {len(images_ids)} files using {cpu_num} cores with '
'{len(images_ids_splitted[0])} files per core...',
f'{len(images_ids_splitted[0])} files per core...',
)

workers = multiprocessing.Pool(processes=cpu_num)
processes = []
output_template = np.zeros((self.IMAGE_HEIGHT, self.IMAGE_WIDTH, 3), dtype=np.uint8)
for images_ids in images_ids_splitted:
if len(images_ids) == 0:
continue
df_to_process = self.df.loc[self.df['image_id'].isin(images_ids), :]
p = workers.apply_async(single_core_to_PS_COCO, (df_to_process, out_dir, output_template, split_by_slide))
p = workers.apply_async(single_core_to_PS_COCO, (df_to_process, out_dir, split_by_slide))
processes.append(p)

annotations_panoptic = []
Expand All @@ -501,12 +500,19 @@ def copy(self) -> CCAgT:
def single_core_to_PS_COCO(
df: pd.DataFrame,
out_dir: str,
output_template: np.ndarray,
split_by_slide: bool,
) -> list[dict[str, Any]]:
annotations_panoptic = []

_out_dir = out_dir

if df['image_width'].nunique() == df['image_height'].nunique() == 1:
w = int(df['image_width'].unique()[0])
h = int(df['image_height'].unique()[0])
output_template = np.zeros((h, w, 3), dtype=np.uint8)
else:
output_template = None

for img_id, df_by_img in df.groupby('image_id'):
img_name = df_by_img.iloc[0]['image_name']
output_basename = basename(img_name) + '.png'
Expand All @@ -525,7 +531,14 @@ def single_core_to_PS_COCO(
])

segments_info = []
out = output_template.copy()

if isinstance(output_template, np.ndarray):
out = output_template.copy()
else:
w = int(df_by_img['image_width'].unique()[0])
h = int(df_by_img['image_height'].unique()[0])
out = np.zeros((h, w, 3), dtype=np.uint8)

for ann in annotations_sorted:
out = draw_annotation(out, ann, ann.color.rgb, out.shape[:2])
segments_info.append({
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = CCAgT_utils
version = 0.2.2-alpha
version = 0.2.3-alpha
description = A framework of utilities to help at the use of the CCAgT dataset
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down
5 changes: 4 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,11 @@ def ccagt_df_single_nucleus(nucleus_ex):


@pytest.fixture
def ccagt_ann_single_nucleus(nucleus_ex):
def ccagt_ann_single_nucleus(nucleus_ex, shape):
d = pd.DataFrame([create.row_CCAgT(nucleus_ex, 1, 'C_xx1')])
d['image_width'] = shape[1]
d['image_height'] = shape[0]

return CCAgT.CCAgT(d)


Expand Down
25 changes: 21 additions & 4 deletions tests/converters/CCAgT_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,14 @@ def test_generate_masks(ccagt_ann_single_nucleus):
assert os.path.isfile(os.path.join(tmp_dir, 'C/C_xx1' + '.png'))


def test_single_core_to_PS_COCO(ccagt_ann_single_nucleus, tmpdir, shape):
def test_single_core_to_PS_COCO(ccagt_ann_single_nucleus, tmpdir):
ccagt_ann_single_nucleus.df['image_name'] = 'C_xx1'
ccagt_ann_single_nucleus.df['area'] = ccagt_ann_single_nucleus.geometries_area()
ccagt_ann_single_nucleus.df['image_id'] = ccagt_ann_single_nucleus.generate_ids(ccagt_ann_single_nucleus.df['image_name'])
ccagt_ann_single_nucleus.df['iscrowd'] = 0
ccagt_ann_single_nucleus.df['color'] = Color(21, 62, 125)

template = np.zeros((shape[0], shape[1], 3), dtype=np.uint8)
out = CCAgT.single_core_to_PS_COCO(ccagt_ann_single_nucleus.df, tmpdir, template, False)
out = CCAgT.single_core_to_PS_COCO(ccagt_ann_single_nucleus.df, tmpdir, False)

check1 = all(y in out[0] for y in {'image_id', 'file_name', 'segments_info'})
assert check1
Expand All @@ -294,10 +293,28 @@ def test_single_core_to_PS_COCO(ccagt_ann_single_nucleus, tmpdir, shape):

ccagt_ann_single_nucleus.df['slide_id'] = ccagt_ann_single_nucleus.get_slide_id()
tmpdir.mkdir('C')
CCAgT.single_core_to_PS_COCO(ccagt_ann_single_nucleus.df, tmpdir, template, True)
CCAgT.single_core_to_PS_COCO(ccagt_ann_single_nucleus.df, tmpdir, True)
assert len(os.listdir(os.path.join(tmpdir, 'C/'))) > 0


def test_single_core_to_PS_COCO_multisizes(ccagt_ann_multi, tmpdir):
ccagt_ann_multi.df['area'] = ccagt_ann_multi.geometries_area()
ccagt_ann_multi.df['image_id'] = ccagt_ann_multi.generate_ids(ccagt_ann_multi.df['image_name'])
ccagt_ann_multi.df['iscrowd'] = 0
ccagt_ann_multi.df['color'] = Color(21, 62, 125)

ccagt_ann_multi.df['image_width'] = 1000
ccagt_ann_multi.df['image_height'] = 1000

ccagt_ann_multi.df.loc[ccagt_ann_multi.df['image_id'] == 1, 'image_height'] = 2000

ccagt_ann_multi.df['geo_type'] = ccagt_ann_multi.df['geometry'].apply(lambda g: g.geom_type).tolist()
ccagt_ann_multi.df = ccagt_ann_multi.df[ccagt_ann_multi.df['geo_type'] != 'Point']
CCAgT.single_core_to_PS_COCO(ccagt_ann_multi.df, tmpdir, False)

assert len(os.listdir(tmpdir)) > 0


def test_CCAgT_to_PS_COCO(ccagt_ann_single_nucleus, categories_infos, tmpdir):
ccagt_ann_single_nucleus.df['image_name'] = 'C_xx1'
ccagt_ann_single_nucleus.df['area'] = ccagt_ann_single_nucleus.geometries_area()
Expand Down

0 comments on commit 770e489

Please sign in to comment.