Mercurial > hg > ltpda
comparison testing/utp_1.1/utps/ao/utp_ao_bin_data.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_BIN_DATA a set of UTPs for the ao/bin_data method | |
2 % | |
3 % M Hueller 06-08-09 | |
4 % | |
5 % $Id: utp_ao_bin_data.m,v 1.9 2011/05/10 16:48:35 mauro Exp $ | |
6 % | |
7 | |
8 % <MethodDescription> | |
9 % | |
10 % The bin_data method of the ao class rebins aos data, on | |
11 % logarithmic scale, linear scale, or arbitrarly chosen | |
12 % | |
13 % </MethodDescription> | |
14 | |
15 function results = utp_ao_bin_data(varargin) | |
16 | |
17 % Check the inputs | |
18 if nargin == 0 | |
19 | |
20 % Some keywords | |
21 class = 'ao'; | |
22 mthd = 'bin_data'; | |
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 % Prepare once some test data | |
33 [a, S_lin,S_log] = prepare_test_data; | |
34 | |
35 % Exception list for the UTPs: | |
36 [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples(); | |
37 | |
38 % Run the tests | |
39 results = [results utp_01]; % getInfo call | |
40 results = [results utp_02(mthd, atvec, @algo_test_y, [], ple3)]; % Vector input | |
41 results = [results utp_03(mthd, atmat, @algo_test_y, [], ple3)]; % Matrix input | |
42 results = [results utp_04(mthd, at1, at2, at3, @algo_test_y, [], ple3)]; % List input | |
43 results = [results utp_05(mthd, at1, atvec, atmat, @algo_test_y, [], ple3)]; % Test with mixed input | |
44 results = [results utp_06(mthd, at1, [], ple2)]; % Test history is working | |
45 results = [results utp_07(mthd, at1, plist('neval',true), ple1)]; % Test the modify call works | |
46 results = [results utp_10(mthd, at5, at6, ple2)]; % Test output of the data | |
47 results = [results utp_11(mthd, at1, ple1)]; % Test plotinfo doesn't disappear | |
48 results = [results utp_17]; % Test units handling & parameter support | |
49 results = [results utp_18]; % Test units handling & parameter support | |
50 results = [results utp_19]; % Test units handling & parameter support | |
51 results = [results utp_20]; % Test vector with bins support | |
52 results = [results utp_21]; % Test ao with bins support | |
53 | |
54 disp('Done.'); | |
55 disp('******************************************************'); | |
56 | |
57 elseif nargin == 1 % Check for UTP functions | |
58 if strcmp(varargin{1}, 'isutp') | |
59 results = 1; | |
60 else | |
61 results = 0; | |
62 end | |
63 else | |
64 error('### Incorrect inputs') | |
65 end | |
66 | |
67 %% UTP_01 | |
68 | |
69 % <TestDescription> | |
70 % | |
71 % Tests that the getInfo call works for this method. | |
72 % | |
73 % </TestDescription> | |
74 function result = utp_01 | |
75 | |
76 | |
77 % <SyntaxDescription> | |
78 % | |
79 % Test that the getInfo call works for no sets, all sets, and each set | |
80 % individually. | |
81 % | |
82 % </SyntaxDescription> | |
83 | |
84 try | |
85 % <SyntaxCode> | |
86 % Call for no sets | |
87 io(1) = eval([class '.getInfo(''' mthd ''', ''None'')']); | |
88 % Call for all sets | |
89 io(2) = eval([class '.getInfo(''' mthd ''')']); | |
90 % Call for each set | |
91 for kk=1:numel(io(2).sets) | |
92 io(kk+2) = eval([class '.getInfo(''' mthd ''', ''' io(2).sets{kk} ''')']); | |
93 end | |
94 % </SyntaxCode> | |
95 stest = true; | |
96 catch err | |
97 disp(err.message) | |
98 stest = false; | |
99 end | |
100 | |
101 % <AlgoDescription> | |
102 % | |
103 % 1) Check that getInfo call returned an minfo object in all cases. | |
104 % 2) Check that all plists have the correct parameters. | |
105 % | |
106 % </AlgoDescription> | |
107 | |
108 atest = true; | |
109 if stest | |
110 % <AlgoCode> | |
111 % check we have minfo objects | |
112 if isa(io, 'minfo') | |
113 %%% SET 'None' | |
114 if ~isempty(io(1).sets), atest = false; end | |
115 if ~isempty(io(1).plists), atest = false; end | |
116 %%% Check all Sets | |
117 if ~any(strcmpi(io(2).sets, 'Default')), atest = false; end | |
118 if numel(io(2).plists) ~= numel(io(2).sets), atest = false; end | |
119 %%%%%%%%%% SET 'Default' | |
120 if io(3).plists.nparams ~= 7, atest = false; end | |
121 % Check key | |
122 if ~io(3).plists.isparam('method'), atest = false; end | |
123 if ~io(3).plists.isparam('xscale'), atest = false; end | |
124 if ~io(3).plists.isparam('resolution'), atest = false; end | |
125 if ~io(3).plists.isparam('xvals'), atest = false; end | |
126 if ~io(3).plists.isparam('weights'), atest = false; end | |
127 if ~io(3).plists.isparam('range'), atest = false; end | |
128 if ~io(3).plists.isparam('inherit_dy'), atest = false; end | |
129 % Check default value | |
130 if ~isequal(io(3).plists.find('method'), 'MEAN'), atest = false; end | |
131 if ~isequal(io(3).plists.find('xscale'), 'LOG'), atest = false; end | |
132 if ~isequal(io(3).plists.find('resolution'), 10), atest = false; end | |
133 if ~isequal(io(3).plists.find('xvals'), []), atest = false; end | |
134 if ~isequal(io(3).plists.find('weights'), []), atest = false; end | |
135 if ~isequal(io(3).plists.find('range'), []), atest = false; end | |
136 if ~isequal(io(3).plists.find('inherit_dy'), 'yes'), atest = false; end | |
137 % Check options | |
138 if ~isequal(io(3).plists.getOptionsForParam('method'), {'MEAN', 'MEDIAN', 'MAX', 'MIN', 'RMS', 'WMEAN'}), atest = false; end | |
139 if ~isequal(io(3).plists.getOptionsForParam('xscale'), {'LOG', 'LIN'}), atest = false; end | |
140 if ~isequal(io(3).plists.getOptionsForParam('resolution'), {10}), atest = false; end | |
141 if ~isequal(io(3).plists.getOptionsForParam('xvals'), {[]}), atest = false; end | |
142 if ~isequal(io(3).plists.getOptionsForParam('weights'), {[]}), atest = false; end | |
143 if ~isequal(io(3).plists.getOptionsForParam('range'), {[]}), atest = false; end | |
144 if ~isequal(io(3).plists.getOptionsForParam('inherit_dy'), {'yes', 'no'}), atest = false; end | |
145 end | |
146 % </AlgoCode> | |
147 else | |
148 atest = false; | |
149 end | |
150 | |
151 % Return a result structure | |
152 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
153 end % END UTP_01 | |
154 | |
155 %% UTP_17 | |
156 | |
157 % <TestDescription> | |
158 % | |
159 % Tests handling of units: | |
160 % 1) rebinning of the PSD data | |
161 % 2) rebinning of the LPSD data | |
162 % 3) compares the units of the input and output | |
163 % | |
164 | |
165 % </TestDescription> | |
166 function result = utp_17 | |
167 | |
168 % <SyntaxDescription> | |
169 % | |
170 % 1) Rebinning the data | |
171 % | |
172 % </SyntaxDescription> | |
173 | |
174 % <SyntaxCode> | |
175 try | |
176 | |
177 % Rebin the data | |
178 % Use resolution and x | |
179 resolution = utils.math.randelement([1 2 5 10],1); | |
180 bin_scale = utils.math.randelement({'LOG','LOG'},1); | |
181 bin_scale = bin_scale{:}; | |
182 | |
183 pl_bin = plist(... | |
184 'resolution', resolution, ... | |
185 'x_scale', bin_scale); | |
186 | |
187 b_lin = S_lin.bin_data(pl_bin); | |
188 b_log = S_log.bin_data(pl_bin); | |
189 | |
190 stest = true; | |
191 | |
192 catch err | |
193 disp(err.message) | |
194 stest = false; | |
195 end | |
196 % </SyntaxCode> | |
197 | |
198 % <AlgoDescription> | |
199 % | |
200 % 1) Check that (rebinned PSD yunits) equals (input PSD yunits) | |
201 % 2) Check that (rebinned PSD xunits) equals (input PSD xunits) | |
202 % 3) Check that (rebinned LPSD yunits) equals (input LPSD yunits) | |
203 % 4) Check that (rebinned LPSD xunits) equals (input LPSD xunits) | |
204 % | |
205 % </AlgoDescription> | |
206 | |
207 % <AlgoCode> | |
208 atest = true; | |
209 | |
210 if stest | |
211 if ne(S_lin.yunits, b_lin.yunits) || ne(S_lin.xunits, b_lin.xunits) | |
212 atest = false; | |
213 end | |
214 if ne(S_log.yunits, b_log.yunits) || ne(S_log.xunits, b_log.xunits) | |
215 atest = false; | |
216 end | |
217 else | |
218 atest = false; | |
219 end | |
220 % </AlgoCode> | |
221 | |
222 % Return a result structure | |
223 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
224 end % END UTP_17 | |
225 | |
226 | |
227 %% UTP_18 | |
228 | |
229 % <TestDescription> | |
230 % | |
231 % Tests handling of units: | |
232 % 1) rebinning of the PSD data | |
233 % 2) rebinning of the LPSD data | |
234 % 3) compares the units of the input and output | |
235 % | |
236 | |
237 % </TestDescription> | |
238 function result = utp_18 | |
239 | |
240 % <SyntaxDescription> | |
241 % | |
242 % 1) Rebinning the data | |
243 % | |
244 % </SyntaxDescription> | |
245 | |
246 % <SyntaxCode> | |
247 try | |
248 | |
249 % Rebin the data | |
250 % Use resolution and x | |
251 resolution = utils.math.randelement([1 2 5 10],1); | |
252 | |
253 pl_bin = plist(... | |
254 'resolution', resolution); | |
255 | |
256 b_lin = S_lin.bin_data(pl_bin); | |
257 b_log = S_log.bin_data(pl_bin); | |
258 | |
259 stest = true; | |
260 | |
261 catch err | |
262 disp(err.message) | |
263 stest = false; | |
264 end | |
265 % </SyntaxCode> | |
266 | |
267 % <AlgoDescription> | |
268 % | |
269 % 1) Check that (rebinned PSD yunits) equals (input PSD yunits) | |
270 % 2) Check that (rebinned PSD xunits) equals (input PSD xunits) | |
271 % 3) Check that (rebinned LPSD yunits) equals (input LPSD yunits) | |
272 % 4) Check that (rebinned LPSD xunits) equals (input LPSD xunits) | |
273 % | |
274 % </AlgoDescription> | |
275 | |
276 % <AlgoCode> | |
277 atest = true; | |
278 | |
279 if stest | |
280 if ne(S_lin.yunits, b_lin.yunits) || ne(S_lin.xunits, b_lin.xunits) | |
281 atest = false; | |
282 end | |
283 if ne(S_log.yunits, b_log.yunits) || ne(S_log.xunits, b_log.xunits) | |
284 atest = false; | |
285 end | |
286 else | |
287 atest = false; | |
288 end | |
289 % </AlgoCode> | |
290 | |
291 % Return a result structure | |
292 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
293 end % END UTP_18 | |
294 | |
295 | |
296 %% UTP_19 | |
297 | |
298 % <TestDescription> | |
299 % | |
300 % Tests handling of units: | |
301 % 1) rebinning of the PSD data | |
302 % 2) rebinning of the LPSD data | |
303 % 3) compares the units of the input and output | |
304 % | |
305 | |
306 % </TestDescription> | |
307 function result = utp_19 | |
308 | |
309 % <SyntaxDescription> | |
310 % | |
311 % 1) Rebinning the data | |
312 % | |
313 % </SyntaxDescription> | |
314 | |
315 % <SyntaxCode> | |
316 try | |
317 | |
318 % Rebin the data | |
319 % Use resolution and x | |
320 bin_scale = utils.math.randelement({'LOG','LOG'},1); | |
321 bin_scale = bin_scale{:}; | |
322 | |
323 pl_bin = plist(... | |
324 'x_scale', bin_scale); | |
325 | |
326 b_lin = S_lin.bin_data(pl_bin); | |
327 b_log = S_log.bin_data(pl_bin); | |
328 | |
329 stest = true; | |
330 | |
331 catch err | |
332 disp(err.message) | |
333 stest = false; | |
334 end | |
335 % </SyntaxCode> | |
336 | |
337 % <AlgoDescription> | |
338 % | |
339 % 1) Check that (rebinned PSD yunits) equals (input PSD yunits) | |
340 % 2) Check that (rebinned PSD xunits) equals (input PSD xunits) | |
341 % 3) Check that (rebinned LPSD yunits) equals (input LPSD yunits) | |
342 % 4) Check that (rebinned LPSD xunits) equals (input LPSD xunits) | |
343 % | |
344 % </AlgoDescription> | |
345 | |
346 % <AlgoCode> | |
347 atest = true; | |
348 | |
349 if stest | |
350 if ne(S_lin.yunits, b_lin.yunits) || ne(S_lin.xunits, b_lin.xunits) | |
351 atest = false; | |
352 end | |
353 if ne(S_log.yunits, b_log.yunits) || ne(S_log.xunits, b_log.xunits) | |
354 atest = false; | |
355 end | |
356 else | |
357 atest = false; | |
358 end | |
359 % </AlgoCode> | |
360 | |
361 % Return a result structure | |
362 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
363 end % END UTP_19 | |
364 | |
365 %% UTP_20 | |
366 | |
367 % <TestDescription> | |
368 % | |
369 % Tests handling of units: | |
370 % 1) rebinning of the PSD data | |
371 % 2) rebinning of the LPSD data | |
372 % | |
373 | |
374 % </TestDescription> | |
375 function result = utp_20 | |
376 | |
377 % <SyntaxDescription> | |
378 % | |
379 % 1) Rebinning the data | |
380 % | |
381 % </SyntaxDescription> | |
382 | |
383 % <SyntaxCode> | |
384 try | |
385 | |
386 % Rebin the data | |
387 % Use vector of x values | |
388 n_bins = 25; | |
389 x_vals_lin = linspace(min(S_lin.x), max(S_lin.x), n_bins); | |
390 x_vals_log = logspace(min(S_lin.x), max(S_lin.x), n_bins); | |
391 | |
392 pl_bin_lin = plist(... | |
393 'x_vals', x_vals_lin); | |
394 pl_bin_log = plist(... | |
395 'x_vals', x_vals_log); | |
396 | |
397 b_lin_lin = S_lin.bin_data(pl_bin_lin); | |
398 b_log_lin = S_log.bin_data(pl_bin_lin); | |
399 b_lin_log = S_lin.bin_data(pl_bin_log); | |
400 b_log_log = S_log.bin_data(pl_bin_log); | |
401 | |
402 stest = true; | |
403 | |
404 catch err | |
405 disp(err.message) | |
406 stest = false; | |
407 end | |
408 % </SyntaxCode> | |
409 | |
410 % <AlgoDescription> | |
411 % | |
412 % Nothing to check | |
413 % | |
414 % </AlgoDescription> | |
415 | |
416 % <AlgoCode> | |
417 atest = true; | |
418 | |
419 if stest | |
420 | |
421 else | |
422 atest = false; | |
423 end | |
424 % </AlgoCode> | |
425 | |
426 % Return a result structure | |
427 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
428 end % END UTP_20 | |
429 | |
430 %% UTP_21 | |
431 | |
432 % <TestDescription> | |
433 % | |
434 % Tests handling of units: | |
435 % 1) rebinning of the PSD data | |
436 % 2) rebinning of the LPSD data | |
437 % | |
438 | |
439 % </TestDescription> | |
440 function result = utp_21 | |
441 | |
442 % <SyntaxDescription> | |
443 % | |
444 % 1) Rebinning the data | |
445 % | |
446 % </SyntaxDescription> | |
447 | |
448 % <SyntaxCode> | |
449 try | |
450 | |
451 % Rebin the data | |
452 % Use vector of x values | |
453 n_bins = 25; | |
454 x_vals_lin = ao(linspace(min(S_lin.x), max(S_lin.x), n_bins), randn(n_bins,1), ... | |
455 plist('type', 'xydata', 'xunits', S_lin.xunits)); | |
456 x_vals_log = ao(logspace(min(S_lin.x), max(S_lin.x), n_bins), randn(n_bins,1), ... | |
457 plist('type', 'xydata', 'xunits', S_lin.xunits)); | |
458 | |
459 pl_bin_lin = plist(... | |
460 'x_vals', x_vals_lin); | |
461 pl_bin_log = plist(... | |
462 'x_vals', x_vals_log); | |
463 | |
464 b_lin_lin = S_lin.bin_data(pl_bin_lin); | |
465 b_log_lin = S_log.bin_data(pl_bin_lin); | |
466 b_lin_log = S_lin.bin_data(pl_bin_log); | |
467 b_log_log = S_log.bin_data(pl_bin_log); | |
468 | |
469 stest = true; | |
470 | |
471 catch err | |
472 disp(err.message) | |
473 stest = false; | |
474 end | |
475 % </SyntaxCode> | |
476 | |
477 % <AlgoDescription> | |
478 % | |
479 % Nothing to check | |
480 % | |
481 % </AlgoDescription> | |
482 | |
483 % <AlgoCode> | |
484 atest = true; | |
485 | |
486 if stest | |
487 | |
488 else | |
489 atest = false; | |
490 end | |
491 % </AlgoCode> | |
492 | |
493 % Return a result structure | |
494 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
495 end % END UTP_21 | |
496 | |
497 %% Local helper functions | |
498 | |
499 %% Algorithm test for UTP 02,03,04 | |
500 | |
501 function atest = algo_test_y(in, out, pli) | |
502 atest = true; | |
503 if numel(in.data.getY) < numel(out.data.getY) | |
504 atest = false; | |
505 end | |
506 if isa(in.data, 'data2D') | |
507 if numel(in.data.getX) < numel(out.data.getX) | |
508 atest = false; | |
509 end | |
510 end | |
511 end | |
512 | |
513 %% FUnction to prepare test data | |
514 function [a, S_lin, S_log] = prepare_test_data | |
515 % Build time-series test data | |
516 fs = 1; | |
517 nsecs = 86400; | |
518 sigma_distr = 4.69e-12; | |
519 mu_distr = -5.11e-14; | |
520 | |
521 % White noise | |
522 type = 'Normal'; | |
523 | |
524 a_n = ao(plist('waveform', 'noise', ... | |
525 'type', type, 'fs', fs, 'nsecs', nsecs, 'sigma', sigma_distr)); | |
526 a_const = ao(mu_distr); | |
527 a = a_n + a_const; | |
528 | |
529 % Set units and prefix from those supported | |
530 unit_list = unit.supportedUnits; | |
531 % remove the first empty unit '' from the list, because then is it | |
532 % possible that we add a prefix to an empty unit | |
533 unit_list = unit_list(2:end); | |
534 prefix_list = unit.supportedPrefixes; | |
535 a.setYunits(unit([cell2mat(utils.math.randelement(prefix_list,1)) cell2mat(utils.math.randelement(unit_list,1))])); | |
536 | |
537 % Evaluate the PSD and LPSD of the time-series data | |
538 win = specwin('BH92'); | |
539 olap = win.rov; | |
540 detrend = 0; | |
541 n_pts = nsecs*fs/10; | |
542 scale = 'PSD'; | |
543 pl_psd = plist('Win', win, 'olap', olap, ... | |
544 'Nfft', n_pts, 'order', detrend, 'scale', scale); | |
545 | |
546 S_lin = a.psd(pl_psd); | |
547 S_log = a.lpsd(pl_psd); | |
548 end | |
549 | |
550 end |