clc; close all; clear all;
addpath ./bin

% % Set Colors and Markers
% Pink=[242 22 140];
% Gray=[120 120 120];
% Black=[0 0 0];
% Brick=[150 0 0];
% Red=[255 0 0];
% DRed=[157 2 2];
% Blue=[0 0 255];
% DBlue=[2 2 125];
% RedOrange=[255 98 0];
% Orange=[255 140 22];
% Green=[0 255 0];
% Purple=[166 25 189];
% BlueGreen=[0 255 146];
% Yellow=[255 255 0];
% Golden=[255 209 0];
% White=[200 200 200];
% Forest=[0 88 20];
% DeepPurple=[102 0 102];
% Brown=[131 63 28];
% BrightPur=[156 0 156];
% Colorbl_Org=[230 159 0];
% Colorbl_Sky=[86 180 233];
% Colorbl_BlGr=[0 158 115];
% Colorbl_Y=[240 228 66];
% Colorbl_Bl=[0 114 178];
% Colorbl_Ver=[213 94 0];
% Colorbl_RP=[204 121 167];
% Colors=[Brick;Red;Pink;Colorbl_Org;Colorbl_Y;Green;Forest;Colorbl_BlGr;DBlue;Colorbl_Sky;Colorbl_Bl;Colorbl_RP;BrightPur;White;Brown;Gray;Golden;Black];
% 
% CONS = {'C','D','G','J','S','T','Z','b','d','f','g','h','k','L','m','n','p','r','s','t','v','w','y','z'};
% VOWS = {'@','A','E','I','O','R','U','W','Y','a','c','e','i','o','u'};
% CON = 1;
% VOW = 2;
% % offest to con vow depending on position
% INI = 2;
% FIN = 4;
% 
% SUBJECTS = {'Alina','Angela','Anton','Bob','Carly','Edward','Evan','Joanna', 'Latisha','Laura','Miguel','Norene','Shauna','Teddy','Tony'};
% READING_DISABLED = {'Alina','Angela','Edward','Latisha','Laura','Norene','Shauna','Teddy','Tony'};
% GOOD_READER = {'Anton','Bob','Carly','Evan','Joanna','Miguel'};
% 
% con_overall_trails = zeros(length(CONS),2);
% con_ini_trails = zeros(length(CONS),2);
% con_fin_trails = zeros(length(CONS),2);
% vow_overall_trails = zeros(length(VOWS),2);
% vow_ini_trails = zeros(length(VOWS),2);
% vow_fin_trails = zeros(length(VOWS),2);
% 
% sbj_i = [0 0]; % sbj index sbj_i(1) Normal Reader, sbj_i(2) Reading disabled
% for sbj = SUBJECTS
%     % set reading disabled flag
%     RD = sum(strcmp(sbj,READING_DISABLED));
%     sbj_i(RD+1)=sbj_i(RD+1) + 1 ;
%     count = zeros(6,length(CONS));
%     correct = zeros(6,length(CONS));
%     % Read in resolved log file
%     [ndata, xls_text, alldata] = xlsread('../NSCM_data.xlsx', sbj{1} );
%     
%     %count total number of consonants and vowel irrelvant of their poistion
%     for trail_idx = 2 : length(xls_text)
%         stimulus = xls_text{trail_idx,4};
%         examiner = xls_text{trail_idx,5};
%         transcriber1 = xls_text{trail_idx,6};
%         transcriber2 = xls_text{trail_idx,7};
%         % look at c or v individually
%         for i=1:length(stimulus)
%             % consonant or vowel?
%             pos = find(strcmp(stimulus(i), CONS));
%             if i == 1
%                 inifin = INI;
%             else
%                 inifin = FIN;
%             end
%             if isempty(pos)
%                 pos = find(strcmp(stimulus(i), VOWS));
%                 convow = VOW;
%             else
%                 convow = CON;
%             end
%             % count irrelevant of poistion
%             count(convow,pos) = count(convow,pos) + 1;
%             % count depending on positon
%             count(convow + inifin, pos) = count(convow + inifin,pos) + 1;
%             
%             cor = logical(0);
%             for j = 1 : length(examiner) 
%                 cor = cor || examiner(j)==stimulus(i); 
%             end
%             for j = 1 : length(transcriber1) 
%                 cor = cor || transcriber1(j)==stimulus(i); 
%             end
%             for j = 1 : length(transcriber2) 
%                 cor = cor || transcriber2(j)==stimulus(i); 
%             end
%             % correct count irrelevant of poistion
%             correct(convow,pos) = correct(convow,pos) + cor;
%             % correct count depending on poistion
%             correct(convow+inifin,pos) = correct(convow+inifin,pos) + cor; 
%         end
%     end
%     % Consonants
%     % overall
%     select = CON;
%     con_overall_err(RD+1,:,sbj_i(RD+1)) = count(select,:)' - correct(select,:)';
%     con_overall_trails(:,RD+1) = con_overall_trails(:,RD+1) + count(select,:)';
%     % initial
%     select = CON + INI;
%     con_ini_err(RD+1,:,sbj_i(RD+1)) = count(select,:)' - correct(select,:)';
%     con_ini_trails(:,RD+1) = con_ini_trails(:,RD+1) + count(select,:)';
%     % final
%     select = CON + FIN;
%     con_fin_err(RD+1,:,sbj_i(RD+1)) = count(select,:)' - correct(select,:)';
%     con_fin_trails(:,RD+1) = con_fin_trails(:,RD+1) + count(select,:)';    
%     
%     
%     %Vowels
%     % overall
%     select = VOW;
%     vow_overall_err(RD+1,:,sbj_i(RD+1)) = count(select,1:length(VOWS))' - correct(select,1:length(VOWS))';
%     vow_overall_trails(:,RD+1) = vow_overall_trails(:,RD+1) + count(select,1:length(VOWS))';
%     % initial
%     select = VOW + INI;
%     vow_ini_err(RD+1,:,sbj_i(RD+1)) = count(select,1:length(VOWS))' - correct(select,1:length(VOWS))';
%     vow_ini_trails(:,RD+1) = vow_ini_trails(:,RD+1) + count(select,1:length(VOWS))';
%     % final
%     select = VOW + FIN;
%     vow_fin_err(RD+1,:,sbj_i(RD+1)) =count(select,1:length(VOWS))' - correct(select,1:length(VOWS))';
%     vow_fin_trails(:,RD+1) = vow_fin_trails(:,RD+1) + count(select,1:length(VOWS))';
% end
% save('error_and_trials.mat')
load('error_and_trials_with_r_rmvd_Edward_and_v_rmvd_Anton.mat')

GR = 1; % index for Reading disabled
RD = 2; % index for Good Readers


% VOWELS
% Final Position
figure
vow_fin_RD = sum( squeeze(vow_fin_err(RD,:,1:sbj_i(RD))) ./ repmat(vow_fin_trails(:,RD),1,sbj_i(RD)), 2 );
vow_fin_GR = sum( squeeze(vow_fin_err(GR,:,1:sbj_i(GR))) ./ repmat(vow_fin_trails(:,GR),1,sbj_i(GR)), 2 );

figure
loglog(vow_fin_GR,vow_fin_RD,'o');hold on;
loglog([10e-4 1] , [10e-4 1])
xlim([3*10e-4 1])
ylim([3*10e-4 1])
xlabel('Total Error Normal Readers','Fontsize',14)
ylabel('Total Error Abnormal Readers','Fontsize',14)
title('Error Scatter for Vowels in Final Position','Fontsize',16)
for vow_i = 1 :length(VOWS)
    
    if strcmp(VOWS(vow_i),'c')
        y = vow_fin_RD(vow_i)+0.2*vow_fin_RD(vow_i);
    else
        y = vow_fin_RD(vow_i)+0.1*vow_fin_RD(vow_i);
    end
    if strcmp(VOWS(vow_i),'o')
        x = vow_fin_GR(vow_i)-0.15*vow_fin_GR(vow_i);
    else
        x = vow_fin_GR(vow_i)+0.1*vow_fin_GR(vow_i);
    end
    text(x,y,VOWS(vow_i),'Fontsize',12)
end
fname = sprintf('vowels_final.eps'); % figure name
print(gcf, fullfile('fig', fname), '-depsc2'); % save figure

% Initial
vow_ini_RD = sum( squeeze(vow_ini_err(RD,:,1:sbj_i(RD))) ./ repmat(vow_ini_trails(:,RD),1,sbj_i(RD)), 2 );
vow_ini_GR = sum( squeeze(vow_ini_err(GR,:,1:sbj_i(GR))) ./ repmat(vow_ini_trails(:,GR),1,sbj_i(GR)), 2 );

figure
loglog(vow_ini_GR,vow_ini_RD,'o');hold on;
loglog([10e-4 1] , [10e-4 1])
xlim([3*10e-4 1])
ylim([3*10e-4 1])
xlabel('Total Error Normal Readers','Fontsize',14)
ylabel('Total Error Abnormal Readers','Fontsize',14)
title('Error Scatter for Vowels in Initial Position','Fontsize',16)
for vow_i = 1 :length(VOWS)
    
    if strcmp(VOWS(vow_i),'o')
        y = vow_ini_RD(vow_i)-0.1*vow_ini_RD(vow_i);
    else
        y = vow_ini_RD(vow_i)+0.1*vow_ini_RD(vow_i);
    end
    if strcmp(VOWS(vow_i),'o')
        x = vow_ini_GR(vow_i)-0.1*vow_ini_GR(vow_i);
    elseif strcmp(VOWS(vow_i),'I')
        x = vow_ini_GR(vow_i)+0.05*vow_ini_GR(vow_i);
    else
        x = vow_ini_GR(vow_i)+0.1*vow_ini_GR(vow_i);
    end
    text(x,y,VOWS(vow_i),'Fontsize',12)
end
fname = sprintf('vowels_initial.eps'); % figure name
print(gcf, fullfile('fig', fname), '-depsc2'); % save figure

% CONSONANTS
% Final Position
figure
con_fin_RD = sum( squeeze(con_fin_err(RD,:,1:sbj_i(RD))) ./ repmat(con_fin_trails(:,RD),1,sbj_i(RD)), 2 );
con_fin_GR = sum( squeeze(con_fin_err(GR,:,1:sbj_i(GR))) ./ repmat(con_fin_trails(:,GR),1,sbj_i(GR)), 2 );

figure
loglog(con_fin_GR,con_fin_RD,'o');hold on;
loglog([10e-4 1] , [10e-4 1])
xlim([10e-3 1])
ylim([10e-3 1])
set(gca,'XTickLabel',{'1','10','100'});
set(gca,'YTickLabel',{'1','10','100'});
xlabel('% Error Good Reader Group','Fontsize',14)
ylabel('% Error Reading Disabled Group','Fontsize',14)
title('Error Scatter for Consonants in Final Position','Fontsize',16)

fname = sprintf('consonants_final_no_labels.png'); % figure name
print(gcf, fullfile('fig', fname), '-dpng'); % save figure

for con_i = 1 :length(CONS)
    
    if strcmp(CONS(con_i),'g')
        y = con_fin_RD(con_i)-0.15*con_fin_RD(con_i);
    elseif strcmp(CONS(con_i),'m')
        y = con_fin_RD(con_i);
    else
        y = con_fin_RD(con_i)+0.1*con_fin_RD(con_i);
    end
    if strcmp(CONS(con_i),'b')
        x = con_fin_GR(con_i)-0.1*con_fin_GR(con_i);
    elseif strcmp(CONS(con_i),'g')
        x = con_fin_GR(con_i);
    elseif strcmp(CONS(con_i),'b')
        
    else
        x = con_fin_GR(con_i)+0.1*con_fin_GR(con_i);
    end
    text(x,y,CONS(con_i),'Fontsize',12)
end


fname = sprintf('consonants_final.eps'); % figure name
print(gcf, fullfile('fig', fname), '-depsc'); % save figure

% Initial
con_ini_RD = sum( squeeze(con_ini_err(RD,:,1:sbj_i(RD))) ./ repmat(con_ini_trails(:,RD),1,sbj_i(RD)), 2 );
con_ini_GR = sum( squeeze(con_ini_err(GR,:,1:sbj_i(GR))) ./ repmat(con_ini_trails(:,GR),1,sbj_i(GR)), 2 );

figure
loglog(con_ini_GR,con_ini_RD,'o');hold on;
loglog([10e-4 1] , [10e-4 1])
xlim([3*10e-4 1])
ylim([3*10e-4 1])
xlabel('Total Error Normal Readers','Fontsize',14)
ylabel('Total Error Abnormal Readers','Fontsize',14)
title('Error Scatter for Consonants in Initial Position','Fontsize',16)
for con_i = 1 :length(CONS)
    
    if strcmp(CONS(con_i),'g')
        y = con_ini_RD(con_i)+0.2*con_ini_RD(con_i);
    elseif strcmp(CONS(con_i),'L')
        y = con_ini_RD(con_i)-0.1*con_ini_RD(con_i);
    else
        y = con_ini_RD(con_i)+0.1*con_ini_RD(con_i);
    end
    if strcmp(CONS(con_i),'g')
        x = con_ini_GR(con_i);
    elseif strcmp(CONS(con_i),'L')
        x = con_ini_GR(con_i);                
    else
        x = con_ini_GR(con_i)+0.1*con_ini_GR(con_i);
    end
    text(x,y,CONS(con_i),'Fontsize',12)
end
fname = sprintf('consonants_initial.eps'); % figure name
print(gcf, fullfile('fig', fname), '-depsc2'); % save figure