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 deprecated usage in zarr #8313

Merged
merged 5 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 17 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,20 @@ 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
if version_geq(get_package_version("zarr"), "3.0.0"):
if value_store is None:
tmpdir = TemporaryDirectory()
self.value_store = zarr.storage.LocalStore(tmpdir.name)
else:
self.value_store = value_store
if count_store is None:
tmpdir = TemporaryDirectory()
self.count_store = zarr.storage.LocalStore(tmpdir.name)
else:
self.count_store = count_store
else:
KumoLiu marked this conversation as resolved.
Show resolved Hide resolved
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
Loading