function [accuracyByParm,names,allvalues] = sensitivity(cv)
%  [accuracyByParm,names,allvalues] = sensitivity(cv)

%%% Collect all unique parameter values
nTestTrials = length(cv.testResults);
for ttrial = 1:nTestTrials
  [bestvalues,names] = getfields(cv.testResults{ttrial}{1});
  for i=1:length(names)
    col = find(strcmp(cv.cvresultsNames,names{i}));
    allvalues{ttrial}{i} = unique(cv.cvresults(:,col))';
  end
end

%%% combine cv results into their mean
nreps = cv.validate.repetitions;
mat = cv.cvresults(:,1:end-nreps);
mat = [mat mean(cv.cvresults(:,end-nreps+1:end),2)];


%%% accuracyByParm{testTrial}{parametervaried}
for ttrial = 1:nTestTrials
  nparms = length(allvalues{ttrial});
  %% For each parameter, collect cv accuracy for each of its values
  for parm = 1:nparms
    if isnan(bestvalues(parm))
      continue;
    end
    rows = ones(size(mat,1),1);
    rows = rows & (mat(:,1) == ttrial);
    for p = setdiff(1:nparms,parm)
      if isnan(bestvalues(p))
	continue;
      end
      col = find(strcmp(cv.cvresultsNames,names{p}));
      rows = rows & (mat(:,col) == bestvalues(p));
%      fprintf(1,'p %d %s col %d sum(rows) %d\n',...
%	  p,names{p},col,sum(rows));
    end
    col = find(strcmp(cv.cvresultsNames,names{parm}));
    accuracyByParm{ttrial}{parm} = [mat(rows,col) mat(rows,end)];
  end
end

figure(1);
for p=1:length(accuracyByParm{1})
  subplot(2,3,p);
  for i=1:length(accuracyByParm)
    r = accuracyByParm{i}{p};
    z=plot(r(:,1),r(:,2),'o-k');
	set(z,'MarkerSize',3);
    xlabel(names{p});
    ylabel('Fraction Correct');
    hold on; 
  end; 
  if p==2
    title('Sensitivity Using Best Values');
  end
  hold off;
end

%%% accuracyByParm{testTrial}{parametervaried}
for ttrial = 1:nTestTrials
  nparms = length(allvalues{ttrial});
  %% For each parameter, collect cv rmse for each of its values
  for parm = 1:nparms
    if isnan(bestvalues(parm))
      continue;
    end
    rows = ones(size(mat,1),1);
    rows = rows & (mat(:,1) == ttrial);
    %% For each value of parm, find best validation accuracy
    parmvals = allvalues{ttrial}{parm};
    curve = [];
    col = find(strcmp(cv.cvresultsNames,names{parm}));
    for pv = parmvals
      rowspv = rows & (mat(:,col) == pv);
%if parm==2
%  fprintf(1,'col %d %s sum(rows) %d pv %d sum(rowspv) %d\n',...
%      col,names{parm},sum(rows),pv,sum(rowspv));
%  end
      best = max(mat(rowspv,end));
      curve = [curve; pv best];
    end
    accuracyByParm{ttrial}{parm} = curve;
  end
end

figure(2);
for p=1:length(accuracyByParm{1})
  subplot(2,3,p);
  for i=1:length(accuracyByParm)
    r = accuracyByParm{i}{p};
    z=plot(r(:,1),r(:,2),'o-k');
set(z,'MarkerSize',3);
    xlabel(names{p});
    ylabel('Fraction Correct');
    hold on; 
  end; 
  if p==2
    title('Sensitivity Using All Values');
  end
  hold off;
end


