|
6 | 6 | import os
|
7 | 7 | import warnings
|
8 | 8 |
|
9 |
| -from ...utils.filemanip import fname_presuffix |
10 |
| -from ..base import (CommandLine, traits, CommandLineInputSpec, isdefined) |
| 9 | +from ...utils.filemanip import fname_presuffix, split_filename |
| 10 | +from ..base import (CommandLine, traits, CommandLineInputSpec, isdefined, File, TraitedSpec) |
11 | 11 |
|
12 | 12 | warn = warnings.warn
|
13 | 13 | warnings.filterwarnings('always', category=UserWarning)
|
@@ -98,6 +98,9 @@ def standard_image(img_name):
|
98 | 98 | class AFNITraitedSpec(CommandLineInputSpec):
|
99 | 99 | outputtype = traits.Enum('AFNI', Info.ftypes.keys(),
|
100 | 100 | desc = 'AFNI output filetype')
|
| 101 | + |
| 102 | + |
| 103 | + |
101 | 104 |
|
102 | 105 |
|
103 | 106 | class AFNICommand(CommandLine):
|
@@ -143,7 +146,7 @@ def set_default_outputtype(cls, outputtype):
|
143 | 146 | else:
|
144 | 147 | raise AttributeError('Invalid AFNI outputtype: %s' % outputtype)
|
145 | 148 |
|
146 |
| - def _gen_fname(self, basename, cwd=None, suffix='_afni', change_ext=True): |
| 149 | + def _gen_fname(self, basename, cwd=None, suffix='_afni', change_ext=True, prefix=''): |
147 | 150 | """Generate a filename based on the given parameters.
|
148 | 151 |
|
149 | 152 | The filename will take the form: cwd/basename<suffix><ext>.
|
@@ -182,8 +185,42 @@ def _gen_fname(self, basename, cwd=None, suffix='_afni', change_ext=True):
|
182 | 185 | else:
|
183 | 186 | suffix = ext
|
184 | 187 | fname = fname_presuffix(basename, suffix = suffix,
|
185 |
| - use_ext = False, newpath = cwd) |
| 188 | + use_ext = False, newpath = cwd, prefix=prefix) |
186 | 189 | return fname
|
187 | 190 |
|
188 |
| - |
189 |
| - |
| 191 | +class AFNIPrefixInputSpec(AFNITraitedSpec): |
| 192 | + out_file = File(desc='output image file name', |
| 193 | + argstr='-prefix %s', xor=['out_file', 'prefix', 'suffix'], genfile=True, hash_files=True) |
| 194 | + prefix = traits.Str(desc='output image prefix', xor=['out_file', 'prefix']) |
| 195 | + suffix = traits.Str(desc='output image suffix', xor=['out_file', 'suffix']) |
| 196 | + |
| 197 | +class AFNIPrefixCommand(AFNICommand): |
| 198 | + input_spec = AFNIPrefixInputSpec |
| 199 | + _suffix = '_afni' |
| 200 | + |
| 201 | + def _gen_out_file(self, source_filename): |
| 202 | + suffix = self._suffix |
| 203 | + prefix = '' |
| 204 | + if isdefined(self.inputs.prefix): |
| 205 | + prefix = self.inputs.prefix |
| 206 | + if isdefined(self.inputs.suffix): |
| 207 | + suffix = self.inputs.suffix |
| 208 | + |
| 209 | + _, base, _ = split_filename(source_filename) |
| 210 | + return self._gen_fname(basename=base, prefix=prefix, suffix=suffix) |
| 211 | + |
| 212 | + def _gen_filename(self, name): |
| 213 | + if name == 'out_file': |
| 214 | + return self._gen_out_file(self.inputs.in_file) |
| 215 | + return None |
| 216 | + |
| 217 | + def _list_outputs(self): |
| 218 | + outputs = self.output_spec().get() |
| 219 | + outputs['out_file'] = self.inputs.out_file |
| 220 | + if not isdefined(outputs['out_file']): |
| 221 | + outputs['out_file'] = self._gen_filename('out_file') |
| 222 | + return outputs |
| 223 | + |
| 224 | +class AFNIPrefixOutputSpec(TraitedSpec): |
| 225 | + out_file = File(desc='output file', |
| 226 | + exists=True) |
0 commit comments