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

Adding legacy options to scil_convert_sh_basis.py #704

Merged
merged 4 commits into from
May 23, 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
19 changes: 16 additions & 3 deletions scilpy/reconst/multi_processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,8 @@ def convert_sh_basis_parallel(args):


def convert_sh_basis(shm_coeff, sphere, mask=None,
input_basis='descoteaux07', nbr_processes=None):
input_basis='descoteaux07', nbr_processes=None,
is_input_legacy=True, is_output_legacy=True):
"""Converts spherical harmonic coefficients between two bases

Parameters
Expand All @@ -444,6 +445,16 @@ def convert_sh_basis(shm_coeff, sphere, mask=None,
nbr_processes: int, optional
The number of subprocesses to use.
Default: multiprocessing.cpu_count()
is_input_legacy: bool, optional
If true, this means that the input SH used a legacy basis definition
for backward compatibility with previous ``tournier07`` and
``descoteaux07`` implementations.
Default: True
is_output_legacy: bool, optional
If true, this means that the output SH will use a legacy basis
definition for backward compatibility with previous ``tournier07`` and
``descoteaux07`` implementations.
karanphil marked this conversation as resolved.
Show resolved Hide resolved
Default: True

Returns
-------
Expand All @@ -455,8 +466,10 @@ def convert_sh_basis(shm_coeff, sphere, mask=None,
else 'tournier07'

sh_order = order_from_ncoef(shm_coeff.shape[-1])
B_in, _ = sh_to_sf_matrix(sphere, sh_order, input_basis)
_, invB_out = sh_to_sf_matrix(sphere, sh_order, output_basis)
B_in, _ = sh_to_sf_matrix(sphere, sh_order, input_basis,
legacy=is_input_legacy)
_, invB_out = sh_to_sf_matrix(sphere, sh_order, output_basis,
legacy=is_output_legacy)

data_shape = shm_coeff.shape
if mask is None:
Expand Down
16 changes: 13 additions & 3 deletions scripts/scil_convert_sh_basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"""
Convert a SH file between the two commonly used bases
('descoteaux07' or 'tournier07'). The specified basis corresponds to the
input data basis.
input data basis. Note that by default, both legacy 'descoteaux07' and
legacy 'tournier07' bases will be assumed. For more information, see
https://dipy.org/documentation/1.4.0./theory/sh_basis/.
"""

import argparse
Expand All @@ -25,9 +27,15 @@ def _build_arg_parser():

p.add_argument('in_sh',
help='Input SH filename. (nii or nii.gz)')

p.add_argument('out_sh',
help='Output SH filename. (nii or nii.gz)')

p.add_argument('--in_sh_is_not_legacy', action='store_true',
help='If set, this means that the input SH are not encoded '
'with the legacy version of their SH basis.')
p.add_argument('--out_sh_is_not_legacy', action='store_true',
help='If set, this means that the output SH will not be '
'encoded with the legacy version of their SH basis.')

add_sh_basis_args(p, mandatory=True)
add_processes_arg(p)
Expand All @@ -48,7 +56,9 @@ def main():

new_data = convert_sh_basis(data, sphere,
input_basis=args.sh_basis,
nbr_processes=args.nbr_processes)
nbr_processes=args.nbr_processes,
is_input_legacy=not args.in_sh_is_not_legacy,
is_output_legacy=not args.out_sh_is_not_legacy)

nib.save(nib.Nifti1Image(new_data, img.affine, header=img.header),
args.out_sh)
Expand Down