Skip to content

Commit

Permalink
Fix deprecated usage in zarr (#8313)
Browse files Browse the repository at this point in the history
Fixes #8298


### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

---------

Signed-off-by: YunLiu <[email protected]>
Co-authored-by: Eric Kerfoot <[email protected]>
  • Loading branch information
KumoLiu and ericspod authored Jan 24, 2025
1 parent df1ba5d commit bc9352e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
23 changes: 19 additions & 4 deletions monai/inferers/merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
from abc import ABC, abstractmethod
from collections.abc import Sequence
from contextlib import nullcontext
from tempfile import TemporaryDirectory
from typing import TYPE_CHECKING, Any

import numpy as np
import torch

from monai.utils import ensure_tuple_size, optional_import, require_pkg
from monai.utils import ensure_tuple_size, get_package_version, optional_import, require_pkg, version_geq

if TYPE_CHECKING:
import zarr
Expand Down Expand Up @@ -233,7 +234,7 @@ def __init__(
store: zarr.storage.Store | str = "merged.zarr",
value_store: zarr.storage.Store | str | None = None,
count_store: zarr.storage.Store | str | None = None,
compressor: str = "default",
compressor: str | None = None,
value_compressor: str | None = None,
count_compressor: str | None = None,
chunks: Sequence[int] | bool = True,
Expand All @@ -246,8 +247,22 @@ def __init__(
self.value_dtype = value_dtype
self.count_dtype = count_dtype
self.store = store
self.value_store = zarr.storage.TempStore() if value_store is None else value_store
self.count_store = zarr.storage.TempStore() if count_store is None else count_store
self.tmpdir: TemporaryDirectory | None
if version_geq(get_package_version("zarr"), "3.0.0"):
if value_store is None:
self.tmpdir = TemporaryDirectory()
self.value_store = zarr.storage.LocalStore(self.tmpdir.name)
else:
self.value_store = value_store
if count_store is None:
self.tmpdir = TemporaryDirectory()
self.count_store = zarr.storage.LocalStore(self.tmpdir.name)
else:
self.count_store = count_store
else:
self.tmpdir = None
self.value_store = zarr.storage.TempStore() if value_store is None else value_store
self.count_store = zarr.storage.TempStore() if count_store is None else count_store
self.chunks = chunks
self.compressor = compressor
self.value_compressor = value_compressor
Expand Down
7 changes: 4 additions & 3 deletions tests/test_zarr_avg_merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,16 @@ class ZarrAvgMergerTests(unittest.TestCase):
]
)
def test_zarr_avg_merger_patches(self, arguments, patch_locations, expected):
codec_reg = numcodecs.registry.codec_registry
if "compressor" in arguments:
if arguments["compressor"] != "default":
arguments["compressor"] = zarr.codec_registry[arguments["compressor"].lower()]()
arguments["compressor"] = codec_reg[arguments["compressor"].lower()]()
if "value_compressor" in arguments:
if arguments["value_compressor"] != "default":
arguments["value_compressor"] = zarr.codec_registry[arguments["value_compressor"].lower()]()
arguments["value_compressor"] = codec_reg[arguments["value_compressor"].lower()]()
if "count_compressor" in arguments:
if arguments["count_compressor"] != "default":
arguments["count_compressor"] = zarr.codec_registry[arguments["count_compressor"].lower()]()
arguments["count_compressor"] = codec_reg[arguments["count_compressor"].lower()]()
merger = ZarrAvgMerger(**arguments)
for pl in patch_locations:
merger.aggregate(pl[0], pl[1])
Expand Down

0 comments on commit bc9352e

Please sign in to comment.