Skip to content

ENH: Add MultiObject, ensure/simplify_list; alias old names for 1.x compatibility #2517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 24, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions nipype/algorithms/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from ..interfaces.base import (
BaseInterface, traits, TraitedSpec, File, InputMultiPath, OutputMultiPath,
BaseInterfaceInputSpec, isdefined, DynamicTraitedSpec, Undefined)
from ..utils.filemanip import fname_presuffix, split_filename, filename_to_list
from ..utils.filemanip import fname_presuffix, split_filename, ensure_list
from ..utils import NUMPY_MMAP

from . import confounds
Expand Down Expand Up @@ -1479,7 +1479,7 @@ def _gen_fname(self, suffix, idx=None, ext=None):
def _run_interface(self, runtime):
total = None
self._median_files = []
for idx, fname in enumerate(filename_to_list(self.inputs.in_files)):
for idx, fname in enumerate(ensure_list(self.inputs.in_files)):
img = nb.load(fname, mmap=NUMPY_MMAP)
data = np.median(img.get_data(), axis=3)
if self.inputs.median_per_file:
Expand Down
6 changes: 3 additions & 3 deletions nipype/algorithms/modelgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from ..interfaces.base import (BaseInterface, TraitedSpec, InputMultiPath,
traits, File, Bunch, BaseInterfaceInputSpec,
isdefined)
from ..utils.filemanip import filename_to_list
from ..utils.filemanip import ensure_list
from ..utils.misc import normalize_mc_params
from .. import config, logging
iflogger = logging.getLogger('interface')
Expand Down Expand Up @@ -383,7 +383,7 @@ def _generate_standard_design(self,
if outliers is not None:
for i, out in enumerate(outliers):
numscans = 0
for f in filename_to_list(sessinfo[i]['scans']):
for f in ensure_list(sessinfo[i]['scans']):
shape = load(f, mmap=NUMPY_MMAP).shape
if len(shape) == 3 or shape[3] == 1:
iflogger.warning('You are using 3D instead of 4D '
Expand Down Expand Up @@ -580,7 +580,7 @@ def _generate_design(self, infolist=None):
else:
infolist = gen_info(self.inputs.event_files)
concatlist, nscans = self._concatenate_info(infolist)
functional_runs = [filename_to_list(self.inputs.functional_runs)]
functional_runs = [ensure_list(self.inputs.functional_runs)]
realignment_parameters = []
if isdefined(self.inputs.realignment_parameters):
realignment_parameters = []
Expand Down
8 changes: 4 additions & 4 deletions nipype/algorithms/rapidart.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from ..interfaces.base import (BaseInterface, traits, InputMultiPath,
OutputMultiPath, TraitedSpec, File,
BaseInterfaceInputSpec, isdefined)
from ..utils.filemanip import filename_to_list, save_json, split_filename
from ..utils.filemanip import ensure_list, save_json, split_filename
from ..utils.misc import find_indices, normalize_mc_params
from .. import logging, config
iflogger = logging.getLogger('interface')
Expand Down Expand Up @@ -376,7 +376,7 @@ def _list_outputs(self):
outputs['displacement_files'] = []
if isdefined(self.inputs.save_plot) and self.inputs.save_plot:
outputs['plot_files'] = []
for i, f in enumerate(filename_to_list(self.inputs.realigned_files)):
for i, f in enumerate(ensure_list(self.inputs.realigned_files)):
(outlierfile, intensityfile, statsfile, normfile, plotfile,
displacementfile, maskfile) = \
self._get_output_filenames(f, os.getcwd())
Expand Down Expand Up @@ -616,8 +616,8 @@ def _detect_outliers_core(self, imgfile, motionfile, runidx, cwd=None):
def _run_interface(self, runtime):
"""Execute this module.
"""
funcfilelist = filename_to_list(self.inputs.realigned_files)
motparamlist = filename_to_list(self.inputs.realignment_parameters)
funcfilelist = ensure_list(self.inputs.realigned_files)
motparamlist = ensure_list(self.inputs.realignment_parameters)
for i, imgf in enumerate(funcfilelist):
self._detect_outliers_core(
imgf, motparamlist[i], i, cwd=os.getcwd())
Expand Down
6 changes: 3 additions & 3 deletions nipype/interfaces/ants/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from builtins import range, str
import os

from ...utils.filemanip import filename_to_list
from ...utils.filemanip import ensure_list
from ..base import TraitedSpec, File, Str, traits, InputMultiPath, isdefined
from .base import ANTSCommand, ANTSCommandInputSpec, LOCAL_DEFAULT_NUMBER_OF_THREADS

Expand Down Expand Up @@ -1088,14 +1088,14 @@ def _format_registration(self):
if any((isdefined(self.inputs.fixed_image_masks),
isdefined(self.inputs.moving_image_masks))):
if isdefined(self.inputs.fixed_image_masks):
fixed_masks = filename_to_list(
fixed_masks = ensure_list(
self.inputs.fixed_image_masks)
fixed_mask = fixed_masks[ii if len(fixed_masks) > 1 else 0]
else:
fixed_mask = 'NULL'

if isdefined(self.inputs.moving_image_masks):
moving_masks = filename_to_list(
moving_masks = ensure_list(
self.inputs.moving_image_masks)
moving_mask = moving_masks[ii
if len(moving_masks) > 1 else 0]
Expand Down
3 changes: 2 additions & 1 deletion nipype/interfaces/base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

from .traits_extension import (
traits, Undefined, TraitDictObject, TraitListObject, TraitError, isdefined,
File, Directory, Str, DictStrStr, has_metadata, ImageFile, MultiPath,
File, Directory, Str, DictStrStr, has_metadata, ImageFile,
OutputMultiObject, InputMultiObject,
OutputMultiPath, InputMultiPath)

from .support import (Bunch, InterfaceResult, load_template,
Expand Down
23 changes: 13 additions & 10 deletions nipype/interfaces/base/traits_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ def has_metadata(trait, metadata, value=None, recursive=True):
return count > 0


class MultiPath(traits.List):
""" Abstract class - shared functionality of input and output MultiPath
class MultiObject(traits.List):
""" Abstract class - shared functionality of input and output MultiObject
"""

def validate(self, object, name, value):
Expand All @@ -335,18 +335,18 @@ def validate(self, object, name, value):
inner_trait = self.inner_traits()[0]
if not isinstance(value, list) \
or (isinstance(inner_trait.trait_type, traits.List) and
not isinstance(inner_trait.trait_type, InputMultiPath) and
not isinstance(inner_trait.trait_type, InputMultiObject) and
not isinstance(value[0], list)):
newvalue = [value]
value = super(MultiPath, self).validate(object, name, newvalue)
value = super(MultiObject, self).validate(object, name, newvalue)

if value:
return value

self.error(object, name, value)


class OutputMultiPath(MultiPath):
class OutputMultiObject(MultiObject):
""" Implements a user friendly traits that accepts one or more
paths to files or directories. This is the output version which
return a single string whenever possible (when it was set to a
Expand All @@ -358,9 +358,9 @@ class OutputMultiPath(MultiPath):

XXX This needs to be vetted by somebody who understands traits

>>> from nipype.interfaces.base import OutputMultiPath, TraitedSpec
>>> from nipype.interfaces.base import OutputMultiObject, TraitedSpec
>>> class A(TraitedSpec):
... foo = OutputMultiPath(File(exists=False))
... foo = OutputMultiObject(File(exists=False))
>>> a = A()
>>> a.foo
<undefined>
Expand Down Expand Up @@ -392,7 +392,7 @@ def set(self, object, name, value):
self.set_value(object, name, value)


class InputMultiPath(MultiPath):
class InputMultiObject(MultiObject):
""" Implements a user friendly traits that accepts one or more
paths to files or directories. This is the input version which
always returns a list. Default value of this trait
Expand All @@ -403,9 +403,9 @@ class InputMultiPath(MultiPath):

XXX This needs to be vetted by somebody who understands traits

>>> from nipype.interfaces.base import InputMultiPath, TraitedSpec
>>> from nipype.interfaces.base import InputMultiObject, TraitedSpec
>>> class A(TraitedSpec):
... foo = InputMultiPath(File(exists=False))
... foo = InputMultiObject(File(exists=False))
>>> a = A()
>>> a.foo
<undefined>
Expand All @@ -424,3 +424,6 @@ class InputMultiPath(MultiPath):

"""
pass

InputMultiPath = InputMultiObject
OutputMultiPath = OutputMultiObject
6 changes: 3 additions & 3 deletions nipype/interfaces/fsl/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from nibabel import load

from ... import LooseVersion
from ...utils.filemanip import list_to_filename, filename_to_list
from ...utils.filemanip import simplify_list, ensure_list
from ...utils.misc import human_order_sorted
from ...external.due import BibTeX
from ..base import (File, traits, isdefined, TraitedSpec, BaseInterface,
Expand Down Expand Up @@ -493,7 +493,7 @@ def _get_design_root(self, infile):
def _list_outputs(self):
# TODO: figure out file names and get rid off the globs
outputs = self._outputs().get()
root = self._get_design_root(list_to_filename(self.inputs.fsf_file))
root = self._get_design_root(simplify_list(self.inputs.fsf_file))
design_file = glob(os.path.join(os.getcwd(), '%s*.mat' % root))
assert len(design_file) == 1, 'No mat file generated by FEAT Model'
outputs['design_file'] = design_file[0]
Expand Down Expand Up @@ -890,7 +890,7 @@ def _run_interface(self, runtime):
num_runs=num_runs,
regimage=self.inputs.reg_image,
regdof=self.inputs.reg_dof)
for i, rundir in enumerate(filename_to_list(self.inputs.feat_dirs)):
for i, rundir in enumerate(ensure_list(self.inputs.feat_dirs)):
fsf_txt += fsf_dirs.substitute(
runno=i + 1, rundir=os.path.abspath(rundir))
fsf_txt += fsf_footer.substitute()
Expand Down
4 changes: 2 additions & 2 deletions nipype/interfaces/fsl/tests/test_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import pytest
import pdb
from nipype.utils.filemanip import split_filename, filename_to_list
from nipype.utils.filemanip import split_filename, ensure_list
from .. import preprocess as fsl
from nipype.interfaces.fsl import Info
from nipype.interfaces.base import File, TraitError, Undefined, isdefined
Expand Down Expand Up @@ -164,7 +164,7 @@ def _run_and_test(opts, output_base):
outputs = fsl.FAST(**opts)._list_outputs()
for output in outputs.values():
if output:
for filename in filename_to_list(output):
for filename in ensure_list(output):
assert os.path.realpath(filename).startswith(
os.path.realpath(output_base))

Expand Down
34 changes: 17 additions & 17 deletions nipype/interfaces/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

from .. import config, logging
from ..utils.filemanip import (
copyfile, list_to_filename, filename_to_list,
copyfile, simplify_list, ensure_list,
get_related_files, related_filetype_sets)
from ..utils.misc import human_order_sorted, str2bool
from .base import (
Expand Down Expand Up @@ -721,7 +721,7 @@ def _list_outputs(self):
if not isdefined(files):
continue
iflogger.debug("key: %s files: %s", key, str(files))
files = filename_to_list(files)
files = ensure_list(files)
tempoutdir = outdir
if s3_flag:
s3tempoutdir = s3dir
Expand All @@ -738,7 +738,7 @@ def _list_outputs(self):
files = [item for sublist in files for item in sublist]

# Iterate through passed-in source files
for src in filename_to_list(files):
for src in ensure_list(files):
# Format src and dst files
src = os.path.abspath(src)
if not os.path.isfile(src):
Expand Down Expand Up @@ -938,7 +938,7 @@ def _list_outputs(self):
else:
if self.inputs.sort_filelist:
filelist = human_order_sorted(filelist)
outputs[key] = list_to_filename(filelist)
outputs[key] = simplify_list(filelist)
for argnum, arglist in enumerate(args):
maxlen = 1
for arg in arglist:
Expand Down Expand Up @@ -987,7 +987,7 @@ def _list_outputs(self):
else:
if self.inputs.sort_filelist:
outfiles = human_order_sorted(outfiles)
outputs[key].append(list_to_filename(outfiles))
outputs[key].append(simplify_list(outfiles))
if any([val is None for val in outputs[key]]):
outputs[key] = []
if len(outputs[key]) == 0:
Expand Down Expand Up @@ -1195,7 +1195,7 @@ def _list_outputs(self):
else:
if self.inputs.sort_filelist:
filelist = human_order_sorted(filelist)
outputs[key] = list_to_filename(filelist)
outputs[key] = simplify_list(filelist)
for argnum, arglist in enumerate(args):
maxlen = 1
for arg in arglist:
Expand Down Expand Up @@ -1241,7 +1241,7 @@ def _list_outputs(self):
else:
if self.inputs.sort_filelist:
outfiles = human_order_sorted(outfiles)
outputs[key].append(list_to_filename(outfiles))
outputs[key].append(simplify_list(outfiles))
if self.inputs.drop_blank_outputs:
outputs[key] = [x for x in outputs[key] if x is not None]
else:
Expand Down Expand Up @@ -1409,7 +1409,7 @@ def _list_outputs(self):

# Handle whether this must be a list or not
if field not in force_lists:
filelist = list_to_filename(filelist)
filelist = simplify_list(filelist)

outputs[field] = filelist

Expand Down Expand Up @@ -1750,7 +1750,7 @@ def _get_files(self, path, key, dirval, altkey=None):
globprefix = self.inputs.hemi + '.'
else:
globprefix = '*'
keys = filename_to_list(altkey) if altkey else [key]
keys = ensure_list(altkey) if altkey else [key]
globfmt = os.path.join(path, dirval, ''.join((globprefix, '{}',
globsuffix)))
return [
Expand All @@ -1768,7 +1768,7 @@ def _list_outputs(self):
output_traits.traits()[k].loc,
output_traits.traits()[k].altkey)
if val:
outputs[k] = list_to_filename(val)
outputs[k] = simplify_list(val)
return outputs


Expand Down Expand Up @@ -1909,7 +1909,7 @@ def _list_outputs(self):
file_objects = xnat.select(template).get('obj')
if file_objects == []:
raise IOError('Template %s returned no files' % template)
outputs[key] = list_to_filename([
outputs[key] = simplify_list([
str(file_object.get()) for file_object in file_objects
if file_object.exists()
])
Expand Down Expand Up @@ -1944,7 +1944,7 @@ def _list_outputs(self):
raise IOError('Template %s '
'returned no files' % target)

outfiles = list_to_filename([
outfiles = simplify_list([
str(file_object.get())
for file_object in file_objects
if file_object.exists()
Expand All @@ -1956,7 +1956,7 @@ def _list_outputs(self):
raise IOError('Template %s '
'returned no files' % template)

outfiles = list_to_filename([
outfiles = simplify_list([
str(file_object.get())
for file_object in file_objects
if file_object.exists()
Expand Down Expand Up @@ -2079,7 +2079,7 @@ def _list_outputs(self):
# gather outputs and upload them
for key, files in list(self.inputs._outputs.items()):

for name in filename_to_list(files):
for name in ensure_list(files):

if isinstance(name, list):
for i, file_name in enumerate(name):
Expand Down Expand Up @@ -2205,7 +2205,7 @@ def __init__(self, input_names, **inputs):

super(SQLiteSink, self).__init__(**inputs)

self._input_names = filename_to_list(input_names)
self._input_names = ensure_list(input_names)
add_traits(self.inputs, [name for name in self._input_names])

def _list_outputs(self):
Expand Down Expand Up @@ -2263,7 +2263,7 @@ def __init__(self, input_names, **inputs):

super(MySQLSink, self).__init__(**inputs)

self._input_names = filename_to_list(input_names)
self._input_names = ensure_list(input_names)
add_traits(self.inputs, [name for name in self._input_names])

def _list_outputs(self):
Expand Down Expand Up @@ -2463,7 +2463,7 @@ def _get_files_over_ssh(self, template):
iflogger.info('remote file %s not found' % f)

# return value
outfiles = list_to_filename(outfiles)
outfiles = simplify_list(outfiles)

return outfiles

Expand Down
4 changes: 2 additions & 2 deletions nipype/interfaces/mne/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import glob

from ... import logging
from ...utils.filemanip import list_to_filename
from ...utils.filemanip import simplify_list
from ..base import (traits, File, Directory, TraitedSpec, OutputMultiPath)
from ..freesurfer.base import FSCommand, FSTraitedSpec

Expand Down Expand Up @@ -120,7 +120,7 @@ def _list_outputs(self):
output_traits.traits()[k].loc,
output_traits.traits()[k].altkey)
if val:
value_list = list_to_filename(val)
value_list = simplify_list(val)
if isinstance(value_list, list):
out_files = []
for value in value_list:
Expand Down
Loading