directory = fullfile('nij_metrics','csv');
files = dir(fullfile(directory,'Exercise*.csv'));

consensus = containers.Map('KeyType','char','ValueType','any');

showwb = 1;

if showwb
    % Trick to find the longest file name
    % - 'files' is a struct, so convert it to a cell array
    % - apply a cell function 'size' to all names
    % - convert it to a matrix
    % - get the max
    cfiles = struct2cell(files);
    maxfnamelen = max(cell2mat(cellfun(@size, cfiles(1,:),'UniformOutput',false)));
    wbtitle = blanks(size(directory,2) + maxfnamelen + 1);
    wbtitle = strrep(wbtitle, ' ', 'A');
    wb = waitbar(0, wbtitle,'Name','Reading consensus maps ...');
    wbc = get(wb,'child');
    wbct = get(wbc, 'title');
    set(wbct, 'Interpreter', 'None');
else
    wb = [];
end

for i=1:size(files)
    
    if showwb
        waitbar((i-1) / size(files,1), wb, fullfile(directory,files(i).name));
    end
    
    if size(strfind(files(i).name, 'Consensus'),1) > 0
        [tokens, count] = sscanf(files(i).name, 'Exercise_%d_Image_%dx%d@%d_Consensus_Quality_%d.csv');
        if count ~= 5
            error(['Unrecognized Consensus map filename:',files(i).name]);
        end
        key = sprintf('ex_%d_q_%d', tokens(1), tokens(5));
        if consensus.isKey(key)
            error(['Unexpected second definition of consensus key ',key]);
        end
        consensus(key) = dlmread(fullfile(directory,files(i).name));
    end
end

if showwb
    close(wb);
    wb = waitbar(0, wbtitle,'Name','Calculating correlations ...');
    wbc = get(wb,'child');
    wbct = get(wbc, 'title');
    set(wbct, 'Interpreter', 'None');
end

correlations = containers.Map('KeyType','char','ValueType','any');

for i=1:size(files)
    
    if showwb
        waitbar((i-1) / size(files,1), wb, files(i).name);
    end
    
    if size(strfind(files(i).name, 'Consensus'),1) == 0
        [tokens, count] = sscanf(files(i).name, 'Exercise_%d_Image_%dx%d@%d_User_%d_Quality_%d.csv');
        if count ~= 6
            error(['Unrecognized User map filename:',files(i).name]);
        end
        key = sprintf('ex_%d_q_%d', tokens(1), tokens(6));
        if ~consensus.isKey(key)
            error(['Could not find consensus key ',key]);
        end
        data = dlmread(fullfile(directory,files(i).name));
        r = corr2(data, consensus(key));
        if ~correlations.isKey(key)
            correlations(key) = [];
        end
        correlations(key) = [correlations(key); tokens(5), r];
    end
end

if showwb
    close(wb);
end
