function []=figure2;

%
% computes figure 3 and 4 of Schomaker, J., Walper, D., Wittmann, B.C., & Einhäuser, W. (2017). Attention in natural scenes: Affective-motivational factors guide gaze independently of visual salience. Vision Research, 133, 161-175.
%

% load motivational rating
load ratings motivationalValue

% select scenes for which the ground truth has the correct rank order
groundTruthValid = ...
	(motivationalValue(:,1)<motivationalValue(:,2))&...
	(motivationalValue(:,2)<motivationalValue(:,3));

% these are the scenes to be used for this analysis, only 99 scenes
% were used here
useScenes=find(groundTruthValid(1:99));

% observers to be included in the analysis
useSubs = 1:18;

% load fixation data
load fixationsExperiment2;

% load bounding box data
load boundingBoxesExperiment2;

% variables of interest
isFixated     = nan(length(useSubs),length(useScenes),3,3); % object fixated or not
isFixatedByFix = nan(length(useSubs),length(useScenes),3,3,50); % object fixated or not in nth fixation
tDwell = nan(length(useSubs),length(useScenes),3,3); % dwell time
tFirst = nan(length(useSubs),length(useScenes),3,3); % time to first fixation


% main loop
for subCount = 1 : length(useSubs)
  
  subNum = useSubs(subCount);
  
  for sceneCount = 1 : length(useScenes)
	
	sceneNum = useScenes(sceneCount);
	
	for visCond = 1 : 3
	  
	  for motCond = 1 : 3
	  
		if hasData(subNum,sceneNum,visCond,motCond)
		  
		  % initize current variables
		  isFixated(subCount,sceneCount,visCond,motCond) = 0;
		  tDwell(subCount,sceneCount,visCond,motCond) = 0;
		  tFirst(subCount,sceneCount,visCond,motCond) = inf;
		
		  % loop over all fixations
		  for fixN = 1 : 50
			
			% fixation data
			X = fixationX(subNum,sceneNum,visCond,motCond,fixN);
			Y = fixationY(subNum,sceneNum,visCond,motCond,fixN);
			D = fixationDuration(subNum,sceneNum,visCond,motCond,fixN);
			T = fixationOnset(subNum,sceneNum,visCond,motCond,fixN);
			N = fixationNumber(subNum,sceneNum,visCond,motCond,fixN);
			% is the fixation on the crtical object?
			onObj = ...
				(boundingBoxX(sceneNum,motCond)<=X)&...
				(boundingBoxY(sceneNum,motCond)<=Y)&...
				((boundingBoxX(sceneNum,motCond)+boundingBoxW(sceneNum,motCond))>=X)&...
				((boundingBoxY(sceneNum,motCond)+boundingBoxH(sceneNum,motCond))>=Y);

			if isfinite(N)&isfinite(X)
			  % if this is a true fixation, check whether it is on the object
			  isFixatedByFix(subCount,sceneCount,visCond,motCond,N) = onObj;
			end
			
			if (fixationInitial(subNum,sceneNum,visCond,motCond,fixN)==0)
			  % fixation is neither the initial one nor NaN
			  
			  if onObj
				
				if ~isfinite(tFirst(subCount,sceneCount,visCond,motCond))
				  % if not yet fixated, this is the time to first
				  tFirst(subCount,sceneCount,visCond,motCond) = T;
				end
				
				% the object is fixated
				isFixated(subCount,sceneCount,visCond,motCond)=1;
				
				% the fixation duration adds to the total dwell time
				tDwell(subCount,sceneCount,visCond,motCond) =...
					tDwell(subCount,sceneCount,visCond,motCond)+D;
			  end
			end
		  end
		end
	  end
	end
  end
end



figure(3);
clf;


subplot(4,1,1);
M = squeeze(mean(nanmean(isFixated,2)));
S = squeeze(std(nanmean(isFixated,2)));
errorbar([1:3]-.05,M(1,:),S(1,:)/sqrt(length(useSubs)),'b.-');
hold on;
errorbar([1:3],M(2,:),S(2,:)/sqrt(length(useSubs)),'k.-');
errorbar([1:3]+.05,M(3,:),S(3,:)/sqrt(length(useSubs)),'r.-');
set(gca,'XTick',[1:3]);
xlim([0.5 3.5]);
ylim([0.8 1]);
set(gca,'XTickLabel',{'appetitive','neutral','aversive'});
ylabel('p(fix)');

subplot(4,1,2);
M = squeeze(mean(nanmean(tDwell/1000,2)));
S = squeeze(std(nanmean(tDwell/1000,2)));
errorbar([1:3]-.05,M(1,:),S(1,:)/sqrt(length(useSubs)),'b.-');
hold on;
errorbar([1:3],M(2,:),S(2,:)/sqrt(length(useSubs)),'k.-');
errorbar([1:3]+.05,M(3,:),S(3,:)/sqrt(length(useSubs)),'r.-');
set(gca,'XTick',[1:3]);
xlim([0.5 3.5]);
ylim([0 2]);
set(gca,'XTickLabel',{'appetitive','neutral','aversive'});
ylabel('dwell time [s]');

subplot(4,1,4);
M = squeeze(mean(nanmedian(tFirst/1000,2)));
S = squeeze(std(nanmedian(tFirst/1000,2)));
errorbar([1:3]-.05,M(1,:),S(1,:)/sqrt(length(useSubs)),'b.-');
hold on;
errorbar([1:3],M(2,:),S(2,:)/sqrt(length(useSubs)),'k.-');
errorbar([1:3]+.05,M(3,:),S(3,:)/sqrt(length(useSubs)),'r.-');
set(gca,'XTick',[1:3]);
xlim([0.5 3.5]);
ylim([0 1.5]);
set(gca,'XTickLabel',{'appetitive','neutral','aversive'});
ylabel('median time to fix [s]');

subplot(4,1,3);
tFirst(find(isinf(tFirst)))=nan;
M = squeeze(mean(nanmean(tFirst/1000,2)));
S = squeeze(std(nanmean(tFirst/1000,2)));
errorbar([1:3]-.05,M(1,:),S(1,:)/sqrt(length(useSubs)),'b.-');
hold on;
errorbar([1:3],M(2,:),S(2,:)/sqrt(length(useSubs)),'k.-');
errorbar([1:3]+.05,M(3,:),S(3,:)/sqrt(length(useSubs)),'r.-');
set(gca,'XTick',[1:3]);
xlim([0.5 3.5]);
ylim([0 1.5]);
set(gca,'XTickLabel',{'appetitive','neutral','aversive'});
ylabel('time to fix | fix [s]');


figure(4);
clf;

MbyCond = nan(3,3,50);
SbyCond = nan(3,3,50);
LbyCond = nan(3,3,50);

for visCon = 1 : 3
  
  for motCon = 1 : 3

	for k = 1 : 50

	  MbyCond(visCon,motCon,k) = ...
		  mean(nanmean(isFixatedByFix(:,:,visCon,motCon,k),2));

	  LbyCond(visCon,motCon,k) = ...
		  sum(isfinite(nanmean(isFixatedByFix(:,:,visCon,motCon,k),2)));
	  
	  SbyCond(visCon,motCon,k) = ...
		  std(nanmean(isFixatedByFix(:,:,visCon,motCon,k),2))./sqrt(LbyCond(visCon,motCon,k));
	  
	end
	
  end

end



conName = {'low','med','high'};

for visCon = 1 : 3
  subplot(1,3,visCon);
  errorbar([0:10],squeeze(MbyCond(visCon,1,1:11))',squeeze(SbyCond(visCon,1,1:11))','r.-');
  hold on
  errorbar([0:10],squeeze(MbyCond(visCon,2,1:11))',squeeze(SbyCond(visCon,2,1:11))','b.-');
  errorbar([0:10],squeeze(MbyCond(visCon,3,1:11))',squeeze(SbyCond(visCon,3,1:11))','g.-');
  
  legend('av','ne','ap');
  xlabel('fixation number');
  ylabel('fraction on critical object');
  title(conName{visCon})
  axis([-.5 10.5 0 1]);
end
