forked from petersenpeter/CellExplorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadCellMetrics.m
185 lines (157 loc) · 5.83 KB
/
loadCellMetrics.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
function [cell_metrics,cell_metrics_idxs] = loadCellMetrics(varargin)
% This function loads cell metrics for a given session
% Check the wiki of the CellExplorer for more details: https://cellexplorer.org/
%
% INPUTS
% id - takes a database id as input
% session - takes a database sessionName as input
% basepath - path to session (base directory)
%
% Filters:
% brainRegion, synapticEffect, putativeCellType, labels, deepSuperficial, animal, tags, groups, groundTruthClassification
%
% OUTPUT
% cell_metrics - Cell_metrics matlab structure
% cell_metrics_idxs - indexes of cells fulfilling filters*
%
% Example calls
% cell_metrics = loadCellMetrics('basepath',pwd);
% cell_metrics = loadCellMetrics('session',session);
% cell_metrics = loadCellMetrics('fileFormat','json')
% cell_metrics = loadCellMetrics('fileFormat','nwb')
% [cell_metrics,Pyramidal_indexes] = loadCellMetrics('session',session,'putativeCellType',{'Pyramidal'});
%
% By Peter Petersen
% petersen.peter@gmail.com
% Last edited: 06-07-2021
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% Parsing parameters
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
p = inputParser;
% Load an existing cell metrics struct
addParameter(p,'cell_metrics',{},@isstruct);
% Single session input
addParameter(p,'basepath',[],@isstr);
addParameter(p,'basename','',@isstr);
addParameter(p,'session',[],@isstruct);
addParameter(p,'sessionId',[],@isnumeric);
addParameter(p,'sessionName',[],@isstr);
% Batch input (not implemented)
addParameter(p,'sessionIDs',{},@iscell);
addParameter(p,'sessionNames',{},@iscell);
addParameter(p,'basepaths',{},@iscell);
% Extra inputs
addParameter(p,'saveAs','cell_metrics',@isstr); % Cell metrics name
addParameter(p,'fileFormat','mat',@isstr); % File format (options: mat,nwb,json)
% Filters
addParameter(p,'brainRegion',[],@iscell);
addParameter(p,'synapticEffect',[],@iscell);
addParameter(p,'putativeCellType',[],@iscell);
addParameter(p,'labels',[],@iscell);
addParameter(p,'deepSuperficial',[],@iscell);
addParameter(p,'animal',[],@iscell);
addParameter(p,'tags',[],@iscell);
addParameter(p,'groups',[],@iscell);
addParameter(p,'groundTruthClassification',[],@iscell);
parse(p,varargin{:})
% Provide existing cell metrics struct
cell_metrics = p.Results.cell_metrics;
% Single session input
sessionId = p.Results.sessionId;
session = p.Results.session;
basepath = p.Results.basepath;
basename = p.Results.basename;
% Batch
sessions = p.Results.sessionNames;
params = p.Results;
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% Loading metrics
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
if ~isempty(cell_metrics)
elseif ~isempty(session)
basepath = session.general.basePath;
basename = session.general.name;
elseif ~isempty(sessions)
cell_metrics = loadCellMetricsBatch('sessions',sessions);
elseif ~isempty(sessionId) || ~isempty(params.sessionName)
bz_database = db_credentials;
if ~isempty(sessionId)
[~, basename, basepath] = db_set_session('sessionId',sessionId);
else
[~, basename, basepath] = db_set_session('sessionName',params.sessionName);
end
else
if isempty(basepath)
basepath = pwd;
end
if isempty(basename)
basename = basenameFromBasepath(basepath);
end
end
file = fullfile(basepath,[basename,'.' ,params.saveAs,'.cellinfo.',params.fileFormat]);
% Loading metrics
if exist(file,'file')
switch lower(params.fileFormat)
case 'mat'
load(file,'cell_metrics')
cell_metrics.general.basepath = basepath;
cell_metrics.general.fileFormat = params.fileFormat;
cell_metrics.general.saveAs = params.saveAs;
case 'nwb'
cell_metrics = loadNwbCellMetrics(file);
case 'json'
cell_metrics = loadJsonCellMetrics(file);
otherwise
warning(['Unknown cell_metrics file format: ' file])
end
else
warning(['Error loading metrics: ' file])
return
end
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% Filtering metrics
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
filterIndx = ones(length(cell_metrics.UID),9);
if ~isempty(params.brainRegion)
filterIndx(:,1) = strcmp(cell_metrics.brainRegion,params.brainRegion);
end
if ~isempty(params.synapticEffect)
filterIndx(:,2) = contains(cell_metrics.synapticEffect,params.synapticEffect);
end
if ~isempty(params.putativeCellType)
filterIndx(:,3) = contains(cell_metrics.putativeCellType,params.putativeCellType);
end
if ~isempty(params.deepSuperficial)
filterIndx(:,4) = contains(cell_metrics.deepSuperficial,params.deepSuperficial);
end
if ~isempty(params.animal)
filterIndx(:,5) = strcmp(cell_metrics.animal,params.animal);
end
if ~isempty(params.labels)
filterIndx(:,6) = strcmp(cell_metrics.labels,params.labels);
end
if ~isempty(params.tags)
filterIndx(:,7) = 0;
for i = 1:numel(params.tags)
if isfield(cell_metrics.tags,params.tags{i})
filterIndx(cell_metrics.tags.(params.tags{i}),7) = 1;
end
end
end
if ~isempty(params.groups)
filterIndx(:,8) = 0;
for i = 1:numel(params.groups)
if isfield(cell_metrics.groups,params.groups{i})
filterIndx(cell_metrics.groups.(params.groups{i}),8) = 1;
end
end
end
if ~isempty(params.groundTruthClassification)
filterIndx(:,9) = 0;
for i = 1:numel(params.groundTruthClassification)
if isfield(cell_metrics.tags,params.groundTruthClassification{i})
filterIndx(cell_metrics.groundTruthClassification.(params.groundTruthClassification{i}),9) = 1;
end
end
end
cell_metrics_idxs = find(sum(filterIndx')==9);