Skip to content

Commit f786fce

Browse files
committed
Warn/error on extension/file type mismatch
Error if extension is associated with a known file type Warn if extension is unknown
1 parent 839e250 commit f786fce

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

nipype/interfaces/freesurfer/utils.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import re
1818
import shutil
1919

20+
from ... import logging
2021
from ...utils.filemanip import fname_presuffix, split_filename
2122
from ..base import (TraitedSpec, File, traits, OutputMultiPath, isdefined,
2223
CommandLine, CommandLineInputSpec)
@@ -37,6 +38,7 @@
3738
'nifti1', 'nii', 'niigz']
3839
implicit_filetypes = ['gii']
3940

41+
logger = logging.getLogger('interface')
4042

4143
def copy2subjdir(cls, in_file, folder=None, basename=None, subject_id=None):
4244
"""Method to copy an input to the subjects directory"""
@@ -222,8 +224,19 @@ def _format_arg(self, name, spec, value):
222224
return spec.argstr % self.inputs.subject_id
223225
if name in ["hits_file", "vox_file"]:
224226
return spec.argstr % self._get_outfilename(name)
225-
if name == "out_type" and value in implicit_filetypes:
226-
return ""
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 ""
227240
return super(SampleToSurface, self)._format_arg(name, spec, value)
228241

229242
def _get_outfilename(self, opt="out_file"):
@@ -401,8 +414,19 @@ class SurfaceTransform(FSCommand):
401414
output_spec = SurfaceTransformOutputSpec
402415

403416
def _format_arg(self, name, spec, value):
404-
if name == "target_type" and value in implicit_filetypes:
405-
return ""
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 ""
406430
return super(SurfaceTransform, self)._format_arg(name, spec, value)
407431

408432
def _list_outputs(self):

0 commit comments

Comments
 (0)