Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dmriprep/interfaces/vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def _run_interface(self, runtime):
rasb_file = _undefined(self.inputs, 'in_rasb')

table = DiffusionGradientTable(
self.inputs.dwi_file,
dwi_file=self.inputs.dwi_file,
bvecs=_undefined(self.inputs, 'in_bvec'),
bvals=_undefined(self.inputs, 'in_bval'),
rasb_file=rasb_file,
Expand Down
6 changes: 4 additions & 2 deletions dmriprep/utils/tests/test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ def test_corruption(tmpdir, dipy_test_data, monkeypatch):
bval_no_b0[0] = 51
with pytest.raises(ValueError):
dgt = v.DiffusionGradientTable(dwi_file=dipy_test_data['dwi_file'],
bvals=bval_no_b0, bvecs=bvecs)
bvals=bval_no_b0, bvecs=bvecs,
raise_inconsistent=True)
bvec_no_b0 = bvecs.copy()
bvec_no_b0[0] = np.array([1.0, 0.0, 0.0])
with pytest.raises(ValueError):
dgt = v.DiffusionGradientTable(dwi_file=dipy_test_data['dwi_file'],
bvals=bvals, bvecs=bvec_no_b0)
bvals=bvals, bvecs=bvec_no_b0,
raise_inconsistent=True)

# Corrupt b0 b-val
bval_odd_b0 = bvals.copy()
Expand Down
56 changes: 36 additions & 20 deletions dmriprep/utils/vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,57 @@ class DiffusionGradientTable:

__slots__ = [
"_affine",
"_gradients",
"_b0_thres",
"_b_scale",
"_bvecs",
"_bvals",
"_bvec_norm_epsilon",
"_bvecs",
"_gradients",
"_normalized",
"_raise_inconsistent",
"_transforms",
"_b0_thres",
"_bvec_norm_epsilon",
]

def __init__(
self,
dwi_file=None,
bvecs=None,
b0_threshold=B0_THRESHOLD,
b_scale=True,
bvals=None,
bvec_norm_epsilon=BVEC_NORM_EPSILON,
bvecs=None,
dwi_file=None,
raise_inconsistent=False,
rasb_file=None,
b_scale=True,
transforms=None,
b0_threshold=B0_THRESHOLD,
bvec_norm_epsilon=BVEC_NORM_EPSILON,
):
"""
Create a new table of diffusion gradients.

Parameters
----------
dwi_file : str or os.pathlike or nibabel.spatialimage
File path to the diffusion-weighted image series to which the
bvecs/bvals correspond.
b0_threshold : :obj:`float`
The upper threshold to consider a low-b shell as :math:`b=0`.
b_scale : :obj:`bool`
Automatically scale the *b*-values with the norm of the corresponding
*b*-vectors before the latter are normalized.
bvals : str or os.pathlike or numpy.ndarray
File path of the b-values.
b_vec_norm_epsilon : :obj:`float`
The minimum difference in the norm of two *b*-vectors to consider them different.
bvecs : str or os.pathlike or numpy.ndarray
File path of the b-vectors.
dwi_file : str or os.pathlike or nibabel.spatialimage
File path to the diffusion-weighted image series to which the
bvecs/bvals correspond.
raise_inconsistent : :obj:`bool`
If ``True``, a :obj:`ValueError` is raised when discrepancies are found between the
:math:`b=0` lists calculated from the *b*-vectors and *b*-values, respectively.
If ``False``, inconsistencies will be mended (if possible).
rasb_file : str or os.pathlike
File path to a RAS-B gradient table. If rasb_file is provided,
then bvecs and bvals will be dismissed.
b_scale : bool
Whether b-values should be normalized.
transforms : :obj:`list` of :obj:`numpy.ndarray`
A list of affine transforms to rotate the list of vectors.

Example
-------
Expand All @@ -75,16 +88,18 @@ def __init__(
>>> out_rasb_mat = check.reorient_rasb()
>>> np.allclose(old_rasb_mat, out_rasb_mat)
True

"""
self._transforms = transforms
self._b_scale = b_scale
self._affine = None
self._b0_thres = b0_threshold
self._bvec_norm_epsilon = bvec_norm_epsilon
self._gradients = None
self._b_scale = b_scale
self._bvals = None
self._bvec_norm_epsilon = bvec_norm_epsilon
self._bvecs = None
self._affine = None
self._gradients = None
self._normalized = False
self._raise_inconsistent = raise_inconsistent
self._transforms = transforms

if dwi_file is not None:
self.affine = dwi_file
Expand Down Expand Up @@ -175,6 +190,7 @@ def normalize(self):
b0_threshold=self._b0_thres,
bvec_norm_epsilon=self._bvec_norm_epsilon,
b_scale=self._b_scale,
raise_error=self._raise_inconsistent,
)
self._normalized = True

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

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ exclude =
[tool:pytest]
norecursedirs = .* _*
addopts = -vx --doctest-modules
doctest_optionflags = ALLOW_UNICODE NORMALIZE_WHITESPACE
doctest_optionflags = ALLOW_UNICODE NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL
env =
PYTHONHASHSEED=0
filterwarnings =
Expand Down