function [classes,binned] = showClasses(cv,nbins)
%%% classes = showClasses(CV.msf,nbins)

figure;
[nTasks,nTrials] = size(cv.classes);
for ttrial = 1:nTrials
  c = cv.testResults{ttrial}{5};
  npertask = length(c)/nTrials; %assumes each trial has same number samples
  correct = repmat(1:nTasks,npertask,1);
  classes{ttrial} = [correct(:) c'];

  subplot(nTrials,1,ttrial);
  adj = [classes{ttrial}(:,1)-0.2 classes{ttrial}(:,2)];
  plot(adj,'k');
  title(['Test Trial ' num2str(ttrial)]);
  if ttrial == nTrials,   xlabel('Sample'); end
  ylabel('Task Index');
  childs = get(gca,'Children');
  set(childs(2),'LineStyle','--');
end

figure;
[nTasks,nTrials] = size(cv.classes);
for ttrial = 1:nTrials
  subplot(nTrials,1,ttrial);
  binned{ttrial} = bins(classes{ttrial},nbins);
  adj = [binned{ttrial}(:,1)-0.2 binned{ttrial}(:,2)];
  plot(adj,'k');
  title(['Test Trial ' num2str(ttrial)]);
  if ttrial == nTrials,   xlabel('Sample'); end
  ylabel('Task Index');
  childs = get(gca,'Children');
  set(childs(2),'LineStyle','--');

  percent = sum(classes{ttrial}(:,1) == classes{ttrial}(:,2)) / ...
      size(classes{ttrial},1) * 100;
  percentbinned = sum(binned{ttrial}(:,1) == binned{ttrial}(:,2)) / ...
      size(binned{ttrial},1) * 100;
  fprintf(1,'Test Trial %d Percent correct %5.1f, %d bins Percent Correct %5.1f\n',...
			ttrial,percent,nbins,percentbinned);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function binned = binsAvg(cv,nbins)
% 
% reps = showTestRep(cv);
% 
% [nTasks,nTrials] = size(cv.classes);
% for ttrial = 1:nTrials
%   w = cv.testResults{ttrial}{2}.weights;
%   w = w - repmat(mean(w,2),1,nTasks);
%   b = cv.testResults{ttrial}{2}.bias;
%   funcs = reps{ttrial}' * w + repmat(b',size(reps{ttrial},2),1);
%   n = size(funcs,1);
%   for r = 1:n-nbins+1
%     v = mean(funcs(r:r+nbins-1,:));
%     [junk,maxi] = max(v);
%     binned(r,:) = [cv.classes{maxi;
%   end
% end
%   
% end
% 
% for r = 1:n-nbins+1
%   for c = 1:size(data,2)
%     v = data(r:r+nbins-1,c);
%     uniq = unique(v)';
%     most = 0;
%     for u = uniq
%       count = sum(v==u);
%       if count > most
% 	most = count;
% 	majority = u;
%       end
%     end
%     binnedMaj(r,c) = majority;
%   end
% end
% 
% 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function binnedMaj = bins(data,nbins)
% data has sequences in columns

n = size(data,1);
binnedMaj = zeros(n-nbins+1,size(data,2));
for r = 1:n-nbins+1
  for c = 1:size(data,2)
    v = data(r:r+nbins-1,c);
    uniq = unique(v)';
    most = 0;
    for u = uniq
      count = sum(v==u);
      if count > most
	most = count;
	majority = u;
      end
    end
    binnedMaj(r,c) = majority;
  end
end


