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 d780b4a57a938073b121415e787ea59c576afd0b..d723ee7af51bb1f401c2622c2bd26d2ff6d51afc 100644 --- a/extensions/chaeo/examples/transfer_labels_to_ilastik_object_classifier.py +++ b/extensions/chaeo/examples/transfer_labels_to_ilastik_object_classifier.py @@ -8,41 +8,11 @@ import skimage import uuid from extensions.chaeo.accessors import MonoPatchStackFromFile +from extensions.chaeo.h5util import get_dataset_info from extensions.chaeo.models import PatchStackObjectClassifier from model_server.accessors import generate_file_accessor, GenericImageDataAccessor, write_accessor_data_to_file -def get_dataset_info(h5: h5py.File, lane : int = 0): - """ - Report out specific datasets in ilastik project file HDF5 - :param h5: handle to ilastik project file, as h5py.File object - :param lane: ilastik lane identifier - :return: (dict) selected data values from project file - """ - lns = f'{lane:04d}' - lane = f'Input Data/infos/lane{lns}' - info = {} - for gk in ['Raw Data', 'Segmentation Image']: - info[gk] = {} - for dk in ['location', 'filePath', 'shape', 'nickname']: - try: - info[gk][dk] = h5[f'{lane}/{gk}/{dk}'][()] - except Exception as e: - print(e) - try: - info[gk]['id'] = uuid.UUID(h5[f'{lane}/{gk}/datasetId'][()].decode()) - except ValueError as e: - info[gk]['id'] = '<invalid UUID>' - info[gk]['axistags'] = json.loads(h5[f'{lane}/{gk}/axistags'][()].decode()) - info[gk]['axes'] = [ax['key'] for ax in info[gk]['axistags']['axes']] - - obj_cl_group = h5[f'ObjectClassification/LabelInputs/{lns}'] - info['misc'] = { - 'number_of_label_inputs': len(obj_cl_group.items()) - } - return info - - def generate_ilastik_object_classifier( template_ilp: str, where: str, @@ -180,15 +150,16 @@ 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-0006' + where_patch_stack = root / 'exp0009/output/labeled_patches-20231030-0001' # auto-populate an object classifier - auto_ilp = generate_ilastik_object_classifier( - template_ilp, - where_patch_stack, - stack_name='train', - proj_name='auto_obj_before' - ) + # auto_ilp = generate_ilastik_object_classifier( + # template_ilp, + # where_patch_stack, + # stack_name='train', + # proj_name='auto_obj_before' + # ) + auto_ilp = 'auto_obj_before.ilp' def infer_and_compare_training_set(ilp, suffix): # infer object labels from the same data used to train the classifier diff --git a/extensions/chaeo/h5util.py b/extensions/chaeo/h5util.py new file mode 100644 index 0000000000000000000000000000000000000000..0b50813266eda04fe2e218805a545a48f510c32c --- /dev/null +++ b/extensions/chaeo/h5util.py @@ -0,0 +1,34 @@ +import json +import uuid + +import h5py + +def get_dataset_info(h5: h5py.File, lane : int = 0): + """ + Report out specific datasets in ilastik project file HDF5 + :param h5: handle to ilastik project file, as h5py.File object + :param lane: ilastik lane identifier + :return: (dict) selected data values from project file + """ + lns = f'{lane:04d}' + lane = f'Input Data/infos/lane{lns}' + info = {} + for gk in ['Raw Data', 'Segmentation Image']: + info[gk] = {} + for dk in ['location', 'filePath', 'shape', 'nickname']: + try: + info[gk][dk] = h5[f'{lane}/{gk}/{dk}'][()] + except Exception as e: + print(e) + try: + info[gk]['id'] = uuid.UUID(h5[f'{lane}/{gk}/datasetId'][()].decode()) + except ValueError as e: + info[gk]['id'] = '<invalid UUID>' + info[gk]['axistags'] = json.loads(h5[f'{lane}/{gk}/axistags'][()].decode()) + info[gk]['axes'] = [ax['key'] for ax in info[gk]['axistags']['axes']] + + obj_cl_group = h5[f'ObjectClassification/LabelInputs/{lns}'] + info['misc'] = { + 'number_of_label_inputs': len(obj_cl_group.items()) + } + return info \ No newline at end of file