|
| 1 | +from nipype.interfaces.base import CommandLineInputSpec, File, traits, TraitedSpec |
| 2 | +from nipype.interfaces.workbench.base import WBCommand |
| 3 | + |
| 4 | + |
| 5 | +class VolumeAffineResampleInputSpec(CommandLineInputSpec): |
| 6 | + in_file = File( |
| 7 | + exists=True, |
| 8 | + mandatory=True, |
| 9 | + argstr="%s", |
| 10 | + position=0, |
| 11 | + desc="volume to resample", |
| 12 | + ) |
| 13 | + volume_space = File( |
| 14 | + exists=True, |
| 15 | + mandatory=True, |
| 16 | + argstr="%s", |
| 17 | + position=1, |
| 18 | + desc="a volume file in the volume space you want for the output", |
| 19 | + ) |
| 20 | + method = traits.Enum("CUBIC", "ENCLOSING_VOXEL", "TRILINEAR", |
| 21 | + mandatory=True, |
| 22 | + argstr="%s", |
| 23 | + position=2, |
| 24 | + desc="The resampling method. The recommended methods are CUBIC " |
| 25 | + "(cubic spline) for most data, and ENCLOSING_VOXEL for label data.", |
| 26 | + ) |
| 27 | + out_file = File( |
| 28 | + name_source=["in_file"], |
| 29 | + name_template="resampled_%s.nii.gz", |
| 30 | + keep_extension=True, |
| 31 | + argstr="%s", |
| 32 | + position=3, |
| 33 | + desc="the output volume", |
| 34 | + ) |
| 35 | + affine = File( |
| 36 | + exists=True, |
| 37 | + mandatory=True, |
| 38 | + argstr="-affine %s", |
| 39 | + position=4, |
| 40 | + desc="the affine file to apply", |
| 41 | + ) |
| 42 | + flirt = traits.Bool( |
| 43 | + argstr="-flirt %s %s", |
| 44 | + position=5, |
| 45 | + desc="Set ``True`` if ``affine`` is a FLIRT affine.", |
| 46 | + ) |
| 47 | + flirt_source_volume = File( |
| 48 | + exists=True, |
| 49 | + desc="the source volume used when generating the affine; defaults to in_file", |
| 50 | + requires=['flirt'], |
| 51 | + ) |
| 52 | + flirt_target_volume = File( |
| 53 | + exists=True, |
| 54 | + desc="the target volume used when generating the affine; defaults to volume_space", |
| 55 | + requires=['flirt'], |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +class VolumeAffineResampleOutputSpec(TraitedSpec): |
| 60 | + out_file = File(exists=True, desc="the output volume") |
| 61 | + |
| 62 | + |
| 63 | +class VolumeAffineResample(WBCommand): |
| 64 | + """ |
| 65 | + Resample a volume file with an affine transformation. |
| 66 | + """ |
| 67 | + |
| 68 | + input_spec = VolumeAffineResampleInputSpec |
| 69 | + output_spec = VolumeAffineResampleOutputSpec |
| 70 | + _cmd = "wb_command -volume-resample" |
| 71 | + |
| 72 | + def _format_arg(self, opt, spec, val): |
| 73 | + if opt == "flirt" and val: |
| 74 | + val = ( |
| 75 | + self.inputs.flirt_source_volume or self.inputs.in_file, |
| 76 | + self.inputs.flirt_target_volume or self.inputs.volume_space, |
| 77 | + ) |
| 78 | + return super()._format_arg(opt, spec, val) |
| 79 | + |
0 commit comments