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 f3b5a5bc01bfc8c5ffa7ab3206adaf87ffac32be..5aa8e6fcec9b9a870b09d224e253c54feeb9ed99 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 @@ -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-0002' # 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()