From 4da7a11950c9e60f800b714db977d5dd90c8d8dd Mon Sep 17 00:00:00 2001 From: Christopher Rhodes <christopher.rhodes@embl.de> Date: Mon, 30 Oct 2023 10:19:25 +0100 Subject: [PATCH] Clean test showing that modified HDF5 isn't providing correct results --- .../examples/test_modify_object_classifier.py | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 extensions/chaeo/examples/test_modify_object_classifier.py diff --git a/extensions/chaeo/examples/test_modify_object_classifier.py b/extensions/chaeo/examples/test_modify_object_classifier.py new file mode 100644 index 00000000..2cabba44 --- /dev/null +++ b/extensions/chaeo/examples/test_modify_object_classifier.py @@ -0,0 +1,53 @@ +from pathlib import Path +import shutil + +import h5py +import numpy as np + +from extensions.chaeo.accessors import MonoPatchStack, MonoPatchStackFromFile +from extensions.chaeo.models import PatchStackObjectClassifier + +def modify_ilastik_object_classifier(template_ilp: Path, lane=0): + where = template_ilp.parent + proj_name = template_ilp.stem + new_ilp = shutil.copy(template_ilp, where / ('mod_' + proj_name + '.ilp')) + + with h5py.File(where / new_ilp, 'r+') as h5: + + # delete ObjectExtraction / RegionFeatures / 0000 + # delete ObjectClassification/ClassifierForests + for key in [f'ObjectExtraction/RegionFeatures/{lane:04d}', 'ObjectExtraction/ClassifierForests']: + if (key) in h5.keys(): + del h5[key] + assert key not in h5.keys() + + # make links to input data relative + for gk in ['Raw Data', 'Segmentation Image']: + key = f'Input Data/infos/lane{lane:04d}/{gk}' + assert h5[f'{key}/location'][()] == 'FileSystem'.encode() + pa = Path(h5[f'{key}/filePath'][()].decode()) + assert pa.is_absolute() + rel_pa = pa.name + assert (where / rel_pa).exists() + del h5[f'{key}/filePath'] + h5[f'{key}/filePath'] = rel_pa + assert h5[f'{key}/filePath'][()] == rel_pa.encode() + + return new_ilp + + +if __name__ == '__main__': + where = Path('c:/Users/rhodes/projects/proj0011-plankton-seg/exp0009/output/labeled_patches-20231030-0001') + where_ilp = where / 'auto_obj_before.ilp' + new_ilp = modify_ilastik_object_classifier(where_ilp) + model = PatchStackObjectClassifier({'project_file': where / new_ilp}) + + raw = MonoPatchStackFromFile(where / 'zstack_train_raw.tif') + mask = MonoPatchStackFromFile(where / 'zstack_train_mask.tif') + labels = MonoPatchStackFromFile(where / f'zstack_train_label.tif') + + result, _ = model.infer(raw, mask) + assert isinstance(result, MonoPatchStack) + + print(f'Unique labels in truth set: {np.unique(labels.data)}') + print(f'Unique labels in inferred set: {np.unique(result.data)}') \ No newline at end of file -- GitLab