diff --git a/mobie/image_data.py b/mobie/image_data.py index 9218249..50bf38d 100644 --- a/mobie/image_data.py +++ b/mobie/image_data.py @@ -210,7 +210,7 @@ def add_image(input_path, input_key, # set default contrast_limits if we don't have a view # or if the passed view doesn't hav contrast limits - if view is None or "contrastLimits" not in view: + if view is None or "contrastLimits" not in view["sourceDisplays"][0]["imageDisplay"]: contrast_limits = _get_default_contrast_limits(input_path, input_key, int_to_uint) else: contrast_limits = None diff --git a/mobie/import_data/utils.py b/mobie/import_data/utils.py index 2151c10..8c12180 100644 --- a/mobie/import_data/utils.py +++ b/mobie/import_data/utils.py @@ -48,7 +48,7 @@ def compute_node_labels(seg_path, seg_key, def check_input_data(in_path, in_key, resolution, require3d, channel): - # TODO to support data with channel, we need to support dowscaling with channels + # TODO to support data with channel, we need to support downscaling with channels if channel is not None: raise NotImplementedError with open_file(in_path, "r") as f: diff --git a/mobie/utils.py b/mobie/utils.py index 19eeca1..d288e60 100644 --- a/mobie/utils.py +++ b/mobie/utils.py @@ -85,7 +85,7 @@ def require_dataset_and_view(root, dataset_name, file_format, update_view = {} if menu_name is not None: update_view["uiSelectionGroup"] = menu_name - if source_type == "image" and contrast_limits is None: + if source_type == "image" and contrast_limits is not None: update_view["contrastLimits"] = contrast_limits if update_view: view.update(update_view) diff --git a/mobie/validation/metadata.py b/mobie/validation/metadata.py index 28136d7..174276c 100644 --- a/mobie/validation/metadata.py +++ b/mobie/validation/metadata.py @@ -21,13 +21,15 @@ def _check_bdv_n5_s3(xml, assert_true): def _check_ome_zarr_s3(address, name, assert_true, assert_equal, channel): try: - attrs = load_json_from_s3(os.path.join(address, ".zattrs")) + load_json_from_s3(os.path.join(address, ".zattrs")) except Exception: assert_true(False, f"Can't find ome.zarr..s3file at {address}") - # we can't do this check if we only load a sub-channel - if channel is None: - ome_name = attrs["multiscales"][0]["name"] - assert_equal(name, ome_name, f"Source name and name in ngff metadata don't match: {name} != {ome_name}") + + # we disable the name check for the time being since it seems to not be necessary, + # AND restricting the name in this fashion prevents embedding existing ome.zarr files in mobie projects + # if channel is None: + # ome_name = attrs["multiscales"][0]["name"] + # assert_equal(name, ome_name, f"Source name and name in ngff metadata don't match: {name} != {ome_name}") def _check_data(storage, format_, name, dataset_folder, diff --git a/test/test_image_data.py b/test/test_image_data.py index c6f7ba6..deed878 100644 --- a/test/test_image_data.py +++ b/test/test_image_data.py @@ -7,15 +7,13 @@ from sys import platform import imageio +import mobie import numpy as np import h5py from elf.io import open_file from pybdv.metadata import get_data_path from pybdv.util import get_key -from mobie import add_image -from mobie.validation import validate_project, validate_source_metadata -from mobie.metadata import read_dataset_metadata class TestImageData(unittest.TestCase): @@ -59,10 +57,10 @@ def test_init_from_tif(self): dataset_name = "test" raw_name = "test-raw" scales = [[1, 2, 2], [1, 2, 2], [2, 2, 2]] - add_image(im_folder, "*.tif", self.root, dataset_name, raw_name, - resolution=(0.25, 1, 1), chunks=(16, 64, 64), - scale_factors=scales, tmp_folder=self.tmp_folder, - target="local", max_jobs=self.max_jobs) + mobie.add_image(im_folder, "*.tif", self.root, dataset_name, raw_name, + resolution=(0.25, 1, 1), chunks=(16, 64, 64), + scale_factors=scales, tmp_folder=self.tmp_folder, + target="local", max_jobs=self.max_jobs) self.check_dataset(os.path.join(self.root, dataset_name), shape, raw_name) @@ -84,13 +82,13 @@ def init_h5_dataset( n_jobs = 1 if file_format == "bdv.hdf5" else self.max_jobs scales = [[2, 2, 2], [2, 2, 2], [2, 2, 2]] - add_image(data_path, data_key, self.root, dataset_name, raw_name, - resolution=(1, 1, 1), chunks=(32, 32, 32), - scale_factors=scales, - tmp_folder=self.tmp_folder, - file_format=file_format, - target="local", max_jobs=n_jobs, - int_to_uint=int_to_uint) + mobie.add_image(data_path, data_key, self.root, dataset_name, raw_name, + resolution=(1, 1, 1), chunks=(32, 32, 32), + scale_factors=scales, + tmp_folder=self.tmp_folder, + file_format=file_format, + target="local", max_jobs=n_jobs, + int_to_uint=int_to_uint) def test_init_from_hdf5(self, func=None, int_to_uint=False): dataset_name = "test" @@ -110,7 +108,7 @@ def float_data(shape): self.test_init_from_hdf5(float_data) ds_folder = os.path.join(self.root, self.dataset_name) - mdata = read_dataset_metadata(ds_folder) + mdata = mobie.metadata.read_dataset_metadata(ds_folder) clims = mdata["views"]["test-raw"]["sourceDisplays"][0]["imageDisplay"]["contrastLimits"] c0, c1 = clims self.assertEqual(c0, 0.0) @@ -140,7 +138,7 @@ def int_data(shape): self.test_init_from_hdf5(int_data, int_to_uint=int_to_uint) ds_folder = os.path.join(self.root, self.dataset_name) - mdata = read_dataset_metadata(ds_folder) + mdata = mobie.metadata.read_dataset_metadata(ds_folder) clims = mdata["views"]["test-raw"]["sourceDisplays"][0]["imageDisplay"]["contrastLimits"] c0, c1 = clims self.assertEqual(c0, np.iinfo(dtype).min) @@ -191,9 +189,9 @@ def init_dataset(self): raw_name = "test-raw" scales = [[2, 2, 2]] - add_image(data_path, data_key, self.root, self.dataset_name, raw_name, - resolution=(1, 1, 1), chunks=(64, 64, 64), scale_factors=scales, - tmp_folder=tmp_folder, target="local", max_jobs=self.max_jobs) + mobie.add_image(data_path, data_key, self.root, self.dataset_name, raw_name, + resolution=(1, 1, 1), chunks=(64, 64, 64), scale_factors=scales, + tmp_folder=tmp_folder, target="local", max_jobs=self.max_jobs) def test_add_image_with_dataset(self): self.init_dataset() @@ -203,11 +201,11 @@ def test_add_image_with_dataset(self): tmp_folder = os.path.join(self.test_folder, "tmp-im") scales = [[2, 2, 2]] - add_image(self.im_path, self.im_key, - self.root, self.dataset_name, im_name, - resolution=(1, 1, 1), scale_factors=scales, - chunks=(64, 64, 64), tmp_folder=tmp_folder, - target="local", max_jobs=self.max_jobs) + mobie.add_image(self.im_path, self.im_key, + self.root, self.dataset_name, im_name, + resolution=(1, 1, 1), scale_factors=scales, + chunks=(64, 64, 64), tmp_folder=tmp_folder, + target="local", max_jobs=self.max_jobs) self.check_data(dataset_folder, im_name) @unittest.skipIf(platform == "win32", "CLI does not work on windows") @@ -238,22 +236,40 @@ def test_cli(self): # # test with numpy data # + def test_numpy(self): im_name = "test-data" scales = [[2, 2, 2]] - add_image(self.data, None, self.root, self.dataset_name, im_name, - resolution=(1, 1, 1), scale_factors=scales, - chunks=(64, 64, 64), tmp_folder=self.tmp_folder, - target="local", max_jobs=self.max_jobs) + mobie.add_image(self.data, None, self.root, self.dataset_name, im_name, + resolution=(1, 1, 1), scale_factors=scales, + chunks=(64, 64, 64), tmp_folder=self.tmp_folder, + target="local", max_jobs=self.max_jobs) + self.check_data(os.path.join(self.root, self.dataset_name), im_name) + + def test_with_view(self): + im_name = "test-data" + scales = [[2, 2, 2]] + + clims = [0.1, 0.9] + view = mobie.metadata.get_default_view("image", im_name, contrastLimits=clims) + + mobie.add_image(self.data, None, self.root, self.dataset_name, im_name, + resolution=(1, 1, 1), scale_factors=scales, + chunks=(64, 64, 64), tmp_folder=self.tmp_folder, + target="local", max_jobs=self.max_jobs, view=view) self.check_data(os.path.join(self.root, self.dataset_name), im_name) + mdata = mobie.metadata.read_dataset_metadata(os.path.join(self.root, self.dataset_name)) + clims_read = mdata["views"][im_name]["sourceDisplays"][0]["imageDisplay"]["contrastLimits"] + self.assertEqual(clims, clims_read) + # # data validation # def check_dataset(self, dataset_folder, exp_shape, raw_name, file_format="bdv.n5"): # validate the full project - validate_project( + mobie.validation.validate_project( self.root, assert_true=self.assertTrue, assert_in=self.assertIn, assert_equal=self.assertEqual ) @@ -279,10 +295,10 @@ def check_data(self, dataset_folder, name): exp_data = self.data # check the image metadata - metadata = read_dataset_metadata(dataset_folder) + metadata = mobie.metadata.read_dataset_metadata(dataset_folder) sources = metadata["sources"] self.assertIn(name, sources) - validate_source_metadata(name, sources[name], dataset_folder) + mobie.validation.validate_source_metadata(name, sources[name], dataset_folder) # check the image data im_path = os.path.join(dataset_folder, "images", "bdv-n5", f"{name}.n5")