-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXsembles_2P_Viewer.m
1722 lines (1494 loc) · 77.7 KB
/
Xsembles_2P_Viewer.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
classdef Xsembles_2P_Viewer < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
Xsembles2PViewerUIFigure matlab.ui.Figure
TabGroup matlab.ui.container.TabGroup
AnalyzeTab matlab.ui.container.Tab
XsembleanalysisPanel matlab.ui.container.Panel
XsemblesAnalysisCheckBox matlab.ui.control.CheckBox
NeuronsPanel matlab.ui.container.Panel
NeuronRadiusSpinner matlab.ui.control.Spinner
samplingperiodmsLabel_3 matlab.ui.control.Label
NeuronsDropDown matlab.ui.control.DropDown
SignalevaluationPanel matlab.ui.container.Panel
SetThresholdVisuallyCheckBox matlab.ui.control.CheckBox
PSNRdBThSpinner matlab.ui.control.Spinner
samplingperiodmsLabel_4 matlab.ui.control.Label
AnalyzeVideoButton matlab.ui.control.Button
MotioncorrectionPanel matlab.ui.container.Panel
SpeedThSpinner matlab.ui.control.Spinner
locomotionthresholdLabel matlab.ui.control.Label
FastMotionCorrectionCheckBox matlab.ui.control.CheckBox
VideoPanel matlab.ui.container.Panel
FileEditField matlab.ui.control.EditField
FileButton matlab.ui.control.Button
AnalysisSamplingPeriodSpinner matlab.ui.control.Spinner
samplingperiodmsLabel_2 matlab.ui.control.Label
VisualizeTab matlab.ui.container.Tab
StimulationPanel matlab.ui.container.Panel
PlotStimLocationButton matlab.ui.control.Button
StimuliDropDown matlab.ui.control.DropDown
PlottrialsButton matlab.ui.control.Button
XsemblesPanel matlab.ui.container.Panel
DividerBLabel matlab.ui.control.Label
DividerALabel matlab.ui.control.Label
HighlightEnsembleDropDown matlab.ui.control.DropDown
HighlightensembleLabel matlab.ui.control.Label
PlotNeuronsButton matlab.ui.control.Button
BrightnessNeuronsCheckBox matlab.ui.control.CheckBox
ShapeCheckBox matlab.ui.control.CheckBox
GetStimulationFilesButton matlab.ui.control.Button
GetNeuronsButton matlab.ui.control.Button
SelectSignalsDropDown matlab.ui.control.DropDown
PlotSignalsButton matlab.ui.control.Button
PlotNonparticipantCheckBox matlab.ui.control.CheckBox
PlotOffsembleCheckBox matlab.ui.control.CheckBox
PlotOnsembleCheckBox matlab.ui.control.CheckBox
RasterPanel matlab.ui.container.Panel
SortNeuronsDropDown matlab.ui.control.DropDown
ReplayActivityButton matlab.ui.control.Button
PlotRasterButton matlab.ui.control.Button
SortVectorsCheckBox matlab.ui.control.CheckBox
SelectDataDropDown matlab.ui.control.DropDown
HelpTab matlab.ui.container.Tab
ContactPanel matlab.ui.container.Panel
EmailLink matlab.ui.control.Hyperlink
EmailLink2 matlab.ui.control.Hyperlink
EmailLabel matlab.ui.control.Label
KeepituptodatePanel matlab.ui.container.Panel
GithubLink matlab.ui.control.Hyperlink
GithubLabel matlab.ui.control.Label
CitationPanel matlab.ui.container.Panel
DOILink matlab.ui.control.Hyperlink
CitationLabel matlab.ui.control.Label
end
properties (Access = private)
DataName = '-- select data --';
NeuronsName = '-- find neurons --';
PSNR2 = [];
Movie = [];
end
methods (Access = private)
function data = Read_Data(app)
if strcmp(app.DataName,'-- select data --')
app.SelectDataDropDown.BackgroundColor = [0.96 0.96 0.96];
data = [];
else
% Check if variable exist
if evalin('base',['exist(''' app.DataName ''',''var'')'])
data = evalin('base',app.DataName);
% Check if data has movie field
if isfield(data,'Movie')
app.SelectDataDropDown.BackgroundColor = [0.8 0.9 0.8];
app.SelectDataDropDown.Tooltip = 'Data compatible!';
% Check if data has analysis field
if isfield(data,'Analysis')
app.SortVectorsCheckBox.Enable = 'on';
else
app.SortVectorsCheckBox.Value = false;
app.SortVectorsCheckBox.Enable = 'off';
app.HighlightEnsembleDropDown.Enable = 'off';
end
else
app.SelectDataDropDown.BackgroundColor = [0.9 0.8 0.8];
app.SelectDataDropDown.Tooltip = 'Data not compatible!';
data = [];
end
else
app.SelectDataDropDown.BackgroundColor = [0.9 0.8 0.8];
app.SelectDataDropDown.Tooltip = 'Variable does not exist!';
data = [];
end
end
end
function neurons = Read_Neurons(app)
if strcmp(app.NeuronsName,'-- find neurons --')
app.NeuronsDropDown.BackgroundColor = [0.96 0.96 0.96];
neurons = struct([]);
else
% Check if exist
if evalin('base',['exist(''' app.NeuronsName ''',''var'')'])
neurons = evalin('base',app.NeuronsName);
% Check if has pixels field
if isfield(neurons,'pixels')
app.NeuronsDropDown.BackgroundColor = [0.8 0.9 0.8];
app.NeuronsDropDown.Tooltip = 'Variable compatible!';
else
app.NeuronsDropDown.BackgroundColor = [0.9 0.8 0.8];
app.NeuronsDropDown.Tooltip = 'Variable not compatible!';
neurons = struct([]);
end
else
app.NeuronsDropDown.BackgroundColor = [0.9 0.8 0.8];
app.NeuronsDropDown.Tooltip = 'Variable does not exist!';
neurons = struct([]);
end
end
end
function Plot_Xsembles(app)
data = Read_Data(app);
if isempty(data)
return
end
plot_all = strcmp(app.HighlightEnsembleDropDown.Value,'no');
% Get ensemble neurons
if plot_all
% All ensemble neurons
options_title = ' (all neurons)';
legend_text = {'all neurons'};
else
plot_onsemble = app.PlotOnsembleCheckBox.Value;
plot_offsemble = app.PlotOffsembleCheckBox.Value;
plot_nonparticipant = app.PlotNonparticipantCheckBox.Value;
if ~plot_onsemble && ~plot_offsemble && ~plot_nonparticipant
return
end
% Selected ensemble neurons
ensemble_number = str2double(app.HighlightEnsembleDropDown.Value);
onsemble_id = data.Analysis.Ensembles.OnsembleNeurons{ensemble_number};
offsemble_id = data.Analysis.Ensembles.OffsembleNeurons{ensemble_number};
nonparticipant_id = setdiff(1:data.Analysis.Neurons,onsemble_id);
nonparticipant_id = setdiff(nonparticipant_id,offsemble_id);
ensemble_color = Get_Color(ensemble_number,'jp');
% Get coordinates
xy_onsemble = data.XY.All(onsemble_id,:);
xy_offsemble = data.XY.All(offsemble_id,:);
xy_nonparticipant = data.XY.All(nonparticipant_id,:);
if plot_onsemble && plot_offsemble && plot_nonparticipant
options_title = [' (onsemble, offsemble, and nonparticipant ' app.HighlightEnsembleDropDown.Value ')'];
legend_text = {'nonparticipant neurons','offsemble neurons','onsemble neurons'};
elseif plot_onsemble && plot_offsemble
options_title = [' (ensemble and offsemble ' app.HighlightEnsembleDropDown.Value ')'];
legend_text = {'offsemble neurons','onsemble neurons'};
elseif plot_onsemble && plot_nonparticipant
options_title = [' (ensemble and nonparticipant ' app.HighlightEnsembleDropDown.Value ')'];
legend_text = {'nonparticipant neurons','onsemble neurons'};
elseif plot_onsemble
options_title = [' (onsemble ' app.HighlightEnsembleDropDown.Value ')'];
legend_text = {'onsemble neurons'};
elseif plot_offsemble && plot_nonparticipant
options_title = [' (offsemble and nonparticipant ' app.HighlightEnsembleDropDown.Value ')'];
legend_text = {'nonparticipant neurons','offsemble neurons'};
elseif plot_offsemble
options_title = [' (offsemble ' app.HighlightEnsembleDropDown.Value ')'];
legend_text = {'offsemble neurons'};
elseif plot_nonparticipant
options_title = [' (nonparticipant neurons ' app.HighlightEnsembleDropDown.Value ')'];
legend_text = {'nonparticipant neurons'};
end
end
% Plot
w = data.Movie.Width;
h = data.Movie.Height;
Set_Figure([data.Movie.DataName ' - Xsembles' options_title],...
[0 0 round(420*w/h) 449])
if plot_all
plot(data.XY.All(:,1),data.XY.All(:,2),'.','Color',[0.9 0.9 0.9],...
'MarkerSize',30); hold on
else
if plot_nonparticipant
plot(xy_nonparticipant(:,1),xy_nonparticipant(:,2),'.','Color',[0.9 0.9 0.9],...
'MarkerSize',30); hold on
end
if plot_offsemble
plot(xy_offsemble(:,1),xy_offsemble(:,2),'.','Color',...
Attenuate_Colors(ensemble_color),'MarkerSize',30); hold on
end
if plot_onsemble
plot(xy_onsemble(:,1),xy_onsemble(:,2),'.','Color',...
ensemble_color,'MarkerSize',30); hold on
end
end
title(strrep([data.Movie.DataName ' - Xsembles' options_title],'_','-'))
set(gca,'xlim',[0 w],'ylim',[0 h],'xtick',[],'ytick',[],'ydir','reverse')
pbaspect([w/h 1 1])
legend(legend_text);
legend off
end
function Plot_Xsembles_Shape(app)
% Read data
data = Read_Data(app);
if isempty(data)
return
end
if app.BrightnessNeuronsCheckBox.Value
brightness = data.Transients.PSNRdB;
else
brightness = ones(1,length(data.Neurons));
end
if strcmp(app.HighlightEnsembleDropDown.Value,'no')
% Get mask for all neurons
mask = Get_ROIs_Image(data.Neurons,data.Movie.Width,...
data.Movie.Height,brightness,0,0);
options_title = '';
else
plot_onsemble = app.PlotOnsembleCheckBox.Value;
plot_offsemble = app.PlotOffsembleCheckBox.Value;
plot_nonparticipant = app.PlotNonparticipantCheckBox.Value;
if ~plot_onsemble && ~plot_offsemble && ~plot_nonparticipant
return
end
% Set mask highlighting the selected ensemble
ensemble_number = str2double(app.HighlightEnsembleDropDown.Value);
onsemble_hsv = rgb2hsv(Get_Color(ensemble_number,'jp'));
offsemble_hsv = rgb2hsv(Attenuate_Colors(Get_Color(ensemble_number,'jp')));
% Get ids
onsemble_id = data.Analysis.Ensembles.OnsembleNeurons{ensemble_number};
offsemble_id = data.Analysis.Ensembles.OffsembleNeurons{ensemble_number};
nonparticipant_id = setdiff(1:data.Analysis.Neurons,onsemble_id);
nonparticipant_id = setdiff(nonparticipant_id,offsemble_id);
% Initialize values
hues = zeros(1,data.Analysis.Neurons);
saturation = zeros(1,data.Analysis.Neurons);
hues(onsemble_id) = onsemble_hsv(1);
saturation(onsemble_id) = onsemble_hsv(2);
hues(offsemble_id) = offsemble_hsv(1);
saturation(offsemble_id) = offsemble_hsv(2);
if ~plot_onsemble
brightness(onsemble_id) = 0;
end
if ~plot_offsemble
brightness(offsemble_id) = 0;
end
if ~plot_nonparticipant
brightness(nonparticipant_id) = 0;
end
mask = Get_ROIs_Image(data.Neurons,data.Movie.Width,...
data.Movie.Height,brightness,hues,saturation);
% Get title
if plot_onsemble && plot_offsemble && plot_nonparticipant
options_title = [' (onsemble, offsemble, and nonparticipant ' app.HighlightEnsembleDropDown.Value ')'];
elseif plot_onsemble && plot_offsemble
options_title = [' (onsemble and offsemble ' app.HighlightEnsembleDropDown.Value ')'];
elseif plot_onsemble && plot_nonparticipant
options_title = [' (onsemble and nonparticipant ' app.HighlightEnsembleDropDown.Value ')'];
elseif plot_onsemble
options_title = [' (onsemble ' app.HighlightEnsembleDropDown.Value ')'];
elseif plot_offsemble && plot_nonparticipant
options_title = [' (offsemble and nonparticipant ' app.HighlightEnsembleDropDown.Value ')'];
elseif plot_offsemble
options_title = [' (offsemble ' app.HighlightEnsembleDropDown.Value ')'];
elseif plot_nonparticipant
options_title = [' (nonparticipant neurons ' app.HighlightEnsembleDropDown.Value ')'];
end
end
image = cast(rescale(mask)*double(intmax('uint8')),'uint8');
% Plot image
Set_Figure([data.Movie.DataName ' - Neurons' options_title],[0 0 500 500])
h_image = imshow(image,'InitialMagnification',300); hold on;
h_image.ButtonDownFcn = @(~,~)ButtonDownImage(app);
title(strrep([data.Movie.DataName ' - Neurons' options_title],'_','-'))
end
function Plot_Neurons_Threshold(app)
% Read data
data = Read_Data(app);
if isempty(data)
return
end
raw = data.Transients.Raw;
smoothed = data.Transients.Smoothed;
raw0 = raw-smoothed;
switch app.EvaluatingMeasureDropDown.Value
case 'PSNR (f0)'
signals = data.Transients.PSNRdB;
case 'PSNR (raw0)'
signals = max((raw-raw0),[],2)./std(raw0,[],2);
case 'range'
signals = range(smoothed,2);
case 'var (raw0)'
signals = var(raw0,[],2);
case 'var (raw)'
signals = var(raw,[],2);
end
% Get ids
accepted_id = signals>app.ThresholdSpinner.Value;
image = Draw_Neurons_Accepted(data.Neurons,accepted_id,...
data.Movie.Width,data.Movie.Height);
% Plot image
Set_Figure([data.Movie.DataName ' - Neurons Accepted'],[0 0 500 500])
h_image = imshow(image,'InitialMagnification',200); hold on;
h_image.ButtonDownFcn = @(~,~)ButtonDownImage(app);
title(strrep([data.Movie.DataName ' - Neurons'],'_','-'))
imwrite(image,[data.Movie.DataName ' - ' app.EvaluatingMeasureDropDown.Value '- '...
num2str(app.ThresholdSpinner.Value,'%.2f') '.png'])
end
function ButtonDownImage(app,~)
point = get(gca,'CurrentPoint');
x = point(1,1);
y = point(1,2);
% Read data
data = Read_Data(app);
if isfield(data.ROIs,'NeuronRadius')
radius = data.ROIs.NeuronRadius;
else
radius = data.ROIs.CellRadius;
end
neuron = Find_Neurons_By_XY(data.Neurons,[x y],radius*1.5);
if ~isempty(neuron)
neuron_id = neuron(1);
% Plot singlas from the neuron selected
h_figure = Set_Figure([data.Movie.DataName ' - Neuron signal'],...
[0 0 1200 500]);
% Add scrollbar
n_neurons = length(data.Neurons);
h_scrollbar = uicontrol(h_figure,'style','slider','units','normalized',...
'position',[0 0 1 .05],'sliderstep',...
[1/n_neurons 10/n_neurons]);
% Add listener to scrollbar
addlistener(h_scrollbar,'Value','PostSet',...
@(~,~)NeuronScroll_Callback(app,h_scrollbar));
h_scrollbar.Value = neuron_id/n_neurons;
else
disp(' no neuron selected')
end
end
function NeuronScroll_Callback(app,h_scrollbar)
data = Read_Data(app);
n_neurons = length(data.Neurons);
neuron_id = round(get(h_scrollbar,'value')*n_neurons-1)+1;
Hold_Figure([data.Movie.DataName ' - Neuron signal'])
Plot_Neuron_Signal(data,neuron_id,[])
end
function NeuronTestScroll_Callback(app,h_scrollbar)
data = Read_Data(app);
[~,neuron_id] = sort(app.PSNR2);
selected = round(get(h_scrollbar,'value')*n_neurons-1)+1;
Hold_Figure([data.Movie.DataName ' - Neuron test'])
Plot_Neuron_Test(data,neuron_id(selected))
end
function ReplayScroll_Callback(app,h)
data = Read_Data(app);
% Get frame selected
n = data.Movie.Frames;
frame = round(h.frame_scrollbar.Value*n);
if frame==0
frame = 1;
end
h.frame_text.String = ['frame: ' num2str(frame,'%.0f')];
im = uint8(app.Movie(:,:,frame));
im = imadjust(im+1,[],[],h.gamma_scrollbar.Value);
% Plot frame
h.frame_image.CData = im;
% Plot current frame
cla(h.current_axes)
axes(h.current_axes)
plot([frame frame],[0 1],'--k')
drawnow
end
function GammaScroll_Callback(app,h)
% get value
value = round(h.gamma_scrollbar.Value*100)/100;
% change text
h.gamma_text.String = ['gamma correction: ' num2str(value,'%0.2f')];
% modify image
ReplayScroll_Callback(app,h)
end
end
% Callbacks that handle component events
methods (Access = private)
% Drop down opening function: SelectDataDropDown
function SelectDataDropDownOpening(app, event)
data_strings = evalin('base','who');
set(app.SelectDataDropDown,'items',[{'-- select data --'};data_strings])
end
% Value changed function: SelectDataDropDown
function SelectDataDropDownValueChanged(app, event)
name = app.SelectDataDropDown.Value;
app.DataName = name;
% Initialize values
app.HighlightEnsembleDropDown.Value = 'no';
app.StimuliDropDown.Value = '-- select stimulus --';
app.StimuliDropDown.Enable = 'off';
% Read data
data = Read_Data(app);
if isfield(data,'VoltageRecording')
if isfield(data.VoltageRecording,'Stimuli')||...
isfield(data.VoltageRecording,'Laser')
app.StimuliDropDown.Enable = 'on';
app.PlottrialsButton.Enable = 'on';
else
app.StimuliDropDown.Enable = 'off';
app.PlottrialsButton.Enable = 'off';
end
else
app.StimuliDropDown.Enable = 'off';
app.PlottrialsButton.Enable = 'off';
end
HighlightEnsembleDropDownValueChanged(app)
end
% Button pushed function: PlotRasterButton
function PlotRasterButtonPushed(app, event)
% Read data
data = Read_Data(app);
if isempty(data)
return
end
value = app.SortNeuronsDropDown.Value;
sort_vectors = app.SortVectorsCheckBox.Value;
switch value
case 'no sorting of neurons'
sort_neurons = false;
case 'ensemble sorting'
sort_neurons = true;
case 'ONsemble sorting'
sort_neurons = Get_Neuron_ID(data,'on');
case 'OFFsemble sorting'
sort_neurons = Get_Neuron_ID(data,'off');
otherwise
if strcmp(value(1:5),'EPI -')
epi_ensemble = str2num(value(end));
[~,sort_neurons] = sort(data.Analysis.Ensembles.EPI(epi_ensemble,:),'descend');
else
sort_neurons = evalin('base',value);
end
end
if isfield(data,'VoltageRecording')
voltage = data.VoltageRecording;
else
voltage = [];
end
if sort_vectors && sum(sort_neurons)
options_title = '(neurons and vectors sorted)';
elseif sort_vectors
options_title = '(vectors sorted)';
elseif sum(sort_neurons)
options_title = '(neurons sorted)';
else
options_title = '';
end
Set_Figure([data.Movie.DataName ' - Ensembles ' options_title],[0 0 1200 700])
fps = data.Movie.FPS;
if isfield(data,'Analysis')
Plot_Xsemble_Raster(data,sort_neurons,sort_vectors)
else
Plot_Raster_And_External(data.Transients.Raster,voltage,fps)
end
end
% Button pushed function: PlotNeuronsButton
function PlotNeuronsButtonPushed(app, event)
if app.ShapeCheckBox.Value
Plot_Xsembles_Shape(app)
else
Plot_Xsembles(app)
end
end
% Drop down opening function: HighlightEnsembleDropDown
function EnsembleDropDownOpening(app, event)
data = Read_Data(app);
if isempty(data)
return
end
data_strings = {};
for i = 1:data.Analysis.Ensembles.Count
data_strings{i} = num2str(i);
end
data_strings = [{'no'} data_strings];
set(app.HighlightEnsembleDropDown,'items',data_strings)
end
% Value changed function: ShapeCheckBox
function ShapeCheckBoxValueChanged(app, event)
shape = app.ShapeCheckBox.Value;
if shape
app.BrightnessNeuronsCheckBox.Enable = 'on';
else
app.BrightnessNeuronsCheckBox.Enable = 'off';
end
end
% Value changed function: HighlightEnsembleDropDown
function HighlightEnsembleDropDownValueChanged(app, event)
value = app.HighlightEnsembleDropDown.Value;
switch value
case 'no'
app.PlotOnsembleCheckBox.Enable = 'off';
app.PlotOffsembleCheckBox.Enable = 'off';
app.PlotNonparticipantCheckBox.Enable = 'off';
otherwise
app.PlotOnsembleCheckBox.Enable = 'on';
app.PlotOffsembleCheckBox.Enable = 'on';
app.PlotNonparticipantCheckBox.Enable = 'on';
end
end
% Button pushed function: GetStimulationFilesButton
function GetStimulationFilesButtonPushed(app, event)
% Read data
data = Read_Data(app);
if isempty(data)
return
end
if strcmp(app.HighlightEnsembleDropDown.Value,'no')
% Spirals
Write_XY_Prairie_Stim(data.XY.All,true,'',...
[data.Movie.DataName ' - all neurons - spiral'])
% Point
Write_XY_Prairie_Stim(data.XY.All,false,'',...
[data.Movie.DataName ' - all neurons - point'])
else
plot_onsemble = app.PlotOnsembleCheckBox.Value;
plot_offsemble = app.PlotOffsembleCheckBox.Value;
plot_nonparticipant = app.PlotNonparticipantCheckBox.Value;
if ~plot_onsemble && ~plot_offsemble && ~plot_nonparticipant
return
end
% Selected ensemble neurons
ensemble_number = str2double(app.HighlightEnsembleDropDown.Value);
onsemble_id = data.Analysis.Ensembles.OnsembleNeurons{ensemble_number};
offsemble_id = data.Analysis.Ensembles.OffsembleNeurons{ensemble_number};
nonparticipant_id = setdiff(1:data.Analysis.Neurons,onsemble_id);
nonparticipant_id = setdiff(nonparticipant_id,offsemble_id);
% Set spiral marker points
if plot_onsemble
Write_XY_Prairie_Stim(data.XY.All(onsemble_id,:),true,'',...
[data.Movie.DataName ' - onsemble ' num2str(ensemble_number) ' spiral'])
Write_XY_Prairie_Stim(data.XY.All(onsemble_id,:),false,'',...
[data.Movie.DataName ' - onsemble ' num2str(ensemble_number) ' point'])
end
if plot_offsemble
Write_XY_Prairie_Stim(data.XY.All(offsemble_id,:),true,'',...
[data.Movie.DataName ' - offsemble ' num2str(ensemble_number) ' spiral'])
Write_XY_Prairie_Stim(data.XY.All(offsemble_id,:),false,'',...
[data.Movie.DataName ' - offsemble ' num2str(ensemble_number) ' point'])
end
if plot_nonparticipant
Write_XY_Prairie_Stim(data.XY.All(nonparticipant_id,:),true,'',...
[data.Movie.DataName ' - nonparticipant ' num2str(ensemble_number) ' spiral'])
Write_XY_Prairie_Stim(data.XY.All(nonparticipant_id,:),false,'',...
[data.Movie.DataName ' - nonparticipant ' num2str(ensemble_number) ' point'])
end
end
end
% Button pushed function: AnalyzeVideoButton
function AnalyzeVideoButtonPushed(app, event)
file_path = app.FileEditField.Value;
sampling_period = app.AnalysisSamplingPeriodSpinner.Value/1000;
neuron_radius = app.NeuronRadiusSpinner.Value;
motion_correction = app.FastMotionCorrectionCheckBox.Value;
speed_threshold = app.SpeedThSpinner.Value;
select_threshold = app.SetThresholdVisuallyCheckBox.Value;
PSNR_Th = app.PSNRdBThSpinner.Value;
neurons = Read_Neurons(app);
get_xsembles = app.XsemblesAnalysisCheckBox.Value;
Xsembles_2P(file_path,...
'SamplingPeriod',sampling_period,...
'NeuronRadius',neuron_radius,...
'MotionCorrection',motion_correction,...
'MotionCorrectionThreshold',speed_threshold,...
'PSNRdBThreshold',PSNR_Th,...
'SelectPSNRThresholdVisually',select_threshold,...
'Neurons',neurons,...
'GetXsembles',get_xsembles)
end
% Value changed function: FastMotionCorrectionCheckBox
function FastMotionCorrectionCheckBoxValueChanged(app, event)
if app.FastMotionCorrectionCheckBox.Value
app.SpeedThSpinner.Enable = 'on';
else
app.SpeedThSpinner.Enable = 'off';
end
end
% Drop down opening function: NeuronsDropDown
function NeuronsDropDownOpening(app, event)
data_strings = evalin('base','who');
set(app.NeuronsDropDown,'items',[{'-- find neurons --'};data_strings])
if isempty(data_strings)
app.NeuronsDropDown.BackgroundColor = [0.96 0.96 0.96];
end
end
% Value changed function: NeuronsDropDown
function NeuronsDropDownValueChanged(app, event)
app.NeuronsName = app.NeuronsDropDown.Value;
if strcmp(app.NeuronsName,'-- find neurons --')
app.NeuronRadiusSpinner.Enable = 'on';
else
app.NeuronRadiusSpinner.Enable = 'off';
end
Read_Neurons(app);
end
% Button pushed function: GetNeuronsButton
function GetNeuronsButtonPushed(app, event)
data = Read_Data(app);
if isempty(data)
return
end
if strcmp(app.HighlightEnsembleDropDown.Value,'no')
neurons = data.Neurons;
assignin('base',...
['all_neurons_' data.Movie.DataName],neurons)
else
get_onsemble = app.PlotOnsembleCheckBox.Value;
get_offsemble = app.PlotOffsembleCheckBox.Value;
get_nonparticipant = app.PlotNonparticipantCheckBox.Value;
ensemble_number = str2double(app.HighlightEnsembleDropDown.Value);
onsemble_id = data.Analysis.Ensembles.OnsembleNeurons{ensemble_number};
offsemble_id = data.Analysis.Ensembles.OffsembleNeurons{ensemble_number};
nonparticipant_id = setdiff(1:data.Analysis.Neurons,onsemble_id);
nonparticipant_id = setdiff(nonparticipant_id,offsemble_id);
if get_onsemble
neurons = data.Neurons(onsemble_id);
assignin('base',...
['neurons_on_' num2str(ensemble_number) '_' data.Movie.DataName],neurons)
end
if get_offsemble
neurons = data.Neurons(offsemble_id);
assignin('base',...
['neurons_off_' num2str(ensemble_number) '_' data.Movie.DataName],neurons)
end
if get_nonparticipant
neurons = data.Neurons(nonparticipant_id);
assignin('base',...
['neurons_non_' num2str(ensemble_number) '_' data.Movie.DataName],neurons)
end
end
end
% Button pushed function: PlotSignalsButton
function PlotSignalsButtonPushed(app, event)
data = Read_Data(app);
if isempty(data)
return
end
if strcmp(app.HighlightEnsembleDropDown.Value,'no')
id = 1:length(data.Neurons);
else
get_onsemble = app.PlotOnsembleCheckBox.Value;
get_offsemble = app.PlotOffsembleCheckBox.Value;
get_nonparticipant = app.PlotNonparticipantCheckBox.Value;
ensemble_number = str2double(app.HighlightEnsembleDropDown.Value);
onsemble_id = data.Analysis.Ensembles.OnsembleNeurons{ensemble_number};
offsemble_id = data.Analysis.Ensembles.OffsembleNeurons{ensemble_number};
nonparticipant = setdiff(1:data.Analysis.Neurons,onsemble_id);
nonparticipant = setdiff(nonparticipant,offsemble_id);
id = [];
if get_onsemble
id = [id onsemble_id];
end
if get_offsemble
id = [id offsemble_id];
end
if get_nonparticipant
id = [id nonparticipant];
end
end
signal_type = app.SelectSignalsDropDown.Value;
switch signal_type
case 'binary'
signals = data.Transients.Raster(id,:);
y_label = 'neuron #';
case 'raw'
signals = data.Transients.Raw(id,:);
y_label = 'fluorescence';
case 'filtered'
signals = data.Transients.Filtered(id,:);
y_label = 'fluorescence';
case 'smoothed'
signals = data.Transients.Smoothed(id,:);
y_label = 'fluorescence';
case 'inference'
signals = data.Transients.Inference(id,:);
y_label = 'spike inference';
end
Set_Figure([data.Movie.DataName ' - ' signal_type],[0 0 1200 700])
if strcmp(signal_type,'binary')
Plot_Raster(signals)
Set_Label_Time(size(signals,2),data.Movie.FPS)
else
Plot_Transients(signals,'separated',data.Movie.FPS,[0 0 0])
end
ylabel(y_label)
end
% Button pushed function: ReplayActivityButton
function ReplayActivityButtonPushed(app, event)
% Read data
data = Read_Data(app);
if isempty(data)
return
end
% Get number of frames
n_frames = data.Movie.Frames;
% Check in workspace
replay_name = ['replay_' data.Movie.DataName];
if evalin('base',['exist(''replay_' data.Movie.DataName ''',''var'')'])
app.Movie = evalin('base',replay_name);
else
app.Movie = [];
end
% If there is no movie
if isempty(app.Movie)
% Recreate movie
width = data.Movie.Width;
height = data.Movie.Height;
signals = data.Transients.Filtered;
app.Movie = Recreate_Movie(width,height,data.Neurons,signals);
% Save in workspace
assignin('base',['replay_' data.Movie.DataName],app.Movie);
% Save in file
Save_Tiff_Fast(app.Movie,[data.Movie.DataName ' - Replay.tiff'])
ReplayActivityButtonPushed(app,[])
else
% Create figure
factor = data.Movie.Width/data.Movie.Height;
hfigure = Set_Figure([data.Movie.DataName ' - Replay'],[0 0 round(400*factor) 500]);
% Image plot
frame_axes = Set_Axes('frame_axes',[0 0.4 1 0.6]);
frame_image = imshow(squeeze(app.Movie(:,:,1,:)));
% Voltage recording plot
external_axes = Set_Axes('external_axes',[0 0 1 0.2]);
% Xsemble activity plot
xsemble_axes = Set_Axes('xsemble_axes',[0 0.2 1 0.2]);
% Current frame plot
current_axes = Set_Axes('current_axes',[0 0 1 0.4]);
% Create structure with convenient variables
h.hfigure = hfigure;
h.frame_axes = frame_axes;
h.frame_image = frame_image;
h.xsemble_axes = xsemble_axes;
h.external_axes = external_axes;
h.current_axes = current_axes;
h.fps = data.Movie.FPS;
% Add scrollbar
h.frame_scrollbar = uicontrol(hfigure,'style','slider','units','normalized','position',[0.1 0.97 0.9 0.03],...
'sliderstep',[1/n_frames 10/n_frames]);
h.frame_text = uicontrol(hfigure,'style','text','units','normalized','position',[0 0.97 0.1 0.03],...
'string','frame: 1','BackgroundColor',[1 1 1]);
% Add second scrollbar
h.gamma_text = uicontrol(hfigure,'style','text','units','normalized','position',[0 0.88 0.1 0.04],...
'string','gamma correction: 0.5','BackgroundColor',[1 1 1]);
h.gamma_scrollbar = uicontrol(hfigure,'style','slider','units','normalized','position',[0 0.85 0.1 0.03],...
'sliderstep',[0.05 0.1],'Value',0.5);
% Event scrollbar
addlistener(h.frame_scrollbar,'Value','PreSet',@(~,~)ReplayScroll_Callback(app,h));
% Event scrollbar
addlistener(h.gamma_scrollbar,'Value','PostSet',@(~,~)GammaScroll_Callback(app,h));
% Plot xsemble activity
axes(xsemble_axes);
hold(xsemble_axes,'on')
raster = data.Analysis.Raster;
ensemble_number = str2double(app.HighlightEnsembleDropDown.Value);
plot_contrast_ratio = false;
if isnan(ensemble_number)
plot(xsemble_axes,mean(raster,1),'k')
legend_text = 'entire population';
else
onsemble_neurons = data.Analysis.Ensembles.OnsembleNeurons{ensemble_number};
offsemble_neurons = data.Analysis.Ensembles.OffsembleNeurons{ensemble_number};
nonparticipant_neurons = setdiff(1:data.Analysis.Neurons,onsemble_neurons);
nonparticipant_neurons = setdiff(nonparticipant_neurons,offsemble_neurons);
legend_text = {};
if app.PlotOnsembleCheckBox.Value
if app.PlotOffsembleCheckBox.Value
onsemble_fraction = mean(raster(onsemble_neurons,:),1);
offsemble_fraction = mean(raster(offsemble_neurons,:),1);
contrast_ratio = (onsemble_fraction+0.05)./(offsemble_fraction+0.05);
plot(xsemble_axes,contrast_ratio/max(contrast_ratio),'color',[0.8 0 0.8])
legend_text{end+1} = 'ratio ONsemble/OFFsemble';
plot_contrast_ratio = true;
end
plot(xsemble_axes,mean(raster(onsemble_neurons,:),1),'color',[1 0.7 0.7])
legend_text{end+1} = 'onsemble neurons';
end
if app.PlotOffsembleCheckBox.Value
plot(xsemble_axes,mean(raster(offsemble_neurons,:),1),'color',[0.7 0.7 1])
legend_text{end+1} = 'offsemble neurons';
end
if app.PlotNonparticipantCheckBox.Value
plot(xsemble_axes,mean(raster(nonparticipant_neurons,:),1),'color',[0.7 0.7 0.7])
legend_text{end+1} = 'nonparticipant neurons';
end
Plot_Area(data.Analysis.Ensembles.Activity(ensemble_number,:),...
0,[0.8 0 0.8],0.2)
end
set(xsemble_axes,'box','off','xtick',[],'ytick',0:0.5:1)
if plot_contrast_ratio
set(xsemble_axes,'yticklabel',...
{'0',['0.5/' num2str(max(contrast_ratio)/2,'%0.1f')],...
['1/' num2str(max(contrast_ratio),'%0.1f')]})
ylabel(xsemble_axes,{'fraction of','active neurons','/contrast ratio'})
else
ylabel(xsemble_axes,{'fraction of','active neurons'})
end
l1 = legend(xsemble_axes,legend_text);
l1.Position(1) = 0.88;
% Plot current frame
axes(current_axes)
plot([10 10],[0 1],'--k')
set(current_axes,'ylim',[0 1])
axis(current_axes,'off')
% Plot external recording
cla(external_axes); hold on
% Plot voltage recording
if isfield(data,'VoltageRecording')
voltage = data.VoltageRecording;
legend_text = {};
max_y = 15;
if isfield(voltage,'Locomotion')
plot(external_axes,voltage.Locomotion,'k')
xlim([0 length(voltage.Locomotion)])
ylabel({'locomotion','[cm/s]'})
legend_text = {'locomotion'};
end
if isfield(voltage,'Licking')
axes(external_axes);
Plot_Area(rescale(voltage.Licking,0,max_y),0,[0.8 0.8 0.8],0.5)
xlim([0 length(voltage.Licking)])
legend_text = [legend_text {'licking'}];
end
if isfield(voltage,'Laser')
axes(external_axes);
Plot_Area(rescale(voltage.Laser,0,max_y),0,[0.3 0.3 0.3],0.5)
xlim([0 length(voltage.Laser)])
legend_text = [legend_text {'laser'}];
end
if isfield(voltage,'Stimuli')
axes(external_axes);
if nnz(voltage.Stimuli)
stimuli = voltage.Stimuli;
if nnz(stimuli)
stim_text = '→↗↑↖←↙↓↘';
legends = Plot_Stimulation(stimuli,max_y,stim_text);
legend_text = [legend_text legends];
end
end
end
linkaxes([external_axes xsemble_axes current_axes],'x')
Set_Label_Time(data.Analysis.Frames,data.Movie.FPS,0,h.external_axes)
ylim([0 max_y])
ylabel({'running speed','(cm/s)'})
box off
l = legend(legend_text);
l.Position(1)=0.91;
l.Position(2)=0.05;
else
linkaxes([xsemble_axes current_axes],'x')
Set_Label_Time(data.Analysis.Frames,data.Movie.FPS,0,xsemble_axes)
end
end
end
% Value changed function: SetThresholdVisuallyCheckBox
function SetThresholdVisuallyCheckBoxValueChanged(app, event)
value = app.SetThresholdVisuallyCheckBox.Value;
if value
app.PSNRdBThSpinner.Enable = 'off';
else
app.PSNRdBThSpinner.Enable = 'on';
end
end
% Button pushed function: PlottrialsButton
function PlottrialsButtonPushed(app, event)
% Read data
data = Read_Data(app);
if isempty(data)
return
end
selection = app.StimuliDropDown.Value;
if strcmp(selection,'-- select stimulus --')