Skip to content

Commit 9bcfd51

Browse files
committed
ENH: Add VolumeAffineResample interface to wb_command -volume-resample
1 parent ee30106 commit 9bcfd51

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

nibabies/interfaces/workbench.py

+107
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,110 @@ class CiftiDilate(WBCommand):
132132
input_spec = CiftiDilateInputSpec
133133
output_spec = CiftiDilateOutputSpec
134134
_cmd = "wb_command -cifti-dilate"
135+
136+
137+
class VolumeAffineResampleInputSpec(CommandLineInputSpec):
138+
in_file = File(
139+
exists=True,
140+
mandatory=True,
141+
argstr="%s",
142+
position=0,
143+
desc="volume to resample",
144+
)
145+
volume_space = File(
146+
exists=True,
147+
mandatory=True,
148+
argstr="%s",
149+
position=1,
150+
desc="a volume file in the volume space you want for the output",
151+
)
152+
method = traits.Enum(
153+
"CUBIC", "ENCLOSING_VOXEL", "TRILINEAR",
154+
mandatory=True,
155+
argstr="%s",
156+
position=2,
157+
desc="The resampling method. The recommended methods are CUBIC "
158+
"(cubic spline) for most data, and ENCLOSING_VOXEL for label data.",
159+
)
160+
out_file = File(
161+
name_source=["in_file"],
162+
name_template="resampled_%s.nii.gz",
163+
keep_extension=True,
164+
argstr="%s",
165+
position=3,
166+
desc="the output volume",
167+
)
168+
affine = File(
169+
exists=True,
170+
mandatory=True,
171+
argstr="-affine %s",
172+
position=4,
173+
desc="the affine file to apply",
174+
)
175+
flirt = traits.Bool(
176+
argstr="-flirt %s %s",
177+
position=5,
178+
desc="Set ``True`` if ``affine`` is a FLIRT affine.",
179+
)
180+
flirt_source_volume = File(
181+
exists=True,
182+
desc="the source volume used when generating the affine; defaults to in_file",
183+
requires=['flirt'],
184+
)
185+
flirt_target_volume = File(
186+
exists=True,
187+
desc="the target volume used when generating the affine; defaults to volume_space",
188+
requires=['flirt'],
189+
)
190+
191+
192+
class VolumeAffineResampleOutputSpec(TraitedSpec):
193+
out_file = File(exists=True, desc="the output volume")
194+
195+
196+
class VolumeAffineResample(WBCommand):
197+
"""
198+
Resample a volume file with an affine transformation.
199+
200+
>>> from nibabies.interfaces.workbench import VolumeAffineResample
201+
>>> resample = VolumeAffineResample()
202+
>>> resample.inputs.in_file = data_dir /'functional.nii'
203+
>>> resample.inputs.volume_space = data_dir /'anatomical.nii'
204+
>>> resample.inputs.method = 'CUBIC'
205+
>>> resample.inputs.affine = data_dir / 'func_to_struct.mat'
206+
>>> resample.cmdline #doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
207+
'wb_command -volume-resample .../functional.nii .../anatomical.nii CUBIC \
208+
resampled_functional.nii.gz -affine .../func_to_struct.mat'
209+
210+
If the affine was generated with FLIRT, this should be indicated.
211+
By default, the interface will use the ``in_file`` and ``volume_space``
212+
for references.
213+
214+
>>> resample.inputs.flirt = True
215+
>>> resample.cmdline #doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
216+
'wb_command -volume-resample .../functional.nii .../anatomical.nii CUBIC \
217+
resampled_functional.nii.gz -affine .../func_to_struct.mat \
218+
-flirt .../functional.nii .../anatomical.nii'
219+
220+
However, if other volumes were used to calculate the affine, they can
221+
be provided:
222+
223+
>>> resample.inputs.flirt_source_volume = data_dir / 'epi.nii'
224+
>>> resample.inputs.flirt_target_volume = data_dir /'T1w.nii'
225+
>>> resample.cmdline #doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
226+
'wb_command -volume-resample .../functional.nii .../anatomical.nii CUBIC \
227+
resampled_functional.nii.gz -affine .../func_to_struct.mat \
228+
-flirt .../epi.nii .../T1w.nii'
229+
"""
230+
231+
input_spec = VolumeAffineResampleInputSpec
232+
output_spec = VolumeAffineResampleOutputSpec
233+
_cmd = "wb_command -volume-resample"
234+
235+
def _format_arg(self, opt, spec, val):
236+
if opt == "flirt" and val:
237+
val = (
238+
self.inputs.flirt_source_volume or self.inputs.in_file,
239+
self.inputs.flirt_target_volume or self.inputs.volume_space,
240+
)
241+
return super()._format_arg(opt, spec, val)

0 commit comments

Comments
 (0)