@@ -52,6 +52,25 @@ def __init__(
52
52
then bvecs and bvals will be dismissed.
53
53
b_scale : bool
54
54
Whether b-values should be normalized.
55
+
56
+ Example
57
+ -------
58
+ >>> os.chdir(tmpdir)
59
+ >>> old_rasb = str(data_dir / 'dwi.tsv')
60
+ >>> old_rasb_mat = np.loadtxt(str(data_dir / 'dwi.tsv'), skiprows=1)
61
+ >>> check = DiffusionGradientTable(
62
+ ... dwi_file=str(data_dir / 'dwi.nii.gz'),
63
+ ... rasb_file=str(data_dir / 'dwi.tsv'))
64
+ >>> affines = np.zeros((old_rasb_mat.shape[0], 4, 4))
65
+ >>> aff_file_list = []
66
+ >>> for ii, aff in enumerate(affines):
67
+ ... aff_file = f'aff_{ii}.npy'
68
+ ... np.save(aff_file, aff)
69
+ ... aff_file_list.append(aff_file)
70
+ >>> check._transforms = aff_file_list
71
+ >>> out_rasb_mat = check.reorient_rasb()
72
+ >>> assert np.allclose(old_rasb_mat, out_rasb_mat)
73
+ True
55
74
"""
56
75
self ._transforms = transforms
57
76
self ._b_scale = b_scale
@@ -164,14 +183,15 @@ def generate_rasb(self):
164
183
165
184
def reorient_rasb (self ):
166
185
"""Reorient the vectors based o a list of affine transforms."""
167
- from dipy .core .gradients import gradient_table_from_bvals_bvecs , reorient_bvecs
186
+ from dipy .core .gradients import (gradient_table_from_bvals_bvecs ,
187
+ reorient_bvecs )
168
188
169
189
affines = self ._transforms .copy ()
170
190
bvals = self ._bvals
171
191
bvecs = self ._bvecs
172
192
173
- # Verify that number of non-B0 volumes corresponds to the number of affines.
174
- # If not, raise an error.
193
+ # Verify that number of non-B0 volumes corresponds to the number of
194
+ # affines. If not, try to fix it, or raise an error:
175
195
if len (self ._bvals [self ._bvals >= self ._b0_thres ]) != len (affines ):
176
196
b0_indices = np .where (self ._bvals <= self ._b0_thres )[0 ].tolist ()
177
197
if len (self ._bvals [self ._bvals >= self ._b0_thres ]) < len (affines ):
@@ -188,7 +208,8 @@ def reorient_rasb(self):
188
208
)
189
209
190
210
# Build gradient table object
191
- gt = gradient_table_from_bvals_bvecs (bvals , bvecs , b0_threshold = self ._b0_thres )
211
+ gt = gradient_table_from_bvals_bvecs (bvals , bvecs ,
212
+ b0_threshold = self ._b0_thres )
192
213
193
214
# Reorient table
194
215
new_gt = reorient_bvecs (gt , [np .load (aff ) for aff in affines ])
@@ -238,7 +259,7 @@ def to_filename(self, filename, filetype="rasb"):
238
259
np .savetxt ("%s.bval" % filename , self .bvals , fmt = "%.6f" )
239
260
else :
240
261
raise ValueError ('Unknown filetype "%s"' % filetype )
241
-
262
+
242
263
243
264
def normalize_gradients (bvecs , bvals , b0_threshold = B0_THRESHOLD ,
244
265
bvec_norm_epsilon = BVEC_NORM_EPSILON , b_scale = True ):
0 commit comments