view testing/utp_1.1/utps/ao/utp_ao_lscov.m @ 52:daf4eab1a51e database-connection-manager tip

Fix. Default password should be [] not an empty string
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 07 Dec 2011 17:29:47 +0100
parents 409a22968d5e
children
line wrap: on
line source

% UTP_AO_LSCOV a set of UTPs for the ao/lscov method
%
% M Hewitson 06-08-08
%
% $Id: utp_ao_lscov.m,v 1.14 2009/12/21 13:46:21 mauro Exp $
%

% <MethodDescription>
%
% The lscov method of the ao class solves a set of linear equations by
% performing a linear least-squares fit.
% It solves the problem
%        Y = HX
% where X are the parameters, Y the measurements, and H the linear
% equations relating the two.
%
% </MethodDescription>

function results = utp_ao_lscov(varargin)

  % Check the inputs
  if nargin == 0

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

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

    % Test AOs
    fs    = 10;
    nsecs = 10;
    x1 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', nsecs));
    x2 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', nsecs));
    x3 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', nsecs));
    n  = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', nsecs));

    % 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];    % Vector input
    results = [results utp_03];    % Matrix input
    results = [results utp_04];    % List input
    results = [results utp_05];    % Test history is working
    results = [results utp_06];    % Test the modify call works
    results = [results utp_07];    % Test weights usage

    c = [1 -2 3];
    y = c(1)*x1 + c(2)*x2 + c(3)*x3 + n;
    avec = [x1, x2, x3];
    results = [results utp_11(mthd, [avec y], ple1)];    % Test plotinfo doesn't disappear
    
    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 ~= 2, atest = false; end
        % Check key
        if ~io(3).plists.isparam('weights'), atest = false; end
        if ~io(3).plists.isparam('cov'), atest = false; end        
        % Check default value
        if ~isEmptyDouble(io(3).plists.find('weights')), atest = false; end
        if ~isEmptyDouble(io(3).plists.find('cov')), atest = false; end        
        % Check options
        if ~isequal(io(3).plists.getOptionsForParam('weights'), {[]}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('cov'), {[]}), 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 lscov method works with a vector of AOs as input.
  %
  % </TestDescription>
  function result = utp_02

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

    try
      % <SyntaxCode>
      c = [1 -2 3];
      y = c(1)*x1 + c(2)*x2 + c(3)*x3 + n;
      avec = [x1, x2, x3];
      out  = lscov(avec, y);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check the data type of the output
    % 2) Check that each output AO contains the correct data.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have exactly one pest with 3 parameters
      if numel(out) ~= 1 || numel(out.y) ~= 3, atest = false; end      
      if ~isa(out, 'pest'), atest = false; end
      
      % Check each output against MATLAB lscov
      C = avec;
      H = C(:).y;
      [P,STDX,MSE,COV] = lscov(H,y.y);         
      if ~isequal(out.y, P), atest = false; end                        
      if ~isequal(out.dy, STDX), atest = false; end
      if ~isequal(out.procinfo.find('MSE'),  MSE),  atest = false; end
      if ~isequal(out.cov,  COV),  atest = false; 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 lscov method works with a matrix of AOs as input.
  %
  % </TestDescription>
  function result = utp_03

    % <SyntaxDescription>
    %
    % Tests that the lscov method works with a matrix of AOs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      c = [1 -2 3 -1 2 4];
      y = c(1)*x1 + c(2)*x2 + c(3)*x3 + c(4)*x1 + c(5)*x2 + c(6)*x3 + n;
      amat = [x1, x2, x3; x1, x2, x3];
      out  = lscov(amat, y);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check the data type of the output
    % 2) Check that each output AO contains the correct data.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have exactly one pest with 6 parameters
      if numel(out) ~= 1 || numel(out.y) ~= 6, atest = false; end
      if ~isa(out, 'pest'), atest = false; end
      
      % Check each output against MATLAB lscov
      C = amat;
      H = C(:).y;
      [P,STDX,MSE,COV] = lscov(H,y.y);
      if ~isequal(out.y, P), atest = false; end
      if ~isequal(out.dy, STDX), atest = false; end
      if ~isequal(out.procinfo.find('MSE'),  MSE),  atest = false; end
      if ~isequal(out.cov,  COV),  atest = false; 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 lscov method works with a list of AOs as input.
  %
  % </TestDescription>
  function result = utp_04

    % <SyntaxDescription>
    %
    % Tests that the lscov method works with a list of AOs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      c = [1 -2 3];
      y = c(1)*x1 + c(2)*x2 + c(3)*x3 + n;
      out  = lscov(x1, x2, x3, y);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check the data type of the output
    % 2) Check that each output AO contains the correct data.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have exactly one pest with 3 parameters
      if numel(out) ~= 1 || numel(out.y) ~= 3, atest = false; end
      if ~isa(out, 'pest'), atest = false; end
      
      % Check each output against MATLAB lscov
      C = [x1, x2, x3];
      H = C(:).y;
      [P,STDX,MSE,COV] = lscov(H,y.y);
      if ~isequal(out.y, P), atest = false; end
      if ~isequal(out.dy, STDX), atest = false; end
      if ~isequal(out.procinfo.find('MSE'),  MSE),  atest = false; end
      if ~isequal(out.cov,  COV),  atest = false; 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 lscov method properly applies history.
  %
  % </TestDescription>
  function result = utp_05

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

    try
      % <SyntaxCode>
      c = [1 -2 3];
      y = c(1)*x1 + c(2)*x2 + c(3)*x3 + n;
      out  = lscov(x1, x2, x3, y);
      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
    %    'lscov'.
    % 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.hist.methodInfo.mname, 'lscov'), atest = false; end
      % The rebuilt object must be the same as 'out'
      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_05

  %% UTP_06

  % <TestDescription>
  %
  % The lscov method can not be used as a modifer method.
  %
  % </TestDescription>
  function result = utp_06

    % <SyntaxDescription>
    %
    % The lscov method can not be used as a modifer method.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      c = [1 -2 3];
      y = c(1)*x1 + c(2)*x2 + c(3)*x3 + n;
      x1.lscov(x1, x2, x3, y);
      stest = false;
      % </SyntaxCode>
    catch
      stest = true;
    end

    % <AlgoDescription>
    %
    % 1) Nothing to check.
    %
    % </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_06

  %% UTP_07

  % <TestDescription>
  %
  % Check that the lscov method uses weights for the fit.
  %
  % </TestDescription>
  function result = utp_07

    % <SyntaxDescription>
    %
    % Check that the lscov method uses weights for the fit.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      c = [1 -2 3];
      x  = 1:100;
      w2 = (x.^2 - 100.*x + 2500)/2500;
      w1(1:100) = 1;

      y = c(1)*x1 + c(2)*x2 + c(3)*x3 + n;
      
      % Setting units
      y.setYunits('V m');
      x1.setYunits('V^-1 Hz');
      x2.setYunits('V^-1 Hz');
      x3.setYunits('V^-1 Hz');

      pl1 = plist('Weights', w1);
      pl2 = plist('Weights', w2);
      pl3 = plist('Weights', ao(w2));
      
      out1 = lscov(x1, x2, x3, y);
      out2 = lscov(x1, x2, x3, y, pl1);
      out3 = lscov(x1, x2, x3, y, pl2);
      out4 = lscov(x1, x2, x3, y, pl3);
      mout1 = rebuild(out1);
      mout2 = rebuild(out2);
      mout3 = rebuild(out3);
      mout4 = rebuild(out4);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check the output data
    % 2) Check the yunits
    % 3) Check that 'out1' and 'out2' have the same data
    % 4) Check that 'out3' and 'out4' have the same data
    % 5) Check the re-built objects
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      C = [x1, x2, x3];
      H = C(:).y;
      
      % Check the output data with w1
      [P,STDX,MSE,COV] = lscov(H,y.y,w1);
      if ~isequal(out2.y, P), atest = false; end
      if ~isequal(out2.dy, STDX), atest = false; end
      if ~isequal(out2.procinfo.find('MSE'),  MSE),  atest = false; end
      if ~isequal(out2.cov,  COV),  atest = false; end
      for ii = 1:3
        if ~eq(out2.yunits(ii), unit('V m V Hz^-1')), atest = false; end
      end
      
      % Check the output data with w2
      [P,STDX,MSE,COV] = lscov(H,y.y,w2);
      if ~isequal(out3.y, P), atest = false; end
      if ~isequal(out3.dy, STDX), atest = false; end
      if ~isequal(out3.procinfo.find('MSE'),  MSE),  atest = false; end
      if ~isequal(out3.cov,  COV),  atest = false; end
      for ii = 1:3
        if ~eq(out3.yunits(ii), unit('V m V Hz^-1')), atest = false; end
      end
      
      % Check that 'out1' and 'out2' have the same data
      if ne(out1, out2, ple3), atest = false; end
      % Check that 'out3' and 'out4' have the same data
      if ne(out3, out4, ple3), atest = false; end
      
      % Run 'test[1..4].m' and check the result
      if ne(mout1, out1, ple2), atest = false; end
      if ne(mout2, out2, ple2), atest = false; end
      if ne(mout3, out3, ple2), atest = false; end
      if ne(mout4, out4, ple2), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

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

  

end