-
Notifications
You must be signed in to change notification settings - Fork 0
/
HDCMOSMEAsort.m
345 lines (271 loc) · 13 KB
/
HDCMOSMEAsort.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
function [ ROIs, params, ROIsAsCC ] = HDCMOSMEAsort(params)
%[ ROIs, params, ROIsAsCC ] = HDCMOSMEAsort(filename, filenameEvents)
%HDCMOSMEAsort performs spike sorting of high density array data
%based on (convolutive) ICA
% created by Christian Leibig 12.02.13
diary logfile_HDCMOSMEAsort.txt
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% I/O
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%recording and events for ROI construction:
dataPath = '/home/cleibig/SimulatedData/SynDataTest/0-0,4ms random/';
params = setfieldifnotpresent(params,'filename',...
strcat(dataPath,'syn8753vs8778.nfx.cpd.h5'));
params = setfieldifnotpresent(params,'filenameEvents',...
strcat(dataPath,'syn8753vs8778.nfx.cpd.h5.basic_events'));
clear dataPath
%results:
resultsPath = '/home/cleibig/SimulatedData/SynDataTest/0-0,4ms random/';
params = setfieldifnotpresent(params,'filenameResults',...
strcat(resultsPath,'syn8753vs8778.nfx.cpd.h5.events'));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Local spike sorter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
params = setfieldifnotpresent(params,'processroiHandle',...
str2func('processroiembeddedcica'));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Get data and array specs from metadata of hdf5 file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
metadata = readmetadata(params.filename);
params.sensor_rows = metadata.sensorRows;
params.sensor_cols = metadata.sensorCols;
params.sr = metadata.sr;
params.frameStartTimes = metadata.frameStartTimes;
params.pitch = metadata.sensorPitch; %in µm
params.chipType = metadata.chipType;
%delta sensor coord.
d_sensor_row = double(params.sensor_rows(2) - params.sensor_rows(1));
d_sensor_col = double(params.sensor_cols(2) - params.sensor_cols(1));
%delta \mum.
params.d_row = d_sensor_row * params.pitch;
params.d_col = d_sensor_col * params.pitch;
params.sensor_rho = 1000000/(params.d_row * params.d_col); %per mm²
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Default arguments
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
params.plotting = 0;
params.interactive = 0;
%%%%% Event detection is not planned to be done in Matlab - refactor that %
if d_sensor_col == 2 && d_sensor_row == 1
params.roi.thr_factor = 10.95;
params.roi.n_rows = 5;
params.roi.n_cols = 3;
params.roi.n_frames = 3;
elseif d_sensor_col == d_sensor_row
params.roi.thr_factor = 9.5;
params.roi.n_rows = 3;
params.roi.n_cols = 3;
params.roi.n_frames = 3;
else
error(strcat('Unknown readout configuration.\n',...
'Please check parameters for ROI identification'));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% ROI construction %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
params.roi.method = 'cog';
params.roi = setfieldifnotpresent(params.roi, 'maxSensorsPerEvent', 169);
params.roi.minNoEvents = 3 * ... %multiplying factor in Spikes / second
(params.frameStartTimes(end) - params.frameStartTimes(1))/1000;
params.roi.mergeThr = 0.1;
params.roi = setfieldifnotpresent(params.roi, 'maxSensorsPerROI', 169);
params.roi.horizon = 2*floor(params.sr);%~1 ms to the left and to the right
%of detected activity is taken for the temporal ROI
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Expected number of neurons %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
params.neuron_rho = 2029; %in mm�?�² only used if params.ica.estimate = 'none'
if params.interactive
params.neuron_rho = input(['Please specify the expected neuron '...
' density in mm�?�²: ']);
end
% Upper bound for neuron number gets automatically estimated if
% params.ica.estimate = 'eigSpectrum'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% instantaneous ICA %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
params.ica.allframes = true; %if true, all frames are used
params.ica.allchannels = false; %if true, all channels are used
%params.ica.frames - boolean array of length T, indicating the frames to
%be used (overwritten, if allframes is true) gets assigned after roi
%identification
%params.ica.channels - boolean array of length M, indicating the channels
%to be used (overwritten, if allchannels is true) gets assigned after
%roi identification
params.ica.nonlinearity = 'pow3';
%Methods for estimating the number of ICs:
% 'none','svdSpectrum','eigSpectrum','icaDeflation'
params.ica.estimate = 'eigSpectrum';
params.ica.cpn = 1; %components per neuron for later use to calculate
%params.ica.numOfIC (overwritten, if params.estimate is true)
params.ica.per_var = 1; %keep that many dimensions such that per_var of the
%total variance gets explained
params.ica.approach = 'symm';
params.ica.verbose = 'off';
params.ica.renorm = false; %if true renormalize W and S such that only noise
%instead of all signal is of unit variance
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% convolutive ICA %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
params = setfieldifnotpresent(params,'L',6);
params = setfieldifnotpresent(params,'M',5);
params = setfieldifnotpresent(params,'allframes_cica',1);
params = setfieldifnotpresent(params,'min_corr',0.02);
params = setfieldifnotpresent(params,'max_cluster_size',4);
params = setfieldifnotpresent(params,'max_iter',1);
params.maxlags = params.L + params.M;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Spike time identification %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
params.thrFactor = 3;
%Upsampling factor used for spike time identification
params.upsample = floor(100/params.sr);
%deprecated:
params.sign_lev = 0.05; %for automatic threshold adaptation;
% was used previously with Hartigans dip test
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Automatic removal of noise sources %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
params.min_no_peaks = 5 * ... %multiplying factor in Spikes / second
(params.frameStartTimes(end) - params.frameStartTimes(1))/1000 ...
+ (1 - normcdf(params.thrFactor))*length(params.frameStartTimes);
%previous line adds expected FPs due to Gaussian noise <-> min. Spiking
%frequency less dependent on thrFactor.
params.min_skewness = 0.2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Automatic removal of mixture units %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
params.maxRSTD = 0.5;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Selective features %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
params.minSeparability = 0;
params.minIsoIBg = 0;
params.minIsoINN = 0;
params.minKurtosis = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Fusion of results from different regions of interest %%%%%%%%%%%%%%%%
%spike train alignment.
params.t_s = 0.5; %ms
params.t_jitter = 1; %ms
%redundancy reduction parameters, adjustable in GUI.
%maximal distance in \mum for extrema of average waveforms
params = setfieldifnotpresent(params,'d_max',params.pitch);
%fraction of coincident spikes
params = setfieldifnotpresent(params,'coin_thr',0.5);
%similarity of average waveforms
params = setfieldifnotpresent(params,'sim_thr',0.5);
params = setfieldifnotpresent(params,'autoremoveRedundantUnits',false);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
t_total_1 = clock;% ⅰVamos!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Construction of regions of interest
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
metaData.sensor_rows = params.sensor_rows;
metaData.sensor_cols = params.sensor_cols;
metaData.sr = params.sr;
metaData.frameStartTimes = params.frameStartTimes;
metaData.filename_events = params.filenameEvents;
fprintf('\nConstructing regions of interest...\n');
t1 = clock;
dummyData = []; %refactor: remove data from parameter list of roisegmentation
[ROIs, OL, ROIsAsCC] = roisegmentation(dummyData, metaData, params.roi,...
params.plotting);
t2 = clock;
fprintf('...prepared %g ROIs in %g seconds\n',length(ROIs),etime(t2,t1));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Parallel processing of multiple regions of interest
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
nrOfROIs = length(ROIs);
if nrOfROIs >= feature('numCores')
params = setfieldifnotpresent(params,'N_SESSIONS',...
feature('numCores')-2);%at leasta -1 due to master process
else
params = setfieldifnotpresent(params,'N_SESSIONS',nrOfROIs - 1);
end
if params.N_SESSIONS > 0
multicoreDir = 'multicorefiles';
mkdir(multicoreDir);
startmatlabsessions(params.N_SESSIONS,multicoreDir);
settings.multicoreDir = multicoreDir;
settings.nrOfEvalsAtOnce = 1;%floor(nrOfROIs/N_SESSIONS); % default: 4
settings.maxEvalTimeSingle = Inf;
settings.masterIsWorker = true;
settings.useWaitbar = false;
% Build cell array containing all nrOfROIs parameter sets.
parameterCell = cell(1, nrOfROIs);
for k = 1:nrOfROIs
ROIs(k).k = k;
parameterCell{1,k} = {ROIs(k), params};
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Actual work is done here
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf('\nParallel processing of %g different ROIs...\n',nrOfROIs);
t1 = clock;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ROIs = cell2mat(startmulticoremaster(params.processroiHandle,...
parameterCell,settings));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
t2 = clock;
fprintf(['Parallel processing of %g different regions ',...
'of interest performed in %g seconds\n'],nrOfROIs,etime(t2,t1));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
stopmatlabsessions(multicoreDir);
rmdir(multicoreDir);
else
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Actual work is done here
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf('\nSerially processing %g ROIs...\n',nrOfROIs);
t1 = clock;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tmpROIs = cell(1, nrOfROIs);
for i = 1:nrOfROIs
ROIs(i).k = i;
[ tmpROIs{i} ] = params.processroiHandle( ROIs(i), params );
end
ROIs = cell2mat(tmpROIs);
clear tmpROIs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
t2 = clock;
fprintf('done in %g seconds\n',etime(t2,t1));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Combine results from different ROIs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf('Fusing results from different ROIs...\n');
[units] = unitsfromrois(ROIs);
%Set all unit states to unchecked - necessary to save results
%1 - unchecked, 2 - mixture, 3 - single, % 4 - delete
if ~isfield([units],'state')
[units.state] = deal(1);
end
if params.autoremoveRedundantUnits
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Removal of redundantly identified units
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf('Removing redundantly identified units...\n');
[units.state] = deal(2);
data = readdatablock(params.filename,...
1,length(params.sensor_rows),...
1,length(params.sensor_cols),...
1,length(params.frameStartTimes));
[numDistinct, sizes, members] = redundantcandidates(units, ROIs, ...
params, data, params.d_max, params.coin_thr, params.sim_thr);
clear data
for i = 1:numDistinct
if sizes(i) > 1
%keep only the most separable one.
markAsDelete = ([units(members{i}).separability] ~= ...
max([units(members{i}).separability]));
fprintf('Changing state of unit(s) %g to delete.\n',...
members{i}(markAsDelete));
[units(members{i}(markAsDelete)).state] = deal(4);
end
end
end
%Save changed unit states back to ROIs
[ROIs] = units2rois(units, ROIs);
clear units
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Save results
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
saveresults(ROIs, params);
t_total_2 = clock;
fprintf('Total HDCMOSMEAsort performed in %g seconds\n',...
etime(t_total_2,t_total_1));
diary off