blockVec = 0:20:size(trialData,2);

for block = 1:numel(blockVec)-1
    curData = trialData(blockVec(block)+1:blockVec(block+1));
    lowTrials_1 = find([curData.level] == 0.0050 & [curData.change] == 1);
    intTrials_1 = find([curData.level] == 0.0100 & [curData.change] == 1);
    highTrials_1 = find([curData.level] == 0.0200 & [curData.change] == 1);
    
    lowTrials_2 = find([curData.level] == 0.0050 & [curData.change] == -1);
    intTrials_2 = find([curData.level] == 0.0100 & [curData.change] == -1);
    highTrials_2 = find([curData.level] == 0.0200 & [curData.change] == -1);
    
    acc_1(1,block) = nanmean([curData(lowTrials_1).correct]);
    acc_1(2,block) = nanmean([curData(intTrials_1).correct]);
    acc_1(3,block) = nanmean([curData(highTrials_1).correct]);
    
    rt_1(1,block) = nanmean([curData(lowTrials_1).rxtime]);
    rt_1(2,block) = nanmean([curData(intTrials_1).rxtime]);
    rt_1(3,block) = nanmean([curData(highTrials_1).rxtime]);
    
    acc_2(1,block) = nanmean([curData(lowTrials_2).correct]);
    acc_2(2,block) = nanmean([curData(intTrials_2).correct]);
    acc_2(3,block) = nanmean([curData(highTrials_2).correct]);
    
    rt_2(1,block) = nanmean([curData(lowTrials_2).rxtime]);
    rt_2(2,block) = nanmean([curData(intTrials_2).rxtime]);
    rt_2(3,block) = nanmean([curData(highTrials_2).rxtime]);
end;

% plots

figure;
subplot(2,2,1);
plot(acc_1'); legend({'low'; 'int'; 'high'}); title('Accuracy');
subplot(2,2,2);
plot(rt_1'); legend({'low'; 'int'; 'high'}); title('RTs');
subplot(2,2,3);
plot(acc_2'); legend({'low'; 'int'; 'high'}); title('Accuracy');
subplot(2,2,4);
plot(rt_2'); legend({'low'; 'int'; 'high'}); title('RTs');

for block = 1:numel(blockVec)-1
    curData = trialData(blockVec(block)+1:blockVec(block+1));
    lowTrials = find([curData.level] == 0.0050);
    intTrials = find([curData.level] == 0.0100);
    highTrials = find([curData.level] == 0.0200);
    
    lowTrials = find([curData.level] == 0.0050);
    intTrials = find([curData.level] == 0.0100);
    highTrials = find([curData.level] == 0.0200);
    
    acc(1,block) = nanmean([curData(lowTrials).correct]);
    acc(2,block) = nanmean([curData(intTrials).correct]);
    acc(3,block) = nanmean([curData(highTrials).correct]);
    
    rt(1,block) = nanmean([curData(lowTrials).rxtime]);
    rt(2,block) = nanmean([curData(intTrials).rxtime]);
    rt(3,block) = nanmean([curData(highTrials).rxtime]);
end;

% plots

figure;
subplot(2,1,1);
plot(acc'); legend({'low'; 'int'; 'high'}); title('Accuracy');
subplot(2,1,2);
plot(rt'); legend({'low'; 'int'; 'high'}); title('RTs');

% images

figure;
subplot(2,1,1);
imagesc(acc); title('Accuracy'); colorbar;
subplot(2,1,2);
imagesc(rt); title('RTs'); colorbar;


%% sort by mean contrast

blockVec = 0:36:size(trialData,2);

for block = 1:numel(blockVec)-1
    curData = trialData(blockVec(block)+1:blockVec(block+1));
    
    lowTrials = find([curData.level] == .01);
    intTrials = find([curData.level] == .03);
    highTrials = find([curData.level] == .06);
   
    levels = {'lowTrials'; 'intTrials'; 'highTrials'};
    
    for indLevel = 1:numel(levels)
        acc(indLevel,block) = nanmean([curData(eval(levels{indLevel})).correct]);
        rt(indLevel,block) = nanmean([curData(eval(levels{indLevel})).rxtime]);
    end;
    
end;

lowMean = find(checkerInfo.contrastBlocks ==.2);
interMean = find(checkerInfo.contrastBlocks ==.5);
highMean = find(checkerInfo.contrastBlocks ==.8);

figure;
subplot(2,3,1);
imagesc(acc(:,lowMean)); title('Accuracy'); colorbar;
subplot(2,3,2);
imagesc(acc(:,interMean)); title('Accuracy'); colorbar;
subplot(2,3,3);
imagesc(acc(:,highMean)); title('Accuracy'); colorbar;

subplot(2,3,4);
imagesc(rt(:,lowMean)); title('RTs'); colorbar;
subplot(2,3,5);
imagesc(rt(:,interMean)); title('RTs'); colorbar;
subplot(2,3,6);
imagesc(rt(:,highMean)); title('RTs'); colorbar;

% show as plots

figure;
subplot(2,3,1);
plot(acc(:,lowMean)'); legend({'low'; 'int'; 'high'}); ylim([0 1]); xlim([1 6]);
title('Accuracy: .2 mean contrast');
subplot(2,3,2);
plot(acc(:,interMean)'); legend({'low'; 'int'; 'high'}); ylim([0 1]); xlim([1 6]);
title('Accuracy: .5 mean contrast');
subplot(2,3,3);
plot(acc(:,highMean)'); legend({'low'; 'int'; 'high'}); ylim([0 1]); xlim([1 6]);
title('Accuracy: .8 mean contrast');

subplot(2,3,4);
plot(rt(:,lowMean)'); legend({'low'; 'int'; 'high'}); ylim([0 2.5]); xlim([1 6]);
title('RT: .2 mean contrast');
subplot(2,3,5);
plot(rt(:,interMean)'); legend({'low'; 'int'; 'high'}); ylim([0 2.5]); xlim([1 6]);
title('RT: .5 mean contrast');
subplot(2,3,6);
plot(rt(:,highMean)'); legend({'low'; 'int'; 'high'}); ylim([0 2.5]); xlim([1 6]);
title('RT: .8 mean contrast');
set(findall(gcf,'-property','FontSize'),'FontSize',18)

%% check checker contrast

[mean(checkerContrastLeft,2), mean(checkerContrastRight,2)]

figure; imagesc([mean(checkerContrastLeft,2), mean(checkerContrastRight,2)])
figure; imagesc([mean(checkerContrastLeft,2)-mean(checkerContrastRight,2)])

lowTrials = find([trialData.level] == 0.0050);
intTrials = find([trialData.level] == 0.0100);
highTrials = find([trialData.level] == 0.0200);

figure;
subplot(1,6,1); tmp = [mean(checkerContrastLeft,2)-mean(checkerContrastRight,2)]; imagesc(tmp(lowTrials));
subplot(1,6,2); tmp = [trialData.change]; imagesc(-1.*tmp(lowTrials)');
subplot(1,6,3); tmp = [mean(checkerContrastLeft,2)-mean(checkerContrastRight,2)]; imagesc(tmp(intTrials));
subplot(1,6,4); tmp = [trialData.change]; imagesc(-1.*tmp(intTrials)');
subplot(1,6,5); tmp = [mean(checkerContrastLeft,2)-mean(checkerContrastRight,2)]; imagesc(tmp(highTrials));
subplot(1,6,6); tmp = [trialData.change]; imagesc(-1.*tmp(highTrials)');

figure;
subplot(1,6,1); tmp = [max(checkerContrastLeft,2)]; imagesc(tmp(lowTrials));
subplot(1,6,2); tmp = [trialData.change]; imagesc(-1.*tmp(lowTrials)');
subplot(1,6,3); tmp = [max(checkerContrastLeft,2)]; imagesc(tmp(intTrials));
subplot(1,6,4); tmp = [trialData.change]; imagesc(-1.*tmp(intTrials)');
subplot(1,6,5); tmp = [max(checkerContrastLeft,2)]; imagesc(tmp(highTrials));
subplot(1,6,6); tmp = [trialData.change]; imagesc(-1.*tmp(highTrials)');
