comparison testing/utp_1.1/utps/ao/utp_ao_polyfit.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_POLYFIT a set of UTPs for the ao/polyfit method
2 %
3 % M Hewitson 06-08-08
4 %
5 % $Id: utp_ao_polyfit.m,v 1.20 2010/05/05 04:20:10 mauro Exp $
6 %
7
8 % <MethodDescription>
9 %
10 % The polyfit method of the ao class fits a polynomial to the input y
11 % and/or x data.
12 %
13 % </MethodDescription>
14
15 function results = utp_ao_polyfit(varargin)
16
17 % Check the inputs
18 if nargin == 0
19
20 % Some keywords
21 class = 'ao';
22 mthd = 'polyfit';
23
24 results = [];
25 disp('******************************************************');
26 disp(['**** Running UTPs for ' class '/' mthd]);
27 disp('******************************************************');
28
29 % Test AOs
30 [at1,at2,at3,at4,at5,at6,atvec,atmat] = eval(['get_test_objects_' class]);
31
32 % Exception list for the UTPs:
33 [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples();
34
35 % Run the tests
36 results = [results utp_01]; % getInfo call
37 results = [results utp_02]; % Vector input
38 results = [results utp_03]; % Matrix input
39 results = [results utp_04]; % List input
40 results = [results utp_05]; % Test with mixed input
41 results = [results utp_06]; % Test history is working
42 results = [results utp_07]; % Test the modify call works
43 results = [results utp_08]; % Test that units are properly handled
44 results = [results utp_09]; % Test against Matlab polyfit
45
46 disp('Done.');
47 disp('******************************************************');
48
49 elseif nargin == 1 % Check for UTP functions
50 if strcmp(varargin{1}, 'isutp')
51 results = 1;
52 else
53 results = 0;
54 end
55 else
56 error('### Incorrect inputs')
57 end
58
59 %% UTP_01
60
61 % <TestDescription>
62 %
63 % Tests that the getInfo call works for this method.
64 %
65 % </TestDescription>
66 function result = utp_01
67
68
69 % <SyntaxDescription>
70 %
71 % Test that the getInfo call works for no sets, all sets, and each set
72 % individually.
73 %
74 % </SyntaxDescription>
75
76 try
77 % <SyntaxCode>
78 % Call for no sets
79 io(1) = eval([class '.getInfo(''' mthd ''', ''None'')']);
80 % Call for all sets
81 io(2) = eval([class '.getInfo(''' mthd ''')']);
82 % Call for each set
83 for kk=1:numel(io(2).sets)
84 io(kk+2) = eval([class '.getInfo(''' mthd ''', ''' io(2).sets{kk} ''')']);
85 end
86 % </SyntaxCode>
87 stest = true;
88 catch err
89 disp(err.message)
90 stest = false;
91 end
92
93 % <AlgoDescription>
94 %
95 % 1) Check that getInfo call returned an minfo object in all cases.
96 % 2) Check that all plists have the correct parameters.
97 %
98 % </AlgoDescription>
99
100 atest = true;
101 if stest
102 % <AlgoCode>
103 % check we have minfo objects
104 if isa(io, 'minfo')
105 %%% SET 'None'
106 if ~isempty(io(1).sets), atest = false; end
107 if ~isempty(io(1).plists), atest = false; end
108 %%% Check all Sets
109 if ~any(strcmpi(io(2).sets, 'Default')), atest = false; end
110 if numel(io(2).plists) ~= numel(io(2).sets), atest = false; end
111 %%%%%%%%%% SET 'Default'
112 if io(3).plists.nparams ~= 2, atest = false; end
113 % Check key
114 if ~io(3).plists.isparam('N'), atest = false; end
115 if ~io(3).plists.isparam('rescale'), atest = false; end
116 % Check default value
117 if ~isequal(io(3).plists.find('N'), 1), atest = false; end
118 if ~isequal(io(3).plists.find('rescale'), false), atest = false; end
119 % Check options
120 if ~isequal(io(3).plists.getOptionsForParam('N'), {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}), atest = false; end
121 if ~isequal(io(3).plists.getOptionsForParam('rescale'), {false, true}), atest = false; end
122 end
123 % </AlgoCode>
124 else
125 atest = false;
126 end
127
128 % Return a result structure
129 result = utp_prepare_result(atest, stest, dbstack, mfilename);
130 end % END UTP_01
131
132 %% UTP_02
133
134 % <TestDescription>
135 %
136 % Tests that the polyfit method fails with with a vector of AOs as input.
137 %
138 % </TestDescription>
139 function result = utp_02
140
141 % <SyntaxDescription>
142 %
143 % Test that the polyfit method works with a vector of AOs as input.
144 %
145 % </SyntaxDescription>
146
147 try
148 % <SyntaxCode>
149 N = 2;
150 % Call with single output
151 out = polyfit(atvec, plist('N', N));
152 % Call with multiple output
153 [out_1, out_2, out_3] = polyfit(atvec, plist('N', N));
154 % </SyntaxCode>
155 stest = true;
156 catch err
157 disp(err.message)
158 stest = false;
159 end
160
161 % <AlgoDescription>
162 %
163 % 1) Check we have the correct number of output objects in the version with
164 % single output
165 % 2) Check we have the correct number of elements in the output objects
166 %
167 % </AlgoDescription>
168
169 atest = true;
170 if stest
171 % <AlgoDescription>
172 % 1) Check we have the correct number of output objects in the version with
173 % single output
174 % 2) Check we have the correct number of elements in the output objects
175 %
176 % </AlgoDescription>
177 % <AlgoCode>
178 if ~isequal(size(out), [1 numel(atvec)]), atest = false; end
179 for jj = 1:numel(out)
180 if ~isequal(numel(out(jj).y), N+1), atest = false; end
181 end
182 if ~isequal(numel(out_1.y), N+1), atest = false; end
183 if ~isequal(numel(out_2.y), N+1), atest = false; end
184 if ~isequal(numel(out_3.y), N+1), atest = false; end
185 % </AlgoCode>
186 else
187 atest = false;
188 end
189
190 % Return a result structure
191 result = utp_prepare_result(atest, stest, dbstack, mfilename);
192 end % END UTP_02
193
194 %% UTP_03
195
196 % <TestDescription>
197 %
198 % Tests that the polyfit method works with a matrix of AOs as input.
199 %
200 % </TestDescription>
201 function result = utp_03
202
203 % <SyntaxDescription>
204 %
205 % Test that the polyfit method works for a matrix of AOs as input.
206 %
207 % </SyntaxDescription>
208
209 try
210 % <SyntaxCode>
211 N = 3;
212 amat = [at1 at2 at5; at6 at5 at2];
213 out = polyfit(amat, plist('N', N));
214 % </SyntaxCode>
215 stest = true;
216 catch err
217 disp(err.message)
218 stest = false;
219 end
220
221 % <AlgoDescription>
222 %
223 % 1) Check we have the correct number of output objects
224 %
225 % </AlgoDescription>
226
227 atest = true;
228 if stest
229 % <AlgoCode>
230 if ~isequal(numel(out), numel(amat)), atest = false; end
231 for jj = 1:numel(out)
232 if ~isequal(numel(out(jj).y), N+1), atest = false; end
233 end
234 % </AlgoCode>
235 else
236 atest = false;
237 end
238
239 % Return a result structure
240 result = utp_prepare_result(atest, stest, dbstack, mfilename);
241 end % END UTP_03
242
243 %% UTP_04
244
245 % <TestDescription>
246 %
247 % Tests that the polyfit method works with a list of AOs as input.
248 %
249 % </TestDescription>
250 function result = utp_04
251
252 % <SyntaxDescription>
253 %
254 % Test that the polyfit method works for a list of AOs as input.
255 %
256 % </SyntaxDescription>
257
258 try
259 % <SyntaxCode>
260 N = 1;
261 % Call with single output
262 out = polyfit(at5, at6, at5, plist('N', N));
263 % Call with multiple output
264 [out_1, out_2, out_3] = polyfit(at5, at6, at5, plist('N', N));
265 % </SyntaxCode>
266 stest = true;
267 catch err
268 disp(err.message)
269 stest = false;
270 end
271
272 % <AlgoDescription>
273 %
274 % 1) Check we have the correct number of output objects in the version with
275 % single output
276 % 2) Check we have the correct number of elements in the output objects
277 %
278 % </AlgoDescription>
279
280 atest = true;
281 if stest
282 % <AlgoDescription>
283 % 1) Check we have the correct number of output objects in the version with
284 % single output
285 % 2) Check we have the correct number of elements in the output objects
286 %
287 % </AlgoDescription>
288 % <AlgoCode>
289 if ~isequal(size(out), [1 3]), atest = false; end
290 for jj = 1:numel(out)
291 if ~isequal(numel(out(jj).y), N+1), atest = false; end
292 end
293 if ~isequal(numel(out_1.y), N+1), atest = false; end
294 if ~isequal(numel(out_2.y), N+1), atest = false; end
295 if ~isequal(numel(out_3.y), N+1), atest = false; end
296 % </AlgoCode>
297 else
298 atest = false;
299 end
300
301 % Return a result structure
302 result = utp_prepare_result(atest, stest, dbstack, mfilename);
303 end % END UTP_04
304
305 %% UTP_05
306
307 % <TestDescription>
308 %
309 % Tests that the polyfit method works with a mix of different shaped AOs as
310 % input.
311 %
312 % </TestDescription>
313 function result = utp_05
314
315 % <SyntaxDescription>
316 %
317 % Test that the polyfit method works with an input of matrices and vectors
318 % and single AOs.
319 %
320 % </SyntaxDescription>
321
322 try
323 % <SyntaxCode>
324 amat = [at1 at2 at5; at6 at5 at2];
325 avec = [at1 at2];
326 out = polyfit(at5, avec, at6, amat, at5);
327 % </SyntaxCode>
328 stest = true;
329 catch err
330 disp(err.message)
331 stest = false;
332 end
333
334 % <AlgoDescription>
335 %
336 % 1) Check we have the correct number of output objects
337 %
338 % </AlgoDescription>
339
340 atest = true;
341 if stest
342 % <AlgoCode>
343 N = find(ao.getInfo('polyfit').plists, 'N');
344 if ~isequal(numel(out), numel(amat) + numel(avec) + 3), atest = false; end
345 for jj = 1:numel(out)
346 if ~isequal(numel(out(jj).y), N+1), atest = false; end
347 end
348 % </AlgoCode>
349 else
350 atest = false;
351 end
352
353 % Return a result structure
354 result = utp_prepare_result(atest, stest, dbstack, mfilename);
355 end % END UTP_05
356
357 %% UTP_06
358
359 % <TestDescription>
360 %
361 % Tests that the polyfit method properly applies history.
362 %
363 % </TestDescription>
364 function result = utp_06
365
366 % <SyntaxDescription>
367 %
368 % Test that the result of applying the polyfit method can be processed back
369 % to an m-file.
370 %
371 % </SyntaxDescription>
372
373 try
374 % <SyntaxCode>
375 out1 = polyfit(at1, plist('rescale', 'true'));
376 out2 = polyfit(at1, plist('vector_out', false));
377 mout1 = rebuild(out1);
378 mout2 = rebuild(out2);
379 % </SyntaxCode>
380 stest = true;
381 catch err
382 disp(err.message)
383 stest = false;
384 end
385
386 % <AlgoDescription>
387 %
388 % 1) Check that the last entry in the history of 'out1' corresponds to
389 % 'polyfit'.
390 % 2) Check that the re-built object is the same object as 'out'.
391 %
392 % </AlgoDescription>
393
394 atest = true;
395 if stest
396 % <AlgoCode>
397 % Check the last step in the history of 'out1' (matrix object)
398 if ~strcmp(out1.hist.methodInfo.mname, 'polyfit'), atest = false; end
399 if ~strcmp(out2.hist.methodInfo.mname, 'polyfit'), atest = false; end
400 % Check the re-built object
401 if ~eq(mout1, out1, ple2), atest = false; end
402 if ~eq(mout2, out2, ple2), atest = false; end
403 % </AlgoCode>
404 else
405 atest = false;
406 end
407
408 % Return a result structure
409 result = utp_prepare_result(atest, stest, dbstack, mfilename);
410 end % END UTP_06
411
412 %% UTP_07
413
414 % <TestDescription>
415 %
416 % Tests that the polyfit method cannot modify the input AO.
417 %
418 % </TestDescription>
419 function result = utp_07
420
421 % <SyntaxDescription>
422 %
423 % Test that the polyfit method cannot modify the input AO by calling with no
424 % output and that the method doesn't change the input of the function
425 % notation (with a equal sign).
426 %
427 % </SyntaxDescription>
428
429 try
430 % <SyntaxCode>
431 % copy at1 to work with
432 ain = ao(at1);
433 % modify ain
434 aout = ain.polyfit();
435 ain.polyfit();
436 % </SyntaxCode>
437 stest = true;
438 catch err
439 stest = true;
440 end
441
442 % <AlgoDescription>
443 %
444 % 1) Nothing to check
445 %
446 % </AlgoDescription>
447
448 atest = true;
449 if stest
450 % <AlgoCode>
451
452 % </AlgoCode>
453 else
454 atest = false;
455 end
456
457 % Return a result structure
458 result = utp_prepare_result(atest, stest, dbstack, mfilename);
459 end % END UTP_07
460
461 %% UTP_08
462
463 % <TestDescription>
464 %
465 % Tests that the polyfit method handles units correctly.
466 %
467 % </TestDescription>
468 function result = utp_08
469
470 % <SyntaxDescription>
471 %
472 % Tests that the polyfit method handles units correctly.
473 %
474 % </SyntaxDescription>
475
476 try
477 % <SyntaxCode>
478 % Input data
479 nsecs = 100;
480 fs = 10;
481
482 u = get_random_unit();
483
484 x1 = ao(plist('tsfcn', 'randn(size(t)) + 0.02*t + 5', ...
485 'fs', fs, 'nsecs', nsecs, ...
486 'yunits', u));
487 x2 = ao(plist('tsfcn', 'randn(size(t)) + 0.02*t + 5', ...
488 'fs', fs, 'nsecs', nsecs, ...
489 'yunits', ''));
490
491 % Settings for polyfit
492 N = 2;
493 out1 = polyfit(x1, plist('N', N));
494 out2 = polyfit(x2, plist('N', N));
495
496 % </SyntaxCode>
497 stest = true;
498 catch err
499 disp(err.message)
500 stest = false;
501 end
502
503 % <AlgoDescription>
504 %
505 % 1) Check that the units in both cases are yunits/(xunits)^(j)
506 %
507 % </AlgoDescription>
508
509 atest = true;
510 if stest
511 % <AlgoCode>
512 % 1) Check that the units in both cases are yunits/(xunits)^(j)
513 for jj = 1:N+1
514 if ~isequal(out1.yunits(jj), x1.yunits./(x1.xunits).^(N+1-jj)), atest = false; end
515 end
516 for jj = 1:N+1
517 if ~isequal(out2.yunits(jj), x2.yunits./(x2.xunits).^(N+1-jj)), atest = false; end
518 end
519 % </AlgoCode>
520 else
521 atest = false;
522 end
523
524 % Return a result structure
525 result = utp_prepare_result(atest, stest, dbstack, mfilename);
526 end % END UTP_08
527
528 %% UTP_09
529
530 % <TestDescription>
531 %
532 % Tests that the ao/polyfit method agrees with MATLAB's polyfit when
533 % configured to use the same parameters.
534 %
535 % </TestDescription>
536 function result = utp_09
537
538 % <SyntaxDescription>
539 %
540 % Test that applying polyfit works on a single AO.
541 %
542 % </SyntaxDescription>
543
544 % <SyntaxCode>
545 try
546
547 % Make test AO
548 nsecs = 100;
549 fs = 10;
550
551 unit_list = unit.supportedUnits;
552 u = unit(cell2mat(utils.math.randelement(unit_list,1)));
553
554 a1 = ao(plist('nsecs', nsecs, 'fs', fs, ...
555 'tsfcn', 'polyval([3 2 1 ], t) + 1000*randn(size(t))', 'xunits', 's', 'yunits', u));
556
557 N = 4;
558 pl = plist('N', N);
559 p1 = a1.polyfit(pl.pset('rescale', true));
560 p2 = a1.polyfit(pl.pset('rescale', false));
561 stest = true;
562 catch err
563 disp(err.message)
564 stest = false;
565 end
566 % </SyntaxCode>
567
568 % <AlgoDescription>
569 %
570 % 1) Check that output agrees with the output of MATLAB's polyfit.
571 %
572 % </AlgoDescription>
573
574 % <AlgoCode>
575 atest = true;
576
577 if stest
578 % Call Matlab polyfit with rescale
579 [p,s,mu] = polyfit(a1.x, a1.y, N);
580
581 if ~isequal(p, p1.y') || ~isequal(s, find(p1.procinfo, 's')) || ~isequal(mu, find(p1.procinfo, 'mu'))
582 atest = false;
583 end
584 % Call Matlab polyfit without rescale
585 [p,s] = polyfit(a1.x, a1.y, N);
586 if ~isequal(p, p2.y') || ~isequal(s, find(p2.procinfo, 's'))
587 atest = false;
588 end
589 else
590 atest = false;
591 end
592 % </AlgoCode>
593
594 % Return a result structure
595 result = utp_prepare_result(atest, stest, dbstack, mfilename);
596 end % END UTP_09
597 end