%% Load data.

close all; clear; clc

%-----------------------------------------------
% Specify the name of the COMPASS root directory
dataPath='/path/to/COMPASS/repository/';
%-----------------------------------------------
addpath(genpath([dataPath 'Scripts/']))

[~, txtData, allData]=xlsread([dataPath 'Data/COMPASS_SceneInfo.xlsx']); 
header=txtData(1,:); sceneInfo=cell2table(allData(2:end,:),'VariableNames',header);

[~, txtData, allData]=xlsread([dataPath 'Data/COMPASS_ValenceRTs.xlsx']);
header=txtData(1,:); valRTs_table=cell2table(allData(2:end,:),'VariableNames',header);
valRTs=table2array(valRTs_table(:,2:end-1));

[~, txtData, allData]=xlsread([dataPath 'Data/COMPASS_ArousalRTs.xlsx']);
header=txtData(1,:); arousRTs_table=cell2table(allData(2:end,:),'VariableNames',header);
arousRTs=table2array(arousRTs_table(:,2:end-1));

%% Prepare data.

% Exclude bad trials (<150 ms or >4000 ms).

exclValTrials=valRTs>4000 | valRTs<150;
exclArousTrials=arousRTs>4000 | arousRTs<150;

fprintf('Excluded %d (%.1f%%) valence ratings \n',sum(sum(exclValTrials)),...
    sum(sum(exclValTrials))/numel(exclValTrials)*100) 
fprintf('Excluded %d (%.1f%%) arousal ratings \n\n',sum(sum(exclArousTrials)),...
    sum(sum(exclArousTrials))/numel(exclArousTrials)*100) 

valRTs(logical(exclValTrials))=NaN; 
arousRTs(logical(exclArousTrials))=NaN; 

%% Make Table 4. 
%  Response times for valence ratings by assigned scene category.

valCat=unique(sceneInfo.Valence); 
arousCat=unique(sceneInfo.Arousal);
socCat=unique(sceneInfo.SocialContent); 

for ii=1:numel(valCat)
    ind=strcmp(sceneInfo.Valence,valCat{ii});
    ind=repmat(ind',size(valRTs,1),1);
    clearvars meanRTcat
    meanRTcat(1,1)=nanmean(valRTs(ind));
    meanRTcat(1,2)=nanstd(valRTs(ind));
    fprintf('%s \nValence M (SD)=%.0f (%.0f)\n\n', ...
        valCat{ii}, meanRTcat(1,1),meanRTcat(1,2))
end

for ii=1:numel(arousCat)
    ind=strcmp(sceneInfo.Arousal,arousCat{ii});
    ind=repmat(ind',size(valRTs,1),1);
    clearvars meanRTcat
    meanRTcat(1,1)=nanmean(valRTs(ind));
    meanRTcat(1,2)=nanstd(valRTs(ind));
    fprintf('%s \nValence M (SD)=%.0f (%.0f)\n\n', ...
        arousCat{ii}, meanRTcat(1,1),meanRTcat(1,2))
end

for ii=1:numel(socCat)
    ind=strcmp(sceneInfo.SocialContent,socCat{ii});
    ind=repmat(ind',size(valRTs,1),1);
    clearvars meanRTcat
    meanRTcat(1,1)=nanmean(valRTs(ind));
    meanRTcat(1,2)=nanstd(valRTs(ind));
    fprintf('%s \nValence M (SD)=%.0f (%.0f)\n\n', ...
        socCat{ii}, meanRTcat(1,1),meanRTcat(1,2))
end

for ii=1:numel(valCat)
    for jj=2:-1:1
        ind=strcmp(sceneInfo.Valence,valCat{ii}) & ...
            strcmp(sceneInfo.SocialContent,socCat{jj});
        ind=repmat(ind',size(valRTs,1),1);
        clearvars meanRTcat
        meanRTcat(1,1)=nanmean(valRTs(ind));
        meanRTcat(1,2)=nanstd(valRTs(ind));
        fprintf('%s %s \nValence M (SD)=%.0f (%.0f)\n\n', ...
            valCat{ii},socCat{jj}, meanRTcat(1,1),meanRTcat(1,2))
    end
end

for ii=1:numel(valCat)
    for jj=1:numel(arousCat)
        ind=strcmp(sceneInfo.Valence,valCat{ii}) & ...
            strcmp(sceneInfo.Arousal,arousCat{jj});
        ind=repmat(ind',size(valRTs,1),1);
        clearvars meanRTcat
        meanRTcat(1,1)=nanmean(valRTs(ind));
        meanRTcat(1,2)=nanstd(valRTs(ind));
        fprintf('%s %s \nValence M (SD)=%.0f (%.0f)\n\n', ...
            valCat{ii},arousCat{jj}, meanRTcat(1,1),meanRTcat(1,2))
    end
end

for ii=1:numel(valCat)
    for jj=1:numel(arousCat)
        for hh=2:-1:1
            ind=strcmp(sceneInfo.Valence,valCat{ii}) & ...
                strcmp(sceneInfo.Arousal,arousCat{jj})...
                & strcmp(sceneInfo.SocialContent,socCat{hh});
            ind=repmat(ind',size(valRTs,1),1);
            clearvars meanRTcat
            meanRTcat(1,1)=nanmean(valRTs(ind));
            meanRTcat(1,2)=nanstd(valRTs(ind));
            fprintf('%s %s %s \nValence M (SD)=%.0f (%.0f)\n\n', ...
                valCat{ii},arousCat{jj},socCat{hh},meanRTcat(1,1),meanRTcat(1,2))
        end
    end
end

%% Make subjects x scenes tables with mean RTs by affective category.

clearvars rmValRT rmArous
rmValRT.Subject=valRTs_table.Subject;
if isstruct(rmValRT)
    rmValRT=struct2table(rmValRT);
end
rmArousRT=rmValRT;

for ii=1:numel(valCat)
    for jj=1:numel(arousCat)
        for kk=1:numel(socCat)
            ind=strcmp(sceneInfo.Valence,valCat{ii}) & ...
                strcmp(sceneInfo.Arousal,arousCat{jj})...
                & strcmp(sceneInfo.SocialContent,socCat{kk});           
            varName=[valCat{ii}(1:3) arousCat{jj} socCat{kk}(1)];
            temp=valRTs(:,ind'); rmValRT.(varName)=nanmean(temp,2);
            temp=arousRTs(:,ind'); rmArousRT.(varName)=nanmean(temp,2);
        end
    end
end

rmValRT(:,{'NegLowN','NegLowS'}) = []; 
rmValRT(:,{'NeuHighN','NeuHighS'}) = [];
rmValRT(:,{'PosLowN','PosLowS'}) = [];
rmArousRT(:,{'NegLowN','NegLowS'}) = [];
rmArousRT(:,{'NeuHighN','NeuHighS'}) = [];
rmArousRT(:,{'PosLowN','PosLowS'}) = [];

writetable(rmValRT,[dataPath 'Data/COMPASS_ValenceRTsByAffect.xlsx'])
writetable(rmArousRT,[dataPath 'Data/COMPASS_ArousalRTsByAffect.xlsx'])

%% Make Figure 7. 
%  Mean response times (RTs) in milliseconds for valence ratings by affective scene 
%  category (lower arousal = low to moderate; higher arousal = moderate to high).

lineWidth=1.5;fontSize=15;
socColor=[1 .6 .1];nonSocColor=[.1 .6 1];

figure('color','w');hold on
e1=errorbar([1 2 3],[nanmean(rmValRT.NegHighS) nanmean(rmValRT.NeuMidS) nanmean(rmValRT.PosHighS)],...
    [sem(rmValRT.NegHighS) sem(rmValRT.NeuMidS) sem(rmValRT.PosHighS)],'LineWidth',lineWidth,...
    'Color',socColor,'Marker','o','MarkerEdgeColor',socColor,'MarkerFaceColor',socColor,...
    'DisplayName','Social Higher Arousal');hold on
e2=errorbar([1 2 3],[nanmean(rmValRT.NegMidS) nanmean(rmValRT.NeuLowS) nanmean(rmValRT.PosMidS)],...
    [sem(rmValRT.NegMidS) sem(rmValRT.NeuLowS) sem(rmValRT.PosMidS)],'LineWidth',lineWidth,...
    'Color',socColor,'Marker','^','MarkerEdgeColor',socColor,'MarkerFaceColor',socColor,...
    'DisplayName','Social Lower Arousal','LineStyle','--');
e3=errorbar([1 2 3],[nanmean(rmValRT.NegHighN) nanmean(rmValRT.NeuMidN) nanmean(rmValRT.PosHighN)],...
    [sem(rmValRT.NegHighN) sem(rmValRT.NeuMidN) sem(rmValRT.PosHighN)],'LineWidth',lineWidth,...
    'Color',nonSocColor,'Marker','o','MarkerEdgeColor',nonSocColor,'MarkerFaceColor',nonSocColor,...
    'DisplayName','Nonsocial Higher Arousal');
e4=errorbar([1 2 3],[nanmean(rmValRT.NegMidN) nanmean(rmValRT.NeuLowN) nanmean(rmValRT.PosMidN)],...
    [sem(rmValRT.NegMidN) sem(rmValRT.NeuLowN) sem(rmValRT.PosMidN)],'LineWidth',lineWidth,...
    'Color',nonSocColor,'Marker','^','MarkerEdgeColor',nonSocColor,'MarkerFaceColor',nonSocColor,...
    'DisplayName','Nonsocial Lower Arousal','LineStyle','--');
set(gca,'FontSize',fontSize,'LineWidth',lineWidth,'XTick',[1 2 3],'XTickLabel',valCat)
l=legend([e1 e2 e3 e4]);xlim([0 4]); ylim([1050 1500]);title('Valence RTs by Scene Category')
ylabel('Mean valence RTs');xlabel('Scene valence');axis square; box on
