|
4 | 4 | How to wrap a MATLAB script |
5 | 5 | =========================== |
6 | 6 |
|
7 | | -This is minimal script for wrapping MATLAB code. You should replace the MATLAB |
8 | | -code template, and define approriate inputs and outputs. |
9 | | - |
10 | 7 |
|
11 | 8 | Example 1 |
12 | 9 | +++++++++ |
13 | 10 |
|
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. |
51 | 13 |
|
52 | | - mlab = MatlabCommand(script=script, mfile=True) |
53 | | - result = mlab.run() |
54 | | - return result.runtime |
| 14 | +.. literalinclude:: matlab_example1.py |
55 | 15 |
|
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 |
60 | 17 |
|
| 18 | + You can download :download:`the source code of this example <matlab_example1.py>`. |
61 | 19 |
|
62 | 20 | Example 2 |
63 | 21 | +++++++++ |
64 | 22 |
|
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 |
127 | 26 |
|
| 27 | +.. literalinclude:: matlab_example2.py |
128 | 28 |
|
| 29 | +.. admonition:: Example source code |
129 | 30 |
|
| 31 | + You can download :download:`the source code of this example <matlab_example2.py>`. |
130 | 32 |
|
| 33 | +.. include:: ../links_names.txt |
0 commit comments