Skip to content

Commit c385a53

Browse files
committed
FIX: Use stable argsort
1 parent 57a3add commit c385a53

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

nibabel/tests/test_parrec.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,6 @@
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]
178176

179177
EXAMPLE_IMAGES = [
180178
# Parameters come from load of Philips' conversion to NIfTI
@@ -526,7 +524,10 @@ def test_diffusion_parameters_strict_sort():
526524
# DTI_PAR_BVECS gives bvecs copied from first slice each vol in DTI.PAR
527525
# Permute to match bvec directions to acquisition directions
528526
# note that bval sorting occurs prior to bvec sorting
529-
assert_almost_equal(bvecs, DTI_PAR_BVECS[np.ix_(DTI_PAR_BVALS_SORT_IDCS, [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+
)
530531
# Check q vectors
531532
assert_almost_equal(dti_hdr.get_q_vectors(), bvals[:, None] * bvecs)
532533

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_BVALS_SORT_IDCS, DTI_PAR_BVECS
30+
from .test_parrec import DTI_PAR_BVALS, 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[..., DTI_PAR_BVALS_SORT_IDCS], 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)