-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspkClusterQual.m
More file actions
268 lines (166 loc) · 6.85 KB
/
spkClusterQual.m
File metadata and controls
268 lines (166 loc) · 6.85 KB
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
function [ nidx , lmetrics , oCen] = spkClusterQual( featData, clustID, clustCens, stage )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
if stage == 1
[lmetrics] = initialQual(featData, clustID);
nidx = nan;
oCen = nan;
elseif stage == 2
if size(featData,1) < 30
lmetrics.IsDs = NaN;
lmetrics.LRat = NaN;
nidx = ones(size(featData,1),1);
oCen = computeCentroids(nidx, featData);
return
else
[fmetrics] = initialQual(featData, clustID);
[lmetrics , nidx, oCen] = finalQual(featData, fmetrics, clustID, clustCens);
end
end
end
%%%% INITAL Quality Parameters function
function [metrics] = initialQual(normalX, clusID)
nSpikes = size(normalX,1);
IsolDist = zeros(max(clusID),1); % > 20
Lratio = zeros(max(clusID),1); % < 0.4
for i = 1:max(clusID)
ClusterSpikes = find(clusID == i);
if length(ClusterSpikes) < size(normalX,2)
Lratio(i) = 0.8;
IsolDist(i) = 5;
continue
end
nClusterSpikes = length(ClusterSpikes);
NoiseSpikes = setdiff(1:nSpikes, ClusterSpikes);
m = mahal(normalX, normalX(ClusterSpikes,:));
% mCluster = m(ClusterSpikes);
mNoise = m(NoiseSpikes);
df = size(normalX,2);
L = sum(1-chi2cdf(m(NoiseSpikes),df));
Lratio(i) = L/nClusterSpikes;
% InClu = ismember(1:nSpikes, ClusterSpikes);
if (nClusterSpikes < nSpikes/2)
[sorted, ~] = sort(mNoise);
IsolDist(i) = sorted(nClusterSpikes);
else
IsolDist(i) = NaN;
end
metrics.IsDs = IsolDist;
metrics.LRat = Lratio;
end
end
%%%% FINAL Quality Parameters function
function [ metrics , nclID , oCens ] = finalQual(normalX, inMetrics, idxF, inCens)
nSpikes = size(normalX,1);
IsolDist = inMetrics.IsDs;
Lratio = inMetrics.LRat;
clustREFINE = true;
while clustREFINE
if all(IsolDist < 20 | Lratio > 0.4)
[nclID, oCens] = kmeans(normalX,2,'Distance','cityblock',...
'Replicates',5);
[metrics] = initialQual(normalX, nclID);
disp('All clusters are bad')
return
elseif any(IsolDist < 20 | Lratio > 0.4)
% Take bad cluster points and reassign two kept centroids
% Step 1
badClustInd = find(IsolDist < 20 | Lratio > 0.4);
badClustNum = numel(badClustInd);
goodCens = inCens(~(IsolDist < 20 | Lratio > 0.4),:);
goodCenID = find(~(IsolDist < 20 | Lratio > 0.4));
inCens = inCens(goodCenID,:);
%
badClustInfo = struct;
for bci = 1:badClustNum
bciId = badClustInd(bci);
badCltPts = find(idxF == bciId);
reaSSign = zeros(length(badCltPts),3);
for reAs = 1:numel(badCltPts);
tempPoint = normalX(badCltPts(reAs),:);
reaSSign(reAs,1) = idxF(badCltPts(reAs));
cenDists = zeros(length(goodCenID),1);
for cens = 1:size(goodCens,1);
compDist = [goodCens(cens,:); tempPoint];
cenDists(cens,1) = pdist(compDist,'euclidean');
end
% Find minDist - assign dist and new cluster
[minDist,minDid] = min(cenDists);
reaSSign(reAs,2) = minDist;
reaSSign(reAs,3) = goodCenID(minDid);
end
badClustInfo.(strcat('bdC_', num2str(bciId))).bdClID = bciId;
badClustInfo.(strcat('bdC_', num2str(bciId))).bdPts = badCltPts;
badClustInfo.(strcat('bdC_', num2str(bciId))).reCls = reaSSign;
end
% Reassign cluster values
for nci = 1:length(fieldnames(badClustInfo));
bdID = badClustInd(nci);
ptInd = badClustInfo.(strcat('bdC_', num2str(bdID))).bdPts;
idxF(ptInd) = badClustInfo.(strcat('bdC_', num2str(bdID))).reCls(:,3);
end
% Recompute cluster metrics
IsolDist = zeros(numel(goodCenID),1); % > 20
Lratio = zeros(numel(goodCenID),1); % < 0.4
for i = 1:numel(goodCenID)
cID = goodCenID(i);
ClusterSpikes = find(idxF == cID);
nClusterSpikes = length(ClusterSpikes);
NoiseSpikes = setdiff(1:nSpikes, ClusterSpikes);
m = mahal(normalX, normalX(ClusterSpikes,:));
% mCluster = m(ClusterSpikes);
mNoise = m(NoiseSpikes);
df = size(normalX,2);
L = sum(1-chi2cdf(m(NoiseSpikes),df));
Lratio(i) = L/nClusterSpikes;
%InClu = ismember(1:nSpikes, ClusterSpikes);
if (nClusterSpikes < nSpikes/2)
[sorted, ~] = sort(mNoise);
IsolDist(i) = sorted(nClusterSpikes);
else
IsolDist(i) = 21;
end
end
% Check whether new clusters pass muster
if any(IsolDist < 20 | Lratio > 0.4)
if sum((IsolDist < 20 | Lratio > 0.4)) < 2
nclID = idxF;
metrics.IsDs = IsolDist;
metrics.LRat = Lratio;
oCens = computeCentroids(idxF, normalX);
clustREFINE = false;
elseif sum((IsolDist < 20 | Lratio > 0.4)) >= 2
% Fix idxf
presNums = unique(idxF);
newNumsC = 1:1:length(presNums);
for ppi = 1:length(presNums);
changeIND = idxF == presNums(ppi);
idxF(changeIND) = newNumsC(ppi);
end
clustREFINE = true;
end
else
nclID = idxF;
metrics.IsDs = IsolDist;
metrics.LRat = Lratio;
oCens = computeCentroids(idxF, normalX);
clustREFINE = false;
end
else
nclID = idxF;
metrics.IsDs = IsolDist;
metrics.LRat = Lratio;
oCens = computeCentroids(idxF, normalX);
clustREFINE = false;
end
end
end
function newCens = computeCentroids(idx, featData)
idxIDS = unique(idx);
newCens = zeros(numel(unique(idx)),size(featData,2));
for fi = 1:numel(unique(idx))
tempID = idxIDS(fi);
tempFeats = featData(idx == tempID,:);
newCens(fi,:) = mean(tempFeats);
end
end