Skip to content

Commit 57a3add

Browse files
committed
TEST: Remove potentially unstable argsort from parrec tests
1 parent b7022e0 commit 57a3add

File tree

2 files changed

+9
-16
lines changed

2 files changed

+9
-16
lines changed

nibabel/tests/test_parrec.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@
173173

174174
# DTI.PAR values for bvecs
175175
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]
176178

177179
EXAMPLE_IMAGES = [
178180
# Parameters come from load of Philips' conversion to NIfTI
@@ -192,15 +194,6 @@
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-
204197
def 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

nibabel/tests/test_scripts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from ..tmpdirs import InTemporaryDirectory
2828
from .nibabel_data import needs_nibabel_data
2929
from .scriptrunner import ScriptRunner
30-
from .test_parrec import DTI_PAR_BVALS, DTI_PAR_BVECS
30+
from .test_parrec import DTI_PAR_BVALS, DTI_PAR_BVALS_SORT_IDCS, DTI_PAR_BVECS
3131
from .test_parrec import EXAMPLE_IMAGES as PARREC_EXAMPLES
3232
from .test_parrec_data import AFF_OFF, BALLS
3333

@@ -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[..., DTI_PAR_BVALS_SORT_IDCS], data_sorted)
422422
del img
423423

424424
# Writes .ordering.csv if requested

0 commit comments

Comments
 (0)