%% Script to analyse individual data from the study examining the stopping of
% planned versus ongoing actions in the same stop signal task.
% Task involves reaching actions performed with a computer mouse.

%% Code is suitable for use with task code "StopWhatYoureDoing15"

% Some variables have been removed from csv file, so requires new import
% options

%%
clc; clear; close all;
set(0,'DefaultFigureWindowStyle','docked');

% All subs in first pass run before filtering
all_subjs = {'81ut_Pilot15', 'k7kb_Pilot15', 'u75q_Pilot15', 'dj19_Pilot15'};

% Good subs (see filter in _allSubs.m script)
% subjs = { };

%get the directory of your input files:
savePath = strcat(pwd, '/group/');

%% Loop to import all individual data as a first pass and filter data 
% according to performance data. 
% Store first pass in struct:GroupData_all
% Import the data

for s = 1:length(all_subjs)
    %     i = 6
    subjID  = all_subjs(s);
    loadID = strcat('IndieData_',subjID);
    
    fileDat = strcat("/Users/riccihannah/Google Drive/Online_Stop_Signal/Online_Stop_Task/Task_replication/data/processed/", loadID ,".mat");
    load(fileDat)
    % subjID = strcat('ID',extractBetween(fileDat,"Jstop_",".csv"));
    
    demographics(s,:) = data.Demographics.Variables;
    
    summary_behavior(s,:) = table2array(data.Summary_Behavior);
   
    
end

% Create output tables
GroupData_all.Demographics = array2table(demographics, 'VariableNames', {'SubID', ...
    'ProlificID', 'Age', 'Sex', 'Handedness', 'Hispanic', 'Race'});
GroupData_all.Summary_Behavior = array2table(summary_behavior, ...
    'VariableNames',{'GoOmit', 'GoMiss', 'GoErr', 'FalseAlarm', ...
        'Go_RT','Go_MT', 'Go_TT', ...
        'Go_RTstd', 'Go_MTstd', 'Go_TTstd', ...
        'FS_RTh', 'FS_MTh', 'FS_TTh',...
        'SS_RTt', ...
        'FS_RTt', 'FS_MTt', 'FS_TTt', ...
        'SSDh','SSDt', ...
        'pStoph','pStopt', ...
        'SSRTh_RT','SSRTt_MT' 'SSRTt_TT', ...
        'N_TFh','TF_RT', 'TF_MT', ...
        'TF_TT', 'TF_SSD'});

%% Index data according to task performance criteria
% FS RT < Go RT (home staircase) and FS TT < GO TT (Target staircase)
idx_FS_Go_RT_h = GroupData_all.Summary_Behavior.FS_RTh < GroupData_all.Summary_Behavior.Go_RT; 
idx_FS_Go_TT_t = GroupData_all.Summary_Behavior.FS_TTt < GroupData_all.Summary_Behavior.Go_TT; 
disp(strcat('People with FSRT<GoRT home stair = ', num2str(sum(idx_FS_Go_RT_h))));
disp(strcat('People with FSRT<GoRT target stair = ', num2str(sum(idx_FS_Go_TT_t))));

% pStop > 25% and < 75%
idx_pStop25h = GroupData_all.Summary_Behavior.pStoph >= 25 & GroupData_all.Summary_Behavior.pStoph <= 75; 
idx_pStop25t = GroupData_all.Summary_Behavior.pStopt >= 25 & GroupData_all.Summary_Behavior.pStopt <= 75; 
disp(strcat('People with pstop <25 and >75 home stair = ', num2str(sum(idx_pStop25h))));
disp(strcat('People with pstop <25 and >75 target stair = ', num2str(sum(idx_pStop25t))));

% SSRT > 0 
idx_SSRT_positive = GroupData_all.Summary_Behavior.SSRTh_RT > 0 & GroupData_all.Summary_Behavior.SSRTt_TT > 0;

% go errors (omissions and misses) < 20% (GoErr includes false alarms)
idx_goErr =  GroupData_all.Summary_Behavior.GoErr <= 20; 
disp(strcat('People with Go Errors <20% = ', num2str(sum(idx_goErr))));

% put them all (i.e. the criteria) together. 1 = those meeting criteria
idx_all = idx_FS_Go_RT_h & idx_FS_Go_TT_t & idx_pStop25h & idx_pStop25t & idx_goErr & idx_SSRT_positive; 
disp(strcat('People meeting all criteria = ', num2str(sum(idx_all))));

% interesting to see how Mouse and SSRT measures of stop latency stack up
% even when race fails. So can remvoe the FSRT < GoRT to see
% i.e. change the above to...
% idx_all = idx_pStop25h & idx_pStop25t & idx_goErr & idx_SSRT_positive; sum(idx_all)

% Create new list of subjects who meet the criteria
subjs = all_subjs(idx_all);

%% Cycle through the included subjects and import all data variables to a 
% new struct: GroupData

clearvars 'demographics' 'summary_behavior';

for s = 1:length(subjs)

    subjID  = subjs(s);
    loadID = strcat('IndieData_',subjID);
    
    fileDat = strcat("/Users/riccihannah/Google Drive/Online_Stop_Signal/Online_Stop_Task/Task_replication/data/processed/", loadID ,".mat");
    load(fileDat)
    
    demographics(s,:) = data.Demographics.Variables;
    
    computer_setup(s,:) = table2array(data.Computer_setup);
    
    SSD_FSRT(1:size(data.SSD_FSRT,1),1:size(data.SSD_FSRT,2),s) = table2array(data.SSD_FSRT);
    
    SSD_pStop(1:size(data.SSD_FSRT,1),1:size(data.SSD_FSRT,2),s) = table2array(data.SSD_pStop);
    
    ZRFT_pStop(1:size(data.SSD_FSRT,1),1:size(data.SSD_FSRT,2),s) = table2array(data.ZRFT_pStop);
    
    summary_behavior(s,:) = table2array(data.Summary_Behavior);
    
    Trl_CntxInd_Violations(1:size(data.Trlwise_CntxInd_pcnt,1),1:size(data.Trlwise_CntxInd_pcnt,2),s) = ...
        table2array(data.Trlwise_CntxInd_pcnt(:,:));
    idx_nonNaN_home = ~isnan(Trl_CntxInd_Violations(:,2,s));
    idx_nonNaN_targ = ~isnan(Trl_CntxInd_Violations(:,6,s));
    mean_pcnt_FSlessGoRT_Home = nanmean(Trl_CntxInd_Violations(idx_nonNaN_home,4,s));
    mean_pcnt_FSlessGoRT_Target = nanmean(Trl_CntxInd_Violations(idx_nonNaN_targ,8,s));
    Trl_CntxInd_Violations_mn(s,1) = round(mean_pcnt_FSlessGoRT_Home);
    Trl_CntxInd_Violations_mn(s,2) = round(mean_pcnt_FSlessGoRT_Target);
    
    Time_maxY(s, :) = table2array(data.time_maxY);
    
    maxYpos_pcntTargDist(s,:) = table2array(data.maxYpos_pcntTargDist);
    
end

% Create summary tables
GroupData.Demographics = array2table(demographics, 'VariableNames', {'SubID', ...
    'ProlificID', 'Age', 'Sex', 'Handedness', 'Hispanic', 'Race'});
GroupData.Computer_setup = array2table(computer_setup, 'VariableNames', {'frameRate',...
        'Browser','OSname' ,'OSversion' ,  ...
        'screenResWidth','screenResHeight' ,'windowResWidth' , ...
        'windowResHeight' , 'centerSreen_x', 'centerSreen_y','targetYcenter',...
        'targetXLcenter', 'targetXRcenter',...
        'leaveHomeYlim','enterTargetYlim','home2targetYdist'});
GroupData.Summary_Behavior = array2table(summary_behavior, ...
    'VariableNames',{'GoOmit', 'GoMiss', 'GoErr', 'FalseAlarm', ...
        'Go_RT','Go_MT', 'Go_TT', ...
        'Go_RTstd', 'Go_MTstd', 'Go_TTstd', ...
        'FS_RTh', 'FS_MTh', 'FS_TTh',...
        'SS_RTt', ...
        'FS_RTt', 'FS_MTt', 'FS_TTt', ...
        'SSDh','SSDt', ...
        'pStoph','pStopt', ...
        'SSRTh_RT','SSRTt_MT' 'SSRTt_TT', ...
        'N_TFh','TF_RT', 'TF_MT', ...
        'TF_TT', 'TF_SSD'});
GroupData.Trl_CntxInd_Violations_mn =  array2table(Trl_CntxInd_Violations_mn, ...
      'VariableNames',{'pcntFSlessGo_h','pcntFSlessGo_t'});
GroupData.time_maxY = array2table(Time_maxY,...
'VariableNames', {'Goh_mn','Goh_std',...
        'FSh_mn','FSh_std','FSh_Min','FSh_Max',...
        'TFFSh_mn',...
        'Got_mn','Got_std',...
        'SSt_mn','SSt_std','SSt_Min','SSt_Max',...
        'Nh_outliers','FSh_Out','TimeonFSh_mn','TimeonFSh_Outlier'});
GroupData.maxYpos_pcntTargDist = array2table(maxYpos_pcntTargDist,...
        'VariableNames',{'Ymaxpos_h','Ymaxpos_t'});

% Var names for cols in SSD arrays
SSD_FSRT_varName = {'SSDh','FSRTh','SSDt','FSTTt'};
SSD_pStop_varName = {'SSDh','pStoph','SSDt','pStopt'};
ZRFT_pStop_varName = {'ZRFTh','pStoph','ZRFTt','pStopt'};
Trl_CntxInd_Violations_varName = {'pcntFSlessGo_h','pcntFSlessGo_t'};

%% Overall Age, Sex Handedness
meanAge = mean(str2num(char(GroupData.Demographics.Age)));
stdAge = std(str2num(char(GroupData.Demographics.Age)));
numMale = sum(char(GroupData.Demographics.Sex) == 'M');
numRightHand = sum(char(GroupData.Demographics.Handedness) == 'R');
numHispanic = sum(char(GroupData.Demographics.Hispanic) == 'Y');
numWhite = sum(GroupData.Demographics.Race == 'White');
numBlack = sum(GroupData.Demographics.Race == 'Black');
numAsian = sum(GroupData.Demographics.Race == 'Asian');
numOther = sum(GroupData.Demographics.Race == 'Other');

demographics_sum = [meanAge, stdAge,numMale, numRightHand, numHispanic, numWhite, ...
    numBlack , numAsian, numOther];
GroupData.Demographics_summ = array2table(demographics_sum, ...
    'VariableNames',{'meanAge', 'stdAge','numMale', 'numRightHand','numHispanic',...
    'GonumWhiteMT', 'numBlack', 'numAsian','numOther'});


%% Demographics of sample kept in analysis
meanAge_SubSamp = mean(str2num(char(GroupData.Demographics.Age)));
stdAge_SubSamp = std(str2num(char(GroupData.Demographics.Age)));
numMale_SubSamp = sum(char(GroupData.Demographics.Sex) == 'M');


%% Plot figures for only those people meeting criteria

%% Plot RT MT TotT SSRT and mean SSD
%% Mean RT
meanGo_RT = nanmean(GroupData.Summary_Behavior.Go_RT);
stdGo_RT = nanstd(GroupData.Summary_Behavior.Go_RT);

meanSS_RTt = nanmean(GroupData.Summary_Behavior.SS_RTt);
stdSS_RTt = nanstd(GroupData.Summary_Behavior.SS_RTt);

meanFS_RTh = nanmean(GroupData.Summary_Behavior.FS_RTh);
stdFS_RTh = nanstd(GroupData.Summary_Behavior.FS_RTh);

meanFS_RTt = nanmean(GroupData.Summary_Behavior.FS_RTt);
stdFS_RTt = nanstd(GroupData.Summary_Behavior.FS_RTt);

%% Plot GoRT, SS_RTt, FS_RTh and FS_rtt
figure()
axes('box','off','tickdir','out','LineWidth',1,'FontSize',14)
beeswarm_plot({GroupData.Summary_Behavior.Go_RT GroupData.Summary_Behavior.SS_RTt,...
   GroupData.Summary_Behavior.FS_RTh GroupData.Summary_Behavior.FS_RTt},...
    'color',[0.8,0.8,0.8; 0.98824,0.53725,0;...
    0.8,0.8,0.8; 0.98824,0.53725,0]);
set(gca,'XTick',1:4,'XTickLabel',{'Go.RT','SS.RTt','FS.RTh','FS.RTt'},'FontSize',14);
ylabel('Reaction time (ms)','FontSize',14);
ylim([0, max(GroupData.Summary_Behavior.Go_RT + 200)]);
title('Reaction times (movement onset)','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
print(gcf,strcat(savePath, 'Group_Reaction_Times.pdf'),'-dpdf','-r400');
% delete(gcf)

%% Mean MT
meanGo_MT = nanmean(GroupData.Summary_Behavior.Go_MT);
stdGo_MT = nanstd(GroupData.Summary_Behavior.Go_MT);

meanFS_MTt = nanmean(GroupData.Summary_Behavior.FS_MTt);
stdFS_MTt = nanstd(GroupData.Summary_Behavior.FS_MTt);

figure()
axes('box','off','tickdir','out','LineWidth',1,'FontSize',14)
beeswarm_plot({GroupData.Summary_Behavior.Go_MT,...
   GroupData.Summary_Behavior.FS_MTt},...
    'color',[0.8,0.8,0.8; 0.98824,0.53725,0]);
set(gca,'XTick',1:2,'XTickLabel',{'Go.RT','FS.RTt'},'FontSize',14);
ylabel('Movement time (ms)','FontSize',14);
ylim([0, max(GroupData.Summary_Behavior.Go_MT)+200]);
title('Movement times (onset to response)','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
print(gcf,strcat(savePath, 'Group_Movement_Times.pdf'),'-dpdf','-r400');
% delete(gcf)

%% Mean TT
meanGo_TT = nanmean(GroupData.Summary_Behavior.Go_TT);
stdGo_TT = nanstd(GroupData.Summary_Behavior.Go_TT);

meanFS_TTt = nanmean(GroupData.Summary_Behavior.FS_TTt);
stdFS_TTt = nanstd(GroupData.Summary_Behavior.FS_TTt);

figure()
axes('box','off','tickdir','out','LineWidth',1,'FontSize',14)
beeswarm_plot({GroupData.Summary_Behavior.Go_TT,...
   GroupData.Summary_Behavior.FS_TTt},...
    'color',[0.8,0.8,0.8; 0.98824,0.53725,0]);
set(gca,'XTick',1:2,'XTickLabel',{'Go.RT','FS.RTt'},'FontSize',14);
ylabel('Total response time (ms)','FontSize',14);
ylim([0, max(GroupData.Summary_Behavior.Go_TT)+200]);
title('Total response times','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
print(gcf,strcat(savePath, 'Group_Total_Times.pdf'),'-dpdf','-r400');
% delete(gcf);

%% RT, MT and Total time variability
figure()
axes('box','off','tickdir','out','LineWidth',1,'FontSize',14)
beeswarm_plot({GroupData.Summary_Behavior.Go_RTstd,...
   GroupData.Summary_Behavior.Go_MTstd,...
   GroupData.Summary_Behavior.Go_TTstd},...
    'color',[0.8,0.8,0.8; 0.98824,0.53725,0;0.98824,0.53725,0]);
set(gca,'XTick',1:3,'XTickLabel',{'Go.RTstd','Go.MTstd', 'Go.TTtstd'},'FontSize',14);
ylabel('Reaction time std. (ms)','FontSize',14);
ylim([0, max(GroupData.Summary_Behavior.Go_TTstd)+50]);
title('Standard deviation of response times','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
print(gcf,strcat(savePath, 'Group_Std_Go_Times.pdf'),'-dpdf','-r400');
% delete(gcf);

%% Plot pStop
meanpStoph = nanmean(GroupData.Summary_Behavior.pStoph);
stdpStoph = nanstd(GroupData.Summary_Behavior.pStoph);

meanpStopt = nanmean(GroupData.Summary_Behavior.pStopt);
stdpStopt = nanstd(GroupData.Summary_Behavior.pStopt);

figure()
axes('box','off','tickdir','out','LineWidth',1,'FontSize',14);
beeswarm_plot({GroupData.Summary_Behavior.pStoph GroupData.Summary_Behavior.pStopt},...
    'color',[0.8,0.8,0.8;0.98824,0.53725,0]);
set(gca,'XTick',1:2,'XTickLabel',{'Home','Target'},'FontSize',14);
ylabel('pStop (%)','FontSize',14);
ylim([0, 100]);
title('Probability of stopping','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
print(gcf,strcat(savePath,'GroupBehav_pStop.pdf'),'-dpdf','-r400');
% delete(gcf)

%% Plot meanSSD
meanSSDh = nanmean(GroupData.Summary_Behavior.SSDh);
stdSSDh = nanstd(GroupData.Summary_Behavior.SSDh);

meanSSDt = nanmean(GroupData.Summary_Behavior.SSDt);
stdpSSDt = nanstd(GroupData.Summary_Behavior.SSDt);

figure()
axes('box','off','tickdir','out','LineWidth',1,'FontSize',14);
beeswarm_plot({GroupData.Summary_Behavior.SSDh GroupData.Summary_Behavior.SSDt},...
    'color',[0.8,0.8,0.8;0.98824,0.53725,0]);
set(gca,'XTick',1:2,'XTickLabel',{'Home','Target'},'FontSize',14);
ylabel('SSD (ms)','FontSize',14);
ylim([0, max(GroupData.Summary_Behavior.SSDt)+100]);
title('Stop signal delay','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
print(gcf,strcat(savePath,'GroupBehav_meanSSD.pdf'),'-dpdf','-r400');
% delete(gcf)

%% Plot SSRT
meanSSRTh_RT = nanmean(GroupData.Summary_Behavior.SSRTh_RT);
stdSSRTh_RT = nanstd(GroupData.Summary_Behavior.SSRTh_RT);

% meanSSRTt_MT = nanmean(GroupData.Summary_Behavior.SSRTt_MT);
% stdpSSRTt_MT = nanstd(GroupData.Summary_Behavior.SSRTt_MT);

meanSSRTt_TT = nanmean(GroupData.Summary_Behavior.SSRTt_TT);
stdpSSRTt_TT = nanstd(GroupData.Summary_Behavior.SSRTt_TT);

figure()
axes('box','off','tickdir','out','LineWidth',1,'FontSize',14);
beeswarm_plot({GroupData.Summary_Behavior.SSRTh_RT GroupData.Summary_Behavior.SSRTt_TT},...
    'color',[0.8,0.8,0.8;0.98824,0.53725,0]);
set(gca,'XTick',1:2,'XTickLabel',{'Home','Target'},'FontSize',14);
ylabel('SSRT (ms)','FontSize',14);
ylim([0, max([GroupData.Summary_Behavior.SSRTh_RT;GroupData.Summary_Behavior.SSRTt_TT])+100]);
title('SSRT','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
print(gcf,strcat(savePath,'GroupBehav_SSRT.pdf'),'-dpdf','-r400');
% delete(gcf)

%% Plot Goerr
meanGoOmit = nanmean(GroupData.Summary_Behavior.GoOmit);
stdGoOmit = nanstd(GroupData.Summary_Behavior.GoOmit);

meanGoErr = nanmean(GroupData.Summary_Behavior.GoErr);
stdGoErr = nanstd(GroupData.Summary_Behavior.GoErr);

meanGoMiss = nanmean(GroupData.Summary_Behavior.GoMiss);
stdpGoMiss = nanstd(GroupData.Summary_Behavior.GoMiss);

% falsealarms = GroupData.Summary_Behavior.GoErr - (GroupData.Summary_Behavior.GoMiss +...
%     GroupData.Summary_Behavior.GoOmit);
% meanGoFA = nanmean(falsealarms);
% stdpGoFA = nanstd(falsealarms);

figure()
axes('box','off','tickdir','out','LineWidth',1,'FontSize',14);
beeswarm_plot({GroupData.Summary_Behavior.GoOmit GroupData.Summary_Behavior.GoMiss ...
    GroupData.Summary_Behavior.FalseAlarm},...
    'color',[0.8,0.8,0.8;0.98824,0.53725,0;0.98824,0.53725,0]);
set(gca,'XTick',1:3,'XTickLabel',{'Omit','Miss','FalseAlarm'},'FontSize',14);
ylabel('Errors (%)','FontSize',14);
ylim([0, max(GroupData.Summary_Behavior.GoErr)+5]);
title('Types of errors','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
print(gcf,strcat(savePath,'GroupBehav_Errors.pdf'),'-dpdf','-r400');
% delete(gcf)

%% Relationship between SSRT for home and target staircases
[r,p] = corr(GroupData.Summary_Behavior.SSRTh_RT,...
    GroupData.Summary_Behavior.SSRTt_TT);

% Robust fit option on corr?
% [b,stats] = robustfit(GroupData.Summary_Behavior.SSRTh_RT,...
%     GroupData.Summary_Behavior.SSRTt_TT)
% corr_result = fitlm(GroupData.Summary_Behavior.SSRTh_RT,...
%     GroupData.Summary_Behavior.SSRTt_TT, 'RobustOpts','on')
%     r = sqrt(corr_result.Rsquared.Adjusted)
%     p = 
    
bf10 = corrbf(r,length(GroupData.Summary_Behavior.SSRTh_RT));

figure();
axes('box','off','tickdir','out','LineWidth',1.25,'FontSize',16)
scatter(GroupData.Summary_Behavior.SSRTh_RT,...
    GroupData.Summary_Behavior.SSRTt_TT, 80, 'k', 'MarkerFaceColor','k'); % MNI target set at start
xlim([0 600]);
lsline;
ylim([0 600]);
hold on
plot([0,600],[0,600],'--r');
r_txt = ['r = ' num2str(round(r,3))];
p_txt = ['p = ' num2str(round(p,3))];
bf_txt = ['BF10 = ' num2str(round(bf10,1))];
tx_x = xlim;
text(tx_x(2)-200,95,r_txt, 'FontSize',16);
text(tx_x(2)-200,65,p_txt,'FontSize',16);
text(tx_x(2)-200,35,bf_txt,'FontSize',16);
ylabel('SSRT target (ms)','FontSize',16)
xlabel('SSRT home (ms)','FontSize',16);
title('SSRT correlation','FontWeight','bold','FontSize',16);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
print(gcf,strcat(savePath,'SSRTcorr_Home_Target.pdf'),'-dpdf','-r400');
% delete(gcf)

%% Trigger failures measured in two basic ways
% 1. how many times on the Home staircase did people completely fail to stop 
% and actually hit the target?
% Number
figure()
axes('box','off','tickdir','out','LineWidth',1,'FontSize',14);
beeswarm_plot({GroupData.Summary_Behavior.N_TFh},...
    'color',[0.8,0.8,0.8]);
set(gca,'XTick',1,'XTickLabel',{'TFh'},'FontSize',14);
ylabel('TF count','FontSize',14);
ylim([0, max(GroupData.Summary_Behavior.N_TFh)+5]);
title('How often was target hit on home staircase','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
print(gcf,strcat(savePath,'GroupBehav_nTF_TargHit_home.pdf'),'-dpdf','-r400');
% delete(gcf)

% Percentage TF / omisssions in Go trials and FSh stair
n_stop_trls = 18 * 32 * .5 *.25;  % 18 blocks 32 trials total, half it for each stair case and * .25 to get stop trials
pcnt_TFh = GroupData.Summary_Behavior.N_TFh./ n_stop_trls * 100;

meanpcnt_TFh = round(nanmean(pcnt_TFh),1);
stdpcnt_TFh = round(nanstd(pcnt_TFh),1);

go_data = GroupData.Summary_Behavior.GoOmit;

[h,p,ci,stats] = ttest(go_data,pcnt_TFh);
bf10 = t1smpbf(stats.tstat,length(go_data));

figure()
axes('box','off','tickdir','out','LineWidth',1,'FontSize',14);
beeswarm_plot({go_data pcnt_TFh},...
    'color',[0.8,0.8,0.8; 0.98824,0.53725, 0]);
set(gca,'XTick',1:2,'XTickLabel',{'Go','TFh'},'FontSize',14);
ylabel('TF (%)','FontSize',14);
ylim([0, max(pcnt_TFh)+5]);
title('How often was Go or Stop omitted (TF)','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
print(gcf,strcat(savePath,'GroupBehav_pcntOmit_pcntTF_TargHit_home.pdf'),'-dpdf','-r400');
% delete(gcf)

% 2. How soon did people stop on failed stop trials for the home staircase?
% if it's really late (e.g. 200+ ms after SSRT or the time of stopping in
% successful target staircase trials) then it could conceivably be a TF
% followed by an error-related, delay triggering of stop.
figure()
axes('box','off','tickdir','out','LineWidth',1,'FontSize',14);
beeswarm_plot({GroupData.time_maxY.Goh_mn ...
    GroupData.time_maxY.FSh_mn ...
    GroupData.time_maxY.Got_mn ...
    GroupData.time_maxY.SSt_mn},...
    'color',[0.8,0.8,0.8; 0.98824,0.53725, 0;0.8,0.8,0.8; 0.98824,0.53725, 0]);
set(gca,'XTick',1:4,'XTickLabel',{'Goh','FSh','Got','SSt'},'FontSize',14);
ylabel('Time Y max wrt home stop (ms)','FontSize',14);
ylim([0, max(GroupData.time_maxY.Goh_mn)+100]);
title('When after stop did Y pos reach its peak? Home Stair','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
print(gcf,strcat(savePath,'GroupBehav_Time_Ymax_wrtStop_FS_home.pdf'),'-dpdf','-r400');
% delete(gcf)

% Same but just focusing on the home and target staircase FS and SS trials
figure()
axes('box','off','tickdir','out','LineWidth',1,'FontSize',14);
beeswarm_plot({GroupData.time_maxY.FSh_mn GroupData.time_maxY.SSt_mn},...
    'color',[0.8,0.8,0.8; 0.98824,0.53725, 0]);
set(gca,'XTick',1:2,'XTickLabel',{'FSh','SSt'},'FontSize',14);
ylabel('Time Y max wrt target stop (ms)','FontSize',14);
ylim([0, max(GroupData.time_maxY.Got_mn)+100]);
title('When after stop did Y pos reach its peak? H vs T','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
print(gcf,strcat(savePath,'GroupBehav_Time_Ymax_wrtStop_FS_home_SS_Targ.pdf'),'-dpdf','-r400');
% delete(gcf)

% Mean vs min vs max of Y max times for the home and target staircase FS and SS trials
figure()
axes('box','off','tickdir','out','LineWidth',1,'FontSize',14);
beeswarm_plot({GroupData.time_maxY.FSh_Min ...
    GroupData.time_maxY.FSh_mn ...
    GroupData.time_maxY.FSh_Max...
    GroupData.time_maxY.SSt_Min ...
    GroupData.time_maxY.SSt_mn ...
    GroupData.time_maxY.SSt_Max}, ...
    'color',[0.8,0.8,0.8; 0.98824,0.53725,0; 0.8,0.8,0.8;0.8,0.8,0.8; 0.98824,0.53725,0; 0.8,0.8,0.8]);
set(gca,'XTick',1:6,'XTickLabel',{'FShMin','FShMean','FShMax','SStMin','SStMean','SStMax'},'FontSize',14);
ylabel('Time Y max wrt stop (ms)','FontSize',14);
ylim([0, max(GroupData.time_maxY.FSh_Max)+100]);
title('When after stop did Y pos reach its peak?','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
print(gcf,strcat(savePath,'GroupBehav_Time_Ymax_MnMinMaxwrtStop.pdf'),'-dpdf','-r400');
% delete(gcf)

% Standard deviation of Y max times for Go, FSh and SSt trials
figure()
axes('box','off','tickdir','out','LineWidth',1,'FontSize',14);
beeswarm_plot({GroupData.time_maxY.Goh_std ...
    GroupData.time_maxY.FSh_std ...
    GroupData.time_maxY.Got_std ...
    GroupData.time_maxY.SSt_std},...
    'color',[0.8,0.8,0.8; 0.98824,0.53725, 0;0.8,0.8,0.8; 0.98824,0.53725, 0]);
set(gca,'XTick',1:4,'XTickLabel',{'Goh','FSh','Got','SSt'},'FontSize',14);
ylabel('Time Y max Std. wrt stop (ms)','FontSize',14);
ylim([0, max(GroupData.time_maxY.FSh_std)+100]);
title('When after stop did Y pos reach its peak? H vs T','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
print(gcf,strcat(savePath,'GroupBehav_Time_Ymax_wrtStop_Stdev.pdf'),'-dpdf','-r400');
% delete(gcf)

% Is there another way to capture TFs? Could some of the Failed Stop Home
% Y max times contain stop outliers representing TFs followed by
% error-related triggering of stop at a later time?

% How frequent were outliers?
idx_FSh_timeYmax_outliers = GroupData.time_maxY.Nh_outliers > 0;
figure()
axes('box','off','tickdir','out','LineWidth',1,'FontSize',14);
beeswarm_plot({GroupData.time_maxY.Nh_outliers(idx_FSh_timeYmax_outliers)},...
    'color',[0.8,0.8,0.8]);
set(gca,'XTick',1,'XTickLabel',{'FSh time Ymax'},'FontSize',14);
ylabel('N outliers','FontSize',14);
ylim([0, 10]);
title('Outliers in FSh time of Y max: How frequent were they?','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
print(gcf,strcat(savePath,'GroupBehav_Time_Ymax_wrtStop_OutliersFreq.pdf'),'-dpdf','-r400');

% How do outliers compare to mean FSh and SSt for those people
% Does this make sense? Already shown that they're outliers with respect to
% their own actual FSh distribution...
idx_FSh_timeYmax_outliers = GroupData.time_maxY.Nh_outliers > 0; 
figure()
axes('box','off','tickdir','out','LineWidth',1,'FontSize',14);
beeswarm_plot({GroupData.time_maxY.TimeonFSh_mn(idx_FSh_timeYmax_outliers) ...
    GroupData.time_maxY.TimeonFSh_Outlier(idx_FSh_timeYmax_outliers), ...
    GroupData.time_maxY.FSh_mn(idx_FSh_timeYmax_outliers) ...
    GroupData.time_maxY.FSh_Out(idx_FSh_timeYmax_outliers),...
     GroupData.time_maxY.SSt_mn(idx_FSh_timeYmax_outliers) ...
    GroupData.time_maxY.SSt_mn(idx_FSh_timeYmax_outliers) + (3* GroupData.time_maxY.SSt_std(idx_FSh_timeYmax_outliers))},...
    'color',[0.8,0.8,0.8; 0.98824,0.53725, 0;0.8,0.8,0.8; 0.98824,0.53725, 0;0.8,0.8,0.8; 0.98824,0.53725, 0]);
set(gca,'XTick',1:6,'XTickLabel',{'FShonset','FShonset_o_u_t','FSh','FSh_o_u_t' ,'SSt','SSt+3SD'},'FontSize',14);
ylabel('Time Y onset/max wrt stop (ms)','FontSize',14);
ylim([0, max(GroupData.time_maxY.FSh_Out(idx_FSh_timeYmax_outliers))+200]);
title('Outliers in FSh time of Y max: Are they TFs?','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
print(gcf,strcat(savePath,'GroupBehav_Time_Ymax_wrtStop_Outliers.pdf'),'-dpdf','-r400');


% % Were outliers associated with late movement onsets too? Generally not
% % paying attention or interaction between go and stop
% % Plot onset of movement time outliers agains the mean time of Ymax
% figure()
% scatter( GroupData.time_maxY.TimeonFSh_Outlier, GroupData.time_maxY.FSh_Out);
% lsline
% xlim([0,max(GroupData.time_maxY.FSh_Out)+100]);
% ylim([0,max(GroupData.time_maxY.FSh_Out)+100]);
% ylabel('Time Y max outliers (ms)','FontSize',14);
% xlabel('Time Y onset outliers (ms)','FontSize',14);
% ylim([0, max(GroupData.time_maxY.FSh_Out)+100]);
% title('FS Ymax outliers: onset versus Ymax time','FontWeight','bold','FontSize',14);
% set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
% print(gcf,strcat(savePath,'GroupBehav_Outlier_Time_OnsetvsYmax_wrtStop.pdf'),'-dpdf','-r400');
% delete(gcf)

% Scatter of FSh outliers to show which ones exceeded SSt+3SD 
% Most don't exceed by enough to suggest it's a TF-error-triggered stop
% only 1 exceeds by about 200 ms (>5SD), and could be considered that. 
figure()
scatter(GroupData.time_maxY.SSt_mn(idx_FSh_timeYmax_outliers) + ...
    (3 * GroupData.time_maxY.SSt_std(idx_FSh_timeYmax_outliers)), ...
    GroupData.time_maxY.FSh_Out(idx_FSh_timeYmax_outliers),'MarkerFaceColor','k');
hold on
plot([0,1200],[0,1200],'--r');
ylabel('FSh time Ymax outlier','FontSize',14);
xlabel('SSt time Y max + 3SD','FontSize',14);
ylim([0,1200]);
xlim([0,1200]);
title('Do FSh Ymax time outliers exceed SSt+3SD?','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
print(gcf,strcat(savePath,'GroupBehav_Time_Ymax_OutliersFSh_vs_SSt3SD.pdf'),'-dpdf','-r400');

% Are these the same people who have TFs? 
count_both_TF_outlier = sum(GroupData.time_maxY.Nh_outliers > 0 & ...
    GroupData.Summary_Behavior.N_TFh > 0);
figure()
scatter(GroupData.time_maxY.Nh_outliers, ...
    GroupData.Summary_Behavior.N_TFh,'MarkerFaceColor','k')
ylabel('N FSh Time Ymax outlier','FontSize',14);
xlabel('N TFs','FontSize',14);
ylim([0,5]);
xlim([0,5]);
title('Are people with FSh time outliers same as those with TFs?','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
text(0.5,4.5,strcat('People with TF: N = ', num2str(sum(GroupData.Summary_Behavior.N_TFh > 0))), 'FontSize',14);
text(0.5,4.2,strcat('People with FSh time Y outlier: N = ', num2str(sum(GroupData.time_maxY.Nh_outliers > 0))), 'FontSize',14);
text(0.5,3.9,strcat('People with both outlier and TF: N = ', num2str(count_both_TF_outlier)), 'FontSize',14);
print(gcf,strcat(savePath,'GroupBehav_Time_Ymax_Outliers_vs_TFs.pdf'),'-dpdf','-r400');


%% Were TF responses exceptionally fast or slow? 
% If fast, stop failure could simply be due to Go winning the race, rather
% than a failure to trigger the stop.
% If slow, then conceivably it was a trigger failure. It was presumably
% also accompanied by a late or slow rising go process, which could
% indicate some interaction? 

% What percentage of people with TFs had response times for TFs were faster 
% (and slower) than the mean Go response times
idx_TF = GroupData.Summary_Behavior.N_TFh > 0;
idx_TFRT_less_GoRT = GroupData.Summary_Behavior.TF_RT(idx_TF) < GroupData.Summary_Behavior.Go_RT(idx_TF);
idx_TFMT_less_GoMT = GroupData.Summary_Behavior.TF_MT(idx_TF) < GroupData.Summary_Behavior.Go_MT(idx_TF);
idx_TFTT_less_GoTT = GroupData.Summary_Behavior.TF_TT(idx_TF) < GroupData.Summary_Behavior.Go_TT(idx_TF);

pcnt_TFRT_less_GoRT = sum(idx_TFRT_less_GoRT) / length(idx_TFRT_less_GoRT) * 100;
pcnt_TFMT_less_GoMT = sum(idx_TFMT_less_GoMT) / length(idx_TFMT_less_GoMT) * 100;
pcnt_TFTT_less_GoTT = sum(idx_TFTT_less_GoTT) / length(idx_TFTT_less_GoTT) * 100;

% Were any faster / slower by more than 3 SD of the mean GO response time
idx_TFTT_less_GoTT_3SD = GroupData.Summary_Behavior.TF_TT(idx_TF) < GroupData.Summary_Behavior.Go_TT(idx_TF) - ...
    (3 * GroupData.Summary_Behavior.Go_TTstd(idx_TF));
idx_TFTT_more_GoTT_3SD = GroupData.Summary_Behavior.TF_TT(idx_TF) > GroupData.Summary_Behavior.Go_TT(idx_TF) + ...
    (3 * GroupData.Summary_Behavior.Go_TTstd(idx_TF));

n_TFTT_less_GoTT_3SD = sum(idx_TFTT_less_GoTT_3SD);
n_TFTT_more_GoTT_3SD = sum(idx_TFTT_more_GoTT_3SD);

% Were they fast or slow with respect to the latency of stopping. 
% In other words, given the response time and the SSD TF trials, could the
% stop process reasonably have caught the go process?
% Use the SSRT and distribution of Y max times from successful stops in
% target staircase to estimate.
% How long did they have between SSD and final response to stop?
window4stop = GroupData.Summary_Behavior.TF_TT(idx_TF) - GroupData.Summary_Behavior.TF_SSD(idx_TF);

% How many people had response times that should have been caught by 95% of
% stop processes if they were triggered?
% GroupData.time_maxY.TFFSh_mn(idx_TF); %-------------------------
%------------------- re-run individual analysis because 'outliers' were
%removed from Y max times, and this includes a couple of TFs.
% for now just use SSRT and Y max STD
window_minus_maxStopLat = window4stop - (GroupData.Summary_Behavior.SSRTt_TT(idx_TF) + ...
    (3 * GroupData.time_maxY.SSt_std(idx_TF)));
n_maxStopLat_more_window = sum(window_minus_maxStopLat > 0);

GroupData.TriggerFailure_ResponseTimes = array2table([ ...
    sum(idx_TF), n_TFTT_less_GoTT_3SD, ...
    n_TFTT_more_GoTT_3SD, n_maxStopLat_more_window],...
    'VariableNames',{'N_peopleTF','TTTFmoreTTGO3SD',...
    'TTTFmoreTFTTGO3SD','TTTFmoreSSRT3SD'})


%% Is Y max time a good indicator of stopping latency?
% Correlations across all Y max stopping times and SSRTs to examine
% relationships

array_StopTimes = [ GroupData.Summary_Behavior.SSRTh_RT, GroupData.Summary_Behavior.SSRTt_TT, ...
    GroupData.time_maxY.FSh_mn,  GroupData.time_maxY.SSt_mn];

[r,p] = corrcoef(array_StopTimes);

figure()
subplot(3,2,1)
bf10 = corrbf(r(1,2),length(subjs));
% axes('box','off','tickdir','out','LineWidth',1.25,'FontSize',16)
scatter(GroupData.Summary_Behavior.SSRTh_RT,...
    GroupData.Summary_Behavior.SSRTt_TT,45, 'k', 'MarkerFaceColor','k'); % MNI target set at start
xlim([0 600]);
lsline;
ylim([0 600]);
hold on
plot([0,600],[0,600],'--r');
r_txt = ['r = ' num2str(round(r(1,2),3))];
p_txt = ['p = ' num2str(round(p(1,2),3))];
bf_txt = ['BF10 = ' num2str(round(bf10,1))];
tx_x = xlim;
text(tx_x(2)-250,200,r_txt, 'FontSize',12);
text(tx_x(2)-250,120,p_txt,'FontSize',12);
text(tx_x(2)-250,40,bf_txt,'FontSize',12);
ylabel('SSRT target (ms)','FontSize',12)
xlabel('SSRT home (ms)','FontSize',12);
title('SSRT H v T','FontWeight','bold','FontSize',12);

subplot(3,2,2)
bf10 = corrbf(r(1,3),length(subjs));
% axes('box','off','tickdir','out','LineWidth',1.25,'FontSize',12)
scatter(GroupData.Summary_Behavior.SSRTh_RT,...
    GroupData.time_maxY.FSh_mn,45, 'k', 'MarkerFaceColor','k'); % MNI target set at start
xlim([0 600]);
lsline;
ylim([0 600]);
hold on
plot([0,600],[0,600],'--r');
r_txt = ['r = ' num2str(round(r(1,3),3))];
p_txt = ['p = ' num2str(round(p(1,3),3))];
bf_txt = ['BF10 = ' num2str(round(bf10,1))];
tx_x = xlim;
text(tx_x(2)-250,200,r_txt, 'FontSize',12);
text(tx_x(2)-250,120,p_txt,'FontSize',12);
text(tx_x(2)-250,40,bf_txt,'FontSize',12);
ylabel('Time Ymax FS-H (ms)','FontSize',12)
xlabel('SSRT home (ms)','FontSize',12);
title('SSRT H v FS Tymax H','FontWeight','bold','FontSize',12);

subplot(3,2,3)
bf10 = corrbf(r(1,4),length(subjs));
% axes('box','off','tickdir','out','LineWidth',1.25,'FontSize',12)
scatter(GroupData.Summary_Behavior.SSRTh_RT,...
    GroupData.time_maxY.SSt_mn,45, 'k', 'MarkerFaceColor','k'); % MNI target set at start
xlim([0 600]);
lsline;
ylim([0 600]);
hold on
plot([0,600],[0,600],'--r');
r_txt = ['r = ' num2str(round(r(1,4),3))];
p_txt = ['p = ' num2str(round(p(1,4),3))];
bf_txt = ['BF10 = ' num2str(round(bf10,1))];
tx_x = xlim;
text(tx_x(2)-250,200,r_txt, 'FontSize',12);
text(tx_x(2)-250,120,p_txt,'FontSize',12);
text(tx_x(2)-250,40,bf_txt,'FontSize',12);
ylabel('Time Ymax SS-T (ms)','FontSize',12)
xlabel('SSRT home (ms)','FontSize',12);
title('SSRT H v SS Tymax T','FontWeight','bold','FontSize',12);

subplot(3,2,4)
bf10 = corrbf(r(2,3),length(subjs));
% axes('box','off','tickdir','out','LineWidth',1.25,'FontSize',12)
scatter(GroupData.Summary_Behavior.SSRTt_TT,...
    GroupData.time_maxY.FSh_mn,45, 'k', 'MarkerFaceColor','k'); % MNI target set at start
xlim([0 600]);
lsline;
ylim([0 600]);
hold on
plot([0,600],[0,600],'--r');
r_txt = ['r = ' num2str(round(r(2,3),3))];
p_txt = ['p = ' num2str(round(p(2,3),3))];
bf_txt = ['BF10 = ' num2str(round(bf10,1))];
tx_x = xlim;
text(tx_x(2)-250,200,r_txt, 'FontSize',12);
text(tx_x(2)-250,120,p_txt,'FontSize',12);
text(tx_x(2)-250,40,bf_txt,'FontSize',12);
ylabel('Time Ymax FS-H (ms)','FontSize',12)
xlabel('SSRT targ (ms)','FontSize',12);
title('SSRT T v FS Tymax H','FontWeight','bold','FontSize',12);

subplot(3,2,5)
bf10 = corrbf(r(2,4),length(subjs));
% axes('box','off','tickdir','out','LineWidth',1.25,'FontSize',12)
scatter(GroupData.Summary_Behavior.SSRTt_TT,...
    GroupData.time_maxY.SSt_mn,45, 'k', 'MarkerFaceColor','k'); % MNI target set at start
xlim([0 600]);
lsline;
ylim([0 600]);
hold on
plot([0,600],[0,600],'--r');
r_txt = ['r = ' num2str(round(r(2,4),3))];
p_txt = ['p = ' num2str(round(p(2,4),3))];
bf_txt = ['BF10 = ' num2str(round(bf10,1))];
tx_x = xlim;
text(tx_x(2)-250,200,r_txt, 'FontSize',12);
text(tx_x(2)-250,120,p_txt,'FontSize',12);
text(tx_x(2)-250,40,bf_txt,'FontSize',12);
ylabel('Time Ymax SS-T (ms)','FontSize',12)
xlabel('SSRT targ (ms)','FontSize',12);
title('SSRT T v SS Tymax T','FontWeight','bold','FontSize',12);

subplot(3,2,6)
bf10 = corrbf(r(3,4),length(subjs));
% axes('box','off','tickdir','out','LineWidth',1.25,'FontSize',12)
scatter(GroupData.time_maxY.FSh_mn,...
    GroupData.time_maxY.SSt_mn,45, 'k', 'MarkerFaceColor','k'); % MNI target set at start
xlim([0 600]);
lsline;
ylim([0 600]);
hold on
plot([0,600],[0,600],'--r');
r_txt = ['r = ' num2str(round(r(3,4),3))];
p_txt = ['p = ' num2str(round(p(3,4),3))];
bf_txt = ['BF10 = ' num2str(round(bf10,1))];
tx_x = xlim;
text(tx_x(2)-250,200,r_txt, 'FontSize',12);
text(tx_x(2)-250,120,p_txt,'FontSize',12);
text(tx_x(2)-250,40,bf_txt,'FontSize',12);
ylabel('Time Ymax SS-T (ms)','FontSize',12)
xlabel('Time Ymax FS-H (ms)','FontSize',12);
title('FS Tymax H v SS Tymax T','FontWeight','bold','FontSize',12);

print(gcf,strcat(savePath,'GroupBehav_StopTime_correlations.pdf'),'-dpdf','-r400');
% delete(gcf)

%% Import BEESTS text file *** Home stair case ***

opts = delimitedTextImportOptions("NumVariables", 5);

% Specify range and delimiter
opts.DataLines = [1, Inf];
opts.Delimiter = "\t\t";

% Specify column names and types
opts.VariableNames = ["Variables","Mean", "SD", "NaiveSE", "TimeseriesSE"];
opts.VariableTypes = ["categorical", "double", "double", "double", "double"];
opts = setvaropts(opts, 1, "EmptyFieldRule", "auto");
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";

% Import the data
BEESTS_OnlineStopTask1_Home = readtable("/Users/riccihannah/Google Drive/Online_Stop_Signal/Online_Stop_Task/Task_replication/data/group/Summary_OnlineStop_Task1_Home.txt", opts);

%% Clear temporary variables
clear opts

%% BEESTS summary *** Home stair case ***

idx_GoRTmn_Home = BEESTS_OnlineStopTask1_Home.Variables == 'mu.true';
idx_GoRTstd_Home = BEESTS_OnlineStopTask1_Home.Variables == 'sigma.true';
idx_GoRTtau_Home = BEESTS_OnlineStopTask1_Home.Variables == 'tau.true';
idx_SSRTmn_Home = BEESTS_OnlineStopTask1_Home.Variables == 'muS';
idx_SSRTstd_Home = BEESTS_OnlineStopTask1_Home.Variables == 'sigmaS';
idx_tf_Home = BEESTS_OnlineStopTask1_Home.Variables == 'tf';

BEESTS_OnlineStop_Summary(1:sum(idx_GoRTmn_Home),1) = round(BEESTS_OnlineStopTask1_Home.Mean(idx_GoRTmn_Home) .* 1000);
BEESTS_OnlineStop_Summary(1:sum(idx_GoRTmn_Home),2) = round(BEESTS_OnlineStopTask1_Home.SD(idx_GoRTmn_Home) .* 1000);

BEESTS_OnlineStop_Summary(1:sum(idx_GoRTstd_Home),3) = round(BEESTS_OnlineStopTask1_Home.Mean(idx_GoRTstd_Home) .* 1000);
BEESTS_OnlineStop_Summary(1:sum(idx_GoRTstd_Home),4) = round(BEESTS_OnlineStopTask1_Home.SD(idx_GoRTstd_Home) .* 1000);

BEESTS_OnlineStop_Summary(1:sum(idx_GoRTtau_Home),5) = round(BEESTS_OnlineStopTask1_Home.Mean(idx_GoRTtau_Home) .* 1000);
BEESTS_OnlineStop_Summary(1:sum(idx_GoRTtau_Home),6) = round(BEESTS_OnlineStopTask1_Home.SD(idx_GoRTtau_Home) .* 1000);

BEESTS_OnlineStop_Summary(1:sum(idx_SSRTmn_Home),7) = round(BEESTS_OnlineStopTask1_Home.Mean(idx_SSRTmn_Home) .* 1000);
BEESTS_OnlineStop_Summary(1:sum(idx_SSRTmn_Home),8) = round(BEESTS_OnlineStopTask1_Home.SD(idx_SSRTmn_Home) .* 1000);

BEESTS_OnlineStop_Summary(1:sum(idx_SSRTstd_Home),9) = round(BEESTS_OnlineStopTask1_Home.Mean(idx_SSRTstd_Home) .* 1000);
BEESTS_OnlineStop_Summary(1:sum(idx_SSRTstd_Home),10) = round(BEESTS_OnlineStopTask1_Home.SD(idx_SSRTstd_Home) .* 1000);

BEESTS_OnlineStop_Summary(1:sum(idx_tf_Home),11) = round(BEESTS_OnlineStopTask1_Home.Mean(idx_tf_Home) .* 100);
BEESTS_OnlineStop_Summary(1:sum(idx_tf_Home),12) = round(BEESTS_OnlineStopTask1_Home.SD(idx_tf_Home) .* 100);

GroupData.BEESTS_Summary_Home = array2table(BEESTS_OnlineStop_Summary, 'VariableNames', ...
    {'GoRTmn','GoRTmnstd','GoRTstd','GoRTstdstd','GoRTtau','GoRTtaustd','SSRTmn','SSRTmnstd','SSRTstd','SSRTstdstd','tfmn','tfmnstd'});


%% Import BEESTS text file *** Target stair case ***

opts = delimitedTextImportOptions("NumVariables", 5);

% Specify range and delimiter
opts.DataLines = [1, Inf];
opts.Delimiter = "\t\t";

% Specify column names and types
opts.VariableNames = ["Variables","Mean", "SD", "NaiveSE", "TimeseriesSE"];
opts.VariableTypes = ["categorical", "double", "double", "double", "double"];
opts = setvaropts(opts, 1, "EmptyFieldRule", "auto");
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";

% Import the data
BEESTS_OnlineStopTask1_Target = readtable("/Users/riccihannah/Google Drive/Online_Stop_Signal/Online_Stop_Task/Task/data/group/Summary_OnlineStop_Task1_Target.txt", opts);

%% Clear temporary variables
clear opts

%% BEESTS summary *** Target stair case ***

idx_GoRTmn_Target = BEESTS_OnlineStopTask1_Target.Variables == 'mu.true';
idx_GoRTstd_Target = BEESTS_OnlineStopTask1_Target.Variables == 'sigma.true';
idx_GoRTtau_Target = BEESTS_OnlineStopTask1_Target.Variables == 'tau.true';
idx_SSRTmn_Target = BEESTS_OnlineStopTask1_Target.Variables == 'muS';
idx_SSRTstd_Target = BEESTS_OnlineStopTask1_Target.Variables == 'sigmaS';
idx_tf_Target = BEESTS_OnlineStopTask1_Target.Variables == 'tf';

BEESTS_OnlineStop_Summary_2(1:sum(idx_GoRTmn_Target),1) = round(BEESTS_OnlineStopTask1_Target.Mean(idx_GoRTmn_Target) .* 1000);
BEESTS_OnlineStop_Summary_2(1:sum(idx_GoRTmn_Target),2) = round(BEESTS_OnlineStopTask1_Target.SD(idx_GoRTmn_Target) .* 1000);

BEESTS_OnlineStop_Summary_2(1:sum(idx_GoRTstd_Target),3) = round(BEESTS_OnlineStopTask1_Target.Mean(idx_GoRTstd_Target) .* 1000);
BEESTS_OnlineStop_Summary_2(1:sum(idx_GoRTstd_Target),4) = round(BEESTS_OnlineStopTask1_Target.SD(idx_GoRTstd_Target) .* 1000);

BEESTS_OnlineStop_Summary_2(1:sum(idx_GoRTtau_Target),5) = round(BEESTS_OnlineStopTask1_Target.Mean(idx_GoRTtau_Target) .* 1000);
BEESTS_OnlineStop_Summary_2(1:sum(idx_GoRTtau_Target),6) = round(BEESTS_OnlineStopTask1_Target.SD(idx_GoRTtau_Target) .* 1000);

BEESTS_OnlineStop_Summary_2(1:sum(idx_SSRTmn_Target),7) = round(BEESTS_OnlineStopTask1_Target.Mean(idx_SSRTmn_Target) .* 1000);
BEESTS_OnlineStop_Summary_2(1:sum(idx_SSRTmn_Target),8) = round(BEESTS_OnlineStopTask1_Target.SD(idx_SSRTmn_Target) .* 1000);

BEESTS_OnlineStop_Summary_2(1:sum(idx_SSRTstd_Target),9) = round(BEESTS_OnlineStopTask1_Target.Mean(idx_SSRTstd_Target) .* 1000);
BEESTS_OnlineStop_Summary_2(1:sum(idx_SSRTstd_Target),10) = round(BEESTS_OnlineStopTask1_Target.SD(idx_SSRTstd_Target) .* 1000);

BEESTS_OnlineStop_Summary_2(1:sum(idx_tf_Target),11) = round(BEESTS_OnlineStopTask1_Target.Mean(idx_tf_Target) .* 100);
BEESTS_OnlineStop_Summary_2(1:sum(idx_tf_Target),12) = round(BEESTS_OnlineStopTask1_Target.SD(idx_tf_Target) .* 100);

GroupData.BEESTS_Summary_Target = array2table(BEESTS_OnlineStop_Summary_2, 'VariableNames', ...
    {'GoRTmn','GoRTmnstd','GoRTstd','GoRTstdstd','GoRTtau','GoRTtaustd','SSRTmn','SSRTmnstd','SSRTstd','SSRTstdstd','tfmn','tfmnstd'});



%% Plot figures for BEESTS *** Home stair case ***
% Go RT BEESTS vs Standard
figure()
axes('box','off','tickdir','out','LineWidth',1,'FontSize',14)
beeswarm_plot({GroupData.Summary_Behavior.Go_RT ...
    GroupData.BEESTS_Summary_Home.GoRTmn ...
    GroupData.Summary_Behavior.Go_TT...
    GroupData.BEESTS_Summary_Target.GoRTmn},...
    'color',[0.8,0.8,0.8; 0.98824,0.53725,0;0.8,0.8,0.8; 0.98824,0.53725,0]);
set(gca,'XTick',1:4,'XTickLabel',{'Go.RT','Go.RTbeests','Go.TT','Go.TTbeests'},'FontSize',14);
ylabel('Reaction time (ms)','FontSize',14);
ylim([0, max(GroupData.Summary_Behavior.Go_TT + 200)]);
title('Reaction times vs BEESTS','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
print(gcf,strcat(savePath, 'Group_Reaction_Times_vs_BEESTS.pdf'),'-dpdf','-r400');
% delete(gcf)


% SSRT BEESTS vs my SSRT
figure()
axes('box','off','tickdir','out','LineWidth',1,'FontSize',14);
beeswarm_plot({GroupData.Summary_Behavior.SSRTh_RT ...
    GroupData.BEESTS_Summary_Home.SSRTmn ...
    GroupData.Summary_Behavior.SSRTt_TT...
    GroupData.BEESTS_Summary_Target.SSRTmn},...
    'color',[0.8,0.8,0.8; 0.98824,0.53725,0;0.8,0.8,0.8; 0.98824,0.53725,0]);
set(gca,'XTick',1:4,'XTickLabel',{'Home','Homebeests','Target','Targetbeests'},'FontSize',14);
ylabel('SSRT (ms)','FontSize',14);
ylim([0, max([GroupData.Summary_Behavior.SSRTh_RT;GroupData.Summary_Behavior.SSRTt_TT])+100]);
title('SSRT vs BEESTS','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
print(gcf,strcat(savePath,'GroupBehav_SSRT_vs_BEESTS.pdf'),'-dpdf','-r400');
% delete(gcf)

% Correlations between SSRT standard and BEESTS
figure()
subplot(3,2,1)
[r,p] = corr(GroupData.Summary_Behavior.SSRTh_RT,...
    GroupData.Summary_Behavior.SSRTt_TT);
bf10 = corrbf(r,length(subjs));
scatter(GroupData.Summary_Behavior.SSRTh_RT,...
    GroupData.Summary_Behavior.SSRTt_TT, 80, 'k','MarkerFaceColor','k')
xlim([0,600]);
lsline
ylim([0,600]);
xlabel('SSRT_h (ms)');
ylabel('SSRT_t (ms)');
r_txt = ['r = ' num2str(round(r,3))];
p_txt = ['p = ' num2str(round(p,3))];
bf_txt = ['BF10 = ' num2str(round(bf10,1))];
tx_x = xlim;
text(50,580,r_txt, 'FontSize',12);
text(50,500,p_txt,'FontSize',12);
text(50,420,bf_txt,'FontSize',12);
title('SSRT_h vs SSRT_t','FontWeight','bold','FontSize',14);

subplot(3,2,2)
[r,p] = corr(GroupData.Summary_Behavior.SSRTh_RT,...
    GroupData.BEESTS_Summary_Home.SSRTmn);
bf10 = corrbf(r,length(subjs));
scatter(GroupData.Summary_Behavior.SSRTh_RT,...
    GroupData.BEESTS_Summary_Home.SSRTmn, 80, 'k','MarkerFaceColor','k')
xlim([0,600]);
lsline
ylim([0,600]);
xlabel('SSRT_h (ms)');
ylabel('SSRT_hbeests (ms)');
r_txt = ['r = ' num2str(round(r,3))];
p_txt = ['p = ' num2str(round(p,3))];
bf_txt = ['BF10 = ' num2str(round(bf10,1))];
tx_x = xlim;
text(50,580,r_txt, 'FontSize',12);
text(50,500,p_txt,'FontSize',12);
text(50,420,bf_txt,'FontSize',12);
title('SSRT_h vs SSRT_hbeests','FontWeight','bold','FontSize',14);

subplot(3,2,3)
[r,p] = corr(GroupData.Summary_Behavior.SSRTh_RT,...
    GroupData.BEESTS_Summary_Target.SSRTmn);
bf10 = corrbf(r,length(subjs));
scatter(GroupData.Summary_Behavior.SSRTh_RT,...
    GroupData.BEESTS_Summary_Target.SSRTmn, 80, 'k','MarkerFaceColor','k')
xlim([0,600]);
lsline
ylim([0,600]);
xlabel('SSRT_h (ms)');
ylabel('SSRT_tbeests (ms)');
r_txt = ['r = ' num2str(round(r,3))];
p_txt = ['p = ' num2str(round(p,3))];
bf_txt = ['BF10 = ' num2str(round(bf10,1))];
tx_x = xlim;
text(50,580,r_txt, 'FontSize',12);
text(50,500,p_txt,'FontSize',12);
text(50,420,bf_txt,'FontSize',12);
title('SSRT_h vs SSRT_tbeests','FontWeight','bold','FontSize',14);


subplot(3,2,4)
[r,p] = corr(GroupData.Summary_Behavior.SSRTt_TT,...
    GroupData.BEESTS_Summary_Home.SSRTmn);
bf10 = corrbf(r,length(subjs));
scatter(GroupData.Summary_Behavior.SSRTt_TT,...
    GroupData.BEESTS_Summary_Home.SSRTmn,80, 'k','MarkerFaceColor','k')
xlim([0,600]);
lsline
ylim([0,600]);
xlabel('SSRT_t (ms)');
ylabel('SSRT_hbeests (ms)');
r_txt = ['r = ' num2str(round(r,3))];
p_txt = ['p = ' num2str(round(p,3))];
bf_txt = ['BF10 = ' num2str(round(bf10,1))];
tx_x = xlim;
text(50,580,r_txt, 'FontSize',12);
text(50,500,p_txt,'FontSize',12);
text(50,420,bf_txt,'FontSize',12);
title('SSRT_t vs SSRT_hbeests','FontWeight','bold','FontSize',14);
% set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);

subplot(3,2,5)
[r,p] = corr(GroupData.Summary_Behavior.SSRTt_TT,...
    GroupData.BEESTS_Summary_Target.SSRTmn);
bf10 = corrbf(r,length(subjs));
scatter(GroupData.Summary_Behavior.SSRTt_TT,...
    GroupData.BEESTS_Summary_Target.SSRTmn,80, 'k','MarkerFaceColor','k')
xlim([0,600]);
lsline
ylim([0,600]);
xlabel('SSRT_t (ms)');
ylabel('SSRT_tbeests (ms)');
r_txt = ['r = ' num2str(round(r,3))];
p_txt = ['p = ' num2str(round(p,3))];
bf_txt = ['BF10 = ' num2str(round(bf10,1))];
tx_x = xlim;
text(50,580,r_txt, 'FontSize',12);
text(50,500,p_txt,'FontSize',12);
text(50,420,bf_txt,'FontSize',12);
title('SSRT_t vs SSRT_tbeests','FontWeight','bold','FontSize',14);
% set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);

subplot(3,2,6)
[r,p] = corr(GroupData.BEESTS_Summary_Target.SSRTmn,...
    GroupData.BEESTS_Summary_Home.SSRTmn);
bf10 = corrbf(r,length(subjs));
scatter(GroupData.BEESTS_Summary_Target.SSRTmn,...
    GroupData.BEESTS_Summary_Home.SSRTmn,80, 'k','MarkerFaceColor','k')
xlim([0,600]);
lsline
ylim([0,600]);
xlabel('SSRT_hbeests (ms)');
ylabel('SSRT_tbeests (ms)');
r_txt = ['r = ' num2str(round(r,3))];
p_txt = ['p = ' num2str(round(p,3))];
bf_txt = ['BF10 = ' num2str(round(bf10,1))];
tx_x = xlim;
text(50,580,r_txt, 'FontSize',12);
text(50,500,p_txt,'FontSize',12);
text(50,420,bf_txt,'FontSize',12);
title('SSRT_hbeests vs SSRT_tbeests','FontWeight','bold','FontSize',14);
% set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);

print(gcf,strcat(savePath,'GroupBehav_SSRT_corr_BEESTS.pdf'),'-dpdf','-r400');


% TF BEESTS versus TF FS failed stop target
% n_stop_trls = 18 * 32 * .5 *.25;  % 18 blocks 32 trials total, half it for each stair case and * .25 to get stop trials
pcnt_FShout = GroupData.time_maxY.Nh_outliers ./ n_stop_trls * 100;

meanpcnt_FShout = round(nanmean(pcnt_FShout),1);
stdpcnt_FShout = round(nanstd(pcnt_FShout),1);

figure()
axes('box','off','tickdir','out','LineWidth',1,'FontSize',14);
beeswarm_plot({go_data pcnt_TFh pcnt_FShout ...
    GroupData.BEESTS_Summary_Home.tfmn ...
    GroupData.BEESTS_Summary_Target.tfmn},...
    'color',[0.8,0.8,0.8; 0.8,0.8,0.8; 0.8,0.8,0.8;0.98824,0.53725, 0;0.98824,0.53725, 0]);
set(gca,'XTick',1:5,'XTickLabel',{'Go','TF_h','FShout','TF_hbeests','TF_tbeests'},'FontSize',14);
ylabel('TF (%)','FontSize',14);
ylim([0, max(GroupData.BEESTS_Summary_Home.tfmn)+5]);
title('How often was Go or Stop omitted (TF) vs BEESTS','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
print(gcf,strcat(savePath,'GroupBehav_pcntOmit_pcntTF_BEESTS.pdf'),'-dpdf','-r400');
% delete(gcf)

% TF BEESTS versus TF FS failed stop target - Correlations
figure()
subplot(2,2,1)
[r,p] = corr(pcnt_TFh, GroupData.BEESTS_Summary_Home.tfmn);
bf10 = corrbf(r,length(subjs));
scatter(pcnt_TFh, GroupData.BEESTS_Summary_Home.tfmn,80, 'k','MarkerFaceColor','k')
xlim([0,max(GroupData.BEESTS_Summary_Home.tfmn)+2]);
lsline
ylim([0,max(GroupData.BEESTS_Summary_Home.tfmn)+2]);
xlabel('TFh (%)');
ylabel('TFhbeests (%)');
r_txt = ['r = ' num2str(round(r,3))];
p_txt = ['p = ' num2str(round(p,3))];
bf_txt = ['BF10 = ' num2str(round(bf10,1))];
tx_x = xlim;
text(25,35,r_txt, 'FontSize',12);
text(25,31,p_txt,'FontSize',12);
text(25,27,bf_txt,'FontSize',12);
title('TFh vs TFhbeests','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);

subplot(2,2,2)
[r,p] = corr(pcnt_TFh, GroupData.BEESTS_Summary_Target.tfmn);
bf10 = corrbf(r,length(subjs));
scatter(pcnt_TFh, GroupData.BEESTS_Summary_Target.tfmn,80, 'k','MarkerFaceColor','k')
xlim([0,max(GroupData.BEESTS_Summary_Target.tfmn)+2]);
lsline
ylim([0,max(GroupData.BEESTS_Summary_Target.tfmn)+2]);
xlabel('TFh (%)');
ylabel('TFhbeests (%)');
r_txt = ['r = ' num2str(round(r,3))];
p_txt = ['p = ' num2str(round(p,3))];
bf_txt = ['BF10 = ' num2str(round(bf10,1))];
tx_x = xlim;
text(12,18,r_txt, 'FontSize',12);
text(12,15,p_txt,'FontSize',12);
text(12,12,bf_txt,'FontSize',12);
title('TFh vs TFtbeests','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);

subplot(2,2,3)
[r,p] = corr(pcnt_FShout, GroupData.BEESTS_Summary_Home.tfmn);
bf10 = corrbf(r,length(subjs));
scatter(pcnt_FShout, GroupData.BEESTS_Summary_Home.tfmn,80, 'k','MarkerFaceColor','k')
xlim([0,max(GroupData.BEESTS_Summary_Home.tfmn)+2]);
lsline
ylim([0,max(GroupData.BEESTS_Summary_Home.tfmn)+2]);
xlabel('FSh time Ymax outliers (%)');
ylabel('TFhbeests (%)');
r_txt = ['r = ' num2str(round(r,3))];
p_txt = ['p = ' num2str(round(p,3))];
bf_txt = ['BF10 = ' num2str(round(bf10,1))];
tx_x = xlim;
text(25,35,r_txt, 'FontSize',12);
text(25,31,p_txt,'FontSize',12);
text(25,27,bf_txt,'FontSize',12);
title('FSh time Ymax outliers vs TFhbeests','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);

subplot(2,2,4)
[r,p] = corr(pcnt_FShout, GroupData.BEESTS_Summary_Target.tfmn);
bf10 = corrbf(r,length(subjs));
scatter(pcnt_TFh, GroupData.BEESTS_Summary_Target.tfmn,80, 'k','MarkerFaceColor','k');
xlim([0,max(GroupData.BEESTS_Summary_Target.tfmn)+2]);
lsline
ylim([0,max(GroupData.BEESTS_Summary_Target.tfmn)+2]);
xlabel('FSh time Ymax outliers (%)');
ylabel('TFhbeests (%)');
r_txt = ['r = ' num2str(round(r,3))];
p_txt = ['p = ' num2str(round(p,3))];
bf_txt = ['BF10 = ' num2str(round(bf10,1))];
tx_x = xlim;
text(12,18,r_txt, 'FontSize',12);
text(12,15,p_txt,'FontSize',12);
text(12,12,bf_txt,'FontSize',12);
title('FSh time Ymax outliers vs TFtbeests','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);

print(gcf,strcat(savePath,'GroupBehav_TF_corr_BEESTS.pdf'),'-dpdf','-r400');

% SSRT STD BEESTS vs SSRT Time Y max FS-home and SS-target
figure()
axes('box','off','tickdir','out','LineWidth',1,'FontSize',14);
beeswarm_plot({GroupData.time_maxY.FSh_std ...
    GroupData.BEESTS_Summary_Home.SSRTstd ...
    GroupData.time_maxY.SSt_std ...
    GroupData.BEESTS_Summary_Target.SSRTstd},...
    'color',[0.8,0.8,0.8;0.98824,0.53725,0;0.8,0.8,0.8;0.98824,0.53725,0]);
set(gca,'XTick',1:4,'XTickLabel',{'Home','Homebeests','Target','Targbeests'},'FontSize',14);
ylabel('Std stop times (ms)','FontSize',14);
ylim([0, max([GroupData.time_maxY.FSh_std;...
    GroupData.time_maxY.SSt_mn;...
    GroupData.BEESTS_Summary_Home.SSRTstd])+20]);
title('Std. stop times vs BEESTS','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
print(gcf,strcat(savePath,'GroupBehav_Std_StopTime_vs_BEESTS.pdf'),'-dpdf','-r400');
% delete(gcf)

% Correlations between Std estimates
figure()
subplot(3,2,1)
[r,p] = corr(GroupData.time_maxY.FSh_std,...
    GroupData.BEESTS_Summary_Home.SSRTstd);
bf10 = corrbf(r,length(subjs));
scatter(GroupData.time_maxY.FSh_std,...
    GroupData.BEESTS_Summary_Home.SSRTstd, 80, 'k','MarkerFaceColor','k')
xlim([0,max([GroupData.time_maxY.FSh_std;...
    GroupData.BEESTS_Summary_Home.SSRTstd])+20]);
lsline
ylim([0,max([GroupData.time_maxY.FSh_std;...
    GroupData.BEESTS_Summary_Home.SSRTstd])+20]);
xlabel('Std  FS_h (ms)');
ylabel('Std SSRT hbeests (ms)');
r_txt = ['r = ' num2str(round(r,3))];
p_txt = ['p = ' num2str(round(p,3))];
bf_txt = ['BF10 = ' num2str(round(bf10,1))];
tx_x = xlim;
text(200,280,r_txt, 'FontSize',12);
text(200,250,p_txt,'FontSize',12);
text(200,220,bf_txt,'FontSize',12);
title('Std','FontWeight','bold','FontSize',14);
% set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);

subplot(3,2,2)
[r,p] = corr(GroupData.time_maxY.SSt_std,...
    GroupData.BEESTS_Summary_Home.SSRTstd);
bf10 = corrbf(r,length(subjs));
scatter(GroupData.time_maxY.SSt_std,...
    GroupData.BEESTS_Summary_Home.SSRTstd,80, 'k','MarkerFaceColor','k')
xlim([0,max([GroupData.time_maxY.FSh_std;...
    GroupData.BEESTS_Summary_Home.SSRTstd])+20]);
lsline
ylim([0,max([GroupData.time_maxY.FSh_std;...
    GroupData.BEESTS_Summary_Home.SSRTstd])+20]);
xlabel('Std TimeYmax SS_t (ms)');
ylabel('Std SSRT hbeests (ms)');
r_txt = ['r = ' num2str(round(r,3))];
p_txt = ['p = ' num2str(round(p,3))];
bf_txt = ['BF10 = ' num2str(round(bf10,1))];
tx_x = xlim;
text(200,280,r_txt, 'FontSize',12);
text(200,250,p_txt,'FontSize',12);
text(200,220,bf_txt,'FontSize',12);
title('Std','FontWeight','bold','FontSize',14);
% set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);

subplot(3,2,3)
[r,p] = corr(GroupData.time_maxY.FSh_std,...
    GroupData.BEESTS_Summary_Target.SSRTstd);
bf10 = corrbf(r,length(subjs));
scatter(GroupData.time_maxY.FSh_std,...
    GroupData.BEESTS_Summary_Target.SSRTstd, 80, 'k','MarkerFaceColor','k')
xlim([0,max([GroupData.time_maxY.FSh_std;...
    GroupData.BEESTS_Summary_Target.SSRTstd])+20]);
lsline
ylim([0,max([GroupData.time_maxY.FSh_std;...
    GroupData.BEESTS_Summary_Target.SSRTstd])+20]);
xlabel('Std TimeYmax FS_h (ms)');
ylabel('Std SSRT tbeests (ms)');
r_txt = ['r = ' num2str(round(r,3))];
p_txt = ['p = ' num2str(round(p,3))];
bf_txt = ['BF10 = ' num2str(round(bf10,1))];
tx_x = xlim;
text(170,250,r_txt, 'FontSize',12);
text(170,220,p_txt,'FontSize',12);
text(170,190,bf_txt,'FontSize',12);
title('Std','FontWeight','bold','FontSize',14);
% set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);

subplot(3,2,4)
[r,p] = corr(GroupData.time_maxY.SSt_std,...
    GroupData.BEESTS_Summary_Target.SSRTstd);
bf10 = corrbf(r,length(subjs));
scatter(GroupData.time_maxY.SSt_std,...
    GroupData.BEESTS_Summary_Target.SSRTstd,80, 'k','MarkerFaceColor','k')
xlim([0,max([GroupData.time_maxY.FSh_std;...
    GroupData.BEESTS_Summary_Target.SSRTstd])+20]);
lsline
ylim([0,max([GroupData.time_maxY.FSh_std;...
    GroupData.BEESTS_Summary_Target.SSRTstd])+20]);
xlabel('Std TimeYmax SS_t (ms)');
ylabel('Std SSRT tbeests (ms)');
r_txt = ['r = ' num2str(round(r,3))];
p_txt = ['p = ' num2str(round(p,3))];
bf_txt = ['BF10 = ' num2str(round(bf10,1))];
tx_x = xlim;
text(170,250,r_txt, 'FontSize',12);
text(170,220,p_txt,'FontSize',12);
text(170,190,bf_txt,'FontSize',12);
title('Std','FontWeight','bold','FontSize',14);
% set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);

subplot(3,2,5)
[r,p] = corr(GroupData.time_maxY.FSh_std,...
    GroupData.time_maxY.SSt_std);
bf10 = corrbf(r,length(subjs));
scatter(GroupData.time_maxY.FSh_std,...
    GroupData.time_maxY.SSt_std, 80, 'k','MarkerFaceColor','k')
xlim([0,max([GroupData.time_maxY.FSh_std;...
    GroupData.time_maxY.SSt_std])+20]);
lsline
ylim([0,max([GroupData.time_maxY.FSh_std;...
    GroupData.time_maxY.SSt_std])+20]);
xlabel('Std TimeYmax FS_h (ms)');
ylabel('Std TimeYmax SS_t (ms)');
r_txt = ['r = ' num2str(round(r,3))];
p_txt = ['p = ' num2str(round(p,3))];
bf_txt = ['BF10 = ' num2str(round(bf10,1))];
tx_x = xlim;
text(120,180,r_txt, 'FontSize',12);
text(120,150,p_txt,'FontSize',12);
text(120,120,bf_txt,'FontSize',12);
title('Std','FontWeight','bold','FontSize',14);
% set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);

subplot(3,2,6)
[r,p] = corr(GroupData.BEESTS_Summary_Home.SSRTstd,...
    GroupData.BEESTS_Summary_Target.SSRTstd);
bf10 = corrbf(r,length(subjs));
scatter(GroupData.BEESTS_Summary_Home.SSRTstd,...
    GroupData.BEESTS_Summary_Target.SSRTstd,80, 'k','MarkerFaceColor','k')
xlim([0,max([GroupData.BEESTS_Summary_Home.SSRTstd;...
    GroupData.BEESTS_Summary_Target.SSRTstd])+20]);
lsline
ylim([0,max([GroupData.BEESTS_Summary_Home.SSRTstd;...
    GroupData.BEESTS_Summary_Target.SSRTstd])+20]);
xlabel('Std SSRT hbeests (ms)');
ylabel('Std SSRT tbeests (ms)');
r_txt = ['r = ' num2str(round(r,3))];
p_txt = ['p = ' num2str(round(p,3))];
bf_txt = ['BF10 = ' num2str(round(bf10,1))];
tx_x = xlim;
text(200,280,r_txt, 'FontSize',12);
text(200,250,p_txt,'FontSize',12);
text(200,220,bf_txt,'FontSize',12);
title('Std','FontWeight','bold','FontSize',14);
% set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);

print(gcf,strcat(savePath,'GroupBehav_Std_stopTime_corr_BEESTS.pdf'),'-dpdf','-r400');


%% Context independence
figure()
axes('box','off','tickdir','out','LineWidth',1,'FontSize',14);
beeswarm_plot({GroupData.Trl_CntxInd_Violations_mn.pcntFSlessGo_h ...
    GroupData.Trl_CntxInd_Violations_mn.pcntFSlessGo_t }, ...
    'color',[0.8,0.8,0.8; 0.98824,0.53725,0]);
set(gca,'XTick',1:2,'XTickLabel',{'FSh','FSt'},'FontSize',14);
ylabel('FS RT < Go RT (%FStrls)','FontSize',14);
ylim([0,max(GroupData.Trl_CntxInd_Violations_mn.pcntFSlessGo_h)+20]);
title('% trialwise context independence violations','FontWeight','bold','FontSize',14);
set(gcf,'PaperUnits','inches','PaperSize',[8 8],'PaperPosition',[1 1 6.65 5]);
print(gcf,strcat(savePath,'GroupBehav_Trlwise_ContextInde_Violations.pdf'),'-dpdf','-r400');
% delete(gcf)

[h,p,ci,stats] = ttest(GroupData.Trl_CntxInd_Violations_mn.pcntFSlessGo_h, ...
    GroupData.Trl_CntxInd_Violations_mn.pcntFSlessGo_t);
bf10 = t1smpbf(stats.tstat,length(GroupData.Trl_CntxInd_Violations_mn.pcntFSlessGo_h));



%% Look at any influence of OS
idx_Mac = GroupData.Computer_setup.OSname == 'macOS';
idx_Windows = GroupData.Computer_setup.OSname == 'Windows';
idx_Linux = GroupData.Computer_setup.OSname == 'Linux';

os_sum = [sum(idx_Mac), sum(idx_Windows), sum(idx_Linux)];
GroupData.OS_summ = array2table(os_sum, ...
    'VariableNames',{'nMac','nWindows','nLinux'});

figure()
beeswarm_plot({GroupData.Summary_Behavior.Go_RT(idx_Mac),...
    GroupData.Summary_Behavior.Go_RT(idx_Windows),...
    GroupData.Summary_Behavior.Go_RT(idx_Linux)},...
    'color',[0.8,0.8,0.8; 0.98824,0.53725,0;0.8,0.8,0.8]);
set(gca,'XTick',1:3,'XTickLabel',{'Mac','Windows','Linux'},'FontSize',14);
ylabel('Go RT (ms)','FontSize',14);
ylim([0, max(nanmean(GroupData.Summary_Behavior.Go_RT(idx_Mac)))+200]);
title('Go RT by OS','FontWeight','bold','FontSize',14);

figure()
beeswarm_plot({GroupData.Summary_Behavior.Go_MT(idx_Mac),...
    GroupData.Summary_Behavior.Go_MT(idx_Windows),...
    GroupData.Summary_Behavior.Go_MT(idx_Linux)},...
    'color',[0.8,0.8,0.8; 0.98824,0.53725,0;0.8,0.8,0.8]);
set(gca,'XTick',1:3,'XTickLabel',{'Mac','Windows','Linux'},'FontSize',14);
ylabel('Go MT (ms)','FontSize',14);
ylim([0, max(nanmean(GroupData.Summary_Behavior.Go_MT(idx_Mac)))+200]);
title('Go MT by OS','FontWeight','bold','FontSize',14);

figure()
beeswarm_plot({GroupData.Summary_Behavior.GoErr(idx_Mac),...
    GroupData.Summary_Behavior.GoErr(idx_Windows),...
    GroupData.Summary_Behavior.GoErr(idx_Linux)},...
    'color',[0.8,0.8,0.8; 0.98824,0.53725,0;0.8,0.8,0.8]);
set(gca,'XTick',1:3,'XTickLabel',{'Mac','Windows','Linux'},'FontSize',14);
ylabel('GoErr (%)','FontSize',14);
ylim([0, 40]);
title('GoErr by OS','FontWeight','bold','FontSize',14);

figure()
beeswarm_plot({GroupData.Summary_Behavior.GoMiss(idx_Mac),...
    GroupData.Summary_Behavior.GoMiss(idx_Windows),...
    GroupData.Summary_Behavior.GoMiss(idx_Linux)},...
    'color',[0.8,0.8,0.8; 0.98824,0.53725,0;0.8,0.8,0.8]);
set(gca,'XTick',1:3,'XTickLabel',{'Mac','Windows','Linux'},'FontSize',14);
ylabel('GoMiss (%)','FontSize',14);
ylim([0, 40]);
title('GoMiss by OS','FontWeight','bold','FontSize',14);

figure()
beeswarm_plot({GroupData.Summary_Behavior.GoOmit(idx_Mac),...
    GroupData.Summary_Behavior.GoOmit(idx_Windows),...
    GroupData.Summary_Behavior.GoOmit(idx_Linux)},...
    'color',[0.8,0.8,0.8; 0.98824,0.53725,0;0.8,0.8,0.8]);
set(gca,'XTick',1:3,'XTickLabel',{'Mac','Windows','Linux'},'FontSize',14);
ylabel('GoOmit (%)','FontSize',14);
ylim([0, 40]);
title('GoOmit by OS','FontWeight','bold','FontSize',14);

figure()
beeswarm_plot({GroupData.Summary_Behavior.SSRTh_RT(idx_Mac),...
    GroupData.Summary_Behavior.SSRTh_RT(idx_Windows),...
    GroupData.Summary_Behavior.SSRTh_RT(idx_Linux)},...
    'color',[0.8,0.8,0.8; 0.98824,0.53725,0;0.8,0.8,0.8]);
set(gca,'XTick',1:3,'XTickLabel',{'Mac','Windows','Linux'},'FontSize',14);
ylabel('SSRTh (ms)','FontSize',14);
ylim([0, max(nanmean(GroupData.Summary_Behavior.SSRTh_RT(idx_Mac)))+250]);
title('SSRTh by OS','FontWeight','bold','FontSize',14);

figure()
beeswarm_plot({GroupData.Summary_Behavior.SSRTt_TT(idx_Mac),...
    GroupData.Summary_Behavior.SSRTt_TT(idx_Windows),...
    GroupData.Summary_Behavior.SSRTt_TT(idx_Linux)},...
    'color',[0.8,0.8,0.8; 0.98824,0.53725,0;0.8,0.8,0.8]);
set(gca,'XTick',1:3,'XTickLabel',{'Mac','Windows','Linux'},'FontSize',14);
ylabel('SSRTt (ms)','FontSize',14);
ylim([0, max(nanmean(GroupData.Summary_Behavior.SSRTt_TT(idx_Mac)))+250]);
title('SSRTt by OS','FontWeight','bold','FontSize',14);

figure()
beeswarm_plot({GroupData.Summary_Behavior.pStoph(idx_Mac),...
    GroupData.Summary_Behavior.pStoph(idx_Windows),...
    GroupData.Summary_Behavior.pStoph(idx_Linux)},...
    'color',[0.8,0.8,0.8; 0.98824,0.53725,0;0.8,0.8,0.8]);
set(gca,'XTick',1:3,'XTickLabel',{'Mac','Windows','Linux'},'FontSize',14);
ylabel('pStoph (ms)','FontSize',14);
ylim([0, 100]);
title('pStoph by OS','FontWeight','bold','FontSize',14);

figure()
beeswarm_plot({GroupData.Summary_Behavior.pStopt(idx_Mac),...
    GroupData.Summary_Behavior.pStopt(idx_Windows),...
    GroupData.Summary_Behavior.pStopt(idx_Linux)},...
    'color',[0.8,0.8,0.8; 0.98824,0.53725,0;0.8,0.8,0.8]);
set(gca,'XTick',1:3,'XTickLabel',{'Mac','Windows','Linux'},'FontSize',14);
ylabel('pStopt (ms)','FontSize',14);
ylim([0, 100]);
title('pStopt by OS','FontWeight','bold','FontSize',14);

figure()
beeswarm_plot({GroupData.Summary_Behavior.SSDh(idx_Mac),...
    GroupData.Summary_Behavior.SSDh(idx_Windows),...
    GroupData.Summary_Behavior.SSDh(idx_Linux)},...
    'color',[0.8,0.8,0.8; 0.98824,0.53725,0;0.8,0.8,0.8]);
set(gca,'XTick',1:3,'XTickLabel',{'Mac','Windows','Linux'},'FontSize',14);
ylabel('SSDh (ms)','FontSize',14);
ylim([0, nanmean(GroupData.Summary_Behavior.SSDh(idx_Mac))+200]);
title('SSDh by OS','FontWeight','bold','FontSize',14);

figure()
beeswarm_plot({GroupData.Summary_Behavior.SSDt(idx_Mac),...
    GroupData.Summary_Behavior.SSDt(idx_Windows),...
    GroupData.Summary_Behavior.SSDt(idx_Linux)},...
    'color',[0.8,0.8,0.8; 0.98824,0.53725,0;0.8,0.8,0.8]);
set(gca,'XTick',1:3,'XTickLabel',{'Mac','Windows','Linux'},'FontSize',14);
ylabel('SSDt (ms)','FontSize',14);
ylim([0, nanmean(GroupData.Summary_Behavior.SSDt(idx_Mac))+200]);
title('SSDt by OS','FontWeight','bold','FontSize',14);

figure()
beeswarm_plot({GroupData.Summary_Behavior.N_TFh(idx_Mac),...
    GroupData.Summary_Behavior.N_TFh(idx_Windows),...
    GroupData.Summary_Behavior.N_TFh(idx_Linux)},...
    'color',[0.8,0.8,0.8; 0.98824,0.53725,0;0.8,0.8,0.8]);
set(gca,'XTick',1:3,'XTickLabel',{'Mac','Windows','Linux'},'FontSize',14);
ylabel('N_TFh (count)','FontSize',14);
ylim([0, 20]);
title('N_TFh by OS','FontWeight','bold','FontSize',14);

%% Save data
matfile = fullfile(savePath, strcat('GroupDataFile.mat'));
save(matfile, 'GroupData','GroupData_all');
