-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdemo.m
358 lines (337 loc) · 15.4 KB
/
demo.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
function demo(set,user_path,varargin)
%DEMO executes the processes: segmentation, labelling, classification
%and produces the results
%How to run standalone (everything, no UI, no display):
% demo(1,'C:\','WAITBAR',0,'DISPLAY',0,'SEGMENTATION',1,...
% 'CLASSIFICATION',1,'MCLASSIFICATION',1,'LABELLING_QUALITY',1,'STATISTICS',1,'PROBABILITIES',1);
if isempty(varargin)
initialization;
else
if ~isequal(varargin{1},'NO_INIT')
initialization;
end
end
% Functionalities
SEGMENTATION = 1;
CLASSIFICATION = 1;
MCLASSIFICATION = 1;
% Results
LABELLING_QUALITY = 0;
STATISTICS = 1;
PROBABILITIES = 1;
% Extra
WAITBAR = 0;
DISPLAY = 0;
for i = 1:length(varargin)
if isequal(varargin{i},'WAITBAR')
WAITBAR = varargin{i+1};
elseif isequal(varargin{i},'DISPLAY')
DISPLAY = varargin{i+1};
elseif isequal(varargin{i},'SEGMENTATION')
SEGMENTATION = varargin{i+1};
elseif isequal(varargin{i},'CLASSIFICATION')
CLASSIFICATION = varargin{i+1};
elseif isequal(varargin{i},'MCLASSIFICATION')
MCLASSIFICATION = varargin{i+1};
elseif isequal(varargin{i},'LABELLING_QUALITY')
LABELLING_QUALITY = varargin{i+1};
elseif isequal(varargin{i},'STATISTICS')
STATISTICS = varargin{i+1};
elseif isequal(varargin{i},'PROBABILITIES')
PROBABILITIES = varargin{i+1};
end
end
%Set Options
if set == 1
seg_overlap = [0.7,0.7,0.9,0.7];
seg_length = [300,250,250,200];
groups = [1,2];
elseif set == 2
return
end
%%
user_path = char_project_path(user_path);
if WAITBAR
h = waitbar(0,'Initializing...');
end
%% Create project folder tree (set_folder.m)
if set == 1
error = build_folder_tree(user_path, 'demo_original_set_1');
elseif set == 2
error = build_folder_tree(user_path, 'demo_original_set_2');
end
if error
if WAITBAR
delete(h);
end
return
end
if set == 1
project_path = fullfile(user_path,'demo_original_set_1');
elseif set == 2
project_path = fullfile(user_path,'demo_original_set_2');
end
if WAITBAR
waitbar(1/2);
end
%% Copy the settings
if isdeployed
if set == 1
datapath = fullfile(ctfroot,'import','original_data_1_settings');
elseif set == 2
datapath = fullfile(ctfroot,'import','original_data_2_settings');
end
else
if set == 1
datapath = fullfile(pwd,'import','original_data_1_settings');
elseif set == 2
datapath = fullfile(pwd,'import','original_data_2_settings');
end
end
files = dir(fullfile(datapath,'*.mat'));
for i = 1:length(files)
copyfile(fullfile(datapath,files(i).name),fullfile(project_path,'settings'));
end
ptags = fullfile(datapath,'tags.txt');
copyfile(ptags,fullfile(project_path,'settings'));
ptags = fullfile(datapath,'animal_groups.csv');
copyfile(ptags,fullfile(project_path,'settings'));
if WAITBAR
waitbar(2/2);
delete(h);
end
%% Load the settings
try
load(fullfile(project_path,'settings','new_properties.mat'));
load(fullfile(project_path,'settings','animal_groups.mat'));
load(fullfile(project_path,'settings','my_trajectories.mat'));
full_trajectory_features(project_path,'WAITBAR',WAITBAR,'DISPLAY',DISPLAY); % Trajectories
load(fullfile(project_path,'settings','my_trajectories_features.mat'));
catch
errordlg('Cannot load project settings','Error');
return
end
%% Segmentation
if SEGMENTATION
for i = 1:length(seg_overlap)
seg_properties = [seg_length(i),seg_overlap(i)];
segmentation_configs = config_segments(new_properties, seg_properties, trajectory_groups, my_trajectories, my_trajectories_features, '', 'WAITBAR',WAITBAR,'DISPLAY',DISPLAY);
save_segmentation(segmentation_configs, project_path);
end
end
%% Labelling
% Copy the labels csv files and generate the mat files
lfiles = dir(fullfile(datapath,'labels','*.csv'));
sfiles = dir(fullfile(project_path,'segmentation','*.mat'));
for i = 1:length(lfiles)
copyfile(fullfile(datapath,'labels',lfiles(i).name),fullfile(project_path,'labels'));
path_labels = fullfile(project_path,'labels',lfiles(i).name);
[~,len,ovl,~,~] = split_labels_name(path_labels);
for j = 1:length(sfiles)
path_segs = fullfile(project_path,'segmentation',sfiles(j).name);
[~,~,seg_length,seg_overlap] = split_segmentation_name(path_segs);
if isequal(seg_length,len) && isequal(seg_overlap,ovl)
load(path_segs);
generate_mat_from_labels(path_labels,segmentation_configs);
break;
end
end
end
%% Animal Trajectories Map
[~, animals_trajectories_map, animals_ids] = trajectories_map(my_trajectories,my_trajectories_features,groups,'Friedman',set);
%% Classification
if CLASSIFICATION
lfiles = dir(fullfile(project_path,'labels','*.mat'));
for i = 1:length(lfiles)
path_labels = fullfile(project_path,'labels',lfiles(i).name);
[~,len,ovl,~,~] = split_labels_name(path_labels);
for j = 1:length(sfiles)
path_segs = fullfile(project_path,'segmentation',sfiles(j).name);
[~,~,seg_length,seg_overlap] = split_segmentation_name(path_segs);
if isequal(seg_length,len) && isequal(seg_overlap,ovl)
if isequal(seg_length,'0') && isequal(seg_overlap,'0')
%Generate full trajectories results
[~,tmp1] = fileparts(path_segs);
[~,tmp2] = fileparts(path_labels);
results_strategies_distributions_manual_full(project_path,tmp1,tmp2,animals_trajectories_map,1);
break;
end
%read the cv file
cv_file = fullfile(datapath,'cv',strcat('cv_',seg_length,'_',seg_overlap,'.csv'));
data = read_cv_file(cv_file);
data = data(:,[1,2,5]);
cvErr = find(data(:,3) < 25);
num_clusters = data(cvErr,1);
error = execute_classification(project_path,sfiles(j).name,lfiles(i).name,num_clusters,varargin{:});
end
end
end
end
%% Mclassification
if MCLASSIFICATION
classifs = dir(fullfile(project_path,'classification'));
for i = 3:length(classifs)
%execute_Mclassification(project_path, {classifs(i).name}, sample, iterations, 0, 'CLUSTERS', Mclusters)
tmp = dir(fullfile(project_path,'classification',classifs(i).name,'*.mat'));
execute_Mclassification(project_path, {classifs(i).name}, length(tmp), 1, 0, varargin{:});
end
end
%% Results
% METRICS
str = num2str(groups);
str = regexprep(str,'[^\w'']',''); %remove gaps
str = strcat('group',str);
output_dir = fullfile(project_path,'results','metrics',str);
if ~exist(output_dir,'dir')
mkdir(output_dir);
end
try
results_latency_speed_length(new_properties,my_trajectories,my_trajectories_features,animals_trajectories_map,1,output_dir,'DISPLAY', DISPLAY);
catch
errordlg('Error: metrics generation','Error');
end
% STRATEGIES - TRANSITIONS - PROBABILITIES - STATISTICS (Ensemble)
if PROBABILITIES
b_pressed = {'Strategies','Transitions','Probabilities'};
else
b_pressed = {'Strategies','Transitions'};
end
segs = dir(fullfile(project_path,'segmentation','*.mat'));
mclasses = dir(fullfile(project_path,'Mclassification'));
for i = 1:length(segs)
seg = fullfile(project_path,'segmentation',segs(i).name);
[~,~,sl,so] = split_segmentation_name(seg);
if isequal(sl,'0') || isequal(so,'0')
continue;
end
for j = 3:length(mclasses)
mclass = fullfile(project_path,'Mclassification',mclasses(j).name);
[~,~,~,seg_length,seg_overlap,~,~,~] = split_mclassification_name(mclass);
if isequal(sl,seg_length) && isequal(so,seg_overlap)
load(fullfile(project_path,'segmentation',segs(i).name));
% Check the classification
[error,name,classifications] = check_classification(project_path,segmentation_configs,mclasses(j).name, 'WAITBAR',WAITBAR, 'DISPLAY', DISPLAY);
if error
errordlg('Classification check failed','Error');
return
end
% Generate the results
for b = 1:length(b_pressed)
error = generate_results(project_path, name, my_trajectories, segmentation_configs, classifications, animals_trajectories_map, animals_ids, b_pressed{b}, groups, 'WAITBAR',WAITBAR, 'DISPLAY', DISPLAY);
if error
errordlg('Cannot create results for strategies, transitions and probabilities','Error');
return
end
error = generate_results(project_path, name, my_trajectories, segmentation_configs, classifications, animals_trajectories_map, animals_ids, b_pressed{b}, groups, 'DISTRIBUTION',2, 'EXTRA_NAME','_nosmooth', 'WAITBAR',WAITBAR, 'DISPLAY', DISPLAY);
if error
errordlg('Cannot create results for strategies, transitions and probabilities (smooth)','Error');
return
end
end
% Statistics
if STATISTICS
[error,~,~] = class_statistics(project_path, mclasses(j).name, 'WAITBAR', WAITBAR);
if error
errordlg('Error: statistics generation','Error');
end
[error,~,~] = class_statistics(project_path, mclasses(j).name, 'SEGMENTATION', segmentation_configs, 'WAITBAR', WAITBAR, 'DISPLAY', DISPLAY);
if error
errordlg('Error: statistics generation (smooth)','Error');
end
end
break;
end
end
end
% STRATEGIES - TRANSITIONS - PROBABILITIES - STATISTICS (Classifiers)
if PROBABILITIES
b_pressed = {'Strategies','Transitions','Probabilities'};
else
b_pressed = {'Strategies','Transitions'};
end
segs = dir(fullfile(project_path,'segmentation','*.mat'));
cclasses = dir(fullfile(project_path,'classification'));
for i = 1:length(segs)
seg = fullfile(project_path,'segmentation',segs(i).name);
[~,~,sl,so] = split_segmentation_name(seg);
if isequal(sl,'0') || isequal(so,'0')
continue;
end
for j = 3:length(cclasses)
mclass = fullfile(project_path,'classification',cclasses(j).name);
[~,~,~,seg_length,seg_overlap,~,~,~] = split_mclassification_name(mclass);
if isequal(sl,seg_length) && isequal(so,seg_overlap)
load(fullfile(project_path,'segmentation',segs(i).name));
% Check the classification
[error,name,classifications] = check_classification(project_path,segmentation_configs,cclasses(j).name, 'WAITBAR',WAITBAR, 'DISPLAY', DISPLAY);
if error
errordlg('Classification check failed','Error');
return
end
% Generate the results
for b = 1:length(b_pressed)
error = generate_results(project_path, name, my_trajectories, segmentation_configs, classifications, animals_trajectories_map, animals_ids, b_pressed{b}, groups, 'WAITBAR',WAITBAR, 'DISPLAY', DISPLAY);
if error
errordlg('Cannot create results for strategies, transitions and probabilities','Error');
return
end
error = generate_results(project_path, name, my_trajectories, segmentation_configs, classifications, animals_trajectories_map, animals_ids, b_pressed{b}, groups, 'DISTRIBUTION',2, 'EXTRA_NAME','_nosmooth', 'WAITBAR',WAITBAR, 'DISPLAY', DISPLAY);
if error
errordlg('Cannot create results for strategies, transitions and probabilities (smooth)','Error');
return
end
end
% Statistics
if STATISTICS
[error,~,~] = class_statistics(project_path, cclasses(j).name, 'WAITBAR', WAITBAR);
if error
errordlg('Error: statistics generation','Error');
end
[error,~,~] = class_statistics(project_path, cclasses(j).name, 'SEGMENTATION', segmentation_configs, 'WAITBAR', WAITBAR, 'DISPLAY', DISPLAY);
if error
errordlg('Error: statistics generation (smooth)','Error');
end
end
break;
end
end
end
%% Labelling Quality
if LABELLING_QUALITY
labs = dir(fullfile(project_path,'labels','*.mat'));
segs = dir(fullfile(project_path,'segmentation','*.mat'));
for i = 1:length(labs)
[~,len,ovl,~,~] = split_labels_name(labs(i).name);
if isequal(len,'0') || isequal(ovl,'0')
continue
end
p = strsplit(labs(i).name,'.mat');
p = p{1};
output_path = char(fullfile(project_path,'labels',strcat(p,'_check')));
if ~exist(output_path,'dir')
mkdir(output_path);
end
for s = 1:length(segs)
seg = fullfile(project_path,'segmentation',segs(s).name);
[~,~,sl,so] = split_segmentation_name(seg);
if isequal(sl,len) && isequal(so,ovl)
load(fullfile(project_path,'segmentation',segs(s).name));
break;
end
end
mkdir(output_path);
[nc,res1bare,res2bare,res1,res2,res3,covering] = cross_validation(segmentation_configs,fullfile(project_path,'labels',labs(i).name),10,[10,100,10],output_path,'labels',0,'WAITBAR', WAITBAR, 'DISPLAY', DISPLAY);
[nc,per_errors1,per_undefined1,coverage,per_errors1_true] = algorithm_statistics(1,1,nc,res1bare,res2bare,res1,res2,res3,covering);
data = [nc', per_errors1', per_undefined1', coverage', per_errors1_true'];
% export results to CSV file
export_num_of_clusters(output_path,data);
output_path = char(fullfile(project_path,'results',strcat(p,'_cross_validation')));
if exist(output_path,'dir')
rmdir(output_path,'s');
end
export_num_of_clusters(output_path,data);
% generate graphs
results_clustering_parameters_graphs(output_path,nc,res1bare,res2bare,res1,res2,res3,covering);
end
end
end