-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdo_clustering.m
377 lines (342 loc) · 15.9 KB
/
do_clustering.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
% PROGRAM Do_clustering.
% Does clustering on all files in Files.txt
% Runs after Get_spikes.
function do_clustering
print2file = 1; % for saving printouts.
%print2file =0; % for printing printouts.
handles.par.w_pre = 20; % number of pre-event data points stored
handles.par.w_post = 44; % number of post-event data points stored
%handles.par.detection = 'pos'; % type of threshold
%handles.par.detection = 'neg'; % type of threshold
handles.par.detection = 'both'; % type of threshold
handles.par.stdmin = 5.00; % minimum threshold
handles.par.stdmax = 50; % maximum threshold
handles.par.interpolation = 'y'; % interpolation for alignment
handles.par.int_factor = 2; % interpolation factor
handles.par.detect_fmin = 300; % high pass filter for detection (default 300)
handles.par.detect_fmax = 3000; % low pass filter for detection (default 3000)
handles.par.sort_fmin = 300; % high pass filter for sorting (default 300)
handles.par.sort_fmax = 3000; % low pass filter for sorting (default 3000)
handles.par.max_spk = 20000; % max. # of spikes before starting templ. match.
handles.par.template_type = 'center'; % nn, center, ml, mahal
handles.par.template_sdnum = 3; % max radius of cluster in std devs.
handles.par.permut = 'y'; % for selection of random 'par.max_spk' spikes before starting templ. match.
% handles.par.permut = 'n'; % for selection of the first 'par.max_spk' spikes before starting templ. match.
handles.par.features = 'wav'; % choice of spike features (wav)
handles.par.inputs = 10; % number of inputs to the clustering (def. 10)
handles.par.scales = 4; % scales for wavelet decomposition
if strcmp(handles.par.features,'pca'); % number of inputs to the clustering for pca
handles.par.inputs=3;
end
handles.par.mintemp = 0; % minimum temperature
handles.par.maxtemp = 0.301; % maximum temperature (0.201)
handles.par.tempstep = 0.01; % temperature step
handles.par.num_temp = floor(...
(handles.par.maxtemp - ...
handles.par.mintemp)/handles.par.tempstep); % total number of temperatures
handles.par.stab = 0.8; % stability condition for selecting the temperature
handles.par.SWCycles = 100; % number of montecarlo iterations (100)
handles.par.KNearNeighb = 11; % number of nearest neighbors
handles.par.randomseed = 0; % if 0, random seed is taken as the clock value
%handles.par.randomseed = 147; % If not 0, random seed
handles.par.fname_in = 'tmp_data'; % temporary filename used as input for SPC
handles.par.min_clus_abs = 20; % minimum cluster size (absolute value)
handles.par.min_clus_rel = 0.005; % minimum cluster size (relative to the total nr. of spikes)
%handles.par.temp_plot = 'lin'; %temperature plot in linear scale
handles.par.temp_plot = 'log'; % temperature plot in log scale
handles.par.force_auto = 'y'; % automatically force membership if temp>3.
handles.par.max_spikes = 5000; % maximum number of spikes to plot.
handles.par.sr = 21000; % sampling frequency, in Hz.
figure
set(gcf,'PaperOrientation','Landscape','PaperPosition',[0.25 0.25 10.5 8])
files = textread('Files.txt','%s');
for k=1:length(files)
tic
file_to_cluster = files(k);
%Load continuous data (for ploting)
% for spikes only data this is not possible
% therefore we use a try-catch case for this
continuous_data_av = 1;
flag0 = 0;
try
eval(['load ' char(file_to_cluster) '.mat;']);
if length(data)>60*handles.par.sr
x = data(1:60*handles.par.sr)'; %will plot just 60 sec.
else
x = data;
end
clear data
eval(['load ' char(file_to_cluster) '_spikes.mat;']);
%Filters and gets threshold
[b,a] = ellip(2,0.1,40,[handles.par.sort_fmin handles.par.sort_fmax]*2/(handles.par.sr));
xf = filtfilt(b,a,x);
thr = handles.par.stdmin * median(abs(xf))/0.6745;
thrmax = handles.par.stdmax * median(abs(xf))/0.6745;
catch
continuous_data_av = 0;
% LOAD SC DATA
eval(['load ' char(file_to_cluster) '_aligned ;'],'flag0=1;');
if flag0==1
eval(['load ' char(file_to_cluster) ]);
end;
end;
% LOAD SPIKES
handles.par.fname = ['data_' char(file_to_cluster)]; %filename for interaction with SPC
nspk = size(spikes,1);
naux = min(handles.par.max_spk,size(spikes,1));
handles.par.min_clus = max(handles.par.min_clus_abs,handles.par.min_clus_rel*naux);
% CALCULATES INPUTS TO THE CLUSTERING ALGORITHM.
inspk = wave_features(spikes,handles); %takes wavelet coefficients.
% SELECTION OF SPIKES FOR SPC
if handles.par.permut == 'n'
% GOES FOR TEMPLATE MATCHING IF TOO MANY SPIKES.
if size(spikes,1)> handles.par.max_spk;
% take first 'handles.par.max_spk' spikes as an input for SPC
inspk_aux = inspk(1:naux,:);
else
inspk_aux = inspk;
end
%INTERACTION WITH SPC
save(handles.par.fname_in,'inspk_aux','-ascii');
[clu, tree] = run_cluster(handles);
[temp] = find_temp(tree,handles);
%DEFINE CLUSTERS
class1=find(clu(temp,3:end)==0);
class2=find(clu(temp,3:end)==1);
class3=find(clu(temp,3:end)==2);
class4=find(clu(temp,3:end)==3);
class5=find(clu(temp,3:end)==4);
class0=setdiff(1:size(spikes,1), sort([class1 class2 class3 class4 class5]));
else
% GOES FOR TEMPLATE MATCHING IF TOO MANY SPIKES.
if size(spikes,1)> handles.par.max_spk;
% random selection of spikes for SPC
ipermut = randperm(length(inspk));
ipermut(naux+1:end) = [];
inspk_aux = inspk(ipermut,:);
else
ipermut = randperm(size(inspk,1));
inspk_aux = inspk(ipermut,:);
end
%INTERACTION WITH SPC
save(handles.par.fname_in,'inspk_aux','-ascii');
[clu, tree] = run_cluster(handles);
[temp] = find_temp(tree,handles);
%DEFINE CLUSTERS
class1=ipermut(find(clu(temp,3:end)==0));
class2=ipermut(find(clu(temp,3:end)==1));
class3=ipermut(find(clu(temp,3:end)==2));
class4=ipermut(find(clu(temp,3:end)==3));
class5=ipermut(find(clu(temp,3:end)==4));
class0=setdiff(1:size(spikes,1), sort([class1 class2 class3 class4 class5]));
end
% IF TEMPLATE MATCHING WAS DONE, THEN FORCE
if (size(spikes,1)> handles.par.max_spk | ...
(handles.par.force_auto == 'y'));
classes = zeros(size(spikes,1),1);
if length(class1)>=handles.par.min_clus; classes(class1) = 1; end
if length(class2)>=handles.par.min_clus; classes(class2) = 2; end
if length(class3)>=handles.par.min_clus; classes(class3) = 3; end
if length(class4)>=handles.par.min_clus; classes(class4) = 4; end
if length(class5)>=handles.par.min_clus; classes(class5) = 5; end
f_in = spikes(classes~=0,:);
f_out = spikes(classes==0,:);
class_in = classes(find(classes~=0),:);
class_out = force_membership_wc(f_in, class_in, f_out, handles);
classes(classes==0) = class_out;
class0=find(classes==0);
class1=find(classes==1);
class2=find(classes==2);
class3=find(classes==3);
class4=find(classes==4);
class5=find(classes==5);
end
%PLOTS
clf
clus_pop = [];
ylimit = [];
subplot(3,5,11)
temperature=handles.par.mintemp+temp*handles.par.tempstep;
switch handles.par.temp_plot
case 'lin'
plot([handles.par.mintemp handles.par.maxtemp-handles.par.tempstep], ...
[handles.par.min_clus handles.par.min_clus],'k:',...
handles.par.mintemp+(1:handles.par.num_temp)*handles.par.tempstep, ...
tree(1:handles.par.num_temp,5:size(tree,2)),[temperature temperature],[1 tree(1,5)],'k:')
case 'log'
semilogy([handles.par.mintemp handles.par.maxtemp-handles.par.tempstep], ...
[handles.par.min_clus handles.par.min_clus],'k:',...
handles.par.mintemp+(1:handles.par.num_temp)*handles.par.tempstep, ...
tree(1:handles.par.num_temp,5:size(tree,2)),[temperature temperature],[1 tree(1,5)],'k:')
end
subplot(3,5,6)
hold on
cluster=zeros(nspk,2);
cluster(:,2)= index';
num_clusters = length(find([length(class1) length(class2) length(class3)...
length(class4) length(class5) length(class0)] >= handles.par.min_clus));
clus_pop = [clus_pop length(class0)];
if length(class0) > handles.par.min_clus;
subplot(3,5,6);
max_spikes=min(length(class0),handles.par.max_spikes);
plot(spikes(class0(1:max_spikes),:)','k');
xlim([1 size(spikes,2)]);
subplot(3,5,10);
hold on
plot(spikes(class0(1:max_spikes),:)','k');
plot(mean(spikes(class0,:),1),'c','linewidth',2)
xlim([1 size(spikes,2)]);
title('Cluster 0','Fontweight','bold')
subplot(3,5,15)
xa=diff(index(class0));
[n,c]=hist(xa,0:1:100);
bar(c(1:end-1),n(1:end-1))
xlim([0 100])
xlabel([num2str(sum(n(1:3))) ' in < 3ms'])
title([num2str(length(class0)) ' spikes']);
end
if length(class1) > handles.par.min_clus;
clus_pop = [clus_pop length(class1)];
subplot(3,5,6);
max_spikes=min(length(class1),handles.par.max_spikes);
plot(spikes(class1(1:max_spikes),:)','b');
xlim([1 size(spikes,2)]);
subplot(3,5,7);
hold
plot(spikes(class1(1:max_spikes),:)','b');
plot(mean(spikes(class1,:),1),'k','linewidth',2)
xlim([1 size(spikes,2)]);
title('Cluster 1','Fontweight','bold')
ylimit = [ylimit;ylim];
subplot(3,5,12)
xa=diff(index(class1));
[n,c]=hist(xa,0:1:100);
bar(c(1:end-1),n(1:end-1))
xlim([0 100])
% set(get(gca,'children'),'facecolor','b','linewidth',0.01)
xlabel([num2str(sum(n(1:3))) ' in < 3ms'])
title([num2str(length(class1)) ' spikes']);
cluster(class1(:),1)=1;
end
if length(class2) > handles.par.min_clus;
clus_pop = [clus_pop length(class2)];
subplot(3,5,6);
max_spikes=min(length(class2),handles.par.max_spikes);
plot(spikes(class2(1:max_spikes),:)','r');
xlim([1 size(spikes,2)]);
subplot(3,5,8);
hold
plot(spikes(class2(1:max_spikes),:)','r');
plot(mean(spikes(class2,:),1),'k','linewidth',2)
xlim([1 size(spikes,2)]);
title('Cluster 2','Fontweight','bold')
ylimit = [ylimit;ylim];
subplot(3,5,13)
xa=diff(index(class2));
[n,c]=hist(xa,0:1:100);
bar(c(1:end-1),n(1:end-1))
xlim([0 100])
% set(get(gca,'children'),'facecolor','r','linewidth',0.01)
xlabel([num2str(sum(n(1:3))) ' in < 3ms'])
cluster(class2(:),1)=2;
title([num2str(length(class2)) ' spikes']);
end
if length(class3) > handles.par.min_clus;
clus_pop = [clus_pop length(class3)];
subplot(3,5,6);
max_spikes=min(length(class3),handles.par.max_spikes);
plot(spikes(class3(1:max_spikes),:)','g');
xlim([1 size(spikes,2)]);
subplot(3,5,9);
hold
plot(spikes(class3(1:max_spikes),:)','g');
plot(mean(spikes(class3,:),1),'k','linewidth',2)
xlim([1 size(spikes,2)]);
title('Cluster 3','Fontweight','bold')
ylimit = [ylimit;ylim];
subplot(3,5,14)
xa=diff(index(class3));
[n,c]=hist(xa,0:1:100);
bar(c(1:end-1),n(1:end-1))
xlim([0 100])
% set(get(gca,'children'),'facecolor','g','linewidth',0.01)
xlabel([num2str(sum(n(1:3))) ' in < 3ms'])
cluster(class3(:),1)=3;
title([num2str(length(class3)) ' spikes']);
end
if length(class4) > handles.par.min_clus;
clus_pop = [clus_pop length(class4)];
subplot(3,5,6);
max_spikes=min(length(class4),handles.par.max_spikes);
plot(spikes(class4(1:max_spikes),:)','c');
xlim([1 size(spikes,2)]);
cluster(class4(:),1)=4;
end
if length(class5) > handles.par.min_clus;
clus_pop = [clus_pop length(class5)];
subplot(3,5,6);
max_spikes=min(length(class5),handles.par.max_spikes);
plot(spikes(class5(1:max_spikes),:)','m');
xlim([1 size(spikes,2)]);
cluster(class5(:),1)=5;
end
% Rescale spike's axis
if ~isempty(ylimit)
ymin = min(ylimit(:,1));
ymax = max(ylimit(:,2));
else
ymin = -200;
ymax = 200;
end
if length(class1) > handles.par.min_clus; subplot(3,5,7); ylim([ymin ymax]); end
if length(class2) > handles.par.min_clus; subplot(3,5,8); ylim([ymin ymax]); end
if length(class3) > handles.par.min_clus; subplot(3,5,9); ylim([ymin ymax]); end
if length(class0) > handles.par.min_clus; subplot(3,5,10); ylim([ymin ymax]); end
subplot(3,1,1)
box off; hold on
% these lines are for plotting continuous data
if continuous_data_av == 1
plot((1:length(xf))/handles.par.sr,xf(1:length(xf)))
if strcmp(handles.par.detection,'pos')
line([0 length(xf)/handles.par.sr],[thr thr],'color','r')
ylim([-thrmax/2 thrmax])
elseif strcmp(handles.par.detection,'neg')
line([0 length(xf)/handles.par.sr],[-thr -thr],'color','r')
ylim([-thrmax thrmax/2])
else
line([0 length(xf)/handles.par.sr],[thr thr],'color','r')
line([0 length(xf)/handles.par.sr],[-thr -thr],'color','r')
ylim([-thrmax thrmax])
end
end;
% end of continuous data plotting
title([pwd '/' char(file_to_cluster)],'Interpreter','none','Fontsize',14)
features_name = handles.par.features;
toc
if print2file==0;
print
else
set(gcf,'papertype','usletter','paperorientation','portrait','paperunits','inches')
set(gcf,'paperposition',[.25 .25 10.5 7.8])
eval(['print -djpeg fig2print_' char(file_to_cluster)]);
end
%SAVE FILES
par = handles.par;
cluster_class = cluster;
outfile=['times_' char(file_to_cluster)];
if handles.par.permut == 'n'
save(outfile, 'cluster_class', 'par', 'spikes', 'inspk')
else
save(outfile, 'cluster_class', 'par', 'spikes', 'inspk', 'ipermut')
end
numclus=length(clus_pop)-1;
outfileclus='cluster_results.txt';
fout=fopen(outfileclus,'at+');
fprintf(fout,'%s\t %s\t %g\t %d %g\t', char(file_to_cluster), features_name, temperature, numclus, handles.par.stdmin);
for ii=1:numclus
fprintf(fout,'%d\t',clus_pop(ii));
end
fprintf(fout,'%d\n',clus_pop(end));
fclose(fout);
clear spikes; clear inspk; clear cluster_class
end