Skip to content

Commit f5ca8c1

Browse files
author
dPys
committed
Add signal prediction vector handling functions
1 parent 47fe0e9 commit f5ca8c1

File tree

1 file changed

+46
-20
lines changed

1 file changed

+46
-20
lines changed

dmriprep/utils/vectors.py

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -377,30 +377,56 @@ def bvecs2ras(affine, bvecs, norm=True, bvec_norm_epsilon=0.2):
377377
return rotated_bvecs
378378

379379

380-
def reorient_bvecs_from_ras_b(ras_b, affines):
380+
def _nonoverlapping_qspace_samples(
381+
prediction_bval, prediction_bvec, all_bvals, all_bvecs, cutoff
382+
):
383+
"""Ensure that none of the training samples are too close to the sample to predict.
384+
Parameters
385+
"""
386+
min_bval = min(min(all_bvals), prediction_bval)
387+
all_qvals = np.sqrt(all_bvals - min_bval)
388+
prediction_qval = np.sqrt(prediction_bval - min_bval)
389+
390+
# Convert q values to percent of maximum qval
391+
max_qval = max(max(all_qvals), prediction_qval)
392+
all_qvals_scaled = all_qvals / max_qval * 100
393+
scaled_qvecs = all_bvecs * all_qvals_scaled[:, np.newaxis]
394+
scaled_prediction_qvec = prediction_bvec * (prediction_qval / max_qval * 100)
395+
396+
# Calculate the distance between the sampled qvecs and the prediction qvec
397+
ok_samples = (
398+
np.linalg.norm(scaled_qvecs - scaled_prediction_qvec, axis=1) > cutoff
399+
) * (np.linalg.norm(scaled_qvecs + scaled_prediction_qvec, axis=1) > cutoff)
400+
401+
return ok_samples
402+
403+
404+
def _rasb_to_bvec_list(in_rasb):
381405
"""
382-
Reorient the vectors from a rasb .tsv file.
383-
When correcting for motion, rotation of the diffusion-weighted volumes
384-
might cause systematic bias in rotationally invariant measures, such as FA
385-
and MD, and also cause characteristic biases in tractography, unless the
386-
gradient directions are appropriately reoriented to compensate for this
387-
effect [Leemans2009]_.
406+
Create a list of b-vectors from a rasb gradient table.
388407
389408
Parameters
390409
----------
391-
rasb_file : str or os.pathlike
392-
File path to a RAS-B gradient table. If rasb_file is provided,
393-
then bvecs and bvals will be dismissed.
394-
395-
affines : list or ndarray of shape (n, 4, 4) or (n, 3, 3)
396-
Each entry in this list or array contain either an affine
397-
transformation (4,4) or a rotation matrix (3, 3).
398-
In both cases, the transformations encode the rotation that was applied
399-
to the image corresponding to one of the non-zero gradient directions.
410+
in_rasb : str or os.pathlike
411+
File path to a RAS-B gradient table.
400412
"""
401-
from dipy.core.gradients import gradient_table_from_bvals_bvecs, reorient_bvecs
413+
import numpy as np
402414

403-
ras_b_mat = np.genfromtxt(ras_b, delimiter='\t')
404-
gt = gradient_table_from_bvals_bvecs(ras_b_mat[:,3], ras_b_mat[:,0:3], b0_threshold=50)
415+
ras_b_mat = np.genfromtxt(in_rasb, delimiter="\t")
416+
bvec = [vec for vec in ras_b_mat[:, 0:3] if not np.isclose(all(vec), 0)]
417+
return list(bvec)
418+
419+
420+
def _rasb_to_bval_floats(in_rasb):
421+
"""
422+
Create a list of b-values from a rasb gradient table.
423+
424+
Parameters
425+
----------
426+
in_rasb : str or os.pathlike
427+
File path to a RAS-B gradient table.
428+
"""
429+
import numpy as np
405430

406-
return reorient_bvecs(gt, affines)
431+
ras_b_mat = np.genfromtxt(in_rasb, delimiter="\t")
432+
return [float(bval) for bval in ras_b_mat[:, 3] if bval > 0]

0 commit comments

Comments
 (0)