% Train on individual cells,
% evaluate on individual cells
clear all; addpath ~/matlab/ideas/hep2cells;
load cellssimple_rotinv.mat
xtrain = a; xtest = b; a = [a;b];

% get imageIDs from test:
testimlab = getident(xtest,'image');

% the classifier:
%u =  scalem([],'variance')*loglc2([],0.01);
%u = scalem([],'variance')*knnc;
u = scalem([],'variance')*liknonc;
%u = scalem([],'variance')*libsvc([],[],0.01);
%u = scalem([],'variance')*libsvc([],proxm([],'r',12),1);

% ***************
% train on cells:
% ***************
wtrain = xtrain*u;
% test on cells:
acc_CC = 1-xtest*wtrain*testd;
c_CC = confmat(xtest*wtrain);
% test on image level:
[labout,truelab] = majorityimagevote(xtest,wtrain);
%[labout,truelab] = combinecells(xtest,wtrain);
acc_CI = mean(all(truelab==labout,2));
c_CI = confmat(truelab,labout);
if size(c_CI,2)<size(c_CI,1), c_CI = [c_CI zeros(6,1)]; end

% *********************
% crossval over images:
% *********************
imlab = getident(a,'image');
[nlab,baglab] = renumlab(imlab);
nrfolds = length(baglab); % leave one image out

truelab = [];
ll = getlabels(wtrain);
c = size(ll,1);
n = zeros(nrfolds,2);
c_IC = zeros(c,c);
c_II = zeros(c,c);
Iall = (1:size(nlab,1))';
for i=1:nrfolds
   dd_message(4,'*');
   Jtst = find(nlab==i);
   Jtrn = Iall; Jtrn(Jtst)=[];
   x = a(Jtrn,:);
   z = a(Jtst,:);

   % train classifier:
   w = x*u;
   % test on cells
   labout = z*w*labeld;
   Icorr = all(getlab(z)==labout,2);
   n(i,1) = sum(Icorr);
   n(i,2) = mean(Icorr);
   c_IC = c_IC + confmat(z*w);

   % test on images
   [labout,truelab(i,:)] = majorityimagevote(z,w);
   %[labout,truelab(i,:)] = combinecells(z,w);
   i1 = strmatch(char(truelab(i,:)),ll);
   i2 = strmatch(labout,ll);
   c_II(i1,i2) = c_II(i1,i2) + 1;
end
acc_IC = sum(diag(c_IC))/sum(c_IC(:));
acc_II = sum(diag(c_II))/sum(c_II(:));

dd_message(4,'\n');

save results_liknon ll c_CC acc_CC c_CI acc_CI truelab n c_IC acc_IC c_II acc_II

% show all:
showconfmat(c_CC,ll);
fprintf('Train:Cell   Test:cell       %3.1f%% correct\n',100*acc_CC);
showconfmat(c_CI,ll);
fprintf('Train:Cell   Test:image      %3.1f%% correct\n',100*acc_CI);
fprintf('*************************************************************\n');
for i=1:28
   fprintf('Image %2d:%s  %3d   %3.1f%%\n',i,truelab(i,:),n(i,1),100*n(i,2));
end
showconfmat(c_IC,ll);
fprintf('Train:Image   Test:cell      %3.1f%% correct\n',100*acc_IC);
showconfmat(c_II,ll);
fprintf('Train:Image   Test:image     %3.1f%% correct\n',100*acc_II);

