Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Add emc helper functions - vectors #76

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
55 changes: 55 additions & 0 deletions dmriprep/utils/vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,58 @@ def bvecs2ras(affine, bvecs, norm=True, bvec_norm_epsilon=0.2):
rotated_bvecs[~b0s] /= norms_bvecs[~b0s, np.newaxis]
rotated_bvecs[b0s] = np.zeros(3)
return rotated_bvecs


def _nonoverlapping_qspace_samples(
dPys marked this conversation as resolved.
Show resolved Hide resolved
prediction_bval, prediction_bvec, all_bvals, all_bvecs, cutoff
):
"""Ensure that none of the training samples are too close to the sample to predict.
Parameters
dPys marked this conversation as resolved.
Show resolved Hide resolved
"""
dPys marked this conversation as resolved.
Show resolved Hide resolved
min_bval = min(min(all_bvals), prediction_bval)
all_qvals = np.sqrt(all_bvals - min_bval)
prediction_qval = np.sqrt(prediction_bval - min_bval)

# Convert q values to percent of maximum qval
max_qval = max(max(all_qvals), prediction_qval)
all_qvals_scaled = all_qvals / max_qval * 100
scaled_qvecs = all_bvecs * all_qvals_scaled[:, np.newaxis]
scaled_prediction_qvec = prediction_bvec * (prediction_qval / max_qval * 100)

# Calculate the distance between the sampled qvecs and the prediction qvec
ok_samples = (
np.linalg.norm(scaled_qvecs - scaled_prediction_qvec, axis=1) > cutoff
) * (np.linalg.norm(scaled_qvecs + scaled_prediction_qvec, axis=1) > cutoff)

return ok_samples


def _rasb_to_bvec_list(in_rasb):
"""
Create a list of b-vectors from a rasb gradient table.

Parameters
----------
in_rasb : str or os.pathlike
File path to a RAS-B gradient table.
"""
dPys marked this conversation as resolved.
Show resolved Hide resolved
import numpy as np

ras_b_mat = np.genfromtxt(in_rasb, delimiter="\t")
bvec = [vec for vec in ras_b_mat[:, 0:3] if not np.isclose(all(vec), 0)]
return list(bvec)


def _rasb_to_bval_floats(in_rasb):
"""
Create a list of b-values from a rasb gradient table.

Parameters
----------
in_rasb : str or os.pathlike
File path to a RAS-B gradient table.
"""
import numpy as np

ras_b_mat = np.genfromtxt(in_rasb, delimiter="\t")
return [float(bval) for bval in ras_b_mat[:, 3] if bval > 0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you point me at the location where these are needed? I believe our Gradients class has already this covered.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(and not covered in Gradients class)