% load data
clear all;
close all;
clc

% data_dir = [pwd filesep 'validation' filesep];

% data_dir = 'E:\MATLAB\CinC_Challenge_2017\training2017\';
% data_dir = 'C:\Matlab_backup\CinC_Challenge_2017\training2017\';

data_dir = '~/Documents/Research/CinCChallenge2017/CinC2017/training2017/';

%% Load the list of records in the validation set.
fid = fopen([data_dir 'RECORDS'],'r');
if(fid ~= -1)
    RECLIST = textscan(fid,'%s');
else
    error(['Could not open ' data_dir 'RECORDS for scoring. Exiting...'])
end
fclose(fid);
RECORDS = RECLIST{1};

%% load reference file
% reffile = ['E:\MATLAB\CinC_Challenge_2017\training2017' filesep 'REFERENCE.csv'];
% reffile = ['C:\Matlab_backup\CinC_Challenge_2017\training2017\'  filesep 'REFERENCE.csv'];
reffile = ['~/Documents/Research/CinCChallenge2017/CinC2017/training2017' filesep 'REFERENCE.csv'];

fid = fopen(reffile, 'r');
if(fid ~= -1)
    Ref = textscan(fid,'%s %s','Delimiter',',');
else
    error(['Could not open ' reffile ' for scoring. Exiting...'])
end
fclose(fid);

RECORDS = Ref{1};
target  = Ref{2};
N       = length(RECORDS);
%%

% idx = ismember(Ref{2},'~');
% idxnoise=find(idx);

% for i = 1:length(idxnoise)
for i = 1:N
%     fname = RECORDS{idxnoise(i)};
    fname = RECORDS{i};
    [tm,ecg,fs,siginfo]=rdmat([data_dir fname]);
    plot(tm,ecg)
%     title(['Type: ', Ref{2}(idxnoise(i))])
    title(['Type: ', Ref{2}(i)])
    xlim([0 8])
    pause
    close
    subplot(311)
    [QRS,sign,en_thres] = qrs_detect2(ecg',0.25,0.6,fs);
    plot(tm,ecg,tm(QRS),ecg(QRS),'or')
    clear QRS
    subplot(312)
    [hrv, R_t, R_amp, QRS, S_t, S_amp]  = rpeakdetect(ecg,fs);
    plot(tm,ecg,tm(QRS),ecg(QRS),'or')
    subplot(313)
    [p] = detqrs3(ecg,fs);
    plot(tm,ecg,tm(p),ecg(p),'or')
    pause
    close
end

%% look at specific groups/signals
 
idx = ismember(Ref{2},'O');
idxnoise=find(idx);
for i = 25:length(idxnoise)
    fname = RECORDS{idxnoise(i)};
    [tm,ecg,fs,siginfo]=rdmat([data_dir fname]);
    plot(tm,ecg)
    title(['Type: ', Ref{2}(idxnoise(i))])
%     xlim([0 8])
    pause
    close
    subplot(311)
    [QRS,sign,en_thres] = qrs_detect2(ecg',0.25,0.6,fs);
    plot(tm,ecg,tm(QRS),ecg(QRS),'or')
    clear QRS
    subplot(312)
    [hrv, R_t, R_amp, QRS, S_t, S_amp]  = rpeakdetect(ecg,fs);
    plot(tm,ecg,tm(QRS),ecg(QRS),'or')
    subplot(313)
    [p] = detqrs3(ecg,fs);
    plot(tm,ecg,tm(p),ecg(p),'or')
    pause
    close
end

%% 
load('E:\MATLAB\CinC_Challenge_2017\workinprogress\partialresults') 

idxnoise = find(TotalParameters(:,1) == inf);

for i = 1:length(idxnoise)
    fname = RECORDS{idxnoise(i)};
    [tm,ecg,fs,siginfo]=rdmat([data_dir fname]);
    Ref{2}(idxnoise(i))
    [classf,x] = challenge([data_dir fname]);
    
    TotalParameters(idxnoise(i),:) = x;

    fprintf(['---Processed ' num2str(idxnoise(i)) ' out of ' num2str(length(RECORDS)) ' records.\n'])
end


%%
%%
%% Running on the entire training set and obtain values for parameters
% comments:
% 1) modification: QRS outlier detection at i = 3196;
% 2) modification: saen2 = SampEn2(2,0.35*std(RR),RR); at i = 3420;
% 3) modification: detection noisy, based on ratio mean median RR

% new:
% 1) modification: chece whether QRS residue reduction is applicable; i = 923
% 2) modification: check for RR periods less than 200 ms; i = 3275/4735 MAJOR!

classifyResult = zeros(length(RECORDS),1);
total_time     = 0;

% TotalParameters = zeros(length(RECORDS),46); 

fid=fopen('answers.txt','wt');
for i = 1:4734%4735:length(RECORDS)
    fname = RECORDS{i};
    tic;
    [P_LF, f_LF, P_HF, f_HF, meanRR, SDRR, rMSSDRR, pNN10, pNN20, ...
    pNN30, pNN40, pNN50, pNN60, pNN70, pNN80,pNN90, pNN100, saen1, saen2, ...
    V0, V1, LV2, UV2, numbercomponents95percQRST, SimQRST1, SimQRST2, ...
    SimQRST3, SimQRST4, SimQRST5, numbercomponents95percP, SimP1, SimP2, ...
    SimP3, SimP4, SimP5, SCN, DF, fWP, fWPMAW, rhe,  SampEn, SampEnMAW, ... 
    OI, SE, FWA, differenceMediaRRmeanRR] = challenge([data_dir fname]);
    
    TotalParameters(i,:) = [P_LF, f_LF, P_HF, f_HF, meanRR, SDRR, rMSSDRR, pNN10, pNN20, ...
    pNN30, pNN40, pNN50, pNN60, pNN70, pNN80,pNN90, pNN100, saen1, saen2, ...
    V0, V1, LV2, UV2, numbercomponents95percQRST, SimQRST1, SimQRST2, ...
    SimQRST3, SimQRST4, SimQRST5, numbercomponents95percP, SimP1, SimP2, ...
    SimP3, SimP4, SimP5, SCN, DF, fWP, fWPMAW, rhe,  SampEn, SampEnMAW, ... 
    OI, SE, FWA, differenceMediaRRmeanRR];

    % write the answer to answers.txt file
    fprintf(fid,'%s,%s\n',RECORDS{i},classifyResult(i));

    total_time = total_time+toc;
    fprintf(['---Processed ' num2str(i) ' out of ' num2str(length(RECORDS)) ' records.\n'])
end
fclose(fid);

%% Running on the entire training set set and obtain the score results
classifyResult = zeros(length(RECORDS),1);
total_time     = 0;

fid=fopen('answers.txt','wt');
% for i = 1:length(RECORDS)
% fname = RECORDS{i};
%tic
% Ref{2}(i)
idx = ismember(Ref{2},'A');
idxnoise=find(idx);
for i = 1:length(idxnoise)
    fname = RECORDS{idxnoise(i)};
    tic;
    Ref{2}(idxnoise(i))
%     i
    classifyResult(i) = challenge_playwith([data_dir fname]);
%     [maxDF(i), DF(i)] = challenge_playwith([data_dir fname]);
    
    % write the answer to answers.txt file
    fprintf(fid,'%s,%s\n',RECORDS{i},classifyResult(i));

    total_time = total_time+toc;
%     fprintf(['---Processed ' num2str(i) ' out of ' num2str(length(RECORDS)) ' records.\n'])
    fprintf(['---Processed ' num2str(idxnoise(i)) ' out of ' num2str(length(RECORDS)) ' records.\n'])
end
fclose(fid);

averageTime = total_time/length(RECORDS);
fprintf(['Generation of validation set completed.\n  Total time = ' ...
    num2str(total_time) '\n  Average time = ' num2str(averageTime) '\n'])

fprintf(['Answer file created as answers.txt.\n  Processing completed.\n'])

fprintf(['Running score2017Challenge.m to get scores on your entry on the validation data in training set....\n'])

%%
clear all;

%% Load the answer classification results
fid = fopen('answers.txt','r');
if(fid ~= -1)
    ANSWERS = textscan(fid, '%s %s','Delimiter',',');
else
    error('Could not open users answer.txt for scoring. Run the generateValidationSet.m script and try again.')
end
fclose(fid);

%% Load the reference classification results
% reffile = ['E:\MATLAB\CinC_Challenge_2017\training2017' filesep 'REFERENCE.csv'];
reffile = ['C:\Matlab_backup\CinC_Challenge_2017\training2017\'  filesep 'REFERENCE.csv'];
% reffile = ['/Volumes/SAMSUNG/MATLAB/CinC_Challenge_2017/training2017' filesep 'REFERENCE.csv'];

fid = fopen(reffile, 'r');
if(fid ~= -1)
    Ref = textscan(fid,'%s %s','Delimiter',',');
else
    error(['Could not open ' reffile ' for scoring. Exiting...'])
end
fclose(fid);

RECORDS = Ref{1};
target  = Ref{2};
N       = length(RECORDS);

a = find(strcmp(ANSWERS{2},'N'));
b = find(strcmp(ANSWERS{2},'A'));
c = find(strcmp(ANSWERS{2},'O'));
d = find(strcmp(ANSWERS{2},'~'));
ln = length(a)+length(b)+length(c)+length(d);
if(length(ANSWERS{2}) ~= ln);
    error('Input must contain only N, A, O or ~');
end

%% 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);

for n = 1:N
    rec = RECORDS{n};
    i = strmatch(rec, ANSWERS{1});
    if(isempty(i))
        warning(['Could not find answer for record ' rec '; treating it as NOISE (~).']);
        this_answer = '~';
    else
        this_answer = ANSWERS{2}(i);
    end
    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;


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)

