-
Notifications
You must be signed in to change notification settings - Fork 16
/
FitResult.m
1742 lines (1519 loc) · 78 KB
/
FitResult.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 FitResult < handle
% FITRESULT
% stores results of a fit using the Analysis object
% The results are for a single neuron over a range of configurations
%
% <a href="matlab: methods('FitResult')">methods</a>
% <a href="matlab:web('FitResultExamples.html', '-helpbrowser')">FitResult Examples</a>
%
% see also <a href="matlab:help('Analysis')">Analysis</a>
%
% Reference page in Help browser
% <a href="matlab:doc('FitResult')">doc FitResult</a>
%
% nSTAT v1 Copyright (C) 2012 Masschusetts Institute of Technology
% Cajigas, I, Malik, WQ, Brown, EN
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License as published
% by the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
% See the GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software Foundation,
% Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
properties
numResults %Number of results in this FitResult object
lambda %Lambda signal
numCoeffs %Number of coefficients for each fitResult
fitType %Poisson or Binomial
b %coefficients for each fit
dev %deviance for each fit
AIC %Akaike's Information Criterion for each fit
BIC %Baysian Information Criterion for each fit
logLL %Log Likelihood
stats %Relevant statistics for each fit
configs % the config collection for the different fits
configNames % names of the the differen fits
neuronNumber % the number of the neuron the data comes from
neuralSpikeTrain % the spike data
covLabels
uniqueCovLabels
indicesToUniqueLabels
numHist % Number of history terms (used for indexing into the regression coefficients) for each fit
histObjects % History object for self firing
ensHistObjects % History object to be applied to the neuron's neigbors to compute ensemble effect
flatMask
Z % Rescaled spike times from the Time-Rescaling theorem, exponential rate 1
U % Transformed z's -> uniform in [0,1]
X % Transformed u's -> gaussian in [-inf, inf]
Residual %fit residual
% xAxis
% KSSorted
% ks_stat
invGausStats
KSStats %Kolmogorov Smirnov Statistics
plotParams
XvalData % cell array of raw data used for validation
XvalTime % cell array of time vectors for each element of XvalData
validation % a FitResult object with the validation data
minTime % The minTime from the spikeTrain (not necessarily the analysis)
maxTime % The maxTime for the spikeTrain (not necessarily the analysis)
end
properties (Constant,Hidden)
colors={'b','g','r','c','m','y','k'};
end
methods
function fitObj=FitResult(spikeObj,covLabels,numHist,histObjects,ensHistObj,lambda,b, dev, stats,AIC,BIC,logLL, configColl,XvalData,XvalTime,distribution)
% fitObj=FitResult(spikeObj,covLabels,numHist,histObjects,ensHistObj,lambda,b, dev, stats,AIC,BIC,configColl,XvalData,XvalTime)
% Stores the results of multiple regressions for a single neuron into a accessible structure.
%
% spikeObj: The spike train for the neuron whose results are
% being stored.
% covLabels: A 2-d cell array, the jth row has all the labels for the covariates used in the jth fit
% numHist: The number of history terms in each of the N fits.
% histObjects: The History object for each of the N fits
% ensHistObj: The History object for used to compute the ensemble history effect.
% lambda: The conditional intensity function evaluated usin the
% data. Each dimension of lambda corresponds to the a different
% GLM Fit.
% b: N-component cell array containing the GLM regression
% coefficient of each of the fits. The jth component has all
% the regression coefficients for the jth trial.
% dev: vector of Deviances for each the GLM fits.
% stats: Cell array of the stats parameters for each GLM fit;
% AIC: vector of Akaike's information criteria for each the GLM fits.
% BIC: vector of Bayes Information criteria for each the GLM fits.
% configColl: configCollection object used to generate this
% results
% XvalData: Data to be used for validation.
% XvalTime: Time vector for the data.
if(nargin< 14)
XvalTime =[];
end
if(nargin<13)
XvalData =[];
end
if(isa(spikeObj,'cell'))
for i=1:length(spikeObj)
if(isnumeric(spikeObj{i}.name))
nNumber(i) =spikeObj{i}.name;
else
nNumber(i) = str2double(spikeObj{i}.name(~isletter(spikeObj{i}.name)));
end
minTime(i)=spikeObj{i}.minTime;
maxTime(i)=spikeObj{i}.maxTime;
end
nNumber = unique(nNumber);
minTime = unique(minTime);
maxTime = unique(maxTime);
if(length(nNumber)>1)
error('Can only have a FitResults with spike trains from a single neuron');
end
if(length(minTime)>1 || length(maxTime)>1)
error('Spike Trains are of different lengths');
end
elseif(isa(spikeObj,'nspikeTrain'))
if(isnumeric(spikeObj.name))
nNumber =spikeObj.name;
else
nNumber = str2double(spikeObj.name(~isletter(spikeObj.name)));
end
minTime=spikeObj.minTime;
maxTime=spikeObj.maxTime;
end
fitObj.neuronNumber = nNumber; %str2num(spikeObj.name);
fitObj.neuralSpikeTrain = spikeObj;
fitObj.minTime = minTime;
fitObj.maxTime = maxTime;
fitObj.numResults = 0;
fitObj.configs = configColl;
fitObj.configNames = configColl.getConfigNames;
fitObj.covLabels=covLabels;
fitObj.uniqueCovLabels= getUniqueLabels(covLabels);
fitObj.mapCovLabelsToUniqueLabels;
fitObj.numHist=numHist;
fitObj.histObjects = histObjects;
fitObj.ensHistObjects = ensHistObj;
fitObj.addParamsToFit(fitObj.neuronNumber,lambda,b, dev, stats,AIC,BIC,logLL,configColl);
fitObj.Z =[]; %rescaled spikes times - exponentially dist.
fitObj.U =[]; %rescaled spike times - uniformly dist.
fitObj.X =[]; %rescaled spike times - gaussian dist.
fitObj.Residual =[]; %fit residual for PP
fitObj.KSStats.xAxis =[];
fitObj.KSStats.KSSorted =[];
fitObj.KSStats.ks_stat =[];
fitObj.invGausStats.rhoSig=[];
fitObj.invGausStats.confBoundSig=[];
fitObj.plotParams = [];
fitObj.XvalData = XvalData;
fitObj.XvalTime = XvalTime;
fitObj.fitType = distribution;
end
function fitObj = setNeuronName(fitObj,name)
fitObj.neuronNumber = name;
end
function mFitRes = mergeResults(fitObj,newFitObj)
% mFitRes = mergeResults(fitObj,newFitObj)
% mFitRes contains the results from fitObj followed by the
% results of newFitObj in a single new FitResult object
%
% newFitObj can be of class 'FitResult' or a cell array of
% 'FitResult' objects. In the latter case, the results are
% apppended in the order that they appear in each cell array.
if(isa(newFitObj,'FitResult'))
% newFitObj.neuronNumber
if(fitObj.neuronNumber ==newFitObj.neuronNumber)
spikeObj = fitObj.neuralSpikeTrain;
covLabels = fitObj.covLabels(1:fitObj.numResults);
covLabels((fitObj.numResults+1):(fitObj.numResults+newFitObj.numResults)) = newFitObj.covLabels(1:newFitObj.numResults);
numHist = fitObj.numHist(1:fitObj.numResults);
numHist((fitObj.numResults+1):(fitObj.numResults+newFitObj.numResults)) = newFitObj.numHist(1:newFitObj.numResults);
histObjects=fitObj.histObjects(1:fitObj.numResults);
histObjects((fitObj.numResults+1):(fitObj.numResults+newFitObj.numResults)) = newFitObj.histObjects(1:newFitObj.numResults);
ensHistObjects=fitObj.ensHistObjects(1:fitObj.numResults);
ensHistObjects((fitObj.numResults+1):(fitObj.numResults+newFitObj.numResults)) = newFitObj.ensHistObjects(1:newFitObj.numResults);
b=fitObj.b(1:fitObj.numResults);
b((fitObj.numResults+1):(fitObj.numResults+newFitObj.numResults)) = newFitObj.b(1:newFitObj.numResults);
dev = [fitObj.dev newFitObj.dev];
AIC = [fitObj.AIC newFitObj.AIC];
BIC = [fitObj.BIC newFitObj.BIC];
logLL = [fitObj.logLL newFitObj.logLL];
stats=fitObj.stats(1:fitObj.numResults);
stats((fitObj.numResults+1):(fitObj.numResults+newFitObj.numResults)) = newFitObj.stats(1:newFitObj.numResults);
lambda = fitObj.lambda.merge(newFitObj.lambda);
for i=1:fitObj.numResults
config{i}=fitObj.configs.getConfig(i);
end
offset=fitObj.numResults;
for i=1:newFitObj.numResults
config{i+offset}=newFitObj.configs.getConfig(i);
end
configColl= ConfigColl(config);
XvalData = [fitObj.XvalData newFitObj.XvalData];
XvalTime = [fitObj.XvalTime newFitObj.XvalTime];
distribution=fitObj.fitType(1:fitObj.numResults);
distribution((fitObj.numResults+1):(fitObj.numResults+newFitObj.numResults)) = newFitObj.fitType(1:newFitObj.numResults);
tempZ = zeros(length(fitObj.Z),size(newFitObj.Z,2));
tempU = zeros(length(fitObj.U),size(newFitObj.U,2));
tempZ(1:length(newFitObj.Z),:) = newFitObj.Z;
tempU(1:length(newFitObj.U),:) = newFitObj.U;
Z=[fitObj.Z tempZ];
U=[fitObj.U tempU];
[X,rhoSig,confBoundSig] = Analysis.computeInvGausTrans(Z);
M=fitObj.Residual.merge(newFitObj.Residual);
origLength = size(fitObj.KSStats.xAxis,1);
currLength = size(newFitObj.KSStats.xAxis,1);
if(currLength~=origLength)
%we use this because some times the time scales
%dont match up. In particular when spikeTrain is
%segmented by steps or windows and the window sizes
%are normalized to 1.
newX = fitObj.KSStats.xAxis;
oldX = newFitObj.KSStats.xAxis;
oldY = newFitObj.KSStats.KSSorted;
y = interp1(oldX,oldY,newX(:,1),'spline','extrap');
xAxis = [fitObj.KSStats.xAxis newX(:,1)];
KSSorted = [fitObj.KSStats.KSSorted y];
else
xAxis = [fitObj.KSStats.xAxis newFitObj.KSStats.xAxis];
KSSorted = [fitObj.KSStats.KSSorted newFitObj.KSStats.KSSorted];
end
ks_stat = [fitObj.KSStats.ks_stat newFitObj.KSStats.ks_stat];
mFitRes=FitResult(spikeObj,covLabels,numHist,histObjects,ensHistObjects,lambda,b, dev, stats,AIC,BIC,logLL,configColl,XvalData,XvalTime,distribution);
mFitRes.setKSStats(Z,U, xAxis, KSSorted, ks_stat);
mFitRes.setInvGausStats(X,rhoSig,confBoundSig);
mFitRes.setFitResidual(M);
elseif(isa(newFitObj,'cell'))
if(isa(newFitObj{1},'FitResult'))
for i=1:length(newFitObj)
if(i==1)
mFitRes = fitObj.mergeResults(newFitObj{i});
else
mFitRes = mFitRes.mergeResults(newFitObj{i});
end
end
end
end
end
end
function subsetFit = getSubsetFitResult(fitObj,subfits)
%subfits is a row vector with numbers corresponding to the
%subfits
if(and(min(subfits)>0,max(subfits)<=fitObj.numResults))
spikeObj = fitObj.neuralSpikeTrain;
covLabels = fitObj.covLabels(subfits);
numHist = fitObj.numHist(subfits);
histObjects = fitObj.histObjects(subfits);
ensHistObj = fitObj.ensHistObjects(subfits);
lambda = fitObj.lambda.getSubSignal(subfits);
b = fitObj.b(subfits);
dev = fitObj.dev(subfits);
stats = fitObj.stats(subfits);
AIC = fitObj.AIC(subfits);
BIC = fitObj.BIC(subfits);
logLL = fitObj.logLL(subfits);
configColl = fitObj.configs.getSubsetConfigs(subfits);
XvalData = fitObj.XvalData;
XvalTime = fitObj.XvalTime;
distribution = fitObj.fitType;
subsetFit=FitResult(spikeObj,covLabels,numHist,histObjects,ensHistObj,lambda,b, dev, stats,AIC,BIC,logLL,configColl,XvalData,XvalTime,distribution);
Z = fitObj.Z(:,subfits);
U = fitObj.U(:,subfits);
X = fitObj.X(:,subfits);
xAxis= fitObj.KSStats.xAxis(:,subfits);
KSSorted=fitObj.KSStats.KSSorted(:,subfits);
ks_stat=fitObj.KSStats.ks_stat(subfits);
rhoSig=fitObj.invGausStats.rhoSig.getSubSignal(subfits);
confBoundSig=fitObj.invGausStats.confBoundSig;
M = fitObj.Residual.getSubSignal(subfits);
subsetFit.setKSStats(Z,U, xAxis, KSSorted, ks_stat);
subsetFit.setInvGausStats(X,rhoSig,confBoundSig);
subsetFit.setFitResidual(M);
end
end
function addParamsToFit(fitObj,neuronNum,lambda,b, dev, stats,AIC,BIC,logLL,configColl)
% addParamsToFit(fitObj,neuronNum,lambda,b, dev, stats,AIC,BIC,configColl)
% Add the specified parameters to the current FitResult object
% only if the neuronNum matches the neuronNum of this object
if(fitObj.neuronNumber==neuronNum)
if(isa(lambda,'cell'))
newLambda=lambda{1};
for i=2:length(lambda)
newLambda = newLambda.merge(lambda{i});
end
elseif(isa(lambda,'Covariate')||isa(lambda,'SignalObj'))
newLambda = lambda;
end
numNewResults = newLambda.dimension;%number of new elements
if(nargin<8)
configColl = cell(1,numNewResults);
end
if(numNewResults==1)
fitObj.b{fitObj.numResults+1} = b{1};
fitObj.dev(fitObj.numResults+1) = dev;
fitObj.stats{fitObj.numResults+1}= stats{1};
if(nargin<7)
fitObj.AIC(fitObj.numResults+1) = 2*length(b)+dev;
fitObj.BIC(fitObj.numResults+1) = length(b)*log(length(newLambda.time))+dev;
delta = 1/newLambda.sampleRate;
fitObj.logLL(fitObj.numResults+1) = sum(y.*log(data*delta)+(1-y).*(1-newLambda.data*delta));
else
fitObj.AIC(fitObj.numResults+1) = AIC;
fitObj.BIC(fitObj.numResults+1) = BIC;
fitObj.logLL(fitObj.numResults+1) = logLL;
end
fitObj.numCoeffs(fitObj.numResults+1) = length(b);
else
for i=1:numNewResults
fitObj.b{fitObj.numResults+i} = b{i};
fitObj.dev(fitObj.numResults+i) = dev(i);
fitObj.stats{fitObj.numResults+i}= stats{i};
if(nargin<7)
fitObj.AIC(fitObj.numResults+i) = 2*length(b{i})+dev(i);
fitObj.BIC(fitObj.numResults+i) = length(b{i})*log(length(newLambda.time))+dev(i);
delta=fitObj.neuralSpikeTrain.sampleRate;
y=fitObj.neuralSpikeTrain.getSigRep.dataToMatrix;
fitObj.logLL(fitObj.numResults+i)= sum(y.*log(newLambda.data*delta)+(1-y).*(1-newLambda.data*delta));
else
fitObj.AIC(fitObj.numResults+i) = AIC(i);
fitObj.BIC(fitObj.numResults+i) = BIC(i);
fitObj.logLL(fitObj.numResults+i)= logLL(i);
end
fitObj.numCoeffs(fitObj.numResults+i) = length(b{i});
end
end
if(fitObj.numResults ==0)
fitObj.lambda = newLambda;
else
fitObj.lambda = fitObj.lambda.merge(newLambda); %new lambda
end
fitObj.numResults = fitObj.numResults+numNewResults;
dataLabels = cell(1,fitObj.numResults);
for i=1:fitObj.numResults
dataLabels{i} = strcat('\lambda_{',num2str(i),'}');
end
fitObj.lambda.setDataLabels(dataLabels);
fitObj.configs.addConfig(configColl);
fitObj.configNames = fitObj.configs.getConfigNames;
else
error('Neuron number does not match');
end
end
function [lambda, logLL] = computeValLambda(fitObj)
% lambda = computeValLambda(fitObj)
% Returns a Covariate object lambda. This is the Conditional
% intensity function evaluated using the validation data
lambdaData = zeros(length(fitObj.XvalTime{1}),fitObj.numResults);
for i=1:fitObj.numResults
lambdaData(:,i) = fitObj.evalLambda(i,fitObj.XvalData{i});
end
lambda=Covariate(fitObj.XvalTime{1},lambdaData,...
'\lambda(t)',fitObj.lambda.xlabelval,...
fitObj.lambda.xunits,'Hz',fitObj.lambda.dataLabels);
delta = 1/lambda.sampleRate;
y=fitObj.neuralSpikeTrain.getSigRep.dataToMatrix;
logLL =sum(y.*log(lambda.data*delta)+(1-y).*(1-lambda.data*delta));
end
function mapCovLabelsToUniqueLabels(fitObj)
% mapCovLabelsToUniqueLabels(fitObj)
% Used internally by the FitResult class generate a matrix that
% maps how covariate labels of the fit object map to unique
% covariate labels. For example, multiple fits that have a
% constant baseline term will be assumed to refer to the same
% "baseline" term and not two separate ones
flatMask = zeros(length(fitObj.uniqueCovLabels),length(fitObj.covLabels));
for j=1:length(fitObj.covLabels)
currLabels = fitObj.covLabels{j};
index=zeros(1,length(currLabels));
for i=1:length(currLabels)
index(i)=strmatch(currLabels{i}, fitObj.uniqueCovLabels, 'exact');
end
fitObj.indicesToUniqueLabels{j} = index;
flatMask(index,j) = 1;
end
fitObj.flatMask = flatMask;
end
function p=getPlotParams(fitObj)
% p=getPlotParams(fitObj)
if(isempty(fitObj.plotParams))
fitObj.computePlotParams;
end
p=fitObj.plotParams;
end
function plotValidation(fitObj)
% plotValidation(fitObj)
% calls plotResults on the validation FitResult object if
% validation data is present. Note that the GLM coefficients
% are not recomputed and therefore the same as those obtained
% from the training data.
if(~isempty(fitObj.validation))
fitObj.validation.plotResults;
else
display('Validation Data not available to plot');
end
end
function answer = isValDataPresent(fitObj)
% answer = isValDataPresent(fitObj)
% returns 1 if validation data is present. This method is used
% to determine if validation data is available to compute the
% validation results.
answer = 0;
if(~isempty(fitObj.XvalTime) && ~isempty(fitObj.XvalData))
for i=1:length(fitObj.XvalTime)
currTime = fitObj.XvalTime{i};
if(~isempty(currTime))
if(currTime(end)-currTime(1)>0)
answer =1;
break;
end
end
end
end
end
function lambdaData = evalLambda(fitObj,lambdaIndex,newData)
% lambdaData = evalLambda(fitObj,lambdaIndex,newData)
% lambdaIndex: the index of the corresponding lambda to be
% evaluated with the new data.
% newData: matrix of covariates in same order as fits without
% constant term in first column
% if(isa(newData,'double'))
% [~,columns] = size(newData);
% tempData = cell(1,columns);
% for i=1:columns
% tempData{i} = newData(:,i);
% end
% newData = tempData;
% end
if(lambdaIndex>0 && lambdaIndex <= fitObj.numResults)
b=fitObj.b{lambdaIndex}; %coefficient matrix
if(isempty(newData))
[rows,~] = size(newData);
baseline=ones(rows,1);
lambdaData = exp(b(1)*baseline);
else
if(isa(newData,'double')) %matrix, 1 column per coefficient
baseline=ones(length(newData),1);
[~,columns] = size(newData);
if(length(b)>=1)
lambdaData = exp(newData*b(1:end));
if(strcmp(fitObj.fitType{lambdaIndex},'poisson'))
% lambdaData = exp(newData*b(1:end));
% lambdaData = exp(b(1) + newData*b(2:end));
else
% lambdaData = exp(b(1) + newData*b(2:end));
lambdaData = lambdaData./(1+lambdaData);
end
% else
% if(strcmp(fitObj.fitType{lambdaIndex},'poisson'))
% lambdaData = exp(b(1)*baseline);
%
% else
% lambdaData = exp(b(1)*baseline);
% lambdaData = lambdaData./(1+lambdaData);
% end
end
lambdaData = lambdaData*fitObj.neuralSpikeTrain.sampleRate;
elseif(isa(newData,'cell')) % a cell array, each element is matrix of values for each coeff
% baseline=ones(size(newData{1})); %design matrix
runSum=0;
for i=1:(length(newData)) %-fitObj.numHist(lambdaIndex))
% if(i==1)
% runSum = b(1)*baseline;
% else
if(i<=length(b))
runSum = runSum+b(i)*newData{i};
end
% end
end
if(strcmp(fitObj.fitType{lambdaIndex},'poisson'))
lambdaData = exp(runSum);
lambdaData = lambdaData*fitObj.neuralSpikeTrain.sampleRate;
else
lambdaData = exp(runSum);
lambdaData = lambdaData./(1+lambdaData);
lambdaData = lambdaData*fitObj.neuralSpikeTrain.sampleRate;
end
else
error('New data must be cell or a matrix');
end
end
else
error('Index into fit params is incorrect');
end
end
% function handle = plotHist(fitObj,fitNum)
% % handle = plotHist(fitObj,fitNum)
% % plots the history terms used in this FitResult object
% % if fitNum is not specified then fitNum=1:numResults
% if(nargin<2 || isempty(fitNum))
% fitNum = 1:fitObj.numResults;
% end
%
% for j=fitNum
% if(j>0 && j <= fitObj.numResults)
% b=fitObj.b{j}; %coefficient matrix
% startHistIndex = length(b)-fitObj.numHist(j)+1;
% if(startHistIndex<length(b))
% bHist = b(startHistIndex:end);
% if(~isempty(fitObj.histObjects{j}))
% windowTimes = fitObj.histObjects{j}.windowTimes;
% t=linspace(windowTimes(1),windowTimes(end),100)';
% histEffect = zeros(length(t),1);
% for i=1:length(windowTimes)-1
% index = and(t>=windowTimes(i),t<=windowTimes(i+1));
% histEffect(index)=exp(-bHist(i))-1; %To offset zero coeffs
% end
% end
% else
% t=[0; 0.00001];
% histEffect =[0;0];
% end
% if(j==fitNum(1))
% hSig = SignalObj(t,histEffect,'History','time','s','',fitObj.lambda.dataLabels{j});
% else
% hSig = hSig.merge(SignalObj(t,histEffect,'History Effect','time','s','',fitObj.lambda.dataLabels{j}));
% end
% end
% end
% N=floor(length(hSig.time)./70); B=ones(1,N)/N; A=1;
% handle=hSig.filtfilt(B,A).plot;
% end
function computePlotParams(fitObj,fitNum)
if(nargin<2)
fitNum = 1:fitObj.numResults;
end
index=find(sum(fitObj.flatMask,2)>0);%1:length(fitObj.flatMask(:,1));
%Only use the labels that appear in at least one fit
%Otherwise that parameter was not present for any of the
%regressions and just takes up plot real-estate
sigIndex=zeros(length(index),length(fitNum));
bAct = nan(length(index),length(fitNum));
seAct= nan(length(index),length(fitNum));
for i=fitNum
%this indexing is to avoid extremely large se's from
%affecting plots
criteria = find(fitObj.stats{i}.se'<100);
%indicesForFit = find(fitObj.flatMask(index,i)==1);
indicesForFit = fitObj.indicesToUniqueLabels{i};
bVals = fitObj.b{i}(criteria);
bAct(indicesForFit(criteria),i) = bVals; %sorted according to uniqueLabels
seVals = fitObj.stats{i}.se(criteria)';
seAct(indicesForFit(criteria),i)= seVals; %sorted according to uniqueLabels;
temp = sign([bAct(:,i)-seAct(:,i) bAct(:,i)+seAct(:,i)]);
productOfSigns = temp(:,1).*temp(:,2); %should be positive
sIndex=and(productOfSigns>0,seAct(:,i)~=0);
sigIndex(:,i)=sIndex;
end
fitObj.plotParams.bAct = bAct;
fitObj.plotParams.seAct= seAct;
fitObj.plotParams.sigIndex = sigIndex;
fitObj.plotParams.xLabels = cell(length(index),1);
fitObj.plotParams.xLabels = fitObj.uniqueCovLabels;
% for i=1:(length(index))
% if(i==1)
% fitObj.plotParams.xLabels{i} = 'baseline';
% %text(i, 0,'baseline','interpreter','latex');
% else
% fitObj.plotParams.xLabels{i} = fitObj.covLabels{index(i)-1};
% %text(i, 0,fitObj.covLabels{index(i)-1},'interpreter','latex');
% end
% end
tempVal =sum(fitObj.flatMask,2);
fitObj.plotParams.numResultsCoeffPresent =tempVal(index);
end
function [coeffIndex, epochId,numEpochs] = getCoeffIndex(fitObj,fitNum,sortByEpoch)
if(nargin<3 || isempty(sortByEpoch))
sortByEpoch=0;
end
if(nargin<2 || isempty(fitNum))
fitNum = 1:fitObj.numResults;
end
if(isempty(fitObj.plotParams))
fitObj.computePlotParams;
end
[histIndex, epochId] = fitObj.getHistIndex(fitNum,sortByEpoch);
allIndex = 1:length(fitObj.uniqueCovLabels);
nonHistIndex = setdiff(allIndex,histIndex);
% nonNANIndex = find(sum(~isnan(fitObj.plotParams.bAct(:,fitNum)),2)>=1);
nonNANIndex= allIndex;
actCoeffIndex = nonHistIndex(ismember(nonHistIndex, nonNANIndex));
allCoeffTerms = fitObj.uniqueCovLabels(actCoeffIndex);
% coeffName = cell(size(allCoeffTerms));
epochStartInd=regexp(allCoeffTerms,'_\{\d*\}','start');
epochEndInd=regexp(allCoeffTerms,'_\{\d*\}','end');
allCoeffIndex = [];
nonEpochIndex=[];
% nonEmptyCoeffNameInd = [];
epochsExist =0;
for i=1:length(allCoeffTerms)
if(~isempty(allCoeffTerms{i}))
allCoeffIndex = [allCoeffIndex i];
if(~isempty(epochStartInd{i}))
% nonEmptyCoeffNameInd = [nonEmptyCoeffNameInd i];
epochsExist=1;
actStart = epochStartInd{i}+2;
actEnd = epochEndInd{i}-1;
numEpoch(i) = str2num(allCoeffTerms{i}(actStart:actEnd));
% coeffName{i} = allCoeffTerms{i}(1:actStart-3);
else
nonEpochIndex = [nonEpochIndex i];
numEpoch(i) = 0; % make terms that only appear once part of epoch 0.
end
end
end
% coeffName = coeffName(nonEmptyCoeffNameInd);
if(epochsExist && ~sortByEpoch)
totalEpochs = unique(numEpoch);
coeffIndex = nonEpochIndex;
if(nargout>1)
epochId=zeros(size(nonEpochIndex));
end
for i=1:length(totalEpochs)
if(totalEpochs(i)~=0)
coeffIndex = [coeffIndex, find(numEpoch==totalEpochs(i))];
if(nargout>1)
epochId = [epochId, totalEpochs(i)*ones(size(find(numEpoch==totalEpochs(i))))];
end
end
end
coeffIndex = actCoeffIndex(coeffIndex);
elseif(epochsExist && sortByEpoch)
coeffIndex = actCoeffIndex(allCoeffIndex);
if(nargout>1)
epochId = numEpoch;
end
else
coeffIndex = actCoeffIndex(allCoeffIndex);
if(nargout>1)
epochId = zeros(size(allCoeffIndex)); %no epochs exist so just create same index for all;
end
end
% nonNANIndex = find(sum(~isnan(fitObj.plotParams.bAct(:,fitNum)),2)>=1);
nonNANIndex = allIndex;
coeffIndex = coeffIndex(ismember(coeffIndex, nonNANIndex));
if(nargout>2)
numEpochs = length(unique(epochId));
end
end
function h=plotCoeffsWithoutHistory(fitObj,fitNum,sortByEpoch,plotSignificance)
if(nargin<4 || isempty(plotSignificance))
plotSignificance=1;
end
if(nargin<3 || isempty(sortByEpoch))
sortByEpoch = 0;
end
if(nargin<2 || isempty(fitNum))
fitNum = 1:fitObj.numResults;
end
if(isempty(fitObj.plotParams))
fitObj.computePlotParams;
end
coeffIndex = fitObj.getCoeffIndex(fitNum,sortByEpoch);
h=fitObj.plotCoeffs([],fitNum,[],plotSignificance,coeffIndex);
end
function [histIndex, epochId,numEpochs] = getHistIndex(fitObj,fitNum,sortByEpoch)
%if sortByEpoch==1 then we group all regression terms with the
%same name one next to each other by epoch (time interval).
%Otherwise, we show all epoch one terms, followed by all epoch
%2 terms, etc.
if(nargin<3 || isempty(sortByEpoch))
sortByEpoch = 0;
end
if(nargin<2 || isempty(fitNum))
fitNum = 1:fitObj.numResults;
end
if(isempty(fitObj.plotParams))
fitObj.computePlotParams;
end
allHistTerms = regexp(fitObj.uniqueCovLabels,'^[\w*');
epochStartInd=regexp(fitObj.uniqueCovLabels,'\]_\{\d*\}','start');
epochEndInd=regexp(fitObj.uniqueCovLabels,'\]_\{\d*\}','end');
allHistIndex = [];
epochsExist =0;
for i=1:length(allHistTerms)
if(~isempty(allHistTerms{i}))
allHistIndex = [allHistIndex i];
if(~isempty(epochStartInd{i}))
epochsExist=1;
actStart = epochStartInd{i}+3;
actEnd = epochEndInd{i}-1;
numEpoch(i) = str2num(fitObj.uniqueCovLabels{i}(actStart:actEnd));
end
end
end
if(epochsExist && ~sortByEpoch)
totalEpochs = unique(numEpoch);
histIndex = [];
if(nargout>1)
epochId=[];
end
for i=1:length(totalEpochs)
histIndex = [histIndex, find(numEpoch==totalEpochs(i))];
if(nargout>1)
epochId = [epochId, totalEpochs(i)*ones(size(find(numEpoch==totalEpochs(i))))];
end
end
elseif(epochsExist && sortByEpoch)
histIndex = allHistIndex;
if(nargout>1)
epochId = numEpoch;
end
else
histIndex = allHistIndex;
if(nargout>1)
epochId = zeros(size(allHistIndex)); %no epochs exist so just create same index for all;
end
end
% nonNANIndex = find(sum(~isnan(fitObj.plotParams.bAct(:,fitNum)),2)>=1);
% histIndex = histIndex(ismember(histIndex, nonNANIndex));
if(nargout>2)
numEpochs = length(unique(epochId));
end
end
function [coeffMat, labels, SEMat] = getCoeffs(fitObj, fitNum)
if(nargin<2 || isempty(fitNum))
fitNum =1:fitObj.numResults;
end
sortByEpoch = 0; % Make sure we have different time series if the history is divided into epochs;
[coeffIndex, epochId, numEpochs] = fitObj.getCoeffIndex(fitNum,sortByEpoch);
epochNums = unique(epochId);
coeffStrings = fitObj.uniqueCovLabels(coeffIndex);
baseStringEndIndex =regexp(coeffStrings,'_\{\d*\}','start');
for i=1:length(baseStringEndIndex)
if(~isempty(baseStringEndIndex{i}))
baseStrings{i} = coeffStrings{i}(1:baseStringEndIndex{i}-1);
else
baseStrings{i} = coeffStrings{i};
end
end
uniqueCoeffs = unique(baseStrings);
for i=1:length(uniqueCoeffs)
coeffStrIndex{i} = coeffIndex(strcmp(baseStrings,uniqueCoeffs{i}));
if(min(epochId)==0)
epochIndices{i} = epochId(strcmp(baseStrings,uniqueCoeffs{i}))+1;
else
epochIndices{i} = epochId(strcmp(baseStrings,uniqueCoeffs{i}));
end
end
%
% for i=1:numEpochs
% epochIndices{i} = find(epochId==epochNums(i));
% epochLength(i) = length(epochIndices{i});
% end
coeffIndMat= nan(length(uniqueCoeffs),numEpochs);
labels = cell(size(coeffIndMat));
for i=1:length(uniqueCoeffs)
coeffIndMat(i,epochIndices{i}) = coeffStrIndex{i};
labels(i,epochIndices{i}) = fitObj.uniqueCovLabels(coeffStrIndex{i});
end
% for i=1:numEpochs
% coeffIndMat(1:epochLength(i),i) = coeffIndex(epochIndices{i});
% labels(1:epochLength(i),i) = fitObj.uniqueCovLabels(coeffIndMat(1:epochLength(i),i));
% end
if(length(fitNum)>1)
coeffMat = nan(size(coeffIndMat,1),size(coeffIndMat,2), length(fitNum));
SEMat = nan(size(coeffIndMat,1),size(coeffIndMat,2), length(fitNum));
for i=1:length(fitNum)
for j=1:length(uniqueCoeffs)
bTemp=fitObj.plotParams.bAct(coeffStrIndex{j},i);
seTemp=fitObj.plotParams.seAct(coeffStrIndex{j},i);
coeffMat(j,epochIndices{j},i) = bTemp';
SEMat(j,epochIndices{j},i) = seTemp';
end
end
else
coeffMat = nan(size(coeffIndMat,1),size(coeffIndMat,2));
SEMat = nan(size(coeffIndMat,1),size(coeffIndMat,2));
for j=1:length(uniqueCoeffs)
bTemp=fitObj.plotParams.bAct(coeffStrIndex{j},fitNum);
seTemp = fitObj.plotParams.seAct(coeffStrIndex{j},fitNum);
coeffMat(j,epochIndices{j}) = bTemp';
SEMat(j,epochIndices{j}) = seTemp';
end
end
end
function [histMat, labels, SEMat] = getHistCoeffs(fitObj,fitNum)
if(nargin<2 || isempty(fitNum))
fitNum =1:fitObj.numResults;
end
sortByEpoch = 0; % Make sure we have different time series if the history is divided into epochs;
[histIndex, epochId, numEpochs] = fitObj.getHistIndex(fitNum,sortByEpoch);
epochNums = unique(epochId);
histcoeffStrings = fitObj.uniqueCovLabels(histIndex);
baseStringEndIndex =regexp(histcoeffStrings,'_\{\d*\}','start');
baseStrings = cell(length(baseStringEndIndex),1);
for i=1:length(baseStringEndIndex)
if(~isempty(baseStringEndIndex{i}))
baseStrings{i} = histcoeffStrings{i}(1:baseStringEndIndex{i}-1);
else
baseStrings{i} = histcoeffStrings{i};
end
end
uniqueCoeffs = unique(baseStrings);
for i=1:length(uniqueCoeffs)
histcoeffStrIndex{i} = histIndex(strcmp(baseStrings,uniqueCoeffs{i}));
if(min(epochId)==0)
epochIndices{i} = epochId(strcmp(baseStrings,uniqueCoeffs{i}))+1;
else
epochIndices{i} = epochId(strcmp(baseStrings,uniqueCoeffs{i}));
end
end
%
% for i=1:numEpochs
% epochIndices{i} = find(epochId==epochNums(i));
% epochLength(i) = length(epochIndices{i});
% end
histcoeffIndMat= nan(length(uniqueCoeffs),numEpochs);
labels = cell(size(histcoeffIndMat));
% SEMat = nan(length(uniqueCoeffs),numEpochs);
for i=1:length(uniqueCoeffs)
histcoeffIndMat(i,epochIndices{i}) = histcoeffStrIndex{i};
labels(i,epochIndices{i}) = fitObj.uniqueCovLabels(histcoeffStrIndex{i});
% SEMat(i,epochIndices{i}) = fitObj.se;
end
% for i=1:numEpochs
% coeffIndMat(1:epochLength(i),i) = coeffIndex(epochIndices{i});
% labels(1:epochLength(i),i) = fitObj.uniqueCovLabels(coeffIndMat(1:epochLength(i),i));
% end
if(length(fitNum)>1)
histMat = nan(size(histcoeffIndMat,1),size(histcoeffIndMat,2), length(fitNum));
SEMat = nan(size(histcoeffIndMat,1),size(histcoeffIndMat,2), length(fitNum));
for i=fitNum
for j=1:length(uniqueCoeffs)
bTemp=fitObj.plotParams.bAct(histcoeffStrIndex{j},i);
seTemp = fitObj.plotParams.seAct(histcoeffStrIndex{j},i);
histMat(j,epochIndices{j},i) = bTemp';
SEMat(j,epochIndices{j},i) = seTemp';
end
end
else
histMat = nan(size(histcoeffIndMat,1),size(histcoeffIndMat,2));
SEMat = nan(size(histcoeffIndMat,1),size(histcoeffIndMat,2));
for j=1:length(uniqueCoeffs)
bTemp=fitObj.plotParams.bAct(histcoeffStrIndex{j},fitNum);
seTemp=fitObj.plotParams.seAct(histcoeffStrIndex{j},fitNum);
histMat(j,epochIndices{j}) = bTemp';
SEMat(j,epochIndices{j}) = seTemp';
end
end
%
% if(nargin<2 || isempty(fitNum))
% fitNum =1:fitObj.numResults;
% end
% sortByEpoch = 0; % Make sure we have different time series if the history is divided into epochs;
% [histIndex, epochId, numEpochs] = fitObj.getHistIndex(fitNum,sortByEpoch);
% epochNums = unique(epochId);
% for i=1:numEpochs
% epochIndices{i} = find(epochId==epochNums(i));
% epochLength(i) = length(epochIndices{i});
% end
%
% histIndMat= nan(max(epochLength),numEpochs);
% labels = cell(size(histIndMat));
% for i=1:numEpochs
% histIndMat(1:epochLength(i),i) = histIndex(epochIndices{i});
% labels(1:epochLength(i),i) = fitObj.uniqueCovLabels(histIndMat(1:epochLength(i),i));
% end
%
%
%
% if(length(fitNum)>1)
% histMat = nan(size(histIndMat,1),size(histIndMat,2), length(fitNum));
% for i=1:length(fitNum)
% for j=1:numEpochs
% bTemp=fitObj.plotParams.bAct(epochIndices{j},i);
% histMat(1:epochLength(j),j,i) = bTemp;
% end
% end
% else
% histMat = nan(size(histIndMat,1),size(histIndMat,2));
%
% for j=1:numEpochs
% bTemp=fitObj.plotParams.bAct(epochIndices{j},fitNum);
% histMat(1:epochLength(j),j) = bTemp;
% end
%
% end
end
function h=plotHistCoeffs(fitObj,fitNum,sortByEpoch,plotSignificance)
if(nargin<4 || isempty(plotSignificance))
plotSignificance=1;
end
if(nargin<3 || isempty(sortByEpoch))
sortByEpoch=0;
end