Skip to content

Commit 9d95e73

Browse files
authored
Merge pull request #103 from oesteban/fix/doctests-broken
FIX: Revise vector tests broken when addressing other issues
2 parents 9d83d50 + 695c43c commit 9d95e73

File tree

4 files changed

+42
-24
lines changed

4 files changed

+42
-24
lines changed

dmriprep/interfaces/vectors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def _run_interface(self, runtime):
6868
rasb_file = _undefined(self.inputs, 'in_rasb')
6969

7070
table = DiffusionGradientTable(
71-
self.inputs.dwi_file,
71+
dwi_file=self.inputs.dwi_file,
7272
bvecs=_undefined(self.inputs, 'in_bvec'),
7373
bvals=_undefined(self.inputs, 'in_bval'),
7474
rasb_file=rasb_file,

dmriprep/utils/tests/test_vectors.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ def test_corruption(tmpdir, dipy_test_data, monkeypatch):
4646
bval_no_b0[0] = 51
4747
with pytest.raises(ValueError):
4848
dgt = v.DiffusionGradientTable(dwi_file=dipy_test_data['dwi_file'],
49-
bvals=bval_no_b0, bvecs=bvecs)
49+
bvals=bval_no_b0, bvecs=bvecs,
50+
raise_inconsistent=True)
5051
bvec_no_b0 = bvecs.copy()
5152
bvec_no_b0[0] = np.array([1.0, 0.0, 0.0])
5253
with pytest.raises(ValueError):
5354
dgt = v.DiffusionGradientTable(dwi_file=dipy_test_data['dwi_file'],
54-
bvals=bvals, bvecs=bvec_no_b0)
55+
bvals=bvals, bvecs=bvec_no_b0,
56+
raise_inconsistent=True)
5557

5658
# Corrupt b0 b-val
5759
bval_odd_b0 = bvals.copy()

dmriprep/utils/vectors.py

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,57 @@ class DiffusionGradientTable:
1515

1616
__slots__ = [
1717
"_affine",
18-
"_gradients",
18+
"_b0_thres",
1919
"_b_scale",
20-
"_bvecs",
2120
"_bvals",
21+
"_bvec_norm_epsilon",
22+
"_bvecs",
23+
"_gradients",
2224
"_normalized",
25+
"_raise_inconsistent",
2326
"_transforms",
24-
"_b0_thres",
25-
"_bvec_norm_epsilon",
2627
]
2728

2829
def __init__(
2930
self,
30-
dwi_file=None,
31-
bvecs=None,
31+
b0_threshold=B0_THRESHOLD,
32+
b_scale=True,
3233
bvals=None,
34+
bvec_norm_epsilon=BVEC_NORM_EPSILON,
35+
bvecs=None,
36+
dwi_file=None,
37+
raise_inconsistent=False,
3338
rasb_file=None,
34-
b_scale=True,
3539
transforms=None,
36-
b0_threshold=B0_THRESHOLD,
37-
bvec_norm_epsilon=BVEC_NORM_EPSILON,
3840
):
3941
"""
4042
Create a new table of diffusion gradients.
4143
4244
Parameters
4345
----------
44-
dwi_file : str or os.pathlike or nibabel.spatialimage
45-
File path to the diffusion-weighted image series to which the
46-
bvecs/bvals correspond.
46+
b0_threshold : :obj:`float`
47+
The upper threshold to consider a low-b shell as :math:`b=0`.
48+
b_scale : :obj:`bool`
49+
Automatically scale the *b*-values with the norm of the corresponding
50+
*b*-vectors before the latter are normalized.
4751
bvals : str or os.pathlike or numpy.ndarray
4852
File path of the b-values.
53+
b_vec_norm_epsilon : :obj:`float`
54+
The minimum difference in the norm of two *b*-vectors to consider them different.
4955
bvecs : str or os.pathlike or numpy.ndarray
5056
File path of the b-vectors.
57+
dwi_file : str or os.pathlike or nibabel.spatialimage
58+
File path to the diffusion-weighted image series to which the
59+
bvecs/bvals correspond.
60+
raise_inconsistent : :obj:`bool`
61+
If ``True``, a :obj:`ValueError` is raised when discrepancies are found between the
62+
:math:`b=0` lists calculated from the *b*-vectors and *b*-values, respectively.
63+
If ``False``, inconsistencies will be mended (if possible).
5164
rasb_file : str or os.pathlike
5265
File path to a RAS-B gradient table. If rasb_file is provided,
5366
then bvecs and bvals will be dismissed.
54-
b_scale : bool
55-
Whether b-values should be normalized.
67+
transforms : :obj:`list` of :obj:`numpy.ndarray`
68+
A list of affine transforms to rotate the list of vectors.
5669
5770
Example
5871
-------
@@ -75,16 +88,18 @@ def __init__(
7588
>>> out_rasb_mat = check.reorient_rasb()
7689
>>> np.allclose(old_rasb_mat, out_rasb_mat)
7790
True
91+
7892
"""
79-
self._transforms = transforms
80-
self._b_scale = b_scale
93+
self._affine = None
8194
self._b0_thres = b0_threshold
82-
self._bvec_norm_epsilon = bvec_norm_epsilon
83-
self._gradients = None
95+
self._b_scale = b_scale
8496
self._bvals = None
97+
self._bvec_norm_epsilon = bvec_norm_epsilon
8598
self._bvecs = None
86-
self._affine = None
99+
self._gradients = None
87100
self._normalized = False
101+
self._raise_inconsistent = raise_inconsistent
102+
self._transforms = transforms
88103

89104
if dwi_file is not None:
90105
self.affine = dwi_file
@@ -175,6 +190,7 @@ def normalize(self):
175190
b0_threshold=self._b0_thres,
176191
bvec_norm_epsilon=self._bvec_norm_epsilon,
177192
b_scale=self._b_scale,
193+
raise_error=self._raise_inconsistent,
178194
)
179195
self._normalized = True
180196

@@ -295,7 +311,7 @@ def normalize_gradients(bvecs, bvals, b0_threshold=B0_THRESHOLD,
295311
--------
296312
>>> bvecs = np.vstack((np.zeros(3), 2.0 * np.eye(3), -0.8 * np.eye(3), np.ones(3)))
297313
>>> bvals = np.array([1000] * bvecs.shape[0])
298-
>>> normalize_gradients(bvecs, bvals, 50) # doctest: +IGNORE_EXCEPTION_DETAIL
314+
>>> normalize_gradients(bvecs, bvals, 50, raise_error=True)
299315
Traceback (most recent call last):
300316
ValueError:
301317

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ exclude =
111111
[tool:pytest]
112112
norecursedirs = .* _*
113113
addopts = -vx --doctest-modules
114-
doctest_optionflags = ALLOW_UNICODE NORMALIZE_WHITESPACE
114+
doctest_optionflags = ALLOW_UNICODE NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL
115115
env =
116116
PYTHONHASHSEED=0
117117
filterwarnings =

0 commit comments

Comments
 (0)