173173
174174# DTI.PAR values for bvecs
175175DTI_PAR_BVALS = [1000 ] * 6 + [0 , 1000 ]
176+ # Numpy's argsort can be unstable so write indices manually
177+ DTI_PAR_BVALS_SORT_IDCS = [6 , 0 , 1 , 2 , 3 , 4 , 5 , 7 ]
176178
177179EXAMPLE_IMAGES = [
178180 # Parameters come from load of Philips' conversion to NIfTI
192194]
193195
194196
195- def _shuffle (arr ):
196- """Return a copy of the array with entries shuffled.
197-
198- Needed to avoid a bug in np.random.shuffle for numpy 1.7.
199- see: numpy/numpy#4286
200- """
201- return arr [np .argsort (np .random .randn (len (arr )))]
202-
203-
204197def test_top_level_load ():
205198 # Test PARREC images can be loaded from nib.load
206199 img = top_load (EG_PAR )
@@ -332,7 +325,7 @@ def test_sorting_dual_echo_T1():
332325 t1_hdr = PARRECHeader .from_fileobj (fobj , strict_sort = True )
333326
334327 # should get the correct order even if we randomly shuffle the order
335- t1_hdr . image_defs = _shuffle (t1_hdr .image_defs )
328+ np . random . shuffle (t1_hdr .image_defs )
336329
337330 sorted_indices = t1_hdr .get_sorted_slice_indices ()
338331 sorted_echos = t1_hdr .image_defs ['echo number' ][sorted_indices ]
@@ -363,7 +356,7 @@ def test_sorting_multiple_echos_and_contrasts():
363356 t1_hdr = PARRECHeader .from_fileobj (fobj , strict_sort = True )
364357
365358 # should get the correct order even if we randomly shuffle the order
366- t1_hdr . image_defs = _shuffle (t1_hdr .image_defs )
359+ np . random . shuffle (t1_hdr .image_defs )
367360
368361 sorted_indices = t1_hdr .get_sorted_slice_indices ()
369362 sorted_slices = t1_hdr .image_defs ['slice number' ][sorted_indices ]
@@ -402,7 +395,7 @@ def test_sorting_multiecho_ASL():
402395 asl_hdr = PARRECHeader .from_fileobj (fobj , strict_sort = True )
403396
404397 # should get the correct order even if we randomly shuffle the order
405- asl_hdr . image_defs = _shuffle (asl_hdr .image_defs )
398+ np . random . shuffle (asl_hdr .image_defs )
406399
407400 sorted_indices = asl_hdr .get_sorted_slice_indices ()
408401 sorted_slices = asl_hdr .image_defs ['slice number' ][sorted_indices ]
@@ -524,7 +517,7 @@ def test_diffusion_parameters_strict_sort():
524517 dti_hdr = PARRECHeader .from_fileobj (fobj , strict_sort = True )
525518
526519 # should get the correct order even if we randomly shuffle the order
527- dti_hdr . image_defs = _shuffle (dti_hdr .image_defs )
520+ np . random . shuffle (dti_hdr .image_defs )
528521
529522 assert dti_hdr .get_data_shape () == (80 , 80 , 10 , 8 )
530523 assert dti_hdr .general_info ['diffusion' ] == 1
@@ -533,7 +526,7 @@ def test_diffusion_parameters_strict_sort():
533526 # DTI_PAR_BVECS gives bvecs copied from first slice each vol in DTI.PAR
534527 # Permute to match bvec directions to acquisition directions
535528 # note that bval sorting occurs prior to bvec sorting
536- assert_almost_equal (bvecs , DTI_PAR_BVECS [np .ix_ (np . argsort ( DTI_PAR_BVALS ) , [2 , 0 , 1 ])])
529+ assert_almost_equal (bvecs , DTI_PAR_BVECS [np .ix_ (DTI_PAR_BVALS_SORT_IDCS , [2 , 0 , 1 ])])
537530 # Check q vectors
538531 assert_almost_equal (dti_hdr .get_q_vectors (), bvals [:, None ] * bvecs )
539532
0 commit comments