Skip to content
Snippets Groups Projects
Commit 65a51503 authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

ENH: parse_data.Image allows additional Image.__init__ arguments to be

passed. Used by fsl_convert_x5
parent 07fc4c17
No related branches found
No related tags found
No related merge requests found
...@@ -9,17 +9,18 @@ transformation file formats. ...@@ -9,17 +9,18 @@ transformation file formats.
""" """
import os.path as op import os.path as op
import sys import functools as ft
import shutil import sys
import logging import shutil
import argparse import logging
from collections import OrderedDict import argparse
import fsl.data.image as fslimage import fsl.data.image as fslimage
import fsl.transform.flirt as flirt import fsl.utils.parse_data as parse_data
import fsl.transform.fnirt as fnirt import fsl.transform.flirt as flirt
import fsl.transform.x5 as x5 import fsl.transform.fnirt as fnirt
import fsl.transform.x5 as x5
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -49,11 +50,14 @@ def parseArgs(args): ...@@ -49,11 +50,14 @@ def parseArgs(args):
subparsers = parser.add_subparsers(dest='ctype') subparsers = parser.add_subparsers(dest='ctype')
flirt = subparsers.add_parser('flirt') flirt = subparsers.add_parser('flirt')
fnirt = subparsers.add_parser('fnirt') fnirt = subparsers.add_parser('fnirt')
imgtype = ft.partial(parse_data.Image, loadData=False)
flirt.add_argument('input', help=helps['input']) flirt.add_argument('input', help=helps['input'])
flirt.add_argument('output', help=helps['output']) flirt.add_argument('output', help=helps['output'])
flirt.add_argument('-s', '--source', help=helps['source']) flirt.add_argument('-s', '--source', help=helps['source'],
flirt.add_argument('-r', '--reference', help=helps['reference']) type=imgtype)
flirt.add_argument('-r', '--reference', help=helps['reference'],
type=imgtype)
flirt.add_argument('-if', '--input_format', help=helps['input_format'], flirt.add_argument('-if', '--input_format', help=helps['input_format'],
choices=('x5', 'mat')) choices=('x5', 'mat'))
flirt.add_argument('-of', '--output_format', help=helps['output_format'], flirt.add_argument('-of', '--output_format', help=helps['output_format'],
...@@ -61,8 +65,10 @@ def parseArgs(args): ...@@ -61,8 +65,10 @@ def parseArgs(args):
fnirt .add_argument('input', help=helps['input']) fnirt .add_argument('input', help=helps['input'])
fnirt .add_argument('output', help=helps['output']) fnirt .add_argument('output', help=helps['output'])
fnirt .add_argument('-s', '--source', help=helps['source']) fnirt .add_argument('-s', '--source', help=helps['source'],
fnirt .add_argument('-r', '--reference', help=helps['reference']) type=imgtype)
fnirt .add_argument('-r', '--reference', help=helps['reference'],
type=imgtype)
fnirt .add_argument('-if', '--input_format', help=helps['input_format'], fnirt .add_argument('-if', '--input_format', help=helps['input_format'],
choices=('x5', 'nii')) choices=('x5', 'nii'))
fnirt .add_argument('-of', '--output_format', help=helps['output_format'], fnirt .add_argument('-of', '--output_format', help=helps['output_format'],
...@@ -109,8 +115,8 @@ def flirtToX5(args): ...@@ -109,8 +115,8 @@ def flirtToX5(args):
"""Convert a linear FLIRT transformation matrix to an X5 transformation """Convert a linear FLIRT transformation matrix to an X5 transformation
file. file.
""" """
src = fslimage.Image(args.source, loadData=False) src = args.source
ref = fslimage.Image(args.reference, loadData=False) ref = args.reference
xform = flirt.readFlirt(args.input) xform = flirt.readFlirt(args.input)
xform = flirt.fromFlirt(xform, src, ref, 'world', 'world') xform = flirt.fromFlirt(xform, src, ref, 'world', 'world')
x5.writeLinearX5(args.output, xform, src, ref) x5.writeLinearX5(args.output, xform, src, ref)
...@@ -127,8 +133,8 @@ def fnirtToX5(args): ...@@ -127,8 +133,8 @@ def fnirtToX5(args):
"""Convert a non-linear FNIRT transformation into an X5 transformation """Convert a non-linear FNIRT transformation into an X5 transformation
file. file.
""" """
src = fslimage.Image(args.source, loadData=False) src = args.source
ref = fslimage.Image(args.reference, loadData=False) ref = args.reference
field = fnirt.readFnirt(args.input, src=src, ref=ref) field = fnirt.readFnirt(args.input, src=src, ref=ref)
field = fnirt.fromFnirt(field, 'world', 'world') field = fnirt.fromFnirt(field, 'world', 'world')
x5.writeNonLinearX5(args.output, field) x5.writeNonLinearX5(args.output, field)
......
...@@ -10,6 +10,7 @@ transformations from/to BIDS X5 files. The following functions are available: ...@@ -10,6 +10,7 @@ transformations from/to BIDS X5 files. The following functions are available:
.. autosummary:: .. autosummary::
:nosignatures: :nosignatures:
inferType
readLinearX5 readLinearX5
writeLinearX5 writeLinearX5
readNonLinearX5 readNonLinearX5
...@@ -339,6 +340,23 @@ class X5Error(Exception): ...@@ -339,6 +340,23 @@ class X5Error(Exception):
pass pass
def inferType(fname):
"""Return the type of the given X5 file - either ``'linear'``or
``'nonlinear'``.
:arg fname: Name of a X5 file
:returns: ``'linear'``or ``'nonlinear'``
"""
with h5py.File(fname, 'r') as f:
ftype = f.attrs.get('Type')
if ftype not in ('linear', 'nonlinear'):
raise X5Error('Unknown type: {}'.format(ftype))
return ftype
def readLinearX5(fname): def readLinearX5(fname):
"""Read a linear X5 transformation file from ``fname``. """Read a linear X5 transformation file from ``fname``.
...@@ -565,7 +583,7 @@ def _readDeformation(group): ...@@ -565,7 +583,7 @@ def _readDeformation(group):
def _writeDeformation(group, field): def _writeDeformation(group, field):
"""Write a deformation fieldto the given group. """Write a deformation field to the given group.
:arg group: A ``h5py.Group`` object :arg group: A ``h5py.Group`` object
:arg field: A :class:`.DeformationField` object :arg field: A :class:`.DeformationField` object
......
...@@ -8,8 +8,9 @@ ...@@ -8,8 +8,9 @@
Argparse is the built-in python library for resolving command line arguments. Argparse is the built-in python library for resolving command line arguments.
The functions in this module can be passed on to the ``type`` argument in the ``ArgumentParser.add_command`` method The functions in this module can be passed on to the ``type`` argument in the
to interpret command line arguments as neuroimageing objects (.e.g, NIFTI image files) ``ArgumentParser.add_command`` method to interpret command line arguments as
neuroimaging objects (.e.g, NIFTI image files)
.. autosummary:: .. autosummary::
...@@ -27,18 +28,21 @@ from fsl.utils import path ...@@ -27,18 +28,21 @@ from fsl.utils import path
import argparse import argparse
def Image(filename): def Image(filename, *args, **kwargs):
""" """
Reads in an image from a NIFTI or Analyze file. Reads in an image from a NIFTI or Analyze file.
:arg filename: filename provided by the user :arg filename: filename provided by the user
:return: fsl.data.image.Image object :return: fsl.data.image.Image object
All other arguments are passed through to the :class:`.Image` upon
creation.
""" """
try: try:
full_filename = image.addExt(filename) full_filename = image.addExt(filename)
except path.PathError as e: except path.PathError as e:
raise argparse.ArgumentTypeError(*e.args) raise argparse.ArgumentTypeError(*e.args)
return image.Image(full_filename) return image.Image(full_filename, *args, **kwargs)
def ImageOut(basename): def ImageOut(basename):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment