%SCRIPT_BuildFigure_ContextModulatedAppearance
%
%intent: for cropped data show that context improves detection


load('C1_ObjectDetection.mat','ye_toyota2');%-->ye_toyota2 (this is the object detection score)

%Ctx{1} = load('ContextData_128x128_car_f001.mat');
%Ctx{2} = load('ContextData_128x128_pedestrian_f001.mat');
%Ctx{3} = load('ContextData_128x128_bicycle_f001.mat');

Ctx{1} = load('ContextData_24sample_car_f001.mat');
Ctx{2} = load('ContextData_24sample_pedestrian_f001.mat');
Ctx{3} = load('ContextData_24sample_bicycle_f001.mat');

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

ctxnames{1} = 'C2';
ctxnames{2} = 'C2X';
ctxnames{3} = 'Tru';
ctxnames{4} = 'C2Tru';
ctxnames{5} = 'BWTru';
ctxnames{6} = 'X';
ctxnames{7} = 'C2XNoPos';
ctxnames{8} = 'TruNoPos';

ctxn{1} = 'ctx_C2';
ctxn{2} = 'ctx_C2X';
ctxn{3} = 'ctx_Tru';
ctxn{4} = 'ctx_C2Tru';
ctxn{5} = 'ctx_BWTru';
ctxn{6} = 'ctx_X';
ctxn{7} = 'ctx_C2XNoPos';
ctxn{8} = 'ctx_TruNoPos';

for i = 1:3
  ContextModulation{i} = zeros(5,400);
end
for iObj = 1:3
   a = ye_toyota2{iObj}(1:length(Ctx{iObj}.(ctxn{1})));
   for iContext = [1,4,6]
      fprintf('%s %s\n',objnames{iObj},ctxnames{iContext});
      c = Ctx{iObj}.(ctxn{iContext});
      y = Ctx{iObj}.y;
      m = min(c);
      M = max(c);
      dTHc = (M-m)/(399);
      comb = a;
      n = 1;
      for THc = m:dTHc:M
         if(mod(n,20)==0),fprintf('.'),end
	 comb(find(c<THc)) = -inf;
         R = ROCetc2(comb',y',1);
	 ContextModulation{iObj}(iContext,n) = R.auc;
         n = n+1;
	 %keyboard
      end
   end
end

ratios = 0:.05:1
nRatios = length(ratios);
for i = 1:3
  ContextModulation2{i} = zeros(5,nRatios);
end
for iObj = 1:3
   a = ye_toyota2{iObj}(1:length(Ctx{iObj}.(ctxn{1})));
   for iContext = [1,4,6]
      fprintf('%s %s\n',objnames{iObj},ctxnames{iContext});
      c = Ctx{iObj}.(ctxn{iContext});
      y = Ctx{iObj}.y;
      n = 1;
      for n = 1:nRatios
         comb = (1 - ratios(n)) * a + (ratios(n)) * c;
         R = ROCetc2(comb',y',1);
	 ContextModulation2{iObj}(iContext,n) = R.auc;
         n = n+1;
      end
   end
end
