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

Aborted bringing ndarray labels into data accessor superclass

parent d44b8073
No related branches found
No related tags found
No related merge requests found
......@@ -22,8 +22,13 @@ class IlastikImageToImageModel(ImageToImageModel):
if 'project_file' not in params or not os.path.exists(params['project_file']):
raise ParameterExpectedError('Ilastik model expects a project (*.ilp) file')
self.project_file = str(params['project_file'])
self.shell = None
self.operator = None
super().__init__(autoload, params)
def __del__(self):
self.shell.closeCurrentProject()
def load(self):
os.environ["LAZYFLOW_THREADS"] = "8"
os.environ["LAZYFLOW_TOTAL_RAM_MB"] = "24000"
......
......@@ -12,15 +12,27 @@ class GenericImageDataAccessor(ABC):
valid_keys = ['X', 'Y', 'C', 'Z']
def __init__(self, **kwargs):
def __init__(self, data, shape_dict, **kwargs):
"""
Abstract base class that exposes an interfaces for image data, irrespective of whether it is instantiated
from file I/O or other means.
:param kwargs: variable-length keyword arguments
"""
self._data = None
self._shape_dict = None
if shape_dict['S'] > 1 or shape_dict['T'] > 1:
raise FileShapeError(f'Cannot handle image with multiple positions or time points: {shape_dict}')
if any([k not in self.valid_keys for k in shape_dict.keys()]):
raise InvalidAxisKey(f'Invalid keys: {shape_dict.keys}')
self._shape_dict = shape_dict
self._data = data
self.data = np.moveaxis(
cf.asarray(),
[cf.axes.index(ch) for ch in self.shape_dict],
[0, 1, 2, 3]
).squeeze()
@property
def chroma(self):
......@@ -31,23 +43,31 @@ class GenericImageDataAccessor(ABC):
@property
def data(self):
return self._data
return np.moveaxis(
cf.asarray(),
[cf.axes.index(ch) for ch in self.shape_dict],
[0, 1, 2, 3]
).squeeze()
@data.setter
def data(self, data_arg):
if len(data_arg.shape) > 4:
raise InvalidDataShape(f'Invalid data shape: {data_arg.shape}')
self._data = data_arg
# @data.setter
# def data(self, data_arg):
# if len(data_arg.shape) > 4:
# raise InvalidDataShape(f'Invalid data shape: {data_arg.shape}')
# self._data = data_arg
@property
def shape_dict(self):
return self._shape_dict
@shape_dict.setter
def shape_dict(self, dict_arg: Dict[str, int]):
if any([k not in self.valid_keys for k in dict_arg.keys()]):
raise InvalidAxisKey(f'Invalid keys: {dict_arg}')
self._shape_dict = dict_arg
# @shape_dict.setter
# def shape_dict(self, dict_arg: Dict[str, int]):
# if any([k not in self.valid_keys for k in dict_arg.keys()]):
# raise InvalidAxisKey(f'Invalid keys: {dict_arg}')
# self._shape_dict = dict_arg
def get_one_channel(self):
ci = self.shape_dict['C']
return self.data[]
class GenericImageFileAccessor(GenericImageDataAccessor): # image data is loaded from a file
def __init__(self, fpath: Path, **kwargs):
......@@ -74,16 +94,19 @@ class CziImageFileAccessor(GenericImageFileAccessor):
except:
raise FileAccessorError(f'Unable to access data in {fpath}')
sd = {ch: cf.shape[cf.axes.index(ch)] for ch in cf.axes}
if sd['S'] > 1 or sd['T'] > 1:
raise FileShapeError(f'Cannot handle image with multiple positions or time points: {sd}')
self.shape_dict = {k: sd[k] for k in ['X', 'Y', 'C', 'Z']}
self.data = np.moveaxis(
cf.asarray(),
[cf.axes.index(ch) for ch in self.shape_dict],
[0, 1, 2, 3]
).squeeze()
self.shape_dict = {ch: cf.shape[cf.axes.index(ch)] for ch in cf.axes}
self.data = cf.asarray()
# sd = {ch: cf.shape[cf.axes.index(ch)] for ch in cf.axes}
# if sd['S'] > 1 or sd['T'] > 1:
# raise FileShapeError(f'Cannot handle image with multiple positions or time points: {sd}')
#
# self.shape_dict = {k: sd[k] for k in ['X', 'Y', 'C', 'Z']}
# self.data = np.moveaxis(
# cf.asarray(),
# [cf.axes.index(ch) for ch in self.shape_dict],
# [0, 1, 2, 3]
# ).squeeze()
......
......@@ -7,13 +7,17 @@ class TestIlastikPixelClassification(unittest.TestCase):
def setUp(self) -> None:
self.cf = CziImageFileAccessor(czifile['path'])
# def tearDown(self) -> None:
# pass
def test_faulthandler(self): # recreate error that is messing up ilastik
import io
import sys
import faulthandler
print(sys.stdout)
print(sys.stderr)
faulthandler.enable(file=sys.stdout)
with self.assertRaises(io.UnsupportedOperation):
faulthandler.enable(file=sys.stdout)
def test_instantiate_pixel_classifier(self):
model = IlastikPixelClassifierModel({'project_file': ilastik['pixel_classifier']})
import unittest
from conf.testing import czifile
from model_server.image import CziImageFileAccessor
from conf.testing import czifile, output_path
from model_server.image import CziImageFileAccessor, WriteableTiffFileAccessor
class TestCziImageFileAccess(unittest.TestCase):
def setUp(self) -> None:
......@@ -11,4 +11,12 @@ class TestCziImageFileAccess(unittest.TestCase):
self.assertEqual(cf.shape_dict['Y'], czifile['h'])
self.assertEqual(cf.shape_dict['X'], czifile['w'])
self.assertEqual(cf.chroma, czifile['c'])
self.assertFalse(cf.is_3d())
\ No newline at end of file
self.assertFalse(cf.is_3d())
def test_write_single_channel_tif(self):
ch = 4
cf = CziImageFileAccessor(czifile['path'])
of = WriteableTiffFileAccessor(
output_path / f'{cf.fpath.stem}_ch{ch}.tif'
)
of.write(cf.data[])
\ No newline at end of file
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