function [] = figure2;

%
% function [] = figure2;
%
% plots raw version of figure 2 of Einhäuser, W., Methfessel, P., & Bendixen, A. (2017). Newly acquired audio-visual associations bias perception in binocular rivalry. Vision Research, 133, 121-129. doi: 10.1016/j.visres.2017.02.001
%
%

% 

% load data file
load eyeTraces oknGain 

% load files containing the conditions per observer and per block and trial
load toneByBlockAndTrail toneByBlockAndTrial
load conditionsByObserver ...
	lowPitchColorByObserver ...
	highPitchColorByObserver ...
	lowPitchDirectionByObserver ...
	highPitchDirectionByObserver

% compute the bias for each observer and tone
biasLowPitchTone  = nan(1,16);
biasHighPitchTone = nan(1,16);
biasNoTone        = nan(1,16);

for obsNum = 1 : 16

  % to aggregate the data over all blocks and trials (which can
  % contain slightly varying amounts of valid data due to blinks and
  % fast okn phases) additional variables are needed to sum the data
  % first and later compute the
  % mean over all trials and blocks

  % for the trials, in which the low-pitch tone is presented
  positiveGainLowPitchTone = 0; % amount of sample points with positive gain
  negativeGainLowPitchTone = 0; % amount of sample points with negative gain

  % for the trials, in which the high-pitch tone is presented
  positiveGainHighPitchTone = 0; % amount of sample points with positive gain
  negativeGainHighPitchTone = 0; % amount of sample points with negative gain
  
  % for the trials, in which no tone is presented
  positiveGainNoTone = 0; % amount of sample points with positive gain
  negativeGainNoTone = 0; % amount of sample points with negative gain

  for block = 1 : 6
	
	for trial = 1 : 3
	 
	  % add data to the correct tone condition
	  if (toneByBlockAndTrial(obsNum,block,trial)==1)
		
		% low-pitch tone was played in trial
		positiveGainLowPitchTone = positiveGainLowPitchTone + ...
			sum(oknGain{obsNum,block,trial}>0.0);
		
		negativeGainLowPitchTone = negativeGainLowPitchTone + ...
			sum(oknGain{obsNum,block,trial}<0.0);
		
	  elseif (toneByBlockAndTrial(obsNum,block,trial)==2)

		% high-pitch tone was played in trial
		positiveGainHighPitchTone = positiveGainHighPitchTone + ...
			sum(oknGain{obsNum,block,trial}>0.0);
		
		negativeGainHighPitchTone = negativeGainHighPitchTone + ...
			sum(oknGain{obsNum,block,trial}<0.0);
		
	  else
		
		% no tone was played in trial
		positiveGainNoTone = positiveGainNoTone + ...
			sum(oknGain{obsNum,block,trial}>0.0);
		
		negativeGainNoTone = negativeGainNoTone + ...
			sum(oknGain{obsNum,block,trial}<0.0);
		
		
	  end % if
	  
	end % trial
	
  end % block
	
  % collect data within observer, compute bias measure
  biasLowPitchTone(obsNum) = ...
	  100*(positiveGainLowPitchTone-negativeGainLowPitchTone)/...
	  (positiveGainLowPitchTone+negativeGainLowPitchTone);
	
  biasHighPitchTone(obsNum) = ...
	  100*(positiveGainHighPitchTone-negativeGainHighPitchTone)/...
	  (positiveGainHighPitchTone+negativeGainHighPitchTone);

  biasNoTone(obsNum) = ...
	  100*(positiveGainNoTone-negativeGainNoTone)/...
	  (positiveGainNoTone+negativeGainNoTone);
	
end % obsNum
  
% so far, the data are sorted by tones, now assign them to the grating
% directions that have been coupled with the respective tone during
% induction (no-tone condition remains unaffected)

biasLeftAssociated = nan(1,16);
biasRightAssociated = nan(1,16);

for obsNum=1:16

  if (lowPitchDirectionByObserver(obsNum)==-1)&(highPitchDirectionByObserver(obsNum)==+1)
  
	biasLeftAssociated(obsNum)  = biasLowPitchTone(obsNum);
	biasRightAssociated(obsNum) = biasHighPitchTone(obsNum);
	
	
  elseif (lowPitchDirectionByObserver(obsNum)==1)&(highPitchDirectionByObserver(obsNum)==-1)

	biasLeftAssociated(obsNum)  = biasHighPitchTone(obsNum);
	biasRightAssociated(obsNum) = biasLowPitchTone(obsNum);
	
  else
	error('conidtions are inconsistent');
  end
	
end

figure(2);
clf;
bar([1:3],mean([biasLeftAssociated;biasNoTone;biasRightAssociated]'));
hold on;
errorbar([1:3],...
	mean([biasLeftAssociated;biasNoTone;biasRightAssociated]'),...
	std([biasLeftAssociated;biasNoTone;biasRightAssociated]')/sqrt(16),...
	'k.');
ylim([-30 30]);
ylabel('bias (%)');
xlabel('condition');
set(gca,'XTick',[1:3]);
set(gca,'XTickLabel',{'tone assoc. left','no tone','tone assoc. right'});
