diff --git a/mobie/image_data.py b/mobie/image_data.py index bf1860e..ee23d2d 100644 --- a/mobie/image_data.py +++ b/mobie/image_data.py @@ -219,7 +219,8 @@ def add_image(input_path, input_key, source_type="image", source_name=image_name, menu_name=menu_name, view=view, is_default_dataset=is_default_dataset, - contrast_limits=contrast_limits) + contrast_limits=contrast_limits, + description=description) dataset_folder = os.path.join(root, dataset_name) @@ -244,7 +245,7 @@ def add_image(input_path, input_key, channel=channel) metadata.add_source_to_dataset(dataset_folder, "image", image_name, image_metadata_path, - view=view, description=description, channel=channel) + view=view, channel=channel) if transformation is not None: utils.update_transformation_parameter(image_metadata_path, transformation, file_format) diff --git a/mobie/metadata/source_metadata.py b/mobie/metadata/source_metadata.py index 8254c01..da61338 100644 --- a/mobie/metadata/source_metadata.py +++ b/mobie/metadata/source_metadata.py @@ -160,7 +160,7 @@ def _get_file_format(path): return file_format -def _get_image_metadata(dataset_folder, path, type_, file_format, channel, description): +def _get_image_metadata(dataset_folder, path, type_, file_format, channel): file_format = _get_file_format(path) if file_format is None else file_format if file_format.startswith("bdv"): @@ -181,23 +181,20 @@ def _get_image_metadata(dataset_folder, path, type_, file_format, channel, descr format_["channel"] = channel source_metadata = {"imageData": {file_format: format_}} - if description is not None: - assert isinstance(description, str) - source_metadata["description"] = description return {type_: source_metadata} def get_image_metadata(dataset_folder, metadata_path, - file_format=None, channel=None, description=None): + file_format=None, channel=None): return _get_image_metadata(dataset_folder, metadata_path, "image", - file_format=file_format, channel=channel, description=description) + file_format=file_format, channel=channel) def get_segmentation_metadata(dataset_folder, metadata_path, table_location=None, file_format=None, - channel=None, description=None): + channel=None): source_metadata = _get_image_metadata(dataset_folder, metadata_path, "segmentation", - file_format=file_format, channel=channel, description=description) + file_format=file_format, channel=channel) if table_location is not None: relative_table_location = os.path.relpath(table_location, dataset_folder) source_metadata["segmentation"]["tableData"] = get_table_metadata(relative_table_location) @@ -207,8 +204,7 @@ def get_segmentation_metadata(dataset_folder, metadata_path, def get_spot_metadata(dataset_folder, table_folder, bounding_box_min, bounding_box_max, - unit, - description=None): + unit): relative_table_location = os.path.relpath(table_folder, dataset_folder) table_data = get_table_metadata(relative_table_location) @@ -220,8 +216,6 @@ def get_spot_metadata(dataset_folder, table_folder, "unit": unit, } } - if description is not None: - source_metadata["spots"]["description"] = description return source_metadata @@ -234,7 +228,6 @@ def add_source_to_dataset( view=None, table_folder=None, overwrite=True, - description=None, channel=None, suppress_warnings=False, **kwargs, @@ -253,7 +246,6 @@ def add_source_to_dataset( If empty dict, will not add a view (default: None) table_folder [str] - table folder for segmentations and spots. (default: None) overwrite [bool] - whether to overwrite existing entries (default: True) - description [str] - description for this source (default: None) channel [int] - the channel to load from the data. Currently only supported for the ome.zarr format (default: None) suppress_warnings [bool] - a flag to suppress warnings raised by the metadata validation (default: False) @@ -273,19 +265,16 @@ def add_source_to_dataset( if source_type == "image": source_metadata = get_image_metadata(dataset_folder, image_metadata_path, file_format=file_format, - channel=channel, - description=description) + channel=channel) elif source_type == "segmentation": source_metadata = get_segmentation_metadata(dataset_folder, image_metadata_path, table_folder, file_format=file_format, - channel=channel, - description=description) + channel=channel) elif source_type == "spots": source_metadata = get_spot_metadata(dataset_folder, table_folder, - description=description, **kwargs) else: raise ValueError(f"Invalid source type: {source_type}, expect one of 'image', 'segmentation' or 'spots'") diff --git a/mobie/metadata/view_metadata.py b/mobie/metadata/view_metadata.py index 488cec9..9b62c25 100644 --- a/mobie/metadata/view_metadata.py +++ b/mobie/metadata/view_metadata.py @@ -300,7 +300,7 @@ def get_viewer_transform(affine=None, normalized_affine=None, position=None, nor def get_view(names, source_types, sources, display_settings, - is_exclusive, menu_name, + is_exclusive, menu_name, description=None, source_transforms=None, viewer_transform=None, region_displays=None): """ Create view for a multiple sources and optional transformations. @@ -311,6 +311,7 @@ def get_view(names, source_types, sources, display_settings, display_settings [list[dict]] - list of display settings in this view. is_exclusive [bool] - is this an exclusive view. menu_name [str] - menu name for this view + description [str] - description for this view (default: None) source_transforms [list[dict]] - (default: None) viewer_transform [dict] - (default: None) region_displays dict[str, dict] - dictionary from region display name @@ -321,6 +322,8 @@ def get_view(names, source_types, sources, display_settings, lens = f"{len(names)} {len(source_types)}, {len(sources)}, {len(display_settings)}" raise ValueError(f"Different length of names, types, sources and settings: {lens}") view = {"isExclusive": is_exclusive, "uiSelectionGroup": menu_name} + if description is not None: + view["description"] = description source_displays = [] for name, source_type, source_list, display_setting in zip(names, source_types, sources, display_settings): @@ -404,7 +407,8 @@ def get_view(names, source_types, sources, display_settings, def get_default_view(source_type, source_name, menu_name=None, - source_transform=None, viewer_transform=None, **kwargs): + source_transform=None, viewer_transform=None, + description=None, **kwargs): """ Create default view metadata for a single source. Arguments: @@ -414,6 +418,7 @@ def get_default_view(source_type, source_name, menu_name=None, source_transform [dict] - dict with affine source transform. If given, must contain "parameters" and may contain "timepoints" (default: None). viewer_transform [dict] - dict with viewer transform (default: None) + description [str] - description for this view (default: None). **kwargs - additional settings for this view """ if menu_name is None: @@ -428,7 +433,7 @@ def get_default_view(source_type, source_name, menu_name=None, ] view = get_view([source_name], [source_type], [[source_name]], [kwargs], - is_exclusive=False, menu_name=menu_name, + is_exclusive=False, menu_name=menu_name, description=description, source_transforms=source_transforms, viewer_transform=viewer_transform) return view diff --git a/mobie/segmentation.py b/mobie/segmentation.py index aaa0733..3772ad8 100644 --- a/mobie/segmentation.py +++ b/mobie/segmentation.py @@ -61,7 +61,8 @@ def add_segmentation(input_path, input_key, source_type="segmentation", source_name=segmentation_name, menu_name=menu_name, view=view, - is_default_dataset=is_default_dataset) + is_default_dataset=is_default_dataset, + description=description) dataset_folder = os.path.join(root, dataset_name) tmp_folder = f"tmp_{dataset_name}_{segmentation_name}" if tmp_folder is None else tmp_folder @@ -118,8 +119,7 @@ def add_segmentation(input_path, input_key, # add the segmentation to the dataset metadata mobie.metadata.add_source_to_dataset(dataset_folder, "segmentation", segmentation_name, image_metadata_path, - table_folder=table_folder, view=view, - description=description) + table_folder=table_folder, view=view) def main(): diff --git a/mobie/spots.py b/mobie/spots.py index 6af4b5e..fe3c46b 100644 --- a/mobie/spots.py +++ b/mobie/spots.py @@ -79,7 +79,8 @@ def add_spots(input_table, root, dataset_name, spot_name, source_type="spots", source_name=spot_name, menu_name=menu_name, view=view, - is_default_dataset=False) + is_default_dataset=False, + description=description) dataset_folder = os.path.join(root, dataset_name) metadata = mobie.metadata.read_dataset_metadata(dataset_folder) @@ -98,8 +99,7 @@ def add_spots(input_table, root, dataset_name, spot_name, # add the spot source to the dataset metadata mobie.metadata.add_source_to_dataset(dataset_folder, "spots", spot_name, image_metadata_path=None, - table_folder=table_folder, view=view, - description=description, unit=unit, + table_folder=table_folder, view=view, unit=unit, bounding_box_min=bounding_box_min, bounding_box_max=bounding_box_max) diff --git a/mobie/traces.py b/mobie/traces.py index 4a3a5b7..7002962 100644 --- a/mobie/traces.py +++ b/mobie/traces.py @@ -48,7 +48,8 @@ def add_traces(input_folder, root, dataset_name, traces_name, source_type="segmentation", source_name=traces_name, menu_name=menu_name, view=view, - is_default_dataset=False) + is_default_dataset=False, + description=description) # get the path to the reference data dataset_folder = os.path.join(root, dataset_name) @@ -85,8 +86,7 @@ def add_traces(input_folder, root, dataset_name, traces_name, metadata.add_source_to_dataset(dataset_folder, 'segmentation', traces_name, image_metadata_path, - view=view, table_folder=table_folder, - description=description) + view=view, table_folder=table_folder) def main(): diff --git a/mobie/utils.py b/mobie/utils.py index f2cfceb..8fe4598 100644 --- a/mobie/utils.py +++ b/mobie/utils.py @@ -73,13 +73,14 @@ def require_dataset(root, dataset_name): def require_dataset_and_view(root, dataset_name, file_format, source_type, source_name, menu_name, - view, is_default_dataset, contrast_limits=None): + view, is_default_dataset, + contrast_limits=None, description=None): ds_exists = require_dataset(root, dataset_name) dataset_folder = os.path.join(root, dataset_name) if view is None: kwargs = {"contrastLimits": contrast_limits} if source_type == "image" else {} - view = metadata.get_default_view(source_type, source_name, menu_name=menu_name, **kwargs) + view = metadata.get_default_view(source_type, source_name, menu_name=menu_name, description=description, **kwargs) elif view == {}: pass else: diff --git a/test/metadata/test_source_metadata.py b/test/metadata/test_source_metadata.py index 1c63204..b0fcf39 100644 --- a/test/metadata/test_source_metadata.py +++ b/test/metadata/test_source_metadata.py @@ -13,8 +13,7 @@ def test_image_source(self): source = get_image_metadata(ds_folder, xml_path, file_format="bdv.n5") validate_with_schema(source, "source") - source = get_image_metadata(ds_folder, xml_path, file_format="bdv.n5", - description="My shiny image") + source = get_image_metadata(ds_folder, xml_path, file_format="bdv.n5") validate_with_schema(source, "source") source = get_image_metadata(ds_folder, xml_path, file_format="bdv.n5") @@ -68,8 +67,7 @@ def test_segmentation_source(self): source = get_segmentation_metadata(ds_folder, xml_path, file_format="bdv.n5") validate_with_schema(source, "source") - source = get_segmentation_metadata(ds_folder, xml_path, file_format="bdv.n5", - description="My shiny segmentation") + source = get_segmentation_metadata(ds_folder, xml_path, file_format="bdv.n5") validate_with_schema(source, "source") source = get_segmentation_metadata(ds_folder, xml_path, diff --git a/test/metadata/test_view_metadata.py b/test/metadata/test_view_metadata.py index 2c60561..54937ff 100644 --- a/test/metadata/test_view_metadata.py +++ b/test/metadata/test_view_metadata.py @@ -25,6 +25,7 @@ def test_image_view(self): # test custom image settings custom_kwargs = [ + {"description": "Lorem ipsum"}, {"contrastLimits": [0., 255.], "color": "white"}, {"contrastLimits": [0., 2000.], "color": "red"}, {"contrastLimits": [-10., 20000000.]}, diff --git a/test/test_image_data.py b/test/test_image_data.py index a4e2990..45cbf80 100644 --- a/test/test_image_data.py +++ b/test/test_image_data.py @@ -243,7 +243,8 @@ def test_numpy(self): 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) + target="local", max_jobs=self.max_jobs, + description="Lorem ipsum.") self.check_data(os.path.join(self.root, self.dataset_name), im_name) def test_with_view(self): @@ -329,6 +330,12 @@ def check_data(self, dataset_folder, name): data = f[key][:] self.assertTrue(np.array_equal(data, exp_data)) + # check the vew + views = metadata["views"] + self.assertIn(name, views) + view = views[name] + mobie.validation.validate_view_metadata(view, sources=sources, dataset_folder=dataset_folder) + if __name__ == "__main__": unittest.main()