Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to specify arbitrary additional metadata in Zarr Sink #1855

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion sources/zarr/large_image_source_zarr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1066,12 +1066,34 @@

@property
def imageDescription(self):
if isinstance(self._imageDescription, dict):
return self._imageDescription.get('description')
return self._imageDescription

@imageDescription.setter
def imageDescription(self, description):
self._checkEditable()
self._imageDescription = description
if isinstance(self._imageDescription, dict):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a guard -- _imageDescription may not exist. I created a file, saved it, and loaded it and these aren't populated. I would expect that loading a zarr we create would have these populated if appropriate.

self._imageDescription['description'] = description

Check warning on line 1077 in sources/zarr/large_image_source_zarr/__init__.py

View check run for this annotation

Codecov / codecov/patch

sources/zarr/large_image_source_zarr/__init__.py#L1077

Added line #L1077 was not covered by tests
else:
self._imageDescription = description

@property
def additionalMetadata(self):
if isinstance(self._imageDescription, dict):
return self._imageDescription.get('additionalMetadata')
return None

Check warning on line 1085 in sources/zarr/large_image_source_zarr/__init__.py

View check run for this annotation

Codecov / codecov/patch

sources/zarr/large_image_source_zarr/__init__.py#L1085

Added line #L1085 was not covered by tests

@additionalMetadata.setter
def additionalMetadata(self, data):
self._checkEditable()
if isinstance(self._imageDescription, dict):
self._imageDescription['additionalMetadata'] = data

Check warning on line 1091 in sources/zarr/large_image_source_zarr/__init__.py

View check run for this annotation

Codecov / codecov/patch

sources/zarr/large_image_source_zarr/__init__.py#L1091

Added line #L1091 was not covered by tests
else:
self.imageDescription = dict(
description=self._imageDescription,
additionalMetadata=data,
)

@property
def channelNames(self):
Expand Down
29 changes: 29 additions & 0 deletions test/test_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,3 +909,32 @@ def testNegativeMinHeight():
with pytest.raises(TileSourceError) as e:
sink.minHeight = -10
assert str(e.value) == 'minHeight must be positive or None'


def testDescriptionAndAdditionalMetadata(tmp_path):
output_file = tmp_path / 'test.tiff'
sink = large_image_source_zarr.new()
sink.addTile(np.zeros((256, 256, 1), dtype=np.uint8), x=0, y=0)

description = 'This is a test description.'
additional_metadata = dict(
name='Test',
values=[1, 2, 3],
nested=dict(hello='world'),
)
both = dict(
description=description,
additionalMetadata=additional_metadata,
)

sink.imageDescription = description
assert sink._imageDescription == description
sink.additionalMetadata = additional_metadata
assert sink._imageDescription == both
assert sink.imageDescription == description
assert sink.additionalMetadata == additional_metadata

sink.write(output_file)
written = large_image.open(output_file)
internal = written.getInternalMetadata()['xml']['internal']['zarr']['base']
assert internal['multiscales'][0]['metadata']['description'] == both