From ca414254e7d9495e832ee4692c7911b3a5b96dad Mon Sep 17 00:00:00 2001 From: KumoLiu Date: Fri, 20 Oct 2023 11:35:58 +0800 Subject: [PATCH 1/2] fix #7147 Signed-off-by: KumoLiu --- monai/data/image_reader.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/data/image_reader.py b/monai/data/image_reader.py index ce77c549ce..4c7f2c8c3b 100644 --- a/monai/data/image_reader.py +++ b/monai/data/image_reader.py @@ -1068,7 +1068,7 @@ def read(self, data: Sequence[PathLike] | PathLike, **kwargs): img = np.load(name, allow_pickle=True, **kwargs_) if Path(name).name.endswith(".npz"): # load expected items from NPZ file - npz_keys = [f"arr_{i}" for i in range(len(img))] if self.npz_keys is None else self.npz_keys + npz_keys = list(img.keys()) if self.npz_keys is None else self.npz_keys for k in npz_keys: img_.append(img[k]) else: From 9d4e50eb671f58e20f1020de9e737da97a157c12 Mon Sep 17 00:00:00 2001 From: KumoLiu Date: Fri, 20 Oct 2023 15:47:36 +0800 Subject: [PATCH 2/2] add more test Signed-off-by: KumoLiu --- tests/test_numpy_reader.py | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/tests/test_numpy_reader.py b/tests/test_numpy_reader.py index 393f613163..eeff2922ad 100644 --- a/tests/test_numpy_reader.py +++ b/tests/test_numpy_reader.py @@ -19,7 +19,7 @@ import numpy as np from monai.data import DataLoader, Dataset, NumpyReader -from monai.transforms import LoadImaged +from monai.transforms import LoadImage, LoadImaged from tests.utils import assert_allclose @@ -97,22 +97,32 @@ def test_kwargs(self): def test_dataloader(self): test_data = np.random.randint(0, 256, size=[3, 4, 5]) - datalist = [] + datalist_dict, datalist_array = [], [] with tempfile.TemporaryDirectory() as tempdir: for i in range(4): filepath = os.path.join(tempdir, f"test_data{i}.npz") np.savez(filepath, test_data) - datalist.append({"image": filepath}) - - num_workers = 2 if sys.platform == "linux" else 0 - loader = DataLoader( - Dataset(data=datalist, transform=LoadImaged(keys="image", reader=NumpyReader())), - batch_size=2, - num_workers=num_workers, - ) - for d in loader: - for c in d["image"]: - assert_allclose(c, test_data, type_test=False) + datalist_dict.append({"image": filepath}) + datalist_array.append(filepath) + + num_workers = 2 if sys.platform == "linux" else 0 + loader = DataLoader( + Dataset(data=datalist_dict, transform=LoadImaged(keys="image", reader=NumpyReader())), + batch_size=2, + num_workers=num_workers, + ) + for d in loader: + for c in d["image"]: + assert_allclose(c, test_data, type_test=False) + + loader = DataLoader( + Dataset(data=datalist_array, transform=LoadImage(reader=NumpyReader())), + batch_size=2, + num_workers=num_workers, + ) + for d in loader: + for c in d: + assert_allclose(c, test_data, type_test=False) def test_channel_dim(self): test_data = np.random.randint(0, 256, size=[3, 4, 5, 2])