Skip to content

Commit bc9352e

Browse files
KumoLiuericspod
andauthored
Fix deprecated usage in zarr (Project-MONAI#8313)
Fixes Project-MONAI#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 <55491388+KumoLiu@users.noreply.github.com> Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
1 parent df1ba5d commit bc9352e

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

monai/inferers/merger.py

+19-4
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
from abc import ABC, abstractmethod
1616
from collections.abc import Sequence
1717
from contextlib import nullcontext
18+
from tempfile import TemporaryDirectory
1819
from typing import TYPE_CHECKING, Any
1920

2021
import numpy as np
2122
import torch
2223

23-
from monai.utils import ensure_tuple_size, optional_import, require_pkg
24+
from monai.utils import ensure_tuple_size, get_package_version, optional_import, require_pkg, version_geq
2425

2526
if TYPE_CHECKING:
2627
import zarr
@@ -233,7 +234,7 @@ def __init__(
233234
store: zarr.storage.Store | str = "merged.zarr",
234235
value_store: zarr.storage.Store | str | None = None,
235236
count_store: zarr.storage.Store | str | None = None,
236-
compressor: str = "default",
237+
compressor: str | None = None,
237238
value_compressor: str | None = None,
238239
count_compressor: str | None = None,
239240
chunks: Sequence[int] | bool = True,
@@ -246,8 +247,22 @@ def __init__(
246247
self.value_dtype = value_dtype
247248
self.count_dtype = count_dtype
248249
self.store = store
249-
self.value_store = zarr.storage.TempStore() if value_store is None else value_store
250-
self.count_store = zarr.storage.TempStore() if count_store is None else count_store
250+
self.tmpdir: TemporaryDirectory | None
251+
if version_geq(get_package_version("zarr"), "3.0.0"):
252+
if value_store is None:
253+
self.tmpdir = TemporaryDirectory()
254+
self.value_store = zarr.storage.LocalStore(self.tmpdir.name)
255+
else:
256+
self.value_store = value_store
257+
if count_store is None:
258+
self.tmpdir = TemporaryDirectory()
259+
self.count_store = zarr.storage.LocalStore(self.tmpdir.name)
260+
else:
261+
self.count_store = count_store
262+
else:
263+
self.tmpdir = None
264+
self.value_store = zarr.storage.TempStore() if value_store is None else value_store
265+
self.count_store = zarr.storage.TempStore() if count_store is None else count_store
251266
self.chunks = chunks
252267
self.compressor = compressor
253268
self.value_compressor = value_compressor

tests/test_zarr_avg_merger.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -287,15 +287,16 @@ class ZarrAvgMergerTests(unittest.TestCase):
287287
]
288288
)
289289
def test_zarr_avg_merger_patches(self, arguments, patch_locations, expected):
290+
codec_reg = numcodecs.registry.codec_registry
290291
if "compressor" in arguments:
291292
if arguments["compressor"] != "default":
292-
arguments["compressor"] = zarr.codec_registry[arguments["compressor"].lower()]()
293+
arguments["compressor"] = codec_reg[arguments["compressor"].lower()]()
293294
if "value_compressor" in arguments:
294295
if arguments["value_compressor"] != "default":
295-
arguments["value_compressor"] = zarr.codec_registry[arguments["value_compressor"].lower()]()
296+
arguments["value_compressor"] = codec_reg[arguments["value_compressor"].lower()]()
296297
if "count_compressor" in arguments:
297298
if arguments["count_compressor"] != "default":
298-
arguments["count_compressor"] = zarr.codec_registry[arguments["count_compressor"].lower()]()
299+
arguments["count_compressor"] = codec_reg[arguments["count_compressor"].lower()]()
299300
merger = ZarrAvgMerger(**arguments)
300301
for pl in patch_locations:
301302
merger.aggregate(pl[0], pl[1])

0 commit comments

Comments
 (0)