%SCRIPT_FigureNearVsFarFeatures
%-------------------
%Experiment: Near vs Far
clear d;
d.bRemoveSky = 0;
d.bRemovePosition = 1;
d.bRemoveNearby = 1;
FarMasks{1} = [1 1 1 1 0];
FarMasks{2} = [1 1 1 0 1];
FarMasks{3} = [1 1 0 1 1];
FarMasks{4} = [1 0 1 1 1];
FarMasks{5} = [0 1 1 1 1];
for i = 1:5
  d.bRemoveNearbyMask = FarMasks{i}
  d.bRemoveBTRS = 0;
  d.bRemoveColTex = 1;
  FSlistFarB = FeatureSelection_BuildFrom800(d);
  d.bRemoveBTRS = 1;
  d.bRemoveColTex = 0;
  FSlistFarC = FeatureSelection_BuildFrom800(d);
  XFarSVMB = LinSVMContextX(find(FSlistFarB),:);
  XFarSVMC = LinSVMContextX(find(FSlistFarC),:);
  XFarTruB = TrueContextX(find(FSlistFarB),:);
%  ModelSVMB = CLSosusvm(XFarSVMB(:,TTS.Train),Appearancey(TTS.Train));
%  ModelSVMC = CLSosusvm(XFarSVMC(:,TTS.Train),Appearancey(TTS.Train));
%  ModelTruB = CLSosusvm(XFarTruB(:,TTS.Train),Appearancey(TTS.Train));
%  [yemplSVMB,yempSVMB{i}] = CLSosusvmC(XFarSVMB(:,TTS.Test),ModelSVMB); 
%  [yemplSVMC,yempSVMC{i}] = CLSosusvmC(XFarSVMC(:,TTS.Test),ModelSVMC); 
%  [yemplTruB,yempTruB{i}] = CLSosusvmC(XFarTruB(:,TTS.Test),ModelTruB); 
  
  ModelSVMB = CLSgentleBoost(XFarSVMB(:,TTS.Train),Appearancey(TTS.Train));
  ModelSVMC = CLSgentleBoost(XFarSVMC(:,TTS.Train),Appearancey(TTS.Train));
  ModelTruB = CLSgentleBoost(XFarTruB(:,TTS.Train),Appearancey(TTS.Train));
  [yemplSVMB,yempSVMB{i}] = CLSgentleBoostC(XFarSVMB(:,TTS.Test),ModelSVMB); 
  [yemplSVMC,yempSVMC{i}] = CLSgentleBoostC(XFarSVMC(:,TTS.Test),ModelSVMC); 
  [yemplTruB,yempTruB{i}] = CLSgentleBoostC(XFarTruB(:,TTS.Test),ModelTruB); 

  SVMBstats{i} = ROCetc2(yempSVMB{i}, Appearancey(TTS.Test));
  SVMCstats{i} = ROCetc2(yempSVMC{i}, Appearancey(TTS.Test));
  TruBstats{i} = ROCetc2(yempTruB{i}, Appearancey(TTS.Test));
  
  %figure
  %ShowROCPlus(SVMBstats{i},'r-');
  %hold on
  %ShowROCPlus(SVMCstats{i},'b-');
  %ShowROCPlus(TruBstats{i},'k-');
end

FigRoot = '/cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/AppearanceDetector/Figures/RelativeContextDistance';

%featdist = [20,15,10,5,3]
featdist = [20,10,5]
clear LegendNames;
linestyle{1} = '-';
linestyle{2} = '--';
linestyle{3} = ':';
linestyle{4} = '-.';
h = figure
useme = [1,3,4]
for i = 1:3
  ShowROC(SVMBstats{useme(i)},linestyle{i},[0 0 1], (6 - 2*i+1));
  hold on
  ShowROC(SVMCstats{useme(i)},linestyle{i},[1 .8 .8],(6 - 2*i+1));
  LegendNames{2*(i-1)+1} = sprintf('Semantic Features d = %d',featdist(i));
  LegendNames{2*(i-1)+2} = sprintf('Color and Texture d = %d',featdist(i));
end
L = legend(LegendNames,'Location','Best');
set(L,'FontSize',18);
%legend('Semantic Features','Color and Texture','Location','Best');
T = xlabel('False positive rate');
set(T,'FontSize',18);
T = ylabel('True positive rate');
set(T,'FontSize',18);
T = title(sprintf('%s detection ROC: features at distance d',objname));
set(T,'FontSize',18);

saveas(h,fullfile(FigRoot,sprintf('FeatNearFar_%s.fig',objname)),'fig');
saveas(h,fullfile(FigRoot,sprintf('FeatNearFar_%s.epsc',objname)),'epsc');
saveas(h,fullfile(FigRoot,sprintf('FeatNearFar_%s.jpg',objname)),'jpg');

featdist = [20,10,5]
linestyle{1} = '-';
linestyle{2} = '--';
linestyle{3} = ':';
linestyle{4} = '-.';
h = figure
useme = [1,3,4]
for i = 1:3
  ShowROCDifferential(SVMBstats{useme(i)},SVMCstats{useme(i)},linestyle{i},[0 0 1], (6 - 2*i+1))
  hold on
  LegendNames{i} = sprintf('Semantic - Color/Texture, d = %d',featdist(i));
end
L = legend(LegendNames,'Location','SouthEast');
set(L,'FontSize',18);
%legend('Semantic Features','Color and Texture','Location','SouthEast');
T = xlabel('False positive rate');
set(T,'FontSize',18);
T = ylabel('Difference of true positive rate');
set(T,'FontSize',18);
T = title(sprintf('%s ROC Difference: only features at distance d ',objname));
set(T,'FontSize',18);
saveas(h,fullfile(FigRoot,sprintf('FeatNearFar_Differential_%s.fig',objname)),'fig');
saveas(h,fullfile(FigRoot,sprintf('FeatNearFar_Differential_%s.epsc',objname)),'epsc');
saveas(h,fullfile(FigRoot,sprintf('FeatNearFar_Differential_%s.jpg',objname)),'jpg');

h = figure
clear AUCs
for i = 1:5
 AUCs(i,1) = SVMBstats{i}.auc;
 AUCs(i,2) = SVMCstats{i}.auc;
end
b = bar([5:-1:1],AUCs);

if(strcmpi(objname,'bicycle'))
 L = legend('Semantic Features','Color-Texture Features','Location','SouthOutside');
 set(L,'FontSize',18);
end
T = xlabel('Feature sample distance');
set(T,'FontSize',18);
T = ylabel('Area under ROC curve');
set(T,'FontSize',18);
T = title(sprintf('Feature utility vs. distance (%s)',objname));
set(T,'FontSize',18);
ticvals= {'3','5','10','15','20'}; 
set(gca,'XTickLabel',ticvals); 
axis([.5 5.5 .5 1]);
colormap('bone')
saveas(h,fullfile(FigRoot,sprintf('FeatNearFar_Bargraph_%s.fig',objname)),'fig');
saveas(h,fullfile(FigRoot,sprintf('FeatNearFar_Bargraph_%s.epsc',objname)),'epsc');
saveas(h,fullfile(FigRoot,sprintf('FeatNearFar_Bargraph_%s.jpg',objname)),'jpg');


