-
Notifications
You must be signed in to change notification settings - Fork 2
/
der_detectDuplicateSpikes.m
304 lines (244 loc) · 12.8 KB
/
der_detectDuplicateSpikes.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
function [spikeInfos, duplicateSpikes_neg, duplicateSpikes_sameSign, EuclDis] = der_detectDuplicateSpikes(spikeInfos, nr_chBundle, index_ampSpi, thresholdShape, minDiffSpike)
%der_detectDuplicateSpikes
% der_detectDuplicateSpikes search for dublicate spikes in all channels
% of a bundle; duplicate spikes in the same channel (because of positive
% and negative spike event extraction) are also deleted
%
%
% Input:
% spikeInfos (table) containing the following information for each spike:
% bundleID: number of bundle
% channelID: number of channel
% threshold: threshold for spike-detection in µV
% clusterID: number of cluster of current channel; 0 defines artifacts and
% unassigned spikes
% unitClass: classification of unit in SU (single-), MU (multi-unit) or A
% (artifact)
% timeStamps: time of amplitude of a spike in milliseconds
% SpikeShapes: shape of spike (in out setup using combinato: 64 samples)
%
% nr_chBundle: number of channels per bundle; default is 8
% thresholdRho: threshold for median eucledian distance to detect duplicate spikes
%
% Output:
% corrected spikeInfos
% duplicateSpikes_neg
% duplicateSpikes_sameSign
% EuclDis
%
%
% Licence:
% This source code form is subject to the terms of the Mozilla Public
% Licence, v. 2.0. if a copy of the MPL was not distributed with this file,
% you can optain one at http://mozilla.org/MPL/2.0/.
dbstop if error
if ~exist('nr_chBundle','var') || isempty(nr_chBundle)
nr_chBundle = 8;
end
if ~exist('index_ampSpi','var') || isempty(index_ampSpi)
index_ampSpi = 20;
end
if ~exist('thresholdShape', 'var') || isempty(thresholdShape)
thresholdShape = 8.5;
end
% compaire spike shapes von first to a chosen sample after amplitude;
% default is 40, see details in Dehnen & Kehl et al., 2020,....
no_samples = 40;
% sort all spiketimes
if ~issorted(spikeInfos.timeStamps)
[~, idxsort] = sort(spikeInfos.timeStamps);
spikeInfos = spikeInfos(idxsort,:);
warning('Input not sorted according to time stamps!');
fprintf('Sorting input spikes... \n')
end
% set some variables
nr_channels = max(spikeInfos.channelID);
duplicateSpikes_neg = 0;
% diff of timestamps in same channel must be below
minDiffSpikeSC = 0.65;
% min difference of times of amplitudes in different channels in �s
if ~exist('minDiffSpike','var') || isempty(minDiffSpike)
minDiffSpike = .05;
end
index_doubleSpikesAll = [];
EuclDis = [];
% loop over bundle
for channel = 1:nr_chBundle:nr_channels
disp(num2str(channel))
% find all existing channels in current bundle
index = 1;
index_ChBundle = [];
for ch = channel:channel+(nr_chBundle-1)
index_currSp = find(spikeInfos.channelID == ch);
if sum(index_currSp) > 0
spikes = spikeInfos.SpikeShapes(index_currSp,:);
timeStamps = spikeInfos.timeStamps(index_currSp);
clusterID = spikeInfos.clusterID(index_currSp);
index_ChBundle(index) = ch; %#ok<*AGROW>
index = index + 1;
%%%%%%%%%%%%%%% compare spikes in same channel %%%%%%%%%%%%%%%%
% delete duplikate spikes compairing positive and negative spikes
% in the same channel
% diff of timestamps must be below minDiffSpikeSC
index_duplicateSpikes = find(abs(diff(timeStamps)) <= minDiffSpikeSC);
index_duplicateSpikes = [index_duplicateSpikes, index_duplicateSpikes+1];
if ~isempty(index_duplicateSpikes)
% compared spikes must have opposite signs to be the same spike
% (this is a necessary criterium because each double detected
% spike is one time extracted as a negative and one time as a
% positive spike)
signesMaxima = [sign(spikes(index_duplicateSpikes(:,1),index_ampSpi)), sign(spikes(index_duplicateSpikes(:,2),index_ampSpi))];
index_oppositeSign = abs(diff(signesMaxima,[],2)) == 2;
index_duplicateSpikes = index_duplicateSpikes(index_oppositeSign,:);
% count duplicate spikes
duplicateSpikes_neg = duplicateSpikes_neg + size(index_duplicateSpikes,1);
% decide which spike should survive
cluster_nr = clusterID(index_duplicateSpikes(:,1));
cluster_nr2 = clusterID(index_duplicateSpikes(:,2));
% index_artifactsToDelete = sort(reshape(index_duplicateSpikes(cluster_nr == 0 | cluster_nr2 == 0,:),[],1));
index_artifactsToDelete = sort([index_duplicateSpikes(cluster_nr == 0,1); index_duplicateSpikes(cluster_nr2 == 0,2)]);
index_duplicateSpikes(cluster_nr == 0 | cluster_nr2 == 0,:) = [];
% kill all artifacts, if spike which is compared to is not in
% artifact cluster
timeStamps(index_artifactsToDelete) = nan;
cluster_nr = [clusterID(index_duplicateSpikes(:,1)) clusterID(index_duplicateSpikes(:,2))];
% delete spike of MU, if same spike is detected in SU and MU;
% else delete spike of cluster with smaller signal to noise
unitClass = cell(2,1);
for spi = 1:size(index_duplicateSpikes,1)
unitClass(1) = spikeInfos.unitClass(index_currSp(index_duplicateSpikes(spi,1)));
unitClass(2) = spikeInfos.unitClass(index_currSp(index_duplicateSpikes(spi,2)));
% if unit class is equal keep spike of cluster with better
% signal / noise
if isequal(unitClass(1),unitClass(2))
index_ch = clusterID == cluster_nr(spi,1);
amplitude = nanmedian(spikes(index_ch,index_ampSpi));
threshold = spikeInfos.threshold(index_currSp(1));
signalToNoiseClA = abs(amplitude) / threshold;
index_ch = clusterID == cluster_nr(spi,2);
amplitude = nanmedian(spikes(index_ch,index_ampSpi));
threshold = spikeInfos.threshold(index_currSp(1));
signalToNoiseClB = abs(amplitude) / threshold;
if signalToNoiseClA <= signalToNoiseClB
timeStamps(index_duplicateSpikes(spi,1)) = nan;
else
timeStamps(index_duplicateSpikes(spi,2)) = nan;
end
elseif strcmp(unitClass{1},'SU')
timeStamps(index_duplicateSpikes(spi,2)) = nan;
else
timeStamps(index_duplicateSpikes(spi,1)) = nan;
end
end
% delete duplicate spikes
index_DS = isnan(timeStamps);
index_duSp = index_currSp(index_DS);
spikeInfos.detectionLabel(index_duSp) = spikeInfos.detectionLabel(index_duSp)*3;
end
end
end
%%%%%%%%%%%%%% compare spikes in different channels %%%%%%%%%%%%%%%%%%%
% all possible combinations of existing channels
if length(index_ChBundle) > 1
index_currSpikes = ismember(spikeInfos.channelID,index_ChBundle);
currSpikeInfos = spikeInfos(index_currSpikes,:);
DiffIndexTS = diff(currSpikeInfos.timeStamps);
duplicateSpikes = find(DiffIndexTS <= minDiffSpike);
diffDuSp = diff(duplicateSpikes);
index_duplicateSpikes = [];
for currSpike = 1:length(diffDuSp)
index_DuSp = [];
if diffDuSp(currSpike) == 1
ds = currSpike;
while diffDuSp(ds) == 1 && ds < size(diffDuSp,1)
index_DuSp = [index_DuSp duplicateSpikes(ds)]; %#ok<*AGROW>
ds = ds + 1;
if ds > size(diffDuSp,1)
break
end
end
index_DuSp = [index_DuSp duplicateSpikes(ds) duplicateSpikes(ds)+1];
% corr for time diff to first spike of current index_DuSp
currTimeDiff = DiffIndexTS(index_DuSp(1:end-1));
if sum(currTimeDiff) > minDiffSpike
currTD = 1;
while sum(currTimeDiff(1:(currTD+1))) <= minDiffSpike
if currTD < length(currTimeDiff)
currTD = currTD + 1;
else
break
end
end
diffLength = length(currTimeDiff) - (currTD);
index_DuSp = index_DuSp(1:end-diffLength);
end
index_chToCompare = nchoosek(index_DuSp,2);
else
index_DuSp = [duplicateSpikes(currSpike) duplicateSpikes(currSpike)+1];
index_chToCompare = index_DuSp;
end
% just compaire within different channels
diffChannels = diff([currSpikeInfos.channelID(index_chToCompare(:,1)) currSpikeInfos.channelID(index_chToCompare(:,2))],1,2);
index_chToCompare = index_chToCompare(diffChannels ~=0,:);
if ~isempty(index_chToCompare)
% no_spikesToCompaire = size(index_chToCompare,1);
possDuplSpi1 = currSpikeInfos.SpikeShapes(index_chToCompare(:,1),1:no_samples);
possDuplSpi2 = currSpikeInfos.SpikeShapes(index_chToCompare(:,2),1:no_samples);
spikeShapes = [possDuplSpi1; possDuplSpi2];
[inspk, inputs] = der_wavedec(spikeShapes);
no_spikes = size(inspk,1);
coefficients = nan(no_spikes/2, inputs, 2);
coefficients(:,:,1) = inspk(1:no_spikes/2,:);
coefficients(:,:,2) = inspk(no_spikes/2+1:end,:);
% calculating euclidean distance
euclDis = sqrt(sum((diff(coefficients,[],3)).^2,2));
EuclDis = [EuclDis; euclDis];
if median(euclDis) <= thresholdShape
index_duplicateSpikes = [index_duplicateSpikes; index_chToCompare];
end
end
end
for spi = 1:size(index_duplicateSpikes,1)
unitClass(1) = currSpikeInfos.unitClass(index_duplicateSpikes(spi,1));
unitClass(2) = currSpikeInfos.unitClass(index_duplicateSpikes(spi,2));
% if unit class is equal keep spike of channel with larger
% signal / noise
% if one unit is labeled as an artifact both spikes
% will be marked to be deleted
if isequal(unitClass(1),'A') || isequal(unitClass(2),'A')
elseif isequal(unitClass(1),unitClass(2))
index_ch = currSpikeInfos.channelID(index_duplicateSpikes(spi,1));
index_ch = currSpikeInfos.channelID == index_ch;
index_cl = currSpikeInfos.clusterID(index_duplicateSpikes(spi,1));
index_cl = currSpikeInfos.clusterID(index_ch) == index_cl;
amplitude = nanmedian(currSpikeInfos.SpikeShapes(index_cl,index_ampSpi));
threshold = currSpikeInfos.threshold(index_duplicateSpikes(spi,1));
signalToNoiseChA = abs(amplitude) / threshold;
index_ch = currSpikeInfos.channelID(index_duplicateSpikes(spi,2));
index_ch = currSpikeInfos.channelID == index_ch;
index_cl = currSpikeInfos.clusterID(index_duplicateSpikes(spi,2));
index_cl = currSpikeInfos.clusterID(index_ch) == index_cl;
amplitude = nanmedian(currSpikeInfos.SpikeShapes(index_cl,index_ampSpi));
threshold = currSpikeInfos.threshold(index_duplicateSpikes(spi,2));
signalToNoiseChB = abs(amplitude) / threshold;
if signalToNoiseChA <= signalToNoiseChB
index_duplicateSpikes(spi,2) = nan;
else
index_duplicateSpikes(spi,1) = nan;
end
elseif strcmp(unitClass{1},'SU')
index_duplicateSpikes(spi,2) = nan;
else
index_duplicateSpikes(spi,1) = nan;
end
end
% delete duplicate spikes
index_duplicateSpikes = reshape(index_duplicateSpikes,[],1);
index_currSpikes = find(index_currSpikes~=0);
index_doubleSpikesAll = [index_doubleSpikesAll; index_currSpikes(index_duplicateSpikes(~isnan(index_duplicateSpikes)))];
end
end
index_doubleSpikesAll = unique(index_doubleSpikesAll);
duplicateSpikes_sameSign = length(index_doubleSpikesAll);
spikeInfos.detectionLabel(index_doubleSpikesAll) = spikeInfos.detectionLabel(index_doubleSpikesAll)*5;
end