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)
            % remove /r/ for Edward and /v/ for Anton, since they can't
            % produce them
            if (strcmp(stimulus(i),'r') && strcmp(sbj,'Edward')) || (strcmp(stimulus(i),'v') && strcmp(sbj,'Anton'))
               continue
            end
            % 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 position
            count(convow,pos) = count(convow,pos) + 1;
            % count depending on position
            count(convow + inifin, pos) = count(convow + inifin,pos) + 1;
            % see if the v or c is in the answer of one of the transcribers
            % (at any position)
            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_with_r_rmvd_Edward_and_v_rmvd_Anton.mat')
% load('error_and_trials.mat')

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

% VOWELS

% Final Position
figure
vow_fin_RD = squeeze(vow_fin_err(RD,:,1:sbj_i(RD))) ./ repmat(vow_fin_trails(:,RD),1,sbj_i(RD));
[vow_fin_RD_sort_err vow_fin_RD_sort_idx] = sort(sum(vow_fin_RD,2),'descend');
bar_RD = bar(1:3:3*length(VOWS),vow_fin_RD(vow_fin_RD_sort_idx,:),0.3,'stacked');hold on;
h1 = gca;
set (h1, 'XTick', 1.5:3:3*length(VOWS)+0.5);
set (h1, 'XTicklabel', cellstr( VOWS(vow_fin_RD_sort_idx) ),'Fontsize',12);
xlim([0 3*length(VOWS)+1]);
vow_fin_GR = squeeze(vow_fin_err(GR,:,1:sbj_i(GR))) ./ repmat(vow_fin_trails(:,GR),1,sbj_i(GR));
bar_GR = bar(2:3:3*length(VOWS),vow_fin_GR(vow_fin_RD_sort_idx,:),0.3,'stacked');
% first legend for RD
h_legend_RD=legend(READING_DISABLED);
% Change Bar Colors
for RD_i = 1 : length(READING_DISABLED)
    set(bar_RD(RD_i),'FaceColor',Colors(RD_i,:)./255,'DisplayName',[READING_DISABLED{RD_i} ' ']);
end
set(h_legend_RD,'Position',[0.62 0.62 0.06 0.16],'FontSize',14);
text(26.75,0.96,'Reading Disabled');
text(37,0.96,'Good Readers');
% second legend for GR
ax2=axes('visible','off');
h_legend_GR = legend(ax2,bar_GR,GOOD_READER)
for GR_i = 1 : length(GOOD_READER)
    set(bar_GR(GR_i),'FaceColor',Colors(length(READING_DISABLED) + GR_i,:)./255,'DisplayName',[GOOD_READER{GR_i} ' ']);
end
set(h_legend_GR,'Position',[0.79 0.70 0.05 0.11],'FontSize',14);
% Plot seperators
for i = 1 : length(VOWS)
    plot(h1,[(i-1)*3 (i-1)*3],[0 1],':', 'Color',[0 0 0],'LineWidth',0.5)
end
% Captions
title(h1,'Error Rates for Vowels in Final Position','Fontsize',14)
xlabel(h1,'Vowel','Fontsize',12)
ylabel(h1,'Error','Fontsize',12)
% RD and GR labels
% for i = 1 : 2*length(VOWS)
%     if mod(i,RD)
%         y = vow_fin_RD_sort_err(floor(i/2)+1)+0.05;
%         text(i,y,'RD','FontSize',9,'HorizontalAlignment','center');
%     else
%         y = sum(vow_fin_GR(vow_fin_RD_sort_idx(i/2),:))+0.05;
%         text(i,y,'GR','FontSize',9,'HorizontalAlignment','center');
%     end
% end
fname = sprintf('vowels_final.eps'); % figure name
print(gcf, fullfile('fig', fname), '-depsc2'); % save stacked barplot

% Initial Position
figure
vow_ini_RD = squeeze(vow_ini_err(RD,:,1:sbj_i(RD))) ./ repmat(vow_ini_trails(:,RD),1,sbj_i(RD));
[vow_ini_RD_sort_err vow_ini_RD_sort_idx] = sort(sum(vow_ini_RD,2),'descend');
bar_RD = bar(1:3:3*length(VOWS),vow_ini_RD(vow_ini_RD_sort_idx,:),0.3,'stacked');hold on;
h1 = gca;
set (h1, 'XTick', 1.5:3:3*length(VOWS)+0.5);
set (h1, 'XTicklabel', cellstr( VOWS(vow_ini_RD_sort_idx) ));
xlim([0 3*length(VOWS)+1]);
vow_ini_GR = squeeze(vow_ini_err(GR,:,1:sbj_i(GR))) ./ repmat(vow_ini_trails(:,GR),1,sbj_i(GR));
bar_GR = bar(2:3:3*length(VOWS),vow_ini_GR(vow_ini_RD_sort_idx,:),0.3,'stacked');
% first legend for RD
h_legend_RD=legend(READING_DISABLED);
% Change Bar Colors
for RD_i = 1 : length(READING_DISABLED)
    set(bar_RD(RD_i),'FaceColor',Colors(RD_i,:)./255,'DisplayName',[READING_DISABLED{RD_i} ' ']);
end
set(h_legend_RD,'Position',[0.62 0.62 0.06 0.16],'FontSize',14);
text(26.75,0.96,'Reading Disabled');
text(37,0.96,'Good Readers');
% second legend for GR
ax2=axes('visible','off');
h_legend_GR = legend(ax2,bar_GR,GOOD_READER)
for GR_i = 1 : length(GOOD_READER)
    set(bar_GR(GR_i),'FaceColor',Colors(length(READING_DISABLED) + GR_i,:)./255,'DisplayName',[GOOD_READER{GR_i} ' ']);
end
set(h_legend_GR,'Position',[0.79 0.70 0.05 0.11],'FontSize',14);
% Plot seperators
for i = 1 : length(VOWS)
    plot(h1,[(i-1)*3 (i-1)*3],[0 1],':', 'Color',[0 0 0],'LineWidth',0.5)
end
% Captions
title(h1,'Error Rates for Vowels in Initial Position','Fontsize',14)
xlabel(h1,'Vowel','Fontsize',12)
ylabel(h1,'Error','Fontsize',12)
% % RD and GR labels
% for i = 1 : 2*length(VOWS)
%     if mod(i,RD)
%         y = vow_ini_RD_sort_err(floor(i/2)+1)+0.05;
%         text(i,y,'RD','FontSize',18,'HorizontalAlignment','center');
%     else
%         y = sum(vow_ini_GR(vow_ini_RD_sort_idx(i/2),:))+0.05;
%         text(i,y,'GR','FontSize',18,'HorizontalAlignment','center');
%     end
% end
fname = sprintf('vowels_initial.eps'); % figure name
print(gcf, fullfile('fig', fname), '-depsc2'); % save stacked barplot

% CONSONANTS

% Final Position
figure
con_fin_RD = squeeze(con_fin_err(RD,:,1:sbj_i(RD))) ./ repmat(con_fin_trails(:,RD),1,sbj_i(RD));
xlabel_txt = {};
for i = 1 : length(con_fin_RD)
    if isnan(con_fin_RD(i,1))
       con_fin_RD(i,:) = zeros(1,sbj_i(RD));
       xlabel_txt = [xlabel_txt [CONS{i} '-rmvd' ]];
    else
        xlabel_txt = [xlabel_txt CONS{i}];
    end
end
[con_fin_RD_sort_err con_fin_RD_sort_idx] = sort(sum(con_fin_RD,2),'descend');
bar_RD = bar(1:3:3*length(CONS),con_fin_RD(con_fin_RD_sort_idx,:),0.3,'stacked','LineStyle','none');hold on;
h1 = gca;
set (h1, 'XTick', 1.5:3:3*length(CONS)+0.5);
set (h1, 'XTicklabel', cellstr( xlabel_txt(con_fin_RD_sort_idx) ));
rotateticklabel(gca,45)
xlim([0 3*length(CONS)+1]);
con_fin_GR = squeeze(con_fin_err(GR,:,1:sbj_i(GR))) ./ repmat(con_fin_trails(:,GR),1,sbj_i(GR));
bar_GR = bar(2:3:3*length(CONS),con_fin_GR(con_fin_RD_sort_idx,:),0.3,'stacked','LineStyle','none');
% first legend for RD
h_legend_RD=legend(READING_DISABLED);
% Change Bar Colors
for RD_i = 1 : length(READING_DISABLED)
    set(bar_RD(RD_i),'FaceColor',Colors(RD_i,:)./255,'DisplayName',[READING_DISABLED{RD_i} ' ']);
end
set(h_legend_RD,'Position',[0.62 0.62 0.06 0.16],'FontSize',14);
text(41.75,0.96,'Reading Disabled');
text(58,0.96,'Good Readers');
% second legend for GR
ax2=axes('visible','off');
h_legend_GR = legend(ax2,bar_GR,GOOD_READER)
for GR_i = 1 : length(GOOD_READER)
    set(bar_GR(GR_i),'FaceColor',Colors(length(READING_DISABLED) + GR_i,:)./255,'DisplayName',[GOOD_READER{GR_i} ' ']);
end
set(h_legend_GR,'Position',[0.79 0.70 0.05 0.11],'FontSize',14);
% Plot seperators
for i = 1 : length(CONS)
    plot(h1,[(i-1)*3 (i-1)*3],[0 1],':', 'Color',[0 0 0],'LineWidth',0.5)
end
% Captions
title(h1,'Error Rates for Consonants in Final Position','Fontsize',14)
xlabel(h1,'Consonants','Fontsize',12)
ylabel(h1,'Error','Fontsize',12)
% % RD and GR labels
% for i = 1 : 2*length(CONS)
%     if mod(i,2)
%         text(i,0.7,'GR','FontSize',15);
%     else
%         text(i,0.7,'RD','FontSize',15);
%     end
% end

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

% Initial Position
figure
con_ini_RD = squeeze(con_ini_err(RD,:,1:sbj_i(RD))) ./ repmat(con_ini_trails(:,RD),1,sbj_i(RD));
xlabel_txt = {};
for i = 1 : length(con_ini_RD)
    if isnan(con_ini_RD(i,1))
       con_ini_RD(i,:) = zeros(1,sbj_i(RD));
       xlabel_txt = [xlabel_txt [CONS{i} '-rmvd' ]];
    else
       xlabel_txt = [xlabel_txt CONS{i}];
    end
end
[con_ini_RD_sort_err con_ini_RD_sort_idx] = sort(sum(con_ini_RD,2),'descend');
bar_RD = bar(1:3:3*length(CONS),con_ini_RD(con_ini_RD_sort_idx,:),0.3,'stacked','LineStyle','none');hold on;
h1 = gca;
set (h1, 'XTick', 1.5:3:3*length(CONS)+0.5);
set (h1, 'XTicklabel', cellstr( xlabel_txt(con_ini_RD_sort_idx) ));
rotateticklabel(h1,45)
xlim([0 3*length(CONS)+1]);
con_ini_GR = squeeze(con_ini_err(GR,:,1:sbj_i(GR))) ./ repmat(con_ini_trails(:,GR),1,sbj_i(GR));
bar_GR = bar(2:3:3*length(CONS),con_ini_GR(con_ini_RD_sort_idx,:),0.3,'stacked','LineStyle','none');
% first legend for RD
h_legend_RD=legend(READING_DISABLED);
% Change Bar Colors
for RD_i = 1 : length(READING_DISABLED)
    set(bar_RD(RD_i),'FaceColor',Colors(RD_i,:)./255,'DisplayName',[READING_DISABLED{RD_i} ' ']);
end
set(h_legend_RD,'Position',[0.62 0.62 0.06 0.16],'FontSize',14);
text(41.75,0.96,'Reading Disabled');
text(58,0.96,'Good Readers');
% second legend for GR
ax2=axes('visible','off');
h_legend_GR = legend(ax2,bar_GR,GOOD_READER)
for GR_i = 1 : length(GOOD_READER)
    set(bar_GR(GR_i),'FaceColor',Colors(length(READING_DISABLED) + GR_i,:)./255,'DisplayName',[GOOD_READER{GR_i} ' ']);
end
set(h_legend_GR,'Position',[0.79 0.70 0.05 0.11],'FontSize',14);
% Plot seperators
for i = 1 : length(CONS)
    plot(h1,[(i-1)*3 (i-1)*3],[0 1],':', 'Color',[0 0 0],'LineWidth',0.5)
end
% Captions
title(h1,'Error Rates for Consonants in Initial Position','Fontsize',14)
xlabel(h1,'Consonants','Fontsize',12)
ylabel(h1,'Error','Fontsize',12)
fname = sprintf('consonants_initial.eps'); % figure name
print(gcf, fullfile('fig', fname), '-depsc2'); % save stacked barplot