From ed29f8f69b7bbbda005266ae7d749daa39f86996 Mon Sep 17 00:00:00 2001 From: Christopher Rhodes <christopher.rhodes@embl.de> Date: Mon, 16 Oct 2023 11:12:27 +0200 Subject: [PATCH] Start end-to-end transfer method to ilastik object model --- extensions/chaeo/examples/label_patches.py | 2 + ...fer_labels_to_ilastik_object_classifier.py | 56 ++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/extensions/chaeo/examples/label_patches.py b/extensions/chaeo/examples/label_patches.py index 04f2c00c..fe877e45 100644 --- a/extensions/chaeo/examples/label_patches.py +++ b/extensions/chaeo/examples/label_patches.py @@ -13,3 +13,5 @@ if __name__ == '__main__': where_output=autonumber_new_directory(root, 'labeled_patches'), ) print('Finished') + + diff --git a/extensions/chaeo/examples/transfer_labels_to_ilastik_object_classifier.py b/extensions/chaeo/examples/transfer_labels_to_ilastik_object_classifier.py index 9d5c273e..7b61d580 100644 --- a/extensions/chaeo/examples/transfer_labels_to_ilastik_object_classifier.py +++ b/extensions/chaeo/examples/transfer_labels_to_ilastik_object_classifier.py @@ -4,8 +4,22 @@ import h5py import ilastik.applets.objectClassification import numpy as np import pandas as pd +import tifffile from extensions.ilastik.models import IlastikObjectClassifierModel +from model_server.accessors import generate_file_accessor + +def get_dataset_info(h5): + lane = 'Input Data/infos/lane0000' + info = {} + for gk in ['Raw Data', 'Segmentation Image']: + info[gk] = {} + for dk in ['location', 'filePath', 'shape']: + try: + info[gk][dk] = h5[f'{lane}/{gk}/{dk}'][()] + except Exception as e: + print(e) + return info def transfer_labels_to_ilastik_ilp(ilp, df_stack_meta): @@ -22,6 +36,9 @@ def transfer_labels_to_ilastik_ilp(ilp, df_stack_meta): hf.write(f'{line[0]} --- {line[1]}\n') h5.visititems(lambda k, v: print(k + ' : ' + str(v))) + # put certain h5 groups in scope + h5info = get_dataset_info(h5) + # change key of label names ln = ['none'] + list(df_stack_meta.sort_values('annotation_class_id').annotation_class.unique()) del h5['ObjectClassification/LabelNames'] @@ -39,9 +56,44 @@ def transfer_labels_to_ilastik_ilp(ilp, df_stack_meta): ds[1] = float(df_stack_meta.loc[df_stack_meta.zi == idx, 'annotation_class_id'].iat[0]) print(f'Changed label {ti} from {la_old} to {ds[1]}') +def generate_ilastik_object_classifier(template_ilp, where_training: str): + + # validate input data + where = Path(where_training) + zstacks = { + 'raw': { + 'path': where / 'zstack_train_raw.tif', + }, + 'seg': { + 'path': where / 'zstack_train_mask.tif', + } + } + + for k, v in zstacks.items(): + # assert v['path'].exists(), 'Could not find input z-stack: ' + v['path'] + # ff = tifffile.imread(v['path']) + # v['nz'] = ff.shape[0] + # v['hw'] = ff.shape[1:2] + # v['dtype'] = ff.dtype + v['acc'] = generate_file_accessor(v['path']) + assert zstacks['raw']['acc'].is_binary() + + assert zstacks['raw'] + + with h5py.File(template_ilp, 'r+') as h5: + pass + + if __name__ == '__main__': - ilp = 'c:/Users/rhodes/model-server/ilastik/test_autolabel_obj.ilp' + ilp = 'c:/Users/rhodes/model-server/ilastik/test_autolabel_obj - Copy.ilp' + # ilp = 'c:/Users/rhodes/model-server/ilastik/test_template_obj.ilp' + df = pd.read_csv( 'c:/Users/rhodes/projects/proj0011-plankton-seg/exp0009/output/labeled_patches-20231014-0002/train_stack.csv' ) - transfer_labels_to_ilastik_ilp(ilp, df) \ No newline at end of file + # transfer_labels_to_ilastik_ilp(ilp, df) + + generate_ilastik_object_classifier( + ilp, + 'c:/Users/rhodes/projects/proj0011-plankton-seg/exp0009/output/labeled_patches-20231014-0002' + ) \ No newline at end of file -- GitLab