function []=figure2;

%
% computes figure 2 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
useScenes=find(groundTruthValid);

% observers to be included in the analysis (#15 hat missing data due
% to technical failure)
useSubs = [1:14,16];

% load fixation data
load fixationsExperiment1;

% load bounding box data
load boundingBoxesExperiment1;

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


% main loop
for subCount = 1 : length(useSubs)
  
  subNum = useSubs(subCount);
  
  for sceneCount = 1 : length(useScenes)
	
	sceneNum = useScenes(sceneCount);
	
	for condNum = 1 : 3
	  
	  % initize current variables
	  isFixated(subCount,sceneCount,condNum) = 0;
	  tDwell(subCount,sceneCount,condNum) = 0;
	  tFirst(subCount,sceneCount,condNum) = inf;
	  
	  % loop over all fixations
	  for fixN = 1 : 50
		
		% fixation data
		X = fixationX(subNum,sceneNum,condNum,fixN);
		Y = fixationY(subNum,sceneNum,condNum,fixN);
		D = fixationDuration(subNum,sceneNum,condNum,fixN);
		T = fixationOnset(subNum,sceneNum,condNum,fixN);
	
		% is the fixation on the crtical object?
		onObj = ...
			(boundingBoxX(sceneNum,condNum)<X)&...
			(boundingBoxY(sceneNum,condNum)<Y)&...
			((boundingBoxX(sceneNum,condNum)+boundingBoxW(sceneNum,condNum))>X)&...
			((boundingBoxY(sceneNum,condNum)+boundingBoxH(sceneNum,condNum))>Y);
		if isfinite(X)
		  % if this is a true fixation, check whether it is on the object
		  isFixatedByFix(subCount,sceneCount,condNum,fixN) = onObj;
		else
		  isFixatedByFix(subCount,sceneCount,condNum,fixN) = nan;
		end
		
		if (fixationInitial(subNum,sceneNum,condNum,fixN)==0)
		  % fixation is neither the initial one nor NaN
		  
		  if onObj
		  
			if ~isfinite(tFirst(subCount,sceneCount,condNum))
			  % if not yet fixated, this is the time to first
			  tFirst(subCount,sceneCount,condNum) = T;
			end
		  
			% the object is fixated
			isFixated(subCount,sceneCount,condNum)=1;

			% the fixation duration adds to the total dwell time
			tDwell(subCount,sceneCount,condNum) =...
				tDwell(subCount,sceneCount,condNum)+D;
		  end
		end
	  end
	end
  end
end


% plot the data
figure(2);
clf;
subplot(5,1,1);

% mean over images
MisFixated = squeeze(mean(isFixated,2));

% plot
bar(mean(MisFixated));
hold on;
errorbar(mean(MisFixated),std(MisFixated)/sqrt(size(MisFixated,1)),'.');
xlabel('condition');
set(gca,'XTick',[1:3]);
set(gca,'XTickLabel',{'aversive','neutral','appetitive'});
ylabel('p(fix)');


subplot(5,1,2);

% mean over images
MtDwell = squeeze(mean(tDwell,2))/1000;

% plot
bar(mean(MtDwell));
hold on;
errorbar(mean(MtDwell),std(MtDwell)/sqrt(size(MtDwell,1)),'.');
xlabel('condition');
set(gca,'XTick',[1:3]);
set(gca,'XTickLabel',{'aversive','neutral','appetitive'});
ylabel('dwell time [s]');
ylim([0 1.5]);


subplot(5,1,3);

% remove unfixated objects for first analysis
cT = tFirst;
cT(find(cT==inf))=nan;
cM = squeeze(nanmean(cT,2))/1000;

% plot
bar(mean(cM));
hold on;
errorbar(mean(cM),std(cM)/sqrt(size(cM,1)),'.');
xlabel('condition');
set(gca,'XTick',[1:3]);
set(gca,'XTickLabel',{'aversive','neutral','appetitive'});
ylabel('time to fix | fix [s]');
ylim([0 1.5]);


subplot(5,1,4);

medCriticalT   = squeeze(median(tFirst,2))/1000;

% remove subject that has <50% fixations
find(any(medCriticalT'==inf))
medCriticalT(find(any(medCriticalT'==inf)),:)=[];

% plot
bar(mean(medCriticalT));
hold on;
errorbar(mean(medCriticalT),std(medCriticalT)/sqrt(size(medCriticalT,1)),'.');

xlabel('condition');
set(gca,'XTick',[1:3]);
set(gca,'XTickLabel',{'aversive','neutral','appetitive'});
ylabel('median time to first fix. [s]');
ylim([0 1.5]);

subplot(5,1,5);

% mean over images
Mtmp = 100*squeeze(nanmean(isFixatedByFix,2));

% mean over observers
M = squeeze(nanmean(Mtmp));
L = squeeze(nansum(isfinite(Mtmp)));
S = squeeze(nanstd(Mtmp))./sqrt(L);
errorbar([0:10],M(1,1:11)',S(1,1:11)','r.-');
hold on
errorbar([0:10],M(2,1:11)',S(2,1:11)','b.-');
errorbar([0:10],M(3,1:11)',S(3,1:11)','g.-');
xlabel('fixation number');
ylabel('critical object fixated (%)');
legend('aversive','neutral','appetitive');
return

