Skip to content

Commit c0950e4

Browse files
committed
update results + bugs fix
- Classification agreement + confusion matrix. - Metrics are generated inside a folder. - Fixed bugs
1 parent 9cf18c4 commit c0950e4

File tree

13 files changed

+477
-16
lines changed

13 files changed

+477
-16
lines changed

gui_browse/browse_create_labels_file.m

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,28 @@
33

44
error = 1;
55
% propose file name
6-
time = fix(clock);
7-
formatOut = 'yyyy-mmm-dd-HH-MM';
6+
%time = fix(clock);
7+
%formatOut = 'yyyy-mmm-dd-HH-MM';
8+
time = datetime('now');
9+
formatOut = 'dd-mmm-yyyy-HH-MM';
810
time = datestr((time),formatOut);
11+
idx = strfind(time,'-');
12+
time(idx(end-1)) = 'h';
13+
time(idx(end)) = 'm';
14+
time = regexprep(time,'[^\w'']','');
915
if ~isempty(varargin)
1016
path = char(varargin{2});
1117
file = strcat('labels_',varargin{1});
12-
pr = strcat('A file with name <',file,'> will be generated inside the project folder. Would you like to add a note to the name of the file?');
18+
pr = strcat('A file with name <',file,'> will be generated inside the project folder. Would you like to add a note to the name of the file? Allowed characters: A-Z, a-z, 0-9');
1319
%save note for user
1420
note = inputdlg(pr,'Input Note',1,{time});
1521
if isempty(note)
1622
error = 0;
1723
fpath = 0;
1824
return;
1925
end
26+
%remove spaces and special characters
27+
note{1} = regexprep(note{1},'[^\w'']','');
2028
if isempty(note{1})
2129
file = strcat(file,'.csv');
2230
else

gui_classification/gui_similarity.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ function export_table_Callback(hObject, eventdata, handles)
122122
time = fix(clock);
123123
formatOut = 'yyyy-mm-dd-HH-MM';
124124
time = datestr((time),formatOut);
125-
results_path = fullfile(char(results_path{1}),'results',strcat(p1,'-',p2,'@',time,'.csv'));
125+
if ~exist(fullfile(char(results_path{1}),'results','similarity'),'dir');
126+
mkdir(fullfile(char(results_path{1}),'results','similarity'));
127+
end
128+
results_path = fullfile(char(results_path{1}),'results','similarity',strcat(p1,'-',p2,'@',time,'.csv'));
126129
% create the file
127130
try
128131
fid = fopen(results_path,'w');

gui_main/pick_defaults.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
for i = 3:length(files)
2525
classifications = [classifications, files(i).name];
2626
end
27-
if isempty(classifications)
27+
if isempty(classifications) || length(files) == 2
2828
classifications = {''};
2929
end
3030
end

gui_project/assign_groups.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,10 @@ function save_callback(hObject, eventdata, def_path)
196196
if gpath==0
197197
return
198198
end
199-
fid = fopen(fullfile(path,file),'w');
200-
fclose(fid);
199+
%fid = fopen(fullfile(gpath,file),'w');
200+
%fclose(fid);
201201
% save to the new csv file the tmp_data
202-
writetable(Table,strcat(fullfile,file),'WriteVariableNames',1);
202+
writetable(Table,fullfile(gpath,file),'WriteVariableNames',1);
203203
end
204204

205205
function load_callback(hObject, eventdata, def_path)

results/class_statistics.m

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function [error, count,percentage_per_classifier] = class_statistics(ppath, class_name)
1+
function [error, count, percentage_per_classifier] = class_statistics(ppath, class_name)
22
%CLASS_STATISTICS computes statistics for the mclassification
33

44
h = waitbar(0,'Computing statistics...');
@@ -71,10 +71,13 @@
7171
percentage_per_classifier_ = [rows',percentage_per_classifier_];
7272
percentage_per_classifier_ = cell2table(percentage_per_classifier_);
7373
% Export the tables
74-
writetable(count_,fullfile(rpath,'numeric.csv'),'WriteVariableNames',0);
75-
writetable(percentage_per_classifier_,fullfile(rpath,'percentage.csv'),'WriteVariableNames',0);
74+
writetable(count_,fullfile(rpath,'statistics_numeric.csv'),'WriteVariableNames',0);
75+
writetable(percentage_per_classifier_,fullfile(rpath,'statistics_percentage.csv'),'WriteVariableNames',0);
7676
delete(h);
7777

78+
% Also create the confusion matrix
79+
results_classification_agreement(rpath,mcpath);
80+
7881
error = 0;
7982

8083
end

results/gui_generate_results.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@
3838

3939
% If metrics is pressed
4040
if isequal(b_pressed,'Metrics')
41-
output_dir = fullfile(project_path,'results');
41+
str = num2str(groups);
42+
str = regexprep(str,'[^\w'']',''); %remove gaps
43+
str = strcat('group',str);
44+
output_dir = fullfile(project_path,'results','metrics',str);
45+
if ~exist(output_dir,'dir')
46+
mkdir(output_dir);
47+
end
4248
try
4349
results_latency_speed_length(segmentation_configs,animals_trajectories_map,1,output_dir);
4450
error_all = 0;

results/one_group_metrics.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function one_group_metrics(animals_trajectories_map,vars,total_trials,days,trial
3030
groups = [groups, repmat(t*2 - 1 + g - 1, 1, length(tmp(:)))];
3131
end
3232

33-
box_plot_data(data, groups, output_dir, names{i});
33+
box_plot_data(data, groups, output_dir, strcat('animals_',names{i}));
3434

3535
f = figure;
3636
set(f,'Visible','off');
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
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+

results/results_latency_speed_length.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function results_latency_speed_length(segmentation_configs,animals_trajectories_map,figures,output_dir,varargin)
1+
function results_latency_speed_length(segmentation_configs,animals_trajectories_map,figures,output_dir)
22
% Comparison of full trajectory metrics for 2 groups of N animals over a
33
% set of M trials. The generated plots show:
44
% 1. The escape latency.
@@ -31,7 +31,7 @@ function results_latency_speed_length(segmentation_configs,animals_trajectories_
3131
ylabels = {'latency [s]', 'speed [cm/s]', 'path length [m]'};
3232
log_y = [0, 0, 0];
3333
% Generate text file for the p-values
34-
fn = fullfile(output_dir,'metrics_p.txt');
34+
fn = fullfile(output_dir,'animals_metrics_p.txt');
3535
fileID = fopen(fn,'wt');
3636

3737
for i = 1:size(vars, 1)
@@ -81,7 +81,7 @@ function results_latency_speed_length(segmentation_configs,animals_trajectories_
8181
end
8282

8383
% Export figures data
84-
box_plot_data(data, groups, output_dir, names{i});
84+
box_plot_data(data, groups, output_dir, strcat('animals_',names{i}));
8585

8686
% Generate figures
8787
if figures

0 commit comments

Comments
 (0)