1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
|
========================
Interface Specifications
========================
Before you start
----------------
Nipype is a young project maintained by an enthusiastic group of developers. Even though the documentation might be sparse or cryptic at times we strongly encourage you to contact us on the official nipype developers mailing list in case of any troubles: nipy-devel@neuroimaging.scipy.org (we are sharing a mailing list with the nipy community therefore please add ``[nipype]`` to the messsage title).
Overview
--------
We're using the `Enthought Traits
<http://code.enthought.com/projects/traits/>`_ package for all of our
inputs and outputs. Traits allows us to validate user inputs and
provides a mechanism to handle all the *special cases* in a simple and
concise way though metadata. With the metadata, each input/output can
have an optional set of metadata attributes (described in more detail
below). The machinery for handling the metadata is located in the
base classes, so all subclasses use the same code to handle these
cases. This is in contrast to our previous code where every class
defined it's own _parse_inputs, run and aggregate_outputs methods to
handle these cases. Which of course leads to a dozen different ways
to solve the same problem.
Traits is a big package with a lot to learn in order to take full
advantage of. But don't be intimidated! To write a Nipype Trait
Specification, you only need to learn a few of the basics of Traits.
Here are a few starting points in the documentation:
* What are Traits? The `Introduction in the User Manual
<http://code.enthought.com/projects/traits/docs/html/traits_user_manual/intro.html>`_
gives a brief description of the functionality traits provides.
* Traits and metadata. The `second section of the User Manual
<http://code.enthought.com/projects/traits/docs/html/traits_user_manual/defining.html>`_
gives more details on traits and how to use them. Plus there a
section describing metadata, including the metadata all traits have.
* If your interested in more of a *big picture* overview, `Gael wrote
a good tutorial
<http://code.enthought.com/projects/traits/docs/html/tutorials/traits_ui_scientific_app.html>`_
that shows how to write a scientific application using traits for
the benefit of the generated UI components. (For now, Nipype is not
taking advantage of the generated UI feature of traits.)
Traits version
^^^^^^^^^^^^^^
We're using Traits version 3.x which can be install as part of `EPD
<http://enthought.com/products/epd.php>`_ or from `pypi
<http://pypi.python.org/pypi/Traits/3.3.0>`_
More documentation
^^^^^^^^^^^^^^^^^^
Not everything is documented in the User Manual, in those cases the
`enthought-dev mailing list
<https://mail.enthought.com/mailman/listinfo/enthought-dev>`_ or the
`API docs
<http://code.enthought.com/projects/files/ETS32_API/enthought.traits.html>`_
is your next place to look.
Nipype Interface Specifications
-------------------------------
Each interface class defines two specifications: 1) an InputSpec and
2) an OutputSpec. Each of these are prefixed with the class name of
the interfaces. For example, Bet has these specs:
- BETInputSpec
- BETOutputSpec
Each of these Specs are classes, derived from a base TraitedSpec class
(more on these below). The InputSpec consists of attributes which
correspond to different parameters for the tool they wrap/interface.
In the case of a command-line tool like Bet, the InputSpec attributes
correspond to the different command-line parameters that can be passed
to Bet. If you are familiar with the Nipype 0.2 code-base, these
attributes are the same as the keys in the opt_map dictionaries. When
an interfaces class is instantiated, the InputSpec is bound to the
``inputs`` attribute of that object. Below is an example of how the
``inputs`` appear to a user for Bet::
>>> from nipype.interfaces import fsl
>>> bet = fsl.BET()
>>> type(bet.inputs)
<class 'nipype.interfaces.fsl.preprocess.BETInputSpec'>
>>> bet.inputs.<TAB>
bet.inputs.__class__ bet.inputs.center
bet.inputs.__delattr__ bet.inputs.environ
bet.inputs.__doc__ bet.inputs.frac
bet.inputs.__getattribute__ bet.inputs.functional
bet.inputs.__hash__ bet.inputs.hashval
bet.inputs.__init__ bet.inputs.infile
bet.inputs.__new__ bet.inputs.items
bet.inputs.__reduce__ bet.inputs.mask
bet.inputs.__reduce_ex__ bet.inputs.mesh
bet.inputs.__repr__ bet.inputs.nooutput
bet.inputs.__setattr__ bet.inputs.outfile
bet.inputs.__str__ bet.inputs.outline
bet.inputs._generate_handlers bet.inputs.outputtype
bet.inputs._get_hashval bet.inputs.radius
bet.inputs._hash_infile bet.inputs.reduce_bias
bet.inputs._xor_inputs bet.inputs.skull
bet.inputs._xor_warn bet.inputs.threshold
bet.inputs.args bet.inputs.vertical_gradient
Each Spec inherits from a parent Spec. The parent Specs provide
attribute(s) that are common to all child classes. For example, FSL
InputSpecs inherit from interfaces.fsl.base.FSLTraitedSpec.
FSLTraitedSpec defines an ``outputtype`` attribute, which stores the
file type (NIFTI, NIFTI_PAIR, etc...) for all generated output files.
InputSpec class hierarchy
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Below is the current class hierarchy for InputSpec classes (from
base class down to subclasses).:
``TraitedSpec``: Nipype's primary base class for all Specs.
Provides initialization, some nipype-specific methods and any trait
handlers we define. Inherits from traits.HasTraits.
``BaseInterfaceInputSpec``: Defines inputs common to all
Interfaces (``ignore_exception``). If in doubt inherit from this.
``CommandLineInputSpec``: Defines inputs common to all
command-line classes (``args`` and ``environ``)
``FSLTraitedSpec``: Defines inputs common to all FSL classes
(``outputtype``)
``SPMCommandInputSpec``: Defines inputs common to all SPM classes (``matlab_cmd``, ``path``, and ``mfile``)
``FSTraitedSpec``: Defines inputs common to all FreeSurfer classes
(``sbjects_dir``)
``MatlabInputSpec``: Defines inputs common to all Matlab classes (``script``, ``nodesktop``, ``nosplash``, ``logfile``, ``single_comp_thread``, ``mfile``, ``script_file``, and ``paths``)
``SlicerCommandLineInputSpec``: Defines inputs common to all Slicer classes (``module``)
Most developers will only need to code at the the interface-level (i.e. implementing custom class inheriting from one of the above classes).
Output Specs
^^^^^^^^^^^^
The OutputSpec defines the outputs that are generated, or possibly
generated depending on inputs, by the tool. OutputSpecs inherit from
``interfaces.base.TraitedSpec`` directly.
Traited Attributes
------------------
Each specification attribute is an instance of a Trait class. These
classes encapsulate many standard Python types like Float and Int, but
with additional behavior like type checking. (*See the documentation
on traits for more information on these trait types.*) To handle
unique behaviors of our attributes we us traits metadata. These are
keyword arguments supplied in the initialization of the attributes.
The base classes ``BaseInterface`` and ``CommandLine``
(defined in ``nipype.interfaces.base``) check for the existence/or
value of these metadata and handle the inputs/outputs accordingly.
For example, all mandatory parameters will have the ``mandatory =
True`` metadata::
class BetInputSpec(FSLTraitedSpec):
infile = File(exists=True,
desc = 'input file to skull strip',
argstr='%s', position=0, mandatory=True)
Common
^^^^^^
``exists``
For files, use ``nipype.interfaces.base.File`` as the trait type. If
the file must exist for the tool to execute, specify ``exists = True``
in the initialization of File (as shown in BetInputSpec above). This
will trigger the underlying traits code to confirm the file assigned
to that *input* actually exists. If it does not exist, the user will
be presented with an error message::
>>> bet.inputs.infile = 'does_not_exist.nii'
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
File "/Users/cburns/local/lib/python2.5/site-packages/nipype/interfaces/base.py", line 76, in validate
self.error( object, name, value )
File "/Users/cburns/local/lib/python2.5/site-packages/enthought/traits/trait_handlers.py", line 175, in error
value )
TraitError: The 'infile' trait of a BetInputSpec instance must be a file
name, but a value of 'does_not_exist.nii' <type 'str'> was specified.
``desc``
All trait objects have a set of default metadata attributes. ``desc``
is one of those and is used as a simple, one-line docstring. The
``desc`` is printed when users use the ``help()`` methods.
**Required:** This metadata is required by all nipype interface
classes.
``usedefault``
Set this metadata to True when the *default value* for the trait type
of this attribute is an acceptable value. All trait objects have a
default value, ``traits.Int`` has a default of ``0``, ``traits.Float``
has a default of ``0.0``, etc... You can also define a default value
when you define the class. For example, in the code below all objects
of ``Foo`` will have a default value of 12 for ``x``::
>>> import enthought.traits.api as traits
>>> class Foo(traits.HasTraits):
... x = traits.Int(12)
... y = traits.Int
...
>>> foo = Foo()
>>> foo.x
12
>>> foo.y
0
Nipype only passes ``inputs`` on to the underlying package if they
have been defined (more on this later). So if you specify
``usedefault = True``, you are telling the parser to pass the default
value on to the underlying package. Let's look at the InputSpec for
SPM Realign::
class RealignInputSpec(BaseInterfaceInputSpec):
jobtype = traits.Enum('estwrite', 'estimate', 'write',
desc='one of: estimate, write, estwrite',
usedefault=True)
Here we've defined ``jobtype`` to be an enumerated trait type,
``Enum``, which can be set to one of the following: ``estwrite``,
``estimate``, or ``write``. In a container, the default is always the
first element. So in this case, the default will be ``estwrite``::
>>> from nipype.interfaces import spm
>>> rlgn = spm.Realign()
>>> rlgn.inputs.infile
<undefined>
>>> rlgn.inputs.jobtype
'estwrite'
``xor`` and ``requires``
Both of these accept a list of trait names. The ``xor`` metadata reflects
mutually exclusive traits, while the requires metadata reflects traits
that have to be set together. When a xor-ed trait is set, all other
traits belonging to the list are set to Undefined. The function
check_mandatory_inputs ensures that all requirements (both mandatory and
via the requires metadata are satisfied). These are also reflected in
the help function.
``copyfile``
This is metadata for a File or Directory trait that is relevant only in
the context of wrapping an interface in a `Node` and `MapNode`. `copyfile`
can be set to either `True` or `False`. `False` indicates that contents
should be symlinked, while `True` indicates that the contents should be
copied over.
CommandLine
^^^^^^^^^^^
``argstr``
The metadata keyword for specifying the format strings
for the parameters. This was the *value* string in the opt_map
dictionaries of Nipype 0.2 code. If we look at the
``FlirtInputSpec``, the ``argstr`` for the reference file corresponds
to the argument string I would need to provide with the command-line
version of ``flirt``::
class FlirtInputSpec(FSLTraitedSpec):
reference = File(exists = True, argstr = '-ref %s', mandatory = True,
position = 1, desc = 'reference file')
**Required:** This metadata is required by all command-line interface classes.
``position``
This metadata is used to specify the position of arguments. Both
positive and negative values are accepted. ``position = 0`` will
position this argument as the first parameter after the command
name. ``position = -1`` will position this argument as the last
parameter, after all other parameters.
``genfile``
If True, the ``genfile`` metadata specifies that a filename should be
generated for this parameter *if-and-only-if* the user did not provide
one. The nipype convention is to automatically generate output
filenames when not specified by the user both as a convenience for the
user and so the pipeline can easily gather the outputs. Requires
``_gen_filename()`` method to be implemented. This way should be used if the
desired file name is dependent on some runtime variables (such as file name
of one of the inputs, or current working directory). In case when it should
be fixed it's recommended to just use ``usedefault``.
``sep``
For List traits the string with witch elements of the list will be joined.
SPM
^^^
``field``
name of the structure refered by the SPM job manager
**Required:** This metadata is required by all SPM-mediated
interface classes.
Defining an interface class
---------------------------
Common
^^^^^^
When you define an interface class, you will define these attributes
and methods:
* ``input_spec``: the InputSpec
* ``output_spec``: the OutputSpec
* ``_list_outputs()``: Returns a dictionary containing names of generated files that are expected after package completes execution. This is used by ``BaseInterface.aggregate_outputs`` to gather all output files for the pipeline.
CommandLine
^^^^^^^^^^^
For command-line interfaces:
* ``_cmd``: the command-line command
If you used genfile:
* ``_gen_filename(name)``: Generate filename, used for filenames that nipype generates as a convenience for users. This is for parameters that are required by the wrapped package, but we're generating from some other parameter. For example, ``BET.inputs.outfile`` is required by BET but we can generate the name from ``BET.inputs.infile``. Override this method in subclass to handle.
And optionally:
* ``_format_arg(name, spec, value)``: For extra formatting of the input values before passing them to generic ``_parse_inputs()`` method.
For example this is the class definition for Flirt, minus the docstring::
class Flirt(NEW_FSLCommand):
_cmd = 'flirt'
input_spec = FlirtInputSpec
output_spec = FlirtOutputSpec
def _list_outputs(self):
outputs = self.output_spec().get()
outputs['outfile'] = self.inputs.outfile
# Generate an outfile if one is not provided
if not isdefined(outputs['outfile']) and isdefined(self.inputs.infile):
outputs['outfile'] = self._gen_fname(self.inputs.infile,
suffix = '_flirt')
outputs['outmatrix'] = self.inputs.outmatrix
# Generate an outmatrix file if one is not provided
if not isdefined(outputs['outmatrix']) and \
isdefined(self.inputs.infile):
outputs['outmatrix'] = self._gen_fname(self.inputs.infile,
suffix = '_flirt.mat',
change_ext = False)
return outputs
def _gen_filename(self, name):
if name in ('outfile', 'outmatrix'):
return self._list_outputs()[name]
else:
return None
There are two possible output files ``outfile`` and ``outmatrix``,
both of which can be generated if not specified by the user.
Also notice the use of ``self._gen_fname()`` - a FSLCommand helper method for generating filenames (with extensions conforming with FSLOUTPUTTYPE).
See also :doc:`cmd_interface_devel`.
SPM
^^^
For SPM-mediated interfaces:
* ``_jobtype`` and ``_jobname``: special names used used by the SPM job manager. You can find them by saving your batch job as an .m file and looking up the code.
And optionally:
* ``_format_arg(name, spec, value)``: For extra formatting of the input values before passing them to generic ``_parse_inputs()`` method.
Matlab
^^^^^^
See :doc:`matlab_interface_devel`.
Python
^^^^^^
See :doc:`python_interface_devel`.
Undefined inputs
----------------
All the inputs and outputs that were not explicitly set (And do not have a usedefault flag - see above) will have Undefined value. To check if something is defined you have to explicitly call ``isdefiend`` function (comparing to None will not work).
Example of inputs
-----------------
Below we have an example of using Bet. We can see from the help which
inputs are mandatory and which are optional, along with the one-line
description provided by the ``desc`` metadata::
>>> from nipype.interfaces import fsl
>>> fsl.BET.help()
Inputs
------
Mandatory:
infile: input file to skull strip
Optional:
args: Additional parameters to the command
center: center of gravity in voxels
environ: Environment variables (default={})
frac: fractional intensity threshold
functional: apply to 4D fMRI data
mask: create binary mask image
mesh: generate a vtk mesh brain surface
nooutput: Don't generate segmented output
outfile: name of output skull stripped image
outline: create surface outline image
outputtype: None
radius: head radius
reduce_bias: bias field and neck cleanup
skull: create skull image
threshold: apply thresholding to segmented brain image and mask
vertical_gradient: vertical gradient in fractional intensity threshold (-1, 1)
Outputs
-------
maskfile: path/name of binary brain mask (if generated)
meshfile: path/name of vtk mesh file (if generated)
outfile: path/name of skullstripped file
outlinefile: path/name of outline file (if generated)
Here we create a bet object and specify the required input. We then
check our inputs to see which are defined and which are not::
>>> bet = fsl.BET(infile = 'f3.nii')
>>> bet.inputs
args = <undefined>
center = <undefined>
environ = {'FSLOUTPUTTYPE': 'NIFTI_GZ'}
frac = <undefined>
functional = <undefined>
infile = f3.nii
mask = <undefined>
mesh = <undefined>
nooutput = <undefined>
outfile = <undefined>
outline = <undefined>
outputtype = NIFTI_GZ
radius = <undefined>
reduce_bias = <undefined>
skull = <undefined>
threshold = <undefined>
vertical_gradient = <undefined>
>>> bet.cmdline
'bet f3.nii /Users/cburns/data/nipype/s1/f3_brain.nii.gz'
We also checked the command-line that will be generated when we run
the command and can see the generated output filename
``f3_brain.nii.gz``.
|