%% For test without reference image, use exptype=1

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%-----------------------------                  GLOBAL CONSTANTS         --------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Maximum number of retargeting methods 
%%%--------------------------------------------------------------------------------------------------------------------------

N_MAX = 8;

%% Maximum number of attributes
%%%--------------------------------------------------------------------------------------------------------------------------

ATRIB_MAX = 9;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%-----------------------------                  DATABASE QUERYS         -----------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Open connection
mysql('open','localhost','root','');

% Choose database
mysql('use survey');


%% Number of participants performing the paired comparison test with a reference image;
%%%--------------------------------------------------------------------------------------------------------------------------

[users] = mysql('SELECT count(*) FROM sessions s where ended is not null and expid is not null and exptype=0 and id<=597');

%% Number of fulfilled iterations;
%%%--------------------------------------------------------------------------------------------------------------------------
Iteraciones_completas_teoricas = (users - mod(users, 70))/70;
%%%------------------------------------------------------------------------

Results_Categoria=zeros(ATRIB_MAX, 9);

Results_37Imagenes=zeros(1, 5);

TotalVotosCat1=zeros(N_MAX,1);
TotalVotosCat2=zeros(N_MAX,1);
TotalVotosCat3=zeros(N_MAX,1);
TotalVotosCat4=zeros(N_MAX,1);
TotalVotosCat5=zeros(N_MAX,1);
TotalVotosCat6=zeros(N_MAX,1);
TotalVotosCat7=zeros(N_MAX,1);
TotalVotosCat8=zeros(N_MAX,1);
TotalVotosCat9=zeros(N_MAX,1);

ComparisonsCateg1=zeros(N_MAX,N_MAX);
ComparisonsCateg2=zeros(N_MAX,N_MAX);
ComparisonsCateg3=zeros(N_MAX,N_MAX);
ComparisonsCateg4=zeros(N_MAX,N_MAX);
ComparisonsCateg5=zeros(N_MAX,N_MAX);
ComparisonsCateg6=zeros(N_MAX,N_MAX);
ComparisonsCateg7=zeros(N_MAX,N_MAX);
ComparisonsCateg8=zeros(N_MAX,N_MAX);
ComparisonsCateg9=zeros(N_MAX,N_MAX);

Comparisons37Imagenes=zeros(N_MAX,N_MAX);


%% Set of [num_imgs] images [Imagenes] to be tested
%-------------------------------------------------------------------------------------------------------------------
    
[Imagenes] = mysql(['SELECT DISTINCT did FROM experiments_judges']);
[num_imgs] = mysql(['SELECT count(*) FROM '...
                                          '(SELECT DISTINCT did FROM experiments_judges) as t']);
                                  
Results=zeros(num_imgs,15);      
TotalVotosFinales=zeros(N_MAX,1);
TotalVotosCompletas=zeros(N_MAX,1);

for imgn_en_juego=1:num_imgs
    
    img = Imagenes(imgn_en_juego);
    
    % Image attributes
    %-------------------------------------------------------------------------------------------------------------------

    query = ['SELECT a.id from images i, datasets d, image_attributes ia, attributes a '...
            'where d.id= ',num2str(img), ' AND d.imid=i.id AND i.id=ia.imid AND ia.aid=a.id AND a.enabled = 1 AND a.id<999'];
    
    [Atributos_img]  = mysql(query);
    
    query = ['select count(*) from ( ', query, ' ) as t'];
    
    [num_atrib] =  mysql(query);
    
    clear query;
    

%%%--------------------------------------------------------------------------------------------------------------------------
%%%-----------------------------------             FOR EACH IMAGE       -----------------------------------------------------
%%%--------------------------------------------------------------------------------------------------------------------------

    %% m = number of test subjects performing the paired comparison test; n = number of objects in the test set
       
    % Number of retargeting results for the image (n)
    %-------------------------------------------------------------------------------------------------------------------
     query = ['select count(distinct opid) from results where did= ',num2str(img)];
     [n] = mysql(query);
     clear query;
      
    query =['SELECT name as operator, id as opid '...
                                    'FROM operators o, '...
                                    '(SELECT opid '...
                                    ' FROM comparisons c, results r '...
                                    ' WHERE c.did = r.did '...
                                            'AND r.did = ' ,num2str(img), ' AND (c.res1 = r.id OR c.res2 = r.id) '...
                                    ' GROUP BY opid) t '...
                               'WHERE o.id = t.opid'];
                       
    [name_operator, opid] = mysql(query);

   
    if n==8
   
        query = ['SELECT s.id '...
                     'FROM sessions s,experiments_judges ej '...
                     'WHERE s.expid = ej.expid AND did =' ,num2str(img), ' AND s.ended IS NOT NULL '...
                     'and s.exptype = 0 order by s.started, ej.expid'];
    else
        query = ['SELECT s.id '...
                     'FROM sessions s,experiments_judges ej '...
                     'WHERE s.expid = ej.expid AND did =' ,num2str(img), ' AND s.ended IS NOT NULL '...
                     'and s.exptype = 0 order by ej.expid'];
                 
    end                 
     

    [jueces] = mysql(query);

    % (m) = Real number of test subjects performing the paired comparison test
    % for this image 
    %-------------------------------------------------------------------------------------------------------------------
    query = ['SELECT COUNT(*) FROM (' , query , ') as t'];

    [m] = mysql(query);
    
    % Each pair is compared by the same number [m_comportado] of
    % participants.
    %--------------------------------------------------------------------------------------------------------------
        
    switch n 

        case 8 %(n= 8; m=7)
            m_min = 7;
            m_comportado = 3;
        case 7 %(n= 7; m=3)
            m_min = 3;
            m_comportado = 2;
        case 6 %(n= 6; m=5)
            m_min = 5;
            m_comportado = 4;
        case 5 %(n= 5; m=6)
            m_min = 6;
            m_comportado = 3;
                
    end

    aux = m - mod(m,m_min*Iteraciones_completas_teoricas);
    aux= aux - mod(aux, 1);
    
    while aux==0
        Iteraciones_completas_teoricas = Iteraciones_completas_teoricas - 1;
        aux = m - mod(m,m_min*Iteraciones_completas_teoricas);
        aux= aux - mod(aux, 1);
    end
    
    [m]=aux;
    Iteraciones_completas = m/m_min;
    Iteraciones_completas = Iteraciones_completas - mod(Iteraciones_completas, 1);


    Iteraciones_completas_teoricas = (users - mod(users, 70))/70;
  
    clear query m_min; 
                      
    %%%-------------------------------------------------------------------------------------------------------------------------
    %%%-------------------------------------------------------------------------------------------------------------------------
    ComparisonsImagen=zeros(N_MAX,N_MAX);
    Coefs=zeros(m,3);
    
    DTotal =0;
    
    for auxiliar=1:m

        juez = jueces(auxiliar);
   
        %%%---------------------------------------------------------------------------------------------------------------------
        %%%-------------------------------           FOR EACH PARTICIPANT      -------------------------------------------------
        %%%---------------------------------------------------------------------------------------------------------------------
 
        % Number of votes (gamma) 
        %-------------------------------------------------------------------------------------------------------------------
 
         query=['SELECT selected, resul1+resul2-selected AS unselected, COUNT(*) '...
                        'FROM (SELECT r1.opid AS selected,r2.opid AS resul1,r3.opid AS resul2 '...
                                      'FROM comparisons AS c,results AS r1, results AS r2, results AS r3, sessions s '...
                                      'WHERE c.did=r1.did AND c.did=r2.did AND c.did=r3.did '...
                                             'AND c.selected=r1.id AND c.res1=r2.id AND c.res2=r3.id '...
                                             'AND s.expid IS NOT NULL AND s.ended IS NOT NULL '...
                                             'AND s.id=c.sid AND s.id= ' ,num2str(juez), ' '...
                                             'AND c.did= ' ,num2str(img), ' '...
                                             'AND s.exptype = 0 '...
                               ')AS t '...
                'GROUP BY selected,unselected'];
   
   
        dim = ['SELECT COUNT(selected) FROM ( ', query, ' ) as w'];
    
        [fila, columna, gamma] = mysql(query);
    
        [dimension] = mysql(dim);
        
        Results(imgn_en_juego, 13)= dimension;

        clear query dim;
        
        %%%---------------------------------------------------------------------------------------------------------------------
        %%%---------------------------------------------------------------------------------------------------------------------
   
         ComparisonsJuez=zeros(N_MAX,N_MAX);
        
        for i=1:dimension
    
            ComparisonsJuez(fila(i,1), columna(i,1))= ComparisonsJuez(fila(i,1), columna(i,1)) + gamma(i,1);
    
        end    

        clear i;

        ComparisonsImagen =  ComparisonsImagen + ComparisonsJuez;
        
        %% Check ComparisonsJuez is balanced

        error=0;
		for i=1:N_MAX
            for j=1:N_MAX
                if (i~=j)
                    if (ComparisonsJuez(i,j)+ ComparisonsJuez(j,i)~=1) & (ComparisonsJuez(i,j)+ ComparisonsJuez(j,i)~=0) 
                        error=1;
                    end
                end
            end
		end 
        
		if error
			display('ERROR comparisonsJuez unbalanced for image did:');
			display(img);
            display('for observer:');
			display(auxiliar);
            display(ComparisonsJuez);
        end
        
        clear fila columna gamma dimension; 
         
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %%%----------------------------------     CIRCULAR TRIADS (d)      -----------------------------------------------
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
        t=0;

        for i=1:n

            origen=i;
            vuelta=zeros(n,1);
            salida=zeros(n,1);

            for j=1:8

                if ComparisonsJuez(j, origen) ~= 0 
                    vuelta(j)= j;
                end
 
                if ComparisonsJuez(origen, j) ~= 0 
                    salida(j)= j;
                end

            end

            for k=1:n
                for h=1:n

                    if ((salida(k)~= 0) & (vuelta(h)~= 0) & (ComparisonsJuez(salida(k), vuelta(h)) ~= 0))
                        t=t+1;    
                        cubtriada(t,1)=origen^3+salida(k)^3+vuelta(h)^3;
                    end  
                end
            end

        end

        clear i j k h origen vuelta salida;

       
        aux=zeros(1071,1);
        d=0;

        for i=1:t

            contenido=cubtriada(i,1);

            if (aux(contenido,1) == 0) 

                d=d+1;
                aux(contenido,1) = 1;

            end

        end    

        clear t i contenido cubtriada aux;
        
        % The maximum number of circular triads, being half the variation from maximum to minimum of T 
        % (the maximum of T corresponding to the ranking case in which there are no inconsistences) is: 
        %  [(n^3- 4n)/24]  --> if n is even 
        %  [(n3 -n)/24]  --> if n is odd.

        
            if mod(n, 2)==0
                Tmax = (n^3- 4*n)/24;
                Tmin = 0;
             else    
                Tmax = (n^3- n)/24;
                Tmin = 0;
            end    

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %%%%%--------------------------            COEFFICIENT OF CONSISTENCY (dseta)  ----------------------------------%%%%%
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        if mod(n, 2)==0
            dseta = 1 - (24*d / ((n^3) - (4*n)));
        else    
            dseta = 1 - (24*d / ((n^3) - (n)));
        end    
        
        Coefs(auxiliar,1)=auxiliar;
        Coefs(auxiliar,2)=d;
        Coefs(auxiliar,3)=dseta;

        DTotal=DTotal+d;        
        
        %%%-------------------------------------------------------------------------------------------------------------------
        %%%-------------------------------------------------------------------------------------------------------------------
        
    end
    
    DSetaMedio=0;
    
    for i=1:m
        DSetaMedio= DSetaMedio + Coefs(i,3);;
    end
    
    DSetaMedio= DSetaMedio/m;
    
    clear i;   
    
 
    clear auxiliar dseta;

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%-------------------------------             GRAPHICS         ------------------------------------------------------
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
    TotalVotosImagen=zeros(n,1);
    TotalVotosAux=zeros(N_MAX,1);

    for i=1:N_MAX
        
        for j=1:N_MAX
            
            TotalVotosAux(i,1)=TotalVotosAux(i,1) + ComparisonsImagen(i,j);
            
        end
        
        TotalVotosFinales(i,1)= TotalVotosFinales(i,1) + TotalVotosAux(i,1);
        
        if n==N_MAX
            
            TotalVotosCompletas(i,1)= TotalVotosCompletas(i,1) + TotalVotosAux(i,1);

            for k=1:num_atrib

                switch Atributos_img(k)
                
                    case 1
                        TotalVotosCat1(i,1) = TotalVotosCat1(i,1) + TotalVotosAux(i,1);
                    case 2
                        TotalVotosCat2(i,1) = TotalVotosCat2(i,1) + TotalVotosAux(i,1);
                    case 3
                        TotalVotosCat3(i,1) = TotalVotosCat3(i,1) + TotalVotosAux(i,1);
                    case 4
                        TotalVotosCat4(i,1) = TotalVotosCat4(i,1) + TotalVotosAux(i,1);
                    case 5
                        TotalVotosCat5(i,1) = TotalVotosCat5(i,1) + TotalVotosAux(i,1);
                    case 6
                        TotalVotosCat6(i,1) = TotalVotosCat6(i,1) + TotalVotosAux(i,1);
                    case 7
                        TotalVotosCat7(i,1) = TotalVotosCat7(i,1) + TotalVotosAux(i,1);
                    case 8
                        TotalVotosCat8(i,1) = TotalVotosCat8(i,1) + TotalVotosAux(i,1);
                    case 9
                        TotalVotosCat9(i,1) = TotalVotosCat9(i,1) + TotalVotosAux(i,1);
                        
                end        
               
            end    

        end 

    end    

    clear i j k;
    
    j=1;
    for i=1:N_MAX
        if opid(j,1) == i
            TotalVotosImagen(j,1)=TotalVotosAux(i,1);
            j=j+1;
        end    
    end   
    clear i j;
    
    Total=0;
    for i=1:N_MAX
        Total= Total+ TotalVotosAux(i,1);
    end     
    
    Results(imgn_en_juego, 14)= Total;

    clear i j TotalVotosAux Total;


%     for i=1:n
%        Opid_MethodName_Votes(i,1)= num2cell(opid(i,1));
%        Opid_MethodName_Votes(i,2)= name_operator(i,1);
%        Opid_MethodName_Votes(i,3)= num2cell(TotalVotosImagen(i,1));
%      end
%     clear i;
% 
    % Name of current image
    
    query=['SELECT name FROM images i, datasets d WHERE d.id=' ,num2str(img), ' AND d.imid=i.id']; 
    [nombre_imagen]= mysql(query);
    clear query;
    
%      figure
% 
%      subplot (2, 1, 1), bar(TotalVotosImagen), xlabel('Operator'), ylabel('Nr times operator selected'), title(nombre_imagen)
%      subplot (2, 1, 2), pie(TotalVotosImagen), legend(name_operator), title('Total')
  
    
     clear nombre_imagen name_operator opid; 

    %%%-------------------------------------------------------------------------------------------------------------------------
    %%%-------------------------------------------------------------------------------------------------------------------------
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%------------------------------               STATISTICS         -----------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


        % Sigma is the sum of the number of agreements between pairs of judges
        
		Sigma=0;
		
		for i=1:N_MAX
            
            for j=1:N_MAX
            
                if i~=j 
                
                    if ComparisonsImagen(i,j)>=2 
                
                        Sigma=Sigma +  nchoosek(ComparisonsImagen(i,j),2);
                                           
                    end
                
                end
                
            end   
            
		end    
		clear i j;

        
		%%----------------------------------------------------------------------------------------------------------------------
		%%----------------------------------------------------------------------------------------------------------------------
		
						
		% u: coefficient of agreement.
       
         switch n 
            
            case 8 %(n= 8; m=7)
                t = 3;
            case 7 %(n= 7; m=3)
                t = 2;
            case 6 %(n= 6; m=5)
                t = 4;
            case 5 %(n= 5; m=6)
                t = 3; 
                
         end         
          
        
		 t = t * Iteraciones_completas;
         u =((2*Sigma)/( nchoosek(t,2) * nchoosek(n,2))) - 1;
         
		%%----------------------------------------------------------------------------------------------------------------------
		%%----------------------------------------------------------------------------------------------------------------------
		
		Ji_2=(4/(t-2)) * (Sigma - (0.5* nchoosek(n,2)*nchoosek(t,2)* ((t-3)/(t-2))));
		
		ni= (nchoosek(n,2) * t * (t-1)) / ((t-2)^2);
		clear t;
		raiz=sqrt(2*Ji_2) - sqrt(2*ni - 1);

		%%----------------------------------------------------------------------------------------------------------------------
		%%----------------------------------------------------------------------------------------------------------------------


	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%----------------------------------------        TEST SIGNIFICANCE         -----------------------------------------------
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %% W(n, alfa) ; alfa = 0,01
    
     switch n
        
        case 5  %(n= 5; m=6)
            W=4.604166667;
            m_recalc = 3;
            
        case 6  %(n= 6; m=5)
            W=4.758333333;
            m_recalc = 4;
            
        case 7  %(n= 7; m=3) 
            W=4.883333333;
            m_recalc = 2;
            
        case 8  %(n= 8; m=7)
            W=4.988461538;
            m_recalc = 3;
    end    
    
    %% Rc     

     m_recalc = m_recalc * Iteraciones_completas;
     
     Rc= (sqrt(m_recalc*n) * W + 0.5)/2;

%%--------------------------------------------------------------------------------------------------------------------------
%%--------------------------------------------------------------------------------------------------------------------------

%%%--------------------------------------------------------------------------------------------------------------------------
%%%--------------------------------        STORAGE FOR EACH IMAGE   ---------------------------------------------
%%%--------------------------------------------------------------------------------------------------------------------------



% Results ==>
% (DID image, n retargeting methods, m observers, Tmin (minimum #triads),
% Tmax (maximum #triads), DTotal (total #triads), DSetaMedio (Coef.
% consistency, Sigma, u (Coef. agreement), Ji_2, ni, raiz (sqrt(2*Ji_2) - sqrt(2*ni - 1)), 
% #votes/observer, #votes/image, Rc)

Results(imgn_en_juego, 1)= img;
Results(imgn_en_juego, 2)= n;
Results(imgn_en_juego, 3)= m;

Results(imgn_en_juego, 4)= Tmin;
Results(imgn_en_juego, 5)= Tmax;
Results(imgn_en_juego, 6)= DTotal;
Results(imgn_en_juego, 7)= DSetaMedio;

Results(imgn_en_juego, 8)= Sigma;
Results(imgn_en_juego, 9)= u;
Results(imgn_en_juego, 10)= Ji_2;
Results(imgn_en_juego, 11)= ni;
Results(imgn_en_juego, 12)= raiz;
Results(imgn_en_juego, 15)= Rc;


%% Check ComparisonsImagen is balanced

error=0;
for i=1:N_MAX
    for j=1:N_MAX
        if (i~=j)
            switch n
                case 5
                    if (ComparisonsImagen(i,j)+ ComparisonsImagen(j,i)~=3*Iteraciones_completas) & (ComparisonsImagen(i,j)+ ComparisonsImagen(j,i)~=0)
                        error=1;
                    end

               case 6
                    if (ComparisonsImagen(i,j)+ ComparisonsImagen(j,i)~=4*Iteraciones_completas) & (ComparisonsImagen(i,j)+ ComparisonsImagen(j,i)~=0) 
                        error=1;
                    end

              case 7
                    if (ComparisonsImagen(i,j)+ ComparisonsImagen(j,i)~=2*Iteraciones_completas) & (ComparisonsImagen(i,j)+ ComparisonsImagen(j,i)~=0) 
                        error=1;
                    end

              case 8
                    if (ComparisonsImagen(i,j)+ ComparisonsImagen(j,i)~=3*Iteraciones_completas) & (ComparisonsImagen(i,j)+ ComparisonsImagen(j,i)~=0) 
                        error=1;
                    end
                
            end  
        end
    end
end 
if error
	display('ERROR--> comparisonsImagen is not balanced for image did:');
	display(img);
	display(ComparisonsImagen);
end



% %%%--------------------------------------------------------------------------------------------------------------------------
% %%%--------------------------------------------------------------------------------------------------------------------------


% We won't use images which don't have results for every retargeting
% methods

if n==N_MAX
    
   
    Comparisons37Imagenes= Comparisons37Imagenes + ComparisonsImagen;
    
        
    for i=1:num_atrib
        
        categ=Atributos_img(i);
        
        Results_Categoria(categ, 1) = Results_Categoria(categ, 1) + 1;
        Results_Categoria(categ, 2) = Results_Categoria(categ, 2) + Results(imgn_en_juego, 7);
        Results_Categoria(categ, 3) = Results_Categoria(categ, 3) + Results(imgn_en_juego, 9);
        
        switch categ
            
            case 1
                ComparisonsCateg1 = ComparisonsCateg1 + ComparisonsImagen;
            case 2
                ComparisonsCateg2 = ComparisonsCateg2 + ComparisonsImagen;
            case 3
                ComparisonsCateg3 = ComparisonsCateg3 + ComparisonsImagen;
            case 4
                ComparisonsCateg4 = ComparisonsCateg4 + ComparisonsImagen;
            case 5
                ComparisonsCateg5 = ComparisonsCateg5 + ComparisonsImagen;
            case 6
                ComparisonsCateg6 = ComparisonsCateg6 + ComparisonsImagen;
            case 7
                ComparisonsCateg7 = ComparisonsCateg7 + ComparisonsImagen;
            case 8
                ComparisonsCateg8 = ComparisonsCateg8 + ComparisonsImagen;
            case 9
                ComparisonsCateg9 = ComparisonsCateg9 + ComparisonsImagen;
                
        end         
                
                
              
    end    

end 

clear num_atrib categ i;

clear jueces juez m n img ComparisonsImagen TotalVotosImagen ;

clear u Sigma Ji_2 ni raiz S_Gamma2 S_Gamma;

clear  d dseta Tmin Tmax DTotal DSetaMedio; 
clear  ComparisonsJuez Coefs;


end
% %%%--------------------------------------------------------------------------------------------------------------------------
% %%%--------------------------------------------------------------------------------------------------------------------------

for categ=1:ATRIB_MAX
        
        Results_Categoria(categ, 2) = Results_Categoria(categ, 2) / Results_Categoria(categ, 1);
        Results_Categoria(categ, 3) = Results_Categoria(categ, 3) / Results_Categoria(categ, 1);

        switch categ
            
            case 1
                ComparisonsAux = ComparisonsCateg1;
            case 2
                ComparisonsAux = ComparisonsCateg2;
            case 3
                ComparisonsAux = ComparisonsCateg3;
            case 4
                ComparisonsAux = ComparisonsCateg4;
            case 5
                ComparisonsAux = ComparisonsCateg5;
            case 6
                ComparisonsAux = ComparisonsCateg6;
            case 7
                ComparisonsAux = ComparisonsCateg7;
            case 8
                ComparisonsAux = ComparisonsCateg8;
            case 9
                ComparisonsAux = ComparisonsCateg9;
                
        end         
        
		SigmaCateg=0;
		
		for i=1:N_MAX
            
            for j=1:N_MAX
            
                if i~=j 
                
                    if ComparisonsAux(i,j)>=2 
                
                        SigmaCateg=SigmaCateg +  nchoosek(ComparisonsAux(i,j),2);
                                           
                    end
                
                end
                
            end   
            
		end    
		clear i j;

        
		%%--------------------------------------------------------------------------------------------------------------------
		%%--------------------------------------------------------------------------------------------------------------------

         n=8;
		 t= 3 * Iteraciones_completas * Results_Categoria(categ, 1);
         Results_Categoria(categ, 4)=((2*SigmaCateg)/( nchoosek(t,2) * nchoosek(n,2))) - 1;
         Results_Categoria(categ, 5)=SigmaCateg;
         
         		%%%-----------------------------------------------------------------------------------------------------------
	
		Ji_2=(4/(t-2)) * (SigmaCateg - (0.5* nchoosek(n,2)*nchoosek(t,2)* ((t-3)/(t-2))));
		
		ni= (nchoosek(n,2) * t * (t-1)) / ((t-2)^2);
		clear t;
		
		raiz=sqrt(2*Ji_2) - sqrt(2*ni - 1);

        Results_Categoria(categ, 6)=Ji_2;
        Results_Categoria(categ, 7)=ni;
        Results_Categoria(categ, 8)=raiz;
        
        clear Ji_2 ni raiz;
% %%%--------------------------------------------------------------------------------------------------------------------------
% %%%--------------------------------------------------------------------------------------------------------------------------


end    

clear categ;

% %%%--------------------------------------------------------------------------------------------------------------------------
% %%%--------------------------------------------------------------------------------------------------------------------------
        
		SigmaGlobal=0;
		
		for i=1:N_MAX
            
            for j=1:N_MAX
            
                if i~=j 
                
                    if Comparisons37Imagenes(i,j)>=2 
                
                        SigmaGlobal=SigmaGlobal+  nchoosek(Comparisons37Imagenes(i,j),2);
                                           
                    end
                
                end
                
            end   
            
		end    
		clear i j;

        
		%%----------------------------------------------------------------------------------------------------------------------
		%%----------------------------------------------------------------------------------------------------------------------
		
         n=8;
        %% # images = 37
		 t= 3 * 37 * Iteraciones_completas;
         
         Results_37Imagenes(1, 1)=((2*SigmaGlobal)/( nchoosek(t,2) * nchoosek(n,2))) - 1;
         Results_37Imagenes(1, 2)=SigmaGlobal;
         
        %%%---------------------------------------------------------------------------------------------------------------------
       
		%% Ji_2:
        
		Results_37Imagenes(1, 3)= (4/(t-2)) * (SigmaGlobal - (0.5* nchoosek(n,2)*nchoosek(t,2)* ((t-3)/(t-2))));
		 
        %% Ni: 
        
		Results_37Imagenes(1, 4)= (nchoosek(n,2) * t * (t-1)) / ((t-2)^2);
		clear t;
        
        %% Raiz
        
        Results_37Imagenes(1, 5)= sqrt(2*Results_37Imagenes(1, 3)) - sqrt(2*Results_37Imagenes(1, 4) - 1);
        
% % %%%--------------------------------------------------------------------------------------------------------------------------
% % %%%--------------------------------------------------------------------------------------------------------------------------

 query =['SELECT name as operator '...
                                    'FROM operators o, '...
                                    '(SELECT opid '...
                                    ' FROM comparisons c, results r '...
                                    ' WHERE c.did = r.did '...
                                            'AND (c.res1 = r.id OR c.res2 = r.id) '...
                                    ' GROUP BY opid) t '...
                               'WHERE o.id = t.opid'];
                       
 [name_operator] = mysql(query);
 
 mysql('close');  
 
 figure

     subplot (2, 1, 1), bar(TotalVotosFinales), xlabel('Operator'), ylabel('Nr times operator selected'), title('TotalAmount')
     subplot (2, 1, 2), pie(TotalVotosFinales), legend(name_operator), title('TotalAmount')

 figure

     subplot (2, 1, 1), bar(TotalVotosCompletas), xlabel('Operator'), ylabel('Nr times operator selected'), title('TotalAmount')
     subplot (2, 1, 2), pie(TotalVotosCompletas), legend(name_operator), title('TotalAmount')

 figure

     subplot (2, 1, 1), bar(TotalVotosCat1), xlabel('Operator'), ylabel('Nr times operator selected'), title('Lines/Edges')
     subplot (2, 1, 2), pie(TotalVotosCat1), legend(name_operator), title('TotalAmount')

  figure

     subplot (2, 1, 1), bar(TotalVotosCat2), xlabel('Operator'), ylabel('Nr times operator selected'), title('Faces/People')
     subplot (2, 1, 2), pie(TotalVotosCat2), legend(name_operator), title('TotalAmount')

  figure

     subplot (2, 1, 1), bar(TotalVotosCat3), xlabel('Operator'), ylabel('Nr times operator selected'), title('Texture')
     subplot (2, 1, 2), pie(TotalVotosCat3), legend(name_operator), title('TotalAmount')
     
%   figure
% 
%      subplot (2, 1, 1), bar(TotalVotosCat4), xlabel('Operator'), ylabel('Nr times operator selected'), title('Random Texture')
%      subplot (2, 1, 2), pie(TotalVotosCat4), legend(name_operator), title('TotalAmount')

  figure

     subplot (2, 1, 1), bar(TotalVotosCat5), xlabel('Operator'), ylabel('Nr times operator selected'), title('Foreground Objects')
     subplot (2, 1, 2), pie(TotalVotosCat5), legend(name_operator), title('TotalAmount')

  figure

     subplot (2, 1, 1), bar(TotalVotosCat6), xlabel('Operator'), ylabel('Nr times operator selected'), title('Geometric Structures')
     subplot (2, 1, 2), pie(TotalVotosCat6), legend(name_operator), title('TotalAmount')

  figure

     subplot (2, 1, 1), bar(TotalVotosCat7), xlabel('Operator'), ylabel('Nr times operator selected'), title('Symmetry')
     subplot (2, 1, 2), pie(TotalVotosCat7), legend(name_operator), title('TotalAmount')

%   figure
% 
%      subplot (2, 1, 1), bar(TotalVotosCat8), xlabel('Operator'), ylabel('Nr times operator selected'), title('Textual Elements')
%      subplot (2, 1, 2), pie(TotalVotosCat8), legend(name_operator), title('TotalAmount')
% 
%   figure
% 
%      subplot (2, 1, 1), bar(TotalVotosCat9), xlabel('Operator'), ylabel('Nr times operator selected'), title('Animals')
%      subplot (2, 1, 2), pie(TotalVotosCat9), legend(name_operator), title('TotalAmount')
     

 for i=1:N_MAX
       Opid_MethodName_TotalVotes(i,1)= name_operator(i,1);
       Opid_MethodName_TotalVotes(i,2)= num2cell(TotalVotosFinales(i,1));
 end
 
 for i=1:N_MAX
       Opid_MethodName_TotalVotes_ImagsFull(i,1)= name_operator(i,1);
       Opid_MethodName_TotalVotes_ImagsFull(i,2)= num2cell(TotalVotosCompletas(i,1));
 end
 
 for i=1:ATRIB_MAX
     
       if i == 1
           
           Opid_MethodName_TotalVotes_Atrib(i,1)={'Operator'};
           Opid_MethodName_TotalVotes_Atrib(i,2)={'Lines/Edges'};
           Opid_MethodName_TotalVotes_Atrib(i,3)={'Faces/People'};
           Opid_MethodName_TotalVotes_Atrib(i,4)={'Texture'};
           Opid_MethodName_TotalVotes_Atrib(i,5)={'Rdm.Text'};
           Opid_MethodName_TotalVotes_Atrib(i,6)={'Foreground Objects'};
           Opid_MethodName_TotalVotes_Atrib(i,7)={'Geometric Structures'};
           Opid_MethodName_TotalVotes_Atrib(i,8)={'Symmetry'};
           Opid_MethodName_TotalVotes_Atrib(i,9)={'Text.Elmnts'};
           Opid_MethodName_TotalVotes_Atrib(i,10)={'Animals'};
           
       else
           
           Opid_MethodName_TotalVotes_Atrib(i,1)= name_operator(i-1,1);
           Opid_MethodName_TotalVotes_Atrib(i,2)= num2cell(TotalVotosCat1(i-1,1));
           Opid_MethodName_TotalVotes_Atrib(i,3)= num2cell(TotalVotosCat2(i-1,1));
           Opid_MethodName_TotalVotes_Atrib(i,4)= num2cell(TotalVotosCat3(i-1,1));
           Opid_MethodName_TotalVotes_Atrib(i,5)= num2cell(TotalVotosCat4(i-1,1));
           Opid_MethodName_TotalVotes_Atrib(i,6)= num2cell(TotalVotosCat5(i-1,1));
           Opid_MethodName_TotalVotes_Atrib(i,7)= num2cell(TotalVotosCat6(i-1,1));
           Opid_MethodName_TotalVotes_Atrib(i,8)= num2cell(TotalVotosCat7(i-1,1));
           Opid_MethodName_TotalVotes_Atrib(i,9)= num2cell(TotalVotosCat8(i-1,1));
           Opid_MethodName_TotalVotes_Atrib(i,10)= num2cell(TotalVotosCat9(i-1,1));
           
       end    
 end

 
 
 clear i;

	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%----------------------------------------        TEST SIGNIFICANCE         -----------------------------------------------
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    m_nuevo= 3 * Iteraciones_completas;
    n=8;
    W=4.988461538;
        
 	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    GrupoSigAtrib=zeros(10,8);

    %% grupoAtrib vary between 1 and ATRIB_MAX + 1
     
    for grupoAtrib=1:10

        Aux=zeros(n,2);
        
        switch grupoAtrib
            
            case 1
 
				for i=1:n
                    Aux(i,1)= TotalVotosCat1(i,1);
                    Aux(i,2)= TotalVotosCat1(i,1) * 10 +i;
				end
                
                Rc= (sqrt(m_nuevo* Results_Categoria(grupoAtrib, 1) *n) * W + 0.5)/2;
                Results_Categoria(grupoAtrib, 9)= Rc;
                 
            case 2
 
				for i=1:n
                    Aux(i,1)= TotalVotosCat2(i,1);
                    Aux(i,2)= TotalVotosCat2(i,1) * 10 +i;
				end
                
                Rc= (sqrt(m_nuevo* Results_Categoria(grupoAtrib, 1) *n) * W + 0.5)/2;
                Results_Categoria(grupoAtrib, 9)= Rc;
                
            case 3
 
				for i=1:n
                    Aux(i,1)= TotalVotosCat3(i,1);
                    Aux(i,2)= TotalVotosCat3(i,1) * 10 +i;
				end
                
                Rc= (sqrt(m_nuevo* Results_Categoria(grupoAtrib, 1) *n) * W + 0.5)/2;
                Results_Categoria(grupoAtrib, 9)= Rc;
   
            case 4
 
				for i=1:n
                    Aux(i,1)= TotalVotosCat4(i,1);
                    Aux(i,2)= TotalVotosCat4(i,1) * 10 +i;
				end
                
                Rc= (sqrt(m_nuevo* Results_Categoria(grupoAtrib, 1) *n) * W + 0.5)/2;
                Results_Categoria(grupoAtrib, 9)= Rc;

            case 5
 
				for i=1:n
                    Aux(i,1)= TotalVotosCat5(i,1);
                    Aux(i,2)= TotalVotosCat5(i,1) * 10 +i;
				end
                
                Rc= (sqrt(m_nuevo* Results_Categoria(grupoAtrib, 1) *n) * W + 0.5)/2;
                Results_Categoria(grupoAtrib, 9)= Rc;

            case 6
 
				for i=1:n
                    Aux(i,1)= TotalVotosCat6(i,1);
                    Aux(i,2)= TotalVotosCat6(i,1) * 10 +i;
				end
                
                Rc= (sqrt(m_nuevo* Results_Categoria(grupoAtrib, 1) *n) * W + 0.5)/2;
                Results_Categoria(grupoAtrib, 9)= Rc;

            case 7
 
				for i=1:n
                    Aux(i,1)= TotalVotosCat7(i,1);
                    Aux(i,2)= TotalVotosCat7(i,1) * 10 +i;
				end
                
                Rc= (sqrt(m_nuevo* Results_Categoria(grupoAtrib, 1) *n) * W + 0.5)/2;
                Results_Categoria(grupoAtrib, 9)= Rc;

            case 8
 
				for i=1:n
                    Aux(i,1)= TotalVotosCat8(i,1);
                    Aux(i,2)= TotalVotosCat8(i,1) * 10 +i;
				end
                
                Rc= (sqrt(m_nuevo* Results_Categoria(grupoAtrib, 1) *n) * W + 0.5)/2;
                Results_Categoria(grupoAtrib, 9)= Rc;

            case 9
 
				for i=1:n
                    Aux(i,1)= TotalVotosCat9(i,1);
                    Aux(i,2)= TotalVotosCat9(i,1) * 10 +i;
				end
                
                Rc= (sqrt(m_nuevo* Results_Categoria(grupoAtrib, 1) *n) * W + 0.5)/2;
                Results_Categoria(grupoAtrib, 9)= Rc;
                
            case 10
 
				for i=1:n
                    Aux(i,1)= TotalVotosCompletas(i,1);
                    Aux(i,2)= TotalVotosCompletas(i,1) * 10 +i;
				end
                
                Rc= (sqrt(m_nuevo* 37 *n) * W + 0.5)/2;
                Results_37Imagenes(1, 6)= Rc;
                    
                
        end              

		Aux=sort(Aux);
		clear i;
		memo=0;
		metodo=1;
        abortar=0;
        
		while abortar == 0 & metodo <= n 
		
            i=metodo;
            seguir=1;
            
            while seguir==1 & (i <= n)
            
                if metodo ~= i 
                    
                    if (abs(Aux(metodo,1)-Aux(i,1))) <= Rc
                        
                        if GrupoSigAtrib(grupoAtrib,metodo) == 0 
                            GrupoSigAtrib(grupoAtrib,metodo)=(mod(Aux(metodo,2),10))*10 + mod(Aux(i,2),10);                    
                        else                    
                            GrupoSigAtrib(grupoAtrib,metodo)=GrupoSigAtrib(grupoAtrib,metodo)*10 + mod(Aux(i,2),10);
                        end
                        
                    else
		
                        if (memo == mod(GrupoSigAtrib(grupoAtrib,metodo),10)) 
                            GrupoSigAtrib(grupoAtrib,metodo)=0;                    
                        else
                           memo = mod(GrupoSigAtrib(grupoAtrib,metodo),10); 
                        end 
                        
                        seguir=0;
                        
                    end
                    
               else
                    
                    GrupoSigAtrib(grupoAtrib,metodo)=(mod(Aux(metodo,2),10));
                    
               end

               if (i)==n & (mod(GrupoSigAtrib(grupoAtrib,metodo),10) ==  mod(Aux(i,2),10))
                   abortar=1;
               end   
               i=i+1;
               
                   
                
            end
            metodo=metodo+1;
		end   

    % Ranking Storage
    %----------------------------------------------------------------------------------------------------
    ClasifGrupoAtributo=[''];
    
    j=1;
    while j<=n
        if GrupoSigAtrib(grupoAtrib,j)~=0
            ClasifGrupoAtributo= [ClasifGrupoAtributo,' ', num2str(GrupoSigAtrib(grupoAtrib,j))];
        end    
        j=j+1;
    end     
    clear j; 
   
    Ranking_Atributos(grupoAtrib,2)={ClasifGrupoAtributo};
    
end    

    Ranking_Atributos(1,1)={'Lines/Edges'};
    Ranking_Atributos(2,1)={'Faces/People'};
    Ranking_Atributos(3,1)={'Texture'};
    Ranking_Atributos(4,1)={'Random texture'};
    Ranking_Atributos(5,1)={'Foreground Objects'};
    Ranking_Atributos(6,1)={'Geommetric Structures'};
    Ranking_Atributos(7,1)={'Symmetry'};
    Ranking_Atributos(8,1)={'Textual elements'};
    Ranking_Atributos(9,1)={'Animals'};
    Ranking_Atributos(10,1)={'GLOBAL'};
    
clear Atributos_img metodo i n m Aux seguir Rc W ; 
clear num_imgs imgn_en_juego Imagenes query TotalVotosFinales TotalVotosCompletas name_operator; 
clear abortar memo grupoAtrib ClasifGrupoAtributo GrupoSigAtrib;
clear ComparisonsAux SigmaCateg SigmaGlobal m_nuevo m_recalc;
clear TotalVotosCat1 TotalVotosCat2 TotalVotosCat3 TotalVotosCat4;
clear TotalVotosCat5 TotalVotosCat6 TotalVotosCat7 TotalVotosCat8 TotalVotosCat9;
clear error m_comportado Iteraciones_completas Iteraciones_completas_teoricas N_MAX ATRIB_MAX;