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

Fix zarr metadata writing error #89

Merged
merged 3 commits into from
Jul 24, 2024
Merged
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
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ requires = [
"setuptools",
"wheel",
"imagecodecs>=2021.2.26",
"numpy<2.0.0",
"numpy",
"ome-types>=0.4.2",
"zarr>=2.6.1",
"scyjava",
Expand All @@ -21,7 +21,7 @@ name = "bfio"
dynamic = ["version"]
dependencies = [
"imagecodecs>=2021.2.26",
"numpy<2.0.0",
"numpy",
"ome-types>=0.4.2",
"zarr>=2.6.1",
"scyjava",
Expand Down
11 changes: 11 additions & 0 deletions src/bfio/bfio.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,17 @@ class if specified. *Defaults to None.*
self._metadata.images[0].pixels.dimension_order = (
ome_types.model.Pixels_DimensionOrder.XYZCT
)
# overwrite values in metadata if explicitly specified in the constructor
if kwargs:
for k, v in kwargs.items():
setattr(self, k, v)
if k in ("YXZCT"):
setattr(
self._metadata.images[0].pixels,
"size_{}".format(k.lower()),
v,
)

else:
self._metadata = self._minimal_xml()

Expand Down
36 changes: 30 additions & 6 deletions tests/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
TEST_IMAGES = {
"Plate1-Blue-A-12-Scene-3-P3-F2-03.czi": "https://downloads.openmicroscopy.org/images/Zeiss-CZI/idr0011/Plate1-Blue-A_TS-Stinger/Plate1-Blue-A-12-Scene-3-P3-F2-03.czi",
"5025551.zarr": "https://uk1s3.embassy.ebi.ac.uk/idr/zarr/v0.4/idr0054A/5025551.zarr",
"0.tif": "https://osf.io/j6aer/download",
}

TEST_DIR = pathlib.Path(__file__).with_name("data")
Expand Down Expand Up @@ -43,12 +44,14 @@ def setUpModule():
shutil.rmtree(TEST_DIR.joinpath(file))
zarr_download(url, str(TEST_DIR))


def tearDownModule():
"""Remove test images"""

logger.info("teardown - Removing test images...")
shutil.rmtree(TEST_DIR)


class TestOmeTiffWrite(unittest.TestCase):
@classmethod
def setUpClass(self):
Expand Down Expand Up @@ -110,25 +113,46 @@ def test_write_zarr_tensorstore(self):

actual_shape = br.shape
actual_dtype = br.dtype

actual_image = br[:]

actual_mdata = br.metadata

with tempfile.TemporaryDirectory() as dir:

# Use the temporary directory
test_file_path = os.path.join(dir, 'out/test.ome.zarr')
test_file_path = os.path.join(dir, "out/test.ome.zarr")

with bfio.BioWriter(test_file_path, metadata=actual_mdata, backend="tensorstore") as bw:
with bfio.BioWriter(
test_file_path, metadata=actual_mdata, backend="tensorstore"
) as bw:

expanded = np.expand_dims(actual_image, axis=-1)
bw[:] = expanded

with bfio.BioReader(test_file_path) as br:


assert br.shape == actual_shape
assert br.dtype == actual_dtype

assert br[:].sum() == actual_image.sum()

def test_overwrite_metadata(self):

with bfio.BioReader(str(TEST_DIR.joinpath("0.tif"))) as br:
with tempfile.TemporaryDirectory() as dir:

# Use the temporary directory
test_file_path = os.path.join(dir, "out/0.ome.zarr")

with bfio.BioWriter(
test_file_path,
metadata=br.metadata,
backend="tensorstore",
Z=br.Z * 2,
) as bw:

bw[:, :, 0, :, :] = br[:]
bw[:, :, 1, :, :] = br[:]

bw_z_dim = bw.Z

with bfio.BioReader(test_file_path) as br2:
assert br2.Z == bw_z_dim
Loading