-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_utils.py
151 lines (109 loc) · 4.05 KB
/
plot_utils.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
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import os
"""
Python script for visualisation of GPR
Author: Hakim Amer
Date: May 15, 2023
"""
script_dir = os.path.dirname(os.path.abspath(__file__))
figs_dir = os.path.join(script_dir, 'figs')
root_dir = os.path.dirname(figs_dir)
def plot_samples(mu,cov,num_samples,x_s):
# Draw samples from the Gaussian Process
samples = np.random.multivariate_normal(mu, cov, size=num_samples)
for i in range(num_samples):
plt.plot(x_s, samples[i, :], alpha=0.7, label='Sample {}'.format(i + 1))
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
return
def plot_gp(y,x,x_s,mu,cov, mu_s, cov_s):
# Plotting the first figure with data, mean, and uncertainty
plt.figure(figsize=(10, 6))
plt.scatter(x, y, color='red', label='Data')
plt.scatter(x[len(x)-1]+1, mu, color='blue', label='predicted')
#plt.plot(x_s, mu, color='blue', label='Predicted Mean')
plt.plot(x_s, mu_s, color='blue', label='Predicted Mean')
plt.fill_between(
x_s,
mu - np.sqrt(np.diag(cov)),
mu + np.sqrt(np.diag(cov)),
color='gray',
alpha=0.4,
label='Uncertainty'
)
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.figure(figsize=(10, 6))
# Plotting the second figure with individual samples
num_samples = 6
plot_samples(mu,cov,num_samples,x_s)
# Display both figures
plt.show()
def plot_gp_dynamic(y_all,y, t_all,t,t_mu, x_s, mu, cov, mu_s, cov_s,i):
# Select the last 100 elements
plt.rcParams["figure.figsize"] = (18, 6)
# Clear the current figure
plt.clf()
# Plotting the figure with data, mean, and uncertainty
plt.scatter(t_all, y_all , color='red', label='Past Data',marker='o')
plt.scatter(t, y , color='green', label='Regression Data', marker='x')
plt.plot(t_mu, mu , color='blue', label='prediction')
plt.xlabel('x')
plt.ylabel('y')
# Set the legend outside the plot
plt.legend(loc='upper left', bbox_to_anchor=(1, 1))
# Update the x-axis limits to slide the view range
# Set the desired step size for sliding the x-axis range
step_size = 0.1
# Set the x-axis limits
plt.xlim(t_all[0]+i +300 , t_all[len(t_all)-1] + 1)
# Update the plot
plt.pause(0.0001) # Pause to allow the plot to update
def plot_gp_animation(y, x, x_s, mu, cov, mu_s, cov_s, filename, duration,iter,frames):
# Select the last 100 elements
last_100_mu = mu[-100:]
last_100_cov = cov[-100:]
last_100_mu_s = mu_s[-100:]
last_100_x_s = x_s[-100:]
last_100_cov_s = cov_s[-100:]
# Select the last 10 elements
last_100_y = y
last_100_x = x
# Clear the current figure
plt.clf()
# Plotting the figure with data, mean, and uncertainty
plt.scatter(last_100_x, last_100_y, color='red', label='Data')
plt.plot(last_100_x_s, last_100_mu_s, color='blue', label='Predicted Mean')
plt.fill_between(
last_100_x_s,
last_100_mu - np.sqrt(np.diag(last_100_cov)),
last_100_mu + np.sqrt(np.diag(last_100_cov)),
color='gray',
alpha=0.4,
label='Uncertainty'
)
plt.xlabel('x')
plt.ylabel('y')
# Set the legend outside the plot
plt.legend(loc='upper left')
# Update the x-axis limits to slide the view range
# Set the desired step size for sliding the x-axis range
step_size = 0.1
last_100_x_s = last_100_x_s[-100:] # Limit x_s to the last 100 points
x_min = max(last_100_x_s[-1] - 2 * np.pi, -2 * np.pi) # Set the minimum x-axis limit
x_max = last_100_x_s[-1] # Set the maximum x-axis limit
# Set the x-axis limits
plt.xlim(x_min, x_max + 0.02)
plt.ylim(-1.1, 1.5)
# Save the plot as an image
plt.savefig(os.path.join(figs_dir, 'animation_frame.png'))
# Open the saved image
image_path = os.path.join(figs_dir, 'animation_frame.png')
image = Image.open(image_path)
#image.show()
frames.append(image)
frames[0].save(filename, save_all=True, append_images=frames[1:], duration=duration*10, loop=0)