From b8bd1a91c86e8a27e63a9f30b78e872f527bceb0 Mon Sep 17 00:00:00 2001 From: Christopher Rhodes <christopher.rhodes@embl.de> Date: Wed, 18 Oct 2023 15:23:54 +0200 Subject: [PATCH] Renamed a few output files --- ...fer_labels_to_ilastik_object_classifier.py | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) 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 f3b5a5bc..6dd5e27d 100644 --- a/extensions/chaeo/examples/transfer_labels_to_ilastik_object_classifier.py +++ b/extensions/chaeo/examples/transfer_labels_to_ilastik_object_classifier.py @@ -8,7 +8,6 @@ import skimage import uuid import vigra -from extensions.chaeo.util import autonumber_new_file from extensions.ilastik.models import IlastikObjectClassifierFromSegmentationModel from model_server.accessors import generate_file_accessor, GenericImageDataAccessor, InMemoryDataAccessor, write_accessor_data_to_file @@ -88,7 +87,13 @@ def get_dataset_info(h5: h5py.File, lane : int = 0): return info -def generate_ilastik_object_classifier(template_ilp: str, where: str, stack_name: str = 'train', lane: int = 0): +def generate_ilastik_object_classifier( + template_ilp: str, + where: str, + stack_name: str = 'train', + lane: int = 0, + proj_name='auto_obj' +): """ Starting with a template project file, transfer input data and labels to a duplicate project file. @@ -134,11 +139,11 @@ def generate_ilastik_object_classifier(template_ilp: str, where: str, stack_name assert info[hg]['location'] == b'FileSystem' assert info[hg]['axes'] == ['t', 'y', 'x'] - new_ilp = shutil.copy(template_ilp, root / autonumber_new_file(root, 'auto-obj', 'ilp')) + new_ilp = shutil.copy(template_ilp, where / (proj_name + '.ilp')) # write to new project file lns = f'{lane:04d}' - with h5py.File(new_ilp, 'r+') as h5: + with h5py.File(where / new_ilp, 'r+') as h5: def set_ds(grp, ds, val): ds = h5[f'Input Data/infos/lane{lns}/{grp}/{ds}'] ds[()] = val @@ -193,7 +198,7 @@ def compare_object_maps(truth: GenericImageDataAccessor, inferred: GenericImageD ob_id = skimage.measure.label(inf_img) pr = skimage.measure.regionprops_table(ob_id, properties=['label', 'area']) mask = inf_img == pr['label'][pr['area'].argmax()] - dd['inferred_label'] = np.unique(mask * inf_img)[1] + dd['inferred_label'] = np.unique(mask * inf_img)[-1] # occasionally no object in frame dd['multiples'] = True else: # exactly one unique object class in frame dd['inferred_label'] = unique[1] @@ -203,13 +208,14 @@ def compare_object_maps(truth: GenericImageDataAccessor, inferred: GenericImageD if __name__ == '__main__': root = Path('c:/Users/rhodes/projects/proj0011-plankton-seg/') template_ilp = root / 'exp0014/template_obj.ilp' - where_patch_stack = root / 'exp0009/output/labeled_patches-20231018-0001' + where_patch_stack = root / 'exp0009/output/labeled_patches-20231018-0005' # auto-populate an object classifier - new_ilp = generate_ilastik_object_classifier( + auto_ilp = generate_ilastik_object_classifier( template_ilp, where_patch_stack, - stack_name='train' + stack_name='train', + proj_name='auto_obj_before' ) def infer_and_compare(ilp, suffix): @@ -223,24 +229,16 @@ if __name__ == '__main__': # write comparison tables train_truth_labels = generate_file_accessor(where_patch_stack / f'zstack_train_label.tif') df_comp = compare_object_maps(train_truth_labels, result_acc) - df_comp.to_csv( - where_patch_stack / autonumber_new_file( - where_patch_stack, f'compare_train_result_{suffix}', 'csv' - ), - index=False - ) + df_comp.to_csv(where_patch_stack / f'compare_train_result_{suffix}.csv', index=False) print(f'Generated ilastik project {ilp}') print('Truth and inferred labels match?') print(pd.value_counts(df_comp['truth_label'] == df_comp['inferred_label'])) # infer object labels from the same data used to train the classifier - infer_and_compare(new_ilp, 'before') + infer_and_compare(auto_ilp, 'before') # copy project and prompt user input once ilastik file has been modified in-app - mod_ilp = shutil.copy( - new_ilp, - where_patch_stack / autonumber_new_file(where_patch_stack, 'auto-obj', 'ilp') - ) + mod_ilp = shutil.copy(auto_ilp, where_patch_stack / 'auto_obj_after.ilp') print(f'Press enter when project file {mod_ilp} has been updated in ilastik') input() -- GitLab