Repository: Project-MONAI/MONAI

Issue:
Bug: NumpyReader for .npz files with variable names.
https://github.com/Project-MONAI/MONAI/blob/6e5fdc0066d9b21a2b50153f5ba1951d0d82f8e4/monai/data/image_reader.py#L1071

If npz_keys is not given, `NumpyReader` assumes variables in .npz files to be named arr_0, arr_1,...
This may or may not be true. It would be better to use the variable names from the file like so:
```python
                npz_keys = self.npz_keys if self.npz_keys else list( img.keys() ) # or img.files
```
Bug: NumpyReader for .npz files with variable names.
https://github.com/Project-MONAI/MONAI/blob/6e5fdc0066d9b21a2b50153f5ba1951d0d82f8e4/monai/data/image_reader.py#L1071

If npz_keys is not given, `NumpyReader` assumes variables in .npz files to be named arr_0, arr_1,...
This may or may not be true. It would be better to use the variable names from the file like so:
```python
                npz_keys = self.npz_keys if self.npz_keys else list( img.keys() ) # or img.files
```


Discussion:

Resolving patch:
diff --git a/monai/data/image_reader.py b/monai/data/image_reader.py
--- 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:


Test patch:
diff --git a/tests/test_numpy_reader.py b/tests/test_numpy_reader.py
--- 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])