hb_setup();

res = rproc.read('scoresroot', ...
  fullfile(hb_path, 'matlab', 'scores', 'scores_all'));

norm_splits = {'c'};
norms_path = fullfile(hb_path, 'matlab', 'data', 'best_normalizations.csv');
norms = readtable(norms_path, 'delimiter', ',');
norms.Properties.RowNames = norms.descriptor;

%%
outpath = fullfile(hb_path, 'matlab', 'results', 'article');
vl_xmkdir(outpath);
addpath(fullfile(hb_path, '../matlab2tikz/src/'));

sequences = utls.listdirs(fullfile(hb_path, 'data', 'hpatches-release'));
illum_seq = cellfun(@(a) strcmp(a(1:2), 'i_'), sequences);
viewp_seq = cellfun(@(a) strcmp(a(1:2), 'v_'), sequences);

categories = cellfun(@(a) a(1), res.matching.sequence, 'Uni', false);
res.matching.category = categories;
% Compute the matching scores averages
res.matching_g = varfun(@mean, res.matching, ...
  'GroupingVariables', {'descriptor', 'geom_noise', 'category'}, ...
  'InputVariables', 'ap');

%% Separate the normalisation type and split out of descriptor name

res.verification_e = rproc.postproc_norm(res.verification);
res.matching_ge = rproc.postproc_norm(res.matching_g);
res.retrieval_e = rproc.postproc_norm(res.retrieval);


%% Compute average over the norm splits
res.verification_en = varfun(@mean, res.verification_e, ...
  'GroupingVariables', {'descriptor', 'split', 'negs', 'geom_noise', 'method', 'norm_type'}, ...
  'InputVariables', 'pr_ap');

res.matching_gen = varfun(@mean, res.matching_ge, ...
  'GroupingVariables', {'descriptor', 'geom_noise', 'category', 'norm_type'}, ...
  'InputVariables', 'mean_ap');

res.retrieval_en = varfun(@mean, res.retrieval_e, ...
  'GroupingVariables', {'descriptor', 'split', 'geom_noise', 'method', 'norm_type'}, ...
  'InputVariables', 'map');

%%

in_c = @(a, b) max(min(a.*b, 1), 0);

normname = 'none';
bnames = {}; cip = 1;  ls = {'EdgeColor', [1, 1, 1] * 0.2};
bnames{end+1} = struct('name', 'meanstd', 'printname', '\meanstd', 'texname', 'MStd', ...
  'color', in_c(utls.rgb('Silver'), cip), 'barargs', {ls}, ...
  'dim', 2, 'isbin', false, 'psize', 65, 'pps', 72479, 'gpu', false, 'normname', normname);
bnames{end+1} = struct('name', 'resize', 'printname', '\resize', 'texname', 'Resz', ...
  'color', in_c(utls.rgb('SlateGray'), cip), 'barargs', {ls}, ...
  'dim', 36, 'isbin', false, 'psize', 65, 'pps', 2583, 'gpu', false, 'normname', normname);

bnames{end+1} = struct('name', 'sift', 'printname', '\sift', 'texname', 'SIFT', ...
  'color', in_c(utls.rgb('SeaGreen'), cip), 'barargs', {ls}, ...
  'dim', 128, 'isbin', false, 'psize', 65, 'pps', 2251, 'gpu', false, 'normname', normname);
bnames{end+1} = struct('name', 'rootsift', 'printname', '\rootsift', 'texname', 'RSIFT', ...
  'color', in_c(utls.rgb('Olive'), cip), 'barargs', {ls}, ...
  'dim', 128, 'isbin', false, 'psize', 65, 'pps', 2157, 'gpu', false, 'normname', normname);

bnames{end+1} = struct('name', 'brief', 'printname', '\brief', 'texname', 'BRIEF', ...
  'color', in_c(utls.rgb('DarkCyan'), cip), 'barargs', {ls},...
  'dim', 256, 'isbin', true, 'psize', 32, 'pps', 1./3e-6, 'gpu', false, 'normname', normname);
bnames{end+1} = struct('name', 'binboost', 'printname', '\binboost', 'texname', 'BBoost', ...
  'color', in_c(utls.rgb('SteelBlue'), cip), 'barargs', {ls},...
  'dim', 256, 'isbin', true, 'psize', 32, 'pps', 1./700e-6, 'gpu', false, 'normname', normname);
bnames{end+1} = struct('name', 'orb', 'printname', '\orb', 'texname', 'ORB', ...
  'color', in_c(utls.rgb('SkyBlue'), cip), 'barargs', {ls}, ...
  'dim', 256, 'isbin', true, 'psize', 32, 'pps', 1./3e-6, 'gpu', false, 'normname', normname);

bnames{end+1} = struct('name', 'siam', 'printname', '\dcsiam', 'texname', 'DC-S', ...
  'color', in_c(utls.rgb('Salmon'), cip), 'barargs', {ls}, ...
  'dim', 256, 'isbin', false, 'psize', 64, 'pps', 1./100e-6, 'gpu', true, 'normname', normname);
bnames{end+1} = struct('name', 'siam2stream', 'printname', '\dcsiamts', 'texname', 'DC-S2S', ...
  'color', in_c(utls.rgb('LightSalmon'), cip), 'barargs', {ls},...
    'dim', 512, 'isbin', false, 'psize', 64, 'pps', 1./200e-6, 'gpu', true, 'normname', normname);
bnames{end+1} = struct('name', 'deepdesc', 'printname', '\deepdesc', 'texname', 'DDesc', ...
  'color', in_c(utls.rgb('BurlyWood'), cip), 'barargs', {ls}, ...
  'dim', 128, 'isbin', false, 'psize', 64, 'pps', 1./500e-6, 'gpu', true, 'normname', normname);
%bnames{end+1} = struct('name', 'tfeat-margin', 'printname', 'tfeat-margin');
bnames{end+1} = struct('name', 'tfeat-margin-star', 'printname', '\tfmargin', 'texname', 'TF-M', ...
  'color', in_c(utls.rgb('Sienna'), cip), 'barargs', {ls}, ...
  'dim', 128, 'isbin', false, 'psize', 32, 'pps', 1./10e-6, 'gpu', true, 'normname', normname);
%bnames{end+1} = struct('name', 'tfeat-ratio', 'printname', 'tf-ratio');
bnames{end+1} = struct('name', 'tfeat-ratio-star', 'printname', '\tfratio', 'texname', 'TF-R', ...
  'color', in_c(utls.rgb('Chocolate'), cip), 'barargs', {ls}, ...
  'dim', 128, 'isbin', false, 'psize', 32, 'pps', 1./10e-6, 'gpu', true, 'normname', normname);

bnames = cell2mat(bnames);

baselines = struct('name', {bnames.name}, 'printname', {bnames.printname}, ...
  'color', {bnames.color}, ...
  'bararg', {bnames.barargs}, 'normname', {bnames.normname});


baselines_chance = struct('name', 'chance', 'printname', '\chance', ...
  'color', in_c(utls.rgb('White'), cip), 'bararg', {ls});
clear normname;
bnames_pca = {}; cip = 1; ls = {'EdgeColor', [1, 1, 1] * 0, 'LineStyle', '--'};
%bnames_pca{end+1} = struct('name', 'meanstd_norm', 'printname', '+meanstd');
%bnames_pca{end+1} = struct('name', 'resize_norm', 'printname', '+resize');
bnames_pca{end+1} = struct('name', 'sift', 'printname', '\psift', 'texname', '+SIFT', ...
  'color', in_c(utls.rgb('SeaGreen'), cip), 'barargs', {ls}, 'normname', norms{'sift', 'normstr'}{1});
bnames_pca{end+1} = struct('name', 'rootsift', 'printname', '\prootsift', 'texname', '+RSIFT', ...
  'color', in_c(utls.rgb('Olive'), cip), 'barargs', {ls}, ...
  'normname', norms{'rootsift', 'normstr'}{1});

bnames_pca{end+1} = struct('name', 'siam', 'printname', '\pdcsiam', 'texname', '+DC-S', ...
  'color', in_c(utls.rgb('Salmon'), cip), 'barargs',{ls}, ...
  'normname', norms{'siam', 'normstr'}{1});
bnames_pca{end+1} = struct('name', 'siam2stream', ...
  'printname', '\pdcsiamts', 'texname', '+DC-S2S', ...
  'color', in_c(utls.rgb('LightSalmon'), cip), 'barargs', {ls}, ...
  'normname', norms{'siam2stream', 'normstr'}{1});
%bnames_pca{end+1} = struct('name', 'siam2stream_norm', 'printname', '+dc-siam2stream');
bnames_pca{end+1} = struct('name', 'deepdesc', ...
  'printname', '\pdeepdesc', 'texname', '+DDesc', ...
  'color', in_c(utls.rgb('BurlyWood'), cip), 'barargs', {ls}, ...
  'normname', norms{'deepdesc', 'normstr'}{1});
%bnames_pca{end+1} = struct('name', 'tfeat-margin_norm', 'printname', '+tf-margin');
bnames_pca{end+1} = struct('name', 'tfeat-margin-star', ...
  'printname', '\ptfmargin', 'texname', '+TF-M', ...
  'color', in_c(utls.rgb('Sienna'), cip), 'barargs', {ls}, ...
  'normname', norms{'tfeat-margin-star', 'normstr'}{1});
%bnames_pca{end+1} = struct('name', 'tfeat-ratio_norm', 'printname', '+tf-ratio');
bnames_pca{end+1} = struct('name', 'tfeat-ratio-star', 'printname', '\ptfratio', 'texname', '+TF-R', ...
  'color', in_c(utls.rgb('Chocolate'), cip), 'barargs', {ls}, ...
  'normname', norms{'tfeat-ratio-star', 'normstr'}{1});
bnames_pca = cell2mat(bnames_pca);

baselines_pca = struct('name', {bnames_pca.name}, 'printname', {bnames_pca.printname}, ...
  'color', {bnames_pca.color}, ...
  'bararg', {bnames_pca.barargs}, 'normname', {bnames_pca.normname});

det_sets = {};

%det_sets{end+1}.dets = baselines;
%det_sets{end}.detnames = {bnames.printname};
%det_sets{end}.name = 'baselines';

%det_sets{end+1}.dets = baselines_pca;
%det_sets{end}.detnames = {bnames_pca.printname};
%det_sets{end}.name = 'baselines-pca';

det_sets{end+1}.dets = [baselines, baselines_pca];
det_sets{end}.detnames = [{bnames.printname}, {bnames_pca.printname}];
det_sets{end}.name = 'baselines-all';

%det_sets{end+1}.dets = [baselines, baselines_pca, baselines_chance];
%det_sets{end}.detnames = [{bnames.printname}, {bnames_pca.printname}, {baselines_chance.printname}];
%det_sets{end}.name = 'baselines-all-chance';

det_sets = cell2mat(det_sets);

task_clrs = [0,136,55; 94,60,153; 202,0,32] ./ 256;
%cl_tasks = {'full_diffseq_easy', 'full_diffseq_hard', 'full_diffseq_tough', 'full_sameseq_easy', 'full_sameseq_hard', 'full_sameseq_tough'};
marker_opts = {'LineWidth', 0.5, 'MarkerSize', 3};
mop = 1; mfp = 0.4;
cl_tasks = {...
  struct('filter', struct('split', 'full', 'negs', 'inter', 'geom_noise', 'easy', 'method', 'imbalanced'), 'style', ...
    {{'Marker', '*', 'Color', in_c(task_clrs(1, :), mop), 'MarkerFaceColor', in_c(task_clrs(1, :), mfp), marker_opts{:}}}), ... % full_diffseq_easy
  struct('filter', struct('split', 'full', 'negs', 'inter', 'geom_noise', 'hard', 'method', 'imbalanced'), 'style', ...
    {{'Marker', '*', 'Color', in_c(task_clrs(2, :), mop), 'MarkerFaceColor', in_c(task_clrs(2, :), mfp), marker_opts{:}}}), ... % full_diffseq_hard
  struct('filter', struct('split', 'full', 'negs', 'inter', 'geom_noise', 'tough', 'method', 'imbalanced'), 'style', ...
    {{'Marker', '*', 'Color', in_c(task_clrs(3, :), mop), 'MarkerFaceColor', in_c(task_clrs(3, :), mfp), marker_opts{:}}}), ... % full_diffseq_tough
  struct('filter', struct('split', 'full', 'negs', 'intra', 'geom_noise', 'easy', 'method', 'imbalanced'), 'style', ...
    {{'Marker', 'd', 'Color', in_c(task_clrs(1, :), mop), 'MarkerFaceColor', in_c(task_clrs(1, :), mfp), marker_opts{:}, 'MarkerSize', 1.5}}), ... % full_sameseq_easy
  struct('filter', struct('split', 'full', 'negs', 'intra', 'geom_noise', 'hard', 'method', 'imbalanced'), 'style', ...
    {{'Marker', 'd', 'Color', in_c(task_clrs(2, :), mop), 'MarkerFaceColor', in_c(task_clrs(2, :), mfp), marker_opts{:}, 'MarkerSize', 1.5}}), ... % full_sameseq_hard
  struct('filter', struct('split', 'full', 'negs', 'intra', 'geom_noise', 'tough', 'method', 'imbalanced'), 'style', ...
    {{'Marker', 'd', 'Color', in_c(task_clrs(3, :), mop), 'MarkerFaceColor', in_c(task_clrs(3, :), mfp), marker_opts{:}, 'MarkerSize', 1.5}})  ... % full_sameseq_tough
};
cl_tasks = cell2mat(cl_tasks);
marker_opts = {'LineWidth', 0.5, 'MarkerSize', 4};

%m_tasks = {...
%  'full_easy_illum', 'full_easy_viewpoint', 'full_hard_illum', 'full_hard_viewpoint', ...
%  'full_tough_illum', 'full_tough_viewpoint'};
m_tasks = {...
  struct('filter', struct('geom_noise', 'easy', 'category', 'i'), 'style', ...
    {{'Marker', 'x', 'Color', in_c(task_clrs(1, :), mop), 'MarkerFaceColor', in_c(task_clrs(1, :), mfp), marker_opts{:}}}), ... % full_easy_illum
  struct('filter', struct('geom_noise', 'easy', 'category', 'v'), 'style', ...
    {{'Marker', '<', 'Color', in_c(task_clrs(1, :), mop), 'MarkerFaceColor', in_c(task_clrs(1, :), mfp), marker_opts{:}}}), ... % full_easy_viewpoint
  struct('filter', struct('geom_noise', 'hard', 'category', 'i'), 'style', ...
    {{'Marker', 'x', 'Color', in_c(task_clrs(2, :), mop), 'MarkerFaceColor', in_c(task_clrs(2, :), mfp), marker_opts{:}}}), ... % full_hard_illum
  struct('filter', struct('geom_noise', 'hard', 'category', 'v'), 'style', ...
    {{'Marker', '<', 'Color', in_c(task_clrs(2, :), mop), 'MarkerFaceColor', in_c(task_clrs(2, :), mfp), marker_opts{:}}}), ... % full_hard_viewpoint
  struct('filter', struct('geom_noise', 'tough', 'category', 'i'), 'style', ...
    {{'Marker', 'x', 'Color', in_c(task_clrs(3, :), mop), 'MarkerFaceColor', in_c(task_clrs(3, :), mfp), marker_opts{:}}}), ... % full_tough_illum
  struct('filter', struct('geom_noise', 'tough', 'category', 'v'), 'style', ...
    {{'Marker', '<', 'Color', in_c(task_clrs(3, :), mop), 'MarkerFaceColor', in_c(task_clrs(3, :), mfp), marker_opts{:}}})  ... % full_tough_viewpoint
  };
m_tasks = cell2mat(m_tasks);

marker_opts = {'LineWidth', 0.5, 'MarkerSize', 3};
%r_tasks = {'full_easy_5s', 'full_easy_40s', ...
%  'full_hard_5s', 'full_hard_40s', 'full_tough_5s', 'full_tough_40s'};
method = 'removequery';
%method = 'keepquery';

r_tasks = {...
%  struct('filter', struct('split', 'small', 'geom_noise', 'easy', 'method', method), 'style', ...
%    {{'Marker', '.', 'Color', in_c(task_clrs(1, :), mop), 'MarkerFaceColor', in_c(task_clrs(1, :), mfp), marker_opts{:}, 'MarkerSize', 4}}), ... % full_easy_5s
  struct('filter', struct('split', 'full', 'geom_noise', 'easy', 'method', method), 'style', ...
    {{'Marker', 'o', 'Color', in_c(task_clrs(1, :), mop), 'MarkerFaceColor', in_c(task_clrs(1, :), mfp), marker_opts{:}, 'MarkerSize', 3}}), ... % full_easy_40s
%  struct('filter', struct('split', 'small', 'geom_noise', 'hard', 'method', method), 'style', ...
%    {{'Marker', '.', 'Color', in_c(task_clrs(2, :), mop), 'MarkerFaceColor', in_c(task_clrs(2, :), mfp), marker_opts{:}, 'MarkerSize', 4}}), ... % full_hard_5s
  struct('filter', struct('split', 'full', 'geom_noise', 'hard', 'method', method), 'style', ...
    {{'Marker', 'o', 'Color', in_c(task_clrs(2, :), mop), 'MarkerFaceColor', in_c(task_clrs(2, :), mfp), marker_opts{:}, 'MarkerSize', 3}}), ... % full_hard_40s
%  struct('filter', struct('split', 'small', 'geom_noise', 'tough', 'method', method), 'style', ...
%    {{'Marker', '.', 'Color', in_c(task_clrs(3, :), mop), 'MarkerFaceColor', in_c(task_clrs(3, :), mfp), marker_opts{:}, 'MarkerSize', 4}}), ... % full_tough_5s
  struct('filter', struct('split', 'full', 'geom_noise', 'tough', 'method', method), 'style', ...
    {{'Marker', 'o', 'Color', in_c(task_clrs(3, :), mop), 'MarkerFaceColor', in_c(task_clrs(3, :), mfp), marker_opts{:}, 'MarkerSize', 3}})  ... % full_tough_40s
  };

r_tasks = cell2mat(r_tasks);
%% Export the Figures

% Verification
ps = 2;
for dsi = 1:numel(det_sets)
  figure(101); clf;
  detnames = det_sets(dsi).detnames;
  rproc.bar_plot(res.verification_en, 'mean_pr_ap', ...
    cl_tasks, det_sets(dsi).dets, 'detnames', detnames, ...
    'legend', false);
  xlabel('Patch Verification mAP [%]');
  %set(gcf, 'Position', get(0, 'Screensize')); % Maximize figure.
  drawnow;
  vl_printsize(ps);
  out_im_path = fullfile(outpath, sprintf('verif_%s.png', ...
      det_sets(dsi).name));
  %print('-dpng', out_im_path, '-r200');
  matlab2tikz(fullfile(outpath, sprintf('verif_%s.tikz', ...
      det_sets(dsi).name)), 'showInfo', false, ...
      'width', '\figW', 'height', '\figH', 'interpretTickLabelsAsTex', false);
end

%% Matching

for dsi = 1:numel(det_sets)
  figure(102); clf;
  detnames = det_sets(dsi).detnames;
  rproc.bar_plot(res.matching_gen, 'mean_mean_ap', ...
    m_tasks, det_sets(dsi).dets, 'detnames', detnames, ...
    'legend', false);
  xlabel('Image Matching mAP [%]');
  drawnow;
  vl_printsize(ps);
  out_im_path = fullfile(outpath, sprintf('matching_%s.png', ...
    det_sets(dsi).name));
  %print('-dpng', out_im_path, '-r200');
  matlab2tikz(fullfile(outpath, sprintf('matching_%s.tikz', ...
    det_sets(dsi).name)), 'showInfo', false, ...
    'width', '\figW', 'height', '\figH', 'interpretTickLabelsAsTex', false);
end

%% Retrieval
for dsi = 1:numel(det_sets)
  figure(103); clf; 
  detnames = det_sets(dsi).detnames;
  rproc.bar_plot(res.retrieval_en, 'mean_map', ...
    r_tasks, det_sets(dsi).dets, 'detnames', detnames, ...
    'legend', false);
  xlabel('Patch Retrieval mAP [%]');
  drawnow;
  vl_printsize(ps);
  out_im_path = fullfile(outpath, ...
    sprintf('retr_patch_%s.png', det_sets(dsi).name));
  %print('-dpng', out_im_path, '-r200');
  matlab2tikz(fullfile(outpath, sprintf('retr_patch_%s.tikz', ...
    det_sets(dsi).name)), 'showInfo', false, ...
    'width', '\figW', 'height', '\figH', 'interpretTickLabelsAsTex', false);
end

