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 97330c76988641d4c9b5f327458f615c408452d1..3b96805354ac6b1949a443ffc7771dea2ac13391 100644 --- a/extensions/chaeo/examples/transfer_labels_to_ilastik_object_classifier.py +++ b/extensions/chaeo/examples/transfer_labels_to_ilastik_object_classifier.py @@ -54,6 +54,7 @@ def infer_and_compare(classifier: PatchStackObjectClassifier, prefix, raw, mask, print('Truth and inferred labels match?') print(pd.value_counts(df_comp['truth_label'] == df_comp['inferred_label'])) + if __name__ == '__main__': root = Path('c:/Users/rhodes/projects/proj0011-plankton-seg/exp0009/output/labeled_patches-20231030-0002') template_ilp = Path('c:/Users/rhodes/projects/proj0011-plankton-seg/exp0014/template_obj.ilp') @@ -67,9 +68,9 @@ if __name__ == '__main__': classifier_file = generate_ilastik_object_classifier( template_ilp, root / 'new_auto_obj.ilp', - root / 'zstack_train_raw.tif', - root / 'zstack_train_mask.tif', - root / 'zstack_train_label.tif', + MonoPatchStackFromFile(root / 'zstack_train_raw.tif'), + MonoPatchStackFromFile(root / 'zstack_train_mask.tif'), + MonoPatchStackFromFile(root / 'zstack_train_label.tif'), label_names, ) classifier = PatchStackObjectClassifier({'project_file': classifier_file}) diff --git a/extensions/chaeo/models.py b/extensions/chaeo/models.py index 9e209be21f89e491a343b6ed3352154763691032..dc2c9cf04bd23766b2e1d51feaab610270f9fc1f 100644 --- a/extensions/chaeo/models.py +++ b/extensions/chaeo/models.py @@ -7,7 +7,6 @@ import vigra from extensions.chaeo.accessors import MonoPatchStack, MonoPatchStackFromFile from extensions.ilastik.models import IlastikObjectClassifierFromSegmentationModel -from model_server.accessors import InMemoryDataAccessor class PatchStackObjectClassifier(IlastikObjectClassifierFromSegmentationModel): @@ -50,9 +49,9 @@ class PatchStackObjectClassifier(IlastikObjectClassifierFromSegmentationModel): def generate_ilastik_object_classifier( template_ilp: Path, target_ilp: Path, - raw_tif: Path, - mask_tif: Path, - label_tif: Path, + raw_stack: MonoPatchStackFromFile, + mask_stack: MonoPatchStackFromFile, + label_stack: MonoPatchStackFromFile, label_names: list, lane: int = 0, ) -> Path: @@ -60,27 +59,27 @@ def generate_ilastik_object_classifier( Starting with a template project file, transfer input data and labels to a new project file. :param template_ilp: path to existing ilastik object classifier to use as a template :param target_ilp: path to new classifier - :param raw_tif: path to stack of patches containing raw data - :param mask_tif: path to stack of patches containing object masks - :param label_tif: path to stack of patches containing object labels + :param raw_stack: stack of patches containing raw data + :param mask_stack: stack of patches containing object masks + :param label_stack: stack of patches containing object labels :param label_names: list of label names :param lane: ilastik lane identifier :return: path to generated object classifier """ + assert mask_stack.shape == raw_stack.shape + assert label_stack.shape == raw_stack.shape + new_ilp = shutil.copy(template_ilp, target_ilp) - paths = { - 'Raw Data': raw_tif, - 'Segmentation Image': mask_tif, + accessors = { + 'Raw Data': raw_stack, + 'Segmentation Image': mask_stack, } - root = raw_tif.parent - accessors = {k: MonoPatchStackFromFile(root / pa) for k, pa in paths.items()} # get labels from label image - acc_labels = MonoPatchStackFromFile(label_tif) labels = [] - for ii in range(0, acc_labels.count): - unique = np.unique(acc_labels.iat(ii)) + for ii in range(0, label_stack.count): + unique = np.unique(label_stack.iat(ii)) assert len(unique) >= 2, 'Label image contains more than one non-zero value' assert unique[0] == 0, 'Label image does not contain unlabeled background' assert unique[-1] < len(label_names) + 1, f'Label ID {unique[-1]} exceeds number of label names: {len(label_names)}' @@ -94,14 +93,14 @@ def generate_ilastik_object_classifier( # set path to input image files del h5[f'{group}/filePath'] - h5[f'{group}/filePath'] = paths[gk].name + h5[f'{group}/filePath'] = accessors[gk].fpath.name assert not Path(h5[f'{group}/filePath'][()].decode()).is_absolute() - assert h5[f'{group}/filePath'][()] == paths[gk].name.encode() + assert h5[f'{group}/filePath'][()] == accessors[gk].fpath.name.encode() assert h5[f'{group}/location'][()] == 'FileSystem'.encode() # set input nickname del h5[f'{group}/nickname'] - h5[f'{group}/nickname'] = paths[gk].stem + h5[f'{group}/nickname'] = accessors[gk].fpath.stem # set input shape del h5[f'{group}/shape']