% analysis of scanner data

% In the first dataset, the contrast blocks were not presented intermixed,
% but in an ordered fashion.

%%
load('/Volumes/MyPassport3TB/RDM_Distract/PureChecker_V1/JQK_1_03-04-2017_20-28-32/Results_JQK_1_170403.mat')

%%
figure;
subplot(1,2,1)
plot(checkerContrastLeft(:,1));
subplot(1,2,2)
plot(checkerContrastRight(:,1));

figure;
subplot(1,2,1)
imagesc([trialData(:).rxtime]); title('RXtime');
subplot(1,2,2)
imagesc([trialData(:).correct]); title('Accuracy');

figure;
tmp_rxTime = [trialData(:).rxtime];
tmp_rxTime(isnan(tmp_rxTime)) = [];
scatter(tmp_rxTime, [trialData(:).correct])

%%

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

levels = checkerInfo.difficultyLevels;

for block = 1:numel(blockVec)-1
    curData = trialData(blockVec(block)+1:blockVec(block+1));
    lowTrials_1 = find([curData.level] == levels(1) & [curData.change] == 1);
    intTrials_1 = find([curData.level] == levels(2) & [curData.change] == 1);
    highTrials_1 = find([curData.level] == levels(3) & [curData.change] == 1);
    
    lowTrials_2 = find([curData.level] == levels(1) & [curData.change] == -1);
    intTrials_2 = find([curData.level] == levels(2) & [curData.change] == -1);
    highTrials_2 = find([curData.level] == levels(3) & [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]);
    
    acc_all(1,block) = nanmean([curData(:).correct]);
    acc_all(2,block) = nanmean([curData(find([curData.change] == 1)).correct]);
    acc_all(3,block) = nanmean([curData(find([curData.change] == -1)).correct]);
    rt_all(1,block) = nanmean([curData(:).rxtime]);
    rt_all(2,block) = nanmean([curData(find([curData.change] == 1)).rxtime]);
    rt_all(3,block) = nanmean([curData(find([curData.change] == -1)).rxtime]);
    
    acc_all_std(1,block) = nanstd([curData(:).correct]);
    acc_all_std(2,block) = nanstd([curData(find([curData.change] == 1)).correct]);
    acc_all_std(3,block) = nanstd([curData(find([curData.change] == -1)).correct]);
    rt_all_std(1,block) = nanstd([curData(:).rxtime]);
    rt_all_std(2,block) = nanstd([curData(find([curData.change] == 1)).rxtime]);
    rt_all_std(3,block) = nanstd([curData(find([curData.change] == -1)).rxtime]);
end;

figure;
subplot(1,2,1);
    plot(acc_all'); title('Accuracy'); legend({'all'; '1'; '-1'})
subplot(1,2,2);
    plot(rt_all'); title('RTs'); legend({'all'; '1'; '-1'})
    
figure;
subplot(2,2,1);
    imagesc(acc_all); title('Accuracy');
subplot(2,2,2);
    imagesc(rt_all); title('RTs');
subplot(2,2,3);
    imagesc(acc_all_std); title('Accuracy STDs');
subplot(2,2,4);
    imagesc(rt_all_std); title('RTs STDs');

% 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:20:size(trialData,2);
% 
% clear acc rt 
% 
% for block = 1:numel(blockVec)-1
%     curData = trialData(blockVec(block)+1:blockVec(block+1));
%     
%     lowTrials = find([curData.level] == levels(1));
%     intTrials = find([curData.level] == levels(2));
%     highTrials = find([curData.level] == levels(3));
%    
%     levelNames = {'lowTrials'; 'intTrials'; 'highTrials'};
%     
%     for indLevel = 1:numel(levelNames)
%         acc(indLevel,block) = nanmean([curData(eval(levelNames{indLevel})).correct]);
%         rt(indLevel,block) = nanmean([curData(eval(levelNames{indLevel})).rxtime]);
%         acc_std(indLevel,block) = nanstd([curData(eval(levelNames{indLevel})).correct]);
%         rt_std(indLevel,block) = nanstd([curData(eval(levelNames{indLevel})).rxtime]);
%         acc_all(indLevel,block) = nanmean([curData(:).correct]);
%         rt_all(indLevel,block) = nanmean([curData(:).rxtime]);
%     end;
%     
% end;
% 
% lowMean = find(checkerInfo.contrastBlocks ==.1);
% interMean = find(checkerInfo.contrastBlocks ==.5);
% highMean = find(checkerInfo.contrastBlocks ==.9);
% 
% % lowMean = 1:6;
% % interMean = 7:12;
% % highMean = 13:18;
% 
% figure;
% subplot(2,3,1);
%     plot(acc(:,lowMean)'); legend({'low'; 'int'; 'high'}); ylim([0 1]); xlim([1 6]);
%     title('Accuracy: .1 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: .9 mean contrast');
% subplot(2,3,4);
%     plot(rt(:,lowMean)'); legend({'low'; 'int'; 'high'}); ylim([0 2.5]); xlim([1 6]);
%     title('RT: .1 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: .9 mean contrast');
% set(findall(gcf,'-property','FontSize'),'FontSize',18)
% 
% figure;
% subplot(2,3,1);
%     plot(acc_std(:,lowMean)'); legend({'low'; 'int'; 'high'}); ylim([0 1]); xlim([1 6]);
%     title('Accuracy: .1 mean contrast');
% subplot(2,3,2);
%     plot(acc_std(:,interMean)'); legend({'low'; 'int'; 'high'}); ylim([0 1]); xlim([1 6]);
%     title('Accuracy: .5 mean contrast');
% subplot(2,3,3);
%     plot(acc_std(:,highMean)'); legend({'low'; 'int'; 'high'}); ylim([0 1]); xlim([1 6]);
%     title('Accuracy: .9 mean contrast');
% subplot(2,3,4);
%     plot(rt_std(:,lowMean)'); legend({'low'; 'int'; 'high'}); ylim([0 2.5]); xlim([1 6]);
%     title('RT: .1 mean contrast');
% subplot(2,3,5);
%     plot(rt_std(:,interMean)'); legend({'low'; 'int'; 'high'}); ylim([0 2.5]); xlim([1 6]);
%     title('RT: .5 mean contrast');
% subplot(2,3,6);
%     plot(rt_std(:,highMean)'); legend({'low'; 'int'; 'high'}); ylim([0 2.5]); xlim([1 6]);
%     title('RT: .9 mean contrast');
% set(findall(gcf,'-property','FontSize'),'FontSize',18)
% 
% figure;
% subplot(1,2,1);
%     plot(acc_all(:,lowMean)'); 
%     hold on; 
%     plot(acc_all(:,interMean)');
%     plot(acc_all(:,highMean)');
%     ylim([0 1]); xlim([1 6]);
%     legend({'low mean'; 'int mean'; 'high mean'});
%     title('Accuracy');
% subplot(1,2,2);
%     plot(rt_all(:,lowMean)'); 
%     hold on; 
%     plot(rt_all(:,interMean)');
%     plot(rt_all(:,highMean)');
%     ylim([0 2.5]); xlim([1 6]);
%     legend({'low mean'; 'int mean'; 'high mean'});
%     title('RTs');
% set(findall(gcf,'-property','FontSize'),'FontSize',18)
% 
%% check checker contrast

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

lowTrials = find([trialData.level] == levels(1));
intTrials = find([trialData.level] == levels(2));
highTrials = find([trialData.level] == levels(3));

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)');
