173
173
174
174
# DTI.PAR values for bvecs
175
175
DTI_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 ]
176
178
177
179
EXAMPLE_IMAGES = [
178
180
# Parameters come from load of Philips' conversion to NIfTI
192
194
]
193
195
194
196
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
-
204
197
def test_top_level_load ():
205
198
# Test PARREC images can be loaded from nib.load
206
199
img = top_load (EG_PAR )
@@ -332,7 +325,7 @@ def test_sorting_dual_echo_T1():
332
325
t1_hdr = PARRECHeader .from_fileobj (fobj , strict_sort = True )
333
326
334
327
# 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 )
336
329
337
330
sorted_indices = t1_hdr .get_sorted_slice_indices ()
338
331
sorted_echos = t1_hdr .image_defs ['echo number' ][sorted_indices ]
@@ -363,7 +356,7 @@ def test_sorting_multiple_echos_and_contrasts():
363
356
t1_hdr = PARRECHeader .from_fileobj (fobj , strict_sort = True )
364
357
365
358
# 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 )
367
360
368
361
sorted_indices = t1_hdr .get_sorted_slice_indices ()
369
362
sorted_slices = t1_hdr .image_defs ['slice number' ][sorted_indices ]
@@ -402,7 +395,7 @@ def test_sorting_multiecho_ASL():
402
395
asl_hdr = PARRECHeader .from_fileobj (fobj , strict_sort = True )
403
396
404
397
# 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 )
406
399
407
400
sorted_indices = asl_hdr .get_sorted_slice_indices ()
408
401
sorted_slices = asl_hdr .image_defs ['slice number' ][sorted_indices ]
@@ -524,7 +517,7 @@ def test_diffusion_parameters_strict_sort():
524
517
dti_hdr = PARRECHeader .from_fileobj (fobj , strict_sort = True )
525
518
526
519
# 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 )
528
521
529
522
assert dti_hdr .get_data_shape () == (80 , 80 , 10 , 8 )
530
523
assert dti_hdr .general_info ['diffusion' ] == 1
@@ -533,7 +526,7 @@ def test_diffusion_parameters_strict_sort():
533
526
# DTI_PAR_BVECS gives bvecs copied from first slice each vol in DTI.PAR
534
527
# Permute to match bvec directions to acquisition directions
535
528
# 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 ])])
537
530
# Check q vectors
538
531
assert_almost_equal (dti_hdr .get_q_vectors (), bvals [:, None ] * bvecs )
539
532
0 commit comments