% Plot results from hemispheric checkerboard task


% add JQK functions
addpath(genpath('/Volumes/fb-lip/LNDG/Julian/functions/'))


% Plot difficulty level (top) and mean contrast (bottom)
figure;
imagesc([trialData(:).level; trialData(:).contrast; trialData(:).rxtime]);

figure;
subplot(5,1,1);
histogram([trialData(1:100).level], 10);
subplot(5,1,2);
histogram([trialData(101:200).level], 10);
subplot(5,1,3);
histogram([trialData(201:300).level], 10);
subplot(5,1,4);
histogram([trialData(301:400).level], 10);
subplot(5,1,5);
histogram([trialData(401:500).level], 10);

figure;
subplot(5,1,1);
bar(mean([trialData(1:100).correct])); ylim([.5 1]);
subplot(5,1,2);
bar(mean([trialData(101:200).correct])); ylim([.5 1]);
subplot(5,1,3);
bar(mean([trialData(201:300).correct])); ylim([.5 1]);
subplot(5,1,4);
bar(mean([trialData(301:400).correct])); ylim([.5 1]);
subplot(5,1,5);
bar(mean([trialData(401:500).correct])); ylim([.5 1]);

figure;
scatter([trialData(:).level], [trialData(:).rxtime]);
title('Correlataion Level - RT');

figure; 
imagesc([trialData(:).rxtime; trialData(:).contrast]);
title('RT - contrast');

cntr_2 = find([trialData(:).contrast] == .2);
cntr_5 = find([trialData(:).contrast] == .5);
cntr_8 = find([trialData(:).contrast] == .8);

level = [trialData(:).level];
acc = [trialData(:).correct];
rt = [trialData(:).rxtime];

%TrialsHalf = numel(cntr_2)/2;

TrialsHalf = numel(level)/2;

figure;
subplot(2,1,1);
histogram([trialData(1:TrialsHalf).level], 10);
title('Levels presented during first half');
subplot(2,1,2);
histogram([trialData(TrialsHalf+1:end).level], 10);
title('Levels presented during second half');
set(findall(gcf,'-property','FontSize'),'FontSize',18)

figure;
subplot(3,1,1); bar([nanmean(level(cntr_2)), nanmean(level(cntr_5)), nanmean(level(cntr_8))]);
subplot(3,1,2); bar([nanmean(acc(cntr_2)), nanmean(acc(cntr_5)), nanmean(acc(cntr_8))]);
subplot(3,1,3); bar([nanmean(rt(cntr_2)), nanmean(rt(cntr_5)), nanmean(rt(cntr_8))]);

figure;
subplot(3,2,1); bar([nanmean(level(cntr_2(1:TrialsHalf))), nanmean(level(cntr_5(1:TrialsHalf))), nanmean(level(cntr_8(1:TrialsHalf)))]);
subplot(3,2,3); bar([nanmean(acc(cntr_2(1:TrialsHalf))), nanmean(acc(cntr_5(1:TrialsHalf))), nanmean(acc(cntr_8(1:TrialsHalf)))]);
subplot(3,2,5); bar([nanmean(rt(cntr_2(1:TrialsHalf))), nanmean(rt(cntr_5(1:TrialsHalf))), nanmean(rt(cntr_8(1:TrialsHalf)))]);

subplot(3,2,2); bar([nanmean(level(cntr_2(TrialsHalf+1:end))), nanmean(level(cntr_5(TrialsHalf+1:end))), nanmean(level(cntr_8(TrialsHalf+1:end)))]);
subplot(3,2,4); bar([nanmean(acc(cntr_2(TrialsHalf+1:end))), nanmean(acc(cntr_5(TrialsHalf+1:end))), nanmean(acc(cntr_8(TrialsHalf+1:end)))]);
subplot(3,2,6); bar([nanmean(rt(cntr_2(TrialsHalf+1:end))), nanmean(rt(cntr_5(TrialsHalf+1:end))), nanmean(rt(cntr_8(TrialsHalf+1:end)))]);

% plot change from first to second half

figure;
subplot(3,1,1); bar([nanmean(level(cntr_2(1:TrialsHalf))), nanmean(level(cntr_5(1:TrialsHalf))), nanmean(level(cntr_8(1:TrialsHalf)))]-...
    [nanmean(level(cntr_2(TrialsHalf+1:end))), nanmean(level(cntr_5(TrialsHalf+1:end))), nanmean(level(cntr_8(TrialsHalf+1:end)))]);
subplot(3,1,2); bar([nanmean(acc(cntr_2(1:TrialsHalf))), nanmean(acc(cntr_5(1:TrialsHalf))), nanmean(acc(cntr_8(1:TrialsHalf)))]-...
    [nanmean(acc(cntr_2(TrialsHalf+1:end))), nanmean(acc(cntr_5(TrialsHalf+1:end))), nanmean(acc(cntr_8(TrialsHalf+1:end)))]);
subplot(3,1,3); bar([nanmean(rt(cntr_2(1:TrialsHalf))), nanmean(rt(cntr_5(1:TrialsHalf))), nanmean(rt(cntr_8(1:TrialsHalf)))]-...
    [nanmean(rt(cntr_2(TrialsHalf+1:end))), nanmean(rt(cntr_5(TrialsHalf+1:end))), nanmean(rt(cntr_8(TrialsHalf+1:end)))]);
title('Change from first to second half');

% plot the above for single condition

figure;
subplot(3,1,1); bar([nanmean(level(cntr_8(1:TrialsHalf))), nanmean(level(cntr_8(TrialsHalf+1:end)))]);
title('Difference Level (i.e. lower = higher difficulty)');
subplot(3,1,2); bar([nanmean(acc(cntr_8(1:TrialsHalf))), nanmean(acc(cntr_8(TrialsHalf+1:end)))]);
title('Accuracy level')
subplot(3,1,3); bar([nanmean(rt(cntr_8(1:TrialsHalf))), nanmean(rt(cntr_8(TrialsHalf+1:end)))]);
title('RT');
set(findall(gcf,'-property','FontSize'),'FontSize',18)


figure;
subplot(3,1,1); bar([nanmedian(level(cntr_8(1:TrialsHalf))), nanmedian(level(cntr_8(TrialsHalf+1:end)))]);
title('Difference Level (i.e. lower = higher difficulty)');
subplot(3,1,2); bar([nanmedian(acc(cntr_8(1:TrialsHalf))), nanmedian(acc(cntr_8(TrialsHalf+1:end)))]);
title('Accuracy level')
subplot(3,1,3); bar([nanmedian(rt(cntr_8(1:TrialsHalf))), nanmedian(rt(cntr_8(TrialsHalf+1:end)))]);
title('RT');

figure;
subplot(3,1,1); bar([nanstd(level(cntr_8(1:TrialsHalf))), nanstd(level(cntr_8(TrialsHalf+1:end)))]);
title('Difference Level (i.e. lower = higher difficulty)');
subplot(3,1,2); bar([nanstd(acc(cntr_8(1:TrialsHalf))), nanstd(acc(cntr_8(TrialsHalf+1:end)))]);
title('Accuracy level')
subplot(3,1,3); bar([nanstd(rt(cntr_8(1:TrialsHalf))), nanstd(rt(cntr_8(TrialsHalf+1:end)))]);
title('RT');

% get parameters for DDM (ez-diffusion)

rtmean1 = nanmean(rt(1:3*TrialsHalf));
rtmean2 = nanmean(rt(3*TrialsHalf+1:end));

rtvar1 = nanstd(rt(1:3*TrialsHalf))^2;
rtvar2 = nanstd(rt(3*TrialsHalf+1:end))^2;

acc1 = nanmean(acc(1:3*TrialsHalf));
acc2 = nanmean(acc(3*TrialsHalf+1:end));

% %% fit psychometric functions for both halves 
% % http://matlaboratory.blogspot.de/2015/04/introduction-to-psychometric-curves-and.html
% 
% % discretize continuous levels
% 
% xData = level(1:TrialsHalf)';
% yData = [trialData(1:TrialsHalf).correct]';
% 
% [coeffs, ~, curve, threshold] = ...
%     FitPsycheCurveLogit(xData, yData, .5, []);


%% fit psychometric functions for both halves
% (see http://davehunter.wp.st-andrews.ac.uk/2015/04/12/fitting-a-psychometric-function/)

% discretize continuous levels

xData = level(1:TrialsHalf)';
yData = [trialData(1:TrialsHalf).correct]';

% no NaNs

ft = fittype( '0.5+(1-0.5-l)./(1+exp(-(x-alpha)/beta))', 'independent', 'x', 'dependent', 'y' );

opts = fitoptions( ft );    
opts.Display = 'Off';    
opts.Lower = [-Inf 0 0];    
opts.StartPoint = [0.132903481469417 0.762038603323984 0];    
opts.Upper = [Inf 1 1];

[fitresult, gof] = fit( xData, yData, ft, opts );

figure( 'Name', 'Example Psychophysical function fitting' );
subplot(2,1,1)
h = plot( fitresult, xData, yData ); title('First Half')   
legend( h, 'responses vs. sample positions', 'fit', 'Location', 'NorthEast' );    
% Label axes    
xlabel( 'stimulus intensity' );    
ylabel( 'Probability of correct response' );    
grid on

% fit for second half

xData = level(TrialsHalf+1:end)';
yData = [trialData(TrialsHalf+1:end).correct]';

ft = fittype( '0.5+(1-0.5-l)./(1+exp(-(x-alpha)/beta))', 'independent', 'x', 'dependent', 'y' );

opts = fitoptions( ft );    
opts.Display = 'Off';    
opts.Lower = [-Inf 0 0];    
opts.StartPoint = [0.132903481469417 0.762038603323984 0];    
opts.Upper = [Inf 1 1];

[fitresult, gof] = fit( xData, yData, ft, opts );

subplot(2,1,2)
h = plot( fitresult, xData, yData ); title('Second Half') 
legend( h, 'responses vs. sample positions', 'fit', 'Location', 'NorthEast' );    
% Label axes    
xlabel( 'stimulus intensity' );    
ylabel( 'Probability of correct response' );    
grid on

%% regress out the effect of level on accuracy and RT, look at temporal trajectory of residuals

% level on correct requires a logistic regression; level on RT is a linear regression

X = level';
Y = [trialData.rxtime]';

% remove NaNs

NaNlocs = find(isnan(Y));

X(NaNlocs) = [];
Y(NaNlocs) = [];

% get residuals

[b, bint, residuals] = regress(Y, X);

% plot residuals

figure;
subplot(2,1,1);
scatter(X, Y);
subplot(2,1,2);
plot(residuals);

figure;
plot(X)

%% check RTs for catch trials

checkerInfo.HighTrial

figure;
imagesc([trialData(checkerInfo.HighTrial).level; trialData(checkerInfo.HighTrial).contrast; trialData(checkerInfo.HighTrial).rxtime]);

figure;
plot([trialData(checkerInfo.HighTrial).rxtime]);
title('RTs for catch trials');
set(findall(gcf,'-property','FontSize'),'FontSize',18)


%% check timing for difficulties

imagesc(diff([TimeStamping{2:end,1}]))