Skip to content

Commit 8ac4755

Browse files
author
Erik Ziegler
committed
Fixes for SPM ApplyTransform
1 parent 07143af commit 8ac4755

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

nipype/interfaces/spm/utils.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -121,28 +121,25 @@ class ApplyTransformInputSpec(SPMCommandInputSpec):
121121
desc='file to apply transform to, (only updates header)')
122122
mat = File( exists = True, mandatory = True,
123123
desc='file holding transform to apply')
124-
124+
out_file = File(desc="output file name for transformed data",
125+
genfile=True)
125126

126127
class ApplyTransformOutputSpec(TraitedSpec):
127-
out_file = File(exists = True, desc = 'File with updated header')
128+
out_file = File(exists = True, desc = 'Transformed image file')
128129

129130

130131
class ApplyTransform(SPMCommand):
131-
""" Uses spm to apply transform stored in a .mat file to given file
132+
""" Uses SPM to apply transform stored in a .mat file to given file
132133
133134
Examples
134135
--------
135136
136137
>>> import nipype.interfaces.spm.utils as spmu
137-
>>> applymat = spmu.ApplyTransform(matlab_cmd='matlab-spm8')
138+
>>> applymat = spmu.ApplyTransform()
138139
>>> applymat.inputs.in_file = 'functional.nii'
139140
>>> applymat.inputs.mat = 'func_to_struct.mat'
140141
>>> applymat.run() # doctest: +SKIP
141142
142-
.. warning::
143-
144-
CHANGES YOUR INPUT FILE (applies transform by updating the header)
145-
except when used with nipype caching or workflow.
146143
"""
147144
input_spec = ApplyTransformInputSpec
148145
output_spec = ApplyTransformOutputSpec
@@ -151,19 +148,37 @@ def _make_matlab_command(self, _):
151148
"""checks for SPM, generates script"""
152149
script = """
153150
infile = '%s';
151+
outfile = '%s'
154152
transform = load('%s');
155-
M = inv(transform.M);
156-
img_space = spm_get_space(infile);
157-
spm_get_space(infile, M * img_space);
153+
154+
V = spm_vol(infile);
155+
X = spm_read_vols(V);
156+
[p n e v] = spm_fileparts(V.fname);
157+
V.mat = transform.M * V.mat;
158+
V.fname = fullfile(outfile);
159+
spm_write_vol(V,X);
160+
158161
"""%(self.inputs.in_file,
162+
self.inputs.out_file,
159163
self.inputs.mat)
164+
#img_space = spm_get_space(infile);
165+
#spm_get_space(infile, transform.M * img_space);
160166
return script
161167

162168
def _list_outputs(self):
163-
outputs = self._outputs().get()
164-
outputs['out_file'] = os.path.abspath(self.inputs.in_file)
169+
outputs = self.output_spec().get()
170+
if not isdefined(self.inputs.out_file):
171+
_, name, _ = split_filename(self.inputs.in_file)
172+
outputs['out_file'] = os.path.abspath(name + '_trans.nii')
173+
else:
174+
outputs['out_file'] = os.path.abspath(self.inputs.out_file)
165175
return outputs
166176

177+
def _gen_filename(self, name):
178+
if name == 'out_file':
179+
return self._list_outputs()[name]
180+
return None
181+
167182
class ResliceInputSpec(SPMCommandInputSpec):
168183
in_file = File( exists = True, mandatory=True,
169184
desc='file to apply transform to, (only updates header)')

0 commit comments

Comments
 (0)