From a37535431d0c02fa19155d78bd62854ac5fc1686 Mon Sep 17 00:00:00 2001 From: Eric Kerfoot Date: Wed, 29 Jan 2025 14:29:52 +0000 Subject: [PATCH 1/7] Update Zarr tests to include compression tests only with versions before 3.0 Signed-off-by: Eric Kerfoot --- tests/test_zarr_avg_merger.py | 49 ++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/tests/test_zarr_avg_merger.py b/tests/test_zarr_avg_merger.py index 3c89e4fb03..e82bca8239 100644 --- a/tests/test_zarr_avg_merger.py +++ b/tests/test_zarr_avg_merger.py @@ -202,7 +202,7 @@ # test for LZ4 compressor TEST_CASE_13_COMPRESSOR_LZ4 = [ - dict(merged_shape=TENSOR_4x4.shape, compressor="LZ4"), + dict(merged_shape=TENSOR_4x4.shape, compressor=zarr.codecs.BloscCodec), [ (TENSOR_4x4[..., :2, :2], (0, 0)), (TENSOR_4x4[..., :2, 2:], (0, 2)), @@ -260,37 +260,38 @@ TENSOR_4x4, ] +ALL_TESTS = [ + TEST_CASE_0_DEFAULT_DTYPE, + TEST_CASE_1_DEFAULT_DTYPE, + TEST_CASE_2_DEFAULT_DTYPE, + TEST_CASE_3_DEFAULT_DTYPE, + TEST_CASE_4_DEFAULT_DTYPE, + TEST_CASE_5_VALUE_DTYPE, + TEST_CASE_6_COUNT_DTYPE, + TEST_CASE_7_COUNT_VALUE_DTYPE, + TEST_CASE_8_DTYPE, + TEST_CASE_9_LARGER_SHAPE, + TEST_CASE_10_DIRECTORY_STORE, + TEST_CASE_11_MEMORY_STORE, + TEST_CASE_12_CHUNKS, + TEST_CASE_16_WITH_LOCK, + TEST_CASE_17_WITHOUT_LOCK, +] + +# add compression tests only when using Zarr version before 3.0 +if not version_geq(get_package_version("zarr"), "3.0.0"): + ALL_TESTS += [TEST_CASE_13_COMPRESSOR_LZ4, TEST_CASE_14_COMPRESSOR_PICKLE, TEST_CASE_15_COMPRESSOR_LZMA] + @unittest.skipUnless(has_zarr and has_numcodecs, "Requires zarr (and numcodecs) packages.)") class ZarrAvgMergerTests(unittest.TestCase): - @parameterized.expand( - [ - TEST_CASE_0_DEFAULT_DTYPE, - TEST_CASE_1_DEFAULT_DTYPE, - TEST_CASE_2_DEFAULT_DTYPE, - TEST_CASE_3_DEFAULT_DTYPE, - TEST_CASE_4_DEFAULT_DTYPE, - TEST_CASE_5_VALUE_DTYPE, - TEST_CASE_6_COUNT_DTYPE, - TEST_CASE_7_COUNT_VALUE_DTYPE, - TEST_CASE_8_DTYPE, - TEST_CASE_9_LARGER_SHAPE, - TEST_CASE_10_DIRECTORY_STORE, - TEST_CASE_11_MEMORY_STORE, - TEST_CASE_12_CHUNKS, - TEST_CASE_13_COMPRESSOR_LZ4, - TEST_CASE_14_COMPRESSOR_PICKLE, - TEST_CASE_15_COMPRESSOR_LZMA, - TEST_CASE_16_WITH_LOCK, - TEST_CASE_17_WITHOUT_LOCK, - ] - ) + @parameterized.expand(ALL_TESTS) 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"] = codec_reg[arguments["compressor"].lower()]() + arguments["compressor"] = arguments["compressor"] if "value_compressor" in arguments: if arguments["value_compressor"] != "default": arguments["value_compressor"] = codec_reg[arguments["value_compressor"].lower()]() From d6152a30c1316bfbc9fa9f61f7894be541ceb188 Mon Sep 17 00:00:00 2001 From: Eric Kerfoot Date: Wed, 29 Jan 2025 14:45:58 +0000 Subject: [PATCH 2/7] Update for loading MetaTensor Signed-off-by: Eric Kerfoot --- monai/data/meta_tensor.py | 3 +++ tests/test_meta_tensor.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/monai/data/meta_tensor.py b/monai/data/meta_tensor.py index c4c491e1b9..5ad57a7216 100644 --- a/monai/data/meta_tensor.py +++ b/monai/data/meta_tensor.py @@ -607,3 +607,6 @@ def print_verbose(self) -> None: print(self) if self.meta is not None: print(self.meta.__repr__()) + + +torch.serialization.add_safe_globals([MetaTensor]) diff --git a/tests/test_meta_tensor.py b/tests/test_meta_tensor.py index 60b6019703..4e0c57d767 100644 --- a/tests/test_meta_tensor.py +++ b/tests/test_meta_tensor.py @@ -256,7 +256,7 @@ def test_pickling(self): with tempfile.TemporaryDirectory() as tmp_dir: fname = os.path.join(tmp_dir, "im.pt") torch.save(m, fname) - m2 = torch.load(fname) + m2 = torch.load(fname, weights_only=False) if not isinstance(m2, MetaTensor) and not pytorch_after(1, 8, 1): warnings.warn("Old version of pytorch. pickling converts `MetaTensor` to `torch.Tensor`.") m = m.as_tensor() From 7c5bb691a930653a07e22f8b38a4046cd5128af4 Mon Sep 17 00:00:00 2001 From: Eric Kerfoot Date: Wed, 29 Jan 2025 14:51:07 +0000 Subject: [PATCH 3/7] Update for loading MetaTensor Signed-off-by: Eric Kerfoot --- monai/data/meta_tensor.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/monai/data/meta_tensor.py b/monai/data/meta_tensor.py index 5ad57a7216..2194f719b1 100644 --- a/monai/data/meta_tensor.py +++ b/monai/data/meta_tensor.py @@ -609,4 +609,5 @@ def print_verbose(self) -> None: print(self.meta.__repr__()) -torch.serialization.add_safe_globals([MetaTensor]) +if hasattr(torch.serialization,"add_safe_globals"): + torch.serialization.add_safe_globals([MetaTensor]) From b26894e0cc2c30ce4c9c2d57087bba6613373528 Mon Sep 17 00:00:00 2001 From: Eric Kerfoot Date: Wed, 29 Jan 2025 14:58:39 +0000 Subject: [PATCH 4/7] Fix pycln issue Signed-off-by: Eric Kerfoot --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2a57fbf31a..9621a1fe95 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -66,7 +66,7 @@ repos: )$ - repo: https://github.com/hadialqattan/pycln - rev: v2.4.0 + rev: v2.5.0 hooks: - id: pycln args: [--config=pyproject.toml] From 4b5759c5bf9ac65561ce2b3d7d609dc28f7f40e9 Mon Sep 17 00:00:00 2001 From: Eric Kerfoot Date: Wed, 29 Jan 2025 15:05:51 +0000 Subject: [PATCH 5/7] Formatting updates Signed-off-by: Eric Kerfoot --- monai/data/meta_tensor.py | 3 ++- monai/utils/jupyter_utils.py | 2 +- monai/visualize/img2tensorboard.py | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/monai/data/meta_tensor.py b/monai/data/meta_tensor.py index 2194f719b1..6425bc0a4f 100644 --- a/monai/data/meta_tensor.py +++ b/monai/data/meta_tensor.py @@ -609,5 +609,6 @@ def print_verbose(self) -> None: print(self.meta.__repr__()) -if hasattr(torch.serialization,"add_safe_globals"): +# needed in later versions of Pytorch to indicate the class is safe for serialisation +if hasattr(torch.serialization, "add_safe_globals"): torch.serialization.add_safe_globals([MetaTensor]) diff --git a/monai/utils/jupyter_utils.py b/monai/utils/jupyter_utils.py index b1b43a6767..c93e93dcb9 100644 --- a/monai/utils/jupyter_utils.py +++ b/monai/utils/jupyter_utils.py @@ -234,7 +234,7 @@ def plot_engine_status( def _get_loss_from_output( - output: list[torch.Tensor | dict[str, torch.Tensor]] | dict[str, torch.Tensor] | torch.Tensor + output: list[torch.Tensor | dict[str, torch.Tensor]] | dict[str, torch.Tensor] | torch.Tensor, ) -> torch.Tensor: """Returns a single value from the network output, which is a dict or tensor.""" diff --git a/monai/visualize/img2tensorboard.py b/monai/visualize/img2tensorboard.py index 677640bd04..fd328f2c7a 100644 --- a/monai/visualize/img2tensorboard.py +++ b/monai/visualize/img2tensorboard.py @@ -65,11 +65,11 @@ def _image3_animated_gif( img_str = b"" for b_data in PIL.GifImagePlugin.getheader(ims[0])[0]: img_str += b_data - img_str += b"\x21\xFF\x0B\x4E\x45\x54\x53\x43\x41\x50" b"\x45\x32\x2E\x30\x03\x01\x00\x00\x00" + img_str += b"\x21\xff\x0b\x4e\x45\x54\x53\x43\x41\x50" b"\x45\x32\x2e\x30\x03\x01\x00\x00\x00" for i in ims: for b_data in PIL.GifImagePlugin.getdata(i): img_str += b_data - img_str += b"\x3B" + img_str += b"\x3b" summary = SummaryX if has_tensorboardx and isinstance(writer, SummaryWriterX) else Summary summary_image_str = summary.Image(height=10, width=10, colorspace=1, encoded_image_string=img_str) From a1d9ac5d92c958d23adb66867df72d83008a4774 Mon Sep 17 00:00:00 2001 From: Eric Kerfoot Date: Wed, 29 Jan 2025 15:34:16 +0000 Subject: [PATCH 6/7] Attempting isort fix, version 6.0 doesn't agree with black about format choices Signed-off-by: Eric Kerfoot --- requirements-dev.txt | 2 +- tests/test_meta_tensor.py | 2 +- tests/test_zarr_avg_merger.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index bffe304df4..c9730ee651 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -18,7 +18,7 @@ pep8-naming pycodestyle pyflakes black>=22.12 -isort>=5.1 +isort>=5.1, <6.0 ruff pytype>=2020.6.1; platform_system != "Windows" types-setuptools diff --git a/tests/test_meta_tensor.py b/tests/test_meta_tensor.py index 4e0c57d767..60b6019703 100644 --- a/tests/test_meta_tensor.py +++ b/tests/test_meta_tensor.py @@ -256,7 +256,7 @@ def test_pickling(self): with tempfile.TemporaryDirectory() as tmp_dir: fname = os.path.join(tmp_dir, "im.pt") torch.save(m, fname) - m2 = torch.load(fname, weights_only=False) + m2 = torch.load(fname) if not isinstance(m2, MetaTensor) and not pytorch_after(1, 8, 1): warnings.warn("Old version of pytorch. pickling converts `MetaTensor` to `torch.Tensor`.") m = m.as_tensor() diff --git a/tests/test_zarr_avg_merger.py b/tests/test_zarr_avg_merger.py index e82bca8239..64e8fbde71 100644 --- a/tests/test_zarr_avg_merger.py +++ b/tests/test_zarr_avg_merger.py @@ -202,7 +202,7 @@ # test for LZ4 compressor TEST_CASE_13_COMPRESSOR_LZ4 = [ - dict(merged_shape=TENSOR_4x4.shape, compressor=zarr.codecs.BloscCodec), + dict(merged_shape=TENSOR_4x4.shape, compressor="LZ4"), [ (TENSOR_4x4[..., :2, :2], (0, 0)), (TENSOR_4x4[..., :2, 2:], (0, 2)), @@ -291,7 +291,7 @@ 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"] = arguments["compressor"] + arguments["compressor"] = codec_reg[arguments["compressor"].lower()]() if "value_compressor" in arguments: if arguments["value_compressor"] != "default": arguments["value_compressor"] = codec_reg[arguments["value_compressor"].lower()]() From ea15e2af2086032049ada4dd73f0498bd4822852 Mon Sep 17 00:00:00 2001 From: Eric Kerfoot Date: Fri, 31 Jan 2025 14:55:20 +0000 Subject: [PATCH 7/7] Fix Pytorch version to below 2.6 for now Signed-off-by: Eric Kerfoot --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index e184322c13..85e7312f5d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -torch>=1.9 +torch>=1.9,<2.6 numpy>=1.24,<2.0