Repository: Project-MONAI/MONAI
File: tests/inferers/test_zarr_avg_merger.py (Python)

Code before:
    ),
    [
        (TENSOR_4x4[..., :2, :2], (0, 0)),
        (TENSOR_4x4[..., :2, 2:], (0, 2)),
        (TENSOR_4x4[..., 2:, :2], (2, 0)),
        (TENSOR_4x4[..., 2:, 2:], (2, 2)),
    ],
    TENSOR_4x4,
]

# explicit chunk size
TEST_CASE_12_CHUNKS = [
    dict(merged_shape=TENSOR_4x4.shape, chunks=(1, 1, 2, 2)),
    [
        (TENSOR_4x4[..., :2, :2], (0, 0)),
        (TENSOR_4x4[..., :2, 2:], (0, 2)),
        (TENSOR_4x4[..., 2:, :2], (2, 0)),
        (TENSOR_4x4[..., 2:, 2:], (2, 2)),
    ],
    TENSOR_4x4,
]

# Define zarr v3 codec configurations with proper bytes codec
ZARR_V3_LZ4_CODECS = [{"name": "bytes", "configuration": {}}, {"name": "blosc", "configuration": {"cname": "lz4"}}]

ZARR_V3_PICKLE_CODECS = [{"name": "bytes", "configuration": {}}, {"name": "pickle", "configuration": {}}]

ZARR_V3_LZMA_CODECS = [{"name": "bytes", "configuration": {}}, {"name": "lzma", "configuration": {}}]

# test for LZ4 compressor (zarr v2) or codecs (zarr v3)
TEST_CASE_13_COMPRESSOR_LZ4 = [
    (
        dict(merged_shape=TENSOR_4x4.shape, compressor="LZ4")
        if not version_geq(get_package_version("zarr"), "3.0.0")
        else dict(merged_shape=TENSOR_4x4.shape, codecs=ZARR_V3_LZ4_CODECS)
    ),
    [
        (TENSOR_4x4[..., :2, :2], (0, 0)),
        (TENSOR_4x4[..., :2, 2:], (0, 2)),
        (TENSOR_4x4[..., 2:, :2], (2, 0)),
        (TENSOR_4x4[..., 2:, 2:], (2, 2)),
    ],
    TENSOR_4x4,
]

# test for pickle compressor (zarr v2) or codecs (zarr v3)
TEST_CASE_14_COMPRESSOR_PICKLE = [
    (
        dict(merged_shape=TENSOR_4x4.shape, compressor="Pickle")
        if not version_geq(get_package_version("zarr"), "3.0.0")
        else dict(merged_shape=TENSOR_4x4.shape, codecs=ZARR_V3_PICKLE_CODECS)

Review comment (suggestion):
Got this error when using zarr==3.0.1
```
======================================================================
ERROR: test_zarr_avg_merger_patches_16 (tests.inferers.test_zarr_avg_merger.ZarrAvgMergerTests.test_zarr_avg_merger_patches_16)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/yunliu/anaconda3/lib/python3.12/site-packages/parameterized/parameterized.py", line 620, in standalone_func
    return func(*(a + p.args), **p.kwargs, **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunliu/Workspace/Code/MONAI/tests/inferers/test_zarr_avg_merger.py", line 410, in test_zarr_avg_merger_patches
    merger = ZarrAvgMerger(**arguments)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunliu/Workspace/Code/MONAI/monai/utils/module.py", line 477, in _wrapper
    return call_obj(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunliu/Workspace/Code/MONAI/monai/inferers/merger.py", line 372, in __init__
    self.output = zarr.empty(
                  ^^^^^^^^^^^
  File "/home/yunliu/anaconda3/lib/python3.12/site-packages/zarr/api/synchronous.py", line 918, in empty
    return Array(sync(async_api.empty(shape, **kwargs)))
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunliu/anaconda3/lib/python3.12/site-packages/zarr/core/sync.py", line 142, in sync
    raise return_result
  File "/home/yunliu/anaconda3/lib/python3.12/site-packages/zarr/core/sync.py", line 98, in _runner
    return await coro
           ^^^^^^^^^^
  File "/home/yunliu/anaconda3/lib/python3.12/site-packages/zarr/api/asynchronous.py", line 1084, in empty
    return await create(shape=shape, fill_value=None, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunliu/anaconda3/lib/python3.12/site-packages/zarr/api/asynchronous.py", line 1044, in create
    return await AsyncArray._create(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunliu/anaconda3/lib/python3.12/site-packages/zarr/core/array.py", line 610, in _create
    result = await cls._create_v3(
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunliu/anaconda3/lib/python3.12/site-packages/zarr/core/array.py", line 714, in _create_v3
    metadata = ArrayV3Metadata(
               ^^^^^^^^^^^^^^^^
  File "/home/yunliu/anaconda3/lib/python3.12/site-packages/zarr/core/metadata/v3.py", line 278, in __init__
    codecs_parsed_partial = parse_codecs(codecs)
                            ^^^^^^^^^^^^^^^^^^^^
  File "/home/yunliu/anaconda3/lib/python3.12/site-packages/zarr/core/metadata/v3.py", line 80, in parse_codecs
    out += (get_codec_class(name_parsed).from_dict(c),)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunliu/anaconda3/lib/python3.12/site-packages/zarr/registry.py", line 144, in get_codec_class
    raise KeyError(key)
KeyError: 'pickle'

======================================================================
ERROR: test_zarr_avg_merger_patches_17 (tests.inferers.test_zarr_avg_merger.ZarrAvgMergerTests.test_zarr_avg_merger_patches_17)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/yunliu/anaconda3/lib/python3.12/site-packages/parameterized/parameterized.py", line 620, in standalone_func
    return func(*(a + p.args), **p.kwargs, **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunliu/Workspace/Code/MONAI/tests/inferers/test_zarr_avg_merger.py", line 410, in test_zarr_avg_merger_patches
    merger = ZarrAvgMerger(**arguments)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunliu/Workspace/Code/MONAI/monai/utils/module.py", line 477, in _wrapper
    return call_obj(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunliu/Workspace/Code/MONAI/monai/inferers/merger.py", line 372, in __init__
    self.output = zarr.empty(
                  ^^^^^^^^^^^
  File "/home/yunliu/anaconda3/lib/python3.12/site-packages/zarr/api/synchronous.py", line 918, in empty
    return Array(sync(async_api.empty(shape, **kwargs)))
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunliu/anaconda3/lib/python3.12/site-packages/zarr/core/sync.py", line 142, in sync
    raise return_result
  File "/home/yunliu/anaconda3/lib/python3.12/site-packages/zarr/core/sync.py", line 98, in _runner
    return await coro
           ^^^^^^^^^^
  File "/home/yunliu/anaconda3/lib/python3.12/site-packages/zarr/api/asynchronous.py", line 1084, in empty
    return await create(shape=shape, fill_value=None, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunliu/anaconda3/lib/python3.12/site-packages/zarr/api/asynchronous.py", line 1044, in create
    return await AsyncArray._create(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunliu/anaconda3/lib/python3.12/site-packages/zarr/core/array.py", line 610, in _create
    result = await cls._create_v3(
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunliu/anaconda3/lib/python3.12/site-packages/zarr/core/array.py", line 714, in _create_v3
    metadata = ArrayV3Metadata(
               ^^^^^^^^^^^^^^^^
  File "/home/yunliu/anaconda3/lib/python3.12/site-packages/zarr/core/metadata/v3.py", line 278, in __init__
    codecs_parsed_partial = parse_codecs(codecs)
                            ^^^^^^^^^^^^^^^^^^^^
  File "/home/yunliu/anaconda3/lib/python3.12/site-packages/zarr/core/metadata/v3.py", line 80, in parse_codecs
    out += (get_codec_class(name_parsed).from_dict(c),)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunliu/anaconda3/lib/python3.12/site-packages/zarr/registry.py", line 144, in get_codec_class
    raise KeyError(key)
KeyError: 'lzma'
```

Code after:
    [
        (TENSOR_4x4[..., :2, :2], (0, 0)),
        (TENSOR_4x4[..., :2, 2:], (0, 2)),
        (TENSOR_4x4[..., 2:, :2], (2, 0)),
        (TENSOR_4x4[..., 2:, 2:], (2, 2)),
    ],
    TENSOR_4x4,
]

# explicit chunk size
TEST_CASE_12_CHUNKS = [
    dict(merged_shape=TENSOR_4x4.shape, chunks=(1, 1, 2, 2)),
    [
        (TENSOR_4x4[..., :2, :2], (0, 0)),
        (TENSOR_4x4[..., :2, 2:], (0, 2)),
        (TENSOR_4x4[..., 2:, :2], (2, 0)),
        (TENSOR_4x4[..., 2:, 2:], (2, 2)),
    ],
    TENSOR_4x4,
]

# Define zarr v3 codec configurations with proper bytes codec
ZARR_V3_LZ4_CODECS = [{"name": "bytes", "configuration": {}}, {"name": "blosc", "configuration": {"cname": "lz4"}}]

ZARR_V3_PICKLE_CODECS = [{"name": "bytes", "configuration": {}}, {"name": "blosc", "configuration": {"cname": "zstd"}}]

ZARR_V3_LZMA_CODECS = [{"name": "bytes", "configuration": {}}, {"name": "blosc", "configuration": {"cname": "zlib"}}]

# test for LZ4 compressor (zarr v2) or codecs (zarr v3)
TEST_CASE_13_COMPRESSOR_LZ4 = [
    (
        dict(merged_shape=TENSOR_4x4.shape, compressor="LZ4")
        if not version_geq(get_package_version("zarr"), "3.0.0")
        else dict(merged_shape=TENSOR_4x4.shape, codecs=ZARR_V3_LZ4_CODECS)
    ),
    [
        (TENSOR_4x4[..., :2, :2], (0, 0)),
        (TENSOR_4x4[..., :2, 2:], (0, 2)),
        (TENSOR_4x4[..., 2:, :2], (2, 0)),
        (TENSOR_4x4[..., 2:, 2:], (2, 2)),
    ],
    TENSOR_4x4,
]

# test for pickle compressor (zarr v2) or codecs (zarr v3)
TEST_CASE_14_COMPRESSOR_PICKLE = [
    (
        dict(merged_shape=TENSOR_4x4.shape, compressor="Pickle")
        if not version_geq(get_package_version("zarr"), "3.0.0")
        else dict(merged_shape=TENSOR_4x4.shape, codecs=ZARR_V3_PICKLE_CODECS)
    ),