-
Notifications
You must be signed in to change notification settings - Fork 0
/
AllProbes.m
executable file
·2215 lines (2056 loc) · 73.2 KB
/
AllProbes.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 result = AllProbes(varargin)
%
% Make sure lfpblank resp is included when doing all files.
% RLS OT probe 1 needs scaling.
% Plot for LFP timecouse averaged over all stimuli, by layer
% add y, n to exps
name = 'AllProbes';
%
%
%If strings is empty, no list is shown. Otherwise a listbox is included.
%
strings = {};
AllExpts = {};
DATA = [];
result = [];
na = 1;
toplevel = 0;
if isstruct(varargin{1}) & isfield(varargin{1},'Title')
tag = [name varargin{1}.Title];
name = tag;
elseif isstruct(varargin{1}) & isfield(varargin{1},'toplevel')
DATA = varargin{1};
toplevel = DATA.toplevel;
na = 2;
elseif isstruct(varargin{1})
AllExpts = varargin{1};
elseif length(varargin) >2 & strcmp(varargin{2},'Tag')
tag = varargin{3};
elseif length(varargin) >2 & ishandle(varargin{1}) & isfigure(varargin{3})
toplevel = varargin{3};
tag = get(toplevel,'Tag');
na = 4;
elseif length(varargin) >1 & isfigure(varargin{1})
toplevel = varargin{1};
tag = get(toplevel,'Tag');
na = 2;
elseif ischar(varargin{1}) && strcmp(varargin{1},'list')
tag = varargin{2};
elseif ischar(varargin{1}) && exist(varargin{1},'file')
tag = varargin{1};
else
tag = name;
end
init = 0;
if toplevel == 0
toplevel = findobj('Tag',tag);
end
if (isempty(toplevel) || toplevel == 0)%% called by user for new window
if ischar(varargin{1}) & strcmp(varargin{1},'list') % a list of expt files
AllExpts = ReadExptList(varargin{2});
nprobes = AllExpts.nprobes;
elseif ischar(name) & isempty(strfind(varargin{1},'.mat')) & exist(varargin{1},'file')
fid = fopen(varargin{1},'r');
names = textscan(fid,'%s');
fclose(fid);
k = 1;
for j = 1:length(names{1})
path = names{1}{j};
if exist(path,'file')
load(path);
nprobes(k) = size(AllExpts.exps{1}.lfp,4);
AllExpts.file = path;
AllCells{k} = AllExpts;
AllCells{k}.Track = LoadDriftFile(strrep(path,'.all.mat','.drift.mat'));
k = k + 1;
else
fprintf('No File %s\n',path);
end
end
elseif iscell(varargin{1}) % a list of AllExpt files
names = varargin{1};
k = 1;
for j = 2:length(names)
[pdir, monk] = fileparts(names{1});
if isempty(monk)
if strfind(names{1},'lem')
monk = 'lem';
end
end
path = [names{1} '/' names{j} '/' monk names{j} '.all.mat'];
if exist(path,'file')
load(path);
nprobes(k) = size(AllExpts.exps{1}.lfp,4);
AllExpts.file = path;
AllExpts.type = 0;
AllCells{k} = AllExpts;
AllCells{k}.Track = LoadDriftFile(strrep(path,'.all.mat','.drift.mat'));
AllCells{k}.nprobes = nprobes(k);
k = k + 1;
else
fprintf('No File %s\n',path);
end
end
elseif exist(varargin{1},'file')
[pdir, tag] = fileparts(varargin{1});
name = tag;
load(varargin{1});
AllExpts.file = varargin{1};
AllExpts.type = 0;
nprobes(1) = size(AllExpts.exps{1}.lfp,4);
else
fprintf('No file %s\n',varargin{1});
return;
end
end
if ~isempty(toplevel)
if strncmpi(varargin{1},'store',5)
set(topelevel,'UserData',varargin{2});
DATA = varargin{2};
elseif isempty(DATA)
DATA = get(toplevel,'UserData');
end
end
if ~isempty(AllExpts)
DATA = SetAllFile(DATA,AllExpts);
end
IsBusy(DATA);
if nargin
if strncmpi(varargin{na},'update',5)
update;
elseif strncmpi(varargin{na},'close',5)
f = fields(DATA.tag);
for j = 1:length(f)
CloseTag(DATA.tag.(f{j}));
end
elseif strncmpi(varargin{na},'getstate',5)
result = DATA;
elseif strncmpi(varargin{na},'PlotLamFind',5) %plots that help identify IV
PlotExptGroup(varargin{na});
elseif strncmpi(varargin{na},'plottype',5)
na = na+1;
DATA.plot.type = varargin{na};
elseif strncmpi(varargin{na},'combine',5)
for k = 1:length(DATA.AllCells)
nid = 0;
for j = 1:length(DATA.AllCells{k}.names)
if strfind(DATA.AllCells{k}.names{j},DATA.plot.compare)
nid = nid+1;
ids(nid) = j;
end
end
if nid
if isfield(DATA.AllCells{k}.Track,'probes')
probes = DATA.AllCells{k}.Track.probes;
else
probes = DATA.plot.plotlfps;
end
lfps{k} = DATA.AllCells{k}.exps{ids(1)}.lfp(:,:,:,probes);
lfpblanks{k} = DATA.AllCells{k}.exps{ids(1)}.lfpblank(:,probes);
lfptimes{k} = DATA.AllCells{k}.exps{ids(1)}.lfptimes;
lfpsizes(k,:) = size(lfps{k});
lfpn{k} = DATA.AllCells{k}.exps{ids(1)}.lfpn;
lfpx{k} = DATA.AllCells{k}.exps{ids(1)}.x;
lfpwrs{k} = DATA.AllCells{k}.exps{ids(1)}.lfpwr;
lfpf{k} = DATA.AllCells{k}.exps{ids(1)}.lfpfrq;
Headers{k} = DATA.AllCells{k}.exps{ids(1)}.Header;
Headers{k}.exptype = name2type(DATA.AllCells{k}.names{ids(1)});
end
end
GetFigure(DATA.tag.MainFig);
subplot(1,1,1);
type = get(findobj(DATA.toplevel,'Tag','combtype'),'value');
if type == 3
shift(1) = 0;
for j = 2:length(lfps)
[shift(j), details] = AlignMatrices(lfps{1},lfps{j},[2 3]);
end
elseif type == 2
for j = 1:length(lfps)
[a,b] = LFPLatency(lfpblanks{j},lfptimes{j});
[latency(j), shift(j)] = min(a(:,3));
end
else
shift = zeros(1,length(lfps));
end
shift = shift - min(shift);
[res.lfp, a] = CombineLFPS(lfps,lfpsizes,lfpn, lfpwrs, shift);
res.lfptimes = mean(cell2mat(lfptimes'));
res.lfpn = mean(a.nsum,3);
res.lfpwr = a.plfp;
res.lfpfrq = lfpf{1};
res.x = mean(cell2mat(lfpx'));
res.Header = Headers{1};
args = PlotArgs(DATA);
if ndims(squeeze(res.lfp)) ==2
imagesc(squeeze(res.lfp));
else
GetFigure(DATA.tag.MainFig);
res.Stimvals = DATA.AllCells{k}.exps{ids(1)}.Stimvals;
PlotAllProbes(res,DATA.plot.type, args{:});
end
elseif strncmpi(varargin{na},'compare',5)
args = PlotArgs(DATA);
rcs = [];
for k = 1:length(DATA.AllCells)
figure(DATA.figures(k));
nid = 0;
for j = 1:length(DATA.AllCells{k}.names)
if strfind(DATA.AllCells{k}.names{j},DATA.plot.compare)
nid = nid+1;
ids(nid) = j;
end
end
if nid
if strmatch(DATA.plot.type,{'AllBlanks' 'FindLam' 'ExpSpikes'})
DATA.AllCells{k}.probelist = [1:DATA.nprobes];
DATA.AllCells{k}.exptypes = DATA.AllCells{k}.names;
PlotExptGroup(DATA.AllCells{k},DATA.plot.type);
elseif strmatch(DATA.plot.type,{'SpTrigLFP'})
args = PlotArgs(DATA);
PlotSpTrigLFP(DATA,ids(1),'cell', k, args{:});
set(gca,'XTick',[])
elseif strmatch(DATA.plot.type,{'Psych'})
PlotPsych(DATA.AllCells{k}.exps{ids(1)})
else
rcs{k} = PlotAllProbes(DATA.AllCells{k}.exps{ids(1)},DATA.plot.type,'probes',DATA.probelist, args{:});
set(gca,'Xtick',[],'Ytick',[]);
rcs{k}.mapprobe =DATA.AllCells{k}.mapprobe;
end
end
end
if length(rcs)
DATA.rcs = rcs;
savercs = 1;
if savercs
save('rcs.mat','rcs');
end
GetFigure('Psych');
PlotRCs(DATA);
end
set(DATA.toplevel,'UserData',DATA);
elseif strncmpi(varargin{na},'wintile',7)
ncells = length(DATA.AllCells);
if ncells > 15
nr = 4; nc = 5;
else
nr = 2; nc = 3;
end
mx = get(0,'monitorpositions')
if size(mx,1) == 1
xo = 100;
fw = (mx(3)-xo)./nc;
fh = (mx(4)-xo)./nr;
else
fw = (mx(2,3)-mx(2,1))./nc;
fh = mx(2,4)./nr;
xo = mx(2,1);
end
ir = 0;
ic = 0;
for j = 1:length(DATA.AllCells)
figpos(j,:) = [(xo +ir * fw ) (ic * fh) fw fh];
set(DATA.figures(j),'Position',figpos(j,:));
ic = ic+1;
if ic >= nc-1
ic = 0;
ir = ir + 1;
end
end
scrsz = get(0','screensize');
elseif strncmpi(varargin{na},'recompare',7)
GetFigure('Psych');
PlotRCs(DATA);
elseif strncmpi(varargin{na},'locatecells',7)
a = GetFigure('AllSummary');
hold off;
CalcCellDepths(DATA);
elseif strncmpi(varargin{na},'checklfps',7)
a = GetFigure('AllSummary');
hold off;
DATA = CheckLFPs(DATA);
elseif strncmpi(varargin{na},'lfpcohs',6)
a = GetFigure('AllSummary');
hold off;
DATA = CalcLFPCoherences(DATA);
elseif strncmpi(varargin{na},'savedrift',5)
ElectrodeDrift = DATA.Track.drift;
Track = DATA.Track;
if isfield(DATA,'Trak')
Track = DATA.Trak;
end
if isfield(DATA,'Markers')
Track.Markers = DATA.Markers;
end
Track.probes = DATA.probelist(DATA.plot.plotlfps);
driftfile = [DATA.dir '/' strrep(DATA.name,'.all','.drift.mat')];
save(driftfile,'ElectrodeDrift','Track');
elseif strncmpi(varargin{na},'loaddrift',5)
driftfile = [DATA.dir '/' strrep(DATA.name,'.all','.drift.mat')];
load(driftfile);
DATA.Track.drift = ElectrodeDrift;
set(DATA.toplevel,'UserData',DATA);
elseif strncmpi(varargin{na},'makeplot',5)
figure(DATA.mfig);
subplot(1,1,1);
PlotExptGroup(DATA,DATA.plot.maketype);
elseif strncmpi(varargin{na},'setentry',5)
n = get(DATA.listui, 'value');
figure(DATA.mfig);
subplot(1,1,1);
if strmatch('onestim', DATA.plot.type)
GetFigure(DATA.tag.OneStimFig);
end
if strmatch(DATA.plot.type,{'AllBlanks' 'FindLam' 'ExpSpikes'})
PlotExptGroup(DATA,DATA.plot.type);
elseif strmatch(DATA.plot.type,{'TrackBlanks'})
if ~isfield(DATA,'LFP')...
|| ~strcmp(DATA.LFP.Header.fstring,DATA.fstrings{n})
name = strrep([DATA.dir '/' DATA.fstrings{n}],'.c1.','.lfp.');
load(name);
DATA.LFP = LFP;
DATA.LFP.Header.fstring = DATA.fstrings{n};
set(DATA.toplevel,'UserData',DATA);
end
if isfield(DATA,'TrackBlank') && ~strcmp(DATA.TrackBlank.name,DATA.fstrings{n})
rebuild = 1;
else
rebuild = 0;
end
if ~isfield(DATA,'TrackBlank') || rebuild
GetFigure('TrackBlanks');
[a,b] = TrackBlanks(DATA.LFP,DATA.plot.plotlfps);
DATA.TrackBlank = b;
DATA.TrackBlank.peaks = a;
DATA.TrackBlank.name = DATA.fstrings{n};
else
a = DATA.TrackBlank.peaks;
b = DATA.TrackBlank;
end
GetFigure('Tracking');
plot(b.xtimes, a,'o-');
hold on;
dv = mean(b.maxs)-mean(b.maxs);
if strfind(DATA.fstrings{n},'rds')
stimname = 'RDS';
elseif strfind(DATA.fstrings{n},'grating')
stimname = 'Grating';
elseif strfind(DATA.fstrings{n},'image')
stimname = 'Image';
elseif strfind(DATA.fstrings{n},'rls')
stimname = 'RLS';
elseif strfind(DATA.fstrings{n},'square')
stimname = 'Squarewv';
else
stimname = 'Other';
end
for j = DATA.exps{n}.Header.expids
DATA.Track.Stimnames{j} = stimname;
end
Track = DATA.Track;
subplot(2,1,1);
hold off;
Track.BlankMax(DATA.exps{n}.Header.expids) = b.maxs;
[l,p] = min(b.latencies');
Track.BlankLat(DATA.exps{n}.Header.expids) = p;
Track.xtimes(DATA.exps{n}.Header.expids) = b.xtimes;
varmax = b.maxvs;
[maxv, maxvp] = max(varmax');
Track.VarMax(DATA.exps{n}.Header.expids) = maxvp;
Track.frameresp(DATA.exps{n}.Header.expids,:) = b.frameresp;
[l,p] = min(b.vlatencies');
Track.VarLat(DATA.exps{n}.Header.expids) = p;
id = find(b.maxvart) < 1000;
[maxr, maxt] = max(b.maxvs);
DATA.Track = Track;
PlotDrift(DATA,b);
set(DATA.toplevel,'UserData',DATA);
elseif strmatch(DATA.plot.type,{'SpTrigLFP'})
args = PlotArgs(DATA);
PlotSpTrigLFP(DATA,n,args{:});
elseif strmatch(DATA.plot.type,{'CellTrigLFP'})
args = PlotArgs(DATA);
PlotSpTrigLFP(DATA,n,args{:},'cells');
else
hold off;
args = PlotArgs(DATA);
DATA.exps{n}.Header.exptype = DATA.exptypes{n};
if strcmp(DATA.plot.type,'Test')
PlotAllProbes(DATA.exps{n},'LFPeigvectors', args{:});
elseif strcmp(DATA.plot.type,'Psych')
PlotPsych(DATA.exps{n},args{:});
elseif strcmp(DATA.plot.type,'CollapseX')
rc = PlotAllProbes(DATA.exps{n},'onestim', args{:},'sumx');
else
rc = PlotAllProbes(DATA.exps{n},DATA.plot.type, args{:});
end
end
elseif strncmpi(varargin{na},'store',5)
set(DATA.toplevel,'UserData',DATA);
else
j = 1;
init = 1;
while(j < nargin)
if(strncmpi(varargin{j},'name',3))
j = j+1;
name = varargin{j};
end
j = j+1;
end
end
else
init = 1;
end
if init & isempty(findobj('Tag',tag))
DATA.name = name;
DATA.tag.top = tag;
if exist('AllCells','var')
for j = 1:length(AllCells)
AllCells{j}.mapprobe = zeros(1,nprobes(j));
if strfind(AllCells{j}.file,'lemM040')
AllCells{j}.mapprobe(8) = 7;
end
end
DATA.AllCells = AllCells;
end
DATA.nprobes = max(nprobes);
InitInterface(DATA, DATA.fstrings, DATA.suffs);
else
NotBusy(DATA);
end
function AllExpts = ReadExptList(name)
fid = fopen(name,'r');
a = textscan(fid,'%s');
names = a{1};
for j = 1:length(names);
AllExpts.names{j} = names{j};
AllExpts.exps{j} = PlotAllProbes(names{j});
nprobes(j) = size(AllExpts.exps{j}.lfp,4);
AllExpt.type = 0;
end
AllExpts.file = name;
AllExpts.type = 1;
AllExpts.nprobes = nprobes;
function PlotPsych(rc, varargin)
id = find(rc.pk.xv > -1000);
if isfield(rc,'pp')
subplot(2,1,2);
hold off;
nmin = max([rc.pp.n])./5;
fitpsf(rc.pp,'nmin',nmin, 'showfit','shown');
subplot(2,1,1);
else
subplot(1,1,1);
end
hold off;
plot(rc.pk.xv(id),rc.pk.kernel(id));
rc.pk.xv = rc.pk.xv(id);
rc.pk.kernel = rc.pk.kernel(id);
rc.pk.n = rc.pk.n(id,:);
sigs = unique(rc.or);
sigs= mod(sigs,180);
for j = 1:length(sigs)
hold on;
plot([sigs(j) sigs(j)],[0 max(rc.pk.kernel)],'r:');
end
title(sprintf('mean %.1f frames',mean(rc.pk.n(:))));
function PlotRCs(DATA,varargin)
GetFigure(DATA.tag.MainFig);
if strcmp(DATA.plot.compare,'DCOR')
CompareDCOR(DATA,varargin);
GetFigure('Latencies');
% R = CompareLatencies(DATA,'plot');
elseif strcmp(DATA.plot.compare,'OPRC') || strcmp(DATA.plot.compare,'PPRC')
GetFigure('Latencies');
hold off;
R = CompareLatencies(DATA,'plot');
end
function CompareOPRC(DATA,varargin)
R = CompareLatencies(DATA,'plot');
function R = CompareLatencies(DATA,varargin)
plottype = 0;
j = 1;
while j <= length(varargin)
if strncmpi(varargin{j},'plot',3)
plottype = 1;
end
j = j+1;
end
rcs = DATA.rcs;
k = 0;
for j = 1:length(rcs)
if isfield(rcs{j},'latencies')
k = k+1;
R.slat(k,:) = rcs{j}.latencies;
R.ssig(k,:) = rcs{j}.latsig;
R.llat(k,:) = rcs{j}.lfplatencies(:,1)';
R.lsig(k,:) = rcs{j}.lfplatsig;
R.cells(k,:) = rcs{j}.iscell;
end
end
for j = 1:size(R.slat,2)
id = find(R.ssig(:,j) > 10 & R.lsig(:,j) > 10 & ~isnan(R.llat(:,j)));
R.mumean(j) = mean(R.slat(id,j));
R.mun(j) = length(id);
R.lmean(j) = mean(R.llat(id,j));
cid = find(R.ssig(:,j) > 10 & R.lsig(:,j) > 10 & ~isnan(R.llat(:,j)) & R.cells(:,j) == 1);
R.sun(j) = length(cid);
R.sumean(j) = mean(R.slat(cid,j));
end
if plottype == 1
plot(R.sumean);
hold on;
plot(R.lmean,'r');
plot(R.mumean,'g');
end
function CompareDCOR(DATA,varargin)
rcs = DATA.rcs;
cellcp = []; cellpcp = []; celllcp = [];
cellpos = [];
cellcv = [];
cellid = [];
cellacp = [];
abscps = [];
probes = [];
cps = [];
cellpwrcp = [];
alln = zeros(100,1);
allbands = zeros(4,100,4);
for j = 1:length(rcs)
pk(j,:) = rcs{j}.pk.kernel;
pkx(j,:) = rcs{j}.pk.xv;
upstim(j) = mean([rcs{j}.cp.dnstim]);
probes(j,:) = (rcs{j}.probes - rcs{j}.blankmax).* DATA.Track.probesep;
cp = [rcs{j}.cp.cp];
id = find(sum(diff(cat(1,rcs{j}.cp.sigcount),[],2),2) < 0);
cp(id) = 1-cp(id);
%cps is MU CP by counts on signal trials
cps(j,:) = cp;
%abscps ignores stim prefs
abscps(j,:) = [rcs{j}.cp.cp];
%pcps uses pref dir of subspace map
pcps(j,:) = [rcs{j}.cp.pcp]; %from subspace pref
cvs(j,:) = rcs{j}.cv;
allcp(j,:) = [rcs{j}.cp.cp];
if isfield(rcs{j},'bandsigpwr')
if DATA.plot.cptype == 5
pwrsign = sum(rcs{j}.bandsigpwr(:,3:4),2);
elseif DATA.plot.cptype == 4
pwrsign = sum(rcs{j}.bandsigpwr(:,1:2),2);
else
pwrsign = sum(rcs{j}.bandsigpwr,2);
end
cp = [rcs{j}.cp.cp];
id = find(pwrsign > 0);
cp(id) = 1-cp(id);
lpwrcps(j,:) = cp;
end
if isfield(rcs{j},'lfpprefdir')
cellproj(:,1) = cos(rcs{j}.lfpprefdir-mean([rcs{j}.cp.upstim] * pi/180));
cellproj(:,2) = cos(rcs{j}.lfpprefdir-mean([rcs{j}.cp.dnstim] * pi/180));
cp = [rcs{j}.cp.cp];
id = find(diff(cellproj,[],2) > 0);
cp(id) = 1-cp(id);
lcps(j,:) = cp;
end
if isfield(rcs{j},'cellcp')
cid = [];
for k = 1:length(rcs{j}.spkres)
cid(k) = ~isempty(rcs{j}.spkres{k});
end
cid = find(cid);
for ic = 1:length(cid)
%cp not adjusted for pref
cellacp = [cellacp rcs{j}.spkres{cid(ic)}.cp.cp];
end
%cellcp pref is assigned from counts from signal trials.
cellcp = [cellcp rcs{j}.cellcp(cid)];
%cp based on pref of the LFP response in teh subspace map
celllcp = [celllcp rcs{j}.celllcp(cid)];
%cp based on pref of the LFP response in teh subspace map
if isfield(rcs{j},'bandsigpwr')
for ic = 1:length(cid)
spk = rcs{j}.spkres{cid(ic)};
probe = round(mean(spk.probes));
if rcs{j}.mapprobe(probe)
probe = rcs{j}.mapprobe(probe);
end
if pwrsign(probe) > 0
cellpwrcp = [cellpwrcp 1-spk.cp.cp];
else
cellpwrcp = [cellpwrcp spk.cp.cp];
end
end
end
%cp based on the cells own subspace map. From direction of mean
%netspk vector.
cellpcp = [cellpcp rcs{j}.cellpcp(cid)];
cellpos = [cellpos (rcs{j}.cellprobe(cid) - rcs{j}.blankmax).*DATA.Track.probesep];
cellcv = [cellcv rcs{j}.cellcv(cid)];
cellid = [cellid ones(size(rcs{j}.cellcv(cid))) .*j];
rid(length(cid)) = 0;
rid(find(rid == 0)) = j;
end
if strcmp(DATA.plot.type,'LFPpwrdiff')
iprobes(j,:) = 20 +rcs{j}.probes - rcs{j}.blankmax;
allbands(:, iprobes(j,:),1,j) = rcs{j}.pwrbands;
s = sign(rcs{j}.bandsigpwr');
allbands(:, iprobes(j,:),2,j) = rcs{j}.pwrbands .*s;
s = abs(sin(rcs{j}.x(rcs{j}.prefs,1) .*pi/180));
s = s > sin(pi/4);
s = repmat(sign(s' -0.5),4,1);
allbands(:, iprobes(j,:),3,j) = rcs{j}.pwrbands .*s;
s = abs(sin(rcs{j}.prefdir .*pi/180));
s = s > sin(pi/4);
s = repmat(sign(s -0.5),4,1);
allbands(:, iprobes(j,:),4,j) = rcs{j}.pwrbands .*s;
alln(iprobes(j,:),:) = alln(iprobes(j,:),:) + 1 ;
end
end
subplot(3,1,1);
hold off;
[a,b] = sort(upstim);
imagesc([min(pkx(:)) max(pkx(:))], 1:length(b), pk(b,:));
hold on;
plot(upstim(b),1:length(b),'w:');
colorbar;
subplot(3,1,3);
hold off;
if ~isfield(DATA.plot,'muCVcrit')
DATA.plot.muCVcrit = 0.2;
end
if ~isfield(DATA.plot,'cellCVcrit')
DATA.plot.cellCVcrit = 0.7;
end
muid = find(cvs > DATA.plot.muCVcrit);
id = find(cellcv >= DATA.plot.cellCVcrit);
uid = find(cellcv < DATA.plot.cellCVcrit);
if DATA.plot.cptype == 1 %by pref from subspace map
mucps = pcps;
cellcps = cellpcp;
elseif DATA.plot.cptype == 0 %by counts on signal trials
mucps = cps;
cellcps = cellcp;
elseif DATA.plot.cptype ==2 %by lfp subspace pref
mucps = lcps;
cellcps = celllcp;
elseif ismember(DATA.plot.cptype, [3 4 5]) %LFP pref by power in bands for sig trials
mucps = lpwrcps;
cellcps = cellpwrcp;
else %abs CP for upstim(first), ignoring all prefs
mucps = abscps;
cellcps = cellacp;
end
plot(probes(muid), mucps(muid),'o');
hold on;
plot(cellpos(id),cellcps(id),'ko','markerfacecolor','k');
plot(cellpos(uid),cellcps(uid),'ro','markerfacecolor','r');
title(sprintf('Mean CP %.3f, Cells %.3f(%.3f)n=%d(%d)',mean(mucps(muid)),...
mean(cellcps(id)),mean(cellcps),length(id),length(cellcps)));
plot([min(probes(muid)) max(probes(muid))],[0.5 0.5],'k:');
% allbands(,probe,cptype,cell)
% cptype = 1 : power in this band
% cptype = 2 : power in this band
if strcmp(DATA.plot.type,'LFPpwrdiff')
GetFigure('LFP power by choice');
id = find(alln > 0);
nm = repmat(alln(id)',[4 1 4]);
allsd = squeeze(std(allbands(:,id,:,:),[],4))./sqrt(nm);
mband = squeeze(sum(allbands(:,id,:,:),4))./nm;
for j = 1:4
subplot(4,1,j);
title('Pref from power in band');
errorbar(squeeze(mband(:,:,j))',squeeze(allsd(:,:,j))');
end
end
function PlotDrift(DATA, b)
Track = DATA.Track;
for j = 1:length(Track.Stimnames)
if isempty(Track.Stimnames{j})
stimtypes(j) = 0;
else
stimtypes(j) = strmatch(Track.Stimnames{j},{'RDS', 'Grating', 'Image', 'RLS', 'Squarewv', 'Other'});
end
end
id = find(Track.BlankMax > 0 & stimtypes > 1) %all expts where this has been done
if nargin > 1
hold off;
plot(b.xtimes,b.maxs,'mo-'); %Max blank resp
hold on;
else
hold off;
end
if length(id)
c = (mean(Track.frameresp(id,2))+mean(Track.BlankMax(id)))/2;
plot(Track.xtimes(id),Track.drift(id)+c,'g');
hold on;
plot(Track.xtimes(id),Track.BlankMax(id),'r');
plot(Track.xtimes(id),Track.VarMax(id),'b');
plot(Track.xtimes(id),Track.frameresp(id,1),'k');
plot(Track.xtimes(id),Track.frameresp(id,2),'k:');
plot(Track.xtimes(id),Track.frameresp(id,3),'k:');
plot(Track.xtimes(id),Track.BlankLat(id),'r:');
plot(Track.xtimes(id),Track.VarLat(id),'b:');
legend('drift','blankmax','varmax','Frame')
else
c = 0;
end
if nargin > 1
plot(b.xtimes,b.blankshifts+c,'ro-'); %from xcorr
hold on;
plot(b.xtimes,b.varshifts+c,'bo-'); %from xcorr
subplot(2,1,2);
hold off;
imagesc(b.tslice);
hold on;
plot(b.maxs,1:size(b.tslice,1),'w:');
end
function type = name2type(name)
if strfind(name, 'OTRC');
type = 'OTRC';
elseif strfind(name, 'PPRC');
type = 'PPRC';
elseif strfind(name, 'OPRC');
type = 'OPRC';
elseif strfind(name, 'ODRC');
type = 'ODRC';
elseif strfind(name, 'square.CO');
type = 'Flash';
elseif strfind(name, 'DCORRC');
type = 'DCORRC';
else
id = regexp(name,'[A-Z][A-Z][A-Z]*');
if isempty(id)
type = 'Unknown';
else
type = name(id(1):end);
end
end
function IsBusy(DATA)
if isfield(DATA,'toplevel')
set(DATA.toplevel,'Name','Busy...');
end
function NotBusy(DATA)
if isfigure(DATA.toplevel)
set(DATA.toplevel,'Name',DATA.tag.top);
end
function DATA = SetAllFile(DATA,AllExpts)
DATA.exps = AllExpts.exps;
DATA.fstrings = AllExpts.names;
[pdir, name] = fileparts(AllExpts.file);
DATA.dir = pdir;
for j = 1:length(DATA.exps)
DATA.suffs{j} = '';
if isfield(DATA.exps{j},'Header')
ts = DATA.exps{j}.Header.BlockStart(1)./(60 * 10000); % time in min
DATA.suffs{j} = sprintf('ed%.2f,%.1f',mean(DATA.exps{j}.Header.eds),ts);
end
DATA.exptypes{j} = name2type(DATA.fstrings{j});
DATA.exps{j}.Header.exptype = DATA.exptypes{j};
end
if AllExpts.type == 1
return;
end
driftfile = [DATA.dir '/' strrep(name,'.all','.drift.mat')];
DATA.Track = LoadDriftFile(driftfile);
if isfield(DATA.Track,'probes')
DATA.plot.plotlfps = DATA.Track.probes;
end
DATA = SetExptDrifts(DATA);
function Track = LoadDriftFile(driftfile)
Track = [];
if exist(driftfile,'file')
load(driftfile);
Track.drift = ElectrodeDrift;
if exist('Track','var')
Track = Track;
if ~isfield(Track,'drift')
Track.drift = ElectrodeDrift;
end
end
end
if ~isfield(Track,'probesep')
Track.probesep = 50;
end
function DATA = SetExptDrifts(DATA)
% add drift values to Headers of each expt
nexp = length(DATA.exps);
nb = 0;
oldn = 1;
for j = 1:nexp
starts = DATA.exps{j}.Header.BlockStart;
nb = nb+length(starts);
allstarts(oldn:nb) = starts;
expid(oldn:nb) = j;
blkid(oldn:nb) = 1:length(starts);
oldn = nb+1;
end
[a,b] = sort(allstarts);
for j = 1:length(b);
ei = expid(b(j));
H = DATA.exps{ei}.Header;
if isfield(DATA,'Track') && isfield(DATA.Track,'drift')
DATA.exps{ei}.Header.drift(blkid(b(j))) = DATA.Track.drift(j);
end
DATA.exps{ei}.Header.expids(blkid(b(j))) = j;
if isfield(H,'BlockTrial')
DATA.Track.BlockStart(j) = DATA.exps{ei}.Header.BlockTrial(blkid(b(j)));
end
end
function [mlfp, details] = CombineLFPS(lfps, sz, lfpn, lfpwrs,shift)
nr = lfpn;
for j = 1:length(lfpwrs)
nfreqs(j) = size(lfpwrs{j},1);
end
nfreq = mean(nfreqs);
if sum(std(sz)) == 0 %same size
ntimes = mean(sz(:,1));
nsum = zeros(size(nr{1}));
np = max(sz(:,4));
nsum = repmat(nsum,[1 1 np+max(shift)]);
for j = 1:length(lfps)
pwr = lfpwrs{j};
for p = 1:min(sz(:,4)) % smallest # of probes
slfp (:,:,:,p+shift(j),j) = lfps{j}(:,:,:,p) .* repmat(reshape(nr{j},[1 size(nr{j})]),[ntimes 1 1]);
if ndims(lfpwrs{j}) == 3
slfpwr(:,:,p+shift(j),j) = pwr(:,:,p) .* sum(nr{j}(:));
else
slfpwr(:,p+shift(j),j) = pwr(:,p) .* sum(nr{j}(:));
end
nsum(:,:,p+shift(j)) = nsum(:,:,p+shift(j))+nr{j};
end
end
details.nsum = nsum;
psum = squeeze(sum(nsum,2));
% mlfp(:,:,:,p) = sum(slfp(:,:,:,p,:),5) ./ repmat(reshape(nsum,[1 size(nsum)]),[ntimes 1 1]);
for p = 1:size(nsum,3)
mlfp(:,:,:,p) = sum(slfp(:,:,:,p,:),5) ./ shiftdim(repmat(nsum(:,:,p),[1 1 ntimes]),2);
if ndims(lfpwrs{j}) == 3
details.plfp(:,:,:,p) = sum(slfpwr(:,:,p,:),4) ./ sum(psum(:,p));
else
details.plfp(:,:,:,p) = sum(slfpwr(:,p,:),3) ./ sum(psum(:,p));
end
end
% slfp = sum(slfp,5) ./ repmat(reshape(nsum,[1 size(nsum)]),[ntimes 1 1]);
% slfp = sum(cat(5,lfps{:}),5);
else
fprintf('Dimensions don''t match:\n');
for j = 1:size(sz,1)
fprintf('%d ',sz(j,:));
fprintf('\n');
end
mlfp = [];
return;
end
function PlotSpTrigLFP(DATA, n, varargin)
[lp, it] = GetPop('plotstyle',DATA.toplevel);
str = get(it,'String')
DATA.plot.style = str(lp,:);
lp = lp-1;
diffy = GetCheck('DiffY',DATA.toplevel);
sumy = GetCheck('SumY',DATA.toplevel);
meanp = GetCheck('MeanProbe',DATA.toplevel);
celln = 0;
Expt = [];
j = 1;
while j <= length(varargin)
if strncmp(varargin{j},'cell',4)
if isfield(DATA,'AllCells')
j = j+1;
celln = varargin{j};
Expt = DATA.AllCells{celln}.exps{n};
else
celln = 1:length(DATA.exps{n}.spkres);
end
end
j = j+1;
end
if isempty(Expt)
Expt = DATA.exps{n};
end
if ~isfield(Expt,'sptriglfp')
dfile = [];
if isfield(Expt.Header,'filename')
dfile = Expt.Header.filename;
end
if isempty(dfile) | ~exist(dfile,'file')
dfile = [DATA.dir '/' DATA.fstrings{n}];
end
sfile = regexprep(dfile,'.c1.*mat','.sptrig.mat');
load(sfile);
if celln & isfield(DATA,'AllCells')
for j = 1:length(DATA.AllCells{celln}.exps)
DATA.AllCells{celln}.exps{j}.sptriglfp = sptrig{j}.lfp;
DATA.AllCells{celln}.exps{j}.sptriglfpn = sptrig{j}.lfpn;
DATA.AllCells{celln}.exps{j}.triglfptimes = sptrig{j}.lfptimes;
if isfield(sptrig{j},'cells')
DATA.AllCells{celln}.exps{j}.trigcells = sptrig{j}.cells;
end
end
Expt = DATA.AllCells{celln}.exps{n};
else
for j = 1:length(DATA.exps)
DATA.exps{j}.sptriglfp = sptrig{j}.lfp;
DATA.exps{j}.sptriglfpn = sptrig{j}.lfpn;
DATA.exps{j}.triglfptimes = sptrig{j}.lfptimes;
if isfield(sptrig{j},'cells')
DATA.exps{j}.trigcells = sptrig{j}.cells;
end
end
Expt = DATA.exps{n};
end
end
spv = Expt.sptriglfp;
iscell = zeros(1,size(spv,2));
if isfield(Expt,'trigcells')
if celln
args = {'smooth', [0 DATA.plot.smoothk(2)]};
if isfield(Expt.Header,'LFPamps')
% args = {args{:} 'scales' Expt.Header.LFPamps};
end
if strcmp(DATA.plot.style,'Pcolor')
args = {args{:} 'avg'};
end
if DATA.plot.diffy
args = {args{:} 'diffy'};
end
trig{1}.name = Expt.Header.filename;
trig{1}.cells = Expt.trigcells;
TrigCSD(trig,'cell',celln,'autooff',args{:});
return;
end
cellprobes = [Expt.trigcells.probe];
for j = 1:length(cellprobes)
if size(Expt.trigcells(j).lfptrig,1) == size(spv,1)
spv(:,:,:,round(cellprobes(j))) = Expt.trigcells(j).lfptrig;
iscell(round(cellprobes(j))) = 1;
end
end
else
cellprobes = [];
end
np = size(spv,2);
[nr,nc] = Nsubplots(np);
cr = [min(spv(:)) max(spv(:))];
plotlfps = DATA.plot.plotlfps;
for j =1:np
if iscell(j) % not just a cell, but has been put in above
utype = 'S';
else
utype = 'M';
end