nipype.interfaces.base.traits_extension module

Traits extension

This module contains Trait classes that we’ve pulled from the traits source and fixed due to various bugs. File and Directory are redefined as the release version had dependencies on TraitsUI, which we do not want Nipype to depend on. At least not yet.

Undefined class was missing the __len__ operator, causing edit_traits and configure_traits to fail on List objects. Even though we don’t require TraitsUI, this bug was the only thing preventing us from popping up GUIs which users like.

These bugs have been in Traits v3.3.0 and v3.2.1. We have reported all of these bugs and they’ve been fixed in enthought svn repository (usually by Robert Kern).

class nipype.interfaces.base.traits_extension.BasePath(value=<undefined>, exists=False, resolve=False, **metadata)

Bases: traits.trait_type.TraitType

Defines a trait whose value must be a valid filesystem path.

exists = False
property info_text

Create the trait’s general description.

resolve = False
validate(objekt, name, value, return_pathlike=False)

Validate a value change.

class nipype.interfaces.base.traits_extension.Directory(value=<undefined>, exists=False, resolve=False, **metadata)

Bases: nipype.interfaces.base.traits_extension.BasePath

Defines a trait whose value must be a directory path.

>>> from nipype.interfaces.base import Directory, TraitedSpec, TraitError
>>> class A(TraitedSpec):
...     foo = Directory(exists=False)
>>> a = A()
>>> a.foo
<undefined>
>>> a.foo = '/some/made/out/path'
>>> a.foo
'/some/made/out/path'
>>> class A(TraitedSpec):
...     foo = Directory(exists=False, resolve=True)
>>> a = A(foo='relative_dir')
>>> a.foo  
'.../relative_dir'
>>> class A(TraitedSpec):
...     foo = Directory(exists=True, resolve=True)
>>> a = A()
>>> a.foo = 'relative_dir'  
Traceback (most recent call last):
TraitError:
>>> from os import mkdir
>>> mkdir('relative_dir')
>>> a.foo = 'relative_dir'
>>> a.foo  
'.../relative_dir'
>>> class A(TraitedSpec):
...     foo = Directory(exists=True, resolve=False)
>>> a = A(foo='relative_dir')
>>> a.foo
'relative_dir'
>>> class A(TraitedSpec):
...     foo = Directory('tmpdir')
>>> a = A()
>>> a.foo  
<undefined>
>>> class A(TraitedSpec):
...     foo = Directory('tmpdir', usedefault=True)
>>> a = A()
>>> a.foo  
'tmpdir'
class nipype.interfaces.base.traits_extension.File(value=<traits.trait_type._NoDefaultSpecifiedType object>, exists=False, resolve=False, allow_compressed=True, extensions=None, **metadata)

Bases: nipype.interfaces.base.traits_extension.BasePath

Defines a trait whose value must be a file path.

>>> from nipype.interfaces.base import File, TraitedSpec, TraitError
>>> class A(TraitedSpec):
...     foo = File()
>>> a = A()
>>> a.foo
<undefined>
>>> a.foo = '/some/made/out/path/to/file'
>>> a.foo
'/some/made/out/path/to/file'
>>> class A(TraitedSpec):
...     foo = File(exists=False, resolve=True)
>>> a = A(foo='idontexist.txt')
>>> a.foo  
'.../idontexist.txt'
>>> class A(TraitedSpec):
...     foo = File(exists=True, resolve=True)
>>> a = A()
>>> a.foo = 'idontexist.txt'  
Traceback (most recent call last):
TraitError:
>>> open('idoexist.txt', 'w').close()
>>> a.foo = 'idoexist.txt'
>>> a.foo  
'.../idoexist.txt'
>>> class A(TraitedSpec):
...     foo = File('idoexist.txt')
>>> a = A()
>>> a.foo
<undefined>
>>> class A(TraitedSpec):
...     foo = File('idoexist.txt', usedefault=True)
>>> a = A()
>>> a.foo
'idoexist.txt'
>>> class A(TraitedSpec):
...     foo = File(exists=True, resolve=True, extensions=['.txt', 'txt.gz'])
>>> a = A()
>>> a.foo = 'idoexist.badtxt'  
Traceback (most recent call last):
TraitError:
>>> a.foo = 'idoexist.txt'
>>> a.foo  
'.../idoexist.txt'
>>> class A(TraitedSpec):
...     foo = File(extensions=['.nii', '.nii.gz'])
>>> a = A()
>>> a.foo = 'badext.txt'  
Traceback (most recent call last):
TraitError:
>>> class A(TraitedSpec):
...     foo = File(extensions=['.nii', '.nii.gz'])
>>> a = A()
>>> a.foo = 'goodext.nii'
>>> a.foo
'goodext.nii'
>>> a = A()
>>> a.foo = 'idontexist.000.nii'
>>> a.foo  
'idontexist.000.nii'
>>> a = A()
>>> a.foo = 'idontexist.000.nii.gz'
>>> a.foo  
'idontexist.000.nii.gz'
validate(objekt, name, value, return_pathlike=False)

Validate a value change.

nipype.interfaces.base.traits_extension.IMG_ZIP_FMT = {'.gii.gz', '.mgh.gz', '.mgz', '.nii.gz', 'img.gz', 'tar.gz'}

The functions that pop-up the Traits GUIs, edit_traits and configure_traits, were failing because all of our inputs default to Undefined deep and down in traits/ui/wx/list_editor.py it checks for the len() of the elements of the list. The _Undefined class in traits does not define the __len__ method and would error. I tried defining our own Undefined and even sublassing Undefined, but both of those failed with a TraitError in our initializer when we assign the Undefined to the inputs because of an incompatible type:

TraitError: The ‘vertical_gradient’ trait of a BetInputSpec instance must be a float, but a value of <undefined> <class ‘nipype.interfaces.traits._Undefined’> was specified.

So… in order to keep the same type but add the missing method, I monkey patched.

class nipype.interfaces.base.traits_extension.ImageFile(value=<traits.trait_type._NoDefaultSpecifiedType object>, exists=False, resolve=False, types=None, **metadata)

Bases: nipype.interfaces.base.traits_extension.File

Defines a trait whose value must be a known neuroimaging file.

class nipype.interfaces.base.traits_extension.InputMultiObject(trait=None, value=None, minlen=0, maxlen=9223372036854775807, items=True, **metadata)

Bases: nipype.interfaces.base.traits_extension.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 is _Undefined. It does not accept empty lists.

XXX This should only be used as a final resort. We should stick to established Traits to the extent possible.

XXX This needs to be vetted by somebody who understands traits

>>> from nipype.interfaces.base import InputMultiObject, TraitedSpec
>>> class A(TraitedSpec):
...     foo = InputMultiObject(File(exists=False))
>>> a = A()
>>> a.foo
<undefined>
>>> a.foo = '/software/temp/foo.txt'
>>> a.foo
['/software/temp/foo.txt']
>>> a.foo = ['/software/temp/foo.txt']
>>> a.foo
['/software/temp/foo.txt']
>>> a.foo = ['/software/temp/foo.txt', '/software/temp/goo.txt']
>>> a.foo
['/software/temp/foo.txt', '/software/temp/goo.txt']
nipype.interfaces.base.traits_extension.InputMultiPath

alias of InputMultiObject

class nipype.interfaces.base.traits_extension.MultiObject(trait=None, value=None, minlen=0, maxlen=9223372036854775807, items=True, **metadata)

Bases: traits.trait_types.List

Abstract class - shared functionality of input and output MultiObject

validate(objekt, name, value)

Validates that the values is a valid list.

Note

object can be None when validating a default value (see e.g. clone())

class nipype.interfaces.base.traits_extension.OutputMultiObject(trait=None, value=None, minlen=0, maxlen=9223372036854775807, items=True, **metadata)

Bases: nipype.interfaces.base.traits_extension.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 single value or a list of length 1). Default value of this trait is _Undefined. It does not accept empty lists.

XXX This should only be used as a final resort. We should stick to established Traits to the extent possible.

XXX This needs to be vetted by somebody who understands traits

>>> from nipype.interfaces.base import OutputMultiObject, TraitedSpec
>>> class A(TraitedSpec):
...     foo = OutputMultiObject(File(exists=False))
>>> a = A()
>>> a.foo
<undefined>
>>> a.foo = '/software/temp/foo.txt'
>>> a.foo
'/software/temp/foo.txt'
>>> a.foo = ['/software/temp/foo.txt']
>>> a.foo
'/software/temp/foo.txt'
>>> a.foo = ['/software/temp/foo.txt', '/software/temp/goo.txt']
>>> a.foo
['/software/temp/foo.txt', '/software/temp/goo.txt']
get(objekt, name)
set(objekt, name, value)
nipype.interfaces.base.traits_extension.OutputMultiPath

alias of OutputMultiObject

class nipype.interfaces.base.traits_extension.Str(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)

Bases: traits.trait_types.Str

Replaces the default traits.Str based in bytes.

nipype.interfaces.base.traits_extension.has_metadata(trait, metadata, value=None, recursive=True)

Checks if a given trait has a metadata (and optionally if it is set to particular value)

nipype.interfaces.base.traits_extension.isdefined(objekt)
nipype.interfaces.base.traits_extension.rebase_path_traits(thistrait, value, cwd)

Rebase a BasePath-derived trait given an interface spec.

nipype.interfaces.base.traits_extension.resolve_path_traits(thistrait, value, cwd)

Resolve a BasePath-derived trait given an interface spec.