From f7c02a0a7fc2e3971470f38752f5eb5e248f14de Mon Sep 17 00:00:00 2001 From: Christopher Rhodes <christopher.rhodes@embl.de> Date: Fri, 27 Oct 2023 11:35:52 +0200 Subject: [PATCH] Moved patch stack-specific ilastik model --- ...fer_labels_to_ilastik_object_classifier.py | 42 ++---------------- extensions/chaeo/models.py | 43 +++++++++++++++++++ 2 files changed, 46 insertions(+), 39 deletions(-) create mode 100644 extensions/chaeo/models.py 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 9e07ec2b..d780b4a5 100644 --- a/extensions/chaeo/examples/transfer_labels_to_ilastik_object_classifier.py +++ b/extensions/chaeo/examples/transfer_labels_to_ilastik_object_classifier.py @@ -6,47 +6,11 @@ import numpy as np import pandas as pd import skimage import uuid -import vigra -from extensions.chaeo.accessors import MonoPatchStack, MonoPatchStackFromFile -from extensions.ilastik.models import IlastikObjectClassifierFromSegmentationModel -from model_server.accessors import generate_file_accessor, GenericImageDataAccessor, InMemoryDataAccessor, write_accessor_data_to_file +from extensions.chaeo.accessors import MonoPatchStackFromFile +from extensions.chaeo.models import PatchStackObjectClassifier +from model_server.accessors import generate_file_accessor, GenericImageDataAccessor, write_accessor_data_to_file -class PatchStackObjectClassifier(IlastikObjectClassifierFromSegmentationModel): - """ - Wrap ilastik object classification for inputs comprising raw image and binary segmentation masks, both represented - as time-series images where each frame contains only one object. - """ - - - def infer(self, input_acc: MonoPatchStack, segmentation_acc: MonoPatchStack) -> (np.ndarray, dict): - assert segmentation_acc.is_mask() - assert input_acc.chroma == 1 - - tagged_input_data = vigra.taggedView(input_acc.make_tczyx(), 'tczyx') - tagged_seg_data = vigra.taggedView(segmentation_acc.make_tczyx(), 'tczyx') - - dsi = [ - { - 'Raw Data': self.PreloadedArrayDatasetInfo(preloaded_array=tagged_input_data), - 'Segmentation Image': self.PreloadedArrayDatasetInfo(preloaded_array=tagged_seg_data), - } - ] - - obmaps = self.shell.workflow.batchProcessingApplet.run_export(dsi, export_to_array=True) # [z x h x w x n] - - assert len(obmaps) == 1, 'ilastik generated more than one object map' - - # for some reason ilastik scrambles these axes to Z(1)YX(1) - assert obmaps[0].shape == (input_acc.nz, 1, input_acc.hw[0], input_acc.hw[1], 1) - yxcz = np.moveaxis( - obmaps[0][:, :, :, :, 0], - [2, 3, 1, 0], - [0, 1, 2, 3] - ) - - assert yxcz.shape == input_acc.shape - return InMemoryDataAccessor(data=yxcz), {'success': True} def get_dataset_info(h5: h5py.File, lane : int = 0): """ diff --git a/extensions/chaeo/models.py b/extensions/chaeo/models.py new file mode 100644 index 00000000..5900134e --- /dev/null +++ b/extensions/chaeo/models.py @@ -0,0 +1,43 @@ +import numpy as np +import vigra + +from extensions.chaeo.accessors import MonoPatchStack +from extensions.ilastik.models import IlastikObjectClassifierFromSegmentationModel +from model_server.accessors import InMemoryDataAccessor + + +class PatchStackObjectClassifier(IlastikObjectClassifierFromSegmentationModel): + """ + Wrap ilastik object classification for inputs comprising raw image and binary segmentation masks, both represented + as time-series images where each frame contains only one object. + """ + + + def infer(self, input_acc: MonoPatchStack, segmentation_acc: MonoPatchStack) -> (np.ndarray, dict): + assert segmentation_acc.is_mask() + assert input_acc.chroma == 1 + + tagged_input_data = vigra.taggedView(input_acc.make_tczyx(), 'tczyx') + tagged_seg_data = vigra.taggedView(segmentation_acc.make_tczyx(), 'tczyx') + + dsi = [ + { + 'Raw Data': self.PreloadedArrayDatasetInfo(preloaded_array=tagged_input_data), + 'Segmentation Image': self.PreloadedArrayDatasetInfo(preloaded_array=tagged_seg_data), + } + ] + + obmaps = self.shell.workflow.batchProcessingApplet.run_export(dsi, export_to_array=True) # [z x h x w x n] + + assert len(obmaps) == 1, 'ilastik generated more than one object map' + + # for some reason ilastik scrambles these axes to Z(1)YX(1) + assert obmaps[0].shape == (input_acc.nz, 1, input_acc.hw[0], input_acc.hw[1], 1) + yxcz = np.moveaxis( + obmaps[0][:, :, :, :, 0], + [2, 3, 1, 0], + [0, 1, 2, 3] + ) + + assert yxcz.shape == input_acc.shape + return InMemoryDataAccessor(data=yxcz), {'success': True} \ No newline at end of file -- GitLab