Skip to content

Commit

Permalink
Add test flip and swap axis
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaRenauld committed Jan 23, 2024
1 parent f7b2683 commit 35a8eb9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
21 changes: 12 additions & 9 deletions scilpy/gradients/bvec_bval_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def is_normalized_bvecs(bvecs):
-------
True/False
"""

bvecs_norm = np.linalg.norm(bvecs, axis=1)
return np.all(np.logical_or(np.abs(bvecs_norm - 1) < 1e-3,
bvecs_norm == 0))
Expand All @@ -48,7 +47,7 @@ def normalize_bvecs(bvecs):
bvecs : (N, 3)
normalized b-vectors
"""

bvecs = bvecs.copy() # Avoid in-place modification.
bvecs_norm = np.linalg.norm(bvecs, axis=1)
idx = bvecs_norm != 0
bvecs[idx] /= bvecs_norm[idx, None]
Expand Down Expand Up @@ -217,19 +216,22 @@ def flip_gradient_sampling(bvecs, axes, sampling_type):
Parameters
----------
bvecs: np.ndarray
Loaded bvecs. In the case 'mrtrix' the bvecs actually also contain the
bvals.
bvecs loaded directly, not re-formatted. Careful! Must respect the
format (not verified here).
axes: list of int
List of axes to flip (e.g. [0, 1])
List of axes to flip (e.g. [0, 1]). See str_to_axis_index.
sampling_type: str
Either 'mrtrix' or 'fsl'.
Either 'mrtrix': bvecs are of shape (N, 4) or
'fsl': bvecs are of shape (3, N)
Returns
-------
bvecs: np.array
The final bvecs.
"""
assert sampling_type in ['mrtrix', 'fsl']

bvecs = bvecs.copy() # Avoid in-place modification.
if sampling_type == 'mrtrix':
for axis in axes:
bvecs[:, axis] *= -1
Expand All @@ -246,12 +248,13 @@ def swap_gradient_axis(bvecs, final_order, sampling_type):
Parameters
----------
bvecs: np.array
Loaded bvecs. In the case 'mrtrix' the bvecs actually also contain the
bvals.
bvecs loaded directly, not re-formatted. Careful! Must respect the
format (not verified here).
final_order: new order
Final order (ex, 2 1 0)
sampling_type: str
Either 'mrtrix' or 'fsl'.
Either 'mrtrix': bvecs are of shape (N, 4) or
'fsl': bvecs are of shape (3, N)
Returns
-------
Expand Down
25 changes: 18 additions & 7 deletions scilpy/gradients/tests/test_bvec_bval_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

from scilpy.gradients.bvec_bval_tools import (
check_b0_threshold, is_normalized_bvecs, normalize_bvecs,
round_bvals_to_shell, identify_shells)
round_bvals_to_shell, identify_shells, str_to_axis_index, flip_gradient_sampling,
swap_gradient_axis)

bvecs = np.asarray([[1.0, 1.0, 1.0],
[1.0, 0.0, 1.0],
[0.0, 1.0, 0.0]])
[0.0, 1.0, 0.0],
[8.0, 1.0, 1.0]])


def test_is_normalized_bvecs():
Expand Down Expand Up @@ -63,17 +65,26 @@ def _subtest_identify_shells(bvals, threshold,

def test_str_to_axis_index():
# Very simple, nothing to do
pass
assert str_to_axis_index('x') == 0


def test_flip_gradient_sampling():
# toDo
pass
fsl_bvecs = bvecs.T
b = flip_gradient_sampling(fsl_bvecs, axes=[0], sampling_type='fsl')
assert np.array_equal(b, np.asarray([[-1.0, 1.0, 1.0],
[-1.0, 0.0, 1.0],
[-0.0, 1.0, 0.0],
[-8.0, 1.0, 1.0]]).T)


def test_swap_gradient_axis():
# toDo
pass
fsl_bvecs = bvecs.T
final_order = [1, 0, 2]
b = swap_gradient_axis(fsl_bvecs, final_order, sampling_type='fsl')
assert np.array_equal(b, np.asarray([[1.0, 1.0, 1.0],
[0.0, 1.0, 1.0],
[1.0, 0.0, 0.0],
[1.0, 8.0, 1.0]]).T)


def test_round_bvals_to_shell():
Expand Down
12 changes: 11 additions & 1 deletion scripts/scil_gradients_modify_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,24 @@ def main():
if len(np.unique(swapped_order)) != 3:
parser.error("final_order should contain the three axis.")

# Could use io.gradients method, but we don't have a bvals file here, only
# treating with the bvecs. Loading directly.
bvecs = np.loadtxt(args.in_gradient_sampling_file)
if ext_in == '.bvec':
# Supposing FSL format
# Supposing FSL format: Per columns
if bvecs.shape[0] != 3:
parser.error("b-vectors format for a .b file should be FSL, "
"and contain 3 lines (x, y, z), but got {}"
.format(bvecs.shape[0]))
bvecs = flip_gradient_sampling(bvecs, axes_to_flip, 'fsl')
bvecs = swap_gradient_axis(bvecs, swapped_order, 'fsl')
np.savetxt(args.out_gradient_sampling_file, bvecs, "%.8f")
else: # ext == '.b':
# Supposing mrtrix format
if bvecs.shape[1] != 4:
parser.error("b-vectors format for a .b file should be mrtrix, "
"and contain 4 columns (x, y, z, bval), but got {}"
.format(bvecs.shape[1]))
bvecs = flip_gradient_sampling(bvecs, axes_to_flip, 'mrtrix')
bvecs = swap_gradient_axis(bvecs, swapped_order, 'mrtrix')
np.savetxt(args.out_gradient_sampling_file, bvecs,
Expand Down

0 comments on commit 35a8eb9

Please sign in to comment.