-
Notifications
You must be signed in to change notification settings - Fork 1
/
coherence.py
1438 lines (1206 loc) · 51.1 KB
/
coherence.py
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
# ================================================================================================ #
# Functions for calculating performance measures.
# Authors: Eddie Lee, [email protected]
# Ted Esposito
# ================================================================================================ #
import numpy as np
from scipy.signal import coherence
from sklearn import gaussian_process
from sklearn.gaussian_process.kernels import RBF,ConstantKernel
from warnings import warn
from gaussian_process.regressor import GaussianProcessRegressor
import multiprocess as mp
def precompute_coherence_nulls(v,t0,windowDuration,pool,
sampling_rate=30,n_iters=100):
"""
This is unnecessary! As noted in Tango NBIII 2017-10-30.
Calculate coherence values for trajectory with many samples of white noise.
This uses multiprocess to speed up calculation.
Parameters
----------
v : function
Given times, return signal.
t0 : ndarray
Times at which windows begin for calculating nulls.
windowDuration : float
Window duration in seconds
pool : multiprocess.Pool
Pool for parallelizing computation.
Returns
-------
One tuple for each x,y,z axis with
f : ndarray
Frequencies at which coherence was calculated
coh_mean : ndarray
Mean of coherence over random noise samples.
coh_std : ndarray
Std of coherence over random noise samples.
"""
import multiprocess as mp
def f(t0):
# Data to analyize.
t = t0+np.arange(windowDuration*sampling_rate)/sampling_rate
v_ = v(t)
# Coherence null values for each axis independently.
Cnullx,Cnully,Cnullz = [],[],[]
for i in range(n_iters):
fx,cwtcohx = cwt_coherence_auto_nskip(v_[:,0],np.random.normal(size=len(v_)),
sampling_period=1/sampling_rate,period_multiple=3)
fy,cwtcohy = cwt_coherence_auto_nskip(v_[:,1],np.random.normal(size=len(v_)),
sampling_period=1/sampling_rate,period_multiple=3)
fz,cwtcohz = cwt_coherence_auto_nskip(v_[:,2],np.random.normal(size=len(v_)),
sampling_period=1/sampling_rate,period_multiple=3)
Cnullx.append( cwtcohx )
Cnully.append( cwtcohy )
Cnullz.append( cwtcohz )
Cnullx = np.vstack(Cnullx)
Cnully = np.vstack(Cnully)
Cnullz = np.vstack(Cnullz)
mux,stdx = Cnullx.mean(0),Cnullx.std(0)
muy,stdy = Cnully.mean(0),Cnully.std(0)
muz,stdz = Cnullz.mean(0),Cnullz.std(0)
return fx,fy,fz,mux,muy,muz,stdx,stdy,stdz
fx,fy,fz,cohmux,cohmuy,cohmuz,cohstdx,cohstdy,cohstdz = list(zip(*pool.map(f,t0)))
return ( (fx[0],np.vstack(cohmux),np.vstack(cohstdx)),
(fy[0],np.vstack(cohmuy),np.vstack(cohstdy)),
(fz[0],np.vstack(cohmuz),np.vstack(cohstdz)) )
def check_coherence_with_null(ref,sample,threshold,
sampling_rate=30):
"""
Given subject's trajectory compare it with the given null and return the fraction of
frequencies at which the subject exceeds the white noise null which is just a flat cutoff.
Parameters
----------
ref : ndarray
Reference signal against which to compare the sample. This determines the noise
threshold.
sample : ndarray
Sample signal to compare against reference signal.
threshold : float
This determines the constant with which to multiply the power spectrum of the reference
signal to determine the null cutoff.
Returns
-------
performanceMetric : float
"""
assert 0<=threshold<=1
# Calculate coherence between given signals.
f,cwtcoh = cwt_coherence_auto_nskip(ref,sample,
sampling_period=1/sampling_rate,
period_multiple=3)
# Ignore nans
notnanix = np.isnan(cwtcoh)==0
betterPerfFreqIx = cwtcoh[notnanix]>threshold
return ( betterPerfFreqIx ).mean()
def phase_coherence(x,y):
"""
Parameters
----------
x : ndarray
y : ndarray
S : ndarray
Smoothing filter for 2d convolution.
Returns
-------
Phase coherence
"""
xcwt,f = pywt.cwt(x,np.logspace(0,2,100),'cgau1',sampling_period=1/60,precision=12)
ycwt,f = pywt.cwt(y,np.logspace(0,2,100),'cgau1',sampling_period=1/60,precision=12)
smoothx = np.abs(xcwt)
smoothy = np.abs(ycwt)
smoothxy = xcwt*ycwt.conjugate()
smoothcoh = smoothxy.mean(1) / ( smoothx*smoothy ).mean(1)
return f,smoothcoh
def tf_phase_coherence(x,y,S):
"""
Parameters
----------
x : ndarray
y : ndarray
S : ndarray
Smoothing filter for 2d convolution.
Returns
-------
"""
from scipy.signal import convolve2d
xcwt,f = pywt.cwt(x,np.logspace(0,2,100),'cgau1',sampling_period=1/60,precision=12)
ycwt,f = pywt.cwt(y,np.logspace(0,2,100),'cgau1',sampling_period=1/60,precision=12)
smoothx = convolve2d(np.abs(xcwt)**2,S,mode='same')
smoothy = convolve2d(np.abs(ycwt)**2,S,mode='same')
smoothxy = convolve2d(xcwt*ycwt.conjugate(),S,mode='same')
smoothcoh = smoothxy.mean(1) / np.sqrt(( smoothx*smoothy ).mean(1))
return f,smoothcoh
def tf_coherence(x,y,S):
"""
Parameters
----------
x : ndarray
y : ndarray
S : ndarray
Smoothing filter for 2d convolution.
"""
from scipy.signal import convolve2d
xcwt,f = pywt.cwt(x,np.logspace(0,2,100),'cgau1',sampling_period=1/60,precision=12)
ycwt,f = pywt.cwt(y,np.logspace(0,2,100),'cgau1',sampling_period=1/60,precision=12)
smoothx = convolve2d(np.abs(xcwt)**2,S,mode='same')
smoothy = convolve2d(np.abs(ycwt)**2,S,mode='same')
smoothxy = convolve2d(xcwt*ycwt.conjugate(),S,mode='same')
smoothcoh = smoothxy/np.sqrt(smoothx*smoothy)
return f,smoothcoh
def coherence_before_vis(subcwt,avcwt,f,vis,dt,min_freq=0,max_freq=10):
"""
Coherence using the wavelet transform for dt seconds around the visibility turning back on.
Parameters
----------
subcwt
avcwt
f : ndarray
Frequencies.
vis
dt : float
temporal distance from the start of a new visibility section. Positive value is for
before visibility starts.
min_freq : float,0
max_freq : float,10
Returns
-------
Average coherence between (min_freq,max_freq).
"""
# Get indices of points near the end of the invisibility window.
dtprev = int(dt*60)
visStartIx = np.where(np.diff(vis)==1)[0]-dtprev
visStartIx = visStartIx[(visStartIx>=0)&(visStartIx<len(vis))]
Psub = ( np.abs(subcwt[:,visStartIx])**2 ).mean(-1)
Pav = ( np.abs(avcwt[:,visStartIx])**2 ).mean(-1)
Pcross = ( subcwt[:,visStartIx]*avcwt[:,visStartIx].conjugate() ).mean(-1)
coh = np.abs(Pcross)**2/Psub/Pav
freqix = (f>=min_freq)&(f<=max_freq)
# Errors.
#print ( Psub/( np.abs(subcwt[:,visStartIx])**2 ).std(-1) )[freqix]
#print ( Pav/( np.abs(avcwt[:,visStartIx])**2 ).std(-1) )[freqix]
#print ( np.abs(Pcross)**2/(np.abs( subcwt[:,visStartIx]*avcwt[:,visStartIx].conjugate()
# )**2).std(-1) )[freqix]
avgC = np.trapz(coh[freqix],x=f[freqix]) / (f[freqix].max()-f[freqix].min())
if avgC<0:
return -avgC
return avgC
def cwt_coherence(x,y,nskip,
scale=np.logspace(0,2,100),
sampling_period=1/60,
**kwargs):
"""
Use the continuous wavelet transform to measure coherence.
Parameters
----------
x : ndarray
y : ndarray
nskip : int
Number of indices to skip when averaging across spectra for coherence. This is to reduce
correlation between samples when averaging.
scale : list
Scale of continuous wavelets.
sampling_period : float,1/60
Used to choose scales.
**kwargs
for pywt.cwt()
Returns
-------
f : ndarray
coherence : ndarray
"""
import pywt
assert len(x)==len(y)
xcwt,f = pywt.cwt(x,scale,'cgau1',sampling_period=sampling_period,**kwargs)
ycwt,f = pywt.cwt(y,scale,'cgau1',sampling_period=sampling_period,**kwargs)
# Get indices of points with some overlap.
selectix = np.arange(nskip,len(x),nskip,dtype=int)
Psub = ( np.abs(xcwt[:,selectix])**2 ).mean(-1)
Pav = ( np.abs(ycwt[:,selectix])**2 ).mean(-1)
Pcross = ( xcwt[:,selectix]*ycwt[:,selectix].conjugate() ).mean(-1)
coh = np.abs(Pcross)**2/Psub/Pav
# Skip low frequencies that have periods longer the duration of the window.
fCutoff = f<(1/(len(x)*sampling_period))
return f[f>=fCutoff],coh[f>=fCutoff]
def cwt_coherence_auto_nskip(x,y,
scale=np.logspace(0,2,100),
sampling_period=1/60,
period_multiple=1,
**kwargs):
"""
Use the continuous wavelet transform to measure coherence but automatically choose the amount to
subsample separately for each frequency when averaging. The subsampling is determined by nskip
which only takes a sample every period of the relevant frequency.
Parameters
----------
x : ndarray
y : ndarray
nskip : int
Number of indices to skip when averaging across spectra for coherence. This is to reduce
correlation between samples when averaging.
scale : list
Scale of continuous wavelets.
sampling_period : float,1/60
Used to choose scales.
**kwargs
for pywt.cwt()
Returns
-------
f : ndarray
coherence : ndarray
"""
import pywt
assert len(x)==len(y)
xcwt,f = pywt.cwt(x,scale,'cgau1',sampling_period=sampling_period,**kwargs)
ycwt,f = pywt.cwt(y,scale,'cgau1',sampling_period=sampling_period,**kwargs)
Psub = np.zeros(len(f))
Pav = np.zeros(len(f))
Pcross = np.zeros(len(f),dtype=complex)
#PsubStd = np.zeros(len(f))
#PavStd = np.zeros(len(f))
#PcrossStd = np.zeros(len(f))
# For each freq, skip roughly by a period.
for fIx,f_ in enumerate(f):
nskip = int(1/f_/sampling_period)
if nskip>(len(x)//3):
Psub[fIx] = np.nan
Pav[fIx] = np.nan
Pcross[fIx] = np.nan
else:
selectix = np.arange(nskip,len(x),nskip,dtype=int)
#print f_,len(selectix)
Psub[fIx] = ( np.abs(xcwt[fIx,selectix])**2 ).mean(-1)
Pav[fIx] = ( np.abs(ycwt[fIx,selectix])**2 ).mean(-1)
Pcross[fIx] = ( xcwt[fIx,selectix]*ycwt[fIx,selectix].conjugate() ).mean(-1)
#PsubStd[fIx] = ( np.abs(xcwt[fIx,selectix])**2 ).std(-1)
#PavStd[fIx] = ( np.abs(ycwt[fIx,selectix])**2 ).std(-1)
#PcrossStd[fIx] = ( xcwt[fIx,selectix]*ycwt[fIx,selectix].conjugate() ).std(-1)
coh = np.abs(Pcross)**2/Psub/Pav
#stds = (PsubStd,PavStd,PcrossStd)
# Skip low frequencies that have periods longer the duration of the window.
fCutoffIx = f>(period_multiple/(len(x)*sampling_period))
return f[fCutoffIx],coh[fCutoffIx]
def max_coh_time_shift(subv,temv,
dtgrid=np.linspace(0,1,100),
mx_freq=10,
sampling_rate=60,
window_width=2,
disp=False,
ax=None):
"""
Find the global time shift that maximizes the coherence between two signals.
Parameters
----------
subv : ndarray
Subject time series. If multiple cols, each col is taken to be a data point and the average
coherence is maximized.
temv : ndarray
Template time series.
dtgrid : ndarray,np.linspace(0,1,100)
window_width : float,2
Window duration for computing coherence in terms of seconds.
disp : bool,False
ax : AxesSubplot,None
Returns
-------
dt : float
Time shift in seconds that maximizes scipy coherence. Time shift is relative to subject
time, i.e. negative shift is shifting subject back in time and positive is shifting subject
forwards in time. If subject is tracking template, then dt>0.
maxcoh : float
Coherence max.
"""
from scipy.signal import coherence
# Convert dtgrid to index shifts.
dtgrid = np.unique(np.around(dtgrid*sampling_rate).astype(int))
if subv.ndim==1:
coh = np.zeros(len(dtgrid))
else:
coh = np.zeros((len(dtgrid),subv.shape[1]))
window_width = int(sampling_rate*window_width)
def _calc_coh(subv,temv):
for i,dt in enumerate(dtgrid):
if dt<0:
f,c = coherence(subv[-dt:],temv[:dt],fs=sampling_rate,nperseg=window_width)
elif dt>0:
f,c = coherence(subv[:-dt],temv[dt:],fs=sampling_rate,nperseg=window_width)
else:
f,c = coherence(subv,temv,fs=sampling_rate,nperseg=window_width)
coh[i] = abs(c)[f<mx_freq].mean()
return coh
if subv.ndim==1:
coh = _calc_coh(subv,temv)
else:
coh = np.vstack([_calc_coh(subv[:,i],temv[:,i]) for i in range(subv.shape[1])]).mean(1)
shiftix = np.argmax(coh)
if disp:
if ax is None:
fig,ax = plt.subplots()
ax.plot(dtgrid/sampling_rate,coh,'o')
ax.set(xlabel='dt',ylabel='coherence')
return dtgrid[shiftix]/sampling_rate,coh[shiftix]
# ======= #
# Classes #
# ======= #
class DTWPerformance(object):
def __init__(self,inner_prod_threshold=.3,
norm_dv_threshold=.1,
norm_dv_ratio_threshold=np.log2(1.5),
dt_threshold=.5,
dwt_kwargs={'dist':2}):
"""
Class for using fast DWT to compare two temporal trajectories and return a performance metric.
Performance is the fraction of time warped trajectories that the individuals remain within some
threshold preset.
Parameters
----------
inner_prod_threshold : float,.5
Max deviation allowed for one minus normalized dot product between vectors.
norm_dv_threshold : float,.1
Max difference in speeds allowed. Units of m/s.
norm_dv_ratio_threshold : float,1
Max ratio in speeds allowed in units of log2.
dt_threshold : float,.5
Max deviation for time allowed.
"""
self.dwtSettings = dwt_kwargs
self.innerThreshold = inner_prod_threshold
self.normdvThreshold = norm_dv_threshold
self.normdvRatioThreshold = norm_dv_ratio_threshold
self.dtThreshold = dt_threshold
def time_average(self,x,y,dt=1.,strict=False,bds=[0,np.inf]):
"""
Measure performance as the fraction of time you are within the thresholds.
Parameters
----------
x : ndarray
y : ndarray
dt : float,1
Sampling rate for x and y.
bds : list,[0,inf]
Lower and upper bounds for times at which to truncate the data according to x before calculating
performance.
Returns
-------
performance : float
Fraction of the length of given trajectories that are within set thresholds.
"""
from numpy.linalg import norm
from fastdtw import fastdtw
from warnings import warn
dist,path = fastdtw(x,y,**self.dwtSettings)
try:
path = np.vstack(path)
except ValueError:
warn("fastdtw could not align. Possible because subject data is flat.")
path=list(range(len(x)))
keepIx=((path[:,0]*dt)>=bds[0])&((path[:,0]*dt)<=bds[1])
normx = norm(x[path[:,0]],axis=1)+np.nextafter(0,1)
normy = norm(y[path[:,1]],axis=1)+np.nextafter(0,1)
# Dot product between the two vectors.
inner = (x[path[:,0]]*y[path[:,1]]).sum(1) / normx / normy
# Relative norms.
normDiff = np.abs(normx-normy)
normRatio = np.abs(np.log2(normx)-np.log2(normy))
dt = np.diff(path,axis=1) * dt
# Calculate performance metric.
# In strict case, dt must be within cutoff at all times to get a nonzero performance value.
# Otherwise, we just take the average time during which subject is within all three norm, inner
# product, and dt cutoffs.
if strict:
if (np.abs(dt)<self.dtThreshold).all():
performance = ((1-inner)<self.innerThreshold)[keepIx].mean()
else:
performance = 0.
else:
performance = ( #((normDiff<self.normdvThreshold)|(normRatio<self.normdvRatioThreshold)) &
((1-inner)<self.innerThreshold) &
(np.abs(dt)<self.dtThreshold) )[keepIx].mean()
return performance
def time_average_binary(self,x,y,dt=1.,bds=[0,np.inf],path=None):
"""
Measure performance as the fraction of time you are within the thresholds using the two
Success and Failure states identified in the paper. Use Laplace counting to regularize the
values.
Parameters
----------
x : ndarray
y : ndarray
dt : float,1
Sampling rate for x and y.
bds : list,[0,inf]
Lower and upper bounds for times at which to truncate the data according to x before calculating
performance.
Returns
-------
performance : float
Fraction of the length of given trajectories that are within set thresholds.
"""
from numpy.linalg import norm
from fastdtw import fastdtw
from warnings import warn
if path is None:
dist,path = fastdtw(x,y,**self.dwtSettings)
try:
path = np.vstack(path)
except ValueError:
warn("fastdtw could not align. Possible because subject data is flat.")
path=list(range(len(x)))
keepIx=((path[:,0]*dt)>=bds[0])&((path[:,0]*dt)<=bds[1])
dt = np.diff(path,axis=1) * dt
# Calculate performance metric.
return ( ((np.abs(dt)<self.dtThreshold))[keepIx].sum()+1 ) / (keepIx.sum()+2)
def raw(self,x,y,dt=1.):
"""
Performance as measured by the similarity of time warped trajectories. If time warping is too big,
then performance is 0.
Parameters
----------
x : ndarray
y : ndarray
dt : float,1
Sampling rate for x and y.
Returns
-------
performance : float
"""
from numpy.linalg import norm
from fastdtw import fastdtw
dist,path = fastdtw(x,y,**self.dwtSettings)
path = np.vstack(path)
# Calculate dot product between the two vectors.
inner = ( (x[path[:,0]]*y[path[:,1]]).sum(1) /
(norm(x[path[:,0]],axis=1)*norm(y[path[:,1]],axis=1)+np.nextafter(0,1)) )
dt = np.diff(path,axis=1) * dt
# Calculate performance metric.
if (np.abs(dt)<self.dtThreshold).all():
performance = inner.mean()
if performance<0:
performance = 0
else:
performance = 0.
return performance
#end DTWPerformance
class CoherenceEvaluator(object):
'''
update() evaluates the average coherence over the given time.
These assume V_person and V_avatar are pre-aligned and have the same length.
'''
def __init__(self,maxfreq,sample_freq=60,window_length=90):
'''
Parameters
----------
maxfreq : float
Max frequency up to which to average coherence.
sampleFreq : float,60
Sampling frequency
windowLength : int,90
Number ofdata points to use in window for coherence calculation.
Subfields
---------
coherence
coherences
'''
self.maxfreq = maxfreq
self.sampleFreq = sample_freq
self.windowLength = window_length
self.v = None
self.v_av = None
self.coherence = 0
self.coherences = np.empty(0)
self.performanceValues = np.empty(0)
def getCoherence(self):
return self.coherence
def getAverageCoherence(self):
'''
For GPR: returns average coherence over a full trial. Coherences should
then be reset for the new trial.
'''
return np.mean(self.coherences)
def resetCoherences(self):
self.coherences = np.empty(0)
def getPerformanceValues(self):
return self.performanceValues
def getAveragePerformance(self):
return np.mean(self.performanceValues)
def resetPerformance(self):
self.performanceValues = np.empty(0)
def evaluateCoherence(self,v1,v2,use_cwt=True):
'''
Returns average coherence between current v and v_av data vectors.
Parameters
----------
v1 : ndarray
Vector.
v2 : ndarray
Vector.
Returns
-------
avg_coh : float
'''
assert len(v1)==len(v2)
if not use_cwt:
# Scipy.signal's coherence implementation.
self.f,self.C = coherence(v1,v2,
fs=self.sampleFreq,
nperseg=self.windowLength,
nfft=2 * self.windowLength,
noverlap=self.windowLength//4)
else:
self.f,self.C = cwt_coherence(v1,v2,1,sampling_period=1/self.sampleFreq)
self.C *= -1
# Evaluate Average Coherence by the Trapezoid rule.
freqIx = (self.f>0)&(self.f<self.maxfreq)
avg_coherence = np.trapz(self.C[freqIx],x=self.f[freqIx]) / (self.f[freqIx].max()-self.f[freqIx].min())
if np.isnan(avg_coherence): avg_coherence = 0.
return avg_coherence
def evaluatePerformance(self):
'''
Evaluates average coherence against threshold value, and writes binary
value to target output file.
Returns
-------
performance
'''
performance = 0
avg_coherence = self.evaluateCoherence()
if avg_coherence >= self.performanceThreshold:
performance = 1
self.coherences = np.append(self.coherences,avg_coherence)
self.performanceValues = np.append(self.performanceValues,performance)
return performance
# end CoherenceEvaluator
class GPR(object):
def __init__(self,
alpha = .2,
mean_performance=np.log(1),
theta=.5,
length_scale=np.array([1.,.2]),
tmin=0.5,tmax=2,tstep=0.1,
fmin=0.1,fmax=1.,fstep=0.1):
'''
Wrapper around GPR class to perform useful functions for HandSyncExperiment.
Parameters
----------
alpha : float
Uncertainty in diagonal matrix for GPR kernel.
mean_performance : float,.5
By default, the sigmoid is centered around 0, the mean of the Gaussian process, corresponding to
perf=0.5. However, we should center the sigmoid around the mean value of y which is specified
here. Since the GPR is trained in the logistic space, the offset is given by the logistic offset.
The mean is automatically accounted for under the hood, so you don't have to worry about adding or
subtracting it in the interface.
theta : float
Coefficient for kernel.
length_scale : ndarray
(duration_scale,fraction_scale) Remember that duration is the angle about the origin restricted to
be between [0,pi] and fraction is the radius. The GPR learns once (r,theta) has been mapped to the
Cartesian coordinate system.
tmin : float,0.5
minimum window time
tmax : float,2
tstep : float,0.1
fmin : float
minimum visibility fraction.
fmax : float
fstep : float
Members
-------
fractions : ndarray
Fraction of time stimulus is visible.
durations : ndarray
Duration of window.
meshPoints : ndarray
List of grid points (duration,fraction) over which performance was measured.
performanceData : ndarray
List of performance data points that have been used to update GPR.
performanceGrid : ndarray
Unraveled grid of performance predictions on mesh of duration and fraction.
'''
assert (type(length_scale) is np.ndarray) and len(length_scale)==2
self.tmin = tmin
self.tmax = tmax
self.tstep = tstep
self.fmin = fmin
self.fmax = fmax
self.fstep = fstep
self.length_scale = length_scale
self.kernel = self.handsync_experiment_kernel(length_scale,theta)
self.alpha = alpha
self.theta = theta
self.durations = np.zeros(0)
self.fractions = np.zeros(0)
self.performanceData = np.zeros(0)
self.mean_performance = mean_performance
# Create two grids for t and f.
self.tRange = np.arange(self.tmin,self.tmax+self.tstep/2,self.tstep)
self.fRange = np.arange(self.fmin,self.fmax+self.fstep/2,self.fstep)
self.meshPoints = np.meshgrid(self.tRange,self.fRange)
# Flatten t and f grids and stack them into an Nx2 array.
self.meshPoints = np.vstack([x.ravel() for x in self.meshPoints]).T
self.gp = GaussianProcessRegressor(self.kernel,alpha**-2)
self.performanceGrid = 0 # [-inf,inf]
self.std_pred = 0
self.pointsToAvoid = []
def predict(self,mesh=None):
"""
Fits the GPR to all data points and saves the predicted values with errors. The mean in the target
perf values is accounted for here.
Updates self.performanceGrid and self.std_pred with the latest prediction.
If you want to just query the model, you should access self.gp directly.
Parameters
----------
mesh : ndarray,None
Points at which to evaluate GPR. Should be (samples,2) with first column durations and the second
fraction of visible window.
Returns
-------
perf : ndarray
Performance grid.
perfErr : ndarray
Performance estimated standard deviation.
"""
if mesh is None:
mesh = self.meshPoints
self.gp.fit( np.vstack((self.durations,self.fractions)).T,self.performanceData-self.mean_performance )
self.performanceGrid, self.std_pred = self.gp.predict(mesh,return_std=True)
self.performanceGrid += self.mean_performance
return self.performanceGrid.copy(),self.std_pred.copy()
def grad(self,eps=1e-5):
'''
Estimates the gradient at each point of the mesh.
Parameters
----------
eps : float,1e-5
Returns
-------
grad : ndarray
Dimensions (n_tRange,n_fRange,2). Last dimension corresponds to the gradient along each diemnsion
of the input.
'''
grad = np.zeros((len(self.meshPoints),2))
X1 = self.meshPoints.copy()
X0 = self.meshPoints.copy()
X1[:,0] += eps
X0[:,0] -= eps
grad[:,0] = ( self.gp.predict(X1)-self.gp.predict(X0) )/(2*eps)
X1[:,0] -= eps
X0[:,0] += eps
X1[:,1] += eps
X0[:,1] -= eps
grad[:,1] = ( self.gp.predict(X1)-self.gp.predict(X0) )/(2*eps)
shape = len(self.fRange),len(self.tRange)
grad = np.concatenate((grad[:,0].reshape(shape)[:,:,None],grad[:,1].reshape(shape)[:,:,None]),2)
return grad
def max_uncertainty(self,explore_new=True):
"""
Returns next_duration,next_fraction as the point where the variance of the GPR is max
Currently finds maximum uncertainty, and then returns a point with that uncertainty as the
update value.
Avoids values that have already been measured and fraction=1.
Parameters
----------
explore_new : bool,True
Returns
-------
next_window_duration : float
next_vis_fraction : float
"""
sortIx=np.argsort(self.std_pred)[::-1]
if explore_new:
next_fraction=1
counter=0
while next_fraction==1 or ((next_duration in self.durations) and (next_fraction in self.fractions)):
next_duration=self.meshPoints[sortIx[counter]][0]
next_fraction=self.meshPoints[sortIx[counter]][1]
counter+=1
else:
next_fraction=1
counter=0
while next_fraction==1:
next_duration=self.meshPoints[sortIx[counter]][0]
next_fraction=self.meshPoints[sortIx[counter]][1]
counter+=1
return next_duration,next_fraction
def select_contour(self,pstar,choose_algo='',algo_kwargs={}):
"""
Select the point in the field with mean closest to desired performance value in the [0,1] space.
Option to avoid points so that we don't pick the same points over and over again.
Parameters
----------
pstar : ndarray
Performance value around which to choose points. This is in the [-inf,inf] stretched space.
choose_algo : str,''
If 'avoid', do not sample points in self.pointsToAvoid. Typically, this will be a stored list of
points already sampled.
If 'err', weight choice by the uncertainty. Choose the point that minimize the distance to pstar
with max error. First, thresholds points by distance to pstar. Must specify 'threshold' and
'std_scale'.
algo_kwargs : dict,{}
Any keyword args needed by algorithm for choosing the next point.
Returns
-------
duration : float
fraction : float
"""
if type(pstar) is int:
pstar = float(pstar)
if type(pstar) is float:
pstar = np.array([pstar])
if choose_algo=='avoid':
ix = []
for pstar_ in pstar:
sortix = np.argsort( np.abs(self.logistic(self.performanceGrid)-pstar_) )
counter = 0
while counter<len(sortix):
if not any(np.array_equal(self.meshPoints[sortix[counter]],x)
for x in self.pointsToAvoid):
ix.append(sortix[counter])
counter = len(sortix)
counter += 1
elif choose_algo=='err':
assert not algo_kwargs.get('threshold',None) is None
assert not algo_kwargs.get('std_scale',None) is None
ix = []
for pstar_ in pstar:
dist = np.abs(self.logistic(self.performanceGrid)-pstar_)
thresholdIx = dist<algo_kwargs['threshold']
dist[thresholdIx==0] += 1e30
ix.append( np.argmin(dist-algo_kwargs['std_scale']*self.std_pred) )
else:
ix = [np.argmin(np.abs(self.performanceGrid-pstar_)) for pstar_ in pstar]
return self.meshPoints[ix,0],self.meshPoints[ix,1]
def update(self,new_performance,window_dur,vis_fraction):
'''
This is called to add new data point to prediction.
Parameters
----------
new_performance : float
window_dur : float
vis_fraction : float
'''
self.performanceData = np.append(self.performanceData,new_performance)
self.durations = np.append(self.durations,window_dur)
self.fractions = np.append(self.fractions,vis_fraction)
self.predict()
def _search_hyperparams(self,initial_guess,n_restarts=1,min_alpha_bound=1e-3):
"""Find the hyperparameters that maximize the log likelihood of the data.
This doesn't seem well-behaved with the length_scale parameters so those are not optimized.
Parameters
----------
n_restarts : int,1
min_alpha_bound : float,1e-3
"""
from scipy.optimize import minimize
def train_new_gpr(params):
length_scale=params[:2]
alpha=params[2]
mean_performance=params[3]
theta=params[4]
kernel=self.handsync_experiment_kernel(length_scale,theta)
gp=GaussianProcessRegressor(kernel,alpha**-2)
gp.fit( np.vstack((self.durations,self.fractions)).T,
self.performanceData-mean_performance )
return gp
def f(params):
"""First parameter is std, second is mean, third is coefficient for kernel."""
assert len(params)==3
if params[0]<min_alpha_bound:
return 1e30
if params[2]<=0:
return 1e30
params=np.concatenate((self.length_scale,params))
gp=train_new_gpr(params)
return -gp.log_likelihood()
soln=[]
soln.append( minimize(f,initial_guess) )
for i in range(1,n_restarts):
initial_guess=np.array([np.random.exponential(),np.random.normal(),np.random.exponential()])
soln.append( minimize(f,initial_guess) )
if len(soln)>1:
minLikIx=np.argmin([s['fun'] for s in soln])
soln=[soln[minLikIx]]
return soln[0]
def optimize_hyperparams(self,initial_guess=None,verbose=False):
"""Find the hyperparameters that optimize the log likelihood and reset the kernel and the
GPR landscape.
Parameters
----------
initial_guess : ndarray
(alpha,mean,theta)
verbose : bool,False
"""
from datetime import datetime
if initial_guess is None:
initial_guess=np.array([self.alpha,self.mean_performance,self.theta])
else:
assert len(initial_guess)==3
t0=datetime.now()
soln=self._search_hyperparams(initial_guess)
if verbose:
print("Optimal hyperparameters are\nalpha=%1.2f, mu=%1.2f"%(soln['x'][0],soln['x'][1]))