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

Added generic Tif accessor

parent 67e20ac1
No related branches found
No related tags found
No related merge requests found
......@@ -12,6 +12,16 @@ czifile = {
'z': 1,
}
filename = 'zmask-test-stack.tif'
tifffile = {
'filename': filename,
'path': root / filename,
'w': 512,
'h': 512,
'c': 2,
'z': 7,
}
ilastik = {
'pixel_classifier': 'demo_px.ilp',
'object_classifier': 'demo_obj.ilp',
......
......@@ -67,6 +67,34 @@ class GenericImageFileAccessor(GenericImageDataAccessor): # image data is loaded
raise FileAccessorError(f'Could not find file at {fpath}')
self.fpath = fpath
class TifSingleSeriesFileAccessor(GenericImageFileAccessor):
def __init__(self, fpath: Path):
super().__init__(fpath)
try:
tf = tifffile.TiffFile(fpath)
self.tf = tf
except Exception:
FileAccessorError(f'Unable to access data in {fpath}')
if len(tf.series) != 1:
raise DataShapeError(f'Expect only one series in {fpath}')
se = tf.series[0]
sd = {ch: se.shape[se.axes.index(ch)] for ch in se.axes}
idx = {k: sd[k] for k in ['Y', 'X', 'C', 'Z']}
yxcz = np.moveaxis(
se.asarray(),
[se.axes.index(ch) for ch in idx],
[0, 1, 2, 3]
)
self._data = self.conform_data(yxcz.reshape(yxcz.shape[0:4]))
def __del__(self):
self.tf.close()
class CziImageFileAccessor(GenericImageFileAccessor):
"""
Image that is stored in a Zeiss .CZI file; may be multi-channel, and/or a z-stack,
......
......@@ -2,14 +2,24 @@ import unittest
import numpy as np
from conf.testing import czifile, output_path
from model_server.accessors import CziImageFileAccessor, DataShapeError, InMemoryDataAccessor, write_accessor_data_to_file
from conf.testing import czifile, output_path, tifffile
from model_server.accessors import CziImageFileAccessor, DataShapeError, InMemoryDataAccessor, write_accessor_data_to_file, TifSingleSeriesFileAccessor
class TestCziImageFileAccess(unittest.TestCase):
def setUp(self) -> None:
pass
def test_tiffile_is_correct_shape(self):
tf = TifSingleSeriesFileAccessor(tifffile['path'])
self.assertEqual(tf.shape_dict['Y'], tifffile['h'])
self.assertEqual(tf.shape_dict['X'], tifffile['w'])
self.assertEqual(tf.chroma, tifffile['c'])
self.assertTrue(tf.is_3d())
self.assertEqual(len(tf.data.shape), 4)
self.assertEqual(tf.shape[0], tifffile['h'])
self.assertEqual(tf.shape[1], tifffile['w'])
def test_czifile_is_correct_shape(self):
cf = CziImageFileAccessor(czifile['path'])
self.assertEqual(cf.shape_dict['Y'], czifile['h'])
......
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