forked from petersenpeter/CellExplorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadCellMetricsBatch.m
350 lines (316 loc) · 20 KB
/
loadCellMetricsBatch.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
function cell_metrics_batch = loadCellMetricsBatch(varargin)
% Load metrics across sessions and concats the metrics into a single struct
% with the appropriate format for each field.
%
% INPUTS:
% varargin: Described below
%
% OUTPUT:
% cell_metrics_batch. Combined cell metrics batch file from selected sessions that can be opened with CellExplorer
% By Peter Petersen
% Last edited: 18-07-2021
% - Example calls:
% cell_metrics = loadCellMetricsBatch('basepaths',{'basepath1','basepath2'},'basenames',{'basename1','basename2'});
% cell_metrics = loadCellMetricsBatch('sessions',{'rec1','rec2'}) % Load batch from database
% cell_metrics = loadCellMetricsBatch('sessionIDs',[10985,10985]) % Load session from database session id
% % % % % % % % % % % % % % % % % % % % % % % % % % % %
% Handling inputs
% % % % % % % % % % % % % % % % % % % % % % % % % % % %
p = inputParser;
addParameter(p,'sessionIDs',{},@isnumeric); % numeric IDs for the sessions to load
addParameter(p,'sessions',{},@iscell); % sessionNames for the sessions to load
addParameter(p,'basepaths',{},@iscell); % basepaths for the sessions to load
addParameter(p,'basenames',{},@iscell); % basenames for the sessions to load
addParameter(p,'saveAs','cell_metrics',@isstr); % saveAs - name of .mat file
addParameter(p,'waitbar_handle',[],@ishandle); % waitbar handle
parse(p,varargin{:})
sessionNames = p.Results.sessions;
sessionIDs = p.Results.sessionIDs;
basepaths = p.Results.basepaths;
basenames = p.Results.basenames;
saveAs = p.Results.saveAs;
waitbar_handle = p.Results.waitbar_handle;
db_settings = db_load_settings;
cell_metrics2 = [];
subfields2 = [];
subfieldstypes = [];
subfieldssizes = [];
batch_timer = tic;
if ishandle(waitbar_handle)
ce_waitbar = waitbar_handle;
else
ce_waitbar = waitbar(0,' ','name','Cell-metrics: loading batch');
end
cell_metrics_type_struct = {'general','putativeConnections','groups','tags','groundTruthClassification','acg','isi','waveforms','firingRateMaps','responseCurves','events','manipulations','spikes'};
% disp('Cell-metrics: $')
if ~isempty(sessionNames)
count_metricsLoad = 1;
waitbar(1/(1+count_metricsLoad+length(sessionNames)),ce_waitbar,['Loading session info from sessionNames']);
% % % % % % % % % % % % %
options = weboptions('Username',db_settings.credentials.username,'Password',db_settings.credentials.password,'RequestMethod','get','Timeout',50);
options.CertificateFilename=('');
% Requesting db list
bz_db = webread([db_settings.address,'views/15356/'],options,'page_size','5000','sorted','1','cellmetrics',1);
sessions = loadjson(bz_db.renderedHtml);
% Setting paths from db struct
db_basename = {};
db_basepath = {};
db_basename = cellfun(@(x) x.name,sessions,'UniformOutput',false);
[~,index,~] = intersect(db_basename,sessionNames);
for i_db = 1:length(sessions)
if strcmp(sessions{i_db}.repositories{1},'NYUshare_Datasets')
Investigator_name = strsplit(sessions{i_db}.investigator,' ');
path_Investigator = [Investigator_name{2},Investigator_name{1}(1)];
db_basepath{i_db} = fullfile(db_settings.repositories.(sessions{i_db}.repositories{1}), path_Investigator,sessions{i_db}.animal, sessions{i_db}.name);
else
db_basepath{i_db} = fullfile(db_settings.repositories.(sessions{i_db}.repositories{1}), sessions{i_db}.animal, sessions{i_db}.name);
end
end
basepaths = db_basepath(index);
basenames = db_basename(index);
elseif ~isempty(sessionIDs)
count_metricsLoad = 1;
waitbar(1/(1+count_metricsLoad+length(sessionIDs)),ce_waitbar,['Loading session info from sessionIDs']);
[sessions, basenames, basepaths] = db_set_session('sessionId',sessionIDs,'changeDir',false);
elseif ~isempty(basepaths)
count_metricsLoad = 1;
for i = 1:length(basepaths)
basepath = basepaths{i};
if isempty(basenames) || length(basenames) < i
[~,basename,~] = fileparts(basepath);
basenames{i} = basename;
else
basename = basenames{i};
end
end
else
warning('Input not sufficient')
end
batch_benchmark.clock(1) = toc(batch_timer);
% % % % % % % % % % % % % % % % % % % % % % % % % % % %
% Loading cell_metircs file batch
% % % % % % % % % % % % % % % % % % % % % % % % % % % %
for iii = 1:length(basepaths)
file_load_timer = tic;
if ~isempty(basenames) && ishandle(ce_waitbar)
waitbar((iii+count_metricsLoad)/(1+count_metricsLoad+length(basepaths)),ce_waitbar,[num2str(iii), '/', num2str(length(basenames)),': ', basenames{iii}]);
else
break
end
if exist(fullfile(basepaths{iii},[basenames{iii},'.',saveAs,'.cellinfo.mat']))
cell_metrics2{iii} = load(fullfile(basepaths{iii},[basenames{iii},'.',saveAs,'.cellinfo.mat']));
else
warning(['session not found: ', fullfile(basepaths{iii},[basenames{iii},'.',saveAs,'.cellinfo.mat'])])
cell_metrics_batch = [];
return
end
% Generating session level metrics
if isfield(cell_metrics2{iii}.cell_metrics.general,'session')
sessionMetrics = fieldnames(cell_metrics2{iii}.cell_metrics.general.session);
for i = 1:numel(sessionMetrics)
cell_metrics2{iii}.cell_metrics.(['session_',sessionMetrics{i}]) = repmat({cell_metrics2{iii}.cell_metrics.general.session.(sessionMetrics{i})},1,cell_metrics2{iii}.cell_metrics.general.cellCount);
end
end
% Generating animal level metrics
if isfield(cell_metrics2{iii}.cell_metrics.general,'animal')
sessionMetrics = fieldnames(cell_metrics2{iii}.cell_metrics.general.animal);
for i = 1:numel(sessionMetrics)
cell_metrics2{iii}.cell_metrics.(['animal_',sessionMetrics{i}]) = repmat({cell_metrics2{iii}.cell_metrics.general.animal.(sessionMetrics{i})},1,cell_metrics2{iii}.cell_metrics.general.cellCount);
end
end
subfields2 = [subfields2(:);fieldnames(cell_metrics2{iii}.cell_metrics)];
temp = struct2cell(structfun(@class,cell_metrics2{iii}.cell_metrics,'UniformOutput',false));
subfieldstypes = [subfieldstypes(:);temp(:)];
temp2 = struct2cell(structfun(@size,cell_metrics2{iii}.cell_metrics,'UniformOutput',false));
subfieldssizes = [subfieldssizes(:);temp2(:)];
batch_benchmark.file_load(iii) = toc(file_load_timer);
batch_benchmark.file_cell_count(iii) = cell_metrics2{iii}.cell_metrics.general.cellCount;
end
[cell_metrics_fieldnames,ia,~] = unique(subfields2);
subfieldstypes = subfieldstypes(ia);
subfieldssizes = subfieldssizes(ia);
subfieldstypes(contains(cell_metrics_fieldnames,{'truePositive','falsePositive','batchIDs'})) = [];
subfieldssizes(contains(cell_metrics_fieldnames,{'truePositive','falsePositive','batchIDs'})) = [];
cell_metrics_fieldnames(contains(cell_metrics_fieldnames,{'truePositive','falsePositive','batchIDs'})) = [];
subfieldstypes(ismember(cell_metrics_fieldnames,cell_metrics_type_struct)) = {'struct'};
h = 0;
cell_metrics_batch = [];
batch_benchmark.clock(2) = toc(batch_timer);
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% Creating cell_metrics_batch from individual session cell_metrics
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
if ishandle(ce_waitbar)
waitbar((count_metricsLoad+length(cell_metrics2))/(1+count_metricsLoad+length(cell_metrics2)),ce_waitbar,['Initializing cell metrics batch']);
end
cell_metrics_batch.putativeConnections.excitatory = [];
cell_metrics_batch.putativeConnections.inhibitory = [];
for ii = 1:length(cell_metrics_fieldnames)
if strcmp(subfieldstypes{ii},'cell')
cell_metrics_batch.(cell_metrics_fieldnames{ii}) = cell(1,sum(batch_benchmark.file_cell_count));
elseif strcmp(subfieldstypes{ii},'double')
cell_metrics_batch.(cell_metrics_fieldnames{ii}) = nan(1,sum(batch_benchmark.file_cell_count));
elseif strcmp(subfieldstypes{ii},'struct')
end
end
if ishandle(ce_waitbar)
waitbar((count_metricsLoad+length(cell_metrics2))/(1+count_metricsLoad+length(cell_metrics2)),ce_waitbar,['Concatenating files']);
end
for iii = 1:length(cell_metrics2)
if ishandle(ce_waitbar)
waitbar((count_metricsLoad+length(cell_metrics2))/(1+count_metricsLoad+length(cell_metrics2)),ce_waitbar,['Concatenating files: ', basenames{iii} ,' (',num2str(iii),'/' num2str(length(cell_metrics2)),')']);
else
break
end
cell_metrics = cell_metrics2{iii}.cell_metrics;
hh = size(cell_metrics.cellID,2);
cell_metrics = validateGroupFormat(cell_metrics,'tags');
cell_metrics = validateGroupFormat(cell_metrics,'groundTruthClassification');
if length(cell_metrics2) > 1 && iii == 1
% cell_metrics_batch = cell_metrics;
% cell_metrics_batch = rmfield(cell_metrics_batch,'general');
cell_metrics_batch.general.basename = 'Batch of sessions';
end
cell_metrics_batch.batchIDs(h+1:hh+h) = iii*ones(1,hh);
cell_metrics_batch.general.batch{iii} = cell_metrics.general;
cell_metrics_batch.general.basepaths{iii} = basepaths{iii};
cell_metrics_batch.general.basenames{iii} = cell_metrics.general.basename;
cell_metrics_batch.general.saveAs{iii} = saveAs;
for ii = 1:length(cell_metrics_fieldnames)
% Struct field
if strcmp(subfieldstypes{ii},'struct') && ~strcmp(cell_metrics_fieldnames{ii},'general')
% If putative connections field (special)
if strcmp(cell_metrics_fieldnames{ii},'putativeConnections')
if isfield(cell_metrics,'putativeConnections') && isfield(cell_metrics.putativeConnections,'excitatory') && isfield(cell_metrics_batch,'putativeConnections') && isfield(cell_metrics,'putativeConnections')
cell_metrics_batch.putativeConnections.excitatory = [cell_metrics_batch.putativeConnections.excitatory; cell_metrics.putativeConnections.excitatory+h];
end
if isfield(cell_metrics,'putativeConnections') && isfield(cell_metrics.putativeConnections,'inhibitory') && isfield(cell_metrics_batch,'putativeConnections') && isfield(cell_metrics,'putativeConnections')
cell_metrics_batch.putativeConnections.inhibitory = [cell_metrics_batch.putativeConnections.inhibitory; cell_metrics.putativeConnections.inhibitory+h];
end
elseif ismember(cell_metrics_fieldnames{ii},{'groups','tags','groundTruthClassification'})
if isfield(cell_metrics,cell_metrics_fieldnames{ii})
fields1 = fieldnames(cell_metrics.(cell_metrics_fieldnames{ii}));
for k = 1:numel(fields1)
if isfield(cell_metrics_batch,cell_metrics_fieldnames{ii}) && isfield(cell_metrics_batch.(cell_metrics_fieldnames{ii}),fields1{k})
cell_metrics_batch.(cell_metrics_fieldnames{ii}).(fields1{k}) = [cell_metrics_batch.(cell_metrics_fieldnames{ii}).(fields1{k}), cell_metrics.(cell_metrics_fieldnames{ii}).(fields1{k})+h];
else
cell_metrics_batch.(cell_metrics_fieldnames{ii}).(fields1{k}) = cell_metrics.(cell_metrics_fieldnames{ii}).(fields1{k})+h;
end
end
end
else
if isfield(cell_metrics,cell_metrics_fieldnames{ii})
structFields = fieldnames(cell_metrics.(cell_metrics_fieldnames{ii}));
structFieldsType = struct2cell(structfun(@class,cell_metrics.(cell_metrics_fieldnames{ii}),'UniformOutput',false));
structFieldsSize = struct2cell(structfun(@size,cell_metrics.(cell_metrics_fieldnames{ii}),'UniformOutput',false));
for k = 1:length(structFields)
if strcmp(structFieldsType{k},'cell')
if ~isempty(cell_metrics.(cell_metrics_fieldnames{ii}).(structFields{k})) && length(size(cell_metrics.(cell_metrics_fieldnames{ii}).(structFields{k})))<3 & size(cell_metrics.(cell_metrics_fieldnames{ii}).(structFields{k}),1)==1 & size(cell_metrics.(cell_metrics_fieldnames{ii}).(structFields{k}),2)== hh
cell_metrics_batch.(cell_metrics_fieldnames{ii}).(structFields{k})(h+1:hh+h) = cell_metrics.(cell_metrics_fieldnames{ii}).(structFields{k});
else
cell_metrics_batch.(cell_metrics_fieldnames{ii}).(structFields{k}){iii} = cell_metrics.(cell_metrics_fieldnames{ii}).(structFields{k}){1};
end
elseif strcmp(structFieldsType{k},'double')
% % % % % % % % % % % % % % % % % % % % % % % %
% If field does not exist
if ~isfield(cell_metrics.(cell_metrics_fieldnames{ii}),structFields{k})
if length(structFieldsSize{k})==2 && structFieldsSize{k}(1) > 0
cell_metrics_batch.(cell_metrics_fieldnames{ii}).(structFields{k})(:,h+1:hh+h) = nan(structFieldsSize{k}(1:end-1),hh);
else
cell_metrics_batch.(cell_metrics_fieldnames{ii}).(structFields{k})(h+1:hh+h) = nan(1,hh);
end
% If field exist
else
if isempty(cell_metrics.(cell_metrics_fieldnames{ii}).(structFields{k})) && length(structFieldsSize{k})==2 && structFieldsSize{k}(1) > 0%% && ~any(strcmp(cell_metrics_fieldnames{ii}, {'firing_rate_map','firing_rate_map_states'}))
cell_metrics_batch.(cell_metrics_fieldnames{ii}).(structFields{k})(:,h+1:hh+h) = nan(structFieldsSize{k}(1:end-1),hh);
elseif isempty(cell_metrics.(cell_metrics_fieldnames{ii})) && length(structFieldsSize{k})==1
cell_metrics_batch.(cell_metrics_fieldnames{ii}).(structFields{k})(h+1:hh+h) = nan(1,hh);
else
if length(structFieldsSize{k})==3
for iiii=1:size(cell_metrics.(cell_metrics_fieldnames{ii}).(structFields{k}),3)
end
elseif structFieldsSize{k}(1)>1 %&& ~any(strcmp(cell_metrics_fieldnames{ii}, {'firing_rate_map','firing_rate_map_states'}))
if ~isfield(cell_metrics_batch,cell_metrics_fieldnames{ii}) || ~isfield(cell_metrics_batch.(cell_metrics_fieldnames{ii}),structFields{k}) || (isfield(cell_metrics_batch.(cell_metrics_fieldnames{ii}),structFields{k}) && size(cell_metrics_batch.(cell_metrics_fieldnames{ii}).(structFields{k}),1) == size(cell_metrics.(cell_metrics_fieldnames{ii}).(structFields{k}),1))
cell_metrics_batch.(cell_metrics_fieldnames{ii}).(structFields{k})(:,h+1:hh+h) = cell_metrics.(cell_metrics_fieldnames{ii}).(structFields{k});
end
% elseif structFieldsSize{k}(1)>1 %&& any(strcmp(cell_metrics_fieldnames{ii}, {'firing_rate_map','firing_rate_map_states'}))
% cell_metrics_batch.(cell_metrics_fieldnames{ii}).(structFields{k}){iii} = cell_metrics.(cell_metrics_fieldnames{ii}).(structFields{k});
else
if ~isempty(cell_metrics.(cell_metrics_fieldnames{ii}).(structFields{k}))
if size(cell_metrics.(cell_metrics_fieldnames{ii}).(structFields{k}),2) == hh && size(cell_metrics.(cell_metrics_fieldnames{ii}).(structFields{k}),1) == 1
cell_metrics_batch.(cell_metrics_fieldnames{ii}).(structFields{k})(h+1:hh+h) = cell_metrics.(cell_metrics_fieldnames{ii}).(structFields{k});
else
cell_metrics_batch.(cell_metrics_fieldnames{ii}).(structFields{k})(h+1:hh+h) = nan(1,hh);
end
end
end
end
end
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
end
end
end
end
% Cell field
elseif strcmp(subfieldstypes{ii},'cell')
% If field does not exist
if ~isfield(cell_metrics,cell_metrics_fieldnames{ii})
if strcmp(subfieldstypes{ii},'cell') && length(subfieldssizes{ii}) < 3
cell_metrics_batch.(cell_metrics_fieldnames{ii})(h+1:hh+h) = repmat({''},1,size(cell_metrics.cellID,2));
elseif strcmp(subfieldstypes{ii},'cell') && length(subfieldssizes{ii}) == 3
cell_metrics_batch.(cell_metrics_fieldnames{ii}){iii} = {};
end
% If field exist
else
if ~isempty(cell_metrics.(cell_metrics_fieldnames{ii})) && length(size(cell_metrics.(cell_metrics_fieldnames{ii})))<3 & size(cell_metrics.(cell_metrics_fieldnames{ii}),1)==1 && size(cell_metrics.(cell_metrics_fieldnames{ii}),2)== hh
cell_metrics_batch.(cell_metrics_fieldnames{ii})(h+1:hh+h) = cell_metrics.(cell_metrics_fieldnames{ii});
else
cell_metrics_batch.(cell_metrics_fieldnames{ii}){iii} = cell_metrics.(cell_metrics_fieldnames{ii}){1};
end
end
% Double field
elseif strcmp(subfieldstypes{ii},'double')
% If field does not exist
if ~isfield(cell_metrics,cell_metrics_fieldnames{ii})
cell_metrics_batch.(cell_metrics_fieldnames{ii})(h+1:hh+h) = nan(1,hh);
% If field exist
else
if size(cell_metrics.(cell_metrics_fieldnames{ii}),2) == hh && size(cell_metrics.(cell_metrics_fieldnames{ii}),1) == 1
cell_metrics_batch.(cell_metrics_fieldnames{ii})(h+1:hh+h) = cell_metrics.(cell_metrics_fieldnames{ii});
elseif size(cell_metrics.(cell_metrics_fieldnames{ii}),1) == hh && size(cell_metrics.(cell_metrics_fieldnames{ii}),2) == 1
cell_metrics_batch.(cell_metrics_fieldnames{ii})(h+1:hh+h) = cell_metrics.(cell_metrics_fieldnames{ii})';
else
cell_metrics_batch.(cell_metrics_fieldnames{ii})(h+1:hh+h) = nan(1,hh);
end
end
end
end
h=h+size(cell_metrics.cellID,2);
end
if ~isempty(cell_metrics_batch)
cell_metrics_batch.general.cellCount = length(cell_metrics_batch.UID);
batch_benchmark.clock(3) = toc(batch_timer);
cell_metrics_batch.general.batch_benchmark = batch_benchmark;
end
% Setting correct size of fields inside structs
cell_metrics_type_struct2 = {'acg','isi','waveforms','firingRateMaps','responseCurves','events','manipulations','spikes'};
for ii = 1:numel(cell_metrics_type_struct2)
if isfield(cell_metrics_batch,cell_metrics_type_struct2{ii})
structFields = fieldnames(cell_metrics_batch.(cell_metrics_type_struct2{ii}));
structFieldsType = struct2cell(structfun(@class,cell_metrics_batch.(cell_metrics_type_struct2{ii}),'UniformOutput',false));
structFieldsSize = struct2cell(structfun(@size,cell_metrics_batch.(cell_metrics_type_struct2{ii}),'UniformOutput',false));
for k = 1:length(structFields)
if strcmp(structFieldsType{k},'cell') & structFieldsSize{k}(2) < cell_metrics_batch.general.cellCount
cell_metrics_batch.(cell_metrics_type_struct2{ii}).(structFields{k}){cell_metrics_batch.general.cellCount} = [];
elseif strcmp(structFieldsType{k},'double') & structFieldsSize{k}(2) < cell_metrics_batch.general.cellCount
cell_metrics_batch.(cell_metrics_type_struct2{ii}).(structFields{k})(:,structFieldsSize{k}(2)+1:cell_metrics_batch.general.cellCount) = nan;
end
end
end
end
if ishandle(ce_waitbar)
waitbar(1,ce_waitbar,'Loading complete');
if isempty(waitbar_handle)
close(ce_waitbar)
end
end