forked from cortex-lab/KiloSort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_data_and_initialize.m
267 lines (219 loc) · 8.07 KB
/
load_data_and_initialize.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
if ~exist('loaded', 'var')
tic
if ~isempty(ops.chanMap)
if ischar(ops.chanMap)
load(ops.chanMap);
try
chanMapConn = chanMap(connected>1e-6);
catch
chanMapConn = 1+chanNums(connected>1e-6);
end
else
chanMapConn = ops.chanMap;
end
else
chanMapConn = 1:ops.Nchan;
end
batch_path = fullfile(root, 'batches');
if ~exist(batch_path, 'dir')
mkdir(batch_path);
end
NchanTOT = ops.NchanTOT;
NT = ops.NT ;
d = dir(fullfile(root, fname));
ops.sampsToRead = floor(d.bytes/NchanTOT/2);
dmem = memory;
memfree = 8 * 2^30;
memallocated = min(ops.ForceMaxRAMforDat, dmem.MemAvailableAllArrays) - memfree;
memallocated = max(0, memallocated);
nint16s = memallocated/2;
NTbuff = NT + 4*ops.ntbuff;
Nbatch = ceil(d.bytes/2/NchanTOT /(NT-ops.ntbuff));
Nbatch_buff = floor(nint16s/ops.Nchan /(NT-ops.ntbuff));
Nbatch_buff = min(Nbatch_buff, Nbatch);
%% load data into patches, filter, compute covariance, write back to
% disk
[b1, a1] = butter(3, ops.fshigh/ops.fs, 'high');
fprintf('Time %3.0fs. Loading raw data... \n', toc);
fid = fopen(fullfile(root, fname), 'r');
ibatch = 0;
Nchan = ops.Nchan;
CC = gpuArray.zeros( Nchan, Nchan, 'single');
if strcmp(ops.whitening, 'noSpikes')
nPairs = gpuArray.zeros( Nchan, Nchan, 'single');
end
if ~exist('DATA', 'var')
DATA = zeros(NT, ops.Nchan, Nbatch_buff, 'int16');
end
while 1
ibatch = ibatch + 1;
offset = max(0, 2*NchanTOT*((NT - ops.ntbuff) * (ibatch-1) - 2*ops.ntbuff));
if ibatch==1
ioffset = 0;
else
ioffset = ops.ntbuff;
end
fseek(fid, offset, 'bof');
buff = fread(fid, [NchanTOT NTbuff], '*int16');
% keyboard;
if isempty(buff)
break;
end
nsampcurr = size(buff,2);
if nsampcurr<NTbuff
buff(:, nsampcurr+1:NTbuff) = repmat(buff(:,nsampcurr), 1, NTbuff-nsampcurr);
end
dataRAW = gpuArray(buff);
dataRAW = dataRAW';
dataRAW = single(dataRAW);
dataRAW = dataRAW(:, chanMapConn);
datr = filter(b1, a1, dataRAW);
datr = flipud(datr);
datr = filter(b1, a1, datr);
datr = flipud(datr);
switch ops.whitening
case 'noSpikes'
smin = my_min(datr, ops.loc_range, [1 2]);
sd = std(datr, [], 1);
peaks = single(datr<smin+1e-3 & bsxfun(@lt, datr, ops.spkTh * sd));
blankout = 1+my_min(-peaks, ops.long_range, [1 2]);
smin = datr .* blankout;
CC = CC + (smin' * smin)/NT;
nPairs = nPairs + (blankout'*blankout)/NT;
otherwise
CC = CC + (datr' * datr)/NT;
end
if ibatch<=Nbatch_buff
DATA(:,:,ibatch) = gather(int16( datr(ioffset + (1:NT),:)));
end
end
CC = CC / ibatch;
switch ops.whitening
case 'noSpikes'
nPairs = nPairs/ibatch;
end
fclose(fid);
fprintf('Time %3.0fs. Channel-whitening filters computed. \n', toc);
fprintf('Time %3.0fs. Loading raw data and applying filters... \n', toc);
switch ops.whitening
case 'diag'
CC = diag(diag(CC));
case 'noSpikes'
CC = CC ./nPairs;
end
[E, D] = svd(CC);
eps = 1e-6;
Wrot = E * diag(1./(diag(D) + eps).^.5) * E';
Wrot = ops.scaleproc * Wrot;
%
ibatch = 0;
fid = fopen(fullfile(root, fname), 'r');
fidW = fopen(fullfile(root, fnameTW), 'w');
%%
if strcmp(ops.initialize, 'fromData')
% initialize set of prototypes
ncurr = 1;
uBase = gpuArray.zeros(Nchan, ops.nFiltMax, size(ops.wPCA,2), 'single');
uBase(:,1,:) = 0;
nS = zeros(size(uBase,2),1);
wPCA = ops.wPCA(:, 1:ops.Nrank);
end
i0 = 0;
proj = zeros(1e6, size(ops.wPCA,2) * Nchan, 'single');
%
while 1
ibatch = ibatch + 1;
if ibatch<=Nbatch_buff
datr = single(gpuArray(DATA(:,:,ibatch)));
else
offset = max(0, 2*NchanTOT*((NT - ops.ntbuff) * (ibatch-1) - 2*ops.ntbuff));
if ibatch==1
ioffset = 0;
else
ioffset = ops.ntbuff;
end
fseek(fid, offset, 'bof');
buff = fread(fid, [NchanTOT NTbuff], '*int16');
if isempty(buff)
break;
end
nsampcurr = size(buff,2);
if nsampcurr<NTbuff
buff(:, nsampcurr+1:NTbuff) = repmat(buff(:,nsampcurr), 1, NTbuff-nsampcurr);
end
dataRAW = gpuArray(buff);
dataRAW = dataRAW';
dataRAW = single(dataRAW);
dataRAW = dataRAW(:, chanMapConn);
datr = filter(b1, a1, dataRAW);
datr = flipud(datr);
datr = filter(b1, a1, datr);
datr = flipud(datr);
datr = datr(ioffset + (1:NT),:);
end
datr = datr * Wrot;
if ibatch<=Nbatch_buff
DATA(:,:,ibatch) = gather(datr);
else
datcpu = gather(int16(datr));
fwrite(fidW, datcpu, 'int16');
end
dataRAW = gpuArray(datr);
dataRAW = single(dataRAW);
dataRAW = dataRAW / ops.scaleproc;
if strcmp(ops.initialize, 'fromData')
if ncurr<ops.nFiltMax
% find isolated spikes
[row, col, mu] = isolated_peaks(dataRAW, ops.loc_range, ops.long_range, ops.spkTh);
% find their PC projections
uS = get_PCproj(dataRAW, row, col, ops.wPCA, ops.maskMaxChannels);
% merge in with existing templates
[nSnew, iNonMatch] = merge_spikes_in(uBase(:,1:ncurr,:), nS(1:ncurr), uS, ops.crit);
nS(1:ncurr) = nSnew;
% reduce non-matches
[uNew, nSadd] = reduce_clusters(uS(:,iNonMatch,:), ops.crit);
% add new spikes to list
uBase(:, ncurr + [1:size(uNew,2)], :) = uNew;
nS(ncurr + [1:size(uNew,2)]) = nSadd;
uS = permute(uS, [2 1 3]);
uS = reshape(uS,numel(row), Nchan * size(ops.wPCA,2));
proj(i0 + (1:numel(row)), :) = gather(uS);
i0 = i0 + numel(row);
if i0>size(proj,1)
proj(1e6 + size(proj,1), 1) = 0;
end
ncurr = ncurr + size(uNew,2);
end
end
end
if strcmp(ops.initialize, 'fromData')
ncurr = min(ncurr, ops.nFiltMax);
nS = nS(1:ncurr);
uBase = uBase(:,1:ncurr, :);
[~, isort] = sort(nS, 'descend');
dU = uBase(:,isort,:);
mu = sum(sum(dU.^2, 3),1).^.5;
muinit = single(gather(mu(:)));
nt0 = size(ops.wPCA,1);
dU = permute(dU, [3 1 2]);
Wrec = reshape(wPCA * dU(:,:), nt0, Nchan, []);
W = zeros(nt0, ncurr, Nrank, 'single');
U = zeros(Nchan, ncurr, Nrank, 'single');
for j = 1:Nfilt
[w sv u] = svd(Wrec(:,:,j));
w = w * sv;
W(:,j,:) = w(:, 1:Nrank);
U(:,j,:) = u(:, 1:Nrank);
end
Uinit = single(gather(dU));
W = repmat(single(wPCA), [1 1 ncurr]);
Winit = permute(W, [1 3 2]);
end
Wrot = gather(Wrot);
rez.Wrot = Wrot;
fclose(fidW);
fclose(fid);
fprintf('Time %3.2f. Whitened data written to disk... \n', toc);
fprintf('Time %3.2f. Preprocessing complete!\n', toc);
loaded = 1;
end