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

(WIP) Adding script to apply transform to bvecs #696

Merged
merged 5 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions scripts/scil_apply_transform_to_bvecs.py
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()
30 changes: 30 additions & 0 deletions scripts/tests/test_apply_transform_to_bvecs.py
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