-
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 branch 'master' of github.com:scilus/scilpy into fix_bbox
- Loading branch information
Showing
10 changed files
with
588 additions
and
97 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
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
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,74 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import numpy as np | ||
|
||
supported_tensor_formats = ['fsl', 'nifti', 'mrtrix', 'dipy'] | ||
tensor_format_description = \ | ||
""" | ||
Dipy's order is [Dxx, Dxy, Dyy, Dxz, Dyz, Dzz] | ||
Shape: [i, j , k, 6]. | ||
Ref: https://github.com/dipy/dipy/blob/master/dipy/reconst/dti.py#L1639 | ||
MRTRIX's order is : [Dxx, Dyy, Dzz, Dxy, Dxz, Dyz] | ||
Shape: [i, j , k, 6]. | ||
Ref: https://mrtrix.readthedocs.io/en/dev/reference/commands/dwi2tensor.html | ||
ANTS's order ('nifti format') is : [Dxx, Dxy, Dyy, Dxz, Dyz, Dzz]. | ||
Shape: [i, j , k, 1, 6] (Careful, file is 5D). | ||
Ref: https://github.com/ANTsX/ANTs/wiki/Importing-diffusion-tensor-data-from-other-software | ||
FSL's order is [Dxx, Dxy, Dxz, Dyy, Dyz, Dzz] | ||
Shape: [i, j , k, 6]. | ||
Ref: https://fsl.fmrib.ox.ac.uk/fsl/fslwiki/FDT/UserGuide | ||
(Also used for the Fibernavigator) | ||
""" | ||
|
||
|
||
def convert_tensor_to_dipy_format(tensor, initial_format): | ||
""" | ||
See description of formats at the top of this file. | ||
""" | ||
assert initial_format in supported_tensor_formats, \ | ||
"Tensor format not supported" | ||
|
||
if initial_format == 'nifti' or initial_format == 'dipy': | ||
correct_order = [0, 1, 2, 3, 4, 5] | ||
tensor = np.squeeze(tensor) | ||
elif initial_format == 'mrtrix': | ||
correct_order = [0, 3, 1, 4, 5, 2] | ||
else: # initial_format == 'fsl': | ||
correct_order = [0, 1, 3, 2, 4, 5] | ||
|
||
return tensor[..., correct_order] | ||
|
||
|
||
def convert_tensor_from_dipy_format(tensor, final_format): | ||
""" | ||
See description of formats at the top of this file. | ||
""" | ||
assert final_format in supported_tensor_formats, \ | ||
"Tensor format not supported" | ||
|
||
if final_format == 'nifti' or final_format == 'dipy': | ||
correct_order = [0, 1, 2, 3, 4, 5] | ||
elif final_format == 'mrtrix': | ||
correct_order = [0, 2, 5, 1, 3, 4] | ||
else: # final_format == 'fsl'. | ||
correct_order = [0, 1, 3, 2, 4, 5] | ||
|
||
tensor_reordered = tensor[..., correct_order] | ||
|
||
if final_format == 'nifti': | ||
# We need to add the fifth dimension | ||
tensor_reordered = tensor_reordered[:, :, :, None, :] | ||
|
||
return tensor_reordered | ||
|
||
|
||
def convert_tensor_format(tensor, initial_format, final_format): | ||
""" | ||
See description of formats at the top of this file. | ||
""" | ||
tensor = convert_tensor_to_dipy_format(tensor, initial_format) | ||
return convert_tensor_from_dipy_format(tensor, final_format) | ||
|
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
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 -*- | ||
""" | ||
Conversion of tensors (the 6 values from the triangular matrix) between various | ||
software standards. We cannot discover the input format type, user must know | ||
how the tensors were created. | ||
""" | ||
|
||
import argparse | ||
|
||
import nibabel as nib | ||
import numpy as np | ||
|
||
from scilpy.io.utils import (add_overwrite_arg, add_reference_arg, | ||
assert_inputs_exist, assert_outputs_exist) | ||
from scilpy.reconst.dti import (supported_tensor_formats, | ||
tensor_format_description, | ||
convert_tensor_format) | ||
|
||
|
||
def _build_arg_parser(): | ||
p = argparse.ArgumentParser(description=__doc__ + tensor_format_description, | ||
formatter_class=argparse.RawTextHelpFormatter) | ||
|
||
p.add_argument('in_file', | ||
help='Input tensors filename.') | ||
p.add_argument('out_file', | ||
help='Output tensors filename.') | ||
p.add_argument('in_format', metavar='in_format', | ||
choices=supported_tensor_formats, | ||
help='Input format. Choices: {}' | ||
.format(supported_tensor_formats)) | ||
p.add_argument('out_format', metavar='out_format', | ||
choices=supported_tensor_formats, | ||
help='Output format. Choices: {}' | ||
.format(supported_tensor_formats)) | ||
add_overwrite_arg(p) | ||
|
||
return p | ||
|
||
|
||
def main(): | ||
parser = _build_arg_parser() | ||
args = parser.parse_args() | ||
|
||
assert_inputs_exist(parser, args.in_file) | ||
assert_outputs_exist(parser, args, args.out_file) | ||
|
||
in_tensors_img = nib.load(args.in_file) | ||
in_tensors = in_tensors_img.get_fdata(dtype=np.float32) | ||
|
||
out_tensors = convert_tensor_format(in_tensors, args.in_format, | ||
args.out_format) | ||
out_tensors_img = nib.Nifti1Image( | ||
out_tensors.astype(np.float32), in_tensors_img.affine) | ||
nib.save(out_tensors_img, args.out_file) | ||
|
||
|
||
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
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,68 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
Validate and correct gradients from eddy outputs | ||
With full AP-PA eddy outputs a full bvec bval (2x nb of dirs and bval) | ||
that doesnt fit with the output dwi (1x nb of dir) | ||
""" | ||
|
||
import argparse | ||
|
||
import numpy as np | ||
|
||
from scilpy.io.utils import (add_overwrite_arg, assert_inputs_exist, | ||
assert_outputs_exist) | ||
|
||
|
||
def _build_arg_parser(): | ||
p = argparse.ArgumentParser( | ||
description=__doc__, | ||
formatter_class=argparse.RawTextHelpFormatter) | ||
p.add_argument('in_bvec', | ||
help='In bvec file.') | ||
p.add_argument('in_bval', | ||
help='In bval file.') | ||
p.add_argument('nb_dirs', type=int, | ||
help='Number of directions per DWI.') | ||
p.add_argument('out_bvec', | ||
help='Out bvec file.') | ||
p.add_argument('out_bval', | ||
help='Out bval file.') | ||
add_overwrite_arg(p) | ||
return p | ||
|
||
|
||
def main(): | ||
parser = _build_arg_parser() | ||
args = parser.parse_args() | ||
|
||
assert_inputs_exist(parser, [args.in_bvec, args.in_bval]) | ||
assert_outputs_exist(parser, args, [args.out_bval, args.out_bvec]) | ||
|
||
""" | ||
IN BVEC | ||
""" | ||
in_bvec = np.genfromtxt(args.in_bvec) | ||
split_dirs = in_bvec.shape[1] / args.nb_dirs | ||
if int(split_dirs) != split_dirs: | ||
parser.error('Number of directions in bvec ({}) can\'t be splited in ' | ||
'even parts using nb_dirs ({}).'.format(in_bvec.shape[1], | ||
args.nb_dirs)) | ||
in_bvec_split = np.hsplit(in_bvec, int(split_dirs)) | ||
if len(in_bvec_split) == 2: | ||
out_bvec = np.mean(np.array([in_bvec_split[0], | ||
in_bvec_split[1]]), axis=0) | ||
else: | ||
out_bvec = in_bvec_split[0] | ||
np.savetxt(args.out_bvec, out_bvec, '%.8f') | ||
|
||
""" | ||
IN BVAL | ||
""" | ||
in_bval = np.genfromtxt(args.in_bval) | ||
np.savetxt(args.out_bval, in_bval[0:out_bvec.shape[1]], '%.0f') | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Oops, something went wrong.