function [accuracy,clipVsPlot,withinClip,withinPlot,inTrain, ...
	  notInTrain,longerClips,confMat] = computeError(predictions);

% This file is part of the data release for the ICANN/PASCAL2
% Challenge on MEG mind reading, and was written by Arto Klami.
% See http://www.cis.hut.fi/icann2011/mindreading.php for more
% information.
%
% Input:
% - a column vector of predicted labels for the test samples
%
% Output:
% - accuracy: The ratio of correct predictions
% - clipVsPlot: Accuracy in the binary task of separating the clips
%               from the films with plot
% - withinClip: Accuracy for recognizing the clips
% - withinPlot: Accuracy for recognizing the films
% - inTrain: Accuracy for samples from stimulus shown also during
%            training
% - notInTrain: Accuracy for new stimuli
% - longerClips: Accuracy on full clips (or 8 consecutive samples)
% - confMat: Confusion matrix, predictions as rows and true classes
%            as columns
%

% Read in the necessary information
load('testClassData.mat');

% Prediction accuracy
accuracy = length(find(predictions==class_test_day2))/length(class_test_day2);

% Compute the confusion matrix
confMat = crosstab(predictions,class_test_day2);

% Accuracies for the alternative tasks
clipVsPlot = (sum(sum(confMat(1:3,1:3))) + sum(sum(confMat(4:5,4: ...
						  5))))/length(class_test_day2);

ind = find(class_test_day2<4 & predictions<4);
withinClip = length(find(class_test_day2(ind)==predictions(ind))) ...
    / length(ind);

ind = find(class_test_day2>3 & predictions>3);
withinPlot = length(find(class_test_day2(ind)==predictions(ind))) / ...
    length(ind);

% Accuracy for samples within train and outside of it
ind = find(detailedTest(:,3));
inTrain = length(find(predictions(ind)==class_test_day2(ind)))/ ...
	  length(ind);

ind = find(~detailedTest(:,3));
notInTrain = length(find(predictions(ind)==class_test_day2(ind)))/ ...
    length(ind);

% Accuracy of making predictions based on all consecutive samples
% of the same stimulus category (for short clips) or 8 consecutive
% samples (for the longer films)

% Re-order the samples into chronological order
ind = find(detailedTest(:,1)==6);
[foo,temp] = sort(detailedTest(ind,2)); order = ind(temp);
ind = find(detailedTest(:,1)==7);
[foo,temp] = sort(detailedTest(ind,2)); order = [order;ind(temp)];
ind = find(detailedTest(:,1)==8);
[foo,temp] = sort(detailedTest(ind,2)); order = [order;ind(temp)];

% Go through the clips one by one, estimating the prediction for
% the whole clip
i = 1; n = 1;
acc = 0;
trueClass = [];
while(i<length(order));
  j = i;
  while(j<length(order) & detailedTest(order(j),1)== ...
	detailedTest(order(i),1) & class_test_day2(order(j))== ...
	class_test_day2(order(i)));
    j = j + 1;
  end;
  setSamples = i:j;

  % Find the most common class; if there are more than one then
  % give 1/K points of accuracy simulating a random choice over the
  % K alternatives
  tab = crosstab([predictions(order(setSamples));[1:5]']);
  ind = find(tab==max(tab));
  if(tab(class_test_day2(order(setSamples(1)))) == max(tab));
    acc = acc + 1/length(ind);
  end;
  trueClass(n) = class_test_day2(order(setSamples(1)));
  
  i = j + 1;
  n = n + 1;
end;

% Do the same for the movies, by dividing the movie into blocks
% of 8 samples each
ind = find(detailedTest(:,1)==10 & class_test_day2==4);
[foo,temp] = sort(detailedTest(ind,2)); order = ind(temp);
i = 1;
while(i<=length(order)-8)
  setSamples = i:(i+7);

  tab = crosstab([predictions(order(setSamples));[1:5]']);
  ind = find(tab==max(tab));
  if(tab(class_test_day2(order(setSamples(1)))) == max(tab));
    acc = acc + 1/length(ind);
  end;
  trueClass(n) = class_test_day2(order(setSamples(1)));
  setSize(n) = length(setSamples);
  
  i = i + 8;
  n = n + 1;
end;
ind = find(detailedTest(:,1)==10 & class_test_day2==5);
[foo,temp] = sort(detailedTest(ind,2)); order = ind(temp);
i = 1;
while(i<=length(order)-8)
  setSamples = i:(i+7);

  tab = crosstab([predictions(order(setSamples));[1:5]']);
  ind = find(tab==max(tab));
  if(tab(class_test_day2(order(setSamples(1)))) == max(tab));
    acc = acc + 1/length(ind);
  end;
  trueClass(n) = class_test_day2(order(setSamples(1)));
  setSize(n) = length(setSamples);
  
  i = i + 8;
  n = n + 1;
end;

longerClips = acc / n;
