Skip to content

Commit ee6c25d

Browse files
author
Mathieu Dubois
committed
DOC reworked devel/matlab_interface_devel.rst: move code to external files and correct output specification in example 2.
1 parent 552d06d commit ee6c25d

File tree

3 files changed

+107
-104
lines changed

3 files changed

+107
-104
lines changed

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: 6 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -11,115 +11,17 @@ Example 1
1111
This is a minimal script for wrapping MATLAB code. You should replace the MATLAB
1212
code template, and define approriate inputs and outputs.
1313

14-
.. testcode::
15-
16-
from nipype.interfaces.matlab import MatlabCommand
17-
from nipype.interfaces.base import TraitedSpec, \
18-
BaseInterface, BaseInterfaceInputSpec, File
19-
import os
20-
from string import Template
21-
22-
23-
class ConmapTxt2MatInputSpec(BaseInterfaceInputSpec):
24-
in_file = File(exists=True, mandatory=True)
25-
out_file = File('cmatrix.mat', usedefault=True)
26-
27-
28-
class ConmapTxt2MatOutputSpec(TraitedSpec):
29-
out_file = File(exists=True)
30-
31-
32-
class ConmapTxt2Mat(BaseInterface):
33-
input_spec = ConmapTxt2MatInputSpec
34-
output_spec = ConmapTxt2MatOutputSpec
35-
36-
def _run_interface(self, runtime):
37-
d = dict(in_file=self.inputs.in_file,
38-
out_file=self.inputs.out_file)
39-
# This is your MATLAB code template
40-
script = Template("""in_file = '$in_file';
41-
out_file = '$out_file';
42-
ConmapTxt2Mat(in_file, out_file);
43-
exit;
44-
""").substitute(d)
45-
46-
# mfile = True will create an .m file with your script and executed.
47-
# Alternatively
48-
# mfile can be set to False which will cause the matlab code to be
49-
# passed
50-
# as a commandline argument to the matlab executable
51-
# (without creating any files).
52-
# This, however, is less reliable and harder to debug
53-
# (code will be reduced to
54-
# a single line and stripped of any comments).
55-
mlab = MatlabCommand(script=script, mfile=True)
56-
result = mlab.run()
57-
return result.runtime
58-
59-
def _list_outputs(self):
60-
outputs = self._outputs().get()
61-
outputs['out_file'] = os.path.abspath(self.inputs.out_file)
62-
return outputs
14+
.. literalinclude:: matlab_example1.py
6315

16+
:download:`Download the source code of this example <matlab_example1.py>`.
6417

6518
Example 2
6619
+++++++++
6720

68-
By subclassing **MatlabCommand** for your main class, and **MatlabInputSpec** for your input and output spec, you gain access to some useful MATLAB hooks
21+
By subclassing :class:`nipype.interfaces.matlab.MatlabCommand` for your main class, and :class:`nipype.interfaces.matlab.MatlabInputSpec` for your input spec, you gain access to some useful MATLAB hooks
6922

70-
.. testcode::
23+
.. literalinclude:: matlab_example2.py
7124

72-
from nipype.interfaces.base import traits
73-
from nipype.interfaces.matlab import MatlabCommand, MatlabInputSpec
74-
75-
76-
class HelloWorldInputSpec(MatlabInputSpec):
77-
name = traits.Str(mandatory=True,
78-
desc='Name of person to say hello to')
79-
80-
81-
class HelloWorldOutputSpec(MatlabInputSpec):
82-
matlab_output = traits.Str()
83-
84-
85-
class HelloWorld(MatlabCommand):
86-
"""Basic Hello World that displays Hello <name> in MATLAB
87-
88-
Returns
89-
-------
90-
91-
matlab_output : capture of matlab output which may be
92-
parsed by user to get computation results
93-
94-
Examples
95-
--------
96-
97-
>>> hello = HelloWorld()
98-
>>> hello.inputs.name = 'hello_world'
99-
>>> out = hello.run()
100-
>>> print out.outputs.matlab_output
101-
"""
102-
input_spec = HelloWorldInputSpec
103-
output_spec = HelloWorldOutputSpec
104-
105-
def _my_script(self):
106-
"""This is where you implement your script"""
107-
script = """
108-
disp('Hello %s Python')
109-
two = 1 + 1
110-
""" % (self.inputs.name)
111-
return script
112-
113-
def run(self, **inputs):
114-
# Inject your script
115-
self.inputs.script = self._my_script()
116-
results = super(MatlabCommand, self).run(**inputs)
117-
stdout = results.runtime.stdout
118-
# Attach stdout to outputs to access matlab results
119-
results.outputs.matlab_output = stdout
120-
return results
121-
122-
def _list_outputs(self):
123-
outputs = self._outputs().get()
124-
return outputs
25+
:download:`Download the source code of this example <matlab_example2.py>`.
12526

27+
.. include:: ../links_names.txt

0 commit comments

Comments
 (0)