-
Notifications
You must be signed in to change notification settings - Fork 0
/
Msort_debloated.m
2629 lines (2136 loc) · 101 KB
/
Msort_debloated.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 = Msort(varargin)
% MSORT MATLAB code for Msort.fig
% MSORT, by itself, creates a new MSORT or raises the existing
% singleton*.
%
% H = MSORT returns the handle to a new MSORT or the handle to
% the existing singleton*.
%
% MSORT('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MSORT.M with the given input arguments.
%
% MSORT('Property','Value',...) creates a new MSORT or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Msort_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Msort_OpeningFcn via varargin.
%
% *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 Msort
% Last Modified by GUIDE v2.5 17-Dec-2014 15:32:38
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Msort_OpeningFcn, ...
'gui_OutputFcn', @Msort_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && 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
% --- Executes just before Msort is made visible.
function Msort_OpeningFcn(hObject, ~, 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 command line arguments to Msort (see VARARGIN)
p=strrep(path,'\','/');
contains_Plex=regexp(p,'Plexon/mexPlex', 'once');
if(isempty(contains_Plex))
Plexon_folder = sprintf('%s\\Plexon',pwd);
mexPlex_folder = sprintf('%s\\Plexon\\mexPlex',pwd);
addpath(Plexon_folder,mexPlex_folder);
end
% determine default data directory
Msort_path=which('Msort.m');
p=regexp(Msort_path,strrep(sprintf('(?<path>.*%c).*.m',filesep),'\','\\'),'names');
param_file=sprintf('%sMsort.cfg',p.path);
params=load_params(param_file);
default_dir = params.default_file_dir;
setappdata(hObject,'default_dir',default_dir);
% Choose default command line output for Msort
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
set_theme(handles); % set palette
warning off all;
% UIWAIT makes Msort wait for user response (see UIRESUME)
% uiwait(handles.fig_msort);
% --- Outputs from this function are returned to the command line.
function varargout = Msort_OutputFcn(~, ~, 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;
set(get(handles.output,'JavaFrame'),'Maximized',1);
% --- Executes on button press in group1_button.
function group1_button_Callback(~, ~, handles) %#ok<DEFNU>
% hObject handle to group1_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
load_group(1, handles);
% --- Executes on button press in group2_button.
function group2_button_Callback(~, ~, handles) %#ok<DEFNU>
% hObject handle to group2_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
load_group(2, handles);
% --- Executes on button press in group3_button.
function group3_button_Callback(~, ~, handles) %#ok<DEFNU>
% hObject handle to group3_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
load_group(3, handles);
% --- Executes on button press in group4_button.
function group4_button_Callback(~, ~, handles) %#ok<DEFNU>
% hObject handle to group4_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
load_group(4, handles);
% --- Executes on button press in group5_button.
function group5_button_Callback(~, ~, handles) %#ok<DEFNU>
% hObject handle to group5_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
load_group(5, handles);
% --- Executes on button press in group6_button.
function group6_button_Callback(~, ~, handles) %#ok<DEFNU>
% hObject handle to group6_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
load_group(6, handles);
% --- Executes on button press in group7_button.
function group7_button_Callback(~, ~, handles) %#ok<DEFNU>
% hObject handle to group7_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
load_group(7, handles);
% --- Executes on button press in group8_button.
function group8_button_Callback(~, ~, handles) %#ok<DEFNU>
% hObject handle to group8_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
load_group(8, handles);
% controls the num_PCs slider & field
function num_PCs_slider_Callback(hObject,~,field,handles)
value=round(get(hObject,'Value'));
% if we're linked, make sure to update all of the values
if(value>get(hObject,'Max')), value=get(hObject,'Max'); end
if(value<get(hObject,'Min')), value=get(hObject,'Min'); end
if(get(handles.link_channels_checkbox,'Value'))
set(handles.num_PCs_field,'String',sprintf('%2.0f',value));
set(handles.num_PCs_slider,'Value',value);
else
set(hObject,'Value',value);
set(field,'String',sprintf('%2.0f',value));
end
% controls the num_PCs slider & field
function num_PCs_field_Callback(hObject,~,slider,handles)
value=str2double(get(hObject,'String'));
if(isnan(value)), value=1; end
if(value>get(slider,'Max')), value=get(hObject,'Max'); end
if(value<get(hObject,'Min')), value=get(hObject,'Min'); end
if(get(handles.link_channels_checkbox,'Value'))
set(handles.num_PCs_field,'String',sprintf('%2.0f',value));
set(handles.num_PCs_slider,'Value',value);
else
set(slider,'Value',value);
set(hObject,'String',sprintf('%2.0f',value));
end
function cluster_checkbox_Callback(hObject,~,row,handles)
if(get(handles.link_channels_checkbox,'Value'))
set(row,'Value',get(hObject,'Value'));
end
% --- Executes on slider movement.
function numPCs_slider_Callback(hObject, ~, handles) %#ok<DEFNU>
% hObject handle to numPCs_slider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
num_PCs = round(get(hObject,'Value'));
if(num_PCs<0)
num_PCs=0;
elseif(num_PCs>8)
num_PCs=8;
end
% sync up slider and field
set(handles.numPCs_field,'String',sprintf('%g',num_PCs));
set(hObject,'Value',num_PCs);
setappdata(handles.output,'num_PCs',num_PCs);
% --- Executes during object creation, after setting all properties.
function numPCs_slider_CreateFcn(hObject, ~, ~) %#ok<DEFNU>
% hObject handle to numPCs_slider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
minVal=0; maxVal=8; % these are defaults
dX=1/(maxVal-minVal);
set(hObject,'SliderStep',[dX dX]);
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function numPCs_field_Callback(hObject, ~, handles) %#ok<DEFNU>
% hObject handle to numPCs_field (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of numPCs_field as text
% str2double(get(hObject,'String')) returns contents of numPCs_field as a double
num_PCs = round(str2double(get(hObject,'String')));
if(num_PCs<1)
num_PCs=1;
elseif(num_PCs>8)
num_PCs=8;
end
setappdata(handles.output,'num_PCs',num_PCs);
% sync up slider and field
set(hObject,'String',sprintf('%g',num_PCs));
set(handles.numPCs_slider,'Value',num_PCs);
% --- Executes during object creation, after setting all properties.
function numPCs_field_CreateFcn(hObject, ~, ~) %#ok<DEFNU>
% hObject handle to numPCs_field (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in return_button.
function return_button_Callback(~, ~, handles)
% hObject handle to return_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clear_panel(handles.features_panel);
clear_panel(handles.channel_panel);
set(handles.output,'WindowKeyPressFcn',@key_press,'WindowKeyReleaseFcn',@ctrl_release);
set(handles.group_cmd_panel,'visible','off');
set(handles.group_panel,'visible','on');
set(handles.feature_control_panel,'visible','off');
%set(handles.features_panel,'backgroundcolor',[.9412 .9412 .9412]);
set(handles.cluster_panel,'visible','off');
set(handles.group_menu,'Value',1);
if(isfield(handles,'cluster_controls'))
delete(handles.cluster_controls(ishandle(handles.cluster_controls)));
end
function loadfile_item_Callback(~, ~, handles,filename,pathname)
%LOADFILE_ITEM_CALLBACK Load .plx or .mat file
% LOADFILE_ITEM_CALLBACK Executes upon the user pressing the Load file button. This loads all metadata from a .mat
% file. If the associated .mat and .xml files are not available, auto-generates .xml and .mat files and populates
% appropriate fields.
%
% Written by Marshall Crumiller
% email: [email protected]
%
% Updates
% 2015-06-03: Created
%-----------------------------------------------------------------------------------------------------------------------
global palette;
set(handles.status_light,'BackgroundColor',palette.red);
set(handles.status_label,'String','Loading file...');
return_button_Callback([],[],handles);
if(~exist('filename','var') && ~exist('pathname','var'))
default_dir=getappdata(handles.output,'default_dir');
[filename,pathname] = uigetfile({'*.plx;*.abf'},'Select the a file.',default_dir);
end
if(~filename)
set(handles.status_label,'String','Please load a .plx file');
set(handles.status_light,'BackgroundColor',palette.green);
return;
end
% parse filename
expr = regexp(filename,'(?<basename>.*)\.(plx|abf|mat)','names');
basename = expr.basename;
setappdata(handles.output,'basename',basename);
setappdata(handles.output,'pathname',pathname);
% look for PLX file
%plx_file = sprintf('%s%s.plx',pathname,basename);
plx_file=sprintf('%s%s',pathname,filename);
setappdata(handles.output,'plx_file',plx_file);
% look for XML file
xml_file = sprintf('%s%s.xml',pathname,basename);
if(~exist(xml_file,'file'))
% generate XML file
set(handles.status_label,'String','Generating associated .xml file...');
switch(filename(end-2:end))
case 'plx'
generate_standard_Plexon_xml(plx_file);
case 'abf'
generate_standard_ABF_xml(plx_file);
end
end
setappdata(handles.output,'xml_file',xml_file);
% look for MAT file
mat_file=sprintf('%s%s.mat',pathname,basename);
if(~exist(mat_file,'file'))
save(mat_file,'-v7.3','');
end
setappdata(handles.output,'mat_file',mat_file);
% load struct into memory
exp = xml2struct(xml_file);
setappdata(handles.output,'exp',exp);
% get electrode type
setappdata(handles.output,'electrode',exp.experiment.electrode.Text);
% Populate Information fields
num_groups = length(exp.experiment.shank);
channel_groups = cell(1,num_groups);
group_nums = zeros(1,num_groups);
group_thresholds = cell(1,num_groups);
if(~iscell(exp.experiment.shank)),exp.experiment.shank={exp.experiment.shank}; end
for i = 1:num_groups
% get group numbers
group_nums(i) = str2double(exp.experiment.shank{i}.Attributes.num);
num_channels = length(exp.experiment.shank{i}.channel);
thresh = zeros(2,length(exp.experiment.shank{i}.channel));
disabled=false(1,length(exp.experiment.shank{i}.channel));
% get channel numbers
% Note: this special case is due to the fact that xml2struct doesn't
% place single items into cell arrays
if(num_channels==1)
channel_groups{i} = str2double(exp.experiment.shank{i}.channel.Attributes.num);
% grab threshold
if(isfield(exp.experiment.shank{group_nums(i)}.channel,'threshold2'))
thresh(1,i) = str2double(exp.experiment.shank{group_nums(i)}.channel.threshold.Text);
thresh(2,i) = str2double(exp.experiment.shank{group_nums(i)}.channel.threshold2.Text);
else
t=str2double(exp.experiment.shank{group_nums(i)}.channel.threshold.Text);
if(t>0), thresh(1,i)=t;
else thresh(2,i)=t;
end
end
% check if channel is enabled
if(strcmp(exp.experiment.shank{group_nums(i)}.channel.enabled.Text,'false'))
disabled=true;
end
else
% get channel number and threshold
for c = 1:num_channels
channel_groups{i}(c) = str2double(exp.experiment.shank{i}.channel{c}.Attributes.num);
% grab both thresholds
if(isfield(exp.experiment.shank{group_nums(i)}.channel{c},'threshold2'))
thresh(1,i) = str2double(exp.experiment.shank{group_nums(i)}.channel{c}.threshold.Text);
thresh(2,i) = str2double(exp.experiment.shank{group_nums(i)}.channel{c}.threshold2.Text);
else
t=str2double(exp.experiment.shank{group_nums(i)}.channel{c}.threshold.Text);
if(t>0), thresh(1,i)=t;
else thresh(2,i)=t;
end
end
if(strcmp(exp.experiment.shank{i}.channel{c}.enabled.Text,'false'))
disabled(c)=true;
end
end
end
channel_groups{i}(disabled)=[];
thresh(:,disabled)=[];
group_thresholds{i} = thresh;
end
Fs = str2double(exp.experiment.ADRate.Text);
num_total_channels = sum(cell2mat(cellfun(@length,channel_groups,'UniformOutput',false)));
% duration of experiment
exp_duration = str2double(exp.experiment.duration.Text);
setappdata(handles.output,'duration',exp_duration);
% save data to application
setappdata(handles.output,'num_groups',num_groups);
setappdata(handles.output,'channel_groups',channel_groups);
setappdata(handles.output,'group_nums',group_nums);
setappdata(handles.output,'group_thresholds',group_thresholds);
setappdata(handles.output,'Fs',Fs);
setappdata(handles.output,'exp_duration',exp_duration);
setappdata(handles.output,'num_total_channels',num_total_channels);
% update Information fields
set(handles.filename_label,'String',sprintf('%s%s.plx',pathname,basename));
set(handles.info_groups_label,'String',num2str(num_groups));
set(handles.info_channels_label,'String',num2str(num_total_channels));
set(handles.info_duration_label,'String',time2str(exp_duration));
set(handles.info_Fs_label,'String',sprintf('%g kHz',Fs/1e3));
% Enable the proper # of groups
strings=cell(1,num_groups+1); strings{1}='Select Group...';
for i = 1:num_groups, strings{i+1}=sprintf('Group %g',i); end
set(handles.group_menu,'String',strings);
% load event data
setappdata(handles.output,'events',[]);
load(mat_file,'events');
if(exist('events','var'))
setappdata(handles.output,'events',events);
end
% note: gaps are in case user paused recording
setappdata(handles.output,'gaps',[]);
load(mat_file,'gaps');
if(exist('gaps','var'))
setappdata(handles.output,'gaps',gaps);
end
set(handles.convert_button,'enable','on','visible','on');
set(handles.group_menu,'visible','on');
% Update status message
set(handles.status_label,'String','Select a group to process');
setappdata(handles.output,'panel_selected','none');
% --- calculates features from extracted waveforms
function calculate_features(handles,PCA_type)
global features palette;
set(handles.status_light,'BackgroundColor',palette.red);
set(handles.status_label,'String','Calculating Features...');
drawnow;
set(handles.status_label,'String','Extracting Features...'); drawnow;
t=getappdata(handles.output,'t');
% grab other features as well
num_PCs=get(handles.num_PCs_slider,'Max');
if(~iscell(num_PCs)),num_PCs={num_PCs}; end
params.PCA=max([num_PCs{:}]);
params.peaks=1;
params.valleys=1;
params.slope=1;
params.energy=1;
params.amplitude=1;
if(~exist('PCA_type','var')), PCA_type = 'all'; end
M_extract_features(t,params,handles,PCA_type);
% flatten features
features = permute(features,[3 1 2]);
features=reshape(features,size(features,1)*size(features,2),[])';
% Generate feature plots
set(handles.status_label,'String','Done!');
set(handles.status_light,'BackgroundColor',palette.green);
% --- Removes outliers from data
function remove_outliers(handles)
global features waveforms;
% find outliers and remove them
sigma_limit=str2double(get(handles.outliers_field,'String'));
%sigma_limit = 10; % 10 sigma limit
z = zscore(features,[],1);
ind = find(abs(z)>sigma_limit);
[I,~]=ind2sub(size(features),ind);
bad_locs = unique(I);
if(~isempty(I))
% update features
features(bad_locs,:)=[];
% update interpolated data
w=getappdata(handles.output,'waveforms_interp');
w(:,bad_locs,:)=[];
setappdata(handles.output,'waveforms_interp',w);
w=getappdata(handles.output,'normalized_waveforms_interp');
w(:,bad_locs,:)=[];
setappdata(handles.output,'normalized_waveforms_interp',w);
t=getappdata(handles.output,'timestamps_interp');
t(bad_locs)=[];
setappdata(handles.output,'timestamps_interp',t);
% update regular data
w=getappdata(handles.output,'original_waveforms');
w(:,bad_locs,:)=[];
setappdata(handles.output,'original_waveforms',w); clear w;
w=getappdata(handles.output,'original_normalized_waveforms');
w(:,bad_locs,:)=[];
setappdata(handles.output,'original_normalized_waveforms',w); clear w;
t=getappdata(handles.output,'original_timestamps');
t(bad_locs)=[];
setappdata(handles.output,'original_timestamps',t); clear t;
% update current data
waveforms(:,bad_locs,:)=[];
w=getappdata(handles.output,'normalized_waveforms');
w(:,bad_locs,:)=[];
setappdata(handles.output,'normalized_waveforms',w);
t=getappdata(handles.output,'timestamps');
t(bad_locs)=[];
setappdata(handles.output,'timestamps',t);
% update trigger channels
trigger_ch=getappdata(handles.output,'trigger_ch');
trigger_ch(bad_locs)=[];
setappdata(handles.output,'trigger_ch',trigger_ch);
% update idx
idx=getappdata(handles.output,'idx');
idx_old=getappdata(handles.output,'idx_old');
if(isempty(idx_old)), idx_old=zeros(1,length(idx),'uint8'); end
idx(bad_locs)=[];
idx_old(bad_locs)=[];
setappdata(handles.output,'idx',idx);
setappdata(handles.output,'idx_old',idx_old);
features_button_Callback([],[],handles);
set(handles.status_label,'String',sprintf('%g waveforms removed.',length(bad_locs)));
else
set(handles.status_label,'String',sprintf('No waveforms removed.'));
return;
end
% --- Executes on button press in features_button.
function features_button_Callback(~, ~, handles)
% hObject handle to features_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
show_featurespanel(handles);
function enable_rotate(f,~,featurepanel_pos,axHandle,rotate_handle)
global rotate;
currentPoint=get(f,'currentpoint');
axPos=get(axHandle,'position');
x_min=featurepanel_pos(1)+featurepanel_pos(3)*axPos(1);
x_max=featurepanel_pos(1)+featurepanel_pos(3)*(axPos(1)+axPos(3));
y_min=featurepanel_pos(2)+featurepanel_pos(4)*axPos(2);
y_max=featurepanel_pos(2)+featurepanel_pos(4)*(axPos(2)+axPos(4));
% we're outside
if(currentPoint(1)<x_min || currentPoint(1)>x_max || currentPoint(2)<y_min || currentPoint(2)>y_max)
if(rotate)
rotate=false;
set(rotate_handle,'enable','off');
end
% we're inside
else
if(~rotate)
rotate=true;
set(rotate_handle,'enable','on');
end
end
% --- hides clusters that aren't currently active
function hide_clusters(handles)
feature_plots=getappdata(handles.output,'feature_plots');
if(~isempty(feature_plots) && all(ishandle(feature_plots)))
clusters_hidden=getappdata(handles.output,'clusters_hidden');
selected_axes=getappdata(handles.output,'selected_axes');
if(clusters_hidden)
set(feature_plots(selected_axes & feature_plots~=0),'visible','on');
set(feature_plots(~selected_axes & feature_plots~=0),'visible','off');
else
set(feature_plots(feature_plots~=0),'visible','on');
end
hist_plots=getappdata(handles.output,'hist_plots');
if(any(hist_plots))
valid_handles=ishandle(hist_plots) & hist_plots~=0;
if(clusters_hidden)
set(hist_plots(selected_axes & valid_handles),'visible','on');
set(hist_plots(~selected_axes & valid_handles),'visible','off');
else
set(hist_plots(valid_handles),'visible','on');
end
end
end
% --- lights up dots in the selected cluster
function highlight_selected_cluster(handles)
feature_plots=getappdata(handles.output,'feature_plots');
if(~isempty(feature_plots) && all(ishandle(feature_plots)))
selected_axes=getappdata(handles.output,'selected_axes');
set(feature_plots(selected_axes & feature_plots~=0),'markersize',5);
set(feature_plots(~selected_axes & feature_plots~=0),'markersize',1);
% move selected to the top if we're in 2D view
% Note: OpenGL rendering sorts by ZData, not by uistack
twodee=get(handles.feature3_menu,'Value')==1;
if(twodee)
locs=find(selected_axes & feature_plots~=0);
badlocs=find(~selected_axes & feature_plots~=0);
for i = locs
X=get(feature_plots(i),'XData');
set(feature_plots(i),'ZData',ones(size(X))*eps);
end
for i = badlocs
X=get(feature_plots(i),'XData');
set(feature_plots(i),'ZData',zeros(size(X)));
end
end
hist_plots=getappdata(handles.output,'hist_plots');
if(any(hist_plots))
valid_handles=ishandle(hist_plots) & hist_plots~=0;
alpha(hist_plots(selected_axes & valid_handles),.9);
alpha(hist_plots(~selected_axes & valid_handles),.6);
if(twodee)
locs=find(selected_axes & valid_handles);
badlocs=find(~selected_axes & valid_handles);
for i = locs
X=get(hist_plots(i),'XData');
set(hist_plots(i),'ZData',ones(size(X))*eps);
end
for i = badlocs
X=get(hist_plots(i),'XData');
set(hist_plots(i),'Zdata',zeros(size(X)));
end
end
end
end
% -- Clears all the elements of a panel
function clear_panel(handle)
delete(get(handle,'Children'));
set(ancestor(handle,'figure'),'windowbuttonmotionfcn',[]);
% -- determine axis selection
function select_axes(src,~,handles)
% get waveform axis handles to compare
wf_axes = getappdata(handles.output,'wf_axes');
% get list of currently selected axes
selected_axes = getappdata(handles.output,'selected_axes');
%selected_axes=false(1,length(selected_axes));
% determine which axis was selected
axis_selected = find(wf_axes==src);
% get selection type
type=get(handles.output,'SelectionType');
% reset others to zero
if(strcmpi(type,'normal'))
% remove frame from all selected axes, select/unselect current
toggle_frame(unique([find(selected_axes) axis_selected]),handles);
else
toggle_frame(axis_selected,handles);
end
selected_axes = getappdata(handles.output,'selected_axes');
axis_selected = find(selected_axes);
if(~isempty(axis_selected))
setappdata(handles.output,'cluster_number',axis_selected-1);
end
panel_selected=getappdata(handles.output,'panel_selected');
if(strcmp(panel_selected,'features'))
% update feature plot to put this plot on top
% move current waveform view to the top
wf_plots=getappdata(handles.output,'wf_plots');
for i = 1:size(wf_plots,1)
uistack(wf_plots(i,axis_selected),'top');
end
fix_featurepanel_display(handles);
elseif(strcmp(panel_selected,'cell_stats'))
update=true;
display_cell_stats(handles,update);
end
% Updates status label
idx=getappdata(handles.output,'idx');
total_wfs=false(1,length(idx));
for i = 1:length(axis_selected)
total_wfs=total_wfs | idx==axis_selected(i)-1;
end
total_wfs=sum(total_wfs);
set(handles.status_label,'String',sprintf('%g waveforms.',total_wfs));
% Draw or remove a frame around a waveform plot
% note: if ax==-1, then turn all off
% also note: cluster_number = selected_axis-1
function selected_axes = toggle_frame(ax, handles)
frame_handles=getappdata(handles.output,'frame_handles');
selected_axes=getappdata(handles.output,'selected_axes');
% instruction to turn all off
if(ax==-1)
if(~isempty(selected_axes))
selected_axes(:)=0;
setappdata(handles.output,'selected_axes',selected_axes);
end
return;
end
% toggle frames
for i = 1:length(ax)
if(selected_axes(ax(i)))
selected_axes(ax(i))=false;
set(frame_handles(ax(i)),'visible','off','hittest','off');
else
selected_axes(ax(i))=true;
set(frame_handles(ax(i)),'visible','on','hittest','off');
end
end
drawnow;
% update merge & split buttons
num_selected=length(find(selected_axes));
if(num_selected>1)
set(handles.merge_button,'enable','on');
else
set(handles.merge_button,'enable','off');
end
if(num_selected==0), set(handles.split_button,'enable','off');
else set(handles.split_button,'enable','on');
end
setappdata(handles.output,'selected_axes',selected_axes);
% --- Executes on button press in merge_button.
function merge_button_Callback(~, ~, handles) %#ok<DEFNU>
% hObject handle to merge_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global palette;
set(handles.status_light,'BackgroundColor',palette.red);
set(handles.status_label,'String','Merging...'); drawnow; pause(0.01);
selected_axes=getappdata(handles.output,'selected_axes');
idx=getappdata(handles.output,'idx');
setappdata(handles.output,'idx_old',idx);
cluster_nums=find(selected_axes)-1;
s=sprintf('Clusters %g, ',cluster_nums(1));
for i = 2:length(cluster_nums)
idx(idx==cluster_nums(i))=cluster_nums(1);
s=sprintf('%s%g',s,cluster_nums(i));
end
% fix cluster numbers
setappdata(handles.output,'idx',idx);
fix_clusters(handles);
s=sprintf('%s merged into cluster %g.',s,cluster_nums(1));
setappdata(handles.output,'cluster_number',cluster_nums(1));
panel_selected=getappdata(handles.output,'panel_selected');
switch panel_selected
case 'features'
show_featurespanel(handles);
case 'cell_stats'
stats_button_Callback([],[],handles);
end
set(handles.status_label,'String',s);
set(handles.status_light,'BackgroundColor',palette.green);
% -- plays a beep or notifies android
function stop_alert(phone_alert)
if(exist('phone_alert','var') && phone_alert==true)
notify_android('Msort finished');
end
soundbeep;
% -- updates cluster numbers so there are no clusters with zero waveforms
function fix_clusters(handles)
global waveforms;
idx=getappdata(handles.output,'idx');
if(isempty(waveforms))
idx=[]; setappdata(handles.output,'idx',idx);
setappdata(handles.output,'timestamps',[]); return;
end
if(isempty(idx))
idx=zeros(1,size(waveforms,2),'uint8');
num_clusters=1;
setappdata(handles.output,'cluster_number',0);
else
u=unique(idx); u(u==0)=[];
% find missing clusters
u(u==0)=[];
for i = 1:length(u)
% renumber this cluster
if(u(i)>i)
idx(idx==u(i))=i;
end
end
u=unique([0 u]); num_clusters=length(u);
end
setappdata(handles.output,'num_clusters',num_clusters);
setappdata(handles.output,'idx',idx);
% add noise cluster if it's missing
cluster_colors=get_cluster_colors(handles);
setappdata(handles.output,'cluster_colors',cluster_colors);
cluster_number=getappdata(handles.output,'cluster_number');
if(cluster_number>num_clusters-1)
setappdata(handles.output,'cluster_number',0);
end
setappdata(handles.output,'selected_axes',false(1,num_clusters));
% --- Executes on button press in voltage_plot_button.
function voltage_plot_button_Callback(~, ~, handles) %#ok<DEFNU>
% hObject handle to voltage_plot_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%clear_panel(handles.features_panel);
%hide_panel(handles.features_panel);
global palette;
plot_voltages(handles);
setappdata(handles.output,'panel_selected','voltage');
set(handles.status_label,'String','Done!');
set(handles.status_light,'BackgroundColor',palette.green);
% --- Executes on button press in param_button.
function param_button_Callback(~, ~, handles) %#ok<DEFNU>
% hObject handle to param_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
calculate_features(handles);
% --- Executes on button press in stats_button.
% This function displays ISI and other statistics
function stats_button_Callback(~, ~, handles)
% hObject handle to stats_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
setappdata(handles.output,'panel_selected','cell_stats');
set(handles.feature_control_panel,'visible','off');
clear_panel(handles.features_panel); drawnow;
display_cell_stats(handles);
% -- Generates merge buttons in the small panel; size depends no number of clusters
function generate_merge_buttons(handles)
num_clusters=getappdata(handles.output,'num_clusters');
merge_buttons=getappdata(handles.output,'merge_buttons');
if(~isempty(merge_buttons) && any(ishandle(merge_buttons)))
delete(merge_buttons);
end
margin_x=.03; margin_y=.03;
margin_top=.05; margin_bot=.05;
margin_left=.05; margin_right=.05;
num_rows=floor(sqrt(num_clusters));
num_cols=ceil(num_clusters/num_rows);
button_height=(1-margin_top-margin_bot-(num_rows-1)*margin_y)/num_rows;
button_width=(1-margin_left-margin_right-(num_cols-1)*margin_x)/num_cols;
buttons=zeros(1,num_clusters);
cluster_colors=getappdata(handles.output,'cluster_colors');
ind=1;
for i = 1:num_rows
y=1-margin_top-(i-1)*margin_y-i*button_height;
for j = 1:num_cols
if(ind>num_clusters), ind=ind+1; continue; end
x=margin_left+(j-1)*(margin_x+button_width);
buttons(ind)=uicontrol('Style','Pushbutton','units','normalized','String',[],'BackgroundColor',cluster_colors(ind,:)/3,...
'Parent',handles.merge_panel,'selectionhighlight','off','enable','off','callback',{@move_wfs,handles,ind-1},...
'Position',[x y button_width button_height]);
ind=ind+1;
end
end
setappdata(handles.output,'merge_buttons',buttons);
% -- creates a new cluster, given an index
function create_new_cluster(handles)
% Retrieve currently selectected cluster
cluster_number=getappdata(handles.output,'cluster_number');
if(isempty(cluster_number)),cluster_number=0; end
setappdata(handles.output,'cluster_number',cluster_number);
% Remove waveforms from old cluster and create new waveform
idx=getappdata(handles.output,'idx');
selected_waveforms=getappdata(handles.output,'selected_waveforms');
if(isempty(selected_waveforms))
selected_waveforms=idx==cluster_number;
end
num_clusters=getappdata(handles.output,'num_clusters');
idx(selected_waveforms)=num_clusters;
setappdata(handles.output,'num_clusters',num_clusters+1);
setappdata(handles.output,'idx',idx);
setappdata(handles.output,'idx_old',idx);
setappdata(handles.output,'selected_waveforms',[]);
setappdata(handles.output,'cluster_number',num_clusters);
fix_clusters(handles);
show_featurespanel(handles);
% -- deselects all waveforms
function deselect_wfs(handles) %#ok<DEFNU>
global wf_plots dim_channel_color main_channel;
buttons=getappdata(handles.output,'merge_buttons');
%selected_wfs=getappdata(handles.features_panel,'selected_wfs');
set(wf_plots(selected_wfs),'color',dim_channel_color(main_channel,:));
setappdata(handles.features_panel,'selected_wfs',[]);
% re-disable buttons
for i = 1:length(buttons)
if(strcmp(get(buttons(i),'enable'),'on'))
set(buttons(i),'backgroundcolor',get(buttons(i),'backgroundcolor')/3,'enable','off');
end
end
% -- Select particular waveforms with the mouse
% Note: there's some error that occurs. After merging two clusters, you
% can't reselect waveforms unless you move away from Feature view and move
% back. This should be looked into.
function select_wfs(hObject,~,handles)
global ax_coord x_ratio y_ratio ax_lim locsX locsY h x y palette;
fig=handles.output;
ax=hObject;
set([ax fig 0],'units','pixels');
ax_pos=get(hObject,'position');
ax_lim=axis(hObject);
fig_pos=get(fig,'position');
% get axis coordinates
g=hObject;
x_offset=fig_pos(1); y_offset=fig_pos(2);
while(g~=fig)
tmp=get(g,'units'); set(g,'units','pixels');
pos=get(g,'position');
x_offset=x_offset+pos(1);
y_offset=y_offset+pos(2);
set(g,'units',tmp);
g=get(g,'parent');
end
ax_coord = [x_offset y_offset x_offset y_offset] + [0 0 ax_pos(3) ax_pos(4)];
% determine mouse position conversion
x_ratio=(ax_lim(2)-ax_lim(1))/ax_pos(3);
y_ratio=(ax_lim(4)-ax_lim(3))/ax_pos(4);
% set up initial plot
mouse_pt = get(0,'PointerLocation');
x1=(mouse_pt(1)-ax_coord(1))*x_ratio+ax_lim(1);
y1=(mouse_pt(2)-ax_coord(2))*y_ratio+ax_lim(3);