
%% Latex Full-Set 
%
% Generates Latex code for presentation of error tables produced by 
% fullset_validation.m.
%
% ex. latex_fullset('./error_analysis', 'rmse', 'fullset_xb3-38mm-left', './latex/', {'std','blind','alvarez'})
%

%% Input Arguments

% errPath - Parent directory of error table .mat files
%           type: String

% errType - Type of error metric
%           type: String {ex. 'rmse'}

% fullsetName - Name of fullset.
%               type: String

% latexPath - Parent directory of latex code 
%             type: String

% methodNames - Name of lens distortion correction methods (tags)
%               type: Nx1 cell

% scaleFactor - Scale factor of cross-validation error table.
%               type: positive integer


%% Output Arguments

% N/A
%

%% Latex Full-set

function latex_fullset(errPath, errType, fullsetName, latexPath, methodNames, scaleFactor)

%check input parameters
assert(ischar(errPath));
assert(ischar(errType));
assert(ischar(fullsetName));
assert(ischar(latexPath));
assert(isa(methodNames, 'cell'));

%formatting constants/variables
cellSize = '\tableWidth';
precision = 4;
lowerFormatStr  = sprintf('\\\\textbf{%%1.%df}', precision);
higherFormatStr = sprintf('%%1.%df', precision);

% Find all supported files within directory
fprintf('\tSearching directory %s...\n', errPath);
suppFiles = {'mat'};
[fileNames, nFiles] = find_supported_files(errPath, suppFiles);
fprintf('\t\tFound %d line-point .mat files\n', nFiles);

% Select files 
errFileNames = select_files(fileNames, '^rmse_[a-zA-Z]+_fullset_(?<group>[a-zA-Z0-9-]+)', 'Fullset Error Analysis');
nFiles = length(errFileNames);

%create output file
fid = fopen(fullfile(latexPath, strcat(fullsetName,'.txt')),'w');

nMethods = length(methodNames);
errTables = cell(nMethods, 1);
nImgs = NaN;

if nFiles ~= nMethods
    error('LatexFullset:InvalidNumFiles', 'Number of chosen files should equal number of different methods tested');
end
    
for i = 1:nFiles,
       
   comps = strsplit(errFileNames{i}, '_', 'DelimiterType', 'RegularExpression');
    
   if strcmp(comps{1}, errType) && any(strcmp(comps{2}, methodNames))                      
        
       loadErrFileName = errFileNames{i};
       load(fullfile(errPath, loadErrFileName), 'errTable'); 
       m = find(strcmp(comps{2}, methodNames));      
       errTables{m} = errTable;
        
   end 
          
   if ~isnan(nImgs) && nImgs ~= size(errTable, 2)       
       error('LatexFullSet:InconsistentErrTable','Inconsistent Error Tables');
   else
       nImgs = size(errTable,2);
   end
       
end

minIndicator = false(nMethods, nImgs);
for i = 1:nImgs,
    
    minVal = NaN;
    for m = 1:nMethods,
                
        if isnan(minVal) || minVal > errTables{m}(i) 
            minModel = m;
            minVal = errTables{m}(i); 
        end
        
    end
    
    minIndicator(minModel,i) = true;
    
end

aveErrTable = nan(nMethods, 1);
for m = 1:nMethods,
    aveErrTable(m) = mean(errTables{m});
end
[~, minAveModel] = min(aveErrTable);

fprintf(fid, '=========== Notes ===========\n\n');

fprintf(fid, 'Scale Factor: %f\n\n', scaleFactor);

fprintf(fid, '=========== Fullset ===========\n\n');

for m = 1:nMethods,
    
    method  = methodNames{m};
    fprintf(fid,'%s - HI:\\RN{%d:%d} \t\t & ', method, 1, nImgs);
    
    for i = 1:nImgs, 
        
        if minIndicator(m,i)
            fprintf(fid, lowerFormatStr, scaleFactor*errTables{m}(i));
        else
            fprintf(fid, higherFormatStr, scaleFactor*errTables{m}(i));
        end
    
        fprintf(fid, ' & ');    
        
    end    
    
    if m == minAveModel,        
        fprintf(fid, lowerFormatStr, scaleFactor*aveErrTable(m));        
    else        
        fprintf(fid, higherFormatStr, scaleFactor*aveErrTable(m));        
    end
        
    fprintf(fid,'\\\\ \\hline\n');
    
end

fclose(fid);

end



