%SCRIPT_ContextAndAppearanceCombined
%
%intent:  Build ROCs of the effect of Contextual modulation of appearance.


load ContextYemp5;%-->Context
load AppearanceYemp5;%-->Appearance
%load p_Train_01;%-->p_Train

%RESET THIS OUTSIDE
%oname = 'car';
clear ROC

load(sprintf('SobelAppearanceTrainData_%s',oname));%-->X,y,N,ExtractedLocations

n_examples = length(Appearance.yempl.(oname));
p_Test = setdiff(1:n_examples, Appearance.pTrain.(oname));

y_test = Appearance.ytrue.(oname)(p_Test);
ROC.ColTex_Pos = ROCetc2(Context.yemp.(oname).ColTex_Pos(p_Test),y_test);
ROC.BTRS_POS = ROCetc2(Context.yemp.(oname).BTRS_POS(p_Test),y_test);
ROC.ALL = ROCetc2(Context.yemp.(oname).ALL(p_Test),y_test);
ROC.TRUE_BTRS = ROCetc2(Context.yemp.(oname).TRUE_BTRS(p_Test),y_test);
ROC.JustPosition = ROCetc2(Context.yemp.(oname).JustPosition(p_Test),y_test);
ROC.Appearance = ROCetc2(Appearance.yemp.(oname)(p_Test), y_test);

%lambdavals = [.005, .01, .02, .04, .08, .16];
lambdavals = .00:.01:.5;
fns = fieldnames(ROC);
l = length(Appearance.yemp.(oname)(p_Test));
%for j = 1:length(fns)
for j = [1,2,4,5];
   f = fns{j};
   if(strcmpi(f,'Appearance'))
     continue;
   end
   %decide whether it is a foreward or reverse classifier
   %this will work unless every example is the same label
   %then there is no way to tell
   [M,Mi] = max(Context.yemp.(oname).(f)(p_Test));
   if((Context.yempl.(oname).(f)(p_Test(Mi))) > 0)
     [s,si] = sort(Context.yemp.(oname).(f)(p_Test));
     fprintf('Forewards Context\n')
   else
      [s,si] = sort(-Context.yempl.(oname).(f)(p_Test));
      fprintf('Backwards Context\n')
   end 
   [M,Mi] = max(Appearance.yemp.(oname)(p_Test));
   if((Appearance.yempl.(oname)(p_Test(Mi))) > 0)
     fprintf('Forewards Appearance\n')
     ResetVal = -inf;
   else
     fprintf('Backwards Appearance\n')
     ResetVal = inf;
   end 
   
   for i = 1:length(lambdavals)
      lambda = lambdavals(i);
      x = Appearance.yemp.(oname)(p_Test);
      x(si(1:floor(l * lambda))) = ResetVal;
      ROC.(f).Lambda{i} = ROCetc2(x,y_test,1);
      ROC.(f).LambdaAucVec(i) = ROC.(f).Lambda{i}.auc;
   end
end

%Figure of appearance and Context ROCs
%figure
%plot(ROC.JustPosition.normfp, ROC.JustPosition.normtp, 'k-');
%hold on
%plot(ROC.ColTex_Pos.normfp, ROC.ColTex_Pos.normtp, 'b-');
%hold on
%plot(ROC.BTRS_POS.normfp, ROC.BTRS_POS.normtp, 'r-');
%hold on
%plot(ROC.TRUE_BTRS.normfp, ROC.TRUE_BTRS.normtp, 'r--');
%hold on
%plot(ROC.Appearance.normfp, ROC.Appearance.normtp, 'g-');
%title([oname,' Context and Appearance']);

%Context Thresholding Images.
%for j = 1:length(fns)
for j = [1,2,4,5];
   f = fns{j};
   if(strcmpi(f,'Appearance'))
     continue;
   end
   figure
   %only plot the lambda with the greatest gain in auc.
   %for i = 1:length(lambdavals)
   %    plot(ROC.(f).Lambda{i}.normfp,ROC.(f).Lambda{i}.normtp, 'r:');
   %    hold on
   %end
   [ma,mai] = max(ROC.(f).LambdaAucVec);
   plot(ROC.(f).normfp, ROC.(f).normtp, 'LineStyle','-','LineWidth',2,'Color',[.2,.6,1]);
   hold on
   plot(ROC.Appearance.normfp, ROC.Appearance.normtp, 'LineStyle','--','Color',[1,.3,.2],'LineWidth',4);
   plot(ROC.(f).Lambda{mai}.normfp,ROC.(f).Lambda{mai}.normtp, 'k-');
   d = ROC.(f).Lambda{mai}.auc - ROC.Appearance.auc;
%   title([oname,' Augmented Detection ',f]) 
%   title([oname,' Augmented Detection']) 
   v = lambdavals(mai);
   plot([0 1],[0 1],'k-.');
   legendnames{3} = 'Best Combination Detector';
   legendnames{1} = 'Context Detector';
   legendnames{2} = 'Appearance Detector';
   legendnames{4} = 'Chance';
   legend(legendnames);
   xlabel('False positive rate');
   ylabel('True positive rate');
   %title(['Context augmented detection of ',oname,' using ',f,' Context: AUC gain is ',sprintf('%.2f',d)]) 
   title([oname,' detection']); 
   fprintf('Best Combination thows away %.0f %% of candidates',v*100);
   fprintf(['Context augmented detection of ',oname,' using ',f,' Context: AUC gain is ',sprintf('%.2f',d)]) 
end


