From 7961492a17f1d5118e6e5581a0e064ad02a300fd Mon Sep 17 00:00:00 2001 From: Christopher Rhodes <christopher.rhodes@embl.de> Date: Fri, 5 Jul 2024 12:29:36 +0200 Subject: [PATCH] Call test data in a big dictionary --- tests/base/_conf.py | 107 ----------------------------------- tests/base/conf.py | 106 ++++++++++++++++++++++++++++++++++ tests/base/test_accessors.py | 52 +++++++++-------- 3 files changed, 133 insertions(+), 132 deletions(-) delete mode 100644 tests/base/_conf.py create mode 100644 tests/base/conf.py diff --git a/tests/base/_conf.py b/tests/base/_conf.py deleted file mode 100644 index 8af41966..00000000 --- a/tests/base/_conf.py +++ /dev/null @@ -1,107 +0,0 @@ -import json -import os -from os import environ -from pathlib import Path - -# places to look for test data -data_paths = [ - environ.get('UNITTEST_DATA_ROOT'), - Path.home() / 'model_server' / 'testing', - os.getcwd(), -] -dd = {} -root = None - -# look for first instance of summary.json -for dp in data_paths: - if dp is None: - continue - sf = (Path(dp) / 'summary.json') - if sf.exists(): - with open(sf, 'r') as fh: - dd = json.load(fh) - root = Path(dp) - break - -if root is None: - raise Exception('Could not find test data, try setting environmental variable UNITTEST_DATA_ROOT.') - -op_ev = environ.get('UNITTEST_OUTPUT', (root / 'test_output').__str__()) -output_path = Path(op_ev) -output_path.mkdir(parents=True, exist_ok=True) - -# TODO: organize this as a dict -# TODO: assert paths exist -# TODO: separate out ilastik stuff - - -filename = 'D3-selection-01.czi' -czifile = { - 'filename': filename, - 'path': root / filename, - 'w': 1274, - 'h': 1274, - 'c': 5, - 'z': 1, - 'um_per_pixel': 1/3.9881, -} - -filename = 'rgb.png' -rgbpngfile = { - 'filename': filename, - 'path': root / filename, - 'w': 64, - 'h': 128, - 'c': 3, - 'z': 1 -} - -filename = 'mono.png' -monopngfile = { - 'filename': filename, - 'path': root / filename, - 'w': 64, - 'h': 128, - 'c': 1, - 'z': 1 -} - -filename = 'zmask-test-stack.tif' -tifffile = { - 'filename': filename, - 'path': root / filename, - 'w': 512, - 'h': 512, - 'c': 2, - 'z': 7, -} - -filename = 'mono_zstack_mask.tif' -monozstackmask = { - 'filename': filename, - 'path': root / filename, - 'w': 256, - 'h': 256, - 'c': 1, - 'z': 85 -} - -roiset_test_data = { - 'multichannel_zstack': { - 'path': root / 'zmask-test-stack-chlorophyl.tif', - 'w': 512, - 'h': 512, - 'c': 5, - 'z': 7, - 'mask_path': root / 'zmask-test-stack-mask.tif', - 'mask_path_3d': root / 'zmask-test-stack-mask-3d.tif', - }, - 'pipeline_params': { - 'segmentation_channel': 0, - 'patches_channel': 4, - 'pxmap_channel': 0, - 'pxmap_threshold': 0.6, - }, - 'pixel_classifier': root / 'zmask' / 'AF405-bodies_boundaries.ilp', -} - diff --git a/tests/base/conf.py b/tests/base/conf.py new file mode 100644 index 00000000..44bc1ceb --- /dev/null +++ b/tests/base/conf.py @@ -0,0 +1,106 @@ +import json +import os +from os import environ +from pathlib import Path + +# places to look for test data +data_paths = [ + environ.get('UNITTEST_DATA_ROOT'), + Path.home() / 'model_server' / 'testing', + os.getcwd(), +] +root = None + +# look for first instance of summary.json +for dp in data_paths: + if dp is None: + continue + sf = (Path(dp) / 'summary.json') + if sf.exists(): + with open(sf, 'r') as fh: + meta = json.load(fh) + root = Path(dp) + break + +if root is None: + raise Exception('Could not find test data, try setting environmental variable UNITTEST_DATA_ROOT.') + +op_ev = environ.get('UNITTEST_OUTPUT', (root / 'test_output').__str__()) +output_path = Path(op_ev) +output_path.mkdir(parents=True, exist_ok=True) + +# resolve paths in test image files +imgs = meta['image_files'] +for k in imgs.keys(): + imgs[k]['path'] = root / imgs[k]['name'] + +# filename = 'D3-selection-01.czi' +# czifile = { +# 'filename': filename, +# 'path': root / filename, +# 'w': 1274, +# 'h': 1274, +# 'c': 5, +# 'z': 1, +# 'um_per_pixel': 1/3.9881, +# } +# +# filename = 'rgb.png' +# rgbpngfile = { +# 'filename': filename, +# 'path': root / filename, +# 'w': 64, +# 'h': 128, +# 'c': 3, +# 'z': 1 +# } +# +# filename = 'mono.png' +# monopngfile = { +# 'filename': filename, +# 'path': root / filename, +# 'w': 64, +# 'h': 128, +# 'c': 1, +# 'z': 1 +# } +# +# filename = 'zmask-test-stack.tif' +# tifffile = { +# 'filename': filename, +# 'path': root / filename, +# 'w': 512, +# 'h': 512, +# 'c': 2, +# 'z': 7, +# } +# +# filename = 'mono_zstack_mask.tif' +# monozstackmask = { +# 'filename': filename, +# 'path': root / filename, +# 'w': 256, +# 'h': 256, +# 'c': 1, +# 'z': 85 +# } +# +# roiset_test_data = { +# 'multichannel_zstack': { +# 'path': root / 'zmask-test-stack-chlorophyl.tif', +# 'w': 512, +# 'h': 512, +# 'c': 5, +# 'z': 7, +# 'mask_path': root / 'zmask-test-stack-mask.tif', +# 'mask_path_3d': root / 'zmask-test-stack-mask-3d.tif', +# }, +# 'pipeline_params': { +# 'segmentation_channel': 0, +# 'patches_channel': 4, +# 'pxmap_channel': 0, +# 'pxmap_threshold': 0.6, +# }, +# 'pixel_classifier': root / 'zmask' / 'AF405-bodies_boundaries.ilp', +# } + diff --git a/tests/base/test_accessors.py b/tests/base/test_accessors.py index f0d98183..f28a03a2 100644 --- a/tests/base/test_accessors.py +++ b/tests/base/test_accessors.py @@ -4,9 +4,11 @@ import numpy as np from model_server.base.accessors import PatchStack, make_patch_stack_from_file, FileNotFoundError -from tests.base._conf import czifile, output_path, monopngfile, rgbpngfile, tifffile, monozstackmask +from tests.base.conf import meta, output_path from model_server.base.accessors import CziImageFileAccessor, DataShapeError, generate_file_accessor, InMemoryDataAccessor, PngFileAccessor, write_accessor_data_to_file, TifSingleSeriesFileAccessor +data = meta['image_files'] + def _random_int(*args): return np.random.randint(0, 2 ** 8, size=args, dtype='uint8') @@ -16,27 +18,27 @@ class TestCziImageFileAccess(unittest.TestCase): pass def test_tiffile_is_correct_shape(self): - tf = generate_file_accessor(tifffile['path']) + tf = generate_file_accessor(data['tifffile']['path']) self.assertIsInstance(tf, TifSingleSeriesFileAccessor) - self.assertEqual(tf.shape_dict['Y'], tifffile['h']) - self.assertEqual(tf.shape_dict['X'], tifffile['w']) - self.assertEqual(tf.chroma, tifffile['c']) + self.assertEqual(tf.shape_dict['Y'], data['tifffile']['h']) + self.assertEqual(tf.shape_dict['X'], data['tifffile']['w']) + self.assertEqual(tf.chroma, data['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']) + self.assertEqual(tf.shape[0], data['tifffile']['h']) + self.assertEqual(tf.shape[1], data['tifffile']['w']) self.assertEqual(tf.get_axis('x'), 1) def test_czifile_is_correct_shape(self): - cf = CziImageFileAccessor(czifile['path']) - self.assertEqual(cf.shape_dict['Y'], czifile['h']) - self.assertEqual(cf.shape_dict['X'], czifile['w']) - self.assertEqual(cf.chroma, czifile['c']) + cf = CziImageFileAccessor(data['czifile']['path']) + self.assertEqual(cf.shape_dict['Y'], data['czifile']['h']) + self.assertEqual(cf.shape_dict['X'], data['czifile']['w']) + self.assertEqual(cf.chroma, data['czifile']['c']) self.assertFalse(cf.is_3d()) self.assertEqual(len(cf.data.shape), 4) - self.assertEqual(cf.shape[0], czifile['h']) - self.assertEqual(cf.shape[1], czifile['w']) + self.assertEqual(cf.shape[0], data['czifile']['h']) + self.assertEqual(cf.shape[1], data['czifile']['w']) def test_get_single_channel_from_zstack(self): w = 256 @@ -60,7 +62,7 @@ class TestCziImageFileAccess(unittest.TestCase): def test_write_single_channel_tif(self): ch = 4 - cf = CziImageFileAccessor(czifile['path']) + cf = CziImageFileAccessor(data['czifile']['path']) mono = cf.get_mono(ch) self.assertTrue( write_accessor_data_to_file( @@ -73,7 +75,7 @@ class TestCziImageFileAccess(unittest.TestCase): def test_write_two_channel_png(self): from model_server.base.process import resample_to_8bit - cf = CziImageFileAccessor(czifile['path']) + cf = CziImageFileAccessor(data['czifile']['path']) acc = cf.get_channels([0, 1]) opa = output_path / f'{cf.fpath.stem}_2ch.png' acc_out = acc.apply(resample_to_8bit) @@ -129,23 +131,23 @@ class TestCziImageFileAccess(unittest.TestCase): fh_shape_dict = {se.axes[i]: se.shape[i] for i in range(0, len(se.shape))} self.assertEqual(fh_shape_dict, acc.shape_dict, 'Axes are not preserved in TIF output') - def test_read_png(self, pngfile=rgbpngfile): + def test_read_png(self, pngfile=data['rgbpngfile']): acc = PngFileAccessor(pngfile['path']) self.assertEqual(acc.hw, (pngfile['h'], pngfile['w'])) self.assertEqual(acc.chroma, pngfile['c']) self.assertEqual(acc.nz, 1) def test_read_mono_png(self): - return self.test_read_png(pngfile=monopngfile) + return self.test_read_png(pngfile=data['monopngfile']) def test_read_zstack_mono_mask(self): - acc = generate_file_accessor(monozstackmask['path']) + acc = generate_file_accessor(data['monozstackmask']['path']) self.assertTrue(acc.is_mask()) def test_read_in_pixel_scale_from_czi(self): - cf = CziImageFileAccessor(czifile['path']) + cf = CziImageFileAccessor(data['czifile']['path']) pxs = cf.pixel_scale_in_micrometers - self.assertAlmostEqual(pxs['X'], czifile['um_per_pixel'], places=3) + self.assertAlmostEqual(pxs['X'], data['czifile']['um_per_pixel'], places=3) class TestPatchStackAccessor(unittest.TestCase): @@ -175,12 +177,12 @@ class TestPatchStackAccessor(unittest.TestCase): def test_make_patch_stack_from_file(self): - h = monozstackmask['h'] - w = monozstackmask['w'] - c = monozstackmask['c'] - n = monozstackmask['z'] + h = data['monozstackmask']['h'] + w = data['monozstackmask']['w'] + c = data['monozstackmask']['c'] + n = data['monozstackmask']['z'] - acc = make_patch_stack_from_file(monozstackmask['path']) + acc = make_patch_stack_from_file(data['monozstackmask']['path']) self.assertEqual(acc.hw, (h, w)) self.assertEqual(acc.count, n) self.assertEqual(acc.pyxcz.shape, (n, h, w, c, 1)) -- GitLab