@@ -17,6 +17,7 @@ class DiffusionGradientTable:
17
17
__slots__ = [
18
18
"_affine" ,
19
19
"_b0_thres" ,
20
+ "_b_mag" ,
20
21
"_b_scale" ,
21
22
"_bvals" ,
22
23
"_bvec_norm_epsilon" ,
@@ -30,6 +31,7 @@ class DiffusionGradientTable:
30
31
def __init__ (
31
32
self ,
32
33
b0_threshold = B0_THRESHOLD ,
34
+ b_mag = None ,
33
35
b_scale = True ,
34
36
bvals = None ,
35
37
bvec_norm_epsilon = BVEC_NORM_EPSILON ,
@@ -46,12 +48,14 @@ def __init__(
46
48
----------
47
49
b0_threshold : :obj:`float`
48
50
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.
49
53
b_scale : :obj:`bool`
50
54
Automatically scale the *b*-values with the norm of the corresponding
51
55
*b*-vectors before the latter are normalized.
52
56
bvals : str or os.pathlike or numpy.ndarray
53
57
File path of the b-values.
54
- b_vec_norm_epsilon : :obj:`float`
58
+ bvec_norm_epsilon : :obj:`float`
55
59
The minimum difference in the norm of two *b*-vectors to consider them different.
56
60
bvecs : str or os.pathlike or numpy.ndarray
57
61
File path of the b-vectors.
@@ -93,6 +97,7 @@ def __init__(
93
97
"""
94
98
self ._affine = None
95
99
self ._b0_thres = b0_threshold
100
+ self ._b_mag = b_mag
96
101
self ._b_scale = b_scale
97
102
self ._bvals = None
98
103
self ._bvec_norm_epsilon = bvec_norm_epsilon
@@ -195,6 +200,7 @@ def normalize(self):
195
200
self .bvals ,
196
201
b0_threshold = self ._b0_thres ,
197
202
bvec_norm_epsilon = self ._bvec_norm_epsilon ,
203
+ b_mag = self ._b_mag ,
198
204
b_scale = self ._b_scale ,
199
205
raise_error = self ._raise_inconsistent ,
200
206
)
@@ -290,6 +296,7 @@ def normalize_gradients(
290
296
bvals ,
291
297
b0_threshold = B0_THRESHOLD ,
292
298
bvec_norm_epsilon = BVEC_NORM_EPSILON ,
299
+ b_mag = None ,
293
300
b_scale = True ,
294
301
raise_error = False ,
295
302
):
@@ -361,17 +368,25 @@ def normalize_gradients(
361
368
raise ValueError (msg )
362
369
config .loggers .cli .warning (msg )
363
370
364
- # Rescale b-vals if requested
371
+ # Rescale bvals if requested
365
372
if b_scale :
366
373
bvals [~ b0s ] *= np .linalg .norm (bvecs [~ b0s ], axis = 1 ) ** 2
367
374
368
375
# Ensure b0s have (0, 0, 0) vectors
369
376
bvecs [b0s , :3 ] = np .zeros (3 )
370
377
371
378
# Round bvals
372
- bvals = round_bvals (bvals )
379
+ bvals = round_bvals (bvals , bmag = b_mag )
373
380
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.
375
390
bvecs [~ b0s ] /= np .linalg .norm (bvecs [~ b0s ], axis = 1 )[..., np .newaxis ]
376
391
return bvecs , bvals .astype ("uint16" )
377
392
0 commit comments