|
| 1 | +function results_classification_agreement(ouput_folder, varargin) |
| 2 | + |
| 3 | + % Get special folder 'Documents' as char |
| 4 | + if ismac |
| 5 | + doc_path = char(java.lang.System.getProperty('user.home')); |
| 6 | + doc_path = fullfile(doc_path,'Documents'); |
| 7 | + else |
| 8 | + doc_path = char(getSpecialFolder('MyDocuments')); |
| 9 | + end |
| 10 | + |
| 11 | + % Get merged classifications |
| 12 | + if isempty(varargin) |
| 13 | + folder = uigetdir(doc_path,'Select one Merged Classification folder'); |
| 14 | + else |
| 15 | + if exist(varargin{1},'dir') |
| 16 | + folder = varargin{1}; |
| 17 | + else |
| 18 | + folder = uigetdir(doc_path,'Select one Merged Classification folder'); |
| 19 | + end |
| 20 | + end |
| 21 | + if isnumeric(folder) |
| 22 | + return; |
| 23 | + end |
| 24 | + files = dir(fullfile(folder,'*.mat')); |
| 25 | + if isempty(files) |
| 26 | + errordlg('No merged classifiers found.','Error'); |
| 27 | + return; |
| 28 | + end |
| 29 | + |
| 30 | + % Check if folder is correct |
| 31 | + try |
| 32 | + load(fullfile(folder,files(1).name)); |
| 33 | + catch |
| 34 | + errordlg('Cannot load merged classifier file','Error'); |
| 35 | + return; |
| 36 | + end |
| 37 | + if ~exist('classification_configs','var') |
| 38 | + errordlg('The specified folder does not contain classification_configs files','Error'); |
| 39 | + return; |
| 40 | + end |
| 41 | + |
| 42 | + % Sort files |
| 43 | + queue = zeros(length(files),1); |
| 44 | + for i = 1:length(files) |
| 45 | + tmp = strsplit(files(i).name,{'merged_','.mat'}); |
| 46 | + queue(i) = str2double(tmp{2}); |
| 47 | + end |
| 48 | + [~,idx] = sort(queue); |
| 49 | + files = files(idx); |
| 50 | + |
| 51 | + ids = zeros(1,length(classification_configs.ALL_TAGS)); |
| 52 | + for i = 1:length(ids) |
| 53 | + ids(i) = classification_configs.ALL_TAGS{i}{3}; |
| 54 | + end |
| 55 | + |
| 56 | + %% Compute confusion matrices and statistics |
| 57 | + h = waitbar(0,strcat('Computing agreements 1/',num2str(length(files))),'Name','Confusion Matrix'); |
| 58 | + cmatrix = 100*eye(length(files)); |
| 59 | + for iter = 1:length(files) |
| 60 | + %fprintf('Computing agreements. Iteration %d/%d...\n',iter,length(files)); |
| 61 | + |
| 62 | + load(fullfile(folder,files(1).name)); |
| 63 | + class_map_1 = classification_configs.CLASSIFICATION.class_map; |
| 64 | + segs = length(class_map_1); |
| 65 | + collect = {}; |
| 66 | + % find number of segs per class (including undefined) |
| 67 | + per_strat = zeros(1,length(ids)); |
| 68 | + for i = 1:length(ids) |
| 69 | + per_strat(i) = length(find(class_map_1==ids(i))); |
| 70 | + end |
| 71 | + |
| 72 | + % Each loop will find agreement of class_map_1 ---> class_map_x |
| 73 | + for i = 1:length(files) |
| 74 | + load(fullfile(folder,files(i).name)); |
| 75 | + class_map_x = classification_configs.CLASSIFICATION.class_map; |
| 76 | + %we have the same thus move on the next one |
| 77 | + if isequal(files(1).name,files(i).name) |
| 78 | + continue; |
| 79 | + end |
| 80 | + |
| 81 | + [confusion_matrix,order] = confusionmat(class_map_1,class_map_x,'order',ids); |
| 82 | + |
| 83 | + |
| 84 | + diagonal = diag(confusion_matrix); |
| 85 | + % overall agreement (numeric) |
| 86 | + overall_agreement_num = sum(diagonal); |
| 87 | + % overall agreement (percentage) |
| 88 | + overall_agreement_per = 100 * ( overall_agreement_num / segs ); |
| 89 | + % agreement per strategy (numeric) |
| 90 | + per_strat_num = zeros(length(order),1); |
| 91 | + for j = 1:length(order) |
| 92 | + per_strat_num(j) = diagonal(j); |
| 93 | + end |
| 94 | + % agreement per strategy (percentage) |
| 95 | + per_strat_per = zeros(length(order),1); |
| 96 | + for j = 1:length(order) |
| 97 | + per_strat_per(j) = 100 * ( per_strat_num(j) / per_strat(j) ); |
| 98 | + end |
| 99 | + % collect the results |
| 100 | + collect{i-1,1} = 'confusion matrix'; |
| 101 | + collect{i-1,2} = confusion_matrix; |
| 102 | + collect{i-1,3} = 'agreement [numeric, percentage]'; |
| 103 | + collect{i-1,4} = [overall_agreement_num, overall_agreement_per]; |
| 104 | + collect{i-1,5} = 'agreement per strategy (numeric)'; |
| 105 | + collect{i-1,6} = per_strat_num; |
| 106 | + collect{i-1,7} = 'agreement per strategy (percentage)'; |
| 107 | + collect{i-1,8} = per_strat_per; |
| 108 | + |
| 109 | + if i + iter -1 <= size(cmatrix,2) |
| 110 | + cmatrix(iter, i + iter -1) = overall_agreement_per; |
| 111 | + else |
| 112 | + % start from the beginning |
| 113 | + cmatrix(iter, i - size(cmatrix,2) +iter-1) = overall_agreement_per; |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + %% Export everything |
| 118 | + %fprintf('Saving & exporting results...\n'); |
| 119 | + |
| 120 | + number = strsplit(files(1).name,{'merged_','.mat'}); |
| 121 | + save(fullfile(ouput_folder,strcat('collect_',number{2},'.mat')),'collect'); |
| 122 | + |
| 123 | + %form the first column |
| 124 | + names = {}; |
| 125 | + for i = 1:length(files) |
| 126 | + fileName = strsplit(files(i).name,'.mat'); |
| 127 | + names = [names fileName{1}]; |
| 128 | + end |
| 129 | + tags = cell(length(classification_configs.ALL_TAGS),1); |
| 130 | + for i = 1:length(tags) |
| 131 | + tags{i} = classification_configs.ALL_TAGS{i}{2}; |
| 132 | + end |
| 133 | + col_names = {'Agreement';'Numeric';'Percentage';'Overall Numeric';'Overall Percentage'}; |
| 134 | + column = cell(length(tags)+1,1); |
| 135 | + for i = 1:length(tags) |
| 136 | + column{i+1} = tags{i}; |
| 137 | + end |
| 138 | + column = [column ; col_names]; |
| 139 | + %form the main table |
| 140 | + table_all = {}; |
| 141 | + for i = 1:size(collect,1) |
| 142 | + table = [tags' ; num2cell(collect{i,2}) ; tags' ; num2cell(collect{i,6})' ; num2cell(collect{i,8})']; |
| 143 | + table{end+2,1} = []; |
| 144 | + table{end-1,1} = strcat(num2str(collect{i,4}(1)),'/',num2str(segs)); |
| 145 | + table{end,1} = collect{i,4}(2); |
| 146 | + %put everything together |
| 147 | + table = [column,table]; |
| 148 | + table{1,1} = strcat(names{1},'--',names{i+1}); |
| 149 | + |
| 150 | + gap_line = cell(1,size(table,2)); |
| 151 | + table_all = [table_all ; table ; gap_line]; |
| 152 | + end |
| 153 | + table_all = table_all(1:end-1,:); %remove last empty row |
| 154 | + %write to CSV-file (.csv) |
| 155 | + header = ['Reference', tags']; |
| 156 | + subheader = [names{1}, num2cell(per_strat)]; |
| 157 | + table_all = [header ; subheader ; gap_line ; table_all]; |
| 158 | + table_all = cell2table(table_all); |
| 159 | + writetable(table_all,fullfile(ouput_folder,strcat('agreement_',number{2},'.csv')),'WriteVariableNames',0); |
| 160 | + |
| 161 | + %finally move the first element to the last place |
| 162 | + files(end+1) = files(1); |
| 163 | + files(1) = []; |
| 164 | + |
| 165 | + h = waitbar(iter/length(files),h,strcat('Computing agreements ',num2str(iter),'/',num2str(length(files)))); |
| 166 | + end |
| 167 | + |
| 168 | + waitbar(1,h,'Finalizing'); |
| 169 | + |
| 170 | + %export the confusion matrix |
| 171 | + column = {}; |
| 172 | + for i = 1:length(files) |
| 173 | + tmp = strsplit(files(i).name,'.mat'); |
| 174 | + column = [column; tmp{1}]; |
| 175 | + end |
| 176 | + header = ['Confusion Matrix',column']; |
| 177 | + table = [column,num2cell(cmatrix)]; |
| 178 | + table = [header;table]; |
| 179 | + table = cell2table(table); |
| 180 | + save(fullfile(ouput_folder,'confusion_matrix.mat'),'cmatrix'); |
| 181 | + writetable(table,fullfile(ouput_folder,'confusion_matrix.csv'),'WriteVariableNames',0); |
| 182 | + |
| 183 | + %export confusion matrix as image |
| 184 | + xylabels = {}; |
| 185 | + for i = 1:length(files) |
| 186 | + xylabels = [xylabels,strcat('c',num2str(i))]; |
| 187 | + end |
| 188 | + delete(h) |
| 189 | + f = imagesc_adv(cmatrix,'colorbar','on','colormap_range',[0 100],'XTickLabel',xylabels,'YTickLabel',xylabels); |
| 190 | + if isempty(f) |
| 191 | + errordlg('Cannot generate image. x-axis size can be up to 676.','Error'); |
| 192 | + return |
| 193 | + else |
| 194 | + [FontName, FontSize, LineWidth, Export, ExportStyle] = parse_configs; |
| 195 | + export_figure(f, ouput_folder, 'confusion_matix_icon', Export, ExportStyle); |
| 196 | + delete(f) |
| 197 | + end |
| 198 | +end |
| 199 | + |
0 commit comments