Skip to content

Commit 86ebe84

Browse files
committed
expose bmag parameter and check number of b0s consistent after rounding
1 parent 25652d6 commit 86ebe84

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

dmriprep/interfaces/vectors.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class _CheckGradientTableInputSpec(BaseInterfaceInputSpec):
2020
in_rasb = File(exists=True, xor=["in_bval", "in_bvec"])
2121
b0_threshold = traits.Float(B0_THRESHOLD, usedefault=True)
2222
bvec_norm_epsilon = traits.Float(BVEC_NORM_EPSILON, usedefault=True)
23+
b_mag = traits.Int(None, usedefault=True)
2324
b_scale = traits.Bool(True, usedefault=True)
2425

2526

@@ -84,6 +85,7 @@ def _run_interface(self, runtime):
8485
bvecs=_undefined(self.inputs, "in_bvec"),
8586
bvals=_undefined(self.inputs, "in_bval"),
8687
rasb_file=rasb_file,
88+
b_mag=self.inputs.b_mag,
8789
b_scale=self.inputs.b_scale,
8890
bvec_norm_epsilon=self.inputs.bvec_norm_epsilon,
8991
b0_threshold=self.inputs.b0_threshold,

dmriprep/utils/vectors.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class DiffusionGradientTable:
1717
__slots__ = [
1818
"_affine",
1919
"_b0_thres",
20+
"_b_mag",
2021
"_b_scale",
2122
"_bvals",
2223
"_bvec_norm_epsilon",
@@ -30,6 +31,7 @@ class DiffusionGradientTable:
3031
def __init__(
3132
self,
3233
b0_threshold=B0_THRESHOLD,
34+
b_mag=None,
3335
b_scale=True,
3436
bvals=None,
3537
bvec_norm_epsilon=BVEC_NORM_EPSILON,
@@ -46,12 +48,14 @@ def __init__(
4648
----------
4749
b0_threshold : :obj:`float`
4850
The upper threshold to consider a low-b shell as :math:`b=0`.
51+
b_mag : :obj:`int`
52+
The order of magnitude to round the b-values.
4953
b_scale : :obj:`bool`
5054
Automatically scale the *b*-values with the norm of the corresponding
5155
*b*-vectors before the latter are normalized.
5256
bvals : str or os.pathlike or numpy.ndarray
5357
File path of the b-values.
54-
b_vec_norm_epsilon : :obj:`float`
58+
bvec_norm_epsilon : :obj:`float`
5559
The minimum difference in the norm of two *b*-vectors to consider them different.
5660
bvecs : str or os.pathlike or numpy.ndarray
5761
File path of the b-vectors.
@@ -93,6 +97,7 @@ def __init__(
9397
"""
9498
self._affine = None
9599
self._b0_thres = b0_threshold
100+
self._b_mag = b_mag
96101
self._b_scale = b_scale
97102
self._bvals = None
98103
self._bvec_norm_epsilon = bvec_norm_epsilon
@@ -195,6 +200,7 @@ def normalize(self):
195200
self.bvals,
196201
b0_threshold=self._b0_thres,
197202
bvec_norm_epsilon=self._bvec_norm_epsilon,
203+
b_mag=self._b_mag,
198204
b_scale=self._b_scale,
199205
raise_error=self._raise_inconsistent,
200206
)
@@ -290,6 +296,7 @@ def normalize_gradients(
290296
bvals,
291297
b0_threshold=B0_THRESHOLD,
292298
bvec_norm_epsilon=BVEC_NORM_EPSILON,
299+
b_mag=None,
293300
b_scale=True,
294301
raise_error=False,
295302
):
@@ -361,17 +368,25 @@ def normalize_gradients(
361368
raise ValueError(msg)
362369
config.loggers.cli.warning(msg)
363370

364-
# Rescale b-vals if requested
371+
# Rescale bvals if requested
365372
if b_scale:
366373
bvals[~b0s] *= np.linalg.norm(bvecs[~b0s], axis=1) ** 2
367374

368375
# Ensure b0s have (0, 0, 0) vectors
369376
bvecs[b0s, :3] = np.zeros(3)
370377

371378
# Round bvals
372-
bvals = round_bvals(bvals)
379+
bvals = round_bvals(bvals, bmag=b_mag)
373380

374-
# Rescale b-vecs, skipping b0's, on the appropriate axis to unit-norm length.
381+
# Ensure rounding bvals doesn't change the number of b0s
382+
rounded_b0s = bvals == 0
383+
if not np.all(b0s == rounded_b0s):
384+
msg = f"Inconsistent b0s before ({b0s.sum()}) and after rounding ({rounded_b0s.sum()})."
385+
if raise_error:
386+
raise ValueError(msg)
387+
config.loggers.cli.warning(msg)
388+
389+
# Rescale bvecs, skipping b0's, on the appropriate axis to unit-norm length.
375390
bvecs[~b0s] /= np.linalg.norm(bvecs[~b0s], axis=1)[..., np.newaxis]
376391
return bvecs, bvals.astype("uint16")
377392

0 commit comments

Comments
 (0)