forked from csn-le/wave_clus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wave_clus.m
1406 lines (1214 loc) · 46.1 KB
/
wave_clus.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function varargout = wave_clus(varargin)
% WAVE_CLUS M-file for wave_clus.fig
% WAVE_CLUS, by itself, creates a new WAVE_CLUS or raises the existing
% singleton*.
%
% H = WAVE_CLUS returns the handle to a new WAVE_CLUS or the handle to
% the existing singleton*.
%
% WAVE_CLUS('Property','Value',...) creates a new WAVE_CLUS using the
% given property value pairs. Unrecognized properties are passed via
% varargin to wave_clus_OpeningFcn. This calling syntax produces a
% warning when there is an existing singleton*.
%
% WAVE_CLUS('CALLBACK') and WAVE_CLUS('CALLBACK',hObject,...) call the
% local function named CALLBACK in WAVE_CLUS.M with the given input
% arguments.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help wave_clus
% Last Modified by GUIDE v2.5 14-Jan-2009 10:01:11
% JMG101208
% USER_DATA DEFINITIONS
% USER_DATA{1} = par;
% USER_DATA{2} = spikes;
% USER_DATA{3} = index;
% USER_DATA{4} = clu;
% USER_DATA{5} = tree;
% USER_DATA{7} = inspk;
% USER_DATA{6} = classes(:)'
% USER_DATA{8} = temp
% USER_DATA{9} = classes(:)', backup for non-forced classes
% USER_DATA{10} = clustering_results
% USER_DATA{11} = clustering_results_bk
% USER_DATA{12} = ipermut, indexes of the previously permuted spikes for clustering taking random number of points
% USER_DATA{13} = forced, boolean vector for forced spikes,
% USER_DATA{14} = forced_bk, backup boolean vector for forced spikes,
% USER_DATA{15} = rejected, boolean vector for rejected spikes. Vector only used in develop mode.
% USER_DATA{16} = rejected_bk, backup boolean vector for rejected spikes. Vector only used in develop mode.
% USER_DATA{17} - USER_DATA{19}, for future changes
% USER_DATA{20} - USER_DATA{42}, fix clusters
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @wave_clus_OpeningFcn, ...
'gui_OutputFcn', @wave_clus_OutputFcn, ...
'gui_LayoutFcn', [], ...
'gui_Callback', []);
if nargin>1 && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
function Close_requestFcn(hObject, handles)
%wave_clus('Close_requestFcn',gcbo,guidata(gcbo))
aux = findobj(0, 'type', 'figure','tag','wave_clus_aux');
if ~isempty(aux)
delete(aux)
end
for i=1:5
aux = findobj(0, 'type', 'figure','tag',['wave_clus_aux' num2str(i)]);
if ~isempty(aux)
delete(aux)
end
end
delete(hObject)
% --- Executes just before wave_clus is made visible.
function wave_clus_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin unrecognized PropertyName/PropertyValue pairs from the
% command line (see VARARGIN)
% Choose default command line output for wave_clus
handles.output = hObject;
set(handles.isi1_accept_button,'value',1);
set(handles.isi2_accept_button,'value',1);
set(handles.isi3_accept_button,'value',1);
set(handles.spike_shapes_button,'value',1);
set(handles.plot_all_button,'value',1);
set(handles.plot_average_button,'value',0);
set(handles.fix1_button,'value',0);
set(handles.fix2_button,'value',0);
set(handles.fix3_button,'value',0);
% Update handles structure
guidata(hObject, handles);
if nargin>3 && ischar(varargin{1})
load_data_button_Callback('w_arg',varargin{1},handles)
end
% UIWAIT makes wave_clus wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = wave_clus_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
clus_colors = [[0.0 0.0 1.0];[1.0 0.0 0.0];[0.0 0.5 0.0];[0.620690 0.0 0.0];[0.413793 0.0 0.758621];[0.965517 0.517241 0.034483];
[0.448276 0.379310 0.241379];[1.0 0.103448 0.724138];[0.545 0.545 0.545];[0.586207 0.827586 0.310345];
[0.965517 0.620690 0.862069];[0.620690 0.758621 1.]];
set(0,'DefaultAxesColorOrder',clus_colors)
% --- Executes on button press in load_data_button.
function load_data_button_Callback(hObject, eventdata, handles)
% select file
if ischar(eventdata)
if isempty(fileparts(eventdata))
filename = eventdata;
pathname = [pwd filesep];
else
[pathname filename ext]=fileparts(eventdata);
pathname = [pathname filesep];
filename = [filename ext];
end
hObject = handles.load_data_button;
else
[filename, pathname] = uigetfile('*.*','Select file'); % Use only the supported extensions can bring case-sensitive related problems.
end
% if any file was selected, cancel the loading
if ~ischar(pathname)
return
end
set(handles.file_name,'string',['Loading: ' pathname filename]); drawnow
cla(handles.cont_data);
clear functions % reset functions, force to reload set_parameters next
handles.par = set_parameters();
%cd(pathname);
handles.par.filename = [pathname filename];
for i = 1:3
si = num2str(i);
set(eval(['handles.isi' si '_accept_button']),'value',1);
set(eval(['handles.isi' si '_reject_button']),'value',0);
set(eval(['handles.fix' si '_button']),'value',0);
eval(['set(handles.isi' si '_nbins,''string'',handles.par.nbins);']);
eval(['set(handles.isi' si '_bin_step,''string'',handles.par.bin_step);']);
end
set(handles.isi0_nbins,'string',handles.par.nbins);
set(handles.isi0_bin_step,'string',handles.par.bin_step);
% Sets to zero fix buttons from aux figures
for i=4:33
eval(['handles.par.fix' num2str(i) '=0;'])
end
for i =0:33
si = num2str(i);
eval(['handles.par.nbins' si ' = handles.par.nbins;']); % # of bins for the ISI histograms
eval(['handles.par.bin_step' si ' = handles.par.bin_step;']); % percentage number of bins to plot
end
data_handler = readInData(handles.par);
handles.par = data_handler.par;
tmpParts = split(data_handler.nick_name, 'Ch');
ChNum = tmpParts{end};
handles.par.file_path = data_handler.file_path;
handles.par.fname_in = [data_handler.file_path filesep 'tmp_data_wc' ChNum]; % temporary filename used as input for SPC
handles.par.fname = ['data_' data_handler.nick_name];
handles.par.nick_name = data_handler.nick_name;
handles.par.fnamesave = fullfile(data_handler.file_path, handles.par.fname); %filename if "save clusters" button is pressed
handles.par.fnamespc = fullfile(data_handler.file_path, ['data_wc' ChNum]);
% Not sure if this is the proper handling, but it seems like this variable
% being populated prevents the 'spikes' variable from being saved when
% hitting save.
if ~isempty(data_handler.spikes_file)
handles.par.spikes_file = fullfile(data_handler.file_path, data_handler.spikes_file);
else
handles.par.spikes_file = data_handler.spikes_file;
end
handles.par = data_handler.update_par(handles.par);
check_WC_params(handles.par)
if data_handler.with_results %data have _times files
[clu, tree, spikes, index, inspk, ipermut, classes, forced,temp] = data_handler.load_results();
rejected = data_handler.load_rejected();
handles.setclus = 1;
if isempty(ipermut)
handles.par.permut = 'n';
end
%Create Index
nspk = size(spikes,1);
nspk_ind = (1:nspk)';
inspk_ind_masked = nspk_ind;
else
if data_handler.with_spikes %data have some time of _spikes files
[spikes, index] = data_handler.load_spikes();
if ~data_handler.with_wc_spikes
[spikes] = spike_alignment(spikes,handles.par);
end
else
set(handles.file_name,'string','Detecting spikes ...'); drawnow
index = [];
spikes = [];
temp_aux_th_vect = [];
for n = 1:data_handler.max_segments
x = data_handler.get_segment();
[new_spikes, temp_aux_th, new_index] = amp_detect(x, handles.par);
index = [index data_handler.index2ts(new_index)]; % new_index to ms
spikes = [spikes; new_spikes];
temp_aux_th_vect = [temp_aux_th_vect; temp_aux_th];
end
if strcmp(handles.par.detection,'neg')
handles.par.threshold = mean(temp_aux_th_vect) * -1;
else
handles.par.threshold = mean(temp_aux_th_vect);
end
handles.par.detection_date = datestr(now);
end
%If you have too many spikes, use 1:max_spk
nspk = size(spikes,1);
nspk_ind = (1:nspk)';
%naux = min(handles.par.max_spk,size(nspk,1));
%nspk is a single number, which means this comes out to 1. Try below.
naux = min(handles.par.max_spk,nspk);
if size(spikes,1) < 15
ME = MException('MyComponent:notEnoughSpikes', 'Less than 15 spikes detected');
throw(ME)
end
set(handles.file_name,'string','Calculating spike features ...'); drawnow
[inspk] = wave_features(spikes,handles.par); %Extract spike features.
handles.par.inputs = size(inspk,2); % number of inputs to the clustering
% Spike amplitudes below some threshold are excluded from clustering.
if handles.par.clusThr == 'y'
spikePeaks = max(abs(spikes),[],2);
spikeMask = spikePeaks > 60; %Get this variable from parameters or spikes, whichever we can add it to most easily.
inspk_masked = inspk(spikeMask,:);
inspk_ind_masked = nspk_ind(spikeMask,:);
if sum(spikeMask) < naux
naux = sum(spikeMask);
end
else
inspk_masked = inspk;
inspk_ind_masked = nspk_ind;
end
if handles.par.permut == 'y'
ipermut = randperm(length(inspk_masked));
if handles.par.match == 'y' && handles.par.max_spk < length(inspk)
ipermut(naux+1:end) = [];
end
inspk_masked = inspk_masked(ipermut,:);
inspk_ind_masked = inspk_ind_masked(ipermut,:);
else
if handles.par.match == 'y' && handles.par.max_spk < length(inspk)
inspk_aux = inspk_masked(1:naux,:); % take first 'par.max_spk' spikes as an input for SPC
inspk_ind_masked = inspk_ind_masked(1:naux,:);
else
inspk_aux = inspk_masked;
inspk_ind_masked = inspk_ind_masked;
end
end
%Package spikes for SPC
save(handles.par.fname_in,'inspk_aux','-ascii');
%Interaction with SPC
set(handles.file_name,'string','Running SPC ...'); drawnow
[clu,tree] = run_cluster(handles.par);
forced = false(size(spikes,1) ,1);
rejected = false(1, size(spikes,1));
handles.setclus = 2; %uses min cluster size but doesn't reset force
end
handles.par.file_name_to_show = [pathname filename];
if (data_handler.with_raw || data_handler.with_psegment) && handles.par.cont_segment %raw exists
[xd_sub, sr_sub] = data_handler.get_signal_sample();
handles = Plot_continuous_data(xd_sub, sr_sub, handles); drawnow
clear xd_sub
end
%Fixing lost elements of clu . Skiped elements will be class -1 because in
%all the uses of clu are like: clu(temp,3:end)+1
if handles.par.permut == 'y' && ~isempty(clu)
if isempty(ipermut) %load from old result without ipermut or par, but par.permut=='y'
naux = size(clu,2)-2;
ipermut = 1:naux;
end
clu_aux = zeros(size(clu,1),2 + size(spikes,1)) -1; %when update classes from clu, not selected elements go to cluster 0
clu_aux(:,ipermut+2) = clu(:,(1:length(ipermut))+2);
clu_aux(:,1:2) = clu(:,1:2);
clu = clu_aux;
clear clu_aux
%Make a matching manipulation to the Index vector.
inspk_ind_masked = [0; 0; inspk_ind_masked]'; %Clu is mostly columns with 2 added to the front, the input was rows.
clu_ind = zeros(1, 2 + size(spikes,1));
clu_ind(:,ipermut+2) = inspk_ind_masked(:,(1:length(ipermut))+2);
clu_ind(:,1:2) = inspk_ind_masked(:,1:2);
inspk_ind_masked = clu_ind;
clear clu_ind
elseif ~isempty(clu)
naux = size(clu,2)-2;
clu_aux = zeros(size(clu,1),2 + size(spikes,1)) -1; %when update classes from clu, not selected elements go to cluster 0
clu_aux(:,(1:naux)+2) = clu(:,(1:naux)+2);
clu_aux(:,1:2) = clu(:,1:2);
clu = clu_aux;
clear clu_aux
%Make a matching manipulation to the Index vector.
inspk_ind_masked = [0; 0; inspk_ind_masked]'; %Clu is mostly columns with 2 added to the front, the input was rows.
clu_ind = zeros(1, 2 + size(spikes,1));
clu_ind(:,(1:naux)+2) = inspk_ind_masked(:,(1:naux)+2);
clu_ind(:,1:2) = inspk_ind_masked(:,1:2);
inspk_ind_masked = clu_ind;
clear clu_ind
end
%If we set a threshold, resort the members of clu back to their proper
%place.
if ~data_handler.with_results % This doesn't need to be done on preprocessed data.
if handles.par.clusThr == 'y'
spikeMask = find(spikeMask);
clu_aux = zeros(size(clu))-1;
clu_aux(:,spikeMask+2) = clu(:,(1:length(spikeMask))+2);
clu_aux(:,1:2) = clu(:,1:2);
clu = clu_aux;
clear clu_aux
%Complementary Index step
clu_ind = zeros(size(inspk_ind_masked));
clu_ind(:,spikeMask+2) = inspk_ind_masked(:,(1:length(spikeMask))+2);
clu_ind(:,1:2) = inspk_ind_masked(:,1:2);
inspk_ind_masked = clu_ind;
clear clu_ind
end
%Check if the index is correctly recreated correctly. The numbers should
%match their indicies (minus 2).
matchInd = (find(inspk_ind_masked(3:end)) == inspk_ind_masked(inspk_ind_masked ~= 0));
if size(inspk_masked,1)> handles.par.max_spk
matchInd = matchInd(1:handles.par.max_spk);
end
assert(sum(matchInd) == length(matchInd));
end
USER_DATA = get(handles.wave_clus_figure,'userdata');
USER_DATA{1} = handles.par;
USER_DATA{2} = spikes;
USER_DATA{3} = index;
USER_DATA{4} = clu;
USER_DATA{5} = tree;
USER_DATA{7} = inspk;
if exist('ipermut','var') && ~isempty(ipermut)
USER_DATA{12} = ipermut;
end
USER_DATA{13} = forced;
USER_DATA{14} = forced;
USER_DATA{15} = rejected; %the clusters numbers are sorted
USER_DATA{16} = rejected; %the clusters numbers are sorted
set(handles.min_clus_edit,'string',num2str(handles.par.min_clus));
setappdata(handles.temperature_plot,'auto_sort_info',[]);
if data_handler.with_gui_status
[saved_gui_status, current_temp,auto_sort_info] = data_handler.get_gui_status();
setappdata(handles.temperature_plot,'auto_sort_info',auto_sort_info);
clustering_results = zeros(length(classes),4);
clustering_results(:,1) = repmat(current_temp,length(classes),1);
for i=1:max(classes)
clustering_results(classes==i,3) = temp(i);
end
clustering_results(:,2) = classes'; % GUI classes
clustering_results(:,4) = saved_gui_status;
handles.undo = 1;
elseif data_handler.with_results
current_temp = 1;
clustering_results(:,1) = repmat(current_temp,length(classes),1); % GUI temperatures
clustering_results(:,2) = classes'; % GUI classes
if exist('temp','var')
for i=1:max(classes)
clustering_results(classes==i,3) = temp(i);
end
else
for i=1:max(classes)
clustering_results(classes==i,3) = current_temp;
end
end
clustering_results(:,4) = classes'; % original classes
handles.undo = 1;
else
%Selects temperature.
[clust_num temp auto_sort] = find_temp(tree,clu, handles.par);
current_temp = max(temp);
classes = zeros(1,size(clu,2)-2);
for c =1: length(clust_num)
aux = clu(temp(c),3:end) +1 == clust_num(c);
classes(aux) = c;
end
if handles.par.permut == 'n'
classes = [classes zeros(1,max(size(spikes,1)-size(clu,2)-2,0))];
end
Temp = [];
% Classes should be consecutive numbers
classes_names = nonzeros(sort(unique(classes)));
for i= 1:length(classes_names)
c = classes_names(i);
if c~= i
classes(classes == c) = i;
end
Temp(i) = temp(i);
end
setappdata(handles.temperature_plot,'auto_sort_info',auto_sort);
% definition of clustering_results
clustering_results = [];
clustering_results(:,1) = repmat(current_temp,length(classes),1); % GUI temperatures
clustering_results(:,2) = classes'; % GUI classes
for i=1:max(classes)
clustering_results(classes==i,3) = temp(i);
clustering_results(classes==i,4) = clust_num(i); % original classes
end
handles.undo = 0;
end
if data_handler.with_results && ~ data_handler.with_spc
temp = -1; % This will work as a flag for not drawing the temperature diagram
cla(handles.temperature_plot,'reset');
end
clustering_results(:,5) = repmat(handles.par.min_clus,length(classes),1); % minimum number of clusters
USER_DATA{6} = classes(:)';
if exist('current_temp','var')
USER_DATA{8} = current_temp;
else
USER_DATA{8} = temp(1);
end
USER_DATA{10} = clustering_results;
USER_DATA{11} = clustering_results;
handles.force = 0;
handles.merge = 0;
handles.minclus = handles.par.min_clus;
set(handles.wave_clus_figure,'userdata',USER_DATA);
clear clustering_results classes rejected spikes
% mark clusters when new data is loaded
guidata(hObject, handles); %this is need for plot the isi histograms
plot_spikes(handles); %This function edits userdata
USER_DATA = get(handles.wave_clus_figure,'userdata');
set(handles.wave_clus_figure,'userdata',USER_DATA);
if isfield(handles,'force_unforce_button') && (nnz(forced)>0)
set(handles.force_unforce_button,'Value',1)
set(handles.force_unforce_button,'String','FORCED')
%set(handles.change_temperature_button,'enable','off');
elseif isfield(handles,'force_unforce_button')
set(handles.force_unforce_button,'Value',0)
set(handles.force_unforce_button,'String','Force')
end
if isfield(handles,'edit_max_force_dist')
set(handles.edit_max_force_dist,'string',num2str(handles.par.template_sdnum));
end
set(handles.file_name,'string',handles.par.file_name_to_show);
% --- Executes on button press in change_temperature_button.
function change_temperature_button_Callback(hObject, eventdata, handles)
%gets the mouse input
if exist('impoint','class')
point = impoint(handles.temperature_plot);
if isempty(point)
return
end
pos = point.getPosition();
temp = pos(1);
aux = pos(2);
point.delete()
else
[temp,aux,button] = ginput(1);
if button == 3
return
end
end
if temp <= handles.par.mintemp+0.5*handles.par.tempstep
temp = 1;
elseif temp>handles.par.maxtemp
temp = floor((handles.par.maxtemp-handles.par.mintemp)/handles.par.tempstep);
else
temp = round((temp-handles.par.mintemp)/handles.par.tempstep); %temp from value to index
end
min_clus = round(aux);
set(handles.min_clus_edit,'string',num2str(min_clus));
USER_DATA = get(handles.wave_clus_figure,'userdata');
rejected = USER_DATA{15};
USER_DATA{16} = rejected;
clu = USER_DATA{4};
classes = clu(temp, 3:end) + 1;
classes(rejected) = 0;
USER_DATA{6} = classes(:)';
USER_DATA{8} = temp;
handles.minclus = min_clus;
set(handles.wave_clus_figure,'userdata',USER_DATA);
handles.setclus = 0;
handles.force = 0;
handles.merge = 0;
handles.undo = 0;
handles.new_spc_classes = classes(:)';
plot_spikes(handles);
if isfield(handles,'force_unforce_button')
set(handles.force_unforce_button,'Value',0)
set(handles.force_unforce_button,'String','Force')
end
% --- Change min_clus_edit
function min_clus_edit_Callback(hObject, eventdata, handles)
USER_DATA = get(handles.wave_clus_figure,'userdata');
min_clus = str2num(get(hObject, 'String'));
clu = USER_DATA{4};
temp = USER_DATA{8};
classes = clu(temp,3:end)+1;
USER_DATA{6} = classes(:)';
USER_DATA{16} = USER_DATA{15};
clustering_results = USER_DATA{10};
clustering_results(:,5) = min_clus;
set(handles.wave_clus_figure,'userdata',USER_DATA);
handles.setclus = 0;
handles.force = 0;
handles.merge = 0;
handles.undo = 0;
handles.minclus = min_clus;
plot_spikes(handles);
% --- Executes on button press in save_clusters_button.
function save_clusters_button_Callback(hObject, eventdata, handles, developer_mode)
USER_DATA = get(handles.wave_clus_figure,'userdata');
classes = USER_DATA{6};
clustering_results = USER_DATA{10};
rejected = USER_DATA{15};
USER_DATA{16} = rejected; %update bk of rejected spikes
USER_DATA{15} = false(size(rejected));
set(handles.wave_clus_figure,'userdata',USER_DATA);
handles.setclus = 1;
handles.force = 0;
handles.merge = 0;
handles.undo = 0;
plot_spikes(handles)
USER_DATA = get(handles.wave_clus_figure,'userdata');
spikes = USER_DATA{2};
used_par = USER_DATA{1};
index = USER_DATA{3};
classes = USER_DATA{6};
gui_classes_data = USER_DATA{10};
% Classes should be consecutive numbers
classes_names = nonzeros(sort(unique(classes)));
for i= 1:length(classes_names)
c = classes_names(i);
if c~= i
classes(classes == c) = i;
end
end
%Saves clusters
cluster_class = zeros(size(spikes,1),2);
cluster_class(:,1) = classes(:);
cluster_class(:,2) = USER_DATA{3}';
[fpath, ~, ~] = fileparts(handles.file_name.String);
outfile = fullfile(fpath, ['times_' used_par.nick_name]);
par = struct;
par = update_parameters(par,used_par,'relevant');
par = update_parameters(par,used_par,'batch_plot');
par.sorting_date = datestr(now);
if isfield(par, 'threshold')
threshold = par.threshold; % Preserve similarity to batch file output.
elseif isfield(used_par, 'thr')
threshold = used_par.thr; % Preserve similarity to batch file output.
end
gui_status = struct();
gui_status.current_temp = gui_classes_data(1,1);
gui_status.original_classes = gui_classes_data(1:end,4);
gui_status.auto_sort_info = getappdata(handles.temperature_plot,'auto_sort_info');
Temp = zeros(length(classes_names),1);
for i = 1:length(classes_names)
Temp(i) = gui_classes_data(find(classes==i,1,'first'),3);
end
forced = USER_DATA{13};
var_list = 'cluster_class'',''par'',''spikes'',''gui_status'', ''forced'', ''Temp'',''threshold''';
if ~isempty(USER_DATA{7})
inspk = USER_DATA{7};
var_list = strcat(var_list , ' ,''inspk''');
end
if ~isempty(USER_DATA{12})
ipermut = USER_DATA{12};
var_list = strcat(var_list , ' ,''ipermut''');
end
if developer_mode
var_list = strcat(var_list , ' ,''rejected''');
end
if isempty(handles.par.spikes_file)
var_list = strcat(var_list , ' ,''spikes''');
else
spikes_file = handles.par.spikes_file;
var_list = strcat(var_list , ' ,''spikes_file''');
end
ver = '';
currentver = version;
if currentver(1) >= 7
ver = ',''-v6''';
end
try
eval(['save( ''' outfile ''',''' var_list '' ver ');']);
catch
eval(['save( ''' outfile ''',''' var_list ',''-v7.3'');']);
end
if exist([handles.par.fnamespc '.dg_01.lab'],'file')
movefile([handles.par.fnamespc '.dg_01.lab'], [handles.par.fnamesave '.dg_01.lab'], 'f');
movefile([handles.par.fnamespc '.dg_01'], [handles.par.fnamesave '.dg_01'], 'f');
end
%Save figures
fig_names = {'figure','aux','aux1','aux2','aux3','aux4','aux5'};
file_names = {'','a','b','c','d','e','f'};
h_figs = get(0,'children');
for i=1:length(fig_names)
h_fig = findobj(h_figs,'tag',['wave_clus_' fig_names{i}]);
new_file_name = [fpath filesep 'fig2print_' used_par.nick_name file_names{i} '.png'];
if ~isempty(h_fig)
figure(h_fig); set(gcf, 'PaperUnits', 'inches', 'PaperType', 'A4', 'PaperPositionMode', 'auto','PaperOrientation','portrait');
print(h_fig,'-dpng',new_file_name,'-r300');
else
if exist(new_file_name, 'file')==2
delete(new_file_name);
end
end
end
set(hObject,'value',0);
% --- Executes on button press in set_parameters_button.
function set_parameters_button_Callback(hObject, eventdata, handles)
%helpdlg('Check the set_parameters files in the subdirectory
%Wave_clus\Parameters_files');
set_parameters_ui()
function force_unforce_button_Callback(hObject, eventdata, handles)
USER_DATA = get(handles.wave_clus_figure,'userdata');
par = USER_DATA{1};
classes = USER_DATA{6};
forced = USER_DATA{13};
USER_DATA{14} = forced; %save forced in forced_bk
rejected = USER_DATA{15};
USER_DATA{16} = USER_DATA{15}; %update bk of rejected spikes
button_state = get(hObject,'Value');
if button_state == get(hObject,'Max')
spikes = USER_DATA{2};
inspk = USER_DATA{7};
USER_DATA{16} = rejected;
if get(handles.fix1_button,'value') ==1
fix_class = USER_DATA{20}';
classes(fix_class) = -1;
end
if get(handles.fix2_button,'value') ==1
fix_class = USER_DATA{21}';
classes(fix_class) = -1;
end
if get(handles.fix3_button,'value') ==1
fix_class = USER_DATA{22}';
classes(fix_class )= -1;
end
% Get fixed clusters from aux figures
for i=4:33
eval(['fixx = par.fix' num2str(i) ';']);
if fixx == 1
fix_class = USER_DATA{22+i-3}';
classes(fix_class) = -1;
end
end
to_force = (~rejected)' & (classes==0);
switch par.force_feature
case 'spk'
f_in = spikes(classes>0,:);
f_out = spikes(to_force,:);
case 'wav'
if isempty(inspk)
set(handles.file_name,'string','Calculating spike features ...');
[inspk] = wave_features(spikes,par); % Extract spike features.
USER_DATA{7} = inspk;
end
f_in = inspk(classes>0,:);
f_out = inspk(to_force,:);
end
class_in = classes(classes>0);
class_out = force_membership_wc(f_in, class_in, f_out, par);
forced = forced(:) | to_force(:);
classes(to_force) = class_out;
USER_DATA{13} = forced;
clustering_results = USER_DATA{10};
handles.minclus = clustering_results(1,5);
handles.force = 1;
handles.setclus = 1;
set(hObject,'String','FORCED')
%set(handles.change_temperature_button,'enable','off');
else
new_forced = zeros(size(forced));
% Fixed clusters are not considered for forcing
set(handles.fix1_button,'value',0);
set(handles.fix2_button,'value',0);
set(handles.fix3_button,'value',0);
for i=4:33
eval(['par.fix' num2str(i) '=0;']);
end
classes(forced(:) & (~new_forced(:)) & (~rejected(:))) = 0; %the elements that before were forced but it isn't force any more, pass to class 0
USER_DATA{13} = new_forced;
handles.force = 0;
handles.setclus = 1;
handles.unforce = true;
set(hObject,'String','Force')
%set(handles.change_temperature_button,'enable','on');
end
USER_DATA{6} = classes(:)';
handles.merge = 0;
handles.undo = 0;
set(handles.wave_clus_figure,'userdata',USER_DATA)
plot_spikes(handles);
function manual_clus_button_Callback(hObject, eventdata,handles_local, cl)
set(hObject,'Enable','off')
try
if isfield(handles_local,'wave_clus_figure')
USER_DATA = get(handles_local.wave_clus_figure,'userdata');
handles = handles_local;
else
h_figs = get(0,'children');
h_fig = findobj(h_figs,'tag','wave_clus_figure');
USER_DATA = get(h_fig,'UserData');
handles = guidata(h_fig);
end
spikes = USER_DATA{2};
classes = USER_DATA{6};
forced = USER_DATA{13};
if cl == -1
current_ax = handles_local.projections;
valids = ~USER_DATA{15}(:); %First, I don't select the rejected
else
eval(['current_ax = handles_local.spikes' num2str(cl) ';']);
valids = ~USER_DATA{15}(:) & (classes(:)==cl); %First, I don't select the rejected
end
if exist('getrect','file')
rect = getrect(current_ax);
else
aux_rec = imrect(current_ax);
rect = aux_rec.getPosition;
aux_rec.delete()
end
if rect(1) > size(spikes,2) || (rect(1) + rect(3))<1 %if the rect is totally outside the axis
set(hObject,'Enable','on');
set(hObject,'value',0);
return;
end
yborders = ylim(current_ax);
if rect(2)<yborders(1)
ymin = -inf;
else
ymin = rect(2);
end
if rect(2) + rect(4)>yborders(2)
ymax = inf;
else
ymax = rect(2) + rect(4);
end
xind = max(1, ceil(rect(1)));
xend = min(size(spikes,2),floor(rect(1) + rect(3)));
if(rect(3)<2)
if rect(3)==0
set(hObject,'Enable','on');
set(hObject,'value',0);
return;
end
sp_selected = (max(spikes(valids,xind:xend),[],2)>ymin) & (min(spikes(valids,xind:xend),[],2)<ymax);
valids(valids==1) = sp_selected;
else
xD = xend-xind;
yD = ymin - ymax;
if xD==0 || yD == 0
set(hObject,'Enable','on');
set(hObject,'value',0);
return;
end
[Mh, Mpos] = max(spikes(valids,xind:xend)');
[mh ,mpos] = min(spikes(valids,xind:xend)');
if ceil(rect(1)) < 1 %if rect is out the axis, extreme in border count like inside the rectangle
xiborder=0;
else
xiborder=1;
end
if floor(rect(1) + rect(3)) > size(spikes,2) %if rect is out the axis, extreme in border count like inside the rectangle
xeborder = xD+2;
else
xeborder = xD;
end
sp_selected = (Mh >= ymin & Mh <= ymax) & (Mpos > xiborder & Mpos < xeborder);
sp_selected = sp_selected |((mh >= ymin & mh <= ymax) & (mpos > xiborder & mpos < xeborder));
valids(valids==1) = sp_selected;
end
if nnz(valids)==0
set(hObject,'Enable','on');
set(hObject,'value',0);
return;
end
clus_n = max(classes) + 1;
USER_DATA{14} = forced;
forced(valids) = 0;
classes(valids)= clus_n;
handles.new_manual = valids;
handles.setclus = 1;
handles.force = 0;
handles.merge = 0;
handles.undo = 0;
USER_DATA{6} = classes(:)';
USER_DATA{13} = forced;
USER_DATA{16} = USER_DATA{15}; %update bk of rejected spikes
if isfield(handles,'wave_clus_figure')
set(handles.wave_clus_figure,'userdata',USER_DATA)
set(hObject,'Enable','on');
else
set(h_fig,'userdata',USER_DATA)
end
set(hObject,'value',0);
plot_spikes(handles);
catch
set(hObject,'Enable','on');
set(hObject,'value',0);
end
% PLOT ALL PROJECTIONS BUTTON
% --------------------------------------------------------------------
function Plot_all_projections_button_Callback(hObject, eventdata, handles)
USER_DATA = get(handles.wave_clus_figure,'userdata');
par = USER_DATA{1};
if par.channels > 1
Plot_amplitudes(handles)
else
Plot_all_features(handles)
end
% --------------------------------------------------------------------
% fix buttons --------------------------------------------------------------------
function fix_button_Callback(hObject, eventdata, handles,cn)
USER_DATA = get(handles.wave_clus_figure,'userdata');
classes = USER_DATA{6};
fix_class = find(classes== cn);
if isempty(fix_class)
return
end
if get(eval(['handles.fix' num2str(cn) '_button']),'value') ==1
USER_DATA{19+cn} = fix_class; %20 for class 1, etc
else
USER_DATA{19+cn} = [];
end
set(handles.wave_clus_figure,'userdata',USER_DATA)
%SETTING OF SPIKE FEATURES OR PROJECTIONS
% --------------------------------------------------------------------
function spike_shapes_button_Callback(hObject, eventdata, handles)
set(gcbo,'value',1);
set(handles.spike_features_button,'value',0);
USER_DATA = get(handles.wave_clus_figure,'userdata');
cluster_results = USER_DATA{10};
handles.setclus = 1;
handles.force = 0;
handles.merge = 0;
handles.undo = 0;
handles.minclus = cluster_results(1,5);
plot_spikes(handles);
% -------------------------------------------------------------------
function spike_features_button_Callback(hObject, eventdata, handles)
set(gcbo,'value',1);
set(handles.spike_shapes_button,'value',0);
USER_DATA = get(handles.wave_clus_figure,'userdata');
cluster_results = USER_DATA{10};
handles.setclus = 1;
handles.force = 0;
handles.merge = 0;
handles.undo = 0;
handles.minclus = cluster_results(1,5);
plot_spikes(handles);
%SETTING OF SPIKE PLOTS
% --------------------------------------------------------------------
function plot_all_button_Callback(hObject, eventdata, handles)
set(hObject,'value',1);
set(handles.plot_average_button,'value',0);
USER_DATA = get(handles.wave_clus_figure,'userdata');
cluster_results = USER_DATA{10};
handles.setclus = 1;
handles.force = 0;
handles.merge = 0;
handles.undo = 0;
handles.minclus = cluster_results(1,5);
plot_spikes(handles);
% --------------------------------------------------------------------
function plot_average_button_Callback(hObject, eventdata, handles)
set(hObject,'value',1);
set(handles.plot_all_button,'value',0);
USER_DATA = get(handles.wave_clus_figure,'userdata');
cluster_results = USER_DATA{10};
handles.setclus = 1;