-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessData.m
384 lines (329 loc) · 10.3 KB
/
preprocessData.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
function [rez, DATA, uproj] = preprocessData(ops)
tic;
uproj = [];
ops.nt0 = getOr(ops, {'nt0'}, 61);
% if strcmp(ops.datatype , 'openEphys')
% ops = convertOpenEphysToRawBInary(ops); % convert data, only for OpenEphys
% end
% if strcmp(ops.datatype, 'h5')
% ops = convertWillowToRawBInary(ops); % convert willow data
% end
if ~isempty(ops.chanMap)
if ischar(ops.chanMap)
load(ops.chanMap);
try
chanMapConn = chanMap(connected>1e-6);
xc = xcoords(connected>1e-6);
yc = ycoords(connected>1e-6);
catch
chanMapConn = 1+chanNums(connected>1e-6);
xc = zeros(numel(chanMapConn), 1);
yc = [1:1:numel(chanMapConn)]';
disp('WTF1!');
keyboard
end
ops.Nchan = getOr(ops, 'Nchan', sum(connected>1e-6));
ops.NchanTOT = getOr(ops, 'NchanTOT', 1024);
%% JPK: fs is loaded from MAT file
if exist('fs', 'var')
ops.fs = getOr(ops, 'fs', fs);
end
else
chanMap = ops.chanMap;
chanMapConn = ops.chanMap;
xc = zeros(numel(chanMapConn), 1);
yc = [1:1:numel(chanMapConn)]';
connected = true(numel(chanMap), 1);
ops.Nchan = numel(connected);
sprintf('rez.ops.Nchan: %i', ops.Nchan);
ops.NchanTOT = 1024;
disp('WTF2!');
keyboard
end
else
chanMap = 1:ops.Nchan;
connected = true(numel(chanMap), 1);
chanMapConn = 1:ops.Nchan;
xc = zeros(numel(chanMapConn), 1);
yc = [1:1:numel(chanMapConn)]';
disp('WTF3!');
keyboard
end
if exist('kcoords', 'var')
% JPK: This should do nothing.
kcoords = kcoords(find(connected==1));
else
kcoords = ones(ops.Nchan, 1);
end
NchanTOT = ops.NchanTOT;
NT = ops.NT ;
rez.ops = ops;
rez.xc = xc;
rez.yc = yc;
rez.xcoords = xcoords;
rez.ycoords = ycoords;
rez.connected = connected;
rez.ops.chanMap = chanMap;
rez.ops.kcoords = kcoords;
d = dir(ops.fbinary);
ops.sampsToRead = floor(d.bytes/NchanTOT/2);
if ispc
dmem = memory;
memfree = dmem.MemAvailableAllArrays/8;
memallocated = min(ops.ForceMaxRAMforDat, dmem.MemAvailableAllArrays) - memfree;
memallocated = max(0, memallocated);
else
memallocated = ops.ForceMaxRAMforDat;
end
nint16s = memallocated/2;
NTbuff = NT + 4*ops.ntbuff;
Nbatch = ceil(d.bytes/2/NchanTOT /(NT-ops.ntbuff));
Nbatch_buff = floor(4/5 * nint16s/rez.ops.Nchan /(NT-ops.ntbuff)); % factor of 4/5 for storing PCs of spikes
Nbatch_buff = min(Nbatch_buff, Nbatch);
%% subtract virtual reference
%
% % open input data file
% fid = fopen(ops.fbinary, 'r');
% buff = fread(fid, '*int16');
% fclose(fid);
% M = 1024;
% N = length(buff)/M;
% data=reshape(buff,M,N);
% % only keep good channels
% tmp = data(chanMapConn+1,:);
% data = single(data)-mean(data(chanMapConn+1,:));
%% load data into patches, filter, compute covariance
% compute filter weights
if isfield(ops,'fslow')&&ops.fslow<ops.fs/2
[b1, a1] = butter(3, [ops.fshigh/ops.fs,ops.fslow/ops.fs]*2, 'bandpass');
else
[b1, a1] = butter(3, ops.fshigh/ops.fs*2, 'high');
end
fprintf('Time %3.0fs. Loading raw data... \n', toc);
% open input data file
fid = fopen(ops.fbinary, 'r');
ibatch = 0;
Nchan = rez.ops.Nchan;
NchanTOT = rez.ops.NchanTOT;
if ops.GPU
CC = gpuArray.zeros( Nchan, Nchan, 'single');
else
CC = zeros( Nchan, Nchan, 'single');
end
%if strcmp(ops.whitening, 'noSpikes')
% if ops.GPU
% nPairs = gpuArray.zeros( Nchan, Nchan, 'single');
% else
% nPairs = zeros( Nchan, Nchan, 'single');
% end
%end
if ~exist('DATA', 'var')
DATA = zeros(NT, rez.ops.Nchan, Nbatch_buff, 'int16');
end
isproc = zeros(Nbatch, 1);
while 1
ibatch = ibatch + ops.nSkipCov;
% jump to correct place in input data file
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');
% read from input data file in column order
% data read is interpreted as int16 and saved as int16
% HOW MUCH IS READ?
% NchanTOT = 1024
% NTbuff = (32*1024 + 64) + 4*64 = 33088 (if samples, then 1 second of data)
buff = fread(fid, [NchanTOT NTbuff], '*int16');
%% JPK: plot some data to make sure that it looks good
%keyboard
% if no more data, then exit while loop
if isempty(buff)
break;
end
% if remaining data is less than buffer, then fill buffer with copies of last sample
nsampcurr = size(buff,2);
if nsampcurr<NTbuff
buff(:, nsampcurr+1:NTbuff) = repmat(buff(:,nsampcurr), 1, NTbuff-nsampcurr);
end
% save data to gpu or cpu (variable)
if ops.GPU
dataRAW = gpuArray(buff);
else
dataRAW = buff;
end
% transpose data
% assumes...
% was rows = channels, columns = time
% now rows = time, columns = channels
dataRAW = dataRAW';
% convert to single precision
dataRAW = single(dataRAW);
% only keep subset of channels (columns)
dataRAW = dataRAW(:, chanMapConn+1);
% subtractvirtual reference
%dataRAW1 = dataRAW-mean(dataRAW,2);
virtref = mean(dataRAW,2);
virtpower = sum(virtref.^2);
a = size(dataRAW);
for i=1:a(2)
overlap = sum(virtref.*dataRAW(:,i))/virtpower;
dataRAW(:,i) = dataRAW(:,i)-overlap*virtref;
end
% JPK: normalize data so standard deviation==1
% since signals are strongly non-gaussian,
% use formula for std dev based on median
%ops.scaleproc = mean(median(abs(dataRAW)))/0.6745;
%keyboard
datr = filter(b1, a1, dataRAW);
datr = flipud(datr);
datr = filter(b1, a1, datr);
datr = flipud(datr);
%keyboard
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;
%disp('This ran1.');
end
if ibatch<=Nbatch_buff
DATA(:,:,ibatch) = gather_try(int16( datr(ioffset + (1:NT),:)));
isproc(ibatch) = 1;
end
end
CC = CC / ceil((Nbatch-1)/ops.nSkipCov);
%switch ops.whitening
% case 'noSpikes'
% nPairs = nPairs/ibatch;
%end
fclose(fid);
fprintf('Time %3.0fs. Channel-whitening filters computed. \n', toc);
switch ops.whitening
case 'diag'
CC = diag(diag(CC));
%disp('This ran2.');
case 'noSpikes'
CC = CC ./nPairs;
end
if ops.whiteningRange<Inf
ops.whiteningRange = min(ops.whiteningRange, Nchan);
Wrot = whiteningLocal(gather(CC), yc, xc, ops.whiteningRange);
else
%
[E, D] = svd(CC);
D = diag(D);
eps = 1e-6;
Wrot = E * diag(1./(D + eps).^.5) * E';
disp('WTF!')
keyboard
end
Wrot = ops.scaleproc * Wrot;
%keyboard
fprintf('Time %3.0fs. Loading raw data and applying filters... \n', toc);
fid = fopen(ops.fbinary, 'r');
fidW = fopen(ops.fproc, 'w');
if strcmp(ops.initialize, 'fromData')
i0 = 0;
ixt = round(linspace(1, size(ops.wPCA,1), ops.nt0));
wPCA = ops.wPCA(ixt, 1:3);
rez.ops.wPCA = wPCA; % write wPCA back into the rez structure
uproj = zeros(1e6, size(wPCA,2) * Nchan, 'single');
end
%
for ibatch = 1:Nbatch
if isproc(ibatch) %ibatch<=Nbatch_buff
%disp('This ran2.');
if ops.GPU
datr = single(gpuArray(DATA(:,:,ibatch)));
else
datr = single(DATA(:,:,ibatch));
end
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
if ops.GPU
dataRAW = gpuArray(buff);
else
dataRAW = buff;
end
dataRAW = dataRAW';
dataRAW = single(dataRAW);
% TODO why do we not need chanMapConn+1 here like we did before?
dataRAW = dataRAW(:, chanMapConn+1);
datr = filter(b1, a1, dataRAW);
datr = flipud(datr);
datr = filter(b1, a1, datr);
datr = flipud(datr);
datr = datr(ioffset + (1:NT),:);
end
%foo = datr * Wrot;
datr = datr * Wrot;
%keyboard
if ops.GPU
dataRAW = gpuArray(datr);
else
dataRAW = datr;
end
% dataRAW = datr;
dataRAW = single(dataRAW);
% TODO eliminate ops.scaleproc
dataRAW = dataRAW / ops.scaleproc;
if strcmp(ops.initialize, 'fromData') %&& rem(ibatch, 10)==1
% 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, wPCA, ops.maskMaxChannels);
uS = permute(uS, [2 1 3]);
uS = reshape(uS,numel(row), Nchan * size(wPCA,2));
if i0+numel(row)>size(uproj,1)
uproj(1e6 + size(uproj,1), 1) = 0;
end
uproj(i0 + (1:numel(row)), :) = gather_try(uS);
i0 = i0 + numel(row);
end
if ibatch<=Nbatch_buff
DATA(:,:,ibatch) = gather_try(datr);
else
datcpu = gather_try(int16(datr));
fwrite(fidW, datcpu, 'int16');
fprintf('Wrote whitened data to temporary file...');
end
end
datcpu = gather_try(int16(datr));
fwrite(fidW, datcpu, 'int16');
fprintf('Wrote whitened data to temporary file...');
if strcmp(ops.initialize, 'fromData')
uproj(i0+1:end, :) = [];
end
Wrot = gather_try(Wrot);
rez.Wrot = Wrot;
fclose(fidW);
fclose(fid);
if ops.verbose
fprintf('Time %3.2f. Whitened data written to disk... \n', toc);
fprintf('Time %3.2f. Preprocessing complete!\n', toc);
end
rez.temp.Nbatch = Nbatch;
rez.temp.Nbatch_buff = Nbatch_buff;