Skip to content

Commit

Permalink
Update description (#126)
Browse files Browse the repository at this point in the history
Update description fields
  • Loading branch information
constantinpape authored Jan 24, 2024
1 parent 60e8065 commit 72db448
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 40 deletions.
5 changes: 3 additions & 2 deletions mobie/image_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down
27 changes: 8 additions & 19 deletions mobie/metadata/source_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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


Expand All @@ -234,7 +228,6 @@ def add_source_to_dataset(
view=None,
table_folder=None,
overwrite=True,
description=None,
channel=None,
suppress_warnings=False,
**kwargs,
Expand All @@ -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)
Expand All @@ -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'")
Expand Down
11 changes: 8 additions & 3 deletions mobie/metadata/view_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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

Expand Down
6 changes: 3 additions & 3 deletions mobie/segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down
6 changes: 3 additions & 3 deletions mobie/spots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions mobie/traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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():
Expand Down
5 changes: 3 additions & 2 deletions mobie/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 2 additions & 4 deletions test/metadata/test_source_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions test/metadata/test_view_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.]},
Expand Down
9 changes: 8 additions & 1 deletion test/test_image_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()

0 comments on commit 72db448

Please sign in to comment.