@@ -21,15 +21,15 @@ grouped into separate classes (usually suffixed with InputSpec and OutputSpec).
21
21
For example:
22
22
23
23
.. testcode ::
24
-
24
+
25
25
class ExampleInputSpec(TraitedSpec):
26
26
input_volume = File(desc = "Input volume", exists = True,
27
27
mandatory = True)
28
28
parameter = traits.Int(desc = "some parameter")
29
-
29
+
30
30
class ExampleOutputSpec(TraitedSpec):
31
31
output_volume = File(desc = "Output volume", exists = True)
32
-
32
+
33
33
For the Traits (and Nipype) to work correctly output and input spec has to be
34
34
inherited from TraitedSpec (however, this does not have to be direct
35
35
inheritance).
@@ -39,7 +39,7 @@ above example we have used the ``desc`` metadata which holds human readable
39
39
description of the input. The ``mandatory `` flag forces Nipype to throw an
40
40
exception if the input was not set. ``exists `` is a special flag that works only
41
41
for ``File traits `` and checks if the provided file exists. More details can be
42
- found at `interface_specs `_ .
42
+ found at :doc: `interface_specs `.
43
43
44
44
The input and output specifications have to be connected to the our example
45
45
interface class:
@@ -49,13 +49,13 @@ interface class:
49
49
class Example(Interface):
50
50
input_spec = ExampleInputSpec
51
51
output_spec = ExampleOutputSpec
52
-
52
+
53
53
Where the names of the classes grouping inputs and outputs were arbitrary the
54
54
names of the fields within the interface they are assigned are not (it always
55
55
has to be input_spec and output_spec). Of course this interface does not do much
56
56
because we have not specified how to process the inputs and create the outputs.
57
57
This can be done in many ways.
58
-
58
+
59
59
Command line executable
60
60
=======================
61
61
@@ -67,14 +67,14 @@ between the inputs and the generated command line. To achieve this we have
67
67
added two metadata: ``argstr `` (string defining how the argument should be
68
68
formated) and ``position `` (number defining the order of the arguments).
69
69
For example
70
-
70
+
71
71
.. testcode ::
72
72
73
73
class ExampleInputSpec(CommandLineSpec):
74
74
input_volume = File(desc = "Input volume", exists = True,
75
75
mandatory = True, position = 0, argstr="%s")
76
76
parameter = traits.Int(desc = "some parameter", argstr = "--param %d")
77
-
77
+
78
78
As you probably noticed the ``argstr `` is a printf type string with formatting
79
79
symbols. For an input defined in InputSpec to be included into the executed
80
80
commandline ``argstr `` has to be included. Additionally inside the main
@@ -88,11 +88,11 @@ to the ``_cmd`` field. Also the main interface class needs to inherit from
88
88
_cmd = 'my_command'
89
89
input_spec = ExampleInputSpec
90
90
output_spec = ExampleOutputSpec
91
-
91
+
92
92
There is one more thing we need to take care of. When the executable finishes
93
93
processing it will presumably create some output files. We need to know which
94
94
files to look for, check if they exist and expose them to whatever node would
95
- like to use them. This is done by implementing `_list_outputs `_ method in the
95
+ like to use them. This is done by implementing `` _list_outputs `` method in the
96
96
main interface class. Basically what it does is assigning the expected output
97
97
files to the fields of our output spec:
98
98
@@ -102,7 +102,7 @@ files to the fields of our output spec:
102
102
outputs = self.output_spec().get()
103
103
outputs['output_volume'] = os.path.abspath('name_of_the_file_this_cmd_made.nii')
104
104
return outputs
105
-
105
+
106
106
Sometimes the inputs need extra parsing before turning into command line
107
107
parameters. For example imagine a parameter selecting between three methods:
108
108
"old", "standard" and "new". Imagine also that the command line accept this as
@@ -122,42 +122,42 @@ numbers. We need to do additional parsing by overloading the following method
122
122
in the main interface class:
123
123
124
124
.. testcode ::
125
-
125
+
126
126
def _format_arg(self, name, spec, value):
127
127
if name == 'method':
128
128
return spec.argstr%{"old":0, "standard":1, "new":2}[value]
129
129
return super(Example, self)._format_arg(name, spec, value)
130
-
130
+
131
131
Here is a minimalistic interface for the gzip command:
132
132
133
133
.. testcode ::
134
-
134
+
135
135
from nipype.interfaces.base import (
136
- TraitedSpec,
136
+ TraitedSpec,
137
137
CommandLineInputSpec,
138
- CommandLine,
138
+ CommandLine,
139
139
File
140
140
)
141
141
import os
142
-
142
+
143
143
class GZipInputSpec(CommandLineInputSpec):
144
144
input_file = File(desc="File", exists=True, mandatory=True, argstr="%s")
145
-
145
+
146
146
class GZipOutputSpec(TraitedSpec):
147
147
output_file = File(desc = "Zip file", exists = True)
148
-
148
+
149
149
class GZipTask(CommandLine):
150
150
input_spec = GZipInputSpec
151
151
output_spec = GZipOutputSpec
152
152
cmd = 'gzip'
153
-
153
+
154
154
def _list_outputs(self):
155
155
outputs = self.output_spec().get()
156
156
outputs['output_file'] = os.path.abspath(self.inputs.input_file + ".gz")
157
157
return outputs
158
-
158
+
159
159
if __name__ == '__main__':
160
-
160
+
161
161
zipper = GZipTask(input_file='an_existing_file')
162
162
print zipper.cmdline
163
163
zipper.run()
@@ -193,9 +193,9 @@ hash_files
193
193
194
194
name_template (optional)
195
195
overrides the default ``_generated `` suffix
196
-
196
+
197
197
output_name (optional)
198
- name of the output (if this is not set same name as the input will be
198
+ name of the output (if this is not set same name as the input will be
199
199
assumed)
200
200
201
201
keep_extension (optional - not used)
0 commit comments