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

Using keys in .npz files for NumpyReader #7148

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion monai/data/image_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
wyli marked this conversation as resolved.
Show resolved Hide resolved
for k in npz_keys:
img_.append(img[k])
else:
Expand Down
36 changes: 23 additions & 13 deletions tests/test_numpy_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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])
Expand Down