%% Analysis of Experiment 2
% to use, put this file in the common root directory of the downloaded
% folder, set the variables expname and comment in the correct participant
% selection (var ptcs), or set your own selections for participants,
% inclusions etc... 
% Maëlle Lerebourg (2022)

%%
addpath(genpath(fullfile(pwd, 'support_funcs')))

%'2A', '2B' or '2_comb' (combined analysis of all participants)
expname = '2B';

if contains(expname, 'comb')
    resdir = {fullfile(pwd, 'data', 'Exp2A'),fullfile(pwd, 'data', 'Exp2B')};
else
    resdir = {fullfile(pwd, 'data', sprintf('Exp%s', expname)), {}};
end



for ne = 1:numel(resdir)
    if exist(resdir{ne}, 'dir')
        ptcfiles{ne,:}         = listfiles({'csv'}, resdir{ne});
    else
        ptcfiles{ne,:} = {};
    end
end
exps = [ones(size(ptcfiles{1})), 2 * ones(size(ptcfiles{2}))];
ptcfiles =  horzcat(ptcfiles{:});

load(fullfile(resdir{1}, 'targetdistr_ims.mat'));
ims_targdistr         = ims_targdistr_os;

%exclusion criteria
min_acc = 55;
rt_min   = 150;
rt_thr   = 3;   %std deviation within participants

%plot figures
save_fig = 1;
col_indivs = 0;


%ptcs = [1:10, 12:15, 17:36] ; %Exp 2A
ptcs = [1:3,5:24, 26:35, 37:75, 77:78, 80:83,85:86] %Exp 2B
%ptcs = [[1:10, 12:15, 17:36], [1:3,5:24, 26:35, 37:75, 77:78, 80:83,85:86] + 36]; %Exp 2_comb



pf = figure;

sigtxt = ['*', '*', '*'];
sigthr = [0.05, 0.01, 0.001];

acc.lim = [0, 100];
acc.dlim = [-20, 40];

rt.lim  = [200, 1200];
rt.dlim = [-150, 150];

L.lim = [200, 1400];
L.dlim = [-200, 200];

kn_cutoff = 4;

know = nan(1, numel(ptcs));
use = nan(1, numel(ptcs));


for np = 1:numel(ptcs)
    
    data = readtable(fullfile(resdir{exps(ptcs(np))}, ptcfiles{ptcs(np)}), 'ReadVariableNames', true);
    
    if ismember('slider_4_response', data.Properties.VariableNames)
        know(np) = data.slider_4_response(end);
    end
    
    if ismember('slider_5_response', data.Properties.VariableNames)
        use(np) = data.slider_5_response(end);
    end
    
    %select only trials of main experiment (not practice)
    main = data.main == 1 ;
    data = data(main,:);
    
    resp = ~ cellfun( @isempty, data.key_resp_keys );
    crt  = data.key_resp_corr == 1;
    
    %number of distractor shape (2 or 6)
    d2 = data.tdisp == 1;
    d6 = data.tdisp == 2;
    
    %distractor contexts
    r_c =  contains(data.im, 'curv');  % rectilinearity context
    r_o =  contains(data.im, 'ori');   % orientation context
    
    match = data.tvalid == 1 & d2 ;
    mismatch = data.tvalid == 0 & d2;
    
    %code trialtypes,
    rm  = r_c & match & d2;    %unique rectilinear trials, rect-blocks, matching
    rmm = r_c & mismatch & d2; %unique rectilinearity trials, ori-blocks, mismatching
    om  = r_o & match & d2;    %unique orientation trials, ori-blocks, matching
    omm = r_o & mismatch & d2; %unique orientation trials, rect-blocks, mismatching
    
    
    %compute some general measures
    resprate(np)    = (sum(resp)/sum(main)) * 100;
    acc.gen(np,1)     = (sum(crt & resp & d2)/ sum(resp & d2)) * 100;
    acc.gen(np,2)     = (sum(crt & resp & d6)/ sum(resp & d6)) * 100;
    acc.gen(np,3)     = (sum(crt & resp )/ sum(resp )) * 100;
    
    
    
    
    if acc.gen(np) < min_acc
        warning( 'Participant %g was < %u %% correct, accuracy: %2.1f%% ', ptcs(np), min_acc, acc.gen(np,3));
    end
    
    all_rt(1)     = mean(data.key_resp_rt(crt & resp & d2)) * 1000;
    all_rt(2)     = mean(data.key_resp_rt(crt & resp & d6)) * 1000;
    all_rt_std(1) = std(data.key_resp_rt(crt & resp & d2)) * 1000;
    all_rt_std(2) = std(data.key_resp_rt(crt & resp & d6)) * 1000;
    
    rt_min = max(150, all_rt - (rt_thr * all_rt_std));
    rt_max = all_rt  + (rt_thr * all_rt_std);
    
    val_rt = crt & resp  & ((d2 &  (data.key_resp_rt * 1000 >= rt_min(1)) & (data.key_resp_rt * 1000 <=  rt_max(1))) | (d6 &  (data.key_resp_rt * 1000 >= rt_min(2)) & (data.key_resp_rt * 1000 <=  rt_max(2)))) ;
    rts    = data.key_resp_rt * 1000;
    
    rt.gen(np,1)     = mean(rts(val_rt & d2));
    rt.gen(np,2)     = mean(rts(val_rt & d6));
    rt.gen(np,3)     = mean(rts(val_rt));
    rt.gen_std(np,1) = std(rts(val_rt & d2));
    rt.gen_std(np,2) = std(rts(val_rt & d6));
    rt.gen_std(np,3) = std(rts(val_rt));
    rt.rej(np)     = (sum(crt & resp  & ((d2 &  (data.key_resp_rt * 1000 < rt_min(1)) & (data.key_resp_rt * 1000 >  rt_max(1))) | (d6 &  (data.key_resp_rt * 1000 < rt_min(2)) & (data.key_resp_rt * 1000 > rt_max(2)))))./ sum(crt & resp)) * 100;
    
    L.gen(np,1)    = rt.gen(np,1) +((1 - acc.gen(np,1)./100) * (rt.gen_std(np,1)./std(crt & d2)));
    L.gen(np,2)    = rt.gen(np,2) +((1 - acc.gen(np,2)./100) * (rt.gen_std(np,2)./std(crt & d6)));
    L.gen(np,3)    = rt.gen(np,3) +((1 - acc.gen(np,3)./100) * (rt.gen_std(np,3)./std(crt)));
    
    targdistr = ismember(data.im,ims_targdistr);
    acc.targdistr(np) =   (sum(crt & resp & targdistr) / sum(resp & targdistr)) * 100;
    
    totnum = sum(resp & targdistr);
    cornum = sum(crt  & resp & targdistr);
    uplim  = binoinv(1-0.05, totnum, 0.5);
    lowlim = binoinv(0.05  , totnum, 0.5);
    
    if cornum < uplim
        warning( 'Participant %g was %2.1f%% correct with familiar shape distractor, which is not better than chance (%g/%g correct, limit:%g)', ptcs(np),  acc.targdistr(np), cornum, totnum, uplim);
    end
    
    acc.rm(np) = (sum(crt & resp & rm )/sum(rm & resp)) * 100;
    acc.rmm(np)= (sum(crt & resp &rmm )/sum(rmm & resp )) * 100;
    acc.om(np) = (sum(crt & resp & om )/sum(om & resp )) * 100;
    acc.omm(np)= (sum(crt & resp & omm )/sum(omm & resp )) * 100;
    acc.r(np)  = (sum(crt & resp & d6 &  r_c )/ sum((resp & d6 & r_c ))) * 100; 
    acc.o(np)  = (sum(crt & resp & d6 &  r_o )/ sum((resp & d6 & r_o ))) * 100;
    
    rt.rm(np)     = mean(rts( rm & val_rt ));
    rt.rmm(np)    = mean(rts(rmm & val_rt ));
    rt.om(np)     = mean(rts( om & val_rt ));
    rt.omm(np)    = mean(rts(omm & val_rt ));
    rt.r(np)      = mean(rts( d6 & r_c & val_rt));
    rt.o(np)      = mean(rts( d6 & r_o & val_rt));
    
    %account for participants who are 100 % correct -> set LISAS = RT
    norm = rt.gen_std(np,1)./std(crt(d2));
    if isinf(norm)
        norm = 0;
    elseif isnan(norm)
        norm = 0;
    end
    
    L.rm(np)      = rt.rm(np) + ((1 - acc.rm(np)./100) * norm);
    L.rmm(np)     = rt.rmm(np) +((1 - acc.rmm(np)./100) * norm);
    L.om(np)      = rt.om(np) + ((1 - acc.om(np)./100) * norm);
    L.omm(np)     = rt.omm(np) +((1 - acc.omm(np)./100) * norm);
    L.r(np)       = rt.r(np) +  ((1 - acc.r(np)./100) * rt.gen_std(np,2)./std(crt(d6)));
    L.o(np)       = rt.o(np) +  ((1 - acc.o(np)./100) * rt.gen_std(np,2)./std(crt(d6)));
    
    %measures per block
    
    %context effects are measured for every 2 blocks combined (1-2, 3-4,...), to compare
    %performance on the same trialtype when they are matching or
    %mismatching blockcontext
    
    for nb =  1:max(data.blockn)/2
        
        blocks = [(2 * nb) - 1, 2 * nb];
        acc.rm_bl(np,nb)  = (sum(crt & resp & rm & ismember(data.blockn, blocks))/sum(rm & resp &  ismember(data.blockn, blocks))) * 100;
        acc.rmm_bl(np,nb) = (sum(crt & resp & rmm & ismember(data.blockn, blocks))/sum(rmm & resp & ismember(data.blockn, blocks))) * 100;
        acc.om_bl(np,nb)  = (sum(crt & resp & om & ismember(data.blockn, blocks))/sum(om & resp & ismember(data.blockn, blocks))) * 100;
        acc.omm_bl(np,nb) = (sum(crt & resp & omm & ismember(data.blockn, blocks))/sum(omm & resp & ismember(data.blockn, blocks))) * 100;
        
        rt.rm_bl(np,nb)     = mean(rts( rm & val_rt &  ismember(data.blockn, blocks)));
        rt.rmm_bl(np,nb)    = mean(rts(rmm & val_rt &  ismember(data.blockn, blocks)));
        rt.om_bl(np,nb)     = mean(rts( om & val_rt &  ismember(data.blockn, blocks)));
        rt.omm_bl(np,nb)    = mean(rts(omm & val_rt &  ismember(data.blockn, blocks)));
        
        
        L.rm_bl(np,nb)      = rt.rm_bl(np,nb) +((1 - acc.rm_bl(np,nb)./100) * norm);
        L.rmm_bl(np,nb)     = rt.rmm_bl(np,nb)+((1 - acc.rmm_bl(np,nb)./100) * norm);
        L.om_bl(np,nb)      = rt.om_bl(np,nb) +((1 - acc.om_bl(np,nb)./100) * norm);
        L.omm_bl(np,nb)     = rt.omm_bl(np,nb)+((1 - acc.omm_bl(np,nb)./100) * norm);
        
    end
    
    
end


perf = struct('Accuracy', acc,'RT',  rt, 'LISAS', L);
perf.Accuracy.unit = '(%)';
perf.RT.unit = '(ms)';
perf.LISAS.unit  = ' ';

if col_indivs
    cols = distinguishable_colors(numel(ptcs), {'w', 'k' });
else
    cols = repmat([0.75, 0.75, 0.75], numel(ptcs),1);
end

res = table();
resa = table();
f = fieldnames(perf)


for nms = 1:numel(f)
    ms = f{nms};
    
    fprintf( '\n\n\n%s\n\n', ms)
    fprintf( 'overall: %.2f %s (sd %.2f)\t 2-shape: %.2f %s (sd %.2f)\t 6-shape: %.2f %s (sd %.2f)  \n', mean(perf.(ms).gen(:,3)), replace(perf.(ms).unit,{'(', ')'}, {''}) , std(perf.(ms).gen(:,3)), mean(perf.(ms).gen(:,1)), replace(perf.(ms).unit,{'(', ')'}, {''}) , std(perf.(ms).gen(:,1)), mean(perf.(ms).gen(:,2)), replace(perf.(ms).unit,{'(', ')'}, {''}) , std(perf.(ms).gen(:,2)) );
    
    fprintf('\nDistractor context effects\n')
    
    figure(pf)
    subplot(1,numel(f),nms)
    pl = plotSpread(perf.(ms).gen(:,1:2) , 'CategoryIdx', repmat((1:numel(ptcs)),2,1)' , 'categoryColors',cols )';
    errorbar(1:2, mean(perf.(ms).gen(:,1:2)), std(perf.(ms).gen(:,1:2))/sqrt(numel(ptcs)), 'ok', 'MarkerSize', 12, 'LineWidth', 2, 'CapSize', 20);
    %plot( [0.75, 1.25] , [min_acc min_acc], 'k.:', 'LineWidth', 1)
    xticklabels({'2', '6'});
    xlabel('Display type')
    pbaspect([1,2.5,1 ])
    ylabel(sprintf('%s %s', ms, perf.(ms).unit))
    set(gcf,'color','w');
    set(gca,'linewidth',0.75)
    set(gca, 'FontSize', 13);
    
    
    mbl(:,:,1) = perf.(ms).omm_bl - perf.(ms).om_bl;
    mbl(:,:,2) = perf.(ms).rmm_bl - perf.(ms).rm_bl;
    d_bl = nanmean(mbl,3);
    
    if strcmp(ms, 'Accuracy')
        d_bl = - d_bl;
    end
    
    perf.(ms).d_bl= d_bl;
    
    m = nanmean(d_bl);
    sem = nanstd(d_bl)./sqrt(size(d_bl,1));
    CI  = m + 1.96 .* [sem; -sem];
    
    figure,
    h = plot(m, 'k', 'LineWidth', 1 );
    hold on
    xplot = [1:size(m,2), fliplr(1:size(m,2))];
    hold on
    fill(xplot,[CI(1,:), fliplr(CI(2,:))] ,1, 'facecolor', 'k', 'edgecolor', 'none', 'facealpha', 0.1)
    
    
    plot( [0.75, 3.25] , [0 0], 'k.:', 'LineWidth', 1)
    xlim([0.75,3.25] )
    xticks([1: 3 ])
    xticklabels({'1-2','3-4', '5-6'})
    xlabel('Blocks')
    set(gcf,'color','w');
    ylabel(sprintf('Context facilitation %s %s', ms, perf.(ms).unit))
    
    set(gca,'linewidth',0.75)
    set(gca, 'FontSize', 13);
    box off
    if save_fig
        set( gcf, 'Renderer', 'painters');
        figname = sprintf('d_%s_blocks_%s', ms, expname);
        print( gcf, fullfile( pwd, figname), '-dpng');
        pause(0.5);
        print( gcf, fullfile( pwd, figname), '-dpdf');
        pause(0.5);
    end
    
    X = [perf.(ms).om, perf.(ms).omm, perf.(ms).rm, perf.(ms).rmm]';
    %first factor is match, second trialtype
    group = [ones(size(perf.(ms).om)), zeros(size(perf.(ms).omm)), ones(size(perf.(ms).rm)), zeros(size(perf.(ms).rmm)); ones(size(perf.(ms).om)), ones(size(perf.(ms).omm)), zeros(size(perf.(ms).rmm)), zeros(size(perf.(ms).rm))]';
    stats = mes2way(X,group, 'partialeta2', 'isDep',  [1 1 ] );
    d = mean([perf.(ms).omm - perf.(ms).om; perf.(ms).rmm- perf.(ms).rm],1);
    d_r =  mean([ perf.(ms).rmm- perf.(ms).rm],1);
    d_o =  mean([perf.(ms).omm - perf.(ms).om],1);
    d_int = d_r - d_o;
    d_tr = mean([perf.(ms).omm; perf.(ms).om],1) - mean([perf.(ms).rmm; perf.(ms).rm],1);
    
    
    if strcmp(ms, 'Accuracy')
        d = -d;
        d_r = -d_r;
        d_o = -d_o;
        d_tr = - d_tr;
    end
    
    perf.(ms).d = d;
    perf.(ms).d_r = d_r;
    perf.(ms).d_o = d_o;
    
    [hd, pd, CId, st_d] = ttest(d);
    [hr, pr, CIr, str] = ttest(d_r);
    [ho, po, CIo, sto] = ttest(d_o);

        
    fprintf('overall context facilitation: %.2f %s (sd %.2f)\n', mean(d), replace(perf.(ms).unit,{'(', ')'}, {''}), std(d));
    fprintf('- rect trials: %.2f %s(sd %.2f), ([%.2f, %.2f], t(%u) = %.2f, p = %.3f)\n', mean(d_r), replace(perf.(ms).unit,{'(', ')'}, {''}), std(d_r), CIr, str.df, str.tstat, pr);
    fprintf('- ori  trials: %.2f %s(sd %.2f), ([%.2f, %.2f], t(%u) = %.2f, p = %.3f)\n', mean(d_o), replace(perf.(ms).unit,{'(', ')'}, {''}), std(d_o), CIo, sto.df, sto.tstat, po);


    %analyse participants with explicit knowledge of distractor
    %manipulation and those without separately.
    
    %no-awareness participants
    X = [perf.(ms).om(know == 1 ), perf.(ms).omm(know ==1), perf.(ms).rm(know == 1), perf.(ms).rmm(know ==1)]';
    %first factor is match, second trialtype
    group = [ones(size(perf.(ms).om(know ==1))), zeros(size(perf.(ms).omm(know ==1))), ones(size(perf.(ms).rm(know ==1))), zeros(size(perf.(ms).rmm(know ==1))); ones(size(perf.(ms).om(know ==1))), ones(size(perf.(ms).omm(know ==1))), zeros(size(perf.(ms).rmm(know ==1))), zeros(size(perf.(ms).rm(know ==1)))]';
    stats = mes2way(X,group, 'partialeta2', 'isDep',  [1 1 ] );
    d_nk = mean([perf.(ms).omm(know == 1 ) - perf.(ms).om(know == 1 ); perf.(ms).rmm(know == 1 )- perf.(ms).rm(know == 1 )],1);
    
    if strcmp(ms, 'Accuracy')
        d_nk = -d_nk;
    end
    
    [h_nk, p_nk, ci_nk, stats_nk ] = ttest(d_nk);
    perf.(ms).d_nk = d_nk;
    
    fprintf('no -awareness group: overall context facilitation: %.2f %s (sd %.2f), d = %.2f, p = %.3f\n', mean(d_nk), replace(perf.(ms).unit,{'(', ')'}, {''}), std(d_nk), mean(d_nk)/std(d_nk), p_nk);
    
    %high-awareness participant
    X = [perf.(ms).om(know >= kn_cutoff ), perf.(ms).omm(know >= kn_cutoff ), perf.(ms).rm(know >= kn_cutoff ), perf.(ms).rmm(know >= kn_cutoff )]';
    %first factor is match, second trialtype
    group = [ones(size(perf.(ms).om(know >= kn_cutoff ))), zeros(size(perf.(ms).omm(know >= kn_cutoff ))), ones(size(perf.(ms).rm(know >= kn_cutoff ))), zeros(size(perf.(ms).rmm(know >= kn_cutoff ))); ones(size(perf.(ms).om(know >= kn_cutoff ))), ones(size(perf.(ms).omm(know >= kn_cutoff ))), zeros(size(perf.(ms).rmm(know >= kn_cutoff ))), zeros(size(perf.(ms).rm(know >= kn_cutoff )))]';
    stats = mes2way(X,group, 'partialeta2', 'isDep',  [1 1 ] );
    d_k = mean([perf.(ms).omm(know >= kn_cutoff ) - perf.(ms).om(know >= kn_cutoff ); perf.(ms).rmm(know >= kn_cutoff )- perf.(ms).rm(know >= kn_cutoff )],1);
    
    if strcmp(ms, 'Accuracy')
        d_k = -d_k;
    end
    
    [h_k, p_k, ci_k, stats_k ] = ttest(d_k);
    perf.(ms).d_k = d_k;
    
    fprintf('high -awareness group: overall context facilitation: %.2f %s (sd %.2f), d = %.2f, p = %.3f\n', mean(d_k), replace(perf.(ms).unit,{'(', ')'}, {''}), std(d_k), mean(d_k)/std(d_k), p_k);
    
    [h_knk,p_knk,ci_knk,stats_knk] =  ttest2(d_nk, d_k);
    fprintf('context-facilitation difference: d = %.2f, p = %.2f \n', (mean(d_nk) - mean(d_k))/sqrt(0.5 * (std(d_nk)^2 + std(d_k)^2)), p_knk);
    
    fprintf('correlations cueing effect and awareness check\n')
    [rhok, pk] = corr(know(~isnan(know))', d(~isnan(know))', 'Type',  'Spearman');
    [rhou, pu] = corr(use(~isnan(use) & ~ (know == 1))', d(~isnan(use) & ~(know ==1 ))', 'Type', 'Spearman');
    fprintf('    - mean value noticed : %.2f (sd %.2f) (%u ratings) \n', nanmean(know),  nanstd(know), sum(~isnan(know)));
    fprintf('    - mean value used    : %.2f (sd %.2f) (%u ratings) \n' , nanmean(use(know ~= 1)),  nanstd(use(know ~= 1)), sum(~isnan(know) & (know ~= 1)));
    fprintf('    - noticing manipulation & context facilitation : rho = %.2f,\t\tp = %.2f \n', rhok, pk)
    fprintf('    - using manipulation    & context facilitation : rho = %.2f,\t\tp = %.2f \n', rhou, pu)
    
    
    
    
    res = [table(perf.(ms).om', perf.(ms).omm', perf.(ms).rm', perf.(ms).rmm', d', d_r', d_o', d_tr', d_int','VariableNames',strcat( {ms}, '_', {'om', 'omm', 'rm', 'rmm', 'd', 'd_r', 'd_ro', 'd_tr', 'd_int'})) , res];
    
    
    %make figure
    figure
    hold on
    subplot(1,2,1)
    hold on
    
    rv = violinplot(perf.(ms).rm'  ,{1}, 'xpos', [0.75], 'ViolinColor', [0 0.4470 0.7410], 'Width', 0.2);
    oi = violinplot(perf.(ms).rmm',{2}, 'xpos', [1.25], 'ViolinColor',[0.6350 0.0780 0.1840], 'Width', 0.2);
    ri = violinplot(perf.(ms).om',{3}, 'xpos', [2.25], 'ViolinColor', [0 0.4470 0.7410] , 'Width', 0.2);
    ov = violinplot(perf.(ms).omm', {4}, 'xpos', [2.75], 'ViolinColor', [0.6350 0.0780 0.1840], 'Width', 0.2);
    xticks([1, 2.5])
    ylim(perf.(ms).lim)
    xlim([0.5, 3])
    set(gca,'linewidth',0.75)
    set(gca, 'FontSize', 13);
    xticklabels({'rectilinearity', 'orientation'})
    xlabel('trial type')
    ylabel(sprintf('%s %s', ms, perf.(ms).unit))
    set(gcf,'color','w');
    
    
    p2 = subplot(1,2,2);
    hold on
    violinplot(d',  {1} , 'ViolinColor', [0, 0, 0]);
    plot( [0.5,1.5] , [0 0], 'k.:', 'LineWidth', 1);
    xticks([])
    ylim(perf.(ms).dlim)
    set(gca,'linewidth',0.75)
    set(gca, 'FontSize', 13);
    legend(p2, [rv.MedianPlot, ov.MedianPlot], {'matching context', 'mismatching context'}, 'Location', 'northoutside', 'FontSize', 14  )
    legend boxoff
    pbaspect([1,2.5,1])
    xlim([0.5, 1.5])
    ylabel('Context facilitation')
%     violinplot(d_r', {1}, 'ViolinColor', 'xpos', 1, [0,0,0]);
%       violinplot(d_o', {2}, 'ViolinColor', 'xpos', 2,[0.25, 0.25, 0.25]);
%       xticklabels({'rectilinearity', 'orientation'})
%       plot( [0.5,2.5] , [0 0], 'k.:', 'LineWidth', 1);
%         set(gca,'linewidth',0.75)
%     set(gca, 'FontSize', 13);
    legend(p2, [rv.MedianPlot, ov.MedianPlot], {'matching context', 'mismatching context'}, 'Location', 'northoutside', 'FontSize', 14  )
    legend boxoff
    pbaspect([1,2.5,1])
    xlim([0.5, 2.5])
    ylabel('Context facilitation')
    
    lims = ylim;
    text(1,min([lims(2), max(d) + 0.7 * max(d)]), sigtxt(pd < sigthr), 'HorizontalAlignment', 'center', 'Fontsize', 20)
    
    
    if save_fig
        set( gcf, 'Renderer', 'painters');
        figname = sprintf('%s_trialsxblocks_c_%s_%s', ms, expname);
        print( gcf, fullfile( pwd, figname), '-dpng');
        pause(0.5);
        print( gcf, fullfile( pwd, figname), '-dpdf');
        pause(0.5);
    end
    
           
    
    figure
    hold on
    violinplot(d_nk',  {1} , 'xpos', [1],'ViolinColor', [0, 0, 0]);
    violinplot(d_k' ,  {2} , 'xpos', [2],'ViolinColor', [0, 0, 0]);
    plot( [0,2.5] , [0 0], 'k.:', 'LineWidth', 1);
    xticks([1,2])
    xticklabels({'no', 'high'})
    xlabel('Awareness group')
    %xtickangle(45)
    ylim(perf.(ms).dlim)
    set(gca,'linewidth',0.75)
    set(gca, 'FontSize', 13);
    pbaspect([1,2.5,1])
    xlim([0.5, 2.5])
    ylabel('Context facilitation')
    lims = ylim;
    text(1,min([lims(2), max(d) + 0.7 * max(d)]), sigtxt(p_nk < sigthr), 'HorizontalAlignment', 'center', 'Fontsize', 20)
    text(2,min([lims(2), max(d) + 0.7 * max(d)]), sigtxt(p_k < sigthr), 'HorizontalAlignment', 'center', 'Fontsize', 20)
    
    if save_fig
        set( gcf, 'Renderer', 'painters');
        figname = sprintf('%s_%s_awareness', ms, expname);
        print( gcf, fullfile( pwd, figname), '-dpng');
        pause(0.5);
        print( gcf, fullfile( pwd, figname), '-dpdf');
        pause(0.5);
    end
    
    figure, 
    h = scatter(know, d, 'filled', 'jitter', 'on','MarkerFaceColor' , 'k');
    %h.XJitter = 'density'
    %h.XJitterWidth = 0.1
    h.MarkerFaceAlpha = 0.5;
    xlim([0,6])
    ylabel('Context facilitation')
    xlabel('Awareness Rating')
    xticks([1:5])
    set(gca,'linewidth',0.75)
    set(gca, 'FontSize', 13)
    set( gcf, 'Renderer', 'painters');
    figname = sprintf('%s_%s_awareness_corr', ms, expname);
        print( gcf, fullfile( pwd, figname), '-dpng');
        pause(0.5);
        print( gcf, fullfile( pwd, figname), '-dpdf');
        pause(0.5);
        
        
    
end

save(sprintf('Exp_%s.mat', expname), 'perf')

writetable(res, sprintf('Exp_%s.csv', expname))
writetable(res((know == 1 ),:), sprintf('Exp_%s_noaware.csv', expname))
writetable(res(know >= kn_cutoff,: ), sprintf('Exp_%s_highaware.csv', expname))

