Skip to content

Commit 55531b8

Browse files
committed
Added interface to FSL fnirtfileutils
1 parent e815741 commit 55531b8

File tree

4 files changed

+185
-1
lines changed

4 files changed

+185
-1
lines changed

nipype/interfaces/fsl/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
ImageStats, FilterRegressor, Overlay, Slicer,
1717
PlotTimeSeries, PlotMotionParams, ConvertXFM,
1818
SwapDimensions, PowerSpectrum, Reorient2Std,
19-
Complex, InvWarp)
19+
Complex, InvWarp, WarpUtils)
2020

2121
from .epi import (PrepareFieldmap, TOPUP, ApplyTOPUP, Eddy, EPIDeWarp,
2222
SigLoss, EddyCorrect)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT
2+
from nipype.testing import assert_equal
3+
from nipype.interfaces.fsl.utils import WarpUtils
4+
5+
def test_WarpUtils_inputs():
6+
input_map = dict(args=dict(argstr='%s',
7+
),
8+
environ=dict(nohash=True,
9+
usedefault=True,
10+
),
11+
ignore_exception=dict(nohash=True,
12+
usedefault=True,
13+
),
14+
in_file=dict(argstr='--in=%s',
15+
mandatory=True,
16+
),
17+
knot_space=dict(argstr='--knotspace=%d,%d,%d',
18+
),
19+
out_file=dict(argstr='--out=%s',
20+
genfile=True,
21+
hash_files=False,
22+
),
23+
out_format=dict(argstr='--outformat=%s',
24+
),
25+
out_jacobian=dict(argstr='--jac=%s',
26+
),
27+
output_type=dict(),
28+
reference=dict(argstr='--ref=%s',
29+
mandatory=True,
30+
),
31+
terminal_output=dict(mandatory=True,
32+
nohash=True,
33+
),
34+
warp_resolution=dict(argstr='--warpres=%0.4f,%0.4f,%0.4f',
35+
),
36+
with_affine=dict(argstr='--withaff',
37+
),
38+
write_jacobian=dict(mandatory=True,
39+
usedefault=True,
40+
),
41+
)
42+
inputs = WarpUtils.input_spec()
43+
44+
for key, metadata in input_map.items():
45+
for metakey, value in metadata.items():
46+
yield assert_equal, getattr(inputs.traits()[key], metakey), value
47+
48+
def test_WarpUtils_outputs():
49+
output_map = dict(out_file=dict(),
50+
out_jacobian=dict(),
51+
)
52+
outputs = WarpUtils.output_spec()
53+
54+
for key, metadata in output_map.items():
55+
for metakey, value in metadata.items():
56+
yield assert_equal, getattr(outputs.traits()[key], metakey), value
57+

nipype/interfaces/fsl/utils.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,3 +1451,130 @@ def _list_outputs(self):
14511451
outputs['magnitude_out_file'] = self._get_output('magnitude_out_file')
14521452
outputs['phase_out_file'] = self._get_output('phase_out_file')
14531453
return outputs
1454+
1455+
1456+
1457+
class WarpUtilsInputSpec(FSLCommandInputSpec):
1458+
in_file = File(exists=True, argstr='--in=%s', mandatory=True,
1459+
desc=('Name of file containing warp-coefficients/fields. This '
1460+
'would typically be the output from the --cout switch of '
1461+
'fnirt (but can also use fields, like the output from '
1462+
'--fout).'))
1463+
reference = File(exists=True, argstr='--ref=%s', mandatory=True,
1464+
desc=('Name of a file in target space. Note that the '
1465+
'target space is now different from the target '
1466+
'space that was used to create the --warp file. It '
1467+
'would typically be the file that was specified '
1468+
'with the --in argument when running fnirt.'))
1469+
1470+
out_format = traits.Either('field', 'spline', argstr='--outformat=%s',
1471+
desc=('Specifies the output format. If set to field (default) '
1472+
'the output will be a (4D) field-file. If set to spline '
1473+
'the format will be a (4D) file of spline coefficients.'))
1474+
1475+
warp_resolution = traits.Tuple(traits.Float, traits.Float, traits.Float,
1476+
argstr='--warpres=%0.4f,%0.4f,%0.4f',
1477+
desc=('Specifes the resolution/knot-spacing of the splines pertaining to '
1478+
'the coefficients in the --out file. This parameter is only relevant '
1479+
'if --outformat is set to spline. It should be noted that if the '
1480+
'--in file has a higher resolution, the resulting coefficents will '
1481+
'pertain to the closest (in a least-squares sense) file in the space '
1482+
'of fields with the --warpres resolution. It should also be noted '
1483+
'that the resolution will always be an integer multiple of the voxel '
1484+
'size.'))
1485+
1486+
knot_space = traits.Tuple(traits.Int, traits.Int, traits.Int,
1487+
argstr='--knotspace=%d,%d,%d',
1488+
desc=('Alternative (to --warpres) specifikation of the resolution of '
1489+
'the output spline-field.'))
1490+
1491+
out_file = File(genfile=True, hash_files=False, argstr='--out=%s',
1492+
desc=('Name of output file. The format of the output depends on what other '
1493+
'parameters are set. The default format is a (4D) field-file. If the '
1494+
'--outformat is set to spline the format will be a (4D) file of spline '
1495+
'coefficients.'))
1496+
1497+
write_jacobian = traits.Bool(False, mandatory=True, usedefault=True,
1498+
desc='Switch on --jac flag with automatically generated filename')
1499+
1500+
out_jacobian = File(argstr='--jac=%s',
1501+
desc=('Specifies that a (3D) file of Jacobian determinants corresponding '
1502+
'to --in should be produced and written to filename.'))
1503+
1504+
with_affine = traits.Bool(False, argstr='--withaff',
1505+
desc=('Specifies that the affine transform (i.e. that which was '
1506+
'specified for the --aff parameter in fnirt) should be '
1507+
'included as displacements in the --out file. That can be '
1508+
'useful for interfacing with software that cannot decode '
1509+
'FSL/fnirt coefficient-files (where the affine transform is '
1510+
'stored separately from the displacements).'))
1511+
1512+
class WarpUtilsOutputSpec(TraitedSpec):
1513+
out_file = File(exists=True,
1514+
desc=('Name of output file, containing the warp as field or coefficients.'))
1515+
out_jacobian = File(exists=True,
1516+
desc=('Name of output file, containing the map of the determinant of '
1517+
'the Jacobian'))
1518+
1519+
1520+
class WarpUtils(FSLCommand):
1521+
"""Use FSL `fnirtfileutils <http://fsl.fmrib.ox.ac.uk/fsl/fsl-4.1.9/fnirt/warp_utils.html>`_
1522+
to convert field->coefficients, coefficients->field, coefficients->other_coefficients etc
1523+
1524+
1525+
Examples::
1526+
1527+
>>> from nipype.interfaces.fsl import WarpUtils
1528+
>>> warputils = WarpUtils()
1529+
>>> warputils.inputs.in_file = "warpfield.nii"
1530+
>>> warputils.inputs.reference = "T1.nii"
1531+
>>> warputils.inputs.out_format = 'spline'
1532+
>>> warputils.inputs.warp_resolution = (10,10,10)
1533+
>>> warputils.cmdline # doctest: +ELLIPSIS
1534+
'fnirtfileutils --in=warpfield.nii --out=.../warpfield_coeffs.nii.gz --outformat=spline --ref=T1.nii --warpres=10.0000,10.0000,10.0000'
1535+
>>> res = invwarp.run() # doctest: +SKIP
1536+
"""
1537+
1538+
input_spec = WarpUtilsInputSpec
1539+
output_spec = WarpUtilsOutputSpec
1540+
1541+
_cmd = 'fnirtfileutils'
1542+
1543+
def _parse_inputs(self, skip=None):
1544+
if skip is None:
1545+
skip = []
1546+
1547+
if self.inputs.write_jacobian:
1548+
if not isdefined(self.inputs.out_jacobian):
1549+
self.inputs.out_jacobian = self._gen_fname(self.inputs.in_file,
1550+
suffix='_jac')
1551+
skip+=['write_jacobian']
1552+
return super(WarpUtils, self)._parse_inputs(skip=skip)
1553+
1554+
1555+
1556+
def _list_outputs(self):
1557+
outputs = self.output_spec().get()
1558+
outputs['out_file'] = self.inputs.out_file
1559+
1560+
suffix = 'field'
1561+
1562+
if isdefined(self.inputs.out_format) and self.inputs.out_format=='spline':
1563+
suffix = 'coeffs'
1564+
1565+
1566+
if not isdefined(outputs['out_file']):
1567+
outputs['out_file'] = self._gen_fname(self.inputs.in_file,
1568+
suffix='_'+suffix)
1569+
1570+
if isdefined(self.inputs.out_jacobian):
1571+
outputs['out_jacobian'] = os.path.abspath(self.inputs.out_jacobian)
1572+
1573+
outputs['out_file'] = os.path.abspath(outputs['out_file'])
1574+
return outputs
1575+
1576+
def _gen_filename(self, name):
1577+
if name == 'out_file':
1578+
return self._list_outputs()[name]
1579+
1580+
return None

nipype/testing/data/warpfield.nii

Whitespace-only changes.

0 commit comments

Comments
 (0)