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 ::
11
+ This is a minimal script for wrapping MATLAB code. You should replace the MATLAB
12
+ code template, and define approriate inputs and outputs.
15
13
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).
14
+ .. testcode ::
51
15
52
- mlab = MatlabCommand(script=script, mfile=True)
53
- result = mlab.run()
54
- return result.runtime
55
-
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
+ 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
60
63
61
64
62
65
Example 2
@@ -65,66 +68,58 @@ Example 2
65
68
By subclassing **MatlabCommand ** for your main class, and **MatlabInputSpec ** for your input and output spec, you gain access to some useful MATLAB hooks
66
69
67
70
.. 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
-
127
-
128
-
129
71
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
130
125
0 commit comments