1717import re
1818import shutil
1919
20+ from ... import logging
2021from ...utils .filemanip import fname_presuffix , split_filename
2122from ..base import (TraitedSpec , File , traits , OutputMultiPath , isdefined ,
2223 CommandLine , CommandLineInputSpec )
2930 afni = 'brik' , brik = 'brik' , bshort = 'bshort' ,
3031 spm = 'img' , analyze = 'img' , analyze4d = 'img' ,
3132 bfloat = 'bfloat' , nifti1 = 'img' , nii = 'nii' ,
32- niigz = 'nii.gz' )
33+ niigz = 'nii.gz' , gii = 'gii' )
3334
3435filetypes = ['cor' , 'mgh' , 'mgz' , 'minc' , 'analyze' ,
3536 'analyze4d' , 'spm' , 'afni' , 'brik' , 'bshort' ,
3637 'bfloat' , 'sdt' , 'outline' , 'otl' , 'gdf' ,
3738 'nifti1' , 'nii' , 'niigz' ]
39+ implicit_filetypes = ['gii' ]
3840
41+ logger = logging .getLogger ('interface' )
3942
4043def copy2subjdir (cls , in_file , folder = None , basename = None , subject_id = None ):
4144 """Method to copy an input to the subjects directory"""
@@ -151,7 +154,8 @@ class SampleToSurfaceInputSpec(FSTraitedSpec):
151154 frame = traits .Int (argstr = "--frame %d" , desc = "save only one frame (0-based)" )
152155
153156 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" )
155159 hits_file = traits .Either (traits .Bool , File (exists = True ), argstr = "--srchit %s" ,
156160 desc = "save image with number of hits at each voxel" )
157161 hits_type = traits .Enum (filetypes , argstr = "--srchit_type" , desc = "hits file type" )
@@ -201,12 +205,6 @@ class SampleToSurface(FSCommand):
201205 input_spec = SampleToSurfaceInputSpec
202206 output_spec = SampleToSurfaceOutputSpec
203207
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-
210208 def _format_arg (self , name , spec , value ):
211209 if name == "sampling_method" :
212210 range = self .inputs .sampling_range
@@ -226,16 +224,29 @@ def _format_arg(self, name, spec, value):
226224 return spec .argstr % self .inputs .subject_id
227225 if name in ["hits_file" , "vox_file" ]:
228226 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 ""
229240 return super (SampleToSurface , self )._format_arg (name , spec , value )
230241
231242 def _get_outfilename (self , opt = "out_file" ):
232243 outfile = getattr (self .inputs , opt )
233244 if not isdefined (outfile ) or isinstance (outfile , bool ):
234245 if isdefined (self .inputs .out_type ):
235246 if opt == "hits_file" :
236- suffix = '_hits.' + self . filemap [self .inputs .out_type ]
247+ suffix = '_hits.' + filemap [self .inputs .out_type ]
237248 else :
238- suffix = '.' + self . filemap [self .inputs .out_type ]
249+ suffix = '.' + filemap [self .inputs .out_type ]
239250 elif opt == "hits_file" :
240251 suffix = "_hits.mgz"
241252 else :
@@ -365,7 +376,7 @@ class SurfaceTransformInputSpec(FSTraitedSpec):
365376 source_type = traits .Enum (filetypes , argstr = '--sfmt %s' ,
366377 requires = ['source_file' ],
367378 desc = "source file format" )
368- target_type = traits .Enum (filetypes , argstr = '--tfmt %s' ,
379+ target_type = traits .Enum (filetypes + implicit_filetypes , argstr = '--tfmt %s' ,
369380 desc = "output format" )
370381 reshape = traits .Bool (argstr = "--reshape" ,
371382 desc = "reshape output surface to conform with Nifti" )
@@ -402,6 +413,22 @@ class SurfaceTransform(FSCommand):
402413 input_spec = SurfaceTransformInputSpec
403414 output_spec = SurfaceTransformOutputSpec
404415
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+
405432 def _list_outputs (self ):
406433 outputs = self ._outputs ().get ()
407434 outputs ["out_file" ] = self .inputs .out_file
0 commit comments