Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions nipype/interfaces/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,7 @@ def _format_arg(self, name, trait_spec, value):
def _filename_from_source(self, name):
trait_spec = self.inputs.trait(name)
retval = getattr(self.inputs, name)

if not isdefined(retval) or "%s" in retval:
if not trait_spec.name_source:
return retval
Expand All @@ -1478,26 +1479,37 @@ def _filename_from_source(self, name):
name_template = trait_spec.name_template
if not name_template:
name_template = "%s_generated"
if isinstance(trait_spec.name_source, list):
for ns in trait_spec.name_source:
if isdefined(getattr(self.inputs, ns)):
name_source = ns
break

ns = trait_spec.name_source
while isinstance(ns, list):
if len(ns) > 1:
iflogger.warn('Only one name_source per trait is allowed')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is only one allowed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, we have a somewhat difficult decision here.

Former implementation iterated the name_source list until it found the first defined name_source.
Current implementation may accept undefined name_sources, but they must have a name_source as well.

In both implementations only one name_source is actually used, right?.

ns = ns[0]

if not isinstance(ns, basestring):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace with six.string_types

raise ValueError(('name_source of \'%s\' trait sould be an '
'input trait name') % name)

if isdefined(getattr(self.inputs, ns)):
name_source = ns
source = getattr(self.inputs, name_source)
while isinstance(source, list):
source = source[0]

# special treatment for files
try:
_, base, _ = split_filename(source)
except AttributeError:
base = source
else:
name_source = trait_spec.name_source
source = getattr(self.inputs, name_source)
while isinstance(source, list):
source = source[0]
#special treatment for files
try:
_, base, _ = split_filename(source)
except AttributeError:
base = source
base = self._filename_from_source(ns)

retval = name_template % base
_, _, ext = split_filename(retval)
if trait_spec.keep_extension and ext:
return retval
return self._overload_extension(retval, name)

return retval

def _gen_filename(self, name):
Expand All @@ -1513,7 +1525,7 @@ def _list_outputs(self):
outputs = self.output_spec().get()
for name, trait_spec in traits.iteritems():
out_name = name
if trait_spec.output_name != None:
if trait_spec.output_name is not None:
out_name = trait_spec.output_name
outputs[out_name] = \
os.path.abspath(self._filename_from_source(name))
Expand Down