17
17
import re
18
18
import shutil
19
19
20
+ from ... import logging
20
21
from ...utils .filemanip import fname_presuffix , split_filename
21
22
from ..base import (TraitedSpec , File , traits , OutputMultiPath , isdefined ,
22
23
CommandLine , CommandLineInputSpec )
29
30
afni = 'brik' , brik = 'brik' , bshort = 'bshort' ,
30
31
spm = 'img' , analyze = 'img' , analyze4d = 'img' ,
31
32
bfloat = 'bfloat' , nifti1 = 'img' , nii = 'nii' ,
32
- niigz = 'nii.gz' )
33
+ niigz = 'nii.gz' , gii = 'gii' )
33
34
34
35
filetypes = ['cor' , 'mgh' , 'mgz' , 'minc' , 'analyze' ,
35
36
'analyze4d' , 'spm' , 'afni' , 'brik' , 'bshort' ,
36
37
'bfloat' , 'sdt' , 'outline' , 'otl' , 'gdf' ,
37
38
'nifti1' , 'nii' , 'niigz' ]
39
+ implicit_filetypes = ['gii' ]
38
40
41
+ logger = logging .getLogger ('interface' )
39
42
40
43
def copy2subjdir (cls , in_file , folder = None , basename = None , subject_id = None ):
41
44
"""Method to copy an input to the subjects directory"""
@@ -151,7 +154,8 @@ class SampleToSurfaceInputSpec(FSTraitedSpec):
151
154
frame = traits .Int (argstr = "--frame %d" , desc = "save only one frame (0-based)" )
152
155
153
156
out_file = File (argstr = "--o %s" , genfile = True , desc = "surface file to write" )
154
- out_type = traits .Enum (filetypes , argstr = "--out_type %s" , desc = "output file type" )
157
+ out_type = traits .Enum (filetypes + implicit_filetypes ,
158
+ argstr = "--out_type %s" , desc = "output file type" )
155
159
hits_file = traits .Either (traits .Bool , File (exists = True ), argstr = "--srchit %s" ,
156
160
desc = "save image with number of hits at each voxel" )
157
161
hits_type = traits .Enum (filetypes , argstr = "--srchit_type" , desc = "hits file type" )
@@ -201,12 +205,6 @@ class SampleToSurface(FSCommand):
201
205
input_spec = SampleToSurfaceInputSpec
202
206
output_spec = SampleToSurfaceOutputSpec
203
207
204
- filemap = dict (cor = 'cor' , mgh = 'mgh' , mgz = 'mgz' , minc = 'mnc' ,
205
- afni = 'brik' , brik = 'brik' , bshort = 'bshort' ,
206
- spm = 'img' , analyze = 'img' , analyze4d = 'img' ,
207
- bfloat = 'bfloat' , nifti1 = 'img' , nii = 'nii' ,
208
- niigz = 'nii.gz' )
209
-
210
208
def _format_arg (self , name , spec , value ):
211
209
if name == "sampling_method" :
212
210
range = self .inputs .sampling_range
@@ -226,16 +224,29 @@ def _format_arg(self, name, spec, value):
226
224
return spec .argstr % self .inputs .subject_id
227
225
if name in ["hits_file" , "vox_file" ]:
228
226
return spec .argstr % self ._get_outfilename (name )
227
+ if name == "out_type" :
228
+ if isdefined (self .inputs .out_file ):
229
+ _ , base , ext = split_filename (self ._get_outfilename ())
230
+ if ext != filemap [value ]:
231
+ if ext in filemap .values ():
232
+ raise ValueError (
233
+ "Cannot create {} file with extension "
234
+ "{}" .format (value , ext ))
235
+ else :
236
+ logger .warn ("Creating {} file with extension {}: "
237
+ "{}{}" .format (value , ext , base , ext ))
238
+ if value in implicit_filetypes :
239
+ return ""
229
240
return super (SampleToSurface , self )._format_arg (name , spec , value )
230
241
231
242
def _get_outfilename (self , opt = "out_file" ):
232
243
outfile = getattr (self .inputs , opt )
233
244
if not isdefined (outfile ) or isinstance (outfile , bool ):
234
245
if isdefined (self .inputs .out_type ):
235
246
if opt == "hits_file" :
236
- suffix = '_hits.' + self . filemap [self .inputs .out_type ]
247
+ suffix = '_hits.' + filemap [self .inputs .out_type ]
237
248
else :
238
- suffix = '.' + self . filemap [self .inputs .out_type ]
249
+ suffix = '.' + filemap [self .inputs .out_type ]
239
250
elif opt == "hits_file" :
240
251
suffix = "_hits.mgz"
241
252
else :
@@ -365,7 +376,7 @@ class SurfaceTransformInputSpec(FSTraitedSpec):
365
376
source_type = traits .Enum (filetypes , argstr = '--sfmt %s' ,
366
377
requires = ['source_file' ],
367
378
desc = "source file format" )
368
- target_type = traits .Enum (filetypes , argstr = '--tfmt %s' ,
379
+ target_type = traits .Enum (filetypes + implicit_filetypes , argstr = '--tfmt %s' ,
369
380
desc = "output format" )
370
381
reshape = traits .Bool (argstr = "--reshape" ,
371
382
desc = "reshape output surface to conform with Nifti" )
@@ -402,6 +413,22 @@ class SurfaceTransform(FSCommand):
402
413
input_spec = SurfaceTransformInputSpec
403
414
output_spec = SurfaceTransformOutputSpec
404
415
416
+ def _format_arg (self , name , spec , value ):
417
+ if name == "target_type" :
418
+ if isdefined (self .inputs .out_file ):
419
+ _ , base , ext = split_filename (self ._list_outputs ()['out_file' ])
420
+ if ext != filemap [value ]:
421
+ if ext in filemap .values ():
422
+ raise ValueError (
423
+ "Cannot create {} file with extension "
424
+ "{}" .format (value , ext ))
425
+ else :
426
+ logger .warn ("Creating {} file with extension {}: "
427
+ "{}{}" .format (value , ext , base , ext ))
428
+ if value in implicit_filetypes :
429
+ return ""
430
+ return super (SurfaceTransform , self )._format_arg (name , spec , value )
431
+
405
432
def _list_outputs (self ):
406
433
outputs = self ._outputs ().get ()
407
434
outputs ["out_file" ] = self .inputs .out_file
0 commit comments