|
1 |
| -from nipype.interfaces.spm.base import SPMCommandInputSpec, SPMCommand |
2 |
| -from nipype.interfaces.base import File |
3 |
| -from nipype.utils.filemanip import split_filename |
| 1 | +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- |
| 2 | +# vi: set ft=python sts=4 ts=4 sw=4 et: |
| 3 | +from nipype.interfaces.spm.base import SPMCommandInputSpec, SPMCommand, Info |
| 4 | +from nipype.interfaces.matlab import MatlabCommand |
| 5 | +from nipype.interfaces.base import (TraitedSpec, BaseInterface, |
| 6 | + BaseInterfaceInputSpec, isdefined) |
| 7 | +from nipype.interfaces.base import File, traits |
| 8 | +from nipype.utils.filemanip import split_filename, fname_presuffix |
4 | 9 | import os
|
5 | 10 |
|
6 | 11 | class Analyze2niiInputSpec(SPMCommandInputSpec):
|
@@ -28,3 +33,176 @@ def _list_outputs(self):
|
28 | 33 | outputs = self._outputs().get()
|
29 | 34 | outputs['nifti_file'] = self.output_name
|
30 | 35 | return outputs
|
| 36 | + |
| 37 | +class CalcCoregAffineInputSpec(SPMCommandInputSpec): |
| 38 | + target = File( exists = True, mandatory = True, |
| 39 | + desc = 'target for generating affine transform') |
| 40 | + moving = File( exists = True, mandatory = True, |
| 41 | + desc = 'volume transform can be applied to register with target') |
| 42 | + mat = File( desc = 'Filename used to store affine matrix') |
| 43 | + invmat = File( desc = 'Filename used to store inverse affine matrix') |
| 44 | + |
| 45 | + |
| 46 | +class CalcCoregAffineOutputSpec(SPMCommandInputSpec): |
| 47 | + mat = File(exists = True, desc = 'Matlab file holding transform') |
| 48 | + invmat = File( desc = 'Matlab file holding inverse transform') |
| 49 | + |
| 50 | + |
| 51 | +class CalcCoregAffine(SPMCommand): |
| 52 | + """ Uses SPM (spm_coreg) to calculate the transform mapping |
| 53 | + moving to target. Saves Transform in mat (matlab binary file) |
| 54 | + Also saves inverse transform |
| 55 | + |
| 56 | + Examples |
| 57 | + -------- |
| 58 | +
|
| 59 | + >>> import nipype.interfaces.spm.utils as spmu |
| 60 | + >>> coreg = spmu.CalcCoregAffine(matlab_cmd='matlab-spm8') |
| 61 | + >>> coreg.inputs.target = 'structural.nii' |
| 62 | + >>> coreg.inputs.moving = 'functional.nii' |
| 63 | + >>> coreg.inputs.mat = 'func_to_struct.mat' |
| 64 | + >>> coreg.run() # doctest: +SKIP |
| 65 | +
|
| 66 | + Notes |
| 67 | + ----- |
| 68 | + |
| 69 | + * the output file mat is saves as a matlab binary file |
| 70 | + * calculating the transforms does NOT change either input image |
| 71 | + it does not **move** the moving image, only calculates the transform |
| 72 | + that can be used to move it |
| 73 | + """ |
| 74 | + |
| 75 | + input_spec = CalcCoregAffineInputSpec |
| 76 | + output_spec = CalcCoregAffineOutputSpec |
| 77 | + |
| 78 | + def _make_inv_file(self): |
| 79 | + """ makes filename to hold inverse transform if not specified""" |
| 80 | + invmat = fname_presuffix(self.inputs.mat, prefix = 'inverse_') |
| 81 | + return invmat |
| 82 | + |
| 83 | + def _make_mat_file(self): |
| 84 | + """ makes name for matfile if doesn exist""" |
| 85 | + pth, mv, _ = split_filename(self.inputs.moving) |
| 86 | + _, tgt, _ = split_filename(self.inputs.target) |
| 87 | + mat = os.path.join(pth, '%s_to_%s.mat'%(mv,tgt)) |
| 88 | + return mat |
| 89 | + |
| 90 | + def _make_matlab_command(self, _): |
| 91 | + """checks for SPM, generates script""" |
| 92 | + if not isdefined(self.inputs.mat): |
| 93 | + self.inputs.mat = self._make_mat_file() |
| 94 | + if not isdefined(self.inputs.invmat): |
| 95 | + self.inputs.invmat = self._make_inv_file() |
| 96 | + script = """ |
| 97 | + target = '%s'; |
| 98 | + moving = '%s'; |
| 99 | + targetv = spm_vol(target); |
| 100 | + movingv = spm_vol(moving); |
| 101 | + x = spm_coreg(movingv, targetv); |
| 102 | + M = spm_matrix(x(:)'); |
| 103 | + save('%s' , 'M' ); |
| 104 | + M = inv(spm_matrix(x(:)')); |
| 105 | + save('%s','M') |
| 106 | + """%(self.inputs.target, |
| 107 | + self.inputs.moving, |
| 108 | + self.inputs.mat, |
| 109 | + self.inputs.invmat) |
| 110 | + return script |
| 111 | + |
| 112 | + def _list_outputs(self): |
| 113 | + outputs = self._outputs().get() |
| 114 | + outputs['mat'] = os.path.abspath(self.inputs.mat) |
| 115 | + outputs['invmat'] = os.path.abspath(self.inputs.invmat) |
| 116 | + return outputs |
| 117 | + |
| 118 | +class ApplyTransformInputSpec(SPMCommandInputSpec): |
| 119 | + in_file = File( exists = True, mandatory = True, |
| 120 | + desc='file to apply transform to, (only updates header)') |
| 121 | + mat = File( exists = True, mandatory = True, |
| 122 | + desc='file holding transform to apply') |
| 123 | + |
| 124 | + |
| 125 | +class ApplyTransformOutputSpec(SPMCommandInputSpec): |
| 126 | + out_file = File(exists = True, desc = 'File with updated header') |
| 127 | + |
| 128 | + |
| 129 | +class ApplyTransform(SPMCommand): |
| 130 | + """ Uses spm to apply transform stored in a .mat file to given file |
| 131 | + |
| 132 | + Examples |
| 133 | + -------- |
| 134 | +
|
| 135 | + >>> import nipype.interfaces.spm.utils as spmu |
| 136 | + >>> applymat = spmu.ApplyTransform(matlab_cmd='matlab-spm8') |
| 137 | + >>> applymat.inputs.in_file = 'functional.nii' |
| 138 | + >>> applymat.inputs.mat = 'func_to_struct.mat' |
| 139 | + >>> applymat.run() # doctest: +SKIP |
| 140 | +
|
| 141 | + Notes |
| 142 | + ----- |
| 143 | + CHANGES YOUR INPUT FILE (applies transform by updating the header) |
| 144 | + """ |
| 145 | + input_spec = ApplyTransformInputSpec |
| 146 | + output_spec = ApplyTransformOutputSpec |
| 147 | + |
| 148 | + def _make_matlab_command(self, _): |
| 149 | + """checks for SPM, generates script""" |
| 150 | + script = """ |
| 151 | + infile = '%s'; |
| 152 | + transform = load('%s'); |
| 153 | + img_space = spm_get_space(infile); |
| 154 | + spm_get_space(infile, transform.M * img_space); |
| 155 | + """%(self.inputs.in_file, |
| 156 | + self.inputs.mat) |
| 157 | + return script |
| 158 | + |
| 159 | + def _list_outputs(self): |
| 160 | + outputs = self._outputs().get() |
| 161 | + outputs['out_file'] = os.path.abspath(self.inputs.mat) |
| 162 | + return outputs |
| 163 | + |
| 164 | +class ResliceInputSpec(SPMCommandInputSpec): |
| 165 | + in_file = File( exists = True, mandatory=True, |
| 166 | + desc='file to apply transform to, (only updates header)') |
| 167 | + space_defining = File ( exists = True, mandatory = True, |
| 168 | + desc = 'Volume defining space to slice in_file into') |
| 169 | + |
| 170 | + interp = traits.Range(low = 0, high = 7, usedefault = True, |
| 171 | + desc='degree of b-spline used for interpolation'\ |
| 172 | + '0 is nearest neighbor (default)') |
| 173 | + |
| 174 | + |
| 175 | + out_file = File(desc = 'Optional file to save resliced volume') |
| 176 | + |
| 177 | +class ResliceOutputSpec(SPMCommandInputSpec): |
| 178 | + out_file = File( exists = True, desc = 'resliced volume') |
| 179 | + |
| 180 | +class Reslice(SPMCommand): |
| 181 | + """ uses spm_reslice to resample in_file into space of space_defining""" |
| 182 | + |
| 183 | + input_spec = ResliceInputSpec |
| 184 | + output_spec = ResliceOutputSpec |
| 185 | + |
| 186 | + def _make_matlab_command(self, _): |
| 187 | + """ generates script""" |
| 188 | + if not isdefined(self.inputs.out_file): |
| 189 | + self.inputs.out_file = fname_presuffix(self.inputs.in_file, |
| 190 | + prefix = 'r') |
| 191 | + script = """ |
| 192 | + flags.mean = 0; |
| 193 | + flags.which = 1; |
| 194 | + flags.mask = 0; |
| 195 | + flags.interp = %d; |
| 196 | + infiles = strvcat(\'%s\', \'%s\'); |
| 197 | + invols = spm_vol(infiles); |
| 198 | + spm_reslice(invols, flags); |
| 199 | + """%(self.inputs.interp, |
| 200 | + self.inputs.space_defining, |
| 201 | + self.inputs.in_file) |
| 202 | + return script |
| 203 | + |
| 204 | + def _list_outputs(self): |
| 205 | + outputs = self._outputs().get() |
| 206 | + outputs['out_file'] = os.path.abspath(self.inputs.out_file) |
| 207 | + return outputs |
| 208 | + |
0 commit comments