-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #696 from karanphil/apply_transform_to_bvecs
(WIP) Adding script to apply transform to bvecs
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
Transform bvecs using an affine/rigid transformation. | ||
""" | ||
|
||
import argparse | ||
|
||
import numpy as np | ||
|
||
from dipy.io.gradients import read_bvals_bvecs | ||
|
||
from scilpy.io.utils import (add_overwrite_arg, assert_inputs_exist, | ||
assert_outputs_exist, load_matrix_in_any_format) | ||
from scilpy.utils.filenames import split_name_with_nii | ||
from scilpy.utils.image import transform_anatomy | ||
|
||
|
||
def _build_arg_parser(): | ||
p = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, | ||
description=__doc__) | ||
|
||
p.add_argument('in_bvecs', | ||
help='Path of the bvec file, in FSL format') | ||
p.add_argument('in_transfo', | ||
help='Path of the file containing the 4x4 \n' | ||
'transformation, matrix (.txt, .npy or .mat).') | ||
p.add_argument('out_bvecs', | ||
help='Output filename of the transformed bvecs.') | ||
|
||
p.add_argument('--inverse', action='store_true', | ||
help='Apply the inverse transformation.') | ||
|
||
add_overwrite_arg(p) | ||
|
||
return p | ||
|
||
|
||
def main(): | ||
parser = _build_arg_parser() | ||
args = parser.parse_args() | ||
|
||
assert_inputs_exist(parser, [args.in_bvecs, args.in_transfo]) | ||
assert_outputs_exist(parser, args, args.out_bvecs) | ||
|
||
transfo = load_matrix_in_any_format(args.in_transfo)[:3, :3] | ||
|
||
if args.inverse: | ||
transfo = np.linalg.inv(transfo) | ||
|
||
_, bvecs = read_bvals_bvecs(None, args.in_bvecs) | ||
|
||
bvecs = bvecs @ transfo | ||
|
||
np.savetxt(str(args.out_bvecs), bvecs.T) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
import os | ||
import tempfile | ||
|
||
from scilpy.io.fetcher import get_testing_files_dict, fetch_data, get_home | ||
|
||
|
||
# If they already exist, this only takes 5 seconds (check md5sum) | ||
fetch_data(get_testing_files_dict(), keys=['bst.zip']) | ||
fetch_data(get_testing_files_dict(), keys=['processing.zip']) | ||
tmp_dir = tempfile.TemporaryDirectory() | ||
|
||
|
||
def test_help_option(script_runner): | ||
ret = script_runner.run('scil_apply_transform_to_bvecs.py', '--help') | ||
assert ret.success | ||
|
||
|
||
def test_execution_bst(script_runner): | ||
os.chdir(os.path.expanduser(tmp_dir.name)) | ||
in_bvecs = os.path.join(get_home(), 'processing', | ||
'dwi.bvec') | ||
in_aff = os.path.join(get_home(), 'bst', | ||
'output0GenericAffine.mat') | ||
ret = script_runner.run('scil_apply_transform_to_bvecs.py', | ||
in_bvecs, in_aff, | ||
'bvecs_transformed.bvec', '--inverse') | ||
assert ret.success |