@@ -168,6 +168,16 @@ def get_data(self, _obj):
168168# test reader consistency between PydicomReader and ITKReader on dicom data
169169TEST_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+
171181TESTS_META = []
172182for track_meta in (False , True ):
173183 TESTS_META .append ([{}, (128 , 128 , 128 ), track_meta ])
@@ -242,16 +252,17 @@ def test_nibabel_reader_gpu(self, input_param, filenames, expected_shape):
242252
243253 @parameterized .expand ([TEST_CASE_6 , TEST_CASE_7 , TEST_CASE_8 , TEST_CASE_8_1 , TEST_CASE_9 ])
244254 def test_itk_reader (self , input_param , filenames , expected_shape ):
245- test_image = np .random .rand (128 , 128 , 128 )
255+ test_image = torch .randint (0 , 256 , (128 , 128 , 128 ), dtype = torch .uint8 ).numpy ()
256+ print ("Test image value range:" , test_image .min (), test_image .max ())
246257 with tempfile .TemporaryDirectory () as tempdir :
247258 for i , name in enumerate (filenames ):
248259 filenames [i ] = os .path .join (tempdir , name )
249- itk_np_view = itk .image_view_from_array (test_image )
250- itk .imwrite (itk_np_view , filenames [i ])
260+ nib .save (nib .Nifti1Image (test_image , np .eye (4 )), filenames [i ])
251261 result = LoadImage (image_only = True , ** input_param )(filenames )
252- self .assertEqual (result .meta ["filename_or_obj" ], os .path .join (tempdir , "test_image.nii.gz" ))
253- diag = torch .as_tensor (np .diag ([- 1 , - 1 , 1 , 1 ]))
254- np .testing .assert_allclose (result .affine , diag )
262+ ext = "" .join (Path (name ).suffixes )
263+ self .assertEqual (result .meta ["filename_or_obj" ], os .path .join (tempdir , "test_image" + ext ))
264+ self .assertEqual (result .meta ["space" ], "RAS" )
265+ assert_allclose (result .affine , torch .eye (4 ))
255266 self .assertTupleEqual (result .shape , expected_shape )
256267
257268 @parameterized .expand ([TEST_CASE_10 , TEST_CASE_11 , TEST_CASE_12 , TEST_CASE_19 , TEST_CASE_20 , TEST_CASE_21 ])
@@ -271,6 +282,26 @@ def test_itk_dicom_series_reader(self, input_param, filenames, expected_shape, e
271282 )
272283 self .assertTupleEqual (result .shape , expected_np_shape )
273284
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+
274305 def test_no_files (self ):
275306 with self .assertRaisesRegex (RuntimeError , "list index out of range" ): # fname_regex excludes everything
276307 LoadImage (image_only = True , reader = "PydicomReader" , fname_regex = r"^(?!.*).*" )("tests/testing_data/CT_DICOM" )
@@ -317,6 +348,21 @@ def test_dicom_reader_consistency(self, filenames):
317348 np .testing .assert_allclose (pydicom_result , itk_result )
318349 np .testing .assert_allclose (pydicom_result .affine , itk_result .affine )
319350
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+
320366 def test_dicom_reader_consistency_single (self ):
321367 itk_param = {"reader" : "ITKReader" }
322368 pydicom_param = {"reader" : "PydicomReader" }
0 commit comments