Skip to content
Snippets Groups Projects
Commit 6f3aae76 authored by Christopher Randolph Rhodes's avatar Christopher Randolph Rhodes
Browse files

Working on export zstack of object labels for ilastik ingest

parent a2c2fa51
No related branches found
No related tags found
No related merge requests found
......@@ -24,11 +24,10 @@ if __name__ == '__main__':
'zmask_expand_box_by': (128, 3),
'export_pixel_probabilities': False,
'export_2d_patches_for_training': True,
'export_2d_patches_for_annotation': False,
'export_2d_patches_for_annotation': True,
'export_3d_patches': False,
'export_annotated_zstack': False,
'export_patch_masks': False,
'export_patch_label_maps': True,
'export_patch_masks': True,
}
input_files = get_matching_files(where_czi, 'czi', coord_filter={'P': (0, 10)}, )
......
from pathlib import Path
from extensions.chaeo.util import autonumber_new_directory, get_matching_files, loop_workflow
from extensions.chaeo.workflows import transfer_ecotaxa_labels_to_patch_stacks
if __name__ == '__main__':
root = Path('c:/Users/rhodes/projects/proj0011-plankton-seg/exp0009/output')
transfer_ecotaxa_labels_to_patch_stacks(
where_masks=(root / 'batch-output-20231012-0016/patch_masks').__str__(),
object_csv=(root / 'batch-output-20231011-0003/df_objects.csv').__str__(),
ecotaxa_tsv='c:/Users/rhodes/projects/proj0011-plankton-seg/exp0013/ecotaxa_export_10468_20231012_0930.tsv',
where_output=autonumber_new_directory(root, 'labeled_patches')
)
......@@ -14,7 +14,7 @@ def autonumber_new_directory(where: str, prefix: str) -> str:
ma = re.match(f'{prefix}-{yyyymmdd}-([\d]+)', ff.name)
if ma:
idx = max(idx, int(ma.groups()[0]) + 1)
new_path = (Path(where) / f'batch-output-{yyyymmdd}-{idx:04d}')
new_path = (Path(where) / f'{prefix}-{yyyymmdd}-{idx:04d}')
new_path.mkdir(parents=True, exist_ok=False)
return new_path.__str__()
......
......@@ -2,6 +2,7 @@ from pathlib import Path
from typing import Dict
from uuid import uuid4
import numpy as np
import pandas as pd
from extensions.ilastik.models import IlastikPixelClassifierModel
......@@ -30,7 +31,6 @@ def export_patches_from_multichannel_zstack(
export_3d_patches=True,
export_annotated_zstack=True,
export_patch_masks=True,
export_patch_label_maps=True,
) -> Dict:
ti = Timer()
stack = generate_file_accessor(Path(input_zstack_path))
......@@ -94,9 +94,11 @@ def export_patches_from_multichannel_zstack(
focus_metric='max_sobel',
ch_white=4,
ch_rgb_overlay=(3, None, None),
draw_bounding_box=True,
draw_bounding_box=False,
bounding_box_channel=1,
bounding_box_linewidth=2,
# draw_contour=True,
draw_mask=True,
overlay_gain=(0.1, 1.0, 1.0)
)
df_patches = pd.DataFrame(files)
......@@ -123,10 +125,13 @@ def export_patches_from_multichannel_zstack(
# prepopulate patch UUID
df['patch_id'] = df.apply(lambda _: uuid4(), axis=1)
if export_patch_masks:
files = export_patch_masks_from_zstack(
Path(where_output) / 'patch_masks',
stack.get_one_channel_data(4),
zmask_meta,
prefix=fstem,
)
if export_annotated_zstack:
......@@ -162,22 +167,61 @@ def export_patches_from_multichannel_zstack(
'interm': interm,
}
def transfer_ecotaxa_labels_to_patch_object_maps(
path_to_patches: str,
path_to_ecotaxa_tsv: str,
path_output: str,
def transfer_ecotaxa_labels_to_patch_stacks(
where_masks: str,
object_csv: str,
ecotaxa_tsv: str,
where_output: str,
patch_size: tuple = (256, 256),
) -> Dict:
where_patches = Path(path_to_patches)
df_meta = pd.read_csv(
path_to_ecotaxa_tsv,
df_obj = pd.read_csv(
object_csv,
)
df_ecotaxa = pd.read_csv(
ecotaxa_tsv,
sep='\t',
header=[0, 1],
header=[0],
dtype={
('object_annotation_date', '[t]'): str,
('object_annotation_time', '[t]'): str,
('object_annotation_category_id', '[t]'): str,
}
)
for pp in where_patches.iterdir():
patch = generate_file_accessor(pp)
df_merge = pd.merge(df_obj, df_ecotaxa, left_on='patch_id', right_on='object_id')
se_unique = pd.Series(
df_merge.object_annotation_hierarchy.unique()
)
df_split = (
se_unique.str.rsplit(
pat='>', n=1, expand=True
)
)
df_labels = pd.DataFrame({
'annotation_class_id': df_split.index,
'hierarchy': se_unique,
'annotation_class': df_split.loc[:, 1].str.lower()
})
df_labels.to_csv(Path(where_output) / 'labels_key.csv')
df_pf = pd.merge(
df_merge[['patch_filename', 'object_annotation_hierarchy']],
df_labels,
left_on='object_annotation_hierarchy',
right_on='hierarchy',
)
df_pl = df_pf[df_pf['object_annotation_hierarchy'].notnull()]
zstack = np.zeros((*patch_size, 1, len(df_pl)), dtype='uint16')
# export patches as z-stack
for fi, pl in enumerate(df_pl.itertuples(name='PatchFile')):
fn = pl._asdict()['patch_filename']
ac = pl._asdict()['annotation_class_id']
bm = generate_file_accessor(Path(where_masks) / fn).data
assert bm.shape == patch_size, f'Unexpected patch size {patch_size}'
zstack[:, :, 0, fi] = (bm == 255) * ac
# export masks as z-stack
write_accessor_data_to_file(where_output / 'zstack_object_label.tif', InMemoryDataAccessor(zstack))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment