-
Notifications
You must be signed in to change notification settings - Fork 2
/
populate_computed_tables.m
143 lines (130 loc) · 5.88 KB
/
populate_computed_tables.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
function [] = populate_computed_tables(skip_errors, parallel)
if nargin<2
parallel = false;
end
if nargin<1
skip_errors = false;
end
run_first = {'sln_results.SpikeDetectCC','sln_funcimage.Alignment'};
%schemas we want to check for computed tables
schemas = {'sln_results','sln_funcimage'};
%schemas = {'sln_results'};
current_time = datetime;
time_str = datestr(current_time,'yyyy-mm-dd_HH-MM-SS');
log_fname = sprintf('%scompute_log_%s.txt', ...
[getenv('SERVER_ROOT'), filesep, 'DJ_computed_logs', filesep],time_str);
C = dj.conn;
fid = fopen(log_fname,'w');
fprintf(fid,'Beginning computation. Logged in user is %s.\n\n',C.user);
%delete old error files
delete([getenv('SERVER_ROOT'), filesep, ...
'DJ_computed_logs', filesep, ...
'error_keys', filesep, ...
'*.mat']);
delete([getenv('SERVER_ROOT'), filesep, ...
'DJ_computed_logs', filesep, ...
'exceptions', filesep, ...
'*.mat']);
delete([getenv('SERVER_ROOT'), filesep, ...
'DJ_computed_logs', filesep, ...
'error_tables', filesep, ...
'*.xls']);
N_schemas = length(schemas);
for s=1:N_schemas
sc_name = schemas{s};
eval(sprintf('s=%s.getSchema;',sc_name))
class_names = s.classNames;
computed_ind = cellfun(@(c)any(ismember(superclasses(c),'dj.Computed')),class_names);
computed_classes = class_names(computed_ind)';
N_classes = length(computed_classes);
for i=1:length(run_first)
ind = find(strcmp(run_first{i},computed_classes));
if ~isempty(ind)
computed_classes = computed_classes([ind, setdiff(1:N_classes,ind)]);
end
end
fprintf(fid,'Schema %s:\n',sc_name);
fprintf(fid,'Computed classes (in order):\n');
for i=1:N_classes
fprintf(fid,'%s\n',computed_classes{i});
end
fprintf(fid,'\n');
for i=1:N_classes
fprintf(fid,'----------------------------------------.\n');
fprintf(fid,'Working on %s.\n',computed_classes{i});
eval(sprintf('obj=%s;',computed_classes{i}));
unpopulated = obj.keySource - obj;
unpop_count = unpopulated.count;
fprintf(fid,'Found %d unpopulated keys.\n',unpop_count);
restriction = '';
N_fail_keys = 0;
if skip_errors
fail_keys_name = [getenv('SERVER_ROOT'), filesep, ...
'DJ_computed_logs', filesep, ...
'error_keys', filesep, ...
computed_classes{i}, '.mat'];
fail_keys = [];
if isfile(fail_keys_name)
load(fail_keys_name);
end
if ~isempty(fail_keys)
N_fail_keys = length(fail_keys);
fprintf(fid,'Skipping %d error entries.\n',N_fail_keys);
restriction = ',obj.keySource-fail_keys';
end
end
if unpop_count > 0
if parallel
fprintf(fid,'Running parpopulate so no error tracking.\n');
eval(sprintf('parpopulate(%s%s);',computed_classes{i},restriction));
%error tracking does not work with parpopulate
%but it does not stop on exceptions
else
tstart = tic;
eval(sprintf('[fail_keys, errs] = populate(%s%s);',computed_classes{i},restriction));
elapsed = toc(tstart);
N_err = length(fail_keys);
if N_err > 0
save([getenv('SERVER_ROOT'), filesep, ...
'DJ_computed_logs', filesep, ...
'error_keys', filesep, ...
computed_classes{i}, '.mat'], 'fail_keys');
save([getenv('SERVER_ROOT'), filesep, ...
'DJ_computed_logs', filesep, ...
'exceptions', filesep, ...
computed_classes{i}, '.mat'], 'errs');
error_table = table('Size',[N_err,4],...
'VariableTypes',{'string','string','uint32','string'}, ...
'VariableNames',{'cell_name','dataset','DJID','experimenter'});
for f=1:length(fail_keys)
error_exp = sln_symphony.ExperimentSource * ...
proj(sln_symphony.ExperimentRetina,'source_id->retina_id','*') * ...
sln_symphony.ExperimentCell & fail_keys(f);
if error_exp.exists
sln_symphony.Experiment * sln_symphony.ExperimentSource * sln_symphony.ExperimentRetina & rmfield(fail_keys(f),'source_id');
error_table.DJID(f) = fetch1(error_exp,'animal_id');
error_table.experimenter(f) = fetch1(error_exp,'experimenter');
end
if isfield(fail_keys(f),'dataset_name')
error_table.dataset(f) = fail_keys(f).dataset_name;
elseif isfield(fail_keys(f),'epoch_id')
error_table.dataset(f) = sprintf('epoch_%d',fail_keys(f).epoch_id);
end
error_table.cell_name(f) = fetch1(sln_cell.CellName & fail_keys(f),'cell_name');
end
error_table_dir = [getenv('SERVER_ROOT'), filesep, ...
'DJ_computed_logs', filesep, ...
'error_tables', filesep];
error_table_name = sprintf('%s%s.xls',error_table_dir,strrep(computed_classes{i},'.','_'));
writetable(error_table,error_table_name);
end
fprintf(fid,'%d entries added. %d errors logged.\n',unpop_count-N_err-N_fail_keys,N_err);
fprintf(fid,'Elapsed time = %f seconds (%f seconds per entry).\n',...
elapsed,elapsed./unpop_count);
end
fprintf(fid,'\n');
end
end
fprintf(fid,'\n');
end
fclose(fid);