Skip to content

Commit 064e80d

Browse files
authored
Merge pull request #1234 from nipy/debug
TEST: Only use stable argsorts in PARREC tests
2 parents b7022e0 + c385a53 commit 064e80d

File tree

2 files changed

+9
-15
lines changed

2 files changed

+9
-15
lines changed

nibabel/tests/test_parrec.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,6 @@
192192
]
193193

194194

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-
204195
def test_top_level_load():
205196
# Test PARREC images can be loaded from nib.load
206197
img = top_load(EG_PAR)
@@ -332,7 +323,7 @@ def test_sorting_dual_echo_T1():
332323
t1_hdr = PARRECHeader.from_fileobj(fobj, strict_sort=True)
333324

334325
# should get the correct order even if we randomly shuffle the order
335-
t1_hdr.image_defs = _shuffle(t1_hdr.image_defs)
326+
np.random.shuffle(t1_hdr.image_defs)
336327

337328
sorted_indices = t1_hdr.get_sorted_slice_indices()
338329
sorted_echos = t1_hdr.image_defs['echo number'][sorted_indices]
@@ -363,7 +354,7 @@ def test_sorting_multiple_echos_and_contrasts():
363354
t1_hdr = PARRECHeader.from_fileobj(fobj, strict_sort=True)
364355

365356
# should get the correct order even if we randomly shuffle the order
366-
t1_hdr.image_defs = _shuffle(t1_hdr.image_defs)
357+
np.random.shuffle(t1_hdr.image_defs)
367358

368359
sorted_indices = t1_hdr.get_sorted_slice_indices()
369360
sorted_slices = t1_hdr.image_defs['slice number'][sorted_indices]
@@ -402,7 +393,7 @@ def test_sorting_multiecho_ASL():
402393
asl_hdr = PARRECHeader.from_fileobj(fobj, strict_sort=True)
403394

404395
# should get the correct order even if we randomly shuffle the order
405-
asl_hdr.image_defs = _shuffle(asl_hdr.image_defs)
396+
np.random.shuffle(asl_hdr.image_defs)
406397

407398
sorted_indices = asl_hdr.get_sorted_slice_indices()
408399
sorted_slices = asl_hdr.image_defs['slice number'][sorted_indices]
@@ -524,7 +515,7 @@ def test_diffusion_parameters_strict_sort():
524515
dti_hdr = PARRECHeader.from_fileobj(fobj, strict_sort=True)
525516

526517
# should get the correct order even if we randomly shuffle the order
527-
dti_hdr.image_defs = _shuffle(dti_hdr.image_defs)
518+
np.random.shuffle(dti_hdr.image_defs)
528519

529520
assert dti_hdr.get_data_shape() == (80, 80, 10, 8)
530521
assert dti_hdr.general_info['diffusion'] == 1
@@ -533,7 +524,10 @@ def test_diffusion_parameters_strict_sort():
533524
# DTI_PAR_BVECS gives bvecs copied from first slice each vol in DTI.PAR
534525
# Permute to match bvec directions to acquisition directions
535526
# 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])])
527+
assert_almost_equal(
528+
bvecs,
529+
DTI_PAR_BVECS[np.ix_(np.argsort(DTI_PAR_BVALS, kind='stable'), [2, 0, 1])],
530+
)
537531
# Check q vectors
538532
assert_almost_equal(dti_hdr.get_q_vectors(), bvals[:, None] * bvecs)
539533

nibabel/tests/test_scripts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ def test_parrec2nii_with_data():
418418
assert_almost_equal(np.loadtxt('DTI.bvals'), np.sort(DTI_PAR_BVALS))
419419
img = load('DTI.nii')
420420
data_sorted = img.get_fdata()
421-
assert_almost_equal(data[..., np.argsort(DTI_PAR_BVALS)], data_sorted)
421+
assert_almost_equal(data[..., np.argsort(DTI_PAR_BVALS, kind='stable')], data_sorted)
422422
del img
423423

424424
# Writes .ordering.csv if requested

0 commit comments

Comments
 (0)