-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotting.py
191 lines (170 loc) · 6.36 KB
/
plotting.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
import numpy as np
import matplotlib.pyplot as plt
import math
import os
import tensorflow as tf
from keras import Model, Input
import keras
# current working directory
if(os.getcwd()[-1] == '/'):
cwd = os.getcwd()
else:
cwd = os.getcwd() + '/'
DATA_PATH = ''
IMG_PATH = cwd + 'plots'
MODEL_PATH = cwd + 'models/'
# plot the distribution of the x-y coordinates of the target variable
# file a .npy input file, title the tile of the plot
def plot_target_distribution(file, title, save_name):
y = np.load(file)
print(y.shape)
plt.figure()
plt.scatter(y[:,0], y[:,1], alpha=0.2)
plt.xlabel('x-coordinate')
plt.ylabel('y-coordinate')
plt.title(title)
plt.savefig(save_name, dpi=200)
# plot the distribution of angles
def plot_ang_distribution(file, save_name, bins):
angs = np.load(file)
print(angs.shape)
plt.figure()
plt.hist(angs, bins=bins)
plt.xlabel('Angles (normalized radians)')
plt.ylabel('Counts')
plt.savefig(save_name, dpi=200)
# plot the distribution of velocities
def plot_vel_distribution(file, save_name, bins):
vels = np.load(file)
print(vels.shape)
plt.figure()
plt.hist(vels, bins=bins)
plt.xlabel('Velocities')
plt.ylabel('Counts')
plt.savefig(save_name, dpi=200)
def coor_to_angle_vel(y_file, vel_name, ang_name):
y = np.load(y_file)
vel = np.sqrt(np.sum(y**2, axis=1))
# normalize the radians between -1 and 1
ang = np.arctan2(y[:,1], y[:,0]) / math.pi
print(vel.shape, ang.shape)
np.save(vel_name, vel)
np.save(ang_name, ang)
def plot_xy_hists(y_file, bins):
y = np.load(y_file)
plt.figure(figsize=(10, 4))
plt.subplot(121)
plt.hist(y[:,0], bins=bins)
plt.xlabel('x-coordinate')
plt.ylabel('counts')
plt.subplot(122)
plt.hist(y[:,1], bins=bins)
plt.xlabel('y-coordinate')
plt.ylabel('counts')
plt.savefig('xy_hists.png', dpi=200)
def plot_loss(history, epochs, model_name):
plt.figure()
plt.plot(np.arange(epochs), history.history['loss'], label='Train')
plt.plot(np.arange(epochs), history.history['val_loss'], label='Validation')
plt.legend()
plt.xlabel('epochs')
plt.ylabel('losses')
plt.savefig(IMG_PATH + '/' + model_name + '/loss_overall', dpi=200)
# plt.show()
plt.figure()
plt.plot(np.arange(epochs), history.history['x_pred_loss'], label='Train x', color='blue')
plt.plot(np.arange(epochs), history.history['val_x_pred_loss'], label='Validation x', color='blue', ls='--')
plt.plot(np.arange(epochs), history.history['y_pred_loss'], label='Train y', color='red')
plt.plot(np.arange(epochs), history.history['val_y_pred_loss'], label='Validation y', color='red', ls='--')
plt.legend()
plt.xlabel('epochs')
plt.ylabel('losses')
plt.savefig(IMG_PATH + '/' + model_name + '/loss_xy', dpi=200)
# plt.show()
def plot_scatter(model, x_train, y_train, x_val, y_val, model_name):
# predictions
y_pred_train = model.predict(x_train)
y_pred_val = model.predict(x_val)
# visualise the velocity and directions of true vs prediction
# i.e. on a 2d plane
# train data
plt.figure()
plt.scatter(y_train[:,0], y_train[:,1], alpha=0.3, label='true')
plt.scatter(y_pred_train[0], y_pred_train[1], alpha=0.3, label='pred')
plt.xlabel('x-coordinates')
plt.ylabel('y-coordinates')
plt.title('Pred vs true on train data')
plt.legend()
plt.savefig(IMG_PATH + '/' + model_name + '/pred_v_true_train', dpi=200)
# val data
plt.figure()
plt.scatter(y_val[:,0], y_val[:,1], alpha=0.3, label='true')
plt.scatter(y_pred_val[0], y_pred_val[1], alpha=0.3, label='pred')
plt.xlabel('x-coordinates')
plt.ylabel('y-coordinates')
plt.title('Pred vs true on val data')
plt.legend()
plt.savefig(IMG_PATH + '/' + model_name + '/pred_v_true_val', dpi=200)
# visualise the x and y predictions separatelly
# train data
unity_x = np.linspace(-0.8,0.8,100)
unity_y = np.linspace(-0.8,0.8,100)
plt.figure(figsize=(10,4))
plt.subplot(121)
plt.plot(unity_x, unity_x, c='orange', label='identity')
plt.scatter(y_train[:,0], y_pred_train[0], alpha=0.3)
plt.xlabel("True x"); plt.ylabel("Pred x")
plt.legend()
plt.subplot(122)
plt.plot(unity_y, unity_y, c='orange', label='identity')
plt.scatter(y_train[:,1], y_pred_train[1], alpha=0.3)
plt.xlabel("True y"); plt.ylabel("Pred y")
plt.legend()
plt.savefig(IMG_PATH + '/' + model_name + '/pred_v_true_train_xy', dpi=200)
# val data
plt.figure(figsize=(10,4))
plt.subplot(121)
plt.plot(unity_x, unity_x, c='orange', label='identity')
plt.scatter(y_val[:,0], y_pred_val[0], alpha=0.3)
plt.xlabel("True x"); plt.ylabel("Pred x")
plt.legend()
plt.subplot(122)
plt.plot(unity_y, unity_y, c='orange', label='identity')
plt.scatter(y_val[:,1], y_pred_val[1], alpha=0.3)
plt.xlabel("True y"); plt.ylabel("Pred y")
plt.legend()
plt.savefig(IMG_PATH + '/' + model_name + '/pred_v_true_val_xy', dpi=200)
def plot_model(model_name):
HOME_PATH = '/home/macleanlab/mufeng/models/'
model_path = HOME_PATH + model_name
cnn_model = keras.models.load_model(model_path)
dot_img_file = 'model_diagram.png'
tf.keras.utils.plot_model(cnn_model, to_file=dot_img_file, show_shapes=True, show_layer_names=True, dpi=200)
def plot_pred_dots(model):
data = np.load('dot_data.npz')
x = data['x']
y_true = data['y']
y_pred = model.predict(x)
plt.figure()
plt.scatter(y_true[:,0], y_true[:,1], alpha=0.3, label='true')
plt.scatter(y_pred[0], y_pred[1], alpha=0.3, label='pred')
plt.xlabel('x-coordinates')
plt.ylabel('y-coordinates')
plt.title('Pred vs true on train data')
plt.legend()
plt.savefig(IMG_PATH+'/res_plots/pred_v_computed_dots', dpi=200)
# visualise the x and y predictions separatelly
unity_x = np.linspace(-1,1,100)
unity_y = np.linspace(-1,1,100)
plt.figure(figsize=(12,4))
plt.subplot(121)
plt.plot(unity_x, unity_x, c='orange', label='identity')
plt.scatter(y_true[:,0], y_pred[0], alpha=0.3)
plt.xlabel("Computed x"); plt.ylabel("Pred x")
plt.legend()
plt.subplot(122)
plt.plot(unity_y, unity_y, c='orange', label='identity')
plt.scatter(y_true[:,1], y_pred[1], alpha=0.3)
plt.xlabel("Computed y"); plt.ylabel("Pred y")
plt.legend()
plt.savefig(IMG_PATH+'/res_plots/pred_v_computed_xy_dots', dpi=200)