From 1c45425fd72d7e6fed2748d4da80601b5e2fb629 Mon Sep 17 00:00:00 2001 From: Christopher Rhodes <christopher.rhodes@embl.de> Date: Sun, 22 Oct 2023 08:36:21 +0200 Subject: [PATCH] Elevate whether to use a specific z-index or MIP to find best focus in zmask --- .../chaeo/actual_runs/20231008_Bilbao_PA.py | 64 +++++++++++++++++++ extensions/chaeo/workflows.py | 7 +- 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 extensions/chaeo/actual_runs/20231008_Bilbao_PA.py diff --git a/extensions/chaeo/actual_runs/20231008_Bilbao_PA.py b/extensions/chaeo/actual_runs/20231008_Bilbao_PA.py new file mode 100644 index 00000000..4312bccd --- /dev/null +++ b/extensions/chaeo/actual_runs/20231008_Bilbao_PA.py @@ -0,0 +1,64 @@ +from pathlib import Path + +from extensions.chaeo.util import autonumber_new_directory, get_matching_files, loop_workflow +from extensions.chaeo.workflows import export_patches_from_multichannel_zstack + +from model_server.accessors import CziImageFileAccessor, write_accessor_data_to_file, InMemoryDataAccessor +from model_server.process import rescale + + +def export_single_channel_tif_from_multichannel_czi(input_file_path, output_folder_path, channel, **kwargs): + in_acc = CziImageFileAccessor(input_file_path) + data = in_acc.get_one_channel_data(channel).data + if 'rescale_zmask_clip' in kwargs: + data = rescale(data, clip=kwargs['rescale_zmask_clip']) + outf = Path(output_folder_path) / (Path(input_file_path).stem + '.tif') + write_accessor_data_to_file( + outf, + InMemoryDataAccessor(data), + ) + print(f'Wrote file: {outf}') + +if __name__ == '__main__': + root = Path('c:/Users/rhodes/projects/proj0012-trec-handoff/owncloud-sync/TREC-HD/Images/') + where_czi = (root / 'TREC_STOP_24_Bilbao/231008_automic/20231008-162336/Selection').__str__() + where_output = autonumber_new_directory( + 'c:/Users/rhodes/projects/proj0011-plankton-seg/exp0017/output', + 'batch-output' + ) + + px_ilp = Path('c:/Users/rhodes/projects/proj0011-plankton-seg/exp0017/pxAF405_dim8bit.ilp').__str__() + + params = { + 'ilastik_project_file': px_ilp, + 'pxmap_threshold': 0.25, + 'pixel_class': 0, + 'zmask_channel': 0, + 'zmask_zindex': 3, + 'patches_channel': 2, + 'mask_type': 'boxes', + 'zmask_filters': {'area': (1e3, 1e8)}, + 'zmask_expand_box_by': (128, 3), + 'export_pixel_probabilities': True, + 'export_2d_patches_for_training': True, + 'draw_bounding_box_on_2d_patch': True, + 'export_2d_patches_for_annotation': True, + 'export_3d_patches': False, + 'export_annotated_zstack': True, + 'export_patch_masks': True, + 'rescale_zmask_clip': 0.01, + 'rgb_overlay_channels': (1, None, None), + 'rgb_overlay_weights': (0.5, 1.0, 1.0) + } + + input_files = get_matching_files(where_czi, 'czi', coord_filter={}) + + loop_workflow( + input_files, + where_output, + export_patches_from_multichannel_zstack, + params, + catch_and_continue=False, + ) + + print('Finished') \ No newline at end of file diff --git a/extensions/chaeo/workflows.py b/extensions/chaeo/workflows.py index bf1503e4..5e1b4e3c 100644 --- a/extensions/chaeo/workflows.py +++ b/extensions/chaeo/workflows.py @@ -23,6 +23,7 @@ def export_patches_from_multichannel_zstack( pxmap_threshold: float, pixel_class: int, zmask_channel: int, + zmask_zindex: None, # None for MIP, patches_channel: int, rescale_zmask_clip: int = None, mask_type: str = 'boxes', @@ -46,7 +47,11 @@ def export_patches_from_multichannel_zstack( ti.click('file_input') # MIP and classify pixels - zmask_data = stack.get_one_channel_data(channel=zmask_channel).data.max(axis=-1, keepdims=True) + if isinstance(zmask_zindex, int): + assert 0 < zmask_zindex < stack.nz + zmask_data = stack.get_one_channel_data(channel=zmask_channel).data[:, :, :, zmask_zindex] + else: + zmask_data = stack.get_one_channel_data(channel=zmask_channel).data.max(axis=-1, keepdims=True) if rescale_zmask_clip: zmask_data = rescale(zmask_data, rescale_zmask_clip) mip = InMemoryDataAccessor( -- GitLab