Skip to content

Commit dbf58c2

Browse files
author
Mathieu Dubois
committed
DOC updates in users/tutorial_101: move code in external file (and update it), mention that nodes need absolut file path correct typos and add download section.
1 parent 32c4409 commit dbf58c2

File tree

2 files changed

+91
-45
lines changed

2 files changed

+91
-45
lines changed

doc/users/tutorial_101.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# -*- coding: utf-8 -*-
2+
import os
3+
import nipype.interfaces.spm as spm # the spm interfaces
4+
import nipype.pipeline.engine as pe # the workflow and node wrappers
5+
6+
import nipype.interfaces.matlab as mlab # how to run matlab
7+
# Path to matlab
8+
mlab.MatlabCommand.set_default_matlab_cmd("/full/path/to/matlab_exe")
9+
# Add SPM to MATLAB path if not present
10+
mlab.MatlabCommand.set_default_paths("/full/path/to/spm")
11+
12+
#
13+
# Define nodes
14+
#
15+
16+
realigner = pe.Node(interface=spm.Realign(), name='realign')
17+
realigner.inputs.in_files = os.abspath('somefuncrun.nii')
18+
realigner.inputs.register_to_mean = True
19+
20+
smoother = pe.Node(interface=spm.Smooth(fwhm=6), name='smooth')
21+
22+
#
23+
# Creating and configuring a workflow
24+
#
25+
workflow = pe.Workflow(name='preproc')
26+
workflow.base_dir = '.'
27+
28+
#
29+
# Connecting nodes to each other
30+
#
31+
workflow.connect(realigner, 'realigned_files', smoother, 'in_files')
32+
33+
34+
#
35+
# Visualizing the workflow
36+
#
37+
workflow.write_graph()
38+
39+
#
40+
# Extend it
41+
#
42+
import nipype.algorithms.rapidart as ra
43+
artdetect = pe.Node(interface=ra.ArtifactDetect(), name='artdetect')
44+
artdetect.inputs.use_differences = [True, False]
45+
artdetect.inputs.use_norm = True
46+
artdetect.inputs.norm_threshold = 0.5
47+
artdetect.inputs.zintensity_threshold = 3
48+
artdetect.inputs.parameter_source = 'SPM'
49+
artdetect.inputs.mask_type = 'spm_global'
50+
workflow.connect([(realigner, artdetect,
51+
[('realigned_files', 'realigned_files'),
52+
('realignment_parameters', 'realignment_parameters')]
53+
)])
54+
workflow.write_graph()
55+
56+
#
57+
# Execute the workflow
58+
#
59+
workflow.run()

doc/users/tutorial_101.rst

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,24 @@ setting up a workflow is separate from executing it.
2525

2626
**1. Import appropriate modules**
2727

28-
.. testcode::
29-
30-
import nipype.interfaces.spm as spm # the spm interfaces
31-
import nipype.pipeline.engine as pe # the workflow and node wrappers
28+
.. literalinclude:: tutorial_101.py
29+
:lines: 2-4
3230

3331
**2. Define nodes**
3432

3533
Here we take instances of interfaces and make them pipeline compatible by
3634
wrapping them with pipeline specific elements. To determine the inputs and outputs
3735
of a given interface, please see :ref:`interface_tutorial`. Let's
38-
start with defining a realign node using the interface
39-
:class:`nipype.interfaces.spm.Realign`
36+
start with defining a realign node using the :ref:`Realign <nipype.interfaces.spm.Realign>` interface:
4037

41-
.. testcode::
42-
43-
realigner = pe.Node(interface=spm.Realign(), name='realign')
44-
realigner.inputs.in_files = 'somefuncrun.nii'
45-
realigner.inputs.register_to_mean = True
38+
.. literalinclude:: tutorial_101.py
39+
:lines: 16-18
4640

4741
This would be equivalent to:
4842

4943
.. testcode::
5044

51-
realigner = pe.Node(interface=spm.Realign(infile='somefuncrun.nii',
45+
realigner = pe.Node(interface=spm.Realign(infile=os.abspath('somefuncrun.nii'),
5246
register_to_mean = True),
5347
name='realign')
5448

@@ -58,15 +52,17 @@ later or while initializing the interface.
5852

5953
.. note::
6054

61-
In the above example, 'somefuncrun.nii' has to exist, otherwise the
62-
commands won't work. A node will check if appropriate inputs are
63-
being supplied.
55+
a) In the above example, 'somefuncrun.nii' has to exist in the current directory,
56+
otherwise the commands won't work. A node will check if appropriate
57+
inputs are being supplied.
6458

65-
Similar to the realigner node, we now set up a smoothing node.
59+
b) As noted above, you have to use the absolute path
60+
of the file otherwise the workflow will fail to run.
6661

67-
.. testcode::
62+
Similar to the realigner node, we now set up a smoothing node.
6863

69-
smoother = pe.Node(interface=spm.Smooth(fwhm=6), name='smooth')
64+
.. literalinclude:: tutorial_101.py
65+
:lines: 20
7066

7167
Now we have two nodes with their inputs defined. Note that we have not defined
7268
an input file for the smoothing node. This will be done by connecting the
@@ -77,17 +73,15 @@ realigner to the smoother in step 5.
7773
Here we create an instance of a workflow and indicate that it should operate in
7874
the current directory.
7975

80-
.. testcode::
81-
82-
workflow = pe.Workflow(name='preproc')
83-
workflow.base_dir = '.'
76+
.. literalinclude:: tutorial_101.py
77+
:lines: 25-26
8478

8579
**4. Adding nodes to workflows (optional)**
8680

8781
If nodes are going to be connected (see step 5), this step is not
8882
necessary. However, if you would like to run a node by itself without
8983
connecting it to any other node, then you need to add it to the
90-
workflow. For adding nodes, order of nodes is not important.
84+
workflow. When adding nodes, the order is not important.
9185

9286
.. testcode::
9387

@@ -100,15 +94,14 @@ This results in a workflow containing two isolated nodes:
10094
**5. Connecting nodes to each other**
10195

10296
We want to connect the output produced by realignment to the input of
103-
smoothing. This is done as follows.
97+
smoothing. This is done as follows:
10498

105-
.. testcode::
106-
107-
workflow.connect(realigner, 'realigned_files', smoother, 'in_files')
99+
.. literalinclude:: tutorial_101.py
100+
:lines: 31
108101

109102
or alternatively, a more flexible notation can be used. Although not shown here,
110103
the following notation can be used to connect multiple outputs from one node to
111-
multiple inputs (see step 7 below).
104+
multiple inputs (see step 7 below):
112105

113106
.. testcode::
114107

@@ -121,8 +114,8 @@ This results in a workflow containing two connected nodes:
121114
**6. Visualizing the workflow**
122115

123116
The workflow is represented as a directed acyclic graph (DAG) and one
124-
can visualize this using the following command. In fact, the pictures
125-
above were generated using this.
117+
can visualize this using the following command (in fact, the pictures
118+
above were generated using this):
126119

127120
.. testcode::
128121

@@ -147,18 +140,9 @@ options:
147140
Now that you have seen a basic pipeline let's add another node to the
148141
above pipeline.
149142

150-
.. testcode::
151-
152-
import nipype.algorithms.rapidart as ra
153-
artdetect = pe.Node(interface=ra.ArtifactDetect(), name='artdetect')
154-
artdetect.inputs.use_differences = [True, False]
155-
art.inputs.use_norm = True
156-
art.inputs.norm_threshold = 0.5
157-
art.inputs.zintensity_threshold = 3
158-
workflow.connect([(realigner, artdetect,
159-
[('realigned_files', 'realigned_files'),
160-
('realignment_parameters','realignment_parameters')]
161-
)])
143+
144+
.. literalinclude:: tutorial_101.py
145+
:lines: 42-53
162146

163147
.. note::
164148

@@ -180,12 +164,15 @@ This results in
180164
Assuming that **somefuncrun.nii** is actually a file or you've
181165
replaced it with an appropriate one, you can run the pipeline with:
182166

183-
.. testcode::
184-
185-
workflow.run()
167+
.. literalinclude:: tutorial_101.py
168+
:lines: 59
186169

187170
This should create a folder called preproc in your current directory,
188171
inside which are three folders: realign, smooth and artdetect (the names
189172
of the nodes). The outputs of these routines are in these folders.
190173

174+
.. admonition:: Example source code
175+
176+
You can download :download:`the source code of this example <tutorial_101.py>`.
177+
191178
.. include:: ../links_names.txt

0 commit comments

Comments
 (0)