view 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
line wrap: on
line source

% UTP_AO_BIN_DATA a set of UTPs for the ao/bin_data method
%
% M Hueller 06-08-09
%
% $Id: utp_ao_bin_data.m,v 1.9 2011/05/10 16:48:35 mauro Exp $
%

% <MethodDescription>
%
% The bin_data method of the ao class rebins aos data, on
% logarithmic scale, linear scale, or arbitrarly chosen
%
% </MethodDescription>

function results = utp_ao_bin_data(varargin)
  
  % Check the inputs
  if nargin == 0
    
    % Some keywords
    class   = 'ao';
    mthd    = 'bin_data';
    
    results = [];
    disp('******************************************************');
    disp(['****  Running UTPs for ' class '/' mthd]);
    disp('******************************************************');
    
    % Test AOs
    [at1,at2,at3,at4,at5,at6,atvec,atmat] = eval(['get_test_objects_' class]);
    
    % Prepare once some test data
    [a, S_lin,S_log] = prepare_test_data;
    
    % Exception list for the UTPs:
    [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples();
    
    % Run the tests
    results = [results utp_01];    % getInfo call
    results = [results utp_02(mthd, atvec, @algo_test_y, [], ple3)];    % Vector input
    results = [results utp_03(mthd, atmat, @algo_test_y, [], ple3)];    % Matrix input
    results = [results utp_04(mthd, at1, at2, at3, @algo_test_y, [], ple3)];    % List input
    results = [results utp_05(mthd, at1, atvec, atmat, @algo_test_y, [], ple3)];    % Test with mixed input
    results = [results utp_06(mthd, at1, [], ple2)];    % Test history is working
    results = [results utp_07(mthd, at1, plist('neval',true), ple1)];    % Test the modify call works
    results = [results utp_10(mthd, at5, at6, ple2)];    % Test output of the data
    results = [results utp_11(mthd, at1, ple1)];    % Test plotinfo doesn't disappear
    results = [results utp_17];    % Test units handling & parameter support
    results = [results utp_18];    % Test units handling & parameter support
    results = [results utp_19];    % Test units handling & parameter support
    results = [results utp_20];    % Test vector with bins support
    results = [results utp_21];    % Test ao with bins support
    
    disp('Done.');
    disp('******************************************************');
    
  elseif nargin == 1 % Check for UTP functions
    if strcmp(varargin{1}, 'isutp')
      results = 1;
    else
      results = 0;
    end
  else
    error('### Incorrect inputs')
  end
  
  %% UTP_01
  
  % <TestDescription>
  %
  % Tests that the getInfo call works for this method.
  %
  % </TestDescription>
  function result = utp_01
    
    
    % <SyntaxDescription>
    %
    % Test that the getInfo call works for no sets, all sets, and each set
    % individually.
    %
    % </SyntaxDescription>
    
    try
      % <SyntaxCode>
      % Call for no sets
      io(1) = eval([class '.getInfo(''' mthd ''', ''None'')']);
      % Call for all sets
      io(2) = eval([class '.getInfo(''' mthd ''')']);
      % Call for each set
      for kk=1:numel(io(2).sets)
        io(kk+2) = eval([class '.getInfo(''' mthd ''', ''' io(2).sets{kk} ''')']);
      end
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end
    
    % <AlgoDescription>
    %
    % 1) Check that getInfo call returned an minfo object in all cases.
    % 2) Check that all plists have the correct parameters.
    %
    % </AlgoDescription>
    
    atest = true;
    if stest
      % <AlgoCode>
      % check we have minfo objects
      if isa(io, 'minfo')
        %%% SET 'None'
        if ~isempty(io(1).sets), atest = false; end
        if ~isempty(io(1).plists), atest = false; end
        %%% Check all Sets
        if ~any(strcmpi(io(2).sets, 'Default')), atest = false; end
        if numel(io(2).plists) ~= numel(io(2).sets), atest = false; end
        %%%%%%%%%%   SET 'Default'
        if io(3).plists.nparams ~= 7, atest = false; end
        % Check key
        if ~io(3).plists.isparam('method'), atest = false; end
        if ~io(3).plists.isparam('xscale'), atest = false; end
        if ~io(3).plists.isparam('resolution'), atest = false; end
        if ~io(3).plists.isparam('xvals'), atest = false; end
        if ~io(3).plists.isparam('weights'), atest = false; end
        if ~io(3).plists.isparam('range'), atest = false; end
        if ~io(3).plists.isparam('inherit_dy'), atest = false; end
        % Check default value
        if ~isequal(io(3).plists.find('method'), 'MEAN'), atest = false; end
        if ~isequal(io(3).plists.find('xscale'), 'LOG'), atest = false; end
        if ~isequal(io(3).plists.find('resolution'), 10), atest = false; end
        if ~isequal(io(3).plists.find('xvals'), []), atest = false; end
        if ~isequal(io(3).plists.find('weights'), []), atest = false; end
        if ~isequal(io(3).plists.find('range'), []), atest = false; end
        if ~isequal(io(3).plists.find('inherit_dy'), 'yes'), atest = false; end
        % Check options
        if ~isequal(io(3).plists.getOptionsForParam('method'), {'MEAN', 'MEDIAN', 'MAX', 'MIN', 'RMS', 'WMEAN'}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('xscale'), {'LOG', 'LIN'}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('resolution'), {10}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('xvals'), {[]}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('weights'), {[]}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('range'), {[]}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('inherit_dy'), {'yes', 'no'}), atest = false; end
      end
      % </AlgoCode>
    else
      atest = false;
    end
    
    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_01
  
  %% UTP_17
  
  % <TestDescription>
  %
  % Tests handling of units:
  % 1) rebinning of the PSD data
  % 2) rebinning of the LPSD data
  % 3) compares the units of the input and output
  %
  
  % </TestDescription>
  function result = utp_17
    
    % <SyntaxDescription>
    %
    % 1) Rebinning the data
    %
    % </SyntaxDescription>
    
    % <SyntaxCode>
    try
            
      % Rebin the data
      % Use resolution and x
      resolution = utils.math.randelement([1 2 5 10],1);
      bin_scale = utils.math.randelement({'LOG','LOG'},1);
      bin_scale = bin_scale{:};
      
      pl_bin = plist(...
        'resolution', resolution, ...
        'x_scale', bin_scale);
      
      b_lin = S_lin.bin_data(pl_bin);
      b_log = S_log.bin_data(pl_bin);
      
      stest = true;
      
    catch err
      disp(err.message)
      stest = false;
    end
    % </SyntaxCode>
    
    % <AlgoDescription>
    %
    % 1) Check that (rebinned PSD yunits) equals (input PSD yunits)
    % 2) Check that (rebinned PSD xunits) equals (input PSD xunits)
    % 3) Check that (rebinned LPSD yunits) equals (input LPSD yunits)
    % 4) Check that (rebinned LPSD xunits) equals (input LPSD xunits)
    %
    % </AlgoDescription>
    
    % <AlgoCode>
    atest = true;
    
    if stest
      if ne(S_lin.yunits, b_lin.yunits) || ne(S_lin.xunits, b_lin.xunits)
        atest = false;
      end
      if ne(S_log.yunits, b_log.yunits) || ne(S_log.xunits, b_log.xunits)
        atest = false;
      end
    else
      atest = false;
    end
    % </AlgoCode>
    
    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_17
  
  
  %% UTP_18
  
  % <TestDescription>
  %
  % Tests handling of units:
  % 1) rebinning of the PSD data
  % 2) rebinning of the LPSD data
  % 3) compares the units of the input and output
  %
  
  % </TestDescription>
  function result = utp_18
    
    % <SyntaxDescription>
    %
    % 1) Rebinning the data
    %
    % </SyntaxDescription>
    
    % <SyntaxCode>
    try
            
      % Rebin the data
      % Use resolution and x
      resolution = utils.math.randelement([1 2 5 10],1);      
      
      pl_bin = plist(...
        'resolution', resolution);
      
      b_lin = S_lin.bin_data(pl_bin);
      b_log = S_log.bin_data(pl_bin);
      
      stest = true;
      
    catch err
      disp(err.message)
      stest = false;
    end
    % </SyntaxCode>
    
    % <AlgoDescription>
    %
    % 1) Check that (rebinned PSD yunits) equals (input PSD yunits)
    % 2) Check that (rebinned PSD xunits) equals (input PSD xunits)
    % 3) Check that (rebinned LPSD yunits) equals (input LPSD yunits)
    % 4) Check that (rebinned LPSD xunits) equals (input LPSD xunits)
    %
    % </AlgoDescription>
    
    % <AlgoCode>
    atest = true;
    
    if stest
      if ne(S_lin.yunits, b_lin.yunits) || ne(S_lin.xunits, b_lin.xunits)
        atest = false;
      end
      if ne(S_log.yunits, b_log.yunits) || ne(S_log.xunits, b_log.xunits)
        atest = false;
      end
    else
      atest = false;
    end
    % </AlgoCode>
    
    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_18
  
  
  %% UTP_19
  
  % <TestDescription>
  %
  % Tests handling of units:
  % 1) rebinning of the PSD data
  % 2) rebinning of the LPSD data
  % 3) compares the units of the input and output
  %
  
  % </TestDescription>
  function result = utp_19
    
    % <SyntaxDescription>
    %
    % 1) Rebinning the data
    %
    % </SyntaxDescription>
    
    % <SyntaxCode>
    try
            
      % Rebin the data
      % Use resolution and x      
      bin_scale = utils.math.randelement({'LOG','LOG'},1);
      bin_scale = bin_scale{:};
      
      pl_bin = plist(...        
        'x_scale', bin_scale);
      
      b_lin = S_lin.bin_data(pl_bin);
      b_log = S_log.bin_data(pl_bin);
      
      stest = true;
      
    catch err
      disp(err.message)
      stest = false;
    end
    % </SyntaxCode>
    
    % <AlgoDescription>
    %
    % 1) Check that (rebinned PSD yunits) equals (input PSD yunits)
    % 2) Check that (rebinned PSD xunits) equals (input PSD xunits)
    % 3) Check that (rebinned LPSD yunits) equals (input LPSD yunits)
    % 4) Check that (rebinned LPSD xunits) equals (input LPSD xunits)
    %
    % </AlgoDescription>
    
    % <AlgoCode>
    atest = true;
    
    if stest
      if ne(S_lin.yunits, b_lin.yunits) || ne(S_lin.xunits, b_lin.xunits)
        atest = false;
      end
      if ne(S_log.yunits, b_log.yunits) || ne(S_log.xunits, b_log.xunits)
        atest = false;
      end
    else
      atest = false;
    end
    % </AlgoCode>
    
    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_19
  
  %% UTP_20
  
  % <TestDescription>
  %
  % Tests handling of units:
  % 1) rebinning of the PSD data
  % 2) rebinning of the LPSD data  
  %
  
  % </TestDescription>
  function result = utp_20
    
    % <SyntaxDescription>
    %
    % 1) Rebinning the data
    %
    % </SyntaxDescription>
    
    % <SyntaxCode>
    try
            
      % Rebin the data
      % Use vector of x values      
      n_bins = 25;
      x_vals_lin = linspace(min(S_lin.x), max(S_lin.x), n_bins);
      x_vals_log = logspace(min(S_lin.x), max(S_lin.x), n_bins);      
      
      pl_bin_lin = plist(...        
        'x_vals', x_vals_lin);
      pl_bin_log = plist(...        
        'x_vals', x_vals_log);
      
      b_lin_lin = S_lin.bin_data(pl_bin_lin);
      b_log_lin = S_log.bin_data(pl_bin_lin);
      b_lin_log = S_lin.bin_data(pl_bin_log);
      b_log_log = S_log.bin_data(pl_bin_log);
      
      stest = true;
      
    catch err
      disp(err.message)
      stest = false;
    end
    % </SyntaxCode>
    
    % <AlgoDescription>
    %
    % Nothing to check
    %
    % </AlgoDescription>
    
    % <AlgoCode>
    atest = true;
    
    if stest
      
    else
      atest = false;
    end
    % </AlgoCode>
    
    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_20
  
  %% UTP_21
  
  % <TestDescription>
  %
  % Tests handling of units:
  % 1) rebinning of the PSD data
  % 2) rebinning of the LPSD data
  %
  
  % </TestDescription>
  function result = utp_21
    
    % <SyntaxDescription>
    %
    % 1) Rebinning the data
    %
    % </SyntaxDescription>
    
    % <SyntaxCode>
    try
            
      % Rebin the data
      % Use vector of x values      
      n_bins = 25;
      x_vals_lin = ao(linspace(min(S_lin.x), max(S_lin.x), n_bins), randn(n_bins,1), ...
        plist('type', 'xydata', 'xunits', S_lin.xunits));
      x_vals_log = ao(logspace(min(S_lin.x), max(S_lin.x), n_bins), randn(n_bins,1), ...
        plist('type', 'xydata', 'xunits', S_lin.xunits));   
      
      pl_bin_lin = plist(...        
        'x_vals', x_vals_lin);
      pl_bin_log = plist(...        
        'x_vals', x_vals_log);
      
      b_lin_lin = S_lin.bin_data(pl_bin_lin);
      b_log_lin = S_log.bin_data(pl_bin_lin);
      b_lin_log = S_lin.bin_data(pl_bin_log);
      b_log_log = S_log.bin_data(pl_bin_log);
      
      stest = true;
      
    catch err
      disp(err.message)
      stest = false;
    end
    % </SyntaxCode>
    
    % <AlgoDescription>
    %
    % Nothing to check
    %
    % </AlgoDescription>
    
    % <AlgoCode>
    atest = true;
    
    if stest
      
    else
      atest = false;
    end
    % </AlgoCode>
    
    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_21
  
  %% Local helper functions
  
  %% Algorithm test for UTP 02,03,04
  
  function atest = algo_test_y(in, out, pli)
    atest = true;
    if numel(in.data.getY) < numel(out.data.getY)
      atest = false;
    end
    if isa(in.data, 'data2D')
      if numel(in.data.getX) < numel(out.data.getX)
        atest = false;
      end
    end
  end
  
  %% FUnction to prepare test data
  function [a, S_lin, S_log] = prepare_test_data
    % Build time-series test data
    fs = 1;
    nsecs = 86400;
    sigma_distr = 4.69e-12;
    mu_distr = -5.11e-14;
    
    % White noise
    type = 'Normal';
    
    a_n = ao(plist('waveform', 'noise', ...
      'type', type, 'fs', fs, 'nsecs', nsecs, 'sigma', sigma_distr));
    a_const = ao(mu_distr);
    a = a_n + a_const;
    
    % Set units and prefix from those supported
    unit_list = unit.supportedUnits;
    % remove the first empty unit '' from the list, because then is it
    % possible that we add a prefix to an empty unit
    unit_list = unit_list(2:end);
    prefix_list = unit.supportedPrefixes;
    a.setYunits(unit([cell2mat(utils.math.randelement(prefix_list,1)) cell2mat(utils.math.randelement(unit_list,1))]));
    
    % Evaluate the PSD and LPSD of the time-series data
    win = specwin('BH92');
    olap = win.rov;
    detrend = 0;
    n_pts = nsecs*fs/10;
    scale = 'PSD';
    pl_psd = plist('Win', win, 'olap', olap, ...
      'Nfft', n_pts, 'order', detrend, 'scale', scale);
    
    S_lin = a.psd(pl_psd);
    S_log = a.lpsd(pl_psd);
  end
  
end