function AP = Average_Precision(filename)
% filename = one line per sample, the similarities to the class in the
first raw and the groundtruth in the second raw (0 or 1)
MAP is the average of AP among all classes
% H. Glotin feb. 2010. glotin@univ-tln.fr

X = load( filename ) ;
SIM = X(:,1);
TRUTH = X(:,2);
draw = 0 ;
[ v r ] = sort(SIM,'descend');
veritetest = TRUTH(r);
totalcorrectdanstest= sum(veritetest);
  renvoyecorrect = cumsum(veritetest);
  P = renvoyecorrect ./ ([1:length(veritetest)]');
  R = renvoyecorrect / totalcorrectdanstest;
AP = sum(diff(R).*(P(1:end-1)+P(2:end)))/2;
