% data must be loaded via SRread_script

close all
clear res p

testnC = 11;

%% Collect data
res.nC = [];%zeros([1 length(example)]);
res.nH = res.nC;
res.nS = res.nC;
res.nPU = res.nC;
res.nPS = res.nC;
res.t_domain = res.nC;
res.t_partitions = res.nC;
res.t_part = res.nC;
nS_max = 0;
nC_max = 0;
nC_count = 0;
mean_t_part = 0;
for i = 1:length(unique(ex_idx))
    i_ex = find(ex_idx==i);
    for j = 1:length(i_ex)
        res(i).nC(j) = size(data.(example{i_ex(j)}).SR_part.grouping, 2);
        res(i).nH(j) = size(data.(example{i_ex(j)}).SR_part.grouping, 1);
        res(i).nS(j) = size(data.(example{i_ex(j)}).SR_part.se, 1);
        res(i).nPU(j) = size(data.(example{i_ex(j)}).SR_part.unified.se_partitions, 1);
        res(i).nPS(j) = size(data.(example{i_ex(j)}).SR_part.simplex.se_partitions, 1);
        res(i).t_domain(j) = data.(example{i_ex(j)}).time(2);
        res(i).t_partitions(j) = data.(example{i_ex(j)}).time(3);
        res(i).t_part(j) = res(i).t_domain(j) + res(i).t_partitions(j);
        res(i).t_verify(j) = data.(example{i_ex(j)}).time(4);
        if size(nC_count, 2) >= res(i).nC(j) && size(nC_count, 1) >= i
            nC_count(i,res(i).nC(j)) = nC_count(i,res(i).nC(j)) + 1;
            mean_t_part(i,res(i).nC(j)) = mean_t_part(i,res(i).nC(j)) ...
                                        + res(i).t_part(j);
        else
            nC_count(i,res(i).nC(j)) = 1;
            mean_t_part(i,res(i).nC(j)) = res(i).t_part(j);
        end
    end
    
    nC_max = max([nC_max res(i).nC]);
    nS_max = max([nS_max res(i).nS]);
end
mean_t_part = mean_t_part ./ nC_count;

%% Plotting Complexity
figure,hold on,grid off,box on
plot(res(1).nC, res(1).nH, 'kx', 'MarkerSize', 5)
plot(res(2).nC, res(2).nH, 'kd', 'MarkerSize', 4)
plot(res(3).nC, res(3).nH, 'ko', 'MarkerSize', 4)

plot(res(1).nC, 8+10.5*(res(1).nC-1), 'r-')
plot(res(2).nC, 18/7*(res(2).nC-3)+10, 'r-')
plot(res(3).nC, 2*res(3).nC, 'r-')

%legend(ex_filter, 'Location', 'NorthWest')
xlim([0 nC_max+1]);ylim([0 50]);
xlabel('n_C');ylabel('n_H')
standardplot('nH_nC')

%% Plotting nC-Counts
figure,hold on,grid off,box on
plot(1:nC_max, sum(nC_count,1), 'k-', 'linewidth', 2, 'color', [1 1 1]*0.5)
plot(1:nC_max, nC_count(1,:)./(nC_count(1,:)~=0), 'k-x', 'MarkerSize', 5)
plot(1:nC_max, nC_count(2,:)./(nC_count(2,:)~=0), 'k-d', 'MarkerSize', 4)
plot(1:nC_max, nC_count(3,:)./(nC_count(3,:)~=0), 'k-o', 'MarkerSize', 4)
xlim([0 nC_max+1])
xlabel('nC');ylabel('nex');
standardplot('number_of_examples');

%% Fitting nC to nS fit
fit_nS = [res(1).nS'; res(2).nS'; res(3).nS'];
fit_nC = [res(1).nC'; res(2).nC'; res(3).nC'];
%p = fit(fit_nC, fit_nS, 'a*exp(b*x)')
p.a = 0.7794;
p.b = 0.5207;
testnS = p.a*exp(p.b*testnC)

%% Plotting nS vs. nC
figure,hold on,grid off,box on
fit_nC = unique(fit_nC);
plot(fit_nC, p.a*exp(p.b*fit_nC), 'k-', 'linewidth', 2, 'color', [1 1 1]*0.5)
plot(res(1).nC, res(1).nS, 'kx', 'MarkerSize', 5)
plot(res(2).nC, res(2).nS, 'kd', 'MarkerSize', 4)
plot(res(3).nC, res(3).nS, 'ko', 'MarkerSize', 4)
%legend(ex_filter)
xlim([0 nC_max+1])
xlabel('nC');ylabel('nS');
set(gca, 'yscale', 'log');
standardplot('nS_nC');

%% Fitting
fit_nS = [res(1).nS'; res(2).nS'; res(3).nS'];
fit_nPU = [res(1).nPU'; res(2).nPU'; res(3).nPU'];
%p = fit(fit_nS, fit_nPU, 'a*x')
p.a=1.406; %(proportional fit)
%p.a = 1.568;
%p.b = -5.101;
%testnPU = p.a*testnS

%% Plotting nPU vs. nS
figure,hold on,grid off,box on
fit_nS = unique(fit_nS);
plot(fit_nS, p.a*fit_nS, 'k-', 'linewidth', 2, 'color', [1 1 1]*0.5)
plot(res(1).nS, res(1).nPU, 'kx', 'MarkerSize', 5)
plot(res(2).nS, res(2).nPU, 'kd', 'MarkerSize', 4)
plot(res(3).nS, res(3).nPU, 'ko', 'MarkerSize', 4)
%xlim([0 nC_max+1])
xlabel('nS');ylabel('nPU');
set(gca, 'yscale', 'log', 'xscale', 'log');
standardplot('nPU_nS');

%% Fitting
fit_nS = [res(1).nS'; res(2).nS'; res(3).nS'];
fit_nPS = [res(1).nPS'; res(2).nPS'; res(3).nPS'];
%p = fit(fit_nS, fit_nPS, 'a*(x)^n')
p.a = 0.1115;
p.n = 2.139;


%% Plotting nPS vs. nS
figure,hold on,grid off,box on
fit_nS = unique(fit_nS);
plot(fit_nS, p.a*(fit_nS).^p.n, 'k-', 'linewidth', 2, 'color', [1 1 1]*0.5)
plot(res(1).nS, res(1).nPS, 'kx', 'MarkerSize', 5)
plot(res(2).nS, res(2).nPS, 'kd', 'MarkerSize', 4)
plot(res(3).nS, res(3).nPS, 'ko', 'MarkerSize', 4)
xlabel('nS');ylabel('nPS');ylim([1 2000]);
set(gca, 'yscale', 'log', 'xscale', 'log',...
    'ytick', [1, 10, 100, 1e3]);
standardplot('nPS_nS');

%% Fitting
%fit_nH = [res(1).nH'; res(2).nH'; res(3).nH'];
%fit_tdom = [res(1).t_domain'; res(2).t_domain'; res(3).t_domain'];
%fit_nH = res(2).nH';
%fit_tdom = res(2).t_domain';
%p = fit(res(1).nH',res(1).t_domain', 'a*exp(b*x) + c')
%p = fit(fit_nH,fit_tdom, 'a*x^n')

%% Plotting Domain Time
figure,hold on,grid off,box on
%fit_nH = unique(fit_nH);
%plot(fit_nH, p.a*exp(p.b*fit_nH+p.c), 'k-', 'linewidth', 2, 'color', [1 1 1]*0.5)
%plot(fit_nH, p.a*fit_nH.^p.n, 'k-', 'linewidth', 2, 'color', [1 1 1]*0.5)
plot(res(1).nH, res(1).t_domain, 'kx', 'MarkerSize', 5)
plot(res(2).nH, res(2).t_domain, 'kd', 'MarkerSize', 4)
plot(res(3).nH, res(3).t_domain, 'ko', 'MarkerSize', 4)
xlabel('nH');ylabel('tdom');xlim([4, 100])
set(gca, 'yscale', 'log', 'xscale', 'log');
standardplot('time_domain');

%% Fitting
%fit_nH = [res(1).nH'; res(2).nH'; res(3).nH'];
%fit_tpart = [res(1).t_partitions'; res(2).t_partitions'; res(3).t_partitions'];
%p = fit(fit_nPU, fit_tpart, 'a*x+b')
%h = @(x)(p.a*x+p.b);

%% Plotting Partitioning Time
figure,hold on,grid off,box on
%fit_nPU = unique(fit_nPU);
%plot(fit_nPU, h(fit_nPU), 'k-', 'linewidth', 2, 'color', [1 1 1]*0.5)
plot(res(1).nPU, res(1).t_partitions, 'kx', 'MarkerSize', 5)
plot(res(2).nPU, res(2).t_partitions, 'kd', 'MarkerSize', 4)
plot(res(3).nPU, res(3).t_partitions, 'ko', 'MarkerSize', 4)
xlabel('nPU');ylabel('tpart');xlim([1;2e2]);
set(gca, 'yscale', 'log', 'xscale', 'log');
standardplot('time_paritions');

%% nC to nU fit
%fit_nC = [res(1).nC; res(2).nC; res(3).nC];
%fit_nPU = [res(1).nPU; res(2).nPU; res(3).nPU];
%p = fit(fit_nC, fit_nPU, 'a*exp(b*x)');

% %% Plotting Partitioning Times
% figure,hold on
% plot(res(1).nC, res(1).t_part, 'rx')
% plot(res(2).nC, res(2).t_part, 'bd')
% plot(res(3).nC, res(3).t_part, 'k*')
% %legend(ex_filter)
% % mean times
% % plot(1:nC_max, mean_t_part(1,:), 'r-')
% % plot(1:nC_max, mean_t_part(2,:), 'b-')
% % plot(1:nC_max, mean_t_part(3,:), 'k-')
% xlabel('n_C');ylabel('time for partitioning in s');


%% Plotting Partitioning Times
%figure,hold on

%plot(res(2).nC, mean_t_part, 'kd')
%plot(res(3).nC, mean_t_part, 'k*')
%legend(ex_filter)