Skip to content
Snippets Groups Projects
Commit f6efd5db authored by Christopher Randolph Rhodes's avatar Christopher Randolph Rhodes
Browse files

Pass accessors, not paths, to model generator

parent 72b15b18
No related branches found
No related tags found
No related merge requests found
...@@ -54,6 +54,7 @@ def infer_and_compare(classifier: PatchStackObjectClassifier, prefix, raw, mask, ...@@ -54,6 +54,7 @@ def infer_and_compare(classifier: PatchStackObjectClassifier, prefix, raw, mask,
print('Truth and inferred labels match?') print('Truth and inferred labels match?')
print(pd.value_counts(df_comp['truth_label'] == df_comp['inferred_label'])) print(pd.value_counts(df_comp['truth_label'] == df_comp['inferred_label']))
if __name__ == '__main__': if __name__ == '__main__':
root = Path('c:/Users/rhodes/projects/proj0011-plankton-seg/exp0009/output/labeled_patches-20231030-0002') 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') template_ilp = Path('c:/Users/rhodes/projects/proj0011-plankton-seg/exp0014/template_obj.ilp')
...@@ -67,9 +68,9 @@ if __name__ == '__main__': ...@@ -67,9 +68,9 @@ if __name__ == '__main__':
classifier_file = generate_ilastik_object_classifier( classifier_file = generate_ilastik_object_classifier(
template_ilp, template_ilp,
root / 'new_auto_obj.ilp', root / 'new_auto_obj.ilp',
root / 'zstack_train_raw.tif', MonoPatchStackFromFile(root / 'zstack_train_raw.tif'),
root / 'zstack_train_mask.tif', MonoPatchStackFromFile(root / 'zstack_train_mask.tif'),
root / 'zstack_train_label.tif', MonoPatchStackFromFile(root / 'zstack_train_label.tif'),
label_names, label_names,
) )
classifier = PatchStackObjectClassifier({'project_file': classifier_file}) classifier = PatchStackObjectClassifier({'project_file': classifier_file})
......
...@@ -7,7 +7,6 @@ import vigra ...@@ -7,7 +7,6 @@ import vigra
from extensions.chaeo.accessors import MonoPatchStack, MonoPatchStackFromFile from extensions.chaeo.accessors import MonoPatchStack, MonoPatchStackFromFile
from extensions.ilastik.models import IlastikObjectClassifierFromSegmentationModel from extensions.ilastik.models import IlastikObjectClassifierFromSegmentationModel
from model_server.accessors import InMemoryDataAccessor
class PatchStackObjectClassifier(IlastikObjectClassifierFromSegmentationModel): class PatchStackObjectClassifier(IlastikObjectClassifierFromSegmentationModel):
...@@ -50,9 +49,9 @@ class PatchStackObjectClassifier(IlastikObjectClassifierFromSegmentationModel): ...@@ -50,9 +49,9 @@ class PatchStackObjectClassifier(IlastikObjectClassifierFromSegmentationModel):
def generate_ilastik_object_classifier( def generate_ilastik_object_classifier(
template_ilp: Path, template_ilp: Path,
target_ilp: Path, target_ilp: Path,
raw_tif: Path, raw_stack: MonoPatchStackFromFile,
mask_tif: Path, mask_stack: MonoPatchStackFromFile,
label_tif: Path, label_stack: MonoPatchStackFromFile,
label_names: list, label_names: list,
lane: int = 0, lane: int = 0,
) -> Path: ) -> Path:
...@@ -60,27 +59,27 @@ def generate_ilastik_object_classifier( ...@@ -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. 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 template_ilp: path to existing ilastik object classifier to use as a template
:param target_ilp: path to new classifier :param target_ilp: path to new classifier
:param raw_tif: path to stack of patches containing raw data :param raw_stack: stack of patches containing raw data
:param mask_tif: path to stack of patches containing object masks :param mask_stack: stack of patches containing object masks
:param label_tif: path to stack of patches containing object labels :param label_stack: stack of patches containing object labels
:param label_names: list of label names :param label_names: list of label names
:param lane: ilastik lane identifier :param lane: ilastik lane identifier
:return: path to generated object classifier :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) new_ilp = shutil.copy(template_ilp, target_ilp)
paths = { accessors = {
'Raw Data': raw_tif, 'Raw Data': raw_stack,
'Segmentation Image': mask_tif, 'Segmentation Image': mask_stack,
} }
root = raw_tif.parent
accessors = {k: MonoPatchStackFromFile(root / pa) for k, pa in paths.items()}
# get labels from label image # get labels from label image
acc_labels = MonoPatchStackFromFile(label_tif)
labels = [] labels = []
for ii in range(0, acc_labels.count): for ii in range(0, label_stack.count):
unique = np.unique(acc_labels.iat(ii)) unique = np.unique(label_stack.iat(ii))
assert len(unique) >= 2, 'Label image contains more than one non-zero value' 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[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)}' 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( ...@@ -94,14 +93,14 @@ def generate_ilastik_object_classifier(
# set path to input image files # set path to input image files
del h5[f'{group}/filePath'] 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 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() assert h5[f'{group}/location'][()] == 'FileSystem'.encode()
# set input nickname # set input nickname
del h5[f'{group}/nickname'] del h5[f'{group}/nickname']
h5[f'{group}/nickname'] = paths[gk].stem h5[f'{group}/nickname'] = accessors[gk].fpath.stem
# set input shape # set input shape
del h5[f'{group}/shape'] del h5[f'{group}/shape']
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment