comparison m-toolbox/sltpda/sltpda_execute_loops.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 function sltpda_execute_loops(model)
2 % SLTPDA_EXECUTE_LOOPS runs all the loops in the input data-analysis pipeline.
3 % If not loops are present, then the pipeline is executed once.
4 %
5 % Usage: sltpda_execute_loops(model)
6 %
7 % M Hewitson 12-06-07
8 %
9 % $Id: sltpda_execute_loops.m,v 1.2 2007/06/13 08:47:28 hewitson Exp $
10 %
11
12 % Make sure the model is open
13 open_system(model);
14
15 if sltpda_verify_model(model)
16
17 global sltpda_loop_plotTitle;
18
19 % parse model file
20 cmds = sltpda_parse_model(model);
21 save 'cmds.mat' cmds
22 ncmds = length(cmds);
23
24 % get loops
25 loops = sltpda_buildloops(model)
26 save 'loops.mat' loops
27 nloops = length(loops);
28 % how many loops switched on
29 non = 0;
30 for j=1:nloops
31 if strcmp(loops(j).on, 'on')
32 non = non + 1;
33 end
34 end
35 % go through each loop
36 if nloops > 0 && non > 0
37
38 % convert the loops to a set of substitution commands
39 subs = loops2subs(loops);
40
41 % Now run levels
42 l1idx = find([subs.level] == 1);
43 l2idx = find([subs.level] == 2);
44 l3idx = find([subs.level] == 3);
45
46 for l1=l1idx
47 if isempty(l2idx)
48 ll = [l1];
49 % command substitution
50 runLoop2(cmds, subs(ll))
51 else
52 for l2=l2idx
53 if isempty(l3idx)
54 ll = [l1 l2];
55 % command substitution
56 runLoop2(cmds, subs(ll))
57 else
58 for l3=l3idx
59 ll = [l1 l2 l3];
60
61
62
63
64 % command substitution
65 runLoop2(cmds, subs(ll))
66 end
67 end
68 end
69 end
70 end
71
72 % for l=1:nloops
73 % % - for each loop we need to adjust the parameters of the model
74 % % - look for the command to modify then set the parameter
75 % loop = loops(l);
76 % if strcmp(loop.on, 'on')
77 % values = str2num(loop.values);
78 % if isempty(values)
79 % values = loop.values;
80 % end
81 % nvals = length(values);
82 % for v=1:nvals
83 % % set sltpda_loop_plotTitle
84 % if isnumeric(values(v))
85 % sltpda_loop_plotTitle = ['loop_' loop.key '_' num2str(values(v))];
86 % else
87 % sltpda_loop_plotTitle = ['loop_' loop.key '_' values(v)];
88 % end
89 % runLoop(cmds, str2num(loop.handle), loop.key, values(v));
90 % end
91 % end
92 % end
93 else
94 runLoop2(cmds, []);
95 % runLoop(cmds, [], [], []);
96 end
97 end
98
99
100 %--------------------------------------------------------------------------
101 % Run a loop around a set of commands
102 %
103 function runLoop2(cmds, subs)
104
105
106 global sltpda_loop_plotTitle;
107
108 % evaluate each command
109 % - here we can set the waitbar caption to display the current
110 % - parameter name and value
111 disp('-----------------------------------------------------------')
112 sltpda_loop_plotTitle = [];
113 h = waitbar(0,'Executing pipeline...');
114 nc = length(cmds);
115 for j=1:nc
116 cmd = cmds(j);
117 if isempty(subs)
118 sidx = [];
119 else
120 sidx = find(cmd.handle == [subs.handle]);
121 end
122 for k=1:length(sidx)
123 % cmd
124 key = subs(sidx(k)).key;
125 val = subs(sidx(k)).value;
126 if isnumeric(val)
127 valstr = mat2str(val);
128 else
129 valstr = char(val);
130 end
131 sltpda_loop_plotTitle = [sltpda_loop_plotTitle ' / ' key ':' valstr];
132 if iscell(val)
133 val = char(val);
134 end
135 disp('^^^ performing parameter substitution.');
136 pl = cmd.plist;
137 pl = set(pl, key, val);
138 params = string(pl);
139 newcmd = sltpda_buildCmd(cmd.ins, cmd.outs, params, cmd.fcn);
140 cmd.cmd = newcmd;
141 end
142 disp(['== executing: ' cmd.cmd]);
143 eval(cmd.cmd);
144 waitbar(j/nc,h)
145 end
146 delete(h);
147
148 disp('-----------------------------------------------------------')
149
150 %--------------------------------------------------------------------------
151 % convert loops to substitutions
152 function subs = loops2subs(loops)
153
154 % make empty substitutions; one for each allowed level
155 subs = [];
156 % subs(1).handle = [];
157 % subs(1).level = 1;
158 % subs(1).key = '';
159 % subs(1).value = [];
160 % subs(2).handle = [];
161 % subs(2).level = 2;
162 % subs(2).key = '';
163 % subs(2).value = [];
164 % subs(3).handle = [];
165 % subs(3).level = 3;
166 % subs(3).key = '';
167 % subs(3).value = [];
168
169
170 k = length(subs)+1;
171 for j=1:length(loops)
172
173 loop = loops(j);
174 ll = loop.level;
175
176 % go through each value for this loop
177 vals = loop.values;
178 if ~isempty(str2num(vals))
179 vals = str2num(vals);
180 else
181 vals = str2cells(vals);
182 end
183
184 for v=1:length(vals)
185 eval(sprintf('subs(%d).handle = str2double(loop.handle);', k));
186 eval(sprintf('subs(%d).level = str2num(loop.level);', k));
187 eval(sprintf('subs(%d).key = loop.key;', k));
188 eval(sprintf('subs(%d).value = vals(v);', k));
189
190 k = k + 1;
191 end
192 end
193
194 %--------------------------------------------------------------------------
195 % Run a loop around a set of commands
196 %
197 function runLoop(cmds, handle, key, val)
198
199 % evaluate each command
200 % - here we can set the waitbar caption to display the current
201 % - parameter name and value
202 h = waitbar(0,'Executing pipeline...');
203 nc = length(cmds);
204 for j=1:nc
205 cmd = cmds(j);
206 hval = (1-handle/cmd.handle);
207 % if hval < 1e-10
208 if handle == cmd.handle
209 cmd
210 disp('^^^ performing parameter substitution.');
211 pl = cmd.plist;
212 pl = set(pl, key, val);
213 params = string(pl);
214 newcmd = sltpda_buildCmd(cmd.ins, cmd.outs, params, cmd.fcn);
215 cmd.cmd = newcmd;
216 end
217 disp(['== executing: ' cmd.cmd]);
218 eval(cmd.cmd);
219 waitbar(j/nc,h)
220 end
221 delete(h);