|
6 | 6 | import os |
7 | 7 | import warnings |
8 | 8 |
|
9 | | -from ...utils.filemanip import fname_presuffix, split_filename |
| 9 | +from ...utils.filemanip import split_filename |
10 | 10 | from ..base import ( |
11 | 11 | CommandLine, traits, CommandLineInputSpec, isdefined, File, TraitedSpec) |
12 | 12 |
|
13 | 13 | warn = warnings.warn |
14 | 14 | warnings.filterwarnings('always', category=UserWarning) |
15 | 15 |
|
16 | | -################################### |
17 | | -# |
18 | | -# NEW_AFNI base class |
19 | | -# |
20 | | -################################### |
21 | | - |
22 | 16 |
|
23 | 17 | class Info(object): |
24 | 18 | """Handle afni output type and version information. |
25 | 19 | """ |
26 | 20 | __outputtype = 'AFNI' |
27 | 21 | ftypes = {'NIFTI': '.nii', |
28 | | - 'AFNI': '+orig.BRIK', |
| 22 | + 'AFNI': '', |
29 | 23 | 'NIFTI_GZ': '.nii.gz'} |
30 | 24 |
|
31 | 25 | @staticmethod |
@@ -98,25 +92,26 @@ def standard_image(img_name): |
98 | 92 | return os.path.join(basedir, img_name) |
99 | 93 |
|
100 | 94 |
|
101 | | -class AFNIBaseCommandInputSpec(CommandLineInputSpec): |
| 95 | +class AFNICommandInputSpec(CommandLineInputSpec): |
102 | 96 | outputtype = traits.Enum('AFNI', Info.ftypes.keys(), |
103 | 97 | desc='AFNI output filetype') |
| 98 | + out_file = File(name_template="%s_afni", desc='output image file name', |
| 99 | + argstr='-prefix %s', |
| 100 | + name_source=["in_file"]) |
| 101 | + |
| 102 | +class AFNICommandOutputSpec(TraitedSpec): |
| 103 | + out_file = File(desc='output file', |
| 104 | + exists=True) |
104 | 105 |
|
105 | | -class AFNITraitedSpec(AFNIBaseCommandInputSpec): |
106 | | - pass |
107 | | - |
108 | | - |
109 | | -class AFNIBaseCommand(CommandLine): |
110 | | - """General support for AFNI commands. Every AFNI command accepts 'outputtype' input. For example: |
111 | | - afni.Threedsetup(outputtype='NIFTI_GZ') |
112 | | - """ |
113 | 106 |
|
114 | | - input_spec = AFNIBaseCommandInputSpec |
| 107 | +class AFNICommand(CommandLine): |
| 108 | + |
| 109 | + input_spec = AFNICommandInputSpec |
115 | 110 | _outputtype = None |
116 | 111 |
|
117 | 112 |
|
118 | 113 | def __init__(self, **inputs): |
119 | | - super(AFNIBaseCommand, self).__init__(**inputs) |
| 114 | + super(AFNICommand, self).__init__(**inputs) |
120 | 115 | self.inputs.on_trait_change(self._output_update, 'outputtype') |
121 | 116 |
|
122 | 117 | if self._outputtype is None: |
@@ -148,64 +143,19 @@ def set_default_output_type(cls, outputtype): |
148 | 143 | cls._outputtype = outputtype |
149 | 144 | else: |
150 | 145 | raise AttributeError('Invalid AFNI outputtype: %s' % outputtype) |
151 | | - |
152 | | - def _gen_fname(self, basename, cwd=None, suffix='_afni', change_ext=True, prefix=''): |
153 | | - """Generate a filename based on the given parameters. |
154 | | -
|
155 | | - The filename will take the form: cwd/basename<suffix><ext>. |
156 | | - If change_ext is True, it will use the extensions specified in |
157 | | - <instance>inputs.outputtype. |
158 | | -
|
159 | | - Parameters |
160 | | - ---------- |
161 | | - basename : str |
162 | | - Filename to base the new filename on. |
163 | | - cwd : str |
164 | | - Path to prefix to the new filename. (default is os.getcwd()) |
165 | | - suffix : str |
166 | | - Suffix to add to the `basename`. (default is '_fsl') |
167 | | - change_ext : bool |
168 | | - Flag to change the filename extension to the FSL output type. |
169 | | - (default True) |
170 | | -
|
171 | | - Returns |
172 | | - ------- |
173 | | - fname : str |
174 | | - New filename based on given parameters. |
175 | | -
|
176 | | - """ |
177 | | - |
178 | | - if basename == '': |
179 | | - msg = 'Unable to generate filename for command %s. ' % self.cmd |
180 | | - msg += 'basename is not set!' |
181 | | - raise ValueError(msg) |
182 | | - if cwd is None: |
183 | | - cwd = os.getcwd() |
184 | | - ext = Info.outputtype_to_ext(self.inputs.outputtype) |
185 | | - if change_ext: |
186 | | - if suffix: |
187 | | - suffix = ''.join((suffix, ext)) |
188 | | - else: |
189 | | - suffix = ext |
190 | | - fname = fname_presuffix(basename, suffix=suffix, |
191 | | - use_ext=False, newpath=cwd, prefix=prefix) |
192 | | - return fname |
193 | | - |
194 | | - |
195 | | -class AFNICommandInputSpec(AFNIBaseCommandInputSpec): |
196 | | - out_file = File("%s_afni", desc='output image file name', |
197 | | - argstr='-prefix %s', xor=['out_file', 'prefix', 'suffix'], |
198 | | - name_source="in_file", usedefault=True) |
199 | | - |
200 | | - |
201 | | -class AFNICommand(AFNIBaseCommand): |
202 | | - input_spec = AFNICommandInputSpec |
203 | | - |
| 146 | + |
204 | 147 | def _overload_extension(self, value): |
205 | 148 | path, base, _ = split_filename(value) |
206 | 149 | return os.path.join(path, base + Info.outputtype_to_ext(self.inputs.outputtype)) |
207 | 150 |
|
208 | | - |
209 | | -class AFNICommandOutputSpec(TraitedSpec): |
210 | | - out_file = File(desc='output file', |
211 | | - exists=True) |
| 151 | + def _list_outputs(self): |
| 152 | + outputs = super(AFNICommand, self)._list_outputs() |
| 153 | + metadata = dict(name_source=lambda t: t is not None) |
| 154 | + out_names = self.inputs.traits(**metadata).keys() |
| 155 | + if out_names: |
| 156 | + for name in out_names: |
| 157 | + if outputs[name]: |
| 158 | + _,_,ext = split_filename(outputs[name]) |
| 159 | + if ext == "": |
| 160 | + outputs[name] = outputs[name] + "+orig.BRIK" |
| 161 | + return outputs |
0 commit comments