-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPTester.py
More file actions
223 lines (179 loc) · 5.54 KB
/
GPTester.py
File metadata and controls
223 lines (179 loc) · 5.54 KB
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
import KenGP as gp
import numpy as np
import pandas as pd
import numpy.linalg as la
from scipy.integrate import odeint
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
# Rossler System
def Rossler(xi, t):
a = 0.2
b = 0.2
c = 5.7
(x,y,z) = xi
dx = -y - z
dy = x + a * y
dz = b + z * ( x - c )
return np.array( [dx,dy,dz] )
def Lorenz(xi,t):
rho = 25.0
sigma = 10.0
beta = 8.0 / 3.0
(x,y,z) = xi
return sigma * (y - x), x * (rho - z) - y, x * y - beta * z # Derivatives
def LorenzP(xi,t, rho, sigma, beta):
(x,y,z) = xi
return sigma * (y - x), x * (rho - z) - y, x * y - beta * z # Derivatives
def Lorenz96(x, t):
N = 5 # dimension
F = 8
# Setting up vector
d = np.zeros(N)
# Loops over indices (with operations and Python underflow indexing handling edge cases)
for i in range(N):
d[i] = (x[(i + 1) % N] - x[i - 2]) * x[i - 1] - x[i] + F
return d
begin = 0
end = 40
step = 0.25
tlen = int((end-begin)/step)
trainToTest = 0.8 # between 0 and 1
t = np.arange(begin, end, step)
# MAKE SURE TO UPDATE THE DIMENSION WHEN SWITCHING ATTRACTORS
dim = 3
t0 = np.zeros(dim) * 2
t0[0] += 0.1
# STATIONARY SIMULATION VERSION: UPDATE ATTRACTOR YOU WANT HERE
# \/\/\/\/
states = odeint(Lorenz,t0,t)
# END STATIONARY SIMULATION
# FROM DATA
"""
file = "paramecium_didinium - cleaned.csv"
data = pd.read_csv(file,encoding="utf-8",na_filter=False)
states = data.to_numpy()
print(states)
"""
# END FROM DATA
# NON STATIONARY VERSION
"""
rho = 28.0
sigma = 10.0
beta = 8.0 / 3.0
deltaP = 0.01
states = np.zeros((tlen,3))
states[0] = t0
for i in range(1, tlen ):
states[i] = odeint(LorenzP,states[i-1],np.array([0,step]),args=(rho,sigma,beta))[1,:]
sigma += deltaP
"""
# END NON STATIONARY
# Normalize, split data
states = (states - states.mean(0)) / states.std(0) # normalize
testTrainSplit = int(states.shape[0] * trainToTest)
X = states[:-1,]
Y = states[1:,]
# Print Input
fig2 = plt.figure(2)
ax2 = fig2.gca(projection="3d")
if dim == 2:
ax2 = plt.subplot()
ax2.plot(X[:,0],X[:,1])
else:
ax2.plot(X[:,0],X[:,1],X[:,2])
print(states)
# Xt = np.column_stack((X[testTrainSplit:,], X[testTrainSplit-1:-1,1]))
Xt = X[testTrainSplit:,]
Yt = Y[testTrainSplit:,]
X = X[:testTrainSplit,]
Y = Y[:testTrainSplit,]
print(Xt)
gp.setCovar("sqrexpf")
gp.setPrior(1,"half-normal")
gp.setPrior(2,"half-normal")
gp.setData(X,Y)
gp.setTimeDelayInterval(1)
# gp.setDelayEmbedding([0,1,0])
# optimize them parameters
gp.hyperParamOptimize()
diffTable = []
predictions = np.zeros((len(Xt),dim))
for i in range(len(Xt)):
prediction = gp.predict(Xt[i])[0]
predictions[i] = prediction # list of one step prediction values for graphing
diff = la.norm(Yt[i] - prediction) # norm error for 1 step
diffTable.append(diff)
print("Predictions ", predictions)
print("DiffTable ", diffTable)
diffTable = np.array(diffTable)
# feed forward prediction
n = 200 # num steps
feedForwardPrediction = np.zeros(((n+1,dim)))
feedForwardPrediction[0] = X[0]
for i in range(n):
feedForwardPrediction[i+1] = gp.predict(feedForwardPrediction[i])[0]
print(predictions)
fig0 = plt.figure(0)
if dim == 2:
ax0 = plt.subplot()
ax0.plot(Yt[:,0],Yt[:,1],"b") # states
ax0.plot(predictions[:,0],predictions[:,1],'--r') # map of 1 step ahead predictions
else:
ax0 = fig0.gca(projection="3d")
ax0.plot(Yt[:,0],Yt[:,1],Yt[:,2],"b") # states
ax0.plot(predictions[:,0],predictions[:,1],predictions[:,2],'--r') # map of 1 step ahead predictions
# ax0.plot(feedForwardPrediction[:,0],feedForwardPrediction[:,1],feedForwardPrediction[:,2],'--g') # map of 50 step ahead prediction
fig1 = plt.figure(1)
plt.hist(diffTable)
"""
# user interaction stuff
fig3 = plt.figure(3)
sliderAx = plt.axes()
slider = Slider(sliderAx,"Tao", 0, 50, valinit=0, valstep=0.1)
fig4 = plt.figure(4)
paramAx = plt.axes()
paramButtons = RadioButtons(paramAx, ("rho","sigma","beta"), active = 0)
def update(val):
global rho, sigma, beta
if (paramButtons.value_selected == "rho"):
rho = slider.val
elif (paramButtons.value_selected == "sigma"):
sigma = slider.val
elif (paramButtons.value_selected == "beta"):
beta = slider.val
# Line Plot Update
s = odeint(LorenzP,t0,t, args=(rho,sigma,beta))
ax2.clear()
ax2.plot(s[:,0], s[:,1], s[:,2])
# Quiver Update
mi = np.nanmin(s,axis=0)
ma = np.nanmax(s,axis=0)
st = abs(mi - ma) / 5
print(st)
x, y, z = np.meshgrid(np.arange(mi[0],ma[0],st[0]), np.arange(mi[1],ma[1],st[1]),np.arange(mi[2],ma[2],st[2]))
u, v, w = (sigma * (y - x), x * (rho - z) - y, x * y - beta * z)
ax2.quiver(x,y,z,u,v,w,length=5,normalize=True, color = "r", alpha = 0.25)
fig2.canvas.draw()
fig2.canvas.flush_events()
def update2(val):
global rho, sigma, beta
if (paramButtons.value_selected == "rho"):
slider.set_val(rho)
elif (paramButtons.value_selected == "sigma"):
slider.set_val(sigma)
elif (paramButtons.value_selected == "beta"):
slider.set_val(beta)
slider.on_changed(update)
paramButtons.on_clicked(update2)
# Quiver Plot
fig2 = plt.figure(2)
ax2 = fig2.gca(projection="3d")
mi = np.nanmin(X,axis=0)
ma = np.nanmax(X,axis=0)
st = abs(mi - ma) / 5
print(st)
x, y, z = np.meshgrid(np.arange(mi[0],ma[0],st[0]), np.arange(mi[1],ma[1],st[1]),np.arange(mi[2],ma[2],st[2]))
u, v, w = (sigma * (y - x), x * (rho - z) - y, x * y - beta * z)
ax2.quiver(x,y,z,u,v,w,length=st[0],normalize=True, color = "r", alpha=0.25)
"""
plt.show()