-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSummaryPlot.m
1806 lines (1585 loc) · 74.1 KB
/
SummaryPlot.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 = SummaryPlot(varargin)
% SUMMARYPLOT MATLAB code for SummaryPlot.fig
% SUMMARYPLOT, by itself, creates a new SUMMARYPLOT or raises the existing
% singleton*.
%
% H = SUMMARYPLOT returns the handle to a new SUMMARYPLOT or the handle to
% the existing singleton*.
%
% SUMMARYPLOT('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SUMMARYPLOT.M with the given input arguments.
%
% SUMMARYPLOT('Property','Value',...) creates a new SUMMARYPLOT or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before SummaryPlot_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to SummaryPlot_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 SummaryPlot
% Last Modified by GUIDE v2.5 21-Feb-2013 23:14:00
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @SummaryPlot_OpeningFcn, ...
'gui_OutputFcn', @SummaryPlot_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 SummaryPlot is made visible.
function SummaryPlot_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 command line arguments to SummaryPlot (see VARARGIN)
% global directory slash user;
global filename tasktype;
% Choose default command line output for SummaryPlot
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% This sets up the initial plot - only do when we are invisible
% so window can get raised using SummaryPlot.
if strcmp(get(hObject,'Visible'),'off')
% axes(findobj('Tag','defaultaxes'));
plot(rand(5));
set(gca,'XTick',[],'YTick',[],'XColor','white','YColor','white','parent',handles.mainfig);%'YDir','reverse'
box off;
cla;
end
% set(gcf,'Color','white')
% get the arguments passed to the GUI
%if length(varargin)>3
if 0
plotstart=str2num(varargin{4});
plotstop=str2num(varargin{5});
fsigma=str2num(varargin{6});
chrono=varargin{7}; %for plotting rasters in chronological order
causker=varargin{8}; %kernel shape
else
plotstart=1000;
plotstop=500;
fsigma=10;
chrono=0; %for plotting rasters in chronological order
causker=0;
end
if length(varargin)>2
if iscell(varargin{3})
if size(varargin{3},1)>1
tasktype = varargin{3};
else
tasktype=cell2mat(varargin{3});
end
else
tasktype = varargin{3};
end
else
tasktype=[];
end
if length(varargin)>1
filename=varargin{2};
else
filename=[];
end
%% for batch processing
if ischar(varargin{1})
if strfind(varargin{1},'allcx')
batchplot_as(varargin,handles); %activity summary, without eye evelocity or rasters
else
batchplot(varargin,handles);
end
return
end
%% for single file display
if strcmp(tasktype,'optiloc')
optilocplot(varargin,handles);
return
end
set(findobj('tag','dispfilename'),'string',filename);
set(findobj('tag','disptaskname'),'string',tasktype);
alignedata=struct(varargin{1});
alignment=cell2mat(unique({alignedata.alignlabel}));
set(findobj('tag','dispalignment'),'string',alignment);
%figdimension=get(findobj('tag','mainfig'),'Position');
figdimension=get(gca,'Position');
rasterdim=[figdimension(1)*1.1 (figdimension(4)*0.66)+figdimension(2)*1.1 figdimension(3)*0.9 figdimension(4)*0.3];
cc=lines(length(alignedata));
if size(cc,1)==8
cc(8,:)=[0 0.75 0];
end
% if chrono
% cut_chrasters=zeros(length([alignedata.trials]),plotstart+plotstop+1);
% end
numsubplot=length(alignedata)*3; %dividing the panel in three compartments with wequal number of subplots
if numsubplot==3
numsubplot=6;
end
%setappdata(gcf, 'SubplotDefaultAxesLocation', [0, 0, 1, 1]);
%Plot rasters
%rastersh = axes('Position', rasterdim, 'Layer','top','XTick',[],'YTick',[],'XColor','white','YColor','white');
numrast=length(alignedata);
for rastnum=1:numrast
rasters=alignedata(rastnum).rasters;
alignidx=alignedata(rastnum).alignidx;
if chrono
cut_chrasters=zeros(length([alignedata.trials]),plotstart+plotstop+1);
%listing relevant trials in a continuous series with other rasters
chronoidx=ismember(sort([alignedata.trials]),alignedata(rastnum).trials);
end
greyareas=alignedata(rastnum).allgreyareas;
start=alignidx - plotstart;
stop=alignidx + plotstop;
if start < 1
start = 1;
end
if stop > length(rasters)
stop = length(rasters);
end
%trials = size(rasters,1);
isnantrial=zeros(1,size(rasters,1));
if chrono
onerastplot=subplot(numsubplot,1,1:(numsubplot/3),'Layer','top', ...
'XTick',[],'YTick',[],'XColor','white','YColor','white', 'Parent', handles.mainfig);
else
if numrast==1
hrastplot(rastnum)=subplot(numsubplot,1,1:2,'Layer','top', ...
'XTick',[],'YTick',[],'XColor','white','YColor','white', 'Parent', handles.mainfig);
else
hrastplot(rastnum)=subplot(numsubplot,1,rastnum,'Layer','top', ...
'XTick',[],'YTick',[],'XColor','white','YColor','white', 'Parent', handles.mainfig);
end
end
%reducing spacing between rasters
if numrast>1 && ~chrono
rastpos=get(gca,'position');
rastpos(2)=rastpos(2)+rastpos(4)*0.5;
set(gca,'position',rastpos);
end
% sorting rasters according greytime
viscuetimes=nan(size(greyareas,2),2);
for grst=1:size(greyareas,2)
viscuetimes(grst,:)=greyareas{grst}(1,:);
end
if ~chrono
cuestarts=viscuetimes(:,1);
[~,sortidx]=sort(cuestarts,'descend');
viscuetimes=viscuetimes(sortidx,:);
rasters=rasters(sortidx,:);
end
hold on
cut_rasters = rasters(:,start:stop); % Isolate rasters of interest
cut_rast_siz = size(cut_rasters);
isnantrial = isnan(sum(cut_rasters,2)); % Identify nantrials
cut_rasters(isnan(cut_rasters)) = 0; % take nans out so they don't get plotted
if chrono
cut_chrasters(chronoidx,:)=cut_rasters;
[indy, indx] = ind2sub(size(cut_chrasters),find(cut_chrasters)); %find row and column coordinates of spikes
else
[indy, indx] = ind2sub(size(cut_rasters),find(cut_rasters)); %find row and column coordinates of spikes
end
if(size(rasters,1) == 1)
plot([indx;indx],[indy;indy+1],'color',cc(rastnum,:),'LineStyle','-'); % plot rasters
else
plot([indx';indx'],[indy';indy'+1],'color',cc(rastnum,:),'LineStyle','-'); % plot rasters
end
% drawing the grey areas
try
greytimes=viscuetimes-start;
greytimes(greytimes<0)=0;
greytimes(greytimes>(stop-start+1))=stop-start+1;
catch
greytimes=0;
end
if ~sum(sum(isnan(greytimes))) && logical(sum(sum(greytimes))) && ~chrono
grxlims=[greytimes';greytimes(:,2:-1:1)'];
grylims=[1:size(grxlims,2);1:size(grxlims,2);2:size(grxlims,2)+1;2:size(grxlims,2)+1];
patch(grxlims, grylims, [0 0 0], 'EdgeColor', 'none','FaceAlpha', 0.2)
end
set(gca,'xlim',[1 length(start:stop)]);
axis(gca, 'off'); % axis tight sets the axis limits to the range of the data.
%% Plot sdf
sdfplot=subplot(numsubplot,1,(numsubplot/3)+1:(numsubplot/3)+(numsubplot/3),'Layer','top','Parent', handles.mainfig);
%sdfh = axes('Position', [.15 .65 .2 .2], 'Layer','top');
title('Spike Density Function','FontName','calibri','FontSize',11);
hold on;
if size(rasters(~isnantrial,:),1)<5 %if less than 5 good trials
%useless plotting this
sumall=NaN;
else
sumall=sum(rasters(~isnantrial,start:stop));
end
% sdf=spike_density(sumall,fsigma)./length(find(~isnantrial)); %instead of number of trials
sdf=fullgauss_filtconv(sumall,fsigma,causker)./length(find(~isnantrial)).*1000;
% sdf=sdf(fsigma+1:end-fsigma);
%% calculate confidence intervals
lcut_rasters=rasters(~isnantrial,start:stop);
smoothtrial=zeros(size(lcut_rasters));
for crsem=1:size(rasters(~isnantrial),1)
smoothtrial(crsem,:)=fullgauss_filtconv(lcut_rasters(crsem,:),fsigma,causker).*1000;
end
% smoothtrial=smoothtrial(:,fsigma+1:end-fsigma);
if numrast==2 && rastnum==1 %collect old trials
first_smtrials=smoothtrial;
end
rastsem=std(smoothtrial)/ sqrt(size(smoothtrial,1)); %standard error of the mean
%norminv([.025 .975], mean(smoothtrial), std(smoothtrial));
rastsem = rastsem * 1.96; % 95% of the data will fall within 1.96 standard deviations of a normal distribution
% testif significant diff
% differential spike density
% function exceeded by 2 SD the mean difference in activity
% during the 600-ms interval before target presentation, pro
% vided the difference reached 6 SD and remained >2 SD
% threshold for 50 ms.
if numrast==2 && rastnum==numrast
diff_trials=mean(first_smtrials)-mean(smoothtrial);
diff_preal_epoch=diff_trials(alignidx-start-200:alignidx-start);
difftime_preal=find(abs(diff_preal_epoch)>2*(std(diff_preal_epoch)),1);
diff_postal_epoch=diff_trials(alignidx-start:alignidx-start+200);
difftime_postal=find(abs(diff_postal_epoch)>2*(std(diff_postal_epoch)),1);
if ~isempty(difftime_preal)
%recursive time search
difftime_preal=difftime_preal-find(abs(diff_trials(alignidx-start-200+difftime_preal+1:-1:1))<=2*(std(diff_preal_epoch)),1);
difftime_preal=alignidx-start-200+1+difftime_preal;
end
if ~isempty(difftime_postal)
%recursive time search
difftime_postal=difftime_postal-find(abs(diff_trials(alignidx-start+difftime_postal+1:-1:1))<=2*(std(diff_preal_epoch)),1);
difftime_postal=alignidx-start+1+difftime_postal;
end
else
difftime_preal=[];
difftime_postal=[];
end
if size(rasters(~isnantrial,:),1)>=5
% plot confidence intervals
patch([1:length(sdf),fliplr(1:length(sdf))],[sdf-rastsem,fliplr(sdf+rastsem)],cc(rastnum,:),'EdgeColor','none','FaceAlpha',0.1);
%plot sdf
plot(sdf,'Color',cc(rastnum,:),'LineWidth',1.8);
if ~isempty(difftime_preal)
plot(difftime_preal,max([sdf(difftime_preal)-40 1]),'r*')
end
if ~isempty(difftime_postal)
plot(difftime_postal,max([sdf(difftime_postal)-40 1]),'r*')
end
end
% axis([0 stop-start 0 200])
axis(gca,'tight');
box off;
set(gca,'Color','white','TickDir','out','FontName','calibri','FontSize',8); %'YAxisLocation','rigth'
% hxlabel=xlabel(gca,'Time (ms)','FontName','calibri','FontSize',8);
% set(hxlabel,'Position',get(hxlabel,'Position') - [180 -0.2 0]); %doesn't stay there when export !
hylabel=ylabel(gca,'Firing rate (spikes/s)','FontName','calibri','FontSize',8);
currylim=get(gca,'YLim');
if ~isempty(rasters)
% drawing the alignment bar
patch([repmat((alignidx-start)-2,1,2) repmat((alignidx-start)+2,1,2)], ...
[[0 currylim(2)] fliplr([0 currylim(2)])], ...
[0 0 0 0],[1 0 0],'EdgeColor','none','FaceAlpha',0.5);
end
%Plot eye velocities
heyevelplot=subplot(numsubplot,1,(numsubplot*2/3)+1:numsubplot,'Layer','top','Parent', handles.mainfig);
title('Mean Eye Velocity','FontName','calibri','FontSize',11);
hxlabel=xlabel(gca,'Time (ms)','FontName','calibri','FontSize',8);
hold on;
if ~isempty(rasters)
eyevel=alignedata(rastnum).eyevel;
eyevel=mean(eyevel(:,start:stop));
heyevelline(rastnum)=plot(eyevel,'Color',cc(rastnum,:),'LineWidth',1);
%axis(gca,'tight');
eyevelymax=max(eyevel);
if eyevelymax>0.8
eyevelymax=eyevelymax*1.1;
else
eyevelymax=0.8;
end
axis([0 stop-start 0 eyevelymax]);
set(gca,'Color','none','TickDir','out','FontSize',8,'FontName','calibri','box','off');
ylabel(gca,'Eye velocity (deg/ms)','FontName','calibri','FontSize',8);
patch([repmat((alignidx-start)-2,1,2) repmat((alignidx-start)+2,1,2)], ...
[get(gca,'YLim') fliplr(get(gca,'YLim'))], ...
[0 0 0 0],[1 0 0],'EdgeColor','none','FaceAlpha',0.5);
% get directions for the legend
curdir{rastnum}=alignedata(rastnum).dir;
aligntype{rastnum}=alignedata(rastnum).alignlabel;
else
curdir{rastnum}='no';
aligntype{rastnum}='data';
end
end
%moving up all rasters now
if chrono
allrastpos=get(onerastplot,'position');
else
if numrast==1
allrastpos=get(hrastplot,'position');
else
allrastpos=cell2mat(get(hrastplot,'position'));
end
end
disttotop=allrastpos(1,2)+allrastpos(1,4);
if disttotop<0.99 %if not already close to top of container
allrastpos(:,2)=allrastpos(:,2)+(1-disttotop)/1.5;
end
if chrono
set(onerastplot,'position',allrastpos);
else
if numrast>1
allrastpos=mat2cell(allrastpos,ones(1,size(allrastpos,1))); %reconversion to cell .. un brin penible
set(hrastplot,{'position'},allrastpos);
else
set(hrastplot,'position',allrastpos);
end
end
%moving down the eye velocity plot
eyevelplotpos=get(heyevelplot,'position');
eyevelplotpos(1,2)=eyevelplotpos(1,2)-(eyevelplotpos(1,2))/1.5;
set(heyevelplot,'position',eyevelplotpos);
% x axis tick labels
set(heyevelplot,'XTick',[0:100:(stop-start)]);
set(heyevelplot,'XTickLabel',[-plotstart:100:plotstop]);
% plot a legend in this last graph
clear spacer
spacer(1:numrast,1)={' '};
%cellfun('isempty',{alignedata(:).dir})
if logical(sum(cell2mat(strfind(aligntype,'error1'))) || sum(cell2mat(strfind(aligntype,'error2'))))
aligntype{~cellfun(@(x) (strcmp(x,'error1') || strcmp(x,'error2')), aligntype)}=...
['good trial ' aligntype{~cellfun(@(x) (strcmp(x,'error1') || strcmp(x,'error2')), aligntype)}];
aligntype(cellfun(@(x) (strcmp(x,'error1') || strcmp(x,'error2')), aligntype))={'wrong trial'};
end
hlegdir = legend(heyevelline, strcat(aligntype',spacer,curdir'),'Location','NorthWest');
set(hlegdir,'Interpreter','none', 'Box', 'off','LineWidth',1.5,'FontName','calibri','FontSize',9);
% setting sdf plot y axis
ylimdata=get(findobj(sdfplot,'Type','line'),'YDATA');
if ~iscell(ylimdata)
ylimdata={ylimdata};
end
if sum((cell2mat(cellfun(@(x) logical(isnan(sum(x))), ylimdata, 'UniformOutput', false)))) %if NaN data
% ylimdata=ylimdata(~(cell2mat(cellfun(@(x) logical(isnan(sum(x))),...
% ylimdata, 'UniformOutput', false))));
% I think I went overboard with that one. Much simpler:
ylimdata={ylimdata{1}(~isnan(ylimdata{:}))};
end
if sum(logical(cellfun(@(x) length(x),ylimdata)-1))~=length(ylimdata) %some strange data with a single value
ylimdata=ylimdata(logical(cellfun(@(x) length(x),ylimdata)-1));
end
newylim=[0, ceil(max(max(cell2mat(ylimdata)))/10)*10]; %rounding up to the decimal
set(sdfplot,'YLim',newylim);
% x axis tick labels
set(sdfplot,'XTick',[0:100:(stop-start)]);
set(sdfplot,'XTickLabel',[-plotstart:100:plotstop]);
function batchplot_as(arguments,handles)
global directory slash;
if ~isdir([directory,'figures',slash,arguments{1}])
mkdir([directory,'figures',slash,arguments{1}]);
end
filelist=arguments{2};
infolist=arguments{3};
algdir=[directory,'processed',slash,'aligned',slash];
for algfile=1:length(filelist)
filename=filelist{algfile};
fileinfo=infolist(algfile,:);
set(findobj('tag','dispfilename'),'string',filename);
set(findobj('tag','disptaskname'),'string',fileinfo{1});
load([algdir,filename,'_sac.mat']);
alignment=dataaligned(1,1).savealignname(max(strfind(dataaligned(1,1).savealignname,'_'))+1:end);
set(findobj('tag','dispalignment'),'string',alignment);
figuresize=getpixelposition(handles.mainfig);
figuresize(1:2)=[80 167];
exportfigname=[directory,'figures',slash,arguments{1},slash,filename,'_',fileinfo{1},'_',fileinfo{2}];
exportfig=figure('color','white','position',figuresize);
%figdimension=get(findobj('tag','mainfig'),'Position');
% rasterdim=[figuresize(1)*1.1 (figuresize(4)*0.66)+figuresize(2)*1.1 figuresize(3)*0.9 figuresize(4)*0.3];
plotstart=600;
plotstop=500;
fsigma=20;
causeker = 0;
cc=lines(length(dataaligned));
% numsubplot=length(dataaligned)*3; %dividing the panel in three compartments with wequal number of subplots
% if numsubplot==3
% numsubplot=6;
% end
%
numrast=length(dataaligned);
% failed=zeros(numrast,1);
clear hsdfline aligntype curdir;
%% Plot sdf for best direction
gooddirs=find(arrayfun(@(x) nansum(x{:}.h), {dataaligned(~cellfun(@isempty, {dataaligned.stats})).stats}));
maxmeandiffs=arrayfun(@(x) max(x{:}.p), {dataaligned(gooddirs).stats});
bestdir=gooddirs(maxmeandiffs==max(maxmeandiffs));
bdrasters=dataaligned(bestdir).rasters;
bdalignidx=dataaligned(bestdir).alignidx;
if ~ isempty(bdrasters)
start=bdalignidx - plotstart;
stop=bdalignidx + plotstop;
if start < 1
start = 1;
end
if stop > length( bdrasters )
stop = length( bdrasters );
end
isnantrial=zeros(1,size(bdrasters,1));
for rastpos=1:size(bdrasters,1) %plotting rasters trial by trial
if isnan(sum(bdrasters(rastpos,start:stop)))
isnantrial(rastpos)=1;
bdrasters(rastpos,isnan(bdrasters(rastpos,:)))=0;
end
end
sdfplot_bestdir=subplot(3,1,1,'Layer','top');%,'Parent', handles.mainfig
%sdfh = axes('Position', [.15 .65 .2 .2], 'Layer','top');
%title('SDF: best direction','FontName','calibri','FontSize',11);
hold on;
if size(bdrasters,1)==1 %if only one good trial
sumall=bdrasters(~isnantrial,start:stop);
else
sumall=sum(bdrasters(~isnantrial,start:stop));
end
% bdsdf=spike_density(sumall,fsigma)./length(find(~isnantrial)); %instead of number of trials
bdsdf=fullgauss_filtconv(sumall,fsigma,causker)./length(find(~isnantrial)).*1000;
% bdsdf=bdsdf(fsigma+1:end-fsigma);
bdsdfline=plot(bdsdf,'Color',[0.4389 0.1111 0.2581],'LineWidth',1.8);
% axis([0 stop-start 0 200])
axis(gca,'tight');
% currylim=get(gca,'YLim');
% set(gca,'Ylim',[0 currylim(2)]);
box off;
set(gca,'Color','white','TickDir','out','FontName','calibri','FontSize',8); %'YAxisLocation','rigth'
% hxlabel=xlabel(gca,'Time (ms)','FontName','calibri','FontSize',8);
% set(hxlabel,'Position',get(hxlabel,'Position') - [180 -0.2 0]); %doesn't stay there when export !
hylabel=ylabel(gca,'Firing rate (spikes/s)','FontName','calibri','FontSize',8);
currylim=get(gca,'YLim');
% drawing the alignment bar
patch([repmat((bdalignidx-start)-2,1,2) repmat((bdalignidx-start)+2,1,2)], ...
[[0 currylim(2)] fliplr([0 currylim(2)])], ...
[0 0 0 0],[1 0 0],'EdgeColor','none','FaceAlpha',0.5);
axis(gca,'tight');
ylimst=round(currylim(2)/10)*10;
% hlegbdsdf = legend(bdsdfline, 'Best Direction' ,'Location','NorthEast'); % strcat(aligntype',spacer,curdir')
% set(hlegbdsdf,'Interpreter','none', 'Box', 'off','LineWidth',1.5,'FontName','calibri','FontSize',9);
text(50,ylimst,['Active: ',num2str(fileinfo{9})],'Interpreter','none','LineWidth',1.5,'FontName','calibri','FontSize',9);
text(50,ylimst*(85/100),['MMD: ',num2str(fileinfo{10})],'Interpreter','none','LineWidth',1.5,'FontName','calibri','FontSize',9);
text(50,ylimst*(71/100),fileinfo{2},'Interpreter','none','LineWidth',1.5,'FontName','calibri','FontSize',9);
text(50,ylimst*(57/100),fileinfo{3},'Interpreter','none','LineWidth',1.5,'FontName','calibri','FontSize',9);
text(200,ylimst*(57/100),fileinfo{4},'Interpreter','none','LineWidth',1.5,'FontName','calibri','FontSize',9);
%text(150,200,fileinfo{5},'Interpreter','none','LineWidth',1.5,'FontName','calibri','FontSize',9);
text(50,ylimst*(42/100),fileinfo{6},'Interpreter','none','LineWidth',1.5,'FontName','calibri','FontSize',9);
text(50,ylimst*(28/100),fileinfo{7},'Interpreter','none','LineWidth',1.5,'FontName','calibri','FontSize',9);
end
%% Plot sdf for all directions collapsed together
colsdf=nan(numrast,abs(plotstart-plotstop)+1);
for rstplt=1:numrast
colrasters=dataaligned(rstplt).rasters;
colalignidx=dataaligned(rstplt).alignidx;
if ~ isempty(colrasters)
start=colalignidx - plotstart;
stop=colalignidx + plotstop;
if start < 1
start = 1;
end
if stop > length( colrasters )
stop = length( colrasters );
end
isnantrial=zeros(1,size(colrasters,1));
for rastpos=1:size(colrasters,1) %plotting rasters trial by trial
if isnan(sum(colrasters(rastpos,start:stop)))
isnantrial(rastpos)=1;
colrasters(rastpos,isnan(colrasters(rastpos,:)))=0;
end
end
if size(colrasters,1)==1 %if only one good trial
sumall=colrasters(~isnantrial,start:stop);
else
sumall=sum(colrasters(~isnantrial,start:stop));
end
colsdf(rstplt,1:length(sumall))=spike_density(sumall,fsigma)./length(find(~isnantrial)); %instead of number of trials
end
end
colsdf=nansum(colsdf)./size(colsdf,1);
colplot_alldir=subplot(3,1,2,'Layer','top');%,'Parent', handles.mainfig
%sdfh = axes('Position', [.15 .65 .2 .2], 'Layer','top');
title('SDF: all directions collapsed','FontName','calibri','FontSize',11);
hold on;
colsdfline=plot(colsdf,'Color',[0.8212 0.0154 0.0430],'LineWidth',1.8);
% axis([0 stop-start 0 200])
axis(gca,'tight');
% currylim=get(gca,'YLim');
% set(gca,'Ylim',[0 currylim(2)]);
box off;
set(gca,'Color','white','TickDir','out','FontName','calibri','FontSize',8); %'YAxisLocation','rigth'
% hxlabel=xlabel(gca,'Time (ms)','FontName','calibri','FontSize',8);
% set(hxlabel,'Position',get(hxlabel,'Position') - [180 -0.2 0]); %doesn't stay there when export !
hylabel=ylabel(gca,'Firing rate (spikes/s)','FontName','calibri','FontSize',8);
currylim=get(gca,'YLim');
% drawing the alignment bar
patch([repmat((colalignidx-start)-2,1,2) repmat((colalignidx-start)+2,1,2)], ...
[[0 currylim(2)] fliplr([0 currylim(2)])], ...
[0 0 0 0],[1 0 0],'EdgeColor','none','FaceAlpha',0.5);
axis(gca,'tight');
hlegcolsdf = legend(colsdfline, 'all directions' ,'Location','NorthWest'); % strcat(aligntype',spacer,curdir')
set(hlegcolsdf,'Interpreter','none', 'Box', 'off','LineWidth',1.5,'FontName','calibri','FontSize',9);
%% plot sdf for all directions separately
sdfplot=subplot(3,1,3,'Layer','top');%,'Parent', handles.mainfig
%sdfh = axes('Position', [.15 .65 .2 .2], 'Layer','top');
title('SDF: all directions','FontName','calibri','FontSize',11);
hold on;
for rstplt=1:numrast
rasters=dataaligned(rstplt).rasters;
alignidx=dataaligned(rstplt).alignidx;
% greyareas=dataaligned(rstplt).allgreyareas;
if ~ isempty(rasters)
start=alignidx - plotstart;
stop=alignidx + plotstop;
if start < 1
start = 1;
end
if stop > length( rasters )
stop = length( rasters );
end
trials = size(rasters,1);
isnantrial=zeros(1,size(rasters,1));
%% plotting rasters (removed)
% if numrast==1
% hrastplot(rstplt)=subplot(numsubplot,1,1:2,'Layer','top', ...
% 'XTick',[],'YTick',[],'XColor','white','YColor','white');%'Parent', handles.mainfig
% else
% hrastplot(rstplt)=subplot(numsubplot,1,rstplt,'Layer','top', ...
% 'XTick',[],'YTick',[],'XColor','white','YColor','white');%'Parent', handles.mainfig
% end
% %reducing spacing between rasters
% if numrast>1
% rastpos=get(gca,'position');
% rastpos(2)=rastpos(2)+rastpos(4)*0.5;
% set(gca,'position',rastpos);
% end
%
% % sorting rasters according greytime
% viscuetimes=nan(size(greyareas,2),2);
% for grst=1:size(greyareas,2)
% viscuetimes(grst,:)=greyareas{grst}(1,:);
% end
% cuestarts=viscuetimes(:,1);
% try
% [cuestarts,sortidx]=sort(cuestarts,'descend');
% catch
% sortidx=[1:size(viscuetimes,1)];
% end
% viscuetimes=viscuetimes(sortidx,:);
% if size(rasters,1)<length(sortidx)
% % ca deconne
% size(rasters,1)
% else
% rasters=rasters(sortidx,:);
% end
%
% %axis([0 stop-start+1 0 size(rasters,1)]);
% hold on
% for rastpos=1:size(rasters,1) %plotting rasters trial by trial
% if isnan(sum(rasters(rastpos,start:stop)))
% isnantrial(rastpos)=1;
% rasters(rastpos,isnan(rasters(rastpos,:)))=0;
% end
% spiketimes=find(rasters(rastpos,start:stop)); %converting from a matrix representation to a time collection, within selected time range
% plot([spiketimes;spiketimes],[ones(size(spiketimes))*rastpos;ones(size(spiketimes))*rastpos-1],'color',cc(rstplt,:),'LineStyle','-');
%
% % drawing the grey areas
% try
% greytimes=viscuetimes(rastpos,:)-start;
% greytimes(greytimes<0)=0;
% greytimes(greytimes>(plotstart+plotstop))=plotstart+plotstop;
% catch %grey times out of designated period's limits
% greytimes=0;
% end
% % diffgrey = find(diff(greytimes)>1); % In case the two grey areas overlap, it doesn't discriminate.
% % % But that's not a problem
% % diffgreytimes = greytimes(diffgrey);
% if ~sum(isnan(greytimes)) && logical(sum(greytimes))
% patch([greytimes(1) greytimes(end) greytimes(end) greytimes(1)],[rastpos rastpos rastpos-1 rastpos-1],...
% [0 0 0], 'EdgeColor', 'none','FaceAlpha', 0.3);
% end
% % if diffgreytimes % multiple grey areas
% % %we'll see that later
% % diffgreytimes
% % pause
% % end
%
% end
% axis(gca, 'off', 'tight');
%%
% if exist('sdfplot','var')
% clf(sdfplot);
% end
% for rastpos=1:size(rasters,1)
if size(rasters,1)==1 %if only one good trial
sumall=rasters(~isnantrial,start:stop);
else
sumall=sum(rasters(~isnantrial,start:stop));
end
sdfline=spike_density(sumall,fsigma)./length(find(~isnantrial)); %instead of number of trials
hsdfline(rstplt)=plot(sdfline,'Color',cc(rstplt,:),'LineWidth',1.8);
% axis([0 stop-start 0 200])
axis(gca,'tight');
box off;
set(gca,'Color','white','TickDir','out','FontName','calibri','FontSize',8); %'YAxisLocation','rigth'
% hxlabel=xlabel(gca,'Time (ms)','FontName','calibri','FontSize',8);
% set(hxlabel,'Position',get(hxlabel,'Position') - [180 -0.2 0]); %doesn't stay there when export !
hylabel=ylabel(gca,'Firing rate (spikes/s)','FontName','calibri','FontSize',8);
currylim=get(gca,'YLim');
% drawing the alignment bar
patch([repmat((alignidx-start)-2,1,2) repmat((alignidx-start)+2,1,2)], ...
[[0 currylim(2)] fliplr([0 currylim(2)])], ...
[0 0 0 0],[1 0 0],'EdgeColor','none','FaceAlpha',0.5);
%% get directions for the legend
%curdir{rstplt}=dataaligned(rstplt).dir;
sacdeg=nan(size(dataaligned(1,rstplt).trials,2),1);
for eyetr=1:size(dataaligned(1,rstplt).trials,2)
thissach=dataaligned(1,rstplt).eyeh(eyetr,dataaligned(1,rstplt).alignidx:dataaligned(1,rstplt).alignidx+100);
thissacv=dataaligned(1,rstplt).eyev(eyetr,dataaligned(1,rstplt).alignidx:dataaligned(1,rstplt).alignidx+100);
minwidth=5;
[~, ~, thissacvel, ~, ~, ~] = cal_velacc(thissach,thissacv,minwidth);
peakvel=find(thissacvel==max(thissacvel),1);
sacendtime=peakvel+find(thissacvel(peakvel:end)<=...
(min(thissacvel(peakvel:end))+(max(thissacvel(peakvel:end))-min(thissacvel(peakvel:end)))/10),1);
try
sacdeg(eyetr)=abs(atand((thissach(sacendtime)-thissach(1))/(thissacv(sacendtime)-thissacv(1))));
catch
thissacv;
end
% sign adjustements
if thissacv(sacendtime)<thissacv(1) % negative vertical amplitude -> vertical flip
sacdeg(eyetr)=180-sacdeg(eyetr);
end
if thissach(sacendtime)>thissach(1)%inverted signal: leftward is in postive range. Correcting to negative.
sacdeg(eyetr)=360-sacdeg(eyetr); % mirror image;
end
end
% a quick fix to be able to put "upwards" directions together
distrib=hist(sacdeg,3); %floor(length(sacdeg)/2)
if max(bwlabel(distrib,4))>1 && distrib(1)>1 && distrib(end)>1 %=bimodal distribution with more than 1 outlier
sacdeg=sacdeg+45;
sacdeg(sacdeg>360)=-(360-(sacdeg(sacdeg>360)-45));
sacdeg(sacdeg>0)= sacdeg(sacdeg>0)-45;
end
sacdeg=abs(median(sacdeg));
if sacdeg>45/2 && sacdeg <= 45+45/2
curdir{rstplt}='up_right';
elseif sacdeg>45+45/2 && sacdeg <= 90+45/2
curdir{rstplt}='rightward';
elseif sacdeg>90+45/2 && sacdeg <= 135+45/2
curdir{rstplt}='down_right';
elseif sacdeg>135+45/2 && sacdeg < 180+45/2
curdir{rstplt}='downward';
elseif sacdeg>=180+45/2 && sacdeg <= 225+45/2
curdir{rstplt}='down_left';
elseif sacdeg>225+45/2 && sacdeg <= 270+45/2
curdir{rstplt}='leftward';
elseif sacdeg>270+45/2 && sacdeg <= 315+45/2
curdir{rstplt}='up_left';
else
curdir{rstplt}='upward';
end
%get alignement type
aligntype{rstplt}=dataaligned(rstplt).alignlabel;
else
curdir{rstplt}='data';
aligntype{rstplt}='no';
failed(rstplt)=1;
end
%% Plot eye velocities
% heyevelplot=subplot(numsubplot,1,(numsubplot*2/3)+1:numsubplot,'Layer','top');%,'Parent', handles.mainfig
% title('Mean Eye Velocity','FontName','calibri','FontSize',11);
% hxlabel=xlabel(gca,'Time (ms)','FontName','calibri','FontSize',8);
%
% hold on;
%
% eyevel=dataaligned(rstplt).eyevel;
% eyevel=mean(eyevel(:,start:stop));
% heyevelline(rstplt)=plot(eyevel,'Color',cc(rstplt,:),'LineWidth',1);
% %axis(gca,'tight');
% eyevelymax=max(eyevel);
% if eyevelymax>0.8
% eyevelymax=eyevelymax*1.1;
% else
% eyevelymax=0.8;
% end
% axis([0 stop-start 0 eyevelymax]);
% set(gca,'Color','none','TickDir','out','FontSize',8,'FontName','calibri','box','off');
% ylabel(gca,'Eye velocity (deg/ms)','FontName','calibri','FontSize',8);
% patch([repmat((alignidx-start)-2,1,2) repmat((alignidx-start)+2,1,2)], ...
% [get(gca,'YLim') fliplr(get(gca,'YLim'))], ...
% [0 0 0 0],[1 0 0],'EdgeColor','none','FaceAlpha',0.5);
end
%moving up all rasters now
% if numrast==1
% allrastpos=(get(hrastplot,'position'));
% else
% allrastpos=cell2mat(get(hrastplot(~failed),'position'));
% end
%
% disttotop=allrastpos(1,2)+allrastpos(1,4);
% if disttotop<0.99 %if not already close to top of container
% allrastpos(:,2)=allrastpos(:,2)+(1-disttotop)/1.5;
% end
% if numrast>1
% allrastpos=mat2cell(allrastpos,ones(1,size(allrastpos,1))); %reconversion to cell .. un brin penible
% set(hrastplot(~failed),{'position'},allrastpos);
% else
% set(hrastplot,'position',allrastpos);
% end
%
% %moving down the eye velocity plot
% eyevelplotpos=get(heyevelplot,'position');
% eyevelplotpos(1,2)=eyevelplotpos(1,2)-(eyevelplotpos(1,2))/1.5;
% set(heyevelplot,'position',eyevelplotpos);
% plot a legend in this last graph
clear spacer
spacer(1:numrast,1)={' '};
hlegdir = legend(hsdfline, strcat(aligntype',spacer,curdir'),'Location','NorthWest');
set(hlegdir,'Interpreter','none', 'Box', 'off','LineWidth',1.5,'FontName','calibri','FontSize',9);
% setting sdf plot y axis
newylim=[0, ceil(max(max(cell2mat(get(findobj(sdfplot,'Type','line'),'YDATA'))))/10)*10]; %rounding up to the decimal
set(sdfplot,'YLim',newylim);
%eventdata={algfile,aligntype};
%exportfig_Callback(findobj('tag','exportfig'), eventdata, handles);
%% copied from export_callback
% for k=1:length(subplots)
% copyobj(subplots(k),exportfig);
% end
%increase figure height to leave space for the title
%first change axes units to pixels, so that they don't rescale
allaxes=findobj(exportfig,'Type','axes');
% set(allaxes,'Units','pixels');
% figuresize(4)=figuresize(4)+20;
% set(exportfig,'position',figuresize);
%putting title
axespos=cell2mat(get(allaxes,'Position'));
figtitleh = title(allaxes(find(axespos(:,2)==max(axespos(:,2)),1)),...
['File: ',filename,' - Task: ',fileinfo{1},' - Location: ',fileinfo{3}]);
set(figtitleh,'Interpreter','none'); %that prevents underscores turning charcter into subscript
% and moving everything up a bit
% axespos(:,2)=axespos(:,2)+5; %units in pixels now
% axespos=mat2cell(axespos,ones(size(axespos,1),1)); %reconversion
% set(allaxes,{'Position'},axespos);
%and the title a little bit more
titlepos=get(figtitleh,'position');
titlepos(2)=titlepos(2)+titlepos(2)/10;
set(figtitleh,'position',titlepos,'FontName','calibri','FontSize',11);
%changing units back to relative, in case we want to resize the figure
set(allaxes,'Units','normalized')
% remove time label on first two sdf plot
% set(allaxes(4),'XTickLabel','');
% set(allaxes(5),'XTickLabel','');
%% saving figure
%basic png fig:
print(gcf, '-dpng', '-noui', '-opengl','-r600', exportfigname);
delete(exportfig);
%% end copied section
% figure(guifigh);
% allcurax=findall(guifigh,'type','axes');
% for axnum=1:length(allcurax)
% cla(allcurax(axnum))
% end
%clf('reset')
end
% UIWAIT makes SummaryPlot wait for user response (see UIRESUME)
% uiwait(handles.figure1);
function batchplot(arguments,handles)
global directory slash;
if ~isdir([directory,'figures',slash,arguments{1}])
mkdir([directory,'figures',slash,arguments{1}])
end
if size(arguments(2),1)==1
filelist=arguments(2);
else
filelist=arguments{2};
end
tasklist=arguments{3};
algdir=[directory,'processed',slash,'aligned',slash];
for algfile=1:length(filelist)
filename=filelist{algfile};
if size(tasklist, 1) == 1
tasktype=tasklist;
else
tasktype=tasklist{algfile};
end
set(findobj('tag','dispfilename'),'string',filename);
set(findobj('tag','disptaskname'),'string',tasktype);
if length(arguments)<4
load([algdir,filename,'_sac.mat']);
alignment=dataaligned(1,1).savealignname(max(strfind(dataaligned(1,1).savealignname,'_'))+1:end);
else
dataaligned=arguments{4};
alignment=arguments{1};
end
set(findobj('tag','dispalignment'),'string',alignment);
%alignment=get(findobj('tag','dispalignment'),'string');
%findobj(handles.mainfig,'Type','axes','Tag','legend')
figuresize=getpixelposition(handles.mainfig);
figuresize(1:2)=[80 167];
exportfigname=[directory,'figures',slash,arguments{1},slash,filename,'_',tasktype,'_',alignment];
exportfig=figure('color','white','position',figuresize);
%figdimension=get(findobj('tag','mainfig'),'Position');
%figure(guifigh);
%cla(findall(guifigh,'type','axes'))
%handles.mainfig
%axes(plotaxh);
%plotaxh=axes('Position',initaxdim);
%set(gca,'Position',initaxdim);
rasterdim=[figuresize(1)*1.1 (figuresize(4)*0.66)+figuresize(2)*1.1 figuresize(3)*0.9 figuresize(4)*0.3];
plotstart=1000;
plotstop=500;
fsigma=20;
causker = 0;
cc=lines(length(dataaligned));
numsubplot=length(dataaligned)*3; %dividing the panel in three compartments with wequal number of subplots
if numsubplot==3
numsubplot=6;
end
%setappdata(gcf, 'SubplotDefaultAxesLocation', [0, 0, 1, 1]);
%Plot rasters
%rastersh = axes('Position', rasterdim, 'Layer','top','XTick',[],'YTick',[],'XColor','white','YColor','white');
numrast=length(dataaligned);
failed=zeros(numrast,1);
clear heyevelline aligntype curdir;
for rstplt=1:numrast
rasters=dataaligned(rstplt).rasters;
alignidx=dataaligned(rstplt).alignidx;
greyareas=dataaligned(rstplt).allgreyareas;
start=alignidx - plotstart;
stop=alignidx + plotstop;
if ~ isempty(rasters)
if start < 1
start = 1;
end
if stop > length( rasters )
stop = length( rasters );
end
trials = size(rasters,1);
isnantrial=zeros(1,size(rasters,1));
if numrast==1
hrastplot(rstplt)=subplot(numsubplot,1,1:2,'Layer','top', ...
'XTick',[],'YTick',[],'XColor','white','YColor','white');%'Parent', handles.mainfig
else
hrastplot(rstplt)=subplot(numsubplot,1,rstplt,'Layer','top', ...
'XTick',[],'YTick',[],'XColor','white','YColor','white');%'Parent', handles.mainfig
end
%reducing spacing between rasters
if numrast>1
rastpos=get(gca,'position');
rastpos(2)=rastpos(2)+rastpos(4)*0.5;
set(gca,'position',rastpos);
end
% sorting rasters according greytime
viscuetimes=nan(size(greyareas,2),2);
for grst=1:size(greyareas,2)
viscuetimes(grst,:)=greyareas{grst}(1,:);
end
cuestarts=viscuetimes(:,1);
try
[cuestarts,sortidx]=sort(cuestarts,'descend');
catch
sortidx=[1:size(viscuetimes,1)];
end
viscuetimes=viscuetimes(sortidx,:);
if size(rasters,1)<length(sortidx)
% ca deconne
size(rasters,1)
else
rasters=rasters(sortidx,:);
end
%axis([0 stop-start+1 0 size(rasters,1)]);