function [F1n,F1a,F1o,F1p,F1,AA,BB]= Calc_Physionet_Challenge_Score(indications_stats)

siz=size(indications_stats);
N=siz(1);
RECORDS=indications_stats(:,1);
ANSWERS=indications_stats;
target=indications_stats(:,1);
%% Scoring
% We do not assume that the references and the answers are sorted in the
% same order, so we search for the location of the individual records in answer.txt file.
AA=zeros(4,4);
BB=zeros(4,4);

for n = 1:N
 
    this_answer=ANSWERS(n,2);
    switch target{n}
        case 'N'
            if strcmp(this_answer,'N')
                AA(1,1) = AA(1,1)+1;
            elseif strcmp(this_answer,'A')
                AA(1,2) = AA(1,2)+1;
            elseif strcmp(this_answer,'O')
                AA(1,3) = AA(1,3)+1;
            elseif strcmp(this_answer,'~')
                AA(1,4) = AA(1,4)+1;
            end
        case 'A'
            if strcmp(this_answer,'N')
                AA(2,1) = AA(2,1)+1;
            elseif strcmp(this_answer,'A')
                AA(2,2) = AA(2,2)+1;
            elseif strcmp(this_answer,'O')
                AA(2,3) = AA(2,3)+1;
            elseif strcmp(this_answer,'~')
                AA(2,4) = AA(2,4)+1;
            end
        case 'O'
            if strcmp(this_answer,'N')
                AA(3,1) = AA(3,1)+1;
            elseif strcmp(this_answer,'A')
                AA(3,2) = AA(3,2)+1;
            elseif strcmp(this_answer,'O')
                AA(3,3) = AA(3,3)+1;
            elseif strcmp(this_answer,'~')
                AA(3,4) = AA(3,4)+1;
            end
        case '~'
            if strcmp(this_answer,'N')
                AA(4,1) = AA(4,1)+1;
            elseif strcmp(this_answer,'A')
                AA(4,2) = AA(4,2)+1;
            elseif strcmp(this_answer,'O')
                AA(4,3) = AA(4,3)+1;
            elseif strcmp(this_answer,'~')
                AA(4,4) = AA(4,4)+1;
            end
    end
end

F1n=2*AA(1,1)/(sum(AA(1,:))+sum(AA(:,1)));
F1a=2*AA(2,2)/(sum(AA(2,:))+sum(AA(:,2)));
F1o=2*AA(3,3)/(sum(AA(3,:))+sum(AA(:,3)));
F1p=2*AA(4,4)/(sum(AA(4,:))+sum(AA(:,4)));
%F1=(F1n+F1a+F1o+F1p)/4;
F1=(F1n+F1a+F1o)/3;


str = ['F1 measure for Normal rhythm:  ' '%1.4f\n'];
fprintf(str,F1n)
str = ['F1 measure for AF rhythm:  ' '%1.4f\n'];
fprintf(str,F1a)
str = ['F1 measure for Other rhythm:  ' '%1.4f\n'];
fprintf(str,F1o)
str = ['F1 measure for Noisy recordings:  ' '%1.4f\n'];
fprintf(str,F1p)
str = ['Final F1 measure:  ' '%1.4f\n'];
fprintf(str,F1)

BB(2:4,1)=AA(2:4,1)./AA(1,1)*100;
BB(1,2:4)=AA(1,2:4)./AA(1,1)*100;

BB([1 3 4],2)=AA([1 3 4],2)./AA(2,2)*100;
BB(2,[1 3 4])=AA(2,[1 3 4])./AA(2,2)*100;

BB([1 2 4],3)=AA([1 2 4],3)./AA(3,3)*100;
BB(3,[1 2 4])=AA(3,[1 2 4])./AA(3,3)*100;

% BB(1:3,4)=AA(1:3,4)./AA(4,4)*100;
% BB(4,1:3)=AA(4,1:3)./AA(4,4)*100;
