Mercurial > hg > ltpda
comparison testing/utp_1.1/utps/ao/utp_ao_lscov.m @ 44:409a22968d5e default
Add unit tests
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Tue, 06 Dec 2011 18:42:11 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
43:bc767aaa99a8 | 44:409a22968d5e |
---|---|
1 % UTP_AO_LSCOV a set of UTPs for the ao/lscov method | |
2 % | |
3 % M Hewitson 06-08-08 | |
4 % | |
5 % $Id: utp_ao_lscov.m,v 1.14 2009/12/21 13:46:21 mauro Exp $ | |
6 % | |
7 | |
8 % <MethodDescription> | |
9 % | |
10 % The lscov method of the ao class solves a set of linear equations by | |
11 % performing a linear least-squares fit. | |
12 % It solves the problem | |
13 % Y = HX | |
14 % where X are the parameters, Y the measurements, and H the linear | |
15 % equations relating the two. | |
16 % | |
17 % </MethodDescription> | |
18 | |
19 function results = utp_ao_lscov(varargin) | |
20 | |
21 % Check the inputs | |
22 if nargin == 0 | |
23 | |
24 % Some keywords | |
25 class = 'ao'; | |
26 mthd = 'lscov'; | |
27 | |
28 results = []; | |
29 disp('******************************************************'); | |
30 disp(['**** Running UTPs for ' class '/' mthd]); | |
31 disp('******************************************************'); | |
32 | |
33 % Test AOs | |
34 fs = 10; | |
35 nsecs = 10; | |
36 x1 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', nsecs)); | |
37 x2 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', nsecs)); | |
38 x3 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', nsecs)); | |
39 n = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', nsecs)); | |
40 | |
41 % Exception list for the UTPs: | |
42 [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples(); | |
43 | |
44 % Run the tests | |
45 results = [results utp_01]; % getInfo call | |
46 results = [results utp_02]; % Vector input | |
47 results = [results utp_03]; % Matrix input | |
48 results = [results utp_04]; % List input | |
49 results = [results utp_05]; % Test history is working | |
50 results = [results utp_06]; % Test the modify call works | |
51 results = [results utp_07]; % Test weights usage | |
52 | |
53 c = [1 -2 3]; | |
54 y = c(1)*x1 + c(2)*x2 + c(3)*x3 + n; | |
55 avec = [x1, x2, x3]; | |
56 results = [results utp_11(mthd, [avec y], ple1)]; % Test plotinfo doesn't disappear | |
57 | |
58 disp('Done.'); | |
59 disp('******************************************************'); | |
60 | |
61 elseif nargin == 1 % Check for UTP functions | |
62 if strcmp(varargin{1}, 'isutp') | |
63 results = 1; | |
64 else | |
65 results = 0; | |
66 end | |
67 else | |
68 error('### Incorrect inputs') | |
69 end | |
70 | |
71 %% UTP_01 | |
72 | |
73 % <TestDescription> | |
74 % | |
75 % Tests that the getInfo call works for this method. | |
76 % | |
77 % </TestDescription> | |
78 function result = utp_01 | |
79 | |
80 | |
81 % <SyntaxDescription> | |
82 % | |
83 % Test that the getInfo call works for no sets, all sets, and each set | |
84 % individually. | |
85 % | |
86 % </SyntaxDescription> | |
87 | |
88 try | |
89 % <SyntaxCode> | |
90 % Call for no sets | |
91 io(1) = eval([class '.getInfo(''' mthd ''', ''None'')']); | |
92 % Call for all sets | |
93 io(2) = eval([class '.getInfo(''' mthd ''')']); | |
94 % Call for each set | |
95 for kk=1:numel(io(2).sets) | |
96 io(kk+2) = eval([class '.getInfo(''' mthd ''', ''' io(2).sets{kk} ''')']); | |
97 end | |
98 % </SyntaxCode> | |
99 stest = true; | |
100 catch err | |
101 disp(err.message) | |
102 stest = false; | |
103 end | |
104 | |
105 % <AlgoDescription> | |
106 % | |
107 % 1) Check that getInfo call returned an minfo object in all cases. | |
108 % 2) Check that all plists have the correct parameters. | |
109 % | |
110 % </AlgoDescription> | |
111 | |
112 atest = true; | |
113 if stest | |
114 % <AlgoCode> | |
115 % check we have minfo objects | |
116 if isa(io, 'minfo') | |
117 %%% SET 'None' | |
118 if ~isempty(io(1).sets), atest = false; end | |
119 if ~isempty(io(1).plists), atest = false; end | |
120 %%% Check all Sets | |
121 if ~any(strcmpi(io(2).sets, 'Default')), atest = false; end | |
122 if numel(io(2).plists) ~= numel(io(2).sets), atest = false; end | |
123 %%%%%%%%%% SET 'Default' | |
124 if io(3).plists.nparams ~= 2, atest = false; end | |
125 % Check key | |
126 if ~io(3).plists.isparam('weights'), atest = false; end | |
127 if ~io(3).plists.isparam('cov'), atest = false; end | |
128 % Check default value | |
129 if ~isEmptyDouble(io(3).plists.find('weights')), atest = false; end | |
130 if ~isEmptyDouble(io(3).plists.find('cov')), atest = false; end | |
131 % Check options | |
132 if ~isequal(io(3).plists.getOptionsForParam('weights'), {[]}), atest = false; end | |
133 if ~isequal(io(3).plists.getOptionsForParam('cov'), {[]}), atest = false; end | |
134 end | |
135 % </AlgoCode> | |
136 else | |
137 atest = false; | |
138 end | |
139 | |
140 % Return a result structure | |
141 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
142 end % END UTP_01 | |
143 | |
144 %% UTP_02 | |
145 | |
146 % <TestDescription> | |
147 % | |
148 % Tests that the lscov method works with a vector of AOs as input. | |
149 % | |
150 % </TestDescription> | |
151 function result = utp_02 | |
152 | |
153 % <SyntaxDescription> | |
154 % | |
155 % Test that the lscov method works for a vector of AOs as input. | |
156 % | |
157 % </SyntaxDescription> | |
158 | |
159 try | |
160 % <SyntaxCode> | |
161 c = [1 -2 3]; | |
162 y = c(1)*x1 + c(2)*x2 + c(3)*x3 + n; | |
163 avec = [x1, x2, x3]; | |
164 out = lscov(avec, y); | |
165 % </SyntaxCode> | |
166 stest = true; | |
167 catch err | |
168 disp(err.message) | |
169 stest = false; | |
170 end | |
171 | |
172 % <AlgoDescription> | |
173 % | |
174 % 1) Check the data type of the output | |
175 % 2) Check that each output AO contains the correct data. | |
176 % | |
177 % </AlgoDescription> | |
178 | |
179 atest = true; | |
180 if stest | |
181 % <AlgoCode> | |
182 % Check we have exactly one pest with 3 parameters | |
183 if numel(out) ~= 1 || numel(out.y) ~= 3, atest = false; end | |
184 if ~isa(out, 'pest'), atest = false; end | |
185 | |
186 % Check each output against MATLAB lscov | |
187 C = avec; | |
188 H = C(:).y; | |
189 [P,STDX,MSE,COV] = lscov(H,y.y); | |
190 if ~isequal(out.y, P), atest = false; end | |
191 if ~isequal(out.dy, STDX), atest = false; end | |
192 if ~isequal(out.procinfo.find('MSE'), MSE), atest = false; end | |
193 if ~isequal(out.cov, COV), atest = false; end | |
194 % </AlgoCode> | |
195 else | |
196 atest = false; | |
197 end | |
198 | |
199 % Return a result structure | |
200 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
201 end % END UTP_02 | |
202 | |
203 %% UTP_03 | |
204 | |
205 % <TestDescription> | |
206 % | |
207 % Tests that the lscov method works with a matrix of AOs as input. | |
208 % | |
209 % </TestDescription> | |
210 function result = utp_03 | |
211 | |
212 % <SyntaxDescription> | |
213 % | |
214 % Tests that the lscov method works with a matrix of AOs as input. | |
215 % | |
216 % </SyntaxDescription> | |
217 | |
218 try | |
219 % <SyntaxCode> | |
220 c = [1 -2 3 -1 2 4]; | |
221 y = c(1)*x1 + c(2)*x2 + c(3)*x3 + c(4)*x1 + c(5)*x2 + c(6)*x3 + n; | |
222 amat = [x1, x2, x3; x1, x2, x3]; | |
223 out = lscov(amat, y); | |
224 % </SyntaxCode> | |
225 stest = true; | |
226 catch err | |
227 disp(err.message) | |
228 stest = false; | |
229 end | |
230 | |
231 % <AlgoDescription> | |
232 % | |
233 % 1) Check the data type of the output | |
234 % 2) Check that each output AO contains the correct data. | |
235 % | |
236 % </AlgoDescription> | |
237 | |
238 atest = true; | |
239 if stest | |
240 % <AlgoCode> | |
241 % Check we have exactly one pest with 6 parameters | |
242 if numel(out) ~= 1 || numel(out.y) ~= 6, atest = false; end | |
243 if ~isa(out, 'pest'), atest = false; end | |
244 | |
245 % Check each output against MATLAB lscov | |
246 C = amat; | |
247 H = C(:).y; | |
248 [P,STDX,MSE,COV] = lscov(H,y.y); | |
249 if ~isequal(out.y, P), atest = false; end | |
250 if ~isequal(out.dy, STDX), atest = false; end | |
251 if ~isequal(out.procinfo.find('MSE'), MSE), atest = false; end | |
252 if ~isequal(out.cov, COV), atest = false; end | |
253 % </AlgoCode> | |
254 else | |
255 atest = false; | |
256 end | |
257 | |
258 % Return a result structure | |
259 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
260 end % END UTP_03 | |
261 | |
262 %% UTP_04 | |
263 | |
264 % <TestDescription> | |
265 % | |
266 % Tests that the lscov method works with a list of AOs as input. | |
267 % | |
268 % </TestDescription> | |
269 function result = utp_04 | |
270 | |
271 % <SyntaxDescription> | |
272 % | |
273 % Tests that the lscov method works with a list of AOs as input. | |
274 % | |
275 % </SyntaxDescription> | |
276 | |
277 try | |
278 % <SyntaxCode> | |
279 c = [1 -2 3]; | |
280 y = c(1)*x1 + c(2)*x2 + c(3)*x3 + n; | |
281 out = lscov(x1, x2, x3, y); | |
282 % </SyntaxCode> | |
283 stest = true; | |
284 catch err | |
285 disp(err.message) | |
286 stest = false; | |
287 end | |
288 | |
289 % <AlgoDescription> | |
290 % | |
291 % 1) Check the data type of the output | |
292 % 2) Check that each output AO contains the correct data. | |
293 % | |
294 % </AlgoDescription> | |
295 | |
296 atest = true; | |
297 if stest | |
298 % <AlgoCode> | |
299 % Check we have exactly one pest with 3 parameters | |
300 if numel(out) ~= 1 || numel(out.y) ~= 3, atest = false; end | |
301 if ~isa(out, 'pest'), atest = false; end | |
302 | |
303 % Check each output against MATLAB lscov | |
304 C = [x1, x2, x3]; | |
305 H = C(:).y; | |
306 [P,STDX,MSE,COV] = lscov(H,y.y); | |
307 if ~isequal(out.y, P), atest = false; end | |
308 if ~isequal(out.dy, STDX), atest = false; end | |
309 if ~isequal(out.procinfo.find('MSE'), MSE), atest = false; end | |
310 if ~isequal(out.cov, COV), atest = false; end | |
311 % </AlgoCode> | |
312 else | |
313 atest = false; | |
314 end | |
315 | |
316 % Return a result structure | |
317 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
318 end % END UTP_04 | |
319 | |
320 %% UTP_05 | |
321 | |
322 % <TestDescription> | |
323 % | |
324 % Tests that the lscov method properly applies history. | |
325 % | |
326 % </TestDescription> | |
327 function result = utp_05 | |
328 | |
329 % <SyntaxDescription> | |
330 % | |
331 % Test that the result of applying the lscov method can be processed back. | |
332 % | |
333 % </SyntaxDescription> | |
334 | |
335 try | |
336 % <SyntaxCode> | |
337 c = [1 -2 3]; | |
338 y = c(1)*x1 + c(2)*x2 + c(3)*x3 + n; | |
339 out = lscov(x1, x2, x3, y); | |
340 mout = rebuild(out); | |
341 % </SyntaxCode> | |
342 stest = true; | |
343 catch err | |
344 disp(err.message) | |
345 stest = false; | |
346 end | |
347 | |
348 % <AlgoDescription> | |
349 % | |
350 % 1) Check that the last entry in the history of 'out' corresponds to | |
351 % 'lscov'. | |
352 % 2) Check that the re-built object is the same object as the input. | |
353 % | |
354 % </AlgoDescription> | |
355 | |
356 atest = true; | |
357 if stest | |
358 % <AlgoCode> | |
359 % Check the last step in the history of 'out' | |
360 if ~strcmp(out.hist.methodInfo.mname, 'lscov'), atest = false; end | |
361 % The rebuilt object must be the same as 'out' | |
362 if ~eq(mout, out, ple2), atest = false; end | |
363 % </AlgoCode> | |
364 else | |
365 atest = false; | |
366 end | |
367 | |
368 % Return a result structure | |
369 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
370 end % END UTP_05 | |
371 | |
372 %% UTP_06 | |
373 | |
374 % <TestDescription> | |
375 % | |
376 % The lscov method can not be used as a modifer method. | |
377 % | |
378 % </TestDescription> | |
379 function result = utp_06 | |
380 | |
381 % <SyntaxDescription> | |
382 % | |
383 % The lscov method can not be used as a modifer method. | |
384 % | |
385 % </SyntaxDescription> | |
386 | |
387 try | |
388 % <SyntaxCode> | |
389 c = [1 -2 3]; | |
390 y = c(1)*x1 + c(2)*x2 + c(3)*x3 + n; | |
391 x1.lscov(x1, x2, x3, y); | |
392 stest = false; | |
393 % </SyntaxCode> | |
394 catch | |
395 stest = true; | |
396 end | |
397 | |
398 % <AlgoDescription> | |
399 % | |
400 % 1) Nothing to check. | |
401 % | |
402 % </AlgoDescription> | |
403 | |
404 atest = true; | |
405 if stest | |
406 % <AlgoCode> | |
407 % </AlgoCode> | |
408 else | |
409 atest = false; | |
410 end | |
411 | |
412 % Return a result structure | |
413 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
414 end % END UTP_06 | |
415 | |
416 %% UTP_07 | |
417 | |
418 % <TestDescription> | |
419 % | |
420 % Check that the lscov method uses weights for the fit. | |
421 % | |
422 % </TestDescription> | |
423 function result = utp_07 | |
424 | |
425 % <SyntaxDescription> | |
426 % | |
427 % Check that the lscov method uses weights for the fit. | |
428 % | |
429 % </SyntaxDescription> | |
430 | |
431 try | |
432 % <SyntaxCode> | |
433 c = [1 -2 3]; | |
434 x = 1:100; | |
435 w2 = (x.^2 - 100.*x + 2500)/2500; | |
436 w1(1:100) = 1; | |
437 | |
438 y = c(1)*x1 + c(2)*x2 + c(3)*x3 + n; | |
439 | |
440 % Setting units | |
441 y.setYunits('V m'); | |
442 x1.setYunits('V^-1 Hz'); | |
443 x2.setYunits('V^-1 Hz'); | |
444 x3.setYunits('V^-1 Hz'); | |
445 | |
446 pl1 = plist('Weights', w1); | |
447 pl2 = plist('Weights', w2); | |
448 pl3 = plist('Weights', ao(w2)); | |
449 | |
450 out1 = lscov(x1, x2, x3, y); | |
451 out2 = lscov(x1, x2, x3, y, pl1); | |
452 out3 = lscov(x1, x2, x3, y, pl2); | |
453 out4 = lscov(x1, x2, x3, y, pl3); | |
454 mout1 = rebuild(out1); | |
455 mout2 = rebuild(out2); | |
456 mout3 = rebuild(out3); | |
457 mout4 = rebuild(out4); | |
458 % </SyntaxCode> | |
459 stest = true; | |
460 catch err | |
461 disp(err.message) | |
462 stest = false; | |
463 end | |
464 | |
465 % <AlgoDescription> | |
466 % | |
467 % 1) Check the output data | |
468 % 2) Check the yunits | |
469 % 3) Check that 'out1' and 'out2' have the same data | |
470 % 4) Check that 'out3' and 'out4' have the same data | |
471 % 5) Check the re-built objects | |
472 % | |
473 % </AlgoDescription> | |
474 | |
475 atest = true; | |
476 if stest | |
477 % <AlgoCode> | |
478 C = [x1, x2, x3]; | |
479 H = C(:).y; | |
480 | |
481 % Check the output data with w1 | |
482 [P,STDX,MSE,COV] = lscov(H,y.y,w1); | |
483 if ~isequal(out2.y, P), atest = false; end | |
484 if ~isequal(out2.dy, STDX), atest = false; end | |
485 if ~isequal(out2.procinfo.find('MSE'), MSE), atest = false; end | |
486 if ~isequal(out2.cov, COV), atest = false; end | |
487 for ii = 1:3 | |
488 if ~eq(out2.yunits(ii), unit('V m V Hz^-1')), atest = false; end | |
489 end | |
490 | |
491 % Check the output data with w2 | |
492 [P,STDX,MSE,COV] = lscov(H,y.y,w2); | |
493 if ~isequal(out3.y, P), atest = false; end | |
494 if ~isequal(out3.dy, STDX), atest = false; end | |
495 if ~isequal(out3.procinfo.find('MSE'), MSE), atest = false; end | |
496 if ~isequal(out3.cov, COV), atest = false; end | |
497 for ii = 1:3 | |
498 if ~eq(out3.yunits(ii), unit('V m V Hz^-1')), atest = false; end | |
499 end | |
500 | |
501 % Check that 'out1' and 'out2' have the same data | |
502 if ne(out1, out2, ple3), atest = false; end | |
503 % Check that 'out3' and 'out4' have the same data | |
504 if ne(out3, out4, ple3), atest = false; end | |
505 | |
506 % Run 'test[1..4].m' and check the result | |
507 if ne(mout1, out1, ple2), atest = false; end | |
508 if ne(mout2, out2, ple2), atest = false; end | |
509 if ne(mout3, out3, ple2), atest = false; end | |
510 if ne(mout4, out4, ple2), atest = false; end | |
511 % </AlgoCode> | |
512 else | |
513 atest = false; | |
514 end | |
515 | |
516 % Return a result structure | |
517 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
518 end % END UTP_07 | |
519 | |
520 | |
521 | |
522 end |