Newer
Older
from conf.testing import output_path
from extensions.chaeo.conf.testing import multichannel_zstack, pixel_classifier, pipeline_params
from extensions.chaeo.products import export_patches_from_zstack, export_multichannel_patches_from_zstack, export_patch_masks_from_zstack
from extensions.chaeo.zmask import build_zmask_from_object_mask
from model_server.accessors import generate_file_accessor, InMemoryDataAccessor, write_accessor_data_to_file
from extensions.ilastik.models import IlastikPixelClassifierModel
class TestZStackDerivedDataProducts(unittest.TestCase):
def setUp(self) -> None:
# need test data incl obj map
self.stack = generate_file_accessor(multichannel_zstack['path'])
pxmodel = IlastikPixelClassifierModel(
{'project_file': pixel_classifier['path']},
mip = InMemoryDataAccessor(self.stack.get_one_channel_data(channel=0).data.max(axis=-1, keepdims=True))
self.pxmap, result = pxmodel.infer(mip)

Christopher Randolph Rhodes
committed
# write_accessor_data_to_file(output_path / 'pxmap.tif', self.pxmap)
self.obmap = InMemoryDataAccessor(self.pxmap.data > pipeline_params['threshold'])

Christopher Randolph Rhodes
committed
# write_accessor_data_to_file(output_path / 'obmap.tif', self.obmap)
def test_zmask_makes_correct_boxes(self, mask_type='boxes', **kwargs):

Christopher Randolph Rhodes
committed
zmask, meta, df, interm = build_zmask_from_object_mask(
self.obmap.get_one_channel_data(0),
self.stack.get_one_channel_data(0),

Christopher Randolph Rhodes
committed
mask_type=mask_type,
)
zmask_acc = InMemoryDataAccessor(zmask)
self.assertTrue(zmask_acc.is_mask())
# assert dimensionality of zmask
self.assertGreater(zmask_acc.shape_dict['Z'], 1)
self.assertEqual(zmask_acc.shape_dict['C'], 1)
write_accessor_data_to_file(output_path / 'zmask.tif', zmask_acc)
# mask values are not just all True or all False
self.assertTrue(np.any(zmask))
self.assertFalse(np.all(zmask))
# assert non-trivial meta info in boxes
self.assertGreater(len(meta), 1)
sh = meta[1]['mask'].shape
ar = meta[1]['info'].area
self.assertGreaterEqual(sh[0] * sh[1], ar)

Christopher Randolph Rhodes
committed
# assert dimensionality of intermediate data products
self.assertEqual(interm['label_map'].shape, zmask.shape[0:2])
self.assertEqual(interm['argmax'].shape, zmask.shape[0:2])
return zmask, meta
def test_zmask_works_on_non_zstacks(self, **kwargs):
acc_zstack_slice = InMemoryDataAccessor(self.stack.data[:, :, 0, 0])
self.assertEqual(acc_zstack_slice.nz, 1)
zmask, meta, df, interm = build_zmask_from_object_mask(
self.obmap.get_one_channel_data(0),
acc_zstack_slice,
mask_type='boxes',
**kwargs,
)
zmask_acc = InMemoryDataAccessor(zmask)
self.assertTrue(zmask_acc.is_mask())
def test_zmask_makes_correct_contours(self):

Christopher Randolph Rhodes
committed
return self.test_zmask_makes_correct_boxes(mask_type='contours')
def test_zmask_makes_correct_boxes_with_filters(self):
return self.test_zmask_makes_correct_boxes(filters={'area': (1e3, 1e4)})
def test_zmask_makes_correct_expanded_boxes(self):
return self.test_zmask_makes_correct_boxes(expand_box_by=(64, 2))
def test_make_2d_patches_from_zmask(self):
zmask, meta = self.test_zmask_makes_correct_boxes(
filters={'area': (1e3, 1e4)},
expand_box_by=(64, 2)
)

Christopher Randolph Rhodes
committed
files = export_patches_from_zstack(
output_path / '2d_patches',
self.stack.get_one_channel_data(channel=1),
meta,
draw_bounding_box=True,
)
self.assertGreaterEqual(len(files), 1)
def test_make_3d_patches_from_zmask(self):
zmask, meta = self.test_zmask_makes_correct_boxes(
filters={'area': (1e3, 1e4)},
expand_box_by=(64, 2),
)

Christopher Randolph Rhodes
committed
files = export_patches_from_zstack(
output_path / '3d_patches',
self.stack.get_one_channel_data(4),
meta,
make_3d=True)
self.assertGreaterEqual(len(files), 1)

Christopher Randolph Rhodes
committed
def test_flatten_image(self):
zmask, meta, df, interm = build_zmask_from_object_mask(
self.obmap.get_one_channel_data(0),
self.stack.get_one_channel_data(4),

Christopher Randolph Rhodes
committed
mask_type='boxes',
)
from extensions.chaeo.zmask import project_stack_from_focal_points
dff = df[df['keeper']]

Christopher Randolph Rhodes
committed
img = project_stack_from_focal_points(
dff['centroid-0'].to_numpy(),
dff['centroid-1'].to_numpy(),
dff['zi'].to_numpy(),
self.stack,
degree=4,

Christopher Randolph Rhodes
committed
)
self.assertEqual(img.shape[0:2], self.stack.shape[0:2])
write_accessor_data_to_file(
output_path / 'flattened.tif',
InMemoryDataAccessor(img)
)
def test_make_multichannel_2d_patches_from_zmask(self):
zmask, meta = self.test_zmask_makes_correct_boxes(
filters={'area': (1e3, 1e4)},
expand_box_by=(128, 2)
)
files = export_multichannel_patches_from_zstack(

Christopher Randolph Rhodes
committed
output_path / '2d_patches_chlorophyl_bbox_overlay',
InMemoryDataAccessor(self.stack.data),
meta,
ch_white=4,
draw_bounding_box=True,
bounding_box_channel=1,

Christopher Randolph Rhodes
committed
)
self.assertGreaterEqual(len(files), 1)
def test_make_multichannel_2d_patches_with_mask_overlay(self):
zmask, meta = self.test_zmask_makes_correct_boxes(
filters={'area': (1e3, 1e4)},
expand_box_by=(128, 2)
)

Christopher Randolph Rhodes
committed
files = export_multichannel_patches_from_zstack(
output_path / '2d_patches_chlorophyl_mask_overlay',
InMemoryDataAccessor(self.stack.data),
meta,
ch_white=4,
ch_rgb_overlay=(3, None, None),
draw_mask=True,
mask_channel=0,
overlay_gain=(0.1, 1.0, 1.0)
)
self.assertGreaterEqual(len(files), 1)
def test_make_multichannel_2d_patches_with_contour_overlay(self):
zmask, meta = self.test_zmask_makes_correct_boxes(
filters={'area': (1e3, 1e4)},
expand_box_by=(128, 2)
)

Christopher Randolph Rhodes
committed
files = export_multichannel_patches_from_zstack(
output_path / '2d_patches_chlorophyl_contour_overlay',
InMemoryDataAccessor(self.stack.data),
meta,
ch_white=4,
ch_rgb_overlay=(3, None, None),
draw_contour=True,
contour_channel=1,
overlay_gain=(0.1, 1.0, 1.0)
)
self.assertGreaterEqual(len(files), 1)
def test_make_binary_masks_from_zmask(self):
zmask, meta = self.test_zmask_makes_correct_boxes(
filters={'area': (1e3, 1e4)},
expand_box_by=(128, 2)
)
files = export_patch_masks_from_zstack(
output_path / '2d_mask_patches',
InMemoryDataAccessor(self.stack.data),
meta,
)
self.assertGreaterEqual(len(files), 1)