function plotBehaviours(DataB)
%keyboard;
%Simple averages etc

% Agent Ratings
figure('color',[1 1 1],'name','AgentRating by ability (red low, blue high)');hold on;
%set(gca,'Linewidth',5);
for l=1:length(DataB.agent_rating.AbilityspecAgents)
    AverageRatingacrosstime=squeeze(mean(DataB.agent_rating.AbilityspecAgents{l},2));
    errorbar(mean(AverageRatingacrosstime,2),std(AverageRatingacrosstime,0,2)./((size(AverageRatingacrosstime,2)^.5)),'Linewidth',5);
end
set(gca,'fontsize',20);
saveas(gcf,fullfile(DataB.Basepath,'SEAFigures','AgentRating.jpg'))

% Binned Plot of Red Choices Self
lowerbound=[-100 -60 -40 -20 0 20 40 60];
higherbound=[-60 -40 -20 0 20 40 60 100];
allbounds=[lowerbound higherbound(end)];
for Isub=1:size(DataB.evidence.Overall,2)
    Binvariable=DataB.evidence.Overall(:,Isub);
    Dependentvariable=~DataB.decision.self.redchoice(:,Isub);
    for b=1:length(lowerbound)
        index=(Binvariable>=lowerbound(b)) & (Binvariable<higherbound(b));
        binnedValues(b)=mean(Dependentvariable(index));
    end
    AllBinnedValues(:,Isub)=binnedValues;
end
figure('color',[1 1 1],'name','Red SelfChoices overall Evidence');hold on
errorbar(mean(AllBinnedValues,2),std(AllBinnedValues,0,2)./((size(AllBinnedValues,2)^.5)),'Linewidth',5);
set(gca,'XTick',0.5:1:length(lowerbound)+.5,'XTicklabel',allbounds)
xlim([0 length(lowerbound)+.5])
set(gca,'XTicklabelrotation',45)
set(gca,'fontsize',20);

saveas(gcf,fullfile(DataB.Basepath,'SEAFigures','Choice_SC_Softmax.jpg'))

% Binned Plot of Accuracy Self
lowerbound=[0 10 20 30 60];
higherbound=[10 20 30 60  100];
allbounds=[lowerbound higherbound(end)];binnedValues=[];AllBinnedValues=[];
for Isub=1:size(DataB.evidence.Overall,2)
    Binvariable=abs(DataB.evidence.Overall(:,Isub));
    Dependentvariable=DataB.decision.self.correct(:,Isub);
    for b=1:length(lowerbound)
        index=(Binvariable>=lowerbound(b)) & (Binvariable<higherbound(b));
        binnedValues(b)=mean(Dependentvariable(index));
    end
    AllBinnedValues(:,Isub)=binnedValues;
end
figure('color',[1 1 1],'name','Accuracy Choices overall Evidence');hold on
set(gca,'Linewidth',5);
errorbar(mean(AllBinnedValues,2),std(AllBinnedValues,0,2)./((size(AllBinnedValues,2)^.5)),'Linewidth',5);
set(gca,'XTick',0.5:1:length(lowerbound)+.5,'XTicklabel',allbounds)
xlim([0 length(lowerbound)+.5])
set(gca,'XTicklabelrotation',45)
set(gca,'fontsize',20);
saveas(gcf,fullfile(DataB.Basepath,'SEAFigures','Accuracy_SC_Softmax.jpg'))


FigureTypeimage=figure('color',[1 1 1],'name','Split Plots');hold on

FigureTypeErrorbars=figure('color',[1 1 1],'name','Split Plots');hold on

lowerbound1=[0  15   ];
higherbound1=[   15    30 ];
lowerbound2=[0  10    ];
higherbound2=[ 10  20  ];
allbounds1=[lowerbound1 higherbound1(end)];
allbounds2=[lowerbound2 higherbound2(end)];
binnedValues=[];AllBinnedValues=[];

for Isub=1:size(DataB.evidence.Other,2)
    Binvariable1=abs(DataB.evidence.Other(:,Isub));
    Binvariable2=abs(DataB.evidence.occluded(:,Isub));    
    Dependentvariable=DataB.decision.self.correct(:,Isub);    
    for b1=1:length(lowerbound1)
        for b2=1:length(lowerbound2)
            index=(Binvariable1>=lowerbound1(b1)) & (Binvariable1<higherbound1(b1)) & (Binvariable2>=lowerbound2(b2)) & (Binvariable2<higherbound2(b2));
            binnedValues(b1,b2)=mean(Dependentvariable(index));            
        end
    end
    AllBinnedValues(:,:,Isub)=binnedValues;
end

%figure('color',[1 1 1],'name','Accurate SelfChoices Shared and occluded Evidences');hold on
figure(FigureTypeimage);
subplot(2,2,1);
title('Accurate SC Shared and occluded EVid');hold on;
imagesc(mean(AllBinnedValues,3),[0.58 .9])
set(gca,'YTick',.5:length(lowerbound1)+.5,'YTicklabel',allbounds1)
ylim([0 length(lowerbound1)+.5])
set(gca,'XTick',.5:length(lowerbound2)+.5,'XTicklabel',allbounds2)
xlim([0 length(lowerbound2)+.5])
set(gca,'XTicklabelrotation',45)
set(gca,'fontsize',20);


figure(FigureTypeErrorbars);
subplot(2,2,1);
title('Accurate SC Shared and occluded EVid');hold on;
errorbar(mean(AllBinnedValues,3),std(AllBinnedValues,0,3)./(size(AllBinnedValues,3).^.5))
set(gca,'XTick',.5:length(lowerbound1)+.5,'XTicklabel',allbounds1)
xlim([0 length(lowerbound1)+.5])
set(gca,'XTicklabelrotation',45)
set(gca,'fontsize',20);


lowerbound1=[ -30 -15 0 15   ];
higherbound1=[ -15 0  15  30 ];
lowerbound2=[ -30 -10 0  10  ];
higherbound2=[ -10  0  10 30 ];
allbounds1=[lowerbound1 higherbound1(end)];
allbounds2=[lowerbound2 higherbound2(end)];
binnedValues=[];AllBinnedValues=[];
for Isub=1:size(DataB.evidence.Other,2)
    Binvariable1=(DataB.evidence.Other(:,Isub));
    Binvariable2=(DataB.evidence.Overall(:,Isub)-DataB.evidence.Other(:,Isub));
    Dependentvariable=DataB.decision.self.redchoice(:,Isub);
    for b1=1:length(lowerbound1)
        for b2=1:length(lowerbound2)
            index=(Binvariable1>=lowerbound1(b1)) & (Binvariable1<higherbound1(b1)) & (Binvariable2>=lowerbound2(b2)) & (Binvariable2<higherbound2(b2));
            binnedValues(b1,b2)=mean(Dependentvariable(index));
        end
    end
    AllBinnedValues(:,:,Isub)=binnedValues;
end
%figure('color',[1 1 1],'name','Red SelfChoices Shared and occluded Evidences');hold on
figure(FigureTypeimage);
subplot(2,2,2);
title('Red SC Shared and occluded EVid');hold on;

imagesc(mean(AllBinnedValues,3),[0 1])
set(gca,'YTick',.5:length(lowerbound1)+.5,'YTicklabel',allbounds1)
ylim([0 length(lowerbound1)+.5])
set(gca,'XTick',.5:length(lowerbound2)+.5,'XTicklabel',allbounds2)
xlim([0 length(lowerbound2)+.5])
set(gca,'XTicklabelrotation',45)
set(gca,'fontsize',20);

figure(FigureTypeErrorbars);
subplot(2,2,2);
title('Red SC Shared and occluded EVid');hold on;
errorbar(mean(AllBinnedValues,3),std(AllBinnedValues,0,3)./(size(AllBinnedValues,3).^.5))
set(gca,'XTick',.5:length(lowerbound1)+.5,'XTicklabel',allbounds1)
xlim([0 length(lowerbound1)+.5])
set(gca,'XTicklabelrotation',45)
set(gca,'fontsize',20);

lowerbound1=[0  15   ];
higherbound1=[   15    30 ];
lowerbound2=[0  10    ];
higherbound2=[ 10  20  ];
allbounds1=[lowerbound1 higherbound1(end)];
allbounds2=[lowerbound2 higherbound2(end)];
binnedValues=[];AllBinnedValues=[];

for Isub=1:size(DataB.evidence.Other,2)
    Binvariable1=abs(DataB.evidence.Other(:,Isub));
    Binvariable2=abs(DataB.evidence.occluded(:,Isub));    
    Dependentvariable=DataB.decision.other.correct(:,Isub);    
    for b1=1:length(lowerbound1)
        for b2=1:length(lowerbound2)
            index=(Binvariable1>=lowerbound1(b1)) & (Binvariable1<higherbound1(b1)) & (Binvariable2>=lowerbound2(b2)) & (Binvariable2<higherbound2(b2));
            binnedValues(b1,b2)=mean(Dependentvariable(index));
        end
    end
    AllBinnedValues(:,:,Isub)=binnedValues;
end
%figure('color',[1 1 1],'name','Accurate OtherChoices Shared and occluded Evidences');hold on
figure(FigureTypeimage);
subplot(2,2,3);
title('Accurate OC Shared and occluded EVid');hold on;

imagesc(mean(AllBinnedValues,3),[0.58 .9])
set(gca,'YTick',.5:length(lowerbound1)+.5,'YTicklabel',allbounds1)
ylim([0 length(lowerbound1)+.5])
set(gca,'XTick',.5:length(lowerbound2)+.5,'XTicklabel',allbounds2)
xlim([0 length(lowerbound2)+.5])
set(gca,'XTicklabelrotation',45)
set(gca,'fontsize',20);

figure(FigureTypeErrorbars);
subplot(2,2,3);
title('Accurate OC Shared and occluded EVid');hold on;
errorbar(mean(AllBinnedValues,3),std(AllBinnedValues,0,3)./(size(AllBinnedValues,3).^.5))
set(gca,'XTick',.5:length(lowerbound1)+.5,'XTicklabel',allbounds1)
xlim([0 length(lowerbound1)+.5])
set(gca,'XTicklabelrotation',45)
set(gca,'fontsize',20);

lowerbound1=[ -30 -15 0 15   ];
higherbound1=[ -15 0  15  30 ];
lowerbound2=[ -30 -10 0  10  ];
higherbound2=[ -10  0  10 30 ];
allbounds1=[lowerbound1 higherbound1(end)];
allbounds2=[lowerbound2 higherbound2(end)];
binnedValues=[];AllBinnedValues=[];
for Isub=1:size(DataB.evidence.Other,2)
    Binvariable1=(DataB.evidence.Other(:,Isub));
    Binvariable2=(DataB.evidence.Overall(:,Isub)-DataB.evidence.Other(:,Isub));
    Dependentvariable=DataB.decision.other.redchoice(:,Isub);
    for b1=1:length(lowerbound1)
        for b2=1:length(lowerbound2)
            index=(Binvariable1>=lowerbound1(b1)) & (Binvariable1<higherbound1(b1)) & (Binvariable2>=lowerbound2(b2)) & (Binvariable2<higherbound2(b2));
            binnedValues(b1,b2)=mean(Dependentvariable(index));
        end
    end
    AllBinnedValues(:,:,Isub)=binnedValues;
end
%figure('color',[1 1 1],'name','Red OtherChoices Shared and occluded Evidences');hold on
figure(FigureTypeimage);
subplot(2,2,4);
title('Red OC Shared and occluded EVid');hold on;

imagesc(mean(AllBinnedValues,3),[0 1])
set(gca,'YTick',.5:length(lowerbound1)+.5,'YTicklabel',allbounds1)
ylim([0 length(lowerbound1)+.5])
set(gca,'XTick',.5:length(lowerbound2)+.5,'XTicklabel',allbounds2)
xlim([0 length(lowerbound2)+.5])
set(gca,'XTicklabelrotation',45)
set(gca,'fontsize',20);

figure(FigureTypeErrorbars);
subplot(2,2,4);
title('Red OC Shared and occluded EVid');hold on;
errorbar(mean(AllBinnedValues,3),std(AllBinnedValues,0,3)./(size(AllBinnedValues,3).^.5))
set(gca,'XTick',.5:length(lowerbound1)+.5,'XTicklabel',allbounds1)
xlim([0 length(lowerbound1)+.5])
set(gca,'XTicklabelrotation',45)
set(gca,'fontsize',20);



lowerbound1=[ -30 -15 0 15   ];
higherbound1=[ -15 0  15  30 ];
lowerbound2=[ -30 -10 0  10  ];
higherbound2=[ -10  0  10 30 ];
allbounds1=[lowerbound1 higherbound1(end)];
allbounds2=[lowerbound2 higherbound2(end)];
binnedValues=[];AllBinnedValues=[];
for Isub=1:size(DataB.evidence.Other,2)
                 signFlip=2*(DataB.evidence.Overall(:,Isub)>0)-1;
    Binvariable1=(DataB.evidence.Other(:,Isub)).*signFlip;
    Binvariable2=(DataB.evidence.Overall(:,Isub)-DataB.evidence.Other(:,Isub)).*signFlip;
    Dependentvariable=DataB.decision.self.correct(:,Isub);
    for b1=1:length(lowerbound1)
        for b2=1:length(lowerbound2)
            index=(Binvariable1>=lowerbound1(b1)) & (Binvariable1<higherbound1(b1)) & (Binvariable2>=lowerbound2(b2)) & (Binvariable2<higherbound2(b2));
            binnedValues(b1,b2)=mean(Dependentvariable(index));
        end
    end
    AllBinnedValues(:,:,Isub)=binnedValues;
end

saveas(gcf,fullfile(DataB.Basepath,'SEAFigures','BlueChoice_SC_OC_Decs.jpg'))

%figure('color',[1 1 1],'name','Red OtherChoices Shared and occluded Evidences');hold on
figure('color',[1 1 1]);subplot(2,2,1);
title('Red SC Shared and occluded EVid');hold on;

imagesc(mean(AllBinnedValues,3),[0 1])
set(gca,'YTick',.5:length(lowerbound1)+.5,'YTicklabel',allbounds1)
ylim([0 length(lowerbound1)+.5])
set(gca,'XTick',.5:length(lowerbound2)+.5,'XTicklabel',allbounds2)
xlim([0 length(lowerbound2)+.5])
set(gca,'XTicklabelrotation',45)
set(gca,'fontsize',20);

subplot(2,2,2);
title('Red SC Shared and occluded EVid');hold on;
errorbar(mean(AllBinnedValues,3),std(AllBinnedValues,0,3)./(size(AllBinnedValues,3).^.5))
set(gca,'XTick',.5:length(lowerbound1)+.5,'XTicklabel',allbounds1)
xlim([0 length(lowerbound1)+.5])
set(gca,'XTicklabelrotation',45)
set(gca,'fontsize',20);


lowerbound1=[ -30 -15 0 15   ];
higherbound1=[ -15 0  15  30 ];
lowerbound2=[ -30 -10 0  10  ];
higherbound2=[ -10  0  10 30 ];
allbounds1=[lowerbound1 higherbound1(end)];
allbounds2=[lowerbound2 higherbound2(end)];
binnedValues=[];AllBinnedValues=[];
for Isub=1:size(DataB.evidence.Other,2)
                 signFlip=2*(DataB.evidence.Overall(:,Isub)>0)-1;
    Binvariable1=(DataB.evidence.Other(:,Isub)).*signFlip;
    Binvariable2=(DataB.evidence.Overall(:,Isub)-DataB.evidence.Other(:,Isub)).*signFlip;
    Dependentvariable=DataB.decision.other.correct(:,Isub);
    for b1=1:length(lowerbound1)
        for b2=1:length(lowerbound2)
            index=(Binvariable1>=lowerbound1(b1)) & (Binvariable1<higherbound1(b1)) & (Binvariable2>=lowerbound2(b2)) & (Binvariable2<higherbound2(b2));
            binnedValues(b1,b2)=mean(Dependentvariable(index));
        end
    end
    AllBinnedValues(:,:,Isub)=binnedValues;
end
%figure('color',[1 1 1],'name','Red OtherChoices Shared and occluded Evidences');hold on
subplot(2,2,3);
title('Red OC Shared and occluded EVid');hold on;

imagesc(mean(AllBinnedValues,3),[0 1])
set(gca,'YTick',.5:length(lowerbound1)+.5,'YTicklabel',allbounds1)
ylim([0 length(lowerbound1)+.5])
set(gca,'XTick',.5:length(lowerbound2)+.5,'XTicklabel',allbounds2)
xlim([0 length(lowerbound2)+.5])
set(gca,'XTicklabelrotation',45)
set(gca,'fontsize',20);

subplot(2,2,4);
title('Red OC Shared and occluded EVid');hold on;
errorbar(mean(AllBinnedValues,3),std(AllBinnedValues,0,3)./(size(AllBinnedValues,3).^.5))
set(gca,'XTick',.5:length(lowerbound1)+.5,'XTicklabel',allbounds1)
xlim([0 length(lowerbound1)+.5])
set(gca,'XTicklabelrotation',45)
set(gca,'fontsize',20);

saveas(gcf,fullfile(DataB.Basepath,'SEAFigures','AccuracySC_OC_Decs.jpg'))


for Isub=1:size(DataB.evidence.Other,2)
    sortedAL=sort(unique(DataB.AgentAbilityLvl(:,Isub)),'ascend');
    occluded=DataB.evidence.Overall(:,Isub)-DataB.evidence.Other(:,Isub);
    EvidenceAgreement=(DataB.evidence.Other(:,Isub)>0) == (occluded>0);
    ChoiceChanges=(DataB.evidence.Other(:,Isub)>0) ~= (DataB.evidence.Overall(:,Isub)>0);
    for AL=1:length(sortedAL)
        index=DataB.AgentAbilityLvl(:,Isub)==sortedAL(AL);
        AccuraciesEvidsigns(1,AL,Isub)=mean(DataB.decision.other.correct(EvidenceAgreement & index,Isub));
        AccuraciesEvidsigns(2,AL,Isub)=mean(DataB.decision.other.correct(~EvidenceAgreement & index,Isub));
        
        AccuraciesChoiceChange(1,AL,Isub)=mean(DataB.decision.other.correct(~ChoiceChanges & index,Isub));
        AccuraciesChoiceChange(2,AL,Isub)=mean(DataB.decision.other.correct(ChoiceChanges & index,Isub));
    end
end



figure('name','accuracy split by good and bad agent','color',[1 1 1]);subplot(2,1,1);
errorbar(mean(AccuraciesEvidsigns,3)',std(AccuraciesEvidsigns,0,3)./(size(AccuraciesEvidsigns,3)^.5)')
ylim([.5 1]);set(gca,'XTick',1:2,'XTicklabel',{'Good Agent';'Bad Agent'});xlim([.8 2.2]);
set(gca,'XTicklabelrotation',45)
set(gca,'fontsize',20);
legend({'Evidence Agrees';'Evidence Disagrees'})
saveas(gcf,fullfile(DataB.Basepath,'SEAFigures','AgentSpecificEvidAgreement.jpg'))

subplot(2,1,2);
errorbar(mean(AccuraciesChoiceChange,3)',std(AccuraciesChoiceChange,0,3)./(size(AccuraciesChoiceChange,3)^.5)')
ylim([.5 1]);set(gca,'XTick',1:2,'XTicklabel',{'Good Agent';'Bad Agent'});xlim([.8 2.2]);
set(gca,'XTicklabelrotation',45)
set(gca,'fontsize',20);
legend({'OcclEvid no changeC';'OcclEvid changeC'})
saveas(gcf,fullfile(DataB.Basepath,'SEAFigures','AgentSpecificChoiceChange.jpg'))


% Same or different choice

lowerbound1=[ -30 -15 0 15   ];
higherbound1=[ -15 0  15  30 ];
lowerbound2=[ -30 -10 0  10  ];
higherbound2=[ -10  0  10 30 ];
allbounds1=[lowerbound1 higherbound1(end)];
allbounds2=[lowerbound2 higherbound2(end)];
binnedValues=[];AllBinnedValues=[];
for Isub=1:size(DataB.evidence.Other,2)
    Binvariable1=(DataB.evidence.Other(:,Isub));
    Binvariable2=(DataB.evidence.Overall(:,Isub)-DataB.evidence.Other(:,Isub));
    Dependentvariable=DataB.decision.other.redchoice(:,Isub)~=DataB.decision.self.redchoice(:,Isub);
    for b1=1:length(lowerbound1)
        for b2=1:length(lowerbound2)
            index=(Binvariable1>=lowerbound1(b1)) & (Binvariable1<higherbound1(b1)) & (Binvariable2>=lowerbound2(b2)) & (Binvariable2<higherbound2(b2));
            binnedValues(b1,b2)=mean(Dependentvariable(index));
        end
    end
    AllBinnedValues(:,:,Isub)=binnedValues;
end
figure('color',[1 1 1],'name','Red Choice Difference Shared and occluded Evidences');hold on
title('Same or different choice in SC and OC by evidences');hold on;
subplot(1,2,1)
imagesc(mean(AllBinnedValues,3),[0 1])
set(gca,'YTick',.5:length(lowerbound1)+.5,'YTicklabel',allbounds1)
ylim([0 length(lowerbound1)+.5])
set(gca,'XTick',.5:length(lowerbound2)+.5,'XTicklabel',allbounds2)
xlim([0 length(lowerbound2)+.5])
set(gca,'XTicklabelrotation',45)
set(gca,'fontsize',20);

subplot(1,2,2)
title('Same or different choice in SC and OC by evidences');hold on;
errorbar(mean(AllBinnedValues,3),std(AllBinnedValues,0,3)./(size(AllBinnedValues,3).^.5))
set(gca,'XTick',.5:length(lowerbound1)+.5,'XTicklabel',allbounds1)
xlim([0 length(lowerbound1)+.5])
set(gca,'XTicklabelrotation',45)
set(gca,'fontsize',20);


choicedivergance=DataB.decision.other.redchoice~=DataB.decision.self.redchoice;
divprop=sum(choicedivergance,1)./size(choicedivergance,1);
figure('color',[1 1 1],'name','choice divergence proportion');hold on
title('proportion of trials with choice divergence frequency histogram')
hist(divprop)

