Skip to content

Commit 8a701c2

Browse files
add tests
Signed-off-by: Yiheng Wang <[email protected]>
1 parent a50009e commit 8a701c2

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

monai/data/image_reader.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,8 @@ def _get_array_data_from_gpu(self, img, filename):
942942
buffer = cp.empty(expected_pixel_data_length, dtype=cp.int8)
943943
f.read(buffer, expected_pixel_data_length, offset)
944944

945-
data = buffer.view(dtype).reshape((number_of_frames, rows, columns))
945+
new_shape = (number_of_frames, rows, columns) if number_of_frames > 1 else (rows, columns)
946+
data = buffer.view(dtype).reshape(new_shape)
946947

947948
return data
948949

tests/test_load_image.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ def get_data(self, _obj):
168168
# test reader consistency between PydicomReader and ITKReader on dicom data
169169
TEST_CASE_22 = ["tests/testing_data/CT_DICOM"]
170170

171+
# test pydicom gpu reader
172+
TEST_CASE_GPU_5 = [{"reader": "PydicomReader", "to_gpu": True}, "tests/testing_data/CT_DICOM", (16, 16, 4), (16, 16, 4)]
173+
174+
TEST_CASE_GPU_6 = [
175+
{"reader": "PydicomReader", "ensure_channel_first": True, "force": True, "to_gpu": True},
176+
"tests/testing_data/CT_DICOM",
177+
(16, 16, 4),
178+
(1, 16, 16, 4),
179+
]
180+
171181
TESTS_META = []
172182
for track_meta in (False, True):
173183
TESTS_META.append([{}, (128, 128, 128), track_meta])
@@ -272,6 +282,26 @@ def test_itk_dicom_series_reader(self, input_param, filenames, expected_shape, e
272282
)
273283
self.assertTupleEqual(result.shape, expected_np_shape)
274284

285+
@SkipIfNoModule("pydicom")
286+
@SkipIfNoModule("cupy")
287+
@SkipIfNoModule("kvikio")
288+
@parameterized.expand([TEST_CASE_GPU_5, TEST_CASE_GPU_6])
289+
def test_pydicom_gpu_reader(self, input_param, filenames, expected_shape, expected_np_shape):
290+
result = LoadImage(image_only=True, **input_param)(filenames)
291+
self.assertEqual(result.meta["filename_or_obj"], f"{Path(filenames)}")
292+
assert_allclose(
293+
result.affine,
294+
torch.tensor(
295+
[
296+
[-0.488281, 0.0, 0.0, 125.0],
297+
[0.0, -0.488281, 0.0, 128.100006],
298+
[0.0, 0.0, 68.33333333, -99.480003],
299+
[0.0, 0.0, 0.0, 1.0],
300+
]
301+
),
302+
)
303+
self.assertTupleEqual(result.shape, expected_np_shape)
304+
275305
def test_no_files(self):
276306
with self.assertRaisesRegex(RuntimeError, "list index out of range"): # fname_regex excludes everything
277307
LoadImage(image_only=True, reader="PydicomReader", fname_regex=r"^(?!.*).*")("tests/testing_data/CT_DICOM")
@@ -318,6 +348,21 @@ def test_dicom_reader_consistency(self, filenames):
318348
np.testing.assert_allclose(pydicom_result, itk_result)
319349
np.testing.assert_allclose(pydicom_result.affine, itk_result.affine)
320350

351+
@SkipIfNoModule("pydicom")
352+
@SkipIfNoModule("cupy")
353+
@SkipIfNoModule("kvikio")
354+
@parameterized.expand([TEST_CASE_22])
355+
def test_pydicom_reader_gpu_cpu_consistency(self, filenames):
356+
gpu_param = {"reader": "PydicomReader", "to_gpu": True}
357+
cpu_param = {"reader": "PydicomReader", "to_gpu": False}
358+
for affine_flag in [True, False]:
359+
gpu_param["affine_lps_to_ras"] = affine_flag
360+
cpu_param["affine_lps_to_ras"] = affine_flag
361+
gpu_result = LoadImage(image_only=True, **gpu_param)(filenames)
362+
cpu_result = LoadImage(image_only=True, **cpu_param)(filenames)
363+
np.testing.assert_allclose(gpu_result.cpu(), cpu_result)
364+
np.testing.assert_allclose(gpu_result.affine.cpu(), cpu_result.affine)
365+
321366
def test_dicom_reader_consistency_single(self):
322367
itk_param = {"reader": "ITKReader"}
323368
pydicom_param = {"reader": "PydicomReader"}

0 commit comments

Comments
 (0)