Skip to content

Commit ef6c57c

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 d2fca1c commit ef6c57c

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

doc/devel/matlab_example1.py

+47
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

+54
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

0 commit comments

Comments
 (0)