-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMLB_SM.m
1659 lines (1632 loc) · 89.9 KB
/
MLB_SM.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 MLB_SM < SeqMem
% Implementation of a naive bayes classifier for spiking data
properties % Analysis Variables
binSize
dsRate
binType = 'box'
bayesType %1 = Poisson: use with raw spike counts; 2 = Bernoulli: use with binarized spike counts; 3 = Gaussian: Use with z-scored spike counts
windows
alignments
alignmentOffset = 0
end
properties % Data Structures
fiscTrials
trialIDs = [{'Time'}, {'Window'}, {'Position'}, {'Odor'}];
likeTrlSpikes
likeIDvects
likeTrlIDs
likeTimeVect
obsvTrlSpikes
obsvTrlIDs
obsvTimeVect
post
postTrlIDs
decode
decodeIDvects
end
properties % Trial Event Masks
mask_Trial
mask_Int
mask_IntMid
mask_PreInt
mask_PreIntCON
mask_PstInt
mask_PstIntCON
end
methods
function obj = MLB_SM(fileDir)
if nargin == 0
fileDir = uigetdir;
end
obj@SeqMem(fileDir);
fprintf('Completed\n');
obj.PP_IdentifyFISCseqs
end
end
%% Data Pre-Processing Methods
methods
%% Pre-Process Trial Spiking Data
function [dataMtx, timeVect] = PP_TrialMatrix_Spiking(obj, window, alignment)
if isempty(obj.binSize)
error('Define binSize');
elseif isempty(obj.dsRate)
error('Define dsRate');
end
paddedWindow = [window(1)-(obj.binSize/2) window(2)+(obj.binSize/2)];
trialTime = obj.ExtractTrialMatrix(obj.tsVect, paddedWindow, alignment);
tempMtx = obj.ExtractTrialMatrix(obj.ensembleMatrix, paddedWindow, alignment);
if ~isempty(obj.popVectIncludeLog)
tempMtx(:,~obj.popVectIncludeLog,:) = [];
end
firstValid = 1;
switch alignment
case 'Odor'
firstAlignNdx = obj.tsVect(obj.trialInfo(firstValid).OdorIndex);
case 'PokeIn'
firstAlignNdx = obj.tsVect(obj.trialInfo(firstValid).PokeInIndex);
case 'PokeOut'
firstAlignNdx = obj.tsVect(obj.trialInfo(firstValid).PokeOutIndex);
case 'FrontReward'
firstValid = find(~isnan([obj.trialInfo.RewardIndex]),1, 'first');
firstAlignNdx = obj.tsVect(obj.trialInfo(firstValid).RewardIndex);
case 'RewardSignal'
firstValid = find(~isnan([obj.trialInfo.RewardSignalIndex]),1, 'first');
firstAlignNdx = obj.tsVect(obj.trialInfo(firstValid).RewardSignalIndex);
case 'RearReward'
firstValid = find(~isnan([obj.trialInfo.RearRewardIndex]),1, 'first');
firstAlignNdx = obj.tsVect(obj.trialInfo(firstValid).RearRewardIndex);
case 'ErrorSignal'
firstValid = find(~isnan([obj.trialInfo.ErrorIndex]),1, 'first');
firstAlignNdx = obj.tsVect(obj.trialInfo(firstValid).ErrorIndex);
end
tempTS = trialTime(:,1,firstValid) - firstAlignNdx;
binnedMtx = nan(size(tempMtx));
for t = 1:size(tempMtx,3)
for u = 1:size(tempMtx,2)
if strcmp(obj.binType, 'box')
binnedMtx(:,u,t) = conv(tempMtx(:,u,t),ones(1,obj.binSize)./(obj.binSize/obj.sampleRate), 'same');
elseif strcmp(obj.binType, 'gauss')
binnedMtx(:,u,t) = conv(tempMtx(:,u,t),gausswin(obj.binSize)./(obj.binSize/obj.sampleRate), 'same');
end
end
end
unpaddedBinnedMtx = binnedMtx((obj.binSize/2)+1:end-(obj.binSize/2),:,:);
unpaddedTS = tempTS((obj.binSize/2)+1:end-(obj.binSize/2));
dsVect = downsample(1:size(unpaddedBinnedMtx,1),obj.dsRate);
dataMtx = unpaddedBinnedMtx(dsVect,:,:);
timeVect = unpaddedTS(dsVect);
end
%% Pre-Process Trial LFP Data
function [phaseMtx, powerMtx] = PP_TrialMatrix_LFP(obj, freqWin, window, alignment)
if isempty(obj.binSize)
error('Define binSize');
end
if isempty(obj.dsRate)
error('Define dsRate');
end
if isempty(obj.lfpMatrix)
obj.CompileLFPmatrix;
end
paddedWindow = [window(1)-(obj.binSize/2) window(2)+(obj.binSize/2)];
if isempty(obj.lfpRefTet)
obj.lfpRefTet = find(obj.numUniPerTet==max(obj.numUniPerTet), 1, 'first');
end
[~, phase, power] = obj.SimpleFilter(obj.lfpMatrix(:,obj.lfpRefTet), freqWin);
tempPhase = obj.ExtractTrialMatrix(phase, paddedWindow, alignment);
tempPower = obj.ExtractTrialMatrix(power, paddedWindow, alignment);
unpaddedPhase = tempPhase((obj.binSize/2)+1:end-(obj.binSize/2),:,:);
unpaddedPower = tempPower((obj.binSize/2)+1:end-(obj.binSize/2),:,:);
dsVect = downsample(1:size(unpaddedPhase,1),obj.dsRate);
phaseMtx = unpaddedPhase(dsVect,:,:);
powerMtx = unpaddedPower(dsVect,:,:);
end
%% Pre-Process Trial Spiking Data as Binary
function [binMtx, timeVect] = PP_TrialMatrix_SpikeBin(obj, window, alignment)
[dataMtx, timeVect] = obj.PP_TrialMatrix_Spiking(window, alignment);
binMtx = dataMtx.*(obj.binSize/obj.sampleRate);
binMtx(binMtx>=1) = 1;
end
%% Pre-Process Trial Spiking Data as Timebin z-scored
function [timeZMtx, timeVect] = PP_TrialMatrix_SpikeTimeZ(obj,window,alignment)
[dataMtx, timeVect] = obj.PP_TrialMatrix_Spiking(window, alignment);
iscLog = [obj.trialInfo.TranspositionDistance]==0 & [obj.trialInfo.Performance]==1;
cellTimeMeans = mean(dataMtx(:,:,iscLog),3);
cellTimeVar = std(dataMtx(:,:,iscLog),0,3);
for trl = 1:size(dataMtx,3)
dataMtx(:,:,trl) = (dataMtx(:,:,trl)-cellTimeMeans)./cellTimeVar;
end
timeZMtx = dataMtx;
end
%% Identify Fully InSeq Correct Sequences
function PP_IdentifyFISCseqs(obj)
odrVect = [obj.trialInfo.Odor];
posVect = [obj.trialInfo.Position];
pos1 = find(posVect==1);
potentialSeqs = repmat(pos1,[obj.seqLength,1])+ repmat((0:obj.seqLength-1)', [1,length(pos1)]);
fiscLog = false(1,size(potentialSeqs,2));
for seq = 1:size(potentialSeqs,2)
if potentialSeqs(end,seq) <= length(obj.trialInfo)
if sum(odrVect(potentialSeqs(:,seq)) == posVect(potentialSeqs(:,seq))) == obj.seqLength ...
&& sum([obj.trialInfo(potentialSeqs(:,seq)).Performance]) == obj.seqLength
fiscLog(seq) = true;
elseif sum(odrVect(potentialSeqs(:,seq))-10 == posVect(potentialSeqs(:,seq))) == obj.seqLength ...
&& sum([obj.trialInfo(potentialSeqs(:,seq)).Performance]) == obj.seqLength
fiscLog(seq) = true;
end
end
end
fiscSeqs = potentialSeqs(:,fiscLog);
if size(obj.odrSeqs,1)>=2
tempSeqs = cell(size(obj.odrSeqs,1),1);
for list = 1:size(obj.odrSeqs,1)
tempSeqs{list} = fiscSeqs(:,odrVect(fiscSeqs(1,:))==obj.odrSeqs(list,1));
end
numSeqs = cellfun(@(a)size(a,2),tempSeqs);
padSize = max(numSeqs)-numSeqs;
for list = 1:length(numSeqs)
tempSeqs{list} = [tempSeqs{list}, nan(obj.seqLength,padSize(list))];
end
obj.fiscTrials = cell2mat(tempSeqs);
else
obj.fiscTrials = fiscSeqs;
end
end
%% Group Trial Data
function [ssnSpikes, ssnID] = PP_ConcatTrialData(obj)
if isempty(obj.bayesType)
error('Specify bayesian type to be used before processing data');
end
trlDta = cell(size(obj.windows,1),1);
trlTimeVects = cell(size(obj.windows,1),1);
for win = 1:size(obj.windows,1)
if obj.bayesType == 1 || strcmp(obj.bayesType, 'Poisson') || strcmp(obj.bayesType, 'poisson') || strcmp(obj.bayesType, 'P') || strcmp(obj.bayesType, 'p')
[trlDta{win}, trlTimeVects{win}] = obj.PP_TrialMatrix_Spiking(obj.windows{win}, obj.alignments{win});
elseif obj.bayesType == 2 || strcmp(obj.bayesType, 'Bernoulli') || strcmp(obj.bayesType, 'bernoulli') || strcmp(obj.bayesType, 'B') || strcmp(obj.bayesType, 'b')
[trlDta{win}, trlTimeVects{win}] = obj.PP_TrialMatrix_SpikeBin(obj.windows{win}, obj.alignments{win});
elseif obj.bayesType == 3 || strcmp(obj.bayesType, 'Gaussian') || strcmp(obj.bayesType, 'gaussian') || strcmp(obj.bayesType, 'G') || strcmp(obj.bayesType, 'g')
[trlDta{win}, trlTimeVects{win}] = obj.PP_TrialMatrix_SpikeTimeZ(obj.windows{win}, obj.alignments{win});
end
end
if ~isempty(obj.popVectIncludeLog)
ssnSpikes = nan(size(cell2mat(trlTimeVects),1), sum(obj.popVectIncludeLog), length(obj.trialInfo));
else
ssnSpikes = nan(size(cell2mat(trlTimeVects),1), size(obj.ensembleMatrix,2), length(obj.trialInfo));
end
ssnID = nan(size(cell2mat(trlTimeVects),1), 5, length(obj.trialInfo));
for trl = 1:size(trlDta{win},3)
tempTrialData = cell(size(obj.windows,1),1);
tempTrialTimeVect = cell(size(obj.windows,1),1);
tempTrialWindowVect = cell(size(obj.windows,1),1);
for win = 1:size(obj.windows,1)
tempTrialData{win} = trlDta{win}(:,:,trl);
if win==1
tempTrialTimeVect{win} = trlTimeVects{win};
else
tempTrialTimeVect{win} = cumsum([trlTimeVects{win-1}(end) + mode(diff(trlTimeVects{win})); diff(trlTimeVects{win})]);
end
tempTrialWindowVect{win} = ones(size(trlTimeVects{win},1),1).*win;
end
ssnSpikes(:,:,trl) = cell2mat(tempTrialData);
ssnID(:,1,trl) = round(cell2mat(tempTrialTimeVect)*1000)/1000;
ssnID(:,2,trl) = cell2mat(tempTrialWindowVect);
ssnID(:,3,trl) = ones(size(ssnID,1),1).*obj.trialInfo(trl).Position;
ssnID(:,4,trl) = ones(size(ssnID,1),1).*obj.trialInfo(trl).Odor;
ssnID(:,5,trl) = ones(size(ssnID,1),1).*obj.trialInfo(trl).TrialNum;
end
end
end
%% MLB Configuration Methods
methods
%% Set Likelihoods as Fully InSeq Correct
function SetLikes_FISC(obj)
% Set likelihoods & observations using FISC trials where FISC are likelihoods and all other trials are observations
[ssnSpikes, ssnID] = obj.PP_ConcatTrialData;
obj.likeTrlSpikes = cell(size(obj.fiscTrials,1),1,size(obj.fiscTrials,2));
obj.likeIDvects = cell(size(obj.fiscTrials,1),1,size(obj.fiscTrials,2));
for pos = 1:size(obj.fiscTrials,1)
for seq = 1:size(obj.fiscTrials,2)
if ~isnan(obj.fiscTrials(pos,seq))
obj.likeTrlSpikes{pos,seq} = ssnSpikes(:,:,obj.fiscTrials(pos,seq));
obj.likeIDvects{pos,seq} = ssnID(:,:,obj.fiscTrials(pos,seq));
else
obj.likeTrlSpikes{pos,seq} = nan(size(ssnSpikes,1), size(ssnSpikes,2));
obj.likeIDvects{pos,seq} = nan(size(ssnID,1), size(ssnID,2));
end
end
end
obj.likeTrlIDs = nan(size(obj.likeIDvects));
for ind = 1:numel(obj.likeIDvects)
if ~isempty(obj.likeIDvects{ind})
obj.likeTrlIDs(ind) = obj.likeIDvects{ind}(1,end);
end
end
obj.likeTimeVect = cell2mat(cellfun(@(a){a(:,1)}, obj.likeIDvects(:,:,1)));
obj.decodeIDvects = cell2mat(cellfun(@(a){a(:,1:end-1)}, obj.likeIDvects(:,:,1)));
fiscTrls = unique(obj.fiscTrials(~isnan(obj.fiscTrials)));
nonFiscLog = true(1,size(ssnSpikes,3));
nonFiscLog(fiscTrls) = false;
obj.obsvTrlSpikes = ssnSpikes(:,:,nonFiscLog);
obj.obsvTrlIDs = ssnID(:,:,nonFiscLog);
obj.obsvTimeVect = ssnID(:,1,1);
end
%% Set Likelihoods as All InSeq Correct
function SetLikes_ISC(obj)
% Set likelihoods & observations using All ISC trials where ISC are likelihoods and all other trials are Observations
[ssnSpikes, ssnID] = obj.PP_ConcatTrialData;
posTrlIDs = [obj.trialInfo.Position];
odrTrlIDs = [obj.trialInfo.Odor];
perfTrlIDs = [obj.trialInfo.Performance];
iscTrls = cell(fliplr(size(obj.odrSeqs)));
for s = 1:size(obj.odrSeqs,1)
for p = 1:size(obj.odrSeqs,2)
iscTrls{p,s} = find(posTrlIDs==p & odrTrlIDs==obj.odrSeqs(s,p) & perfTrlIDs==1);
end
end
iscTrls = iscTrls(:);
trlCounts = cellfun(@(a)length(a),iscTrls);
trlPad = max(trlCounts)-trlCounts;
for o = 1:length(iscTrls)
iscTrls{o} = [iscTrls{o}, nan(1,trlPad(o))];
end
iscTrls = cell2mat(iscTrls);
obj.likeTrlSpikes = cell(size(iscTrls,1),1,size(iscTrls,2));
obj.likeIDvects = cell(size(iscTrls,1),1,size(iscTrls,2));
for pos = 1:size(iscTrls,1)
for seq = 1:size(iscTrls,2)
if ~isnan(iscTrls(pos,seq))
obj.likeTrlSpikes{pos,seq} = ssnSpikes(:,:,iscTrls(pos,seq));
obj.likeIDvects{pos,seq} = ssnID(:,:,iscTrls(pos,seq));
else
obj.likeTrlSpikes{pos,seq} = nan(size(ssnSpikes,1), size(ssnSpikes,2));
obj.likeIDvects{pos,seq} = nan(size(ssnID,1), size(ssnID,2));
end
end
end
obj.likeTrlIDs = nan(size(obj.likeIDvects));
for ind = 1:numel(obj.likeIDvects)
if ~isempty(obj.likeIDvects{ind})
obj.likeTrlIDs(ind) = obj.likeIDvects{ind}(1,end);
end
end
obj.likeTimeVect = cell2mat(cellfun(@(a){a(:,1)}, obj.likeIDvects(:,:,1)));
obj.decodeIDvects = cell2mat(cellfun(@(a){a(:,1:end-1)}, obj.likeIDvects(:,:,1)));
iscTrls = unique(iscTrls(~isnan(iscTrls)));
nonIscLog = true(1,size(ssnSpikes,3));
nonIscLog(iscTrls) = false;
obj.obsvTrlSpikes = ssnSpikes(:,:,nonIscLog);
obj.obsvTrlIDs = ssnID(:,:,nonIscLog);
obj.obsvTimeVect = ssnID(:,1,1);
end
%% Set Observations as OutSeq Correct
function SetObsvs_OSC(obj)
% I need to be made!!!
end
end
%% MLB Processing Methods
methods
%% Permute the likelihood spiking data
function [permLikes, permLikesTrlIDs] = RandPermLikes(obj, shuffType)
tempLikes = obj.likeTrlSpikes;
% Permute Trial IDs
tempLikesPos = tempLikes;
realTrlIDsNdx = find(~isnan(obj.likeTrlIDs));
permTrlIDsNdx = realTrlIDsNdx(randperm(length(realTrlIDsNdx))');
tempLikesPos(realTrlIDsNdx) = tempLikes(permTrlIDsNdx);
% Permute Time IDs
tempLikesTime = tempLikes;
tempLikesFull = tempLikesPos;
for trl = 1:numel(tempLikes)
chancePerm = randperm(length(obj.obsvTimeVect));
tempLikesTime{trl} = tempLikesTime{trl}(chancePerm,:);
tempLikesFull{trl} = tempLikesFull{trl}(chancePerm,:);
end
permLikesTrlIDs = obj.likeTrlIDs;
if strcmp(shuffType, 'Trial')
permLikes = tempLikesPos;
permLikesTrlIDs(realTrlIDsNdx) = permLikesTrlIDs(permTrlIDsNdx);
elseif strcmp(shuffType, 'Time')
permLikes = tempLikesTime;
elseif strcmp(shuffType, 'Full')
permLikes = tempLikesFull;
permLikesTrlIDs(realTrlIDsNdx) = permLikesTrlIDs(permTrlIDsNdx);
end
end
%% Process via Leave-1-Out leaving out individual trials during calculation
function Process_LikelyL1O(obj, shuffType)
if nargin == 1
shuffYN = false;
elseif nargin == 2
shuffYN = true;
end
tempPost = cell(size(obj.likeTrlSpikes));
obj.postTrlIDs = permute(cellfun(@(a)a(1,end),obj.likeIDvects), [1,3,2]);
tempObsvs = obj.likeTrlSpikes;
if shuffYN
[tempLikes, tempTrlIDs] = obj.RandPermLikes(shuffType);
else
tempLikes = obj.likeTrlSpikes;
tempTrlIDs = obj.postTrlIDs;
end
for odr = 1:size(obj.postTrlIDs,1)
for seq = 1:size(obj.postTrlIDs,2)
if ~isnan(obj.postTrlIDs(odr,seq))
currObsv = tempObsvs{odr,seq};
curLikes = tempLikes;
curLikes{find(tempTrlIDs==obj.postTrlIDs(odr,seq))} = nan(size(currObsv)); %#ok<FNDSB>
curLikes = cell2mat(curLikes);
tempProb = sum(~isnan(curLikes(:,1,:)),3)./sum(sum(~isnan(curLikes(:,1,:))));
if obj.bayesType == 1 || strcmp(obj.bayesType, 'Poisson') || strcmp(obj.bayesType, 'poisson') || strcmp(obj.bayesType, 'P') || strcmp(obj.bayesType, 'p')
tempPost{odr,seq} = obj.CalcStaticBayesPost_Poisson(mean(curLikes,3, 'omitnan'), currObsv, tempProb);
elseif obj.bayesType == 2 || strcmp(obj.bayesType, 'Bernoulli') || strcmp(obj.bayesType, 'bernoulli') || strcmp(obj.bayesType, 'B') || strcmp(obj.bayesType, 'b')
tempPost{odr,seq} = obj.CalcStaticBayesPost_Bernoulli(mean(curLikes,3, 'omitnan'), currObsv, tempProb);
elseif obj.bayesType == 3 || strcmp(obj.bayesType, 'Gaussian') || strcmp(obj.bayesType, 'gaussian') || strcmp(obj.bayesType, 'G') || strcmp(obj.bayesType, 'g')
tempPost{odr,seq} = obj.CalcStaticBayesPost_Gaussian(mean(curLikes,3, 'omitnan'), std(curLikes,0,3), currObsv, tempProb);
end
else
tempPost{odr,seq} = nan(size(currObsv,1), size(curLikes,1));
end
end
end
obj.post = cell(size(obj.postTrlIDs,1),1);
for odr = 1:size(obj.postTrlIDs,1)
obj.post{odr} = cell2mat(tempPost(odr,:,:));
end
end
%% Process via Leave-1-Out Iteratively
function Process_IterativeLikelyL1O(obj, shuffType)
if nargin == 1
shuffYN = false;
elseif nargin ==2
shuffYN = true;
end
tempPost = cell(size(obj.likeTrlSpikes));
obj.postTrlIDs = permute(cellfun(@(a)a(1,end),obj.likeIDvects), [1,3,2]);
tempObsvs = obj.likeTrlSpikes;
if shuffYN
[tempLikes, tempTrlIDs] = obj.RandPermLikes(shuffType);
else
tempLikes = obj.likeTrlSpikes;
tempTrlIDs = obj.postTrlIDs;
end
for pos = 1:size(tempLikes,1)
for seq = 1:size(tempLikes,3)
if ~isnan(obj.postTrlIDs(pos,seq))
cur_TempObsv = tempObsvs{pos,1,seq};
cur_TempLike = tempLikes;
cur_TempLike{find(tempTrlIDs==obj.postTrlIDs(pos,seq))} = nan(size(cur_TempObsv)); %#ok<FNDSB>
cur_TempLike = cell2mat(cur_TempLike);
tempProb = sum(~isnan(cur_TempLike(:,1,:)),3)./sum(sum(~isnan(cur_TempLike(:,1,:))));
if obj.bayesType == 1 || strcmp(obj.bayesType, 'Poisson') || strcmp(obj.bayesType, 'poisson') || strcmp(obj.bayesType, 'P') || strcmp(obj.bayesType, 'p')
tempPost{pos,seq} = obj.CalcIterativeBayesPost_Poisson(mean(cur_TempLike,3, 'omitnan'), cur_TempObsv, obj.decodeIDvects(:,1), obj.decodeIDvects(:,4), tempProb);
elseif obj.bayesType == 2 || strcmp(obj.bayesType, 'Bernoulli') || strcmp(obj.bayesType, 'bernoulli') || strcmp(obj.bayesType, 'B') || strcmp(obj.bayesType, 'b')
error('Not Implemented Yet');
elseif obj.bayesType == 3 || strcmp(obj.bayesType, 'Gaussian') || strcmp(obj.bayesType, 'gaussian') || strcmp(obj.bayesType, 'G') || strcmp(obj.bayesType, 'g')
error('Not Implemented Yet');
end
end
end
end
obj.post = tempPost;
end
%% Process all Observations
function Process_Observes(obj, shuffType)
if nargin == 1
shuffYN = false;
elseif nargin == 2
shuffYN = true;
end
if shuffYN
[tempLike, ~] = obj.RandPermLikes(shuffType);
else
tempLike = obj.likeTrlSpikes;
end
if iscell(obj.likeTrlSpikes)
tempLike = cell2mat(tempLike);
end
tempProb = sum(~isnan(tempLike(:,1,:)),3)./sum(sum(~isnan(tempLike(:,1,:))));
if obj.bayesType == 1 || strcmp(obj.bayesType, 'Poisson') || strcmp(obj.bayesType, 'poisson') || strcmp(obj.bayesType, 'P') || strcmp(obj.bayesType, 'p')
obj.post = obj.CalcStaticBayesPost_Poisson(mean(tempLike,3, 'omitnan'), obj.obsvTrlSpikes, tempProb);
elseif obj.bayesType == 2 || strcmp(obj.bayesType, 'Bernoulli') || strcmp(obj.bayesType, 'bernoulli') || strcmp(obj.bayesType, 'B') || strcmp(obj.bayesType, 'b')
obj.post = obj.CalcStaticBayesPost_Bernoulli(mean(tempLike,3, 'omitnan'), obj.obsvTrlSpikes, tempProb);
elseif obj.bayesType == 3 || strcmp(obj.bayesType, 'Gaussian') || strcmp(obj.bayesType, 'gaussian') || strcmp(obj.bayesType, 'G') || strcmp(obj.bayesType, 'g')
obj.post = obj.CalcStaticBayesPost_Gaussian(mean(tempLike,3, 'omitnan'), std(obj.likeTrlSpikes,0,3,'omitnan'), obj.obsvTrlSpikes, tempProb);
end
obj.postTrlIDs = permute(obj.obsvTrlIDs(1,end,:), [1,3,2]);
end
%% Process all Observations Iteratively (cross-temporal decoding)
function Process_IterativeObserves(obj)
if iscell(obj.likeTrlSpikes)
tempLike = cell2mat(obj.likeTrlSpikes);
else
tempLike = obj.likeTrlSpikes;
end
% Pull out Trial IDs
trlIDs = squeeze(obj.obsvTrlIDs(1,end,:));
trlPosIDs = [obj.trialInfo(trlIDs).Position];
% Identify maximum number of sequence trials
maxNumSeqs = sum(trlPosIDs==1);
obj.post = cell(obj.seqLength, 1, maxNumSeqs);
obj.postTrlIDs = nan(obj.seqLength,maxNumSeqs);
for pos = 1:obj.seqLength
posIDs = trlIDs(trlPosIDs==pos);
obj.postTrlIDs(pos,1:length(posIDs)) = posIDs;
for trl = 1:length(posIDs)
tempObsv = obj.obsvTrlSpikes(:,:,trlIDs==posIDs(trl));
tempProb = sum(~isnan(tempLike(:,1,:)),3)./sum(sum(~isnan(tempLike(:,1,:))));
if obj.bayesType == 1 || strcmp(obj.bayesType, 'Poisson') || strcmp(obj.bayesType, 'poisson') || strcmp(obj.bayesType, 'P') || strcmp(obj.bayesType, 'p')
obj.post{pos,1,trl} = obj.CalcIterativeBayesPost_Poisson(mean(tempLike,3, 'omitnan'), tempObsv, obj.decodeIDvects(:,1), obj.decodeIDvects(:,4), tempProb);
elseif obj.bayesType == 2 || strcmp(obj.bayesType, 'Bernoulli') || strcmp(obj.bayesType, 'bernoulli') || strcmp(obj.bayesType, 'B') || strcmp(obj.bayesType, 'b')
error('Not implemented yet');
% obj.post{perm} = obj.CalcIterativeBayesPost_Bernoulli(mean(obj.likeTrlSpikes{perm},3, 'omitnan'), obj.obsvTrlSpikes{perm});
elseif obj.bayesType == 3 || strcmp(obj.bayesType, 'Gaussian') || strcmp(obj.bayesType, 'gaussian') || strcmp(obj.bayesType, 'G') || strcmp(obj.bayesType, 'g')
error('Not implemented yet');
% obj.post{perm} = obj.CalcIterativeBayesPost_Gaussian(mean(obj.likeTrlSpikes{perm},3, 'omitnan'), std(obj.likeTrlSpikes{perm},0,3), obj.obsvTrlSpikes{perm});
end
end
end
end
end
%% MLB Algorithms
methods
%% Calculate Static MLB Poisson
function post = CalcStaticBayesPost_Poisson(obj,likely, obsv, prob)
% tic;
post = nan(size(obsv,1), size(likely,1), size(obsv,3));
for trl = 1:size(obsv,3)
for t = 1:size(obsv,1)
p = nan(size(likely));
curPopVect = floor(obsv(t,:,trl)*(obj.binSize/obj.sampleRate));
curPopFact = factorial(curPopVect);
for u = 1:size(likely,2)
curAvgUniFR = likely(:,u);
p(:,u) = (((obj.binSize/obj.sampleRate).*curAvgUniFR).^curPopVect(u))./curPopFact(u);
end
p(p==0) = 0.00000000000000001;
pp = prod(p,2, 'omitnan');
ee = exp(-((obj.binSize/obj.sampleRate)*sum(likely,2)));
tempPost = prob.*pp.*ee;
post(t,:,trl) = tempPost./sum(tempPost);
end
end
% toc
end
%% Calculate Static MLB Bernoulli
function post = CalcStaticBayesPost_Bernoulli(~,likely, obsv, prob)
% Developed by M.Saraf...
% MS code: (organized neuron X time)
% for i = 1: size(expected_prob,2)
% indiv_neurons = [];
% for n = 1:size(expected_prob,1)
% indiv_neurons(n,:) = (expected_prob(n,:).^test_data(n,i)).*((1-expected_prob(n,:)).^(1-test_data(n,i)));
% end
% posteriors(i,:) = prod(indiv_neurons,1);
% end (edited)
post = nan(size(obsv,1), size(likely,1), size(obsv,3));
for trl = 1:size(obsv,3)
for t = 1:size(obsv,1)
indiv_neurons = nan(size(likely));
for u = 1:size(likely,2)
indiv_neurons(:,u) = (likely(:,u).^obsv(t,u,trl)).*((1-likely(:,u)).^(1-obsv(t,u,trl)));
end
tempPost = prob.*prod(indiv_neurons,2);
post(t,:,trl) = tempPost./sum(tempPost);
end
end
end
%% Calculate Static MLB Gaussian
function post = CalcStaticBayesPost_Gaussian(~,meanLikely, varLikely, obsv, prob)
% Developed by M.Saraf...
% MS Code: (organized neuron X time)
% %get the first term:
% first_term = prod(1./(sigma.*(2*pi)^0.5),1);
% %get the second term :
% second_term = [];
% for i = 1: size(mu,2)
% indiv_neurons = [];
% for n = 1:size(mu,1)
% a = test_data(n,i) - (mu(n,:));
% b = sigma(n,:);
% indiv_neurons(n,:) = (-0.5).*(a./b).^2;
% end
% second_term(:,i) = exp(sum(indiv_neurons,1));
% end
% posteriors = first_term.*second_term;
varLikely(varLikely==0) = min(varLikely(varLikely~=0));
% varLikely(varLikely==0) = nan;
% meanLikely(varLikely==0) = nan;
post = nan(size(obsv,1), size(meanLikely,1), size(obsv,3));
for trl = 1:size(obsv,3)
for t = 1:size(obsv,1)
first_term = prod(1./(varLikely*sqrt(2*pi)),2, 'omitnan');
second_term = nan(size(meanLikely));
for u = 1:size(meanLikely,2)
second_term(:,u) = -0.5.*(((obsv(t,u,trl)-meanLikely(:,u))./varLikely(:,u)).^2);
end
second_term = exp(sum(second_term,2, 'omitnan'));
tempPost = prob.*first_term.*second_term;
post(t,:,trl) = tempPost./sum(tempPost);
end
end
end
%% Calculate Iterative MLB Poisson
function post = CalcIterativeBayesPost_Poisson(obj, likely, obsv, depVar, grpVar, prob)
% Calculate posterior probability using a dependant variable and a grouping variable
% The dependant variable (mainly time) is the variable that is iterated across
% The grouping variable (mainly odor or position) is the variable used as the likelihoods at each level of the dependant variable
% For example, typical use case here is to look for temporally invariant coding where
% depVar = time, i.e. the algorithm will step through different time points
% grpVar = odor/position, i.e. the algorithm will then choose likelihoods from different levels of odor or position
% Note: The input "likely" is a concatenated vector containing depVar & grpVar arranged data.
% Original use case had the likely arranged with spiking bins from trial types arranged by time and concatenated together into a single
% vector such that depVar (time) and grpVar (odor/position) are both 1xN vectors with N = (trial time bins X #positions).
rateScalar = obj.binSize/obj.sampleRate;
lvlsDepVar = unique(depVar);
lvlsGrpVar = unique(grpVar);
post = nan(size(obsv,1), size(likely,1)/length(lvlsGrpVar), length(lvlsGrpVar), size(obsv,3));
for dv = 1:length(lvlsDepVar)
tempLikely = nan(length(lvlsGrpVar), size(likely,2));
tempProb = nan(length(lvlsGrpVar),size(prob,2));
for gv = 1:length(lvlsGrpVar)
tempLikely(gv,:) = likely(depVar==lvlsDepVar(dv) & grpVar==lvlsGrpVar(gv),:);
tempProb(gv,:) = prob(depVar==lvlsDepVar(dv) & grpVar==lvlsGrpVar(gv),:);
end
for trl = 1:size(obsv,3)
% tic;
for t = 1:size(obsv,1)
p = nan(size(tempLikely));
curPopVect = floor(obsv(t,:,trl)*rateScalar);
curPopFact = factorial(curPopVect);
for u = 1:size(tempLikely,2)
curAvgUniFR = tempLikely(:,u);
p(:,u) = ((rateScalar.*curAvgUniFR).^curPopVect(u))./curPopFact(u);
end
p(p==0) = 0.00000000000000001;
pp = prod(p,2, 'omitnan');
ee = exp(-(rateScalar*sum(tempLikely,2)));
tempTempPost = tempProb.*pp.*ee;
post(t,dv,:,trl) = tempTempPost ./ sum(tempTempPost);
end
% toc
end
end
% toc
end
%% Calculate Iterative MLB Bernoulli
% **** TO CREATE ****
function post = CalcIterativeBayesPost_Bernoulli(obj, likely, obsv, depVar, grpVar)
end
%% Calculate Iterative MLB Gaussian
% **** TO CREATE ****
function post = CalcIterativeBayesPost_Gaussian(obj, likely, obsv, depVar, grpVar)
end
end
%% Posterior Processing Methods
methods
%% Decode MLB
function [decode, maxPost] = DecodeBayesPost(obj, post, id)
if ~iscell(post)
if ndims(post) < 4
% Assumes post is in the structure of ObservTime X LikelyTime X Trial
decode = nan(size(post,1),size(post,3));
maxPost = nan(size(post,1),size(post,3));
for o = 1:size(post,3)
for d = 1:size(post,1)
if ~isnan(post(d,1,o))
maxPost(d,o) = max(post(d,:,o));
tempDecode = find(post(d,:,o)==maxPost(d,o));
select = rand(1,length(tempDecode));
decode(d,o) = id(tempDecode(select==max(select)));
end
end
end
else
% Assumes post is in the structure of ObservTime X LikelyTime X GrpVar X Trial
decode = nan(size(post,1),size(post,1),size(post,4));
maxPost = nan(size(post,1),size(post,1),size(post,4));
for trl = 1:size(post,4)
curTrl = post(:,:,:,trl);
for obsvTime = 1:size(curTrl,1)
for likeTime = 1:size(curTrl,2)
maxPost(obsvTime,likeTime,trl) = max(curTrl(obsvTime,likeTime,:));
if ~isnan(maxPost(obsvTime,likeTime,trl))
tempDecode = find(curTrl(obsvTime,likeTime,:)==maxPost(obsvTime,likeTime,trl));
select = rand(1,length(tempDecode));
decode(obsvTime,likeTime,trl) = tempDecode(select==max(select));
end
end
end
end
end
else
if ndims(post{1}) < 4 % Output from StaticBayes versions are 3d
if size(post{1},3) == size(obj.postTrlIDs,2)
decode = cell(size(post,1),1);
maxPost = cell(size(post,1),1);
for o = 1:size(post,1)
tempPost = post{o};
tempDecodes = nan(size(tempPost,1), size(tempPost,3));
tempMax = nan(size(tempPost,1), size(tempPost,3));
for trl = 1:size(tempPost,3)
for t = 1:size(tempPost,1)
tempMax(t,trl) = max(tempPost(t,:,trl));
tempDecode = find(tempPost(t,:,trl)==tempMax(t,trl));
select = rand(1,length(tempDecode));
tempDecodes(t,trl) = id(tempDecode(select==max(select)));
end
end
decode{o} = tempDecodes;
maxPost{o} = tempMax;
end
elseif size(post,3) == size(obj.postTrlIDs,2)
decode = cell(size(post,1),1,size(post,3));
maxPost = cell(size(post,1), size(post,3));
for o = 1:size(post,1)
for seq = 1:size(post,3)
curPost = post{o,1,seq};
curDecode = nan(size(post{1},1), size(post{1},2));
curMax = nan(size(post{1},1), size(post{1},2));
if ~isempty(curPost)
for tO = 1:size(curPost,1)
for tL = 1:size(curPost,1)
curMax(tO,tL) = max(curPost(tO,tL,:));
tempDecode = find(curPost(tO,tL,:)==curMax(tO,tL));
select = rand(1,length(tempDecode));
curDecode(tO,tL) = tempDecode(find(select==max(select)));
end
end
end
decode{o,1,seq} = curDecode;
maxPost{o,1,seq} = curMax;
end
end
end
elseif ndims(post) == 4 % Output from IterativeBayes versions are 4d
% % Assumes post is in the structure of ObservTime X LikelyTime X GrpVar X Trial
% decode = cell(size(post,1),1);
% maxPost = cell(size(post,1),1);
% % for
% decode = nan(size(post,1),size(post,1),size(post,4));
% maxPost = nan(size(post,1),size(post,1),size(post,4));
% for trl = 1:size(post,4)
% curTrl = post(:,:,:,trl);
% for obsvTime = 1:size(curTrl,1)
% for likeTime = 1:size(curTrl,2)
% maxPost(obsvTime,likeTime,trl) = max(curTrl(obsvTime,likeTime,:));
% if ~isnan(maxPost(obsvTime,likeTime,trl))
% tempDecode = find(curTrl(obsvTime,likeTime,:)==maxPost(obsvTime,likeTime,trl));
% select = rand(1,length(tempDecode));
% decode(obsvTime,likeTime,trl) = tempDecode(select==max(select));
% end
% end
% end
% end
end
end
end
%% New Decode MLB
function [decode, maxPost] = DecodeBayesPostNew(~,post,decodeDim)
if decodeDim==1
decode = nan(size(post,2), size(post,3));
maxPost = nan(size(post,2), size(post,3));
for r = 1:size(post,2)
for c = 1:size(post,3)
decode(r,c) = find(post(:,r,c)==max(post(:,r,c)),1,'first');
maxPost(r,c) = max(post(:,r,c));
end
end
elseif decodeDim==2
decode = nan(size(pos,1), size(post,3));
maxPost = nan(size(post,2), size(post,3));
for r = 1:size(post,1)
for c = 1:size(post,3)
decode(r,c) = find(post(r,:,c)==max(post(r,:,c)),1,'first');
maxPost(r,c) = max(post(r,:,c));
end
end
elseif decodeDim==3
decode = nan(size(post,1), size(post,2));
maxPost = nan(size(post,1), size(post,2));
for r = 1:size(post,1)
for c = 1:size(post,2)
if sum(~isnan(post(r,c,:)),3)~=0
decode(r,c) = find(post(r,c,:)==max(post(r,c,:)),1,'first');
maxPost(r,c) = max(post(r,c,:));
end
end
end
end
end
%% Tabluate MLB
function decode = TabulateBayesPost(~, post, id)
idS = unique(id);
if ~iscell(post)
decode = nan(size(post,1), length(idS), size(post,3));
for trl = 1:size(post,3)
for t = 1:size(post,1)
for iD = 1:length(idS)
decode(t,iD,trl) = sum(post(t,id==idS(iD),trl));
end
end
end
else
decode = cell(length(idS),1);
for p = 1:size(post,1)
tempPost = post{p};
tempDecode = nan(size(tempPost,1), size(tempPost,3), length(idS));
for trl = 1:size(tempPost,3)
for t = 1:size(tempPost,1)
for iD = 1:length(idS)
tempDecode(t,trl,iD) = sum(tempPost(t,id==idS(iD),trl));
end
end
end
decode{p} = tempDecode;
end
end
end
%% Calculate d' vector from decodings
function dOvrT = CalcDprmVectFromDecode(obj, decodeMtx, trlIDvect)
% decodeMtx here is a matrix NxM where N=time and M=trials
% trlIDvect here is a vector 1xM containing the trial IDs
% trlIDs = sort(obj.odrSeqs(:));
trlIDs = 1:obj.seqLength;
dOvrT = nan(size(decodeMtx,1),length(trlIDs));
for trl = 1:length(trlIDs)
tempTrlLogVect = trlIDvect==trlIDs(trl);
for t = 1:size(decodeMtx,1)
decodeCounts(1,1) = sum(decodeMtx(t,tempTrlLogVect)==trlIDs(trl));
decodeCounts(1,2) = sum(decodeMtx(t,tempTrlLogVect)~=trlIDs(trl));
decodeCounts(2,1) = sum(decodeMtx(t,~tempTrlLogVect)==trlIDs(trl));
decodeCounts(2,2) = sum(decodeMtx(t,~tempTrlLogVect)~=trlIDs(trl));
dOvrT(t,trl) = obj.CalculateDprime(decodeCounts);
end
end
end
%% Calculate d' matrix from decodings
function [dOvrTraw, dOvrTchance] = CalcDprmMtxFromDecode(obj, decodeMtx, trlIDvect)
% decodeMtx here is a NxMxP array where N = observation time, M = likelihood time and P = trials
% trlIDvect here is a 1xP matrix containing the trial IDs
% trlIDs = sort(obj.odrSeqs(:));
trlIDs = unique(trlIDvect);
rndDecodeMtx = nan(size(decodeMtx));
for trl = 1:size(decodeMtx,3)
tempDecode = decodeMtx(:,:,trl);
shuffDecode = sortrows([randperm(numel(tempDecode))',tempDecode(:)]);
rndDecodeMtx(:,:,trl) = reshape(shuffDecode(:,2), [size(decodeMtx,1),size(decodeMtx,2)]);
end
dOvrTraw = repmat({nan(size(decodeMtx,1),size(decodeMtx,2))},length(trlIDs),length(trlIDs));
dOvrTchance = repmat({nan(size(decodeMtx,1),size(decodeMtx,2))},length(trlIDs),length(trlIDs));
for trl1 = 1:length(trlIDs)
tempTrlLogVect = trlIDvect==trlIDs(trl1);
for trl2 = 1:length(trlIDs)
for to = 1:size(decodeMtx,1)
for tl = 1:size(decodeMtx,2)
decodeCounts(1,1) = sum(decodeMtx(to,tl,tempTrlLogVect)==trlIDs(trl2));
decodeCounts(1,2) = sum(decodeMtx(to,tl,tempTrlLogVect)~=trlIDs(trl2));
decodeCounts(2,1) = sum(decodeMtx(to,tl,~tempTrlLogVect)==trlIDs(trl2));
decodeCounts(2,2) = sum(decodeMtx(to,tl,~tempTrlLogVect)~=trlIDs(trl2));
dOvrTraw{trl1,trl2}(to,tl) = obj.CalculateDprime(decodeCounts);
randCounts(1,1) = sum(rndDecodeMtx(to,tl,tempTrlLogVect)==trlIDs(trl2));
randCounts(1,2) = sum(rndDecodeMtx(to,tl,tempTrlLogVect)~=trlIDs(trl2));
randCounts(2,1) = sum(rndDecodeMtx(to,tl,~tempTrlLogVect)==trlIDs(trl2));
randCounts(2,2) = sum(rndDecodeMtx(to,tl,~tempTrlLogVect)~=trlIDs(trl2));
dOvrTchance{trl1,trl2}(to,tl) = obj.CalculateDprime(randCounts);
end
end
end
end
end
%% Integrate Anti-diagonal
function int = IntegrateAntiDiagonal(~, mtx)
int = nan(1,size(mtx,1)*2);
for t = 1:2:size(mtx,1)*2
int(t) = trapz(diag(flip(mtx), t-size(mtx,1)));
end
int(isnan(int)) = [];
end
%% Calculate d', HR, FAR from posteriors
function [tMat_HR, tMat_FAR, tMat_D] = CalcDecodabilityTransMatFromPost(obj)
tMat_HR = cell(obj.seqLength, obj.seqLength);
tMat_FAR = cell(obj.seqLength, obj.seqLength);
tMat_D = cell(obj.seqLength, obj.seqLength);
% To avoid nans and infs any values ==1 or ==0 need to be replaced with the next nearest value
allVals = unique([obj.post{:}]);
if min(allVals)==0
minVal = allVals(2);
else
minVal = allVals(1);
end
if max(allVals)==1
maxVal = allVals(end-1);
else
maxVal = allVals(end);
end
clear allVals;
for trlPos = 1:obj.seqLength
for decPos = 1:obj.seqLength
tempHRs = obj.post(trlPos,1,:);
tempHRs(cellfun(@(a)isempty(a),tempHRs)) = [];
temp_tempHRs = cell2mat(cellfun(@(a){a(:,:,decPos)}, tempHRs));
temp_tempHRs(temp_tempHRs==0) = minVal;
temp_tempHRs(temp_tempHRs==1) = maxVal;
tMat_HR{trlPos, decPos} = temp_tempHRs;
faLog = (1:obj.seqLength ~= trlPos);
tempFARs = reshape(squeeze(obj.post(faLog,1,:)), [1,1,numel(obj.post)-(size(obj.post,3)*sum(~faLog))]);
tempFARs(cellfun(@(a)isempty(a),tempFARs)) = [];
temp_tempFARs = repmat(mean(cell2mat(cellfun(@(a){a(:,:,decPos)},tempFARs)),3,'omitnan'), [1,1,size(temp_tempHRs,3)]);
temp_tempFARs(temp_tempFARs==0) = minVal;
temp_tempFARs(temp_tempFARs==1) = maxVal;
tMat_FAR{trlPos,decPos} = temp_tempFARs;
tMat_D{trlPos,decPos} = norminv(temp_tempHRs) - norminv(temp_tempFARs);
end
end
end
%% Organize Posteriors into 3D TransMat
function [tm_HR, tm_D, tm_TrialInfo] = OrganizeDecodabilityTrialHistoryTransMat(obj,hr,d)
if nargin == 1
[hr, ~, d] = obj.CalcDecodabilityTransMatFromPost;
elseif nargin == 2
d = hr;
end
tm_HR = cell(obj.seqLength,obj.seqLength,obj.seqLength,obj.seqLength);
tm_D = cell(obj.seqLength,obj.seqLength,obj.seqLength,obj.seqLength);
tm_TrialInfo = cell(obj.seqLength,obj.seqLength,obj.seqLength);
seqStarts = find([obj.trialInfo.Position]==1);
for trlOP = 1:obj.seqLength
curTrls = obj.postTrlIDs(trlOP,:);
curTrls(isnan(curTrls)) = [];
for trl = 1:length(curTrls)
cur_TrialInfo = obj.trialInfo(curTrls(trl));
if curTrls(trl) ~= 1
cur_TrialInfo.PrevTrlEnd = obj.trialInfo(curTrls(trl)-1).PokeOutIndex;
else
cur_TrialInfo.PrevTrlEnd = nan;
end
if curTrls(trl) ~= length(obj.trialInfo)
cur_TrialInfo.NextTrlStart = obj.trialInfo(curTrls(trl)+1).PokeInIndex;
else
cur_TrialInfo.NextTrlStart = nan;
end
curSeqStart = seqStarts(find(seqStarts<=cur_TrialInfo.TrialNum,1,'last'));
curSeqVect = [obj.trialInfo(curSeqStart:cur_TrialInfo.TrialNum).Odor];
osLog = mod(curSeqVect,10)~=1:trlOP;
if sum(osLog)==0
osPos = trlOP;
osOdr = trlOP;
else
osPos = find(osLog);
osOdr = mod(curSeqVect(osPos),10);
end
tm_TrialInfo{osOdr,osPos,trlOP} = cat(2,tm_TrialInfo{osOdr,osPos,trlOP}, cur_TrialInfo);
for decPos = 1:obj.seqLength
tm_HR{osOdr,osPos,trlOP,decPos} = cat(3,tm_HR{osOdr,osPos,trlOP,decPos}, hr{trlOP,decPos}(:,:,trl));
tm_D{osOdr,osPos,trlOP,decPos} = cat(3,tm_D{osOdr,osPos,trlOP,decPos}, d{trlOP,decPos}(:,:,trl));
end
end
end
%
end
end
%% Mask Creation Method
methods
%% Create TrialInfo Mask
function Create_TrialInfoMasks(obj)
% Find trial start anchors
if strcmp(obj.alignments{1}, 'PokeIn')
tStart = zeros(size(obj.trialInfo));
startNdx = repmat(find(obj.obsvTimeVect==0), size(obj.trialInfo));
tEnd = [obj.trialInfo.PokeDuration];
endNdx = arrayfun(@(a){find(a<obj.obsvTimeVect,1,'first')},tEnd);
messedUpDur = find(cellfun(@(a)isempty(a),endNdx));
for mud = 1:length(messedUpDur)
if obj.trialInfo(messedUpDur(mud)).Performance == 0
obj.trialInfo(messedUpDur(mud)).PokeOutIndex = obj.trialInfo(messedUpDur(mud)).ErrorIndex;
newDur = (obj.trialInfo(messedUpDur(mud)).ErrorIndex - obj.trialInfo(messedUpDur(mud)).PokeInIndex)/obj.sampleRate;
elseif obj.trialInfo(messedUpDur(mud)).Performance == 1
obj.trialInfo(messedUpDur(mud)).PokeOutIndex = obj.trialInfo(messedUpDur(mud)).RewardSignalIndex;
newDur = (obj.trialInfo(messedUpDur(mud)).RewardSignalIndex - obj.trialInfo(messedUpDur(mud)).PokeInIndex)/obj.sampleRate;
end
tEnd(messedUpDur(mud)) = newDur;
endNdx{messedUpDur(mud)} = find(newDur<obj.obsvTimeVect,1,'first');
end
endNdx = cell2mat(endNdx);
elseif strcmp(obj.alignments{1}, 'PokeOut')
tStart = [obj.trialInfo.PokeDuration]*-1;
startNdx = arrayfun(@(a){find(a<obj.obsvTimeVect,1,'first')}, tStart);
messedUpDur = find(cellfun(@(a)isempty(a),startNdx));
for mud = 1:length(messedUpDur)
if obj.trialInfo(messedUpDur(mud)).Performance == 0
obj.trialInfo(messedUpDur(mud)).PokeOutIndex = obj.trialInfo(messedUpDur(mud)).ErrorIndex;
newDur = (obj.trialInfo(messedUpDur(mud)).ErrorIndex - obj.trialInfo(messedUpDur(mud)).PokeInIndex)/obj.sampleRate;
elseif obj.trialInfo(messedUpDur(mud)).Performance == 1
obj.trialInfo(messedUpDur(mud)).PokeOutIndex = obj.trialInfo(messedUpDur(mud)).RewardSignalIndex;
newDur = (obj.trialInfo(messedUpDur(mud)).RewardSignalIndex - obj.trialInfo(messedUpDur(mud)).RewardSignalIndex)/obj.sampleRate;
end
tStart(messedUpDur(mud)) = newDur*-1;
startNdx{messedUpDur(mud)} = find(newDur<obj.obsvTimeVect,1,'first');
end
startNdx = cell2mat(startNdx);
tEnd = zeros(size(obj.trialInfo));
endNdx = repmat(find(obj.obsvTimeVect==0), size(obj.trialInfo));
end
trlStarts = [obj.trialInfo.PokeInIndex];
trlEnds = [obj.trialInfo.PokeOutIndex];
pos1log = [obj.trialInfo.Position]==1;
pos1ndx = find(pos1log);
preTrlIntDur = [nan, trlStarts(2:end)-trlEnds(1:end-1)]/obj.sampleRate;
pstTrlIntDur = [trlStarts(2:end) - trlEnds(1:end-1), nan]/obj.sampleRate;
meanNonP1Int = mean(preTrlIntDur(~pos1log));
preTrlIntDur(pos1log) = meanNonP1Int;
prevTrlStartNdx = arrayfun(@(a)find(a<obj.obsvTimeVect, 1, 'first'), preTrlIntDur*-1);
preTrlIntDurMids = tStart-(preTrlIntDur./2);
preTrlIntDurMidNdx = arrayfun(@(a)find(a<obj.obsvTimeVect,1,'first'), preTrlIntDurMids);
pstTrlIntDur([pos1ndx(2:end)-1,end]) = meanNonP1Int;
nextTrlStartNdx = arrayfun(@(a)find(a>obj.obsvTimeVect, 1, 'last'), pstTrlIntDur);
pstTrlIntDurMids = tEnd+(pstTrlIntDur./2);
pstTrlIntDurMidNdx = arrayfun(@(a)find(a>obj.obsvTimeVect, 1, 'last'), pstTrlIntDurMids);
obj.mask_Trial = obj.BuildVectMaskMtx(startNdx,[startNdx', endNdx']-startNdx',length(obj.obsvTimeVect));
if strcmp(obj.alignments{1}, 'PokeIn')
obj.mask_Int = obj.BuildVectMaskMtx(startNdx, [prevTrlStartNdx', startNdx']-startNdx', length(obj.obsvTimeVect));
obj.mask_IntMid = obj.BuildVectMaskMtx(startNdx, repmat(preTrlIntDurMidNdx', [1,2])-startNdx', length(obj.obsvTimeVect));
elseif strcmp(obj.alignments{1}, 'PokeOut')
obj.mask_Int = obj.BuildVectMaskMtx(startNdx, [startNdx', nextTrlStartNdx]-startNdx', length(obj.obsvTimeVect));
obj.mask_IntMid = obj.BuildVectMaskMtx(startNdx, repmat(pstTrlIntDurMidNdx', [1,2])+startNdx', length(obj.obsvTimeVect));
else
error('What, poke in and poke out alignments aren''t good enough for you you? Get outta here and code if yourself ya lazy bum!' )
end
obj.mask_PreInt = obj.BuildVectMaskMtx(startNdx,[preTrlIntDurMidNdx', startNdx']-startNdx',length(obj.obsvTimeVect));
obj.mask_PreIntCON = obj.BuildVectMaskMtx(startNdx,[preTrlIntDurMidNdx', startNdx']-startNdx'-(floor((startNdx-preTrlIntDurMidNdx)/2))',length(obj.obsvTimeVect));
obj.mask_PstInt = obj.BuildVectMaskMtx(startNdx,[endNdx' pstTrlIntDurMidNdx']-startNdx',length(obj.obsvTimeVect));
obj.mask_PstIntCON = obj.BuildVectMaskMtx(startNdx,[endNdx' pstTrlIntDurMidNdx']-startNdx'+(floor((pstTrlIntDurMidNdx-endNdx)/2))',length(obj.obsvTimeVect));
end
end
%% Analyses
methods
%% Calculate Symmetry From Decodability
function [symTR, symDEC, symMN] = CalcSymmetry_D(obj,data)
if nargin<2
[~, data, ~] = obj.OrganizeDecodabilityTrialHistoryTransMat;
end