view testing/utp_1.1/utps/ao/utp_ao_confint.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_CONFINT a set of UTPs for the ao/confint method
%
% M Hewitson 06-08-08
%
% $Id: utp_ao_confint.m,v 1.2 2011/05/22 21:35:42 mauro Exp $
%

% <MethodDescription>
%
% The confint method of the ao class computes the confidence intervals for spectral estimate.
%
% </MethodDescription>

function results = utp_ao_confint(varargin)
  
  % Check the inputs
  if nargin == 0
    
    addpath(fullfile(fileparts(which(mfilename)), 'reference_files'))
    
    % Some keywords
    class   = 'ao';
    mthd    = 'confint';
    
    results = [];
    disp('******************************************************');
    disp(['****  Running UTPs for ' class '/' mthd]);
    disp('******************************************************');
    
    % Test AOs
    [at1,at2,at3,at4,at5,at6,atvec,atmat] = eval(['get_test_objects_' class]);
    
    % reference psd for rebuild
    rsp = psd(at5);
    
    % Exception list for the UTPs:
    [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples();
    
    % reference confs
    rconf = [5.7 16.0];
    
    % reference ao for dof test
    plrefdata = plist('fs', 1, 'nsecs', 1024, ...
           'tsfcn',  'randn(size(t))');
    refdata = ao(plrefdata);
    
    
    
    % Run the tests
    results = [results utp_01];    % getInfo call
    results = [results utp_06];    % Test history is working
    results = [results utp_07];    % Test against reference data
    results = [results utp_08];    % Test the data shape

    
    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>
    
    % <SyntaxCode>
    try
      % 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
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end
    % </SyntaxCode>
    
    % <AlgoDescription>
    %
    % 1) Check that getInfo call returned an minfo object in all cases.
    % 2) Check that all plists have the correct parameters.
    %
    % </AlgoDescription>
    
    % <AlgoCode>
    atest = true;
    if stest
      % 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
        %%%%%%%%%%   Default PList
        if io(3).plists.nparams ~= 4, atest = false; end
        % Check key
        if ~io(3).plists.isparam('method'), atest = false; end
        if ~io(3).plists.isparam('DataLength'), atest = false; end
        if ~io(3).plists.isparam('Conf'), atest = false; end
        if ~io(3).plists.isparam('dof'), atest = false; end
        % Check default value
        if ~isequal(io(3).plists.find('method'), 'psd'), atest = false; end
        if ~isequal(io(3).plists.find('DataLength'), paramValue.EMPTY_DOUBLE), atest = false; end
        if ~isequal(io(3).plists.find('Conf'), 95), atest = false; end
        if ~isequal(io(3).plists.find('dof'), paramValue.EMPTY_DOUBLE), atest = false; end
        % Check options
        if ~isequal(io(3).plists.getOptionsForParam('method'), {'psd','lpsd','mscohere','mslcohere'}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('DataLength'), {paramValue.EMPTY_DOUBLE}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('Conf'), {95}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('dof'), {paramValue.EMPTY_DOUBLE}), atest = false; end
      end
    else
      atest = false;
    end
    % </AlgoCode>
    
    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_01
  
  %% UTP_06
  
  % <TestDescription>
  %
  % Tests that the confint method properly applies history.
  %
  % </TestDescription>
  function result = utp_06
    
    % <SyntaxDescription>
    %
    % Test that the result of applying the confint method can be processed back
    % to an m-file.
    %
    % </SyntaxDescription>
    
    % <SyntaxCode>
    try
      out  = confint(rsp);
      mout = rebuild(out);
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end
    % </SyntaxCode>
    
    % <AlgoDescription>
    %
    % 1) Check that the last entry in the history of 'out' corresponds to
    %    'confint'.
    % 2) Check that the re-built object is the same object as 'out'.
    %
    % </AlgoDescription>
    
    % <AlgoCode>
    atest = true;
    if stest
      % Check the last step in the history of 'out'
      if ~strcmp(out.hist.methodInfo.mname, 'confint'), atest = false; end
      % Check the re-built object
      if ~eq(mout, out, ple2), atest = false; end
    else
      atest = false;
    end
    % </AlgoCode>
    
    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_06
  
  %% UTP_07
  
  % <TestDescription>
  %
  % Tests that the confint method provides correct values for spectra
  % confidence intervals.
  % Reference on D B Percival and A T Walden, Spectral Analysis for
  % Physical Applications, pg. 299
  %
  % </TestDescription>
  function result = utp_07
    
    % <SyntaxDescription>
    %
    % Test that the applying psd works on a single AO.
    %
    % </SyntaxDescription>
    
    % <SyntaxCode>
    try

      plsp = plist('win','BH92','order',1,'navs',1);
      axx = psd(refdata,plsp);
      plconf = plist('method','psd',...
        'DataLength',numel(refdata.y),...
        'Conf',95);
      cf = confint(axx,plconf);
      
      llcf = 10*log10(cf.objs{1}.y);
      laxx = 10*log10(axx.y);
      lucf = 10*log10(cf.objs{2}.y);
      
      dcf = [(laxx-llcf) (lucf-laxx)];

      
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end
    % </SyntaxCode>
    
    % <AlgoDescription>
    %
    % 1) Check that output agrees with the reference values.
    %
    % </AlgoDescription>
    
    % <AlgoCode>
    atest = true;
    TOL = 0.01;
    
    if stest
      for jj=1:size(dcf,1)
        if any((abs(dcf(jj,:) - rconf) ./ rconf) >= TOL)
          atest = false;
        end
      end
    else
      atest = false;
    end
    % </AlgoCode>
    
    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_07

  %% UTP_08

  % <TestDescription>
  %
  % Tests that the confint method keeps the data shape of the input object.
  %
  % </TestDescription>
  function result = utp_08

    % <SyntaxDescription>
    %
    % Test that the confint method keeps the data shape of the input object. The
    % input AO must be an AO with row data and an AO with column data.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      aa1  = at5.psd;
      aa2  = at6.psd;
      out1obj = confint(aa1);
      out1_1 = out1obj.getObjectAtIndex(1);
      out1_2 = out1obj.getObjectAtIndex(2);
      out1_3 = out1obj.getObjectAtIndex(3);
      out2obj = confint(aa2);
      out2_1 = out2obj.getObjectAtIndex(1);
      out2_2 = out2obj.getObjectAtIndex(2);
      out2_3 = out2obj.getObjectAtIndex(3);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the shpe of the data doesn't change.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the shape of the output data
      if size(out1_1.data.y,1) == 1, atest = false; end
      if size(out1_2.data.y,1) == 1, atest = false; end
      if size(out1_3.data.y,1) == 1, atest = false; end
      if size(out2_1.data.y,2) == 2, atest = false; end
      if size(out2_2.data.y,2) == 2, atest = false; end
      if size(out2_3.data.y,2) == 2, atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_08

  
  
  
  
  
end