function [yel,ye,ROC,Model,ytest] = GetShapeBasedROCPerformance(Root,Template,variablename, objidx, options,savename);
%function [yel,ye,ROC,Model,ytest] = GetShapeBasedROCPerformance(Root,Template,variablename, objidx, options,savename);
%
%computes performance on the shape based object data.
%will randomly pick features if there are over 8000 features!


HelpTextInfo = 'GetPerformance';
AppRoot = '/cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/AppearanceDetector/';

d.bRandsplit = 0; %0 means that we will take the first 2/3 for training
d.TrainingPart = 2/3;
d.sPARAMS.Nrounds = 100;
d.sPARAMS.R = 0;
d.sPARAMS.T = 0;
d.featuresubset = [];
d.MAX_FILES_TO_USE = 4;
seedrand;
d.RandSeed = floor(rand*99999);
options = ResolveMissingOptions(options,d);

sPARAMS = options.sPARAMS;

objname{1} = 'car';
objname{2} = 'pedestrian';
objname{3} = 'bicycle';

objs2do = objidx;
for i = objs2do
  on = objname{i};
  [yfull, NewDataX] = GetNewDataX(Template, variablename, on,Root,options);
  if(size(NewDataX,1) > 4000), 
    rand('state',options.RandSeed);
    p = randperm(size(NewDataX,1));
    NewDataX = NewDataX(p(1:4000),:);
    fprintf('TOO MANY FEATURES: Randomly sampling features');
  end
  L = length(yfull);
  if(options.bRandsplit)
    nTrain = round(options.TrainingPart * L);
    nTest = L - nTrain;
    rand('state',options.RandSeed);
    p = randperm(L);
    Test = p(1:nTest);
    Train = p((nTest+1):end);
  else
    Test = 1:(floor(L * (1-options.TrainingPart)));
    Train = (floor(L * (1-options.TrainingPart))+1):L;
  end
  DXtest = NewDataX(:,Test);
  ytest = yfull(:,Test);
  DXtrain = NewDataX(:,Train);
  ytrain = yfull(:,Train);
  Model = CLSgentleBoost(DXtrain,ytrain',sPARAMS);
  [yel,ye] = CLSgentleBoostC(DXtest,Model);
  ROC = ROCetc2(ye' ,ytest);
end

if(exist('savename'))
  save(savename,'HelpTextInfo','yel','ye','ROC','Root','Template','variablename','objidx','Model','options','ytest','Train','Test');
end


function [yfull, NewDataX] = GetNewDataX(Template, variablename,on,Root,options)
NewDataX = [];
yfull = [];
if(isfield(options,'MAX_FILES_TO_USE'))
  MAX_TAKE = options.MAX_FILES_TO_USE;
else
  MAX_TAKE = 4;
end
if(not(iscell(Template)))
  d = dir(fullfile(Root,sprintf('AppearanceData_128x128_%s*.mat',on)));
  d2 = dir(fullfile(Root,sprintf(Template,on)));
  for iFl = 1:min(min(length(d),length(d2)),MAX_TAKE);
    load(fullfile(Root,d(iFl).name));%-->grayX,toyotaX,y;
    D = load(fullfile(Root,d2(iFl).name));%-->eval(variablename);
    Part = getfield(D,variablename);
    if(isfield(options,'featuresubset'))
         if(not(isempty(options.featuresubset)))
  	   Part = Part(options.featuresubset,:);
	 end
    end
    NewDataX = [NewDataX,Part];
    yfull = [yfull,y]; 
  end
else
  sn = 1;
  d = dir(fullfile(Root,sprintf('AppearanceData_128x128_%s*.mat',on)));
  for iGroup = 1:length(Template)
    d2 = dir(fullfile(Root,sprintf(Template{iGroup},on)));
    sq = 1;
    for iFl = 1:min(min(length(d),length(d2)),MAX_TAKE);
      D = load(fullfile(Root,d2(iFl).name));%-->eval(variablename);
      [n,q] = size(getfield(D,variablename{iGroup}));
      Part = getfield(D,variablename{iGroup});
      if(not(iscell(options.featuresubset)))
        tempfeaturesubset = options.featuresubset;
        for i = 1:length(Template)
	  options.featuresubset{i} = tempfeaturesubset;
        end
      end
      if(isfield(options,'featuresubset'))
         if(not(isempty(options.featuresubset{iGroup})))
  	   Part = Part(options.featuresubset{iGroup},:);
	   n = length(options.featuresubset{iGroup});
	 end
      end
      NewDataX(sn:(n+sn-1),sq:(sq+q-1)) = Part;
      sq = sq + q;
      if(iGroup == 1)
        load(fullfile(Root,d(iFl).name));%-->grayX,toyotaX,y;
        yfull = [yfull,y];
      end 
    end %loop over breaks in data on disk
  sn = size(NewDataX,1) + 1;
  end %loop over data types
end



     
