%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Run many experiments of Av LJ NL3 classifier on pollen data base
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% input parameters
N_ESP = 1;
cltr = 1 : 3;
clts = 1 : 3;
ncl = length(cltr);
nclts = length(clts);
scale = 3;
Thres = 0;
for cc = 1 : ncl 
    
    N_GAUSS{cc} = 4;
    tipo{cc} = 'spherical';
    
end

comp = 8;

CMtot = zeros(nclts,nclts+1);
CMTR = zeros(nclts,nclts+1);
CMTS = zeros(nclts,nclts+1);
CMts = [];
CMtr = [];
ERts = zeros(1,N_ESP);
ERtr = zeros(1,N_ESP);
ERpts = zeros(1,N_ESP);
ERptr = zeros(1,N_ESP);

for nn = 1 : N_ESP

    fprintf(1,'@@@@@@@@@@ Experiment nr.%u @@@@@@@@@@ \n',nn)
    rand('state',sum(100*clock))
	indxtr = [];
	indxts = [];
    indxtrts = [];
	
%BACT
    tot = 600;
	ind = randperm(tot) - 1;
	indxts{1} = ind(1:100);
	indxtr{1} = setdiff([0:tot-1],indxts{1});
    indxtrts{1} = ind(101:200);
%RBC
	indxts{2} = ind(1:100);
	indxtr{2} = setdiff([0:tot-1],indxts{2});  
    indxtrts{2} = ind(101:200);
%SPRM
	indxts{3} = ind(1:100);
	indxtr{3} = setdiff([0:tot-1],indxts{3});  
    indxtrts{3} = ind(101:200);
	
    
    % TRAINING
    ris = training_ljmNL3 (cltr, indxtr, N_GAUSS, comp, scale, tipo);
    
    % TEST ERROR
    [C, ERts(nn), ERpts(nn)] = test_ljmNL3 (cltr, clts, indxts, ris, scale,Thres);
    CMts{nn} = C;

    
    [C, ERtr(nn), ERptr(nn)] = test_ljmNL3 (cltr, clts, indxtrts, ris, scale,Thres);
    CMtr{nn} = C;
    
    CMTS = CMTS + CMts{nn};
    CMTR = CMTR + CMtr{nn};
    
end
save('performance','ERpts','ERptr','ERts','ERtr','CMTR','CMTS','N_ESP','CMts','CMtr')

CMtot = CMTS/N_ESP;
erts = mean(ERpts);
ertr = mean(ERptr);
stderts = std(ERpts);
stdertr = std(ERptr);

% Plot confusion matrix

subplot(1,2,1)
set(gca,'XLim',[0 size(CMtot,2)],'YLim',[0 size(CMtot,2)],'XTick',[0:size(CMtot,1)],'YTick',[1:size(CMtot,1)]);
set(gca,'XTickLabel',{'UNK' 'BACT' 'RBC' 'SPRM'},'FontSize',8);
set(gca,'YTickLabel',{'BACT' 'RBC' 'SPRM'},'FontSize',8);
grid on;
ylabel('TRUE CLASS','FontSize',10);
xlabel('ESTIMATED CLASS','FontSize',10);
title(['av. test ER = ' num2str(erts*100,'%2.1f') '% \sigma = ' num2str(stderts*100,'%2.1f') ' (abs. ER = ' num2str(mean(ERts)*100,'%2.1f') '%)'],'FontSize',10,'FontWeight','bold');
colr = [1 0 0;
    0 1 0;
    0 0 1;
    0 1 1;
    1 0 1;
    1 1 0;
    .8 .6 .1;
    .8 .1 .6;
    0.1 0.6 .8;
    0.1 .8 0.6;
    .6 0.1 .8;
    .6 .8 0.1];

for ii=1:size(CMtot,1)
   
    col = colr(ii,:);
    
   
    for jj=0:size(CMtot,1)
        
        hold on;
        plot(jj,ii,'.','MarkerSize',sqrt(0.1+CMtot(ii,jj+1)/sum(CMtot(ii,:))*5000),'Color',col);
        if CMtot(ii,jj+1) ~= 0
            text(jj+0.1,ii+0.1,num2str(CMtot(ii,jj+1)/sum(CMtot(ii,:))*100,'%2.1f'),'Color','k')
        end
        hold off

    end
    
end
drawnow;



CMtot = CMTR/N_ESP;

% Plot confusion matrix


subplot(1,2,2)
set(gca,'XLim',[0 size(CMtot,2)],'YLim',[0 size(CMtot,2)],'XTick',[0:size(CMtot,1)],'YTick',[1:size(CMtot,1)]);
set(gca,'XTickLabel',{'UNK' 'BACT' 'RBC' 'SPRM'},'FontSize',8);
set(gca,'YTickLabel',{'BACT' 'RBC' 'SPRM'},'FontSize',8);
grid on;
ylabel('TRUE CLASS','FontSize',10);
xlabel('ESTIMATED CLASS','FontSize',10);
title(['av. train ER = ' num2str(ertr*100,'%2.1f') '% \sigma = ' num2str(stdertr*100,'%2.1f') ' (abs ER = ' num2str(mean(ERtr)*100,'%2.1f') '%)'],'FontSize',10,'FontWeight','bold');
colr = [1 0 0;
    0 1 0;
    0 0 1;
    0 1 1;
    1 0 1;
    1 1 0;
    .8 .6 .1;
    .8 .1 .6;
    0.1 0.6 .8;
    0.1 .8 0.6;
    .6 0.1 .8;
    .6 .8 0.1];

for ii=1:size(CMtot,1)
   
    col = colr(ii,:);
   
   
    for jj=0:size(CMtot,1)
        
        hold on;
        plot(jj,ii,'.','MarkerSize',sqrt(0.1+CMtot(ii,jj+1)/sum(CMtot(ii,:))*5000),'Color',col);
        if CMtot(ii,jj+1) ~= 0
            text(jj+0.1,ii+0.1,num2str(CMtot(ii,jj+1)/sum(CMtot(ii,:))*100,'%2.1f'),'Color','k')
        end
        hold off

    end
    
end
drawnow;

