function analyze_grooteman_results(varargin)
%ANALYZE_GROOTEMAN_RESULTS  One line description goes here.
%
%   More detailed description goes here.
%
%   Syntax:
%   varargout = analyze_grooteman_results(varargin)
%
%   Input: For <keyword,value> pairs call analyze_grooteman_results() without arguments.
%   varargin  =
%
%   Output:
%   varargout =
%
%   Example
%   analyze_grooteman_results
%
%   See also

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2012 Deltares
%       Joost den Bieman
%
%       joost.denbieman@deltares.nl
%
%       P.O. Box 177
%       2600 MH Delft
%       The Netherlands
%
%   This library is free software: you can redistribute it and/or modify
%   it under the terms of the GNU General Public License as published by
%   the Free Software Foundation, either version 3 of the License, or
%   (at your option) any later version.
%
%   This library is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%   GNU General Public License for more details.
%
%   You should have received a copy of the GNU General Public License
%   along with this library.  If not, see <http://www.gnu.org/licenses/>.
%   --------------------------------------------------------------------

% This tool is part of <a href="http://www.OpenEarth.eu">OpenEarthTools</a>.
% OpenEarthTools is an online collaboration to share and manage data and
% programming tools in an open source, version controlled environment.
% Sign up to recieve regular updates of this function, and to contribute
% your own tools.

%% Version <http://svnbook.red-bean.com/en/1.5/svn.advanced.props.special.keywords.html>
% Created: 17 Jul 2013
% Created with Matlab version: 8.1.0.604 (R2013a)

% $Id$
% $Date$
% $Author$
% $Revision$
% $HeadURL$
% $Keywords: $

%% Read Settings

OPT = struct(...
    'resultsDir',   'test_results', ...
    'saveDir',      'test_results\images', ...
    'tests',        1:14, ...
    'name',         '', ...
    'Labels',       '', ...
    'Methods',      {{'DS', 'ADIS', 'ADIS', 'ADIS'}}, ...
    'StartUp',      {{'', '', 'AxialSearch', 'AxialPoints'}} ...
    );

OPT = setproperty(OPT, varargin{:});

%% Grooteman 2011 performance

RelativeErrorPfGrooteman    = ...
    [2.4 ...
    10.8 ...
    1.7 ...
    11.3 ...
    2.1 ...
    12.4 ...
    8.4 ...
    6.0 ...
    4.4 ...
    6.0 ...
    6.3 ...
    10.1 ...
    15.6 ...
    8.0];

NumberEvaluationsGrooteman  = ...
    [136 ... 
    57 ...
    116 ...
    65 ...
    32 ...
    81 ...
    71 ...
    148 ...
    91 ...
    51 ...
    63 ...
    32 ...
    65 ...
    28];

%% Read results & plot analysis
% plot the performance of different methods for the same problem

for iTest = 1:length(OPT.tests)
    if OPT.tests(iTest) <10
        testNr  = ['0' num2str(OPT.tests(iTest))];
    else
        testNr  = num2str(OPT.tests(iTest));
    end
    
    for iMethod = 1:length(OPT.Methods)
        fileName    = [OPT.Methods{iMethod} '_results' testNr OPT.StartUp{iMethod} '.mat'];
        
%         load([OPT.resultsDir '/' fileName]);
        load([OPT.resultsDir{iMethod} '/' fileName]);
        
        if iMethod == 1
            % initialize variables
            RelativeErrorPf     = NaN(length(results),length(OPT.Methods));
            NumberEvaluations   = NaN(length(results),length(OPT.Methods));
            ErrorBeta           = NaN(length(results),length(OPT.Methods));
            Beta                = NaN(length(results),length(OPT.Methods));
        end
        
        for iSeed = 1:length(results)
            RelativeErrorPf(iSeed,iMethod)      = abs((results{iSeed}.OO_ADIS.DS_object.Pf-results{iSeed}.Exact.P_f)/results{iSeed}.Exact.P_f);
            NumberEvaluations(iSeed,iMethod)    = results{iSeed}.OO_ADIS.DS_object.LimitState.NumberExactEvaluations;
            ErrorBeta(iSeed,iMethod)            = results{iSeed}.OO_ADIS.Output.Beta-results{iSeed}.Exact.Beta;
            Beta(iSeed,iMethod)                 = results{iSeed}.OO_ADIS.Output.Beta;
        end
    end
    
    % Plot results per test
    hFig    = figure;

    hBeta   = subplot(1,3,1);
%     bar(hEval, mean(NumberEvaluations,1), 'grouped');
%     notBoxPlot(Beta,[],[],'sdline')
    plot(hBeta,median(Beta,1),'bo','MarkerFaceColor','b')
    hold on
    plot(hBeta,max(Beta,[],1),'k+')
    plot(hBeta,min(Beta,[],1),'k+')
%     boxplot(hBeta, Beta, 'labels', OPT.Labels);
    line([0.5 length(OPT.Methods)+0.5],norm_inv(1-results{end}.Exact.P_f, 0, 1)*[1 1],'color','g');
    
    set(hBeta,'XTickLabel',OPT.Labels)
    ylim(hBeta, [0 6])
    title(hBeta, ['Test ' num2str(testNr) ':\beta values']);
    
    hError  = subplot(1,3,2);
%     bar(hError, 100*mean(RelativeErrorPf,1), 'grouped');
    notBoxPlot(100*RelativeErrorPf,[],[],'sdline')
%     boxplot(hError, 100*RelativeErrorPf, 'labels', OPT.Labels)
    line([0.5 length(OPT.Methods)+0.5],RelativeErrorPfGrooteman(iTest)*[1 1])
    
    set(hError,'XTickLabel',OPT.Labels)
    ylim(hError, [0 200])
    title(hError, ['Test ' num2str(testNr) ':Relative error in P_{f} [%]']);
  
    hEval   = subplot(1,3,3);
%     bar(hEval, mean(NumberEvaluations,1), 'grouped');
    notBoxPlot(NumberEvaluations,[],[],'sdline')
%     boxplot(hEval, NumberEvaluations, 'labels', OPT.Labels)
    line([0.5 length(OPT.Methods)+0.5],NumberEvaluationsGrooteman(iTest)*[1 1])
    
    set(hEval,'XTickLabel',OPT.Labels)
    ylim(hEval, [0 1000])
    title(hEval, ['Test ' num2str(testNr) ':Mean number of exact evaluations']);

%     print('-r600','-dpng',[OPT.saveDir '\' OPT.name '_performance_test' testNr])

    Beta1   = Beta(:,1);
    Beta2   = Beta(:,2);
    Beta3   = Beta(:,3);
    Beta4   = Beta(:,4);
    
    Beta1   = Beta1(~isnan(Beta1) & ~isinf(Beta1));
    Beta2   = Beta2(~isnan(Beta2) & ~isinf(Beta2));
    Beta3   = Beta3(~isnan(Beta3) & ~isinf(Beta3));
    Beta4   = Beta4(~isnan(Beta4) & ~isinf(Beta4));
    
    dBeta1  = Beta1-results{1}.Exact.Beta;
    dBeta2  = Beta2-results{1}.Exact.Beta;
    dBeta3  = Beta3-results{1}.Exact.Beta;
    dBeta4  = Beta4-results{1}.Exact.Beta;
%     dBeta   = Beta-results{1}.Exact.Beta;
    display([OPT.Methods;OPT.StartUp])
    display('Accuracy:')
    display([median(dBeta1,1) median(dBeta2,1) median(dBeta3,1) median(dBeta4,1)])
    display('Robustness:')
    display([std(Beta1,0,1) std(Beta2,0,1) std(Beta3,0,1) std(Beta4,0,1)])
    display('Efficiency:')
    display(median(NumberEvaluations,1))
    
    savefile = [median(dBeta1,1) median(dBeta2,1) median(dBeta3,1) median(dBeta4,1); ...
        std(Beta1,0,1) std(Beta2,0,1) std(Beta3,0,1) std(Beta4,0,1); ...
        median(NumberEvaluations,1)];
    
%     save(['test' testNr '.txt'],'-ascii', 'savefile');
    xlswrite(['test' testNr '.xls'], savefile);
    
    clear RelativeErrorPf NumberEvaluations
    close(hFig);
end