view testing/utp_1.1/utps/ao/utp_ao_sDomainFit.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_SDOMAINFIT a set of UTPs for the ao/sDomainFit method
%
% L Ferraioli 16-02-09
%
% $Id: utp_ao_sDomainFit.m,v 1.11 2011/04/14 12:59:36 luigi Exp $
%

% <MethodDescription>
%
% The sDomainFit method of the ao class fit a model (parfrac object) to
% fsdata.
%
% </MethodDescription>

function results = utp_ao_sDomainFit(varargin)

  % Check the inputs
  if nargin == 0

    % Some keywords
    class   = 'ao';
    mthd    = 'sDomainFit';

    results = [];
    disp('******************************************************');
    disp(['****  Running UTPs for ' class '/' mthd]);
    disp('******************************************************');

    % Test AOs
%     [at1,at2,at3,at4,at5,at6,atvec,atmat] = get_test_objects_ao;
    
    % Exception list for the UTPs:
    [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples();

    % ---- Building fsdata test objects

    pl_data1 = plist('fsfcn', '0.01./(0.01+f)', 'f1', 1e-6, 'f2', 5, 'nf', 100);
    a1 = ao(pl_data1);

    pl_data2 = plist('fsfcn', '0.001./(0.1+f)', 'f1', 1e-6, 'f2', 5, 'nf', 100);
    a2 = ao(pl_data2);

    pl_data3 = plist('fsfcn', '(1e-3./(f).^2 + 1e3./(0.001+f)).*1e-9', 'f1', 1e-6, 'f2', 5, 'nf', 100);
    a3 = ao(pl_data3);

    pl_data4 = plist('fsfcn', '(1e-4./(f).^2 + 1e3./(0.01+f)).*1e-10', 'f1', 1e-6, 'f2', 5, 'nf', 100);
    a4 = ao(pl_data4);

    pl_data5 = plist('fsfcn', '(1e5.*f.^2 + 1e3./(0.01+f)).*1e-8', 'f1', 1e-6, 'f2', 5, 'nf', 100);
    a5 = ao(pl_data5);
    
    a1.setName;
    a2.setName;
    a3.setName;
    a4.setName;
    a5.setName;
    
    av = [a1 a2 a3];
    am = [a1 a2; a3 a4];
    
    % ----- Building a simple test model
    f = logspace(-6,log10(5),30);
    res = [0.7 0.2+0.01i 0.2-0.01i];
    poles = [0.5 0.1+0.07i 0.1-0.07i];
    tmod = parfrac(plist('res',res,'poles',poles,'dir',0,'name','test model'));
    rtmod = resp(tmod,plist('f',f.'));
    
    % ----- Buildin a standard plist
    tol = 0.5;
    plstd = plist('AutoSearch','on',...
      'StartPoles',[],...
      'StartPolesOpt','clog',...
      'maxiter',60,...
      'minorder',3,...
      'maxorder',45,...
      'weights',[],...
      'weightparam','abs',...
      'CONDTYPE','MSE',...
      'FITTOL',1e-3,...
      'MSEVARTOL',1e-2,...
      'Plot','off',...
      'ForceStability','off',...
      'CheckProgress','off',...
      'direct term','off');

    % Run the tests
    results = [results utp_01];    % getInfo call
    results = [results utp_02];    % Vector input
    results = [results utp_03];    % Matrix input
    results = [results utp_04];    % List input
    results = [results utp_05];    % Test with mixed input
    results = [results utp_06];    % Test history is working
    results = [results utp_07];    % Test the modify call does not work
    
%     results = [results utp_08];    % Test input data shape == output data
%     shape - this does not work because of the output properties of the
%     function

    results = [results utp_09];    % Test output of the data
    results = [results utp_10];    % Test the fit give the correct coefficients

    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 ~= 16, atest = false; end
        % Check key
        if ~io(3).plists.isparam('autosearch'), atest = false; end
        if ~io(3).plists.isparam('startpoles'), atest = false; end
        if ~io(3).plists.isparam('startpolesopt'), atest = false; end
        if ~io(3).plists.isparam('maxiter'), atest = false; end
        if ~io(3).plists.isparam('minorder'), atest = false; end
        if ~io(3).plists.isparam('maxorder'), atest = false; end
        if ~io(3).plists.isparam('weights'), atest = false; end
        if ~io(3).plists.isparam('weightparam'), atest = false; end
        if ~io(3).plists.isparam('condtype'), atest = false; end
        if ~io(3).plists.isparam('fittol'), atest = false; end
        if ~io(3).plists.isparam('msevartol'), atest = false; end
        if ~io(3).plists.isparam('plot'), atest = false; end
        if ~io(3).plists.isparam('forcestability'), atest = false; end
        if ~io(3).plists.isparam('direct term'), atest = false; end
        if ~io(3).plists.isparam('checkprogress'), atest = false; end
        if ~io(3).plists.isparam('delay'), atest = false; end
        % Check default value
        if ~isequal(io(3).plists.find('autosearch'), 'on'), atest = false; end
        if ~isEmptyDouble(io(3).plists.find('startpoles')), atest = false; end
        if ~isequal(io(3).plists.find('startpolesopt'), 'clog'), atest = false; end
        if ~isequal(io(3).plists.find('maxiter'), 50), atest = false; end
        if ~isequal(io(3).plists.find('minorder'), 2), atest = false; end
        if ~isequal(io(3).plists.find('maxorder'), 20), atest = false; end
        if ~isEmptyDouble(io(3).plists.find('weights')), atest = false; end
        if ~isequal(io(3).plists.find('weightparam'), 'abs'), atest = false; end
        if ~isequal(io(3).plists.find('condtype'), 'MSE'), atest = false; end
        if ~isequal(io(3).plists.find('fittol'), .001), atest = false; end
        if ~isequal(io(3).plists.find('msevartol'), .01), atest = false; end
        if ~isequal(io(3).plists.find('plot'), 'off'), atest = false; end
        if ~isequal(io(3).plists.find('forcestability'), 'off'), atest = false; end
        if ~isequal(io(3).plists.find('direct term'), 'off'), atest = false; end
        if ~isequal(io(3).plists.find('checkprogress'), 'off'), atest = false; end
        if ~isEmptyDouble(io(3).plists.find('delay')), atest = false; end
        % Check options
        if ~isequal(io(3).plists.getOptionsForParam('autosearch'), {'on', 'off'}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('startpoles'), {[]}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('startpolesopt'), {'real', 'clog', 'clin'}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('maxiter'), {50}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('minorder'), {2}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('maxorder'), {20}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('weights'), {[]}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('weightparam'), {'ones', 'abs', 'sqrt'}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('condtype'), {'MSE', 'RLD', 'RSF'}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('fittol'), {.001}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('msevartol'), {.01}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('plot'), {'on', 'off'}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('forcestability'), {'on', 'off'}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('direct term'), {'on', 'off'}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('checkprogress'), {'on', 'off'}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('delay'), {[]}), 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_02

  % <TestDescription>
  %
  % Tests that the sDomainFit method works with a vector of AOs as input.
  %
  % </TestDescription>
  function result = utp_02

    % <SyntaxDescription>
    %
    % Test that the sDomainFit method works for a vector of AOs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      
      out = sDomainFit(av, plstd);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the number of elements in 'out' is the same as in 'av'
    % 2) Check that each output AO contains the correct data.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if ~isequal(numel(out.objs), numel(av)), atest = false; end
      % Check each output against the absolute value of the input
      for kk=1:numel(out.objs)
        % Check the fitted ao(fsdata) objects
        if isa(av(kk).data, 'fsdata')
          % check that the output data responses are accurate to the
          % prescribed accuracy
          mse = out.objs(kk).procinfo.find('FIT_MSE');
          if mse(end)>tol, atest = false; end
        else
          % Check the other objects (they must be empty)
          if ~isempty([out.objs(kk).filters.a]), atest = false; end
          if ~isempty([out.objs(kk).filters.b]), atest = false; end
        end
      end
      % </AlgoCode>
    else
      atest = false;
    end

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

  %% UTP_03

  % <TestDescription>
  %
  % Tests that the sDomainFit method works with a matrix of AOs as input.
  %
  % </TestDescription>
  function result = utp_03

    % <SyntaxDescription>
    %
    % Test that the sDomainFit method works for a matrix of AOs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      out = sDomainFit(am, plstd);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the number of elements in 'out' is the same as in 'am'
    % 2) Check that each output AO contains the correct data.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if ~isequal(numel(out.objs), numel(am)), atest = false; end
      % Check each output against the absolute value of the input
      for kk=1:numel(out.objs)
        % Check the fitted ao(fsdata) objects
        if isa(am(kk).data, 'fsdata')
          % check that the output data responses are accurate to the
          % prescribed accuracy
          mse = out.objs(kk).procinfo.find('FIT_MSE');
          if mse(end)>tol, atest = false; end
        else
          % Check the other objects (they must be empty)
          if ~isempty([out.objs(kk).filters.a]), atest = false; end
          if ~isempty([out.objs(kk).filters.b]), atest = false; end
        end
      end
      % </AlgoCode>
    else
      atest = false;
    end

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

  %% UTP_04

  % <TestDescription>
  %
  % Tests that the sDomainFit method works with a list of AOs as input.
  %
  % </TestDescription>
  function result = utp_04

    % <SyntaxDescription>
    %
    % Test that the sDomainFit method works for a list of AOs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      out = sDomainFit(a1,a2,a3, plstd);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the number of elements in 'out' is the same as in
    %    input.
    % 2) Check that each output AO contains the correct data.
    %
    % </AlgoDescription>

    atest = true;
    aoin = [a1,a2,a3];
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if ~isequal(numel(out.objs), numel(aoin)), atest = false; end
      % Check each output against the absolute value of the input
      for kk=1:numel(out.objs)
        % Check the fitted ao(fsdata) objects
        if isa(aoin(kk).data, 'fsdata')
          % check that the output data responses are accurate to the
          % prescribed accuracy
          mse = out.objs(kk).procinfo.find('FIT_MSE');
          if mse(end)>tol, atest = false; end
        else
          % Check the other objects (they must be empty)
          if ~isempty([out.objs(kk).filters.a]), atest = false; end
          if ~isempty([out.objs(kk).filters.b]), atest = false; end
        end
      end
      % </AlgoCode>
    else
      atest = false;
    end

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

  %% UTP_05

  % <TestDescription>
  %
  % Tests that the sDomainFit method works with a mix of different shaped AOs as
  % input.
  %
  % </TestDescription>
  function result = utp_05

    % <SyntaxDescription>
    %
    % Test that the sDomainFit method works with an input of matrices and vectors
    % and single AOs.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      out = sDomainFit(a5,[a1 a2;a3 a4],plstd);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the number of elements in 'out' is the same as in
    %    input.
    % 2) Check that each output AO contains the correct data.
    %
    % </AlgoDescription>

    atest = true;
    aoin  = [a5,reshape([a1 a2;a3 a4],1,[])];
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if ~isequal(numel(out.objs), numel(aoin)), atest = false; end
      % Check each output against the absolute value of the input
      for kk=1:numel(out.objs)
        % Check the fitted ao(fsdata) objects
        if isa(aoin(kk).data, 'fsdata')
          % check that the output data responses are accurate to the
          % prescribed accuracy
          mse = out.objs(kk).procinfo.find('FIT_MSE');
          if mse(end)>tol, atest = false; end
        else
          % Check the other objects (they must be empty)
          if ~isempty([out.objs(kk).filters.a]), atest = false; end
          if ~isempty([out.objs(kk).filters.b]), atest = false; end
        end
      end
      % </AlgoCode>
    else
      atest = false;
    end

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

  %% UTP_06

  % <TestDescription>
  %
  % Tests that the sDomainFit method properly applies history.
  %
  % </TestDescription>
  function result = utp_06

    % <SyntaxDescription>
    %
    % Test that the result of applying the sDomainFit method can be processed back.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      out = sDomainFit(a1,plstd);
      mout = rebuild(out);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the last entry in the history of 'out' corresponds to
    %    'sDomainFit'.
    % 2) Check that the re-built object is the same object as the input.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the last step in the history of 'out'
      if ~strcmp(out(1).hist.methodInfo.mname, 'sDomainFit'), atest = false; end
      % Check the re-built object
      if ~eq(mout, out, ple2), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

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

  %% UTP_07

  % <TestDescription>
  %
  % sDomainFit cannot modify the input AO.
  %
  % </TestDescription>
  function result = utp_07

    % <SyntaxDescription>
    %
    % Test that the sDomainFit method can modify the input AO by calling with no
    % output and that the method doesn't change the input of the function
    % notation (with a equal sign).
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      % copy a3 to work with
      amodi = ao(a3);
      aeq = ao(a3);
      amodi.sDomainFit(plstd);
      out = aeq.sDomainFit(plstd);
      % </SyntaxCode>
      stest = false;
    catch err
      disp(err.message)
      stest = true;
    end

    % <AlgoDescription>
    %
    % 1) Nothing to do.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % </AlgoCode>
    else
      atest = false;
    end

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

%   %% UTP_08
% 
%   % <TestDescription>
%   %
%   % Test the shape of the output. - Does not make sense for sDomainFit
%   % becauses the output are miir filters
%   %
%   % </TestDescription>
%   function result = utp_08
% 
%     % <SyntaxDescription>
%     %
%     % Test that the sDomainFit 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>
%       out1 = sDomainFit(a2,plstd);
%       out2 = sDomainFit(a1,plstd);
%       % </SyntaxCode>
%       stest = true;
%     catch err
%       disp(err.message)
%       stest = false;
%     end
% 
%     % <AlgoDescription>
%     %
%     % 1) Check that the shape of the data doesn't change.
%     %
%     % </AlgoDescription>
% 
%     atest = true;
%     if stest
%       % <AlgoCode>
%       % Check the shape of the output data
%       if size(out1.data.x) ~= size(at5.data.x), atest = false; end
%       if size(out1.data.y) ~= size(at5.data.y), atest = false; end
%       if size(out2.data.x) ~= size(at6.data.x), atest = false; end
%       if size(out2.data.y) ~= size(at6.data.y), atest = false; end
%       % </AlgoCode>
%     else
%       atest = false;
%     end
% 
%     % Return a result structure
%     result = utp_prepare_result(atest, stest, dbstack, mfilename);
%   end % END UTP_08

  %% UTP_09

  % <TestDescription>
  %
  % Check that the sDomainFit method pass back the output objects to a
  % single variable correctly.
  %
  % </TestDescription>
  function result = utp_09

    % <SyntaxDescription>
    %
    % Call the method with a list of output variables and with a single output
    % variable. Additionaly check that the rebuild method works on the output.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
%       [o1, o2] = sDomainFit(a1, a2, plstd);
      o3  = sDomainFit(a1, a2, plstd);
%       mout1 = rebuild(o1);
%       mout2 = rebuild(o2);
      mout3 = rebuild(o3);
%       o31 = o3.index(1);
%       o32 = o3.index(2);
%       mout31 = rebuild(o31);
%       mout32 = rebuild(o32);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the output contains the right number of objects
    % 2) Check that the 'rebuild' method produces the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the number of outputs
%       if numel(o1) ~=1, atest = false; end
%       if numel(o2) ~=1, atest = false; end
%       if numel(o3) ~=2, atest = false; end
      % Check the rebuilding of the object
%       if ~eq(o1, mout1, ple2), atest = false; end
%       if ~eq(o2, mout2, ple2), atest = false; end
%       if ~eq(o3(1), mout3(1), ple2), atest = false; end
%       if ~eq(o3(2), mout3(2), ple2), atest = false; end
%       if ~eq(o31, mout31, ple2), atest = false; end
%       if ~eq(o32, mout32, ple2), atest = false; end
      if ~eq(o3, mout3, ple4), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

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

  % <TestDescription>
  %
  % Tests that the sDomainFit method return the correct coefficients
  %
  % </TestDescription>
  function result = utp_10

    % <SyntaxDescription>
    %
    % Test that the sDomainFit method works with an input of matrices and vectors
    % and single AOs.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      tol = 0.5;
      plstd2 = plist('AutoSearch','off',...
        'StartPoles',[],...
        'StartPolesOpt','clog',...
        'maxiter',60,...
        'minorder',3,...
        'maxorder',3,...
        'weights',[],...
        'weightparam','abs',...
        'CONDTYPE','MSE',...
        'FITTOL',1e-3,...
        'MSEVARTOL',1e-2,...
        'Plot','off',...
        'ForceStability','off',...
        'CheckProgress','off',...
        'direct term','off');
      out = sDomainFit(rtmod,plstd2);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the number of elements in 'out' is the same as in
    %    input.
    % 2) Check that output contains the correct coefficients.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if ~isequal(numel(out), numel(rtmod)), atest = false; end
      % Check the fitted ao(fsdata) objects
      if isa(rtmod.data, 'fsdata')
        % check that fit coefficients corresponds to real coefficients
        % within a tolerance
        if any(abs(tmod.res - out.res)>1e-4), atest = false; end
        if any(abs(tmod.poles - out.poles)>1e-4), atest = false; end
      else
        % Check the other objects (they must be empty)
        if ~isempty(out.res), atest = false; end
        if ~isempty(out.poles), atest = false; end
      end
      % </AlgoCode>
    else
      atest = false;
    end

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

end