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

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


for i=1:size(files)
    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 ~given.isKey(key)
            given(key) = dlmread(fullfile(directory,files(i).name));
        end
    else
        [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 ~calculated.isKey(key)
            calculated(key) = zeros(tokens(3),tokens(2));
        end
        data = dlmread(fullfile(directory,files(i).name));
        calculated(key) = calculated(key) + data;
    end
end

if ~isempty(setdiff(given.keys, calculated.keys))
    error('Keys differ between given and calculated');
end

keys = given.keys;
for i=1:length(keys)
    k = keys(i);
    if nnz(calculated('ex_67_q_1') ~= given('ex_67_q_1')) ~= 0
        error(['For key',key,',difference between Matlab and Python calculations']);
    end
end

disp('Everything is OK');
