Skip to content

Commit 52efaa0

Browse files
authored
Merge pull request nipy#2459 from djarecka/fix_tutorials
Fix tutorials (continuation of nipy#1036)
2 parents 0c6d3f8 + d4b05ea commit 52efaa0

15 files changed

+202
-1010
lines changed

doc/devel/cmd_interface_devel.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ symbols. For an input defined in InputSpec to be included into the executed
8080
commandline ``argstr`` has to be included. Additionally inside the main
8181
interface class you need to specify the name of the executable by assigning it
8282
to the ``_cmd`` field. Also the main interface class needs to inherit from
83-
:class:`nipype.interfaces.base.CommandLine`:
83+
:class:`CommandLine <nipype.interfaces.base.core.CommandLine>`:
8484

8585
.. testcode::
8686

doc/devel/matlab_example1.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from nipype.interfaces.matlab import MatlabCommand
2+
from nipype.interfaces.base import TraitedSpec, \
3+
BaseInterface, BaseInterfaceInputSpec, File
4+
import os
5+
from string import Template
6+
7+
8+
class ConmapTxt2MatInputSpec(BaseInterfaceInputSpec):
9+
in_file = File(exists=True, mandatory=True)
10+
out_file = File('cmatrix.mat', usedefault=True)
11+
12+
13+
class ConmapTxt2MatOutputSpec(TraitedSpec):
14+
out_file = File(exists=True)
15+
16+
17+
class ConmapTxt2Mat(BaseInterface):
18+
input_spec = ConmapTxt2MatInputSpec
19+
output_spec = ConmapTxt2MatOutputSpec
20+
21+
def _run_interface(self, runtime):
22+
d = dict(in_file=self.inputs.in_file,
23+
out_file=self.inputs.out_file)
24+
# This is your MATLAB code template
25+
script = Template("""in_file = '$in_file';
26+
out_file = '$out_file';
27+
ConmapTxt2Mat(in_file, out_file);
28+
exit;
29+
""").substitute(d)
30+
31+
# mfile = True will create an .m file with your script and executed.
32+
# Alternatively
33+
# mfile can be set to False which will cause the matlab code to be
34+
# passed
35+
# as a commandline argument to the matlab executable
36+
# (without creating any files).
37+
# This, however, is less reliable and harder to debug
38+
# (code will be reduced to
39+
# a single line and stripped of any comments).
40+
mlab = MatlabCommand(script=script, mfile=True)
41+
result = mlab.run()
42+
return result.runtime
43+
44+
def _list_outputs(self):
45+
outputs = self._outputs().get()
46+
outputs['out_file'] = os.path.abspath(self.inputs.out_file)
47+
return outputs

doc/devel/matlab_example2.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from nipype.interfaces.base import traits
2+
from nipype.interfaces.base import TraitedSpec
3+
from nipype.interfaces.matlab import MatlabCommand, MatlabInputSpec
4+
5+
6+
class HelloWorldInputSpec(MatlabInputSpec):
7+
name = traits.Str(mandatory=True,
8+
desc='Name of person to say hello to')
9+
10+
11+
class HelloWorldOutputSpec(TraitedSpec):
12+
matlab_output = traits.Str()
13+
14+
15+
class HelloWorld(MatlabCommand):
16+
"""Basic Hello World that displays Hello <name> in MATLAB
17+
18+
Returns
19+
-------
20+
21+
matlab_output : capture of matlab output which may be
22+
parsed by user to get computation results
23+
24+
Examples
25+
--------
26+
27+
>>> hello = HelloWorld()
28+
>>> hello.inputs.name = 'hello_world'
29+
>>> out = hello.run()
30+
>>> print out.outputs.matlab_output
31+
"""
32+
input_spec = HelloWorldInputSpec
33+
output_spec = HelloWorldOutputSpec
34+
35+
def _my_script(self):
36+
"""This is where you implement your script"""
37+
script = """
38+
disp('Hello %s Python')
39+
two = 1 + 1
40+
""" % (self.inputs.name)
41+
return script
42+
43+
def run(self, **inputs):
44+
# Inject your script
45+
self.inputs.script = self._my_script()
46+
results = super(MatlabCommand, self).run(**inputs)
47+
stdout = results.runtime.stdout
48+
# Attach stdout to outputs to access matlab results
49+
results.outputs.matlab_output = stdout
50+
return results
51+
52+
def _list_outputs(self):
53+
outputs = self._outputs().get()
54+
return outputs

doc/devel/matlab_interface_devel.rst

Lines changed: 12 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -4,127 +4,30 @@
44
How to wrap a MATLAB script
55
===========================
66

7-
This is minimal script for wrapping MATLAB code. You should replace the MATLAB
8-
code template, and define approriate inputs and outputs.
9-
107

118
Example 1
129
+++++++++
1310

14-
.. testcode::
15-
16-
from nipype.interfaces.matlab import MatlabCommand
17-
from nipype.interfaces.base import TraitedSpec, BaseInterface, BaseInterfaceInputSpec, File
18-
import os
19-
from string import Template
20-
21-
class ConmapTxt2MatInputSpec(BaseInterfaceInputSpec):
22-
in_file = File(exists=True, mandatory=True)
23-
out_file = File('cmatrix.mat', usedefault=True)
24-
25-
class ConmapTxt2MatOutputSpec(TraitedSpec):
26-
out_file = File(exists=True)
27-
28-
class ConmapTxt2Mat(BaseInterface):
29-
input_spec = ConmapTxt2MatInputSpec
30-
output_spec = ConmapTxt2MatOutputSpec
31-
32-
def _run_interface(self, runtime):
33-
d = dict(in_file=self.inputs.in_file,
34-
out_file=self.inputs.out_file)
35-
#this is your MATLAB code template
36-
script = Template("""in_file = ‘$in_file';
37-
out_file = ‘$out_file';
38-
ConmapTxt2Mat(in_file, out_file);
39-
exit;
40-
""").substitute(d)
41-
42-
# mfile = True will create an .m file with your script and executed.
43-
# Alternatively
44-
# mfile can be set to False which will cause the matlab code to be
45-
# passed
46-
# as a commandline argument to the matlab executable
47-
# (without creating any files).
48-
# This, however, is less reliable and harder to debug
49-
# (code will be reduced to
50-
# a single line and stripped of any comments).
11+
This is a minimal script for wrapping MATLAB code. You should replace the MATLAB
12+
code template, and define approriate inputs and outputs.
5113

52-
mlab = MatlabCommand(script=script, mfile=True)
53-
result = mlab.run()
54-
return result.runtime
14+
.. literalinclude:: matlab_example1.py
5515

56-
def _list_outputs(self):
57-
outputs = self._outputs().get()
58-
outputs['out_file'] = os.path.abspath(self.inputs.out_file)
59-
return outputs
16+
.. admonition:: Example source code
6017

18+
You can download :download:`the source code of this example <matlab_example1.py>`.
6119

6220
Example 2
6321
+++++++++
6422

65-
By subclassing **MatlabCommand** for your main class, and **MatlabInputSpec** for your input and output spec, you gain access to some useful MATLAB hooks
66-
67-
.. testcode::
68-
69-
import os
70-
from nipype.interfaces.base import File, traits
71-
from nipype.interfaces.matlab import MatlabCommand, MatlabInputSpec
72-
73-
74-
class HelloWorldInputSpec( MatlabInputSpec):
75-
name = traits.Str( mandatory = True,
76-
desc = 'Name of person to say hello to')
77-
78-
class HelloWorldOutputSpec( MatlabInputSpec):
79-
matlab_output = traits.Str( )
80-
81-
class HelloWorld( MatlabCommand):
82-
""" Basic Hello World that displays Hello <name> in MATLAB
83-
84-
Returns
85-
-------
86-
87-
matlab_output : capture of matlab output which may be
88-
parsed by user to get computation results
89-
90-
Examples
91-
--------
92-
93-
>>> hello = HelloWorld()
94-
>>> hello.inputs.name = 'hello_world'
95-
>>> out = hello.run()
96-
>>> print(out.outputs.matlab_output)
97-
"""
98-
input_spec = HelloWorldInputSpec
99-
output_spec = HelloWorldOutputSpec
100-
101-
def _my_script(self):
102-
"""This is where you implement your script"""
103-
script = """
104-
disp('Hello %s Python')
105-
two = 1 + 1
106-
"""%(self.inputs.name)
107-
return script
108-
109-
110-
def run(self, **inputs):
111-
## inject your script
112-
self.inputs.script = self._my_script()
113-
results = super(MatlabCommand, self).run( **inputs)
114-
stdout = results.runtime.stdout
115-
# attach stdout to outputs to access matlab results
116-
results.outputs.matlab_output = stdout
117-
return results
118-
119-
120-
def _list_outputs(self):
121-
outputs = self._outputs().get()
122-
return outputs
123-
124-
125-
126-
23+
By subclassing :class:`nipype.interfaces.matlab.MatlabCommand` for your main
24+
class, and :class:`nipype.interfaces.matlab.MatlabInputSpec` for your input and
25+
output spec, you gain access to some useful MATLAB hooks
12726

27+
.. literalinclude:: matlab_example2.py
12828

29+
.. admonition:: Example source code
12930

31+
You can download :download:`the source code of this example <matlab_example2.py>`.
13032

33+
.. include:: ../links_names.txt

doc/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* - .. image:: images/nipype_architecture_overview2.png
44
:width: 100 %
5+
56
- .. container::
67

78
Current neuroimaging software offer users an incredible opportunity to

doc/users/function_interface.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,6 @@ the string would be
146146
Unlike when using a function object, this input can be set like any other,
147147
meaning that you could write a function that outputs different function
148148
strings depending on some run-time contingencies, and connect that output
149-
the the ``function_str`` input of a downstream Function interface.
149+
the ``function_str`` input of a downstream Function interface.
150150

151151
.. include:: ../links_names.txt

0 commit comments

Comments
 (0)