forked from cortex-lab/KiloSort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgather_mean_spikes.m
81 lines (60 loc) · 1.86 KB
/
gather_mean_spikes.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
tic
if ~isempty(ops.chanMap)
load(ops.chanMap);
chanMapConn = chanMap(connected>1e-6);
else
chanMapConn = 1:ops.Nchan;
end
batch_path = fullfile(root, 'batches');
if ~exist(batch_path, 'dir')
mkdir(batch_path);
end
NchanTOT = ops.NchanTOT;
d = dir(fullfile(root, fname));
ops.sampsToRead = floor(d.bytes/NchanTOT/2);
NT = 128*1024+ ops.ntbuff;
NTbuff = NT + 4*ops.ntbuff;
Nbatch = ceil(d.bytes/2/NchanTOT /(NT-ops.ntbuff));
% load data into patches, filter, compute covariance, write back to
% disk
fprintf('Time %3.0fs. Loading raw data... \n', toc);
fid = fopen(fullfile(root, fname), 'r');
ibatch = 0;
Nchan = ops.Nchan;
Nchans = ops.Nchan;
ts = [1:1:61]';
clear stimes
% for iNN = 1:size(rez.W,2)
% stimes{iNN} = rez.st3pos(rez.st3pos(:,2)==iNN,1);
% end
stimes = gtimes;
Wraw = zeros(61, Nchans, numel(stimes));
for ibatch = 1:Nbatch
if ibatch>Nbatch_buff
offset = 2 * ops.Nchan*batchstart(ibatch-Nbatch_buff); % - ioffset;
fseek(fid, offset, 'bof');
dat = fread(fid, [NT ops.Nchan], '*int16');
else
dat = DATA(:,:,ibatch);
end
dataRAW = gpuArray(dat);
dataRAW = single(dataRAW);
dataRAW = dataRAW / ops.scaleproc;
if ibatch==1; ioffset = 0;
else ioffset = ops.ntbuff;
end
for iNN = 1:numel(stimes)
st = stimes{iNN} + ioffset - (NT-ops.ntbuff)*(ibatch-1) - 20;
st(st<0) = [];
st(st>NT-ops.ntbuff) = [];
if ~isempty(st)
inds = repmat(st', 61, 1) + repmat(ts, 1, numel(st));
Wraw(:,:,iNN) = Wraw(:,:,iNN) + ...
gather(squeeze(sum(reshape(dataRAW(inds, :), 61, numel(st), Nchans),2)));
end
end
end
for iNN = 1:numel(stimes)
Wraw(:,:,iNN) = Wraw(:,:,iNN)/numel(stimes{iNN});
end
fprintf('Time %3.2f. Mean waveforms computed... \n', toc);