-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate_plot.py
executable file
·172 lines (144 loc) · 6.66 KB
/
generate_plot.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Generate plots by calling the plot_1D function in the main file.
This includes plotting:
potential u across time at a particular cell (plot_1D_cell),
potential u across cells (spatially) at a particular time (plot_1D_array),
and potential across time and space (plot_1D_grid).
The function takes inputs:
data_list, dynamics, model, fig_name, x_t_to_plot, i.
Commented by Annie
"""
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import io
def plot_1D(data_list, dynamics, model, fig_name, x_t_to_plot, i):
plot_1D_cell(data_list, dynamics, model, fig_name[1:], x_t_to_plot[0], i)
plot_1D_array(data_list, dynamics, model, fig_name[1:], x_t_to_plot[1], i)
plot_1D_grid(data_list, dynamics, model, fig_name[1:], x_t_to_plot, i)
# plot_1D_vw(data_list, dynamics, model, fig_name[1:], x_t_to_plot, i)
return 0
def plot_1D_cell(data_list, dynamics, model, fig_name, x_to_plot, i):
## Unpack data
observe_x, observe_train, u_train, u, observe_test, u_test= \
data_list[0], data_list[1], data_list[2], data_list[3], data_list[4], data_list[5]
## Pick a cell to show
#cell = dynamics.max_x*x_to_plot
cell = x_to_plot
lnw = 3.0 # line width
szm = 50 # marker size
ftsz = 20 # font size
## Get data for cell
idx = [i for i,ix in enumerate(observe_x) if observe_x[i][0]==cell]
observe_geomtime = observe_x[idx]
u_GT = u[idx]
u_predict = model.predict(observe_geomtime)[:,0:1]
t_axis = observe_geomtime[:,1]
## Get data for points used in training process
idx_train = [i for i,ix in enumerate(observe_train) if observe_train[i][0]==cell]
u_trained_points = u_train[idx_train]
t_markers = (observe_train[idx_train])[:,1]
## Get data for points used in testing
idx_test = [i for i,ix in enumerate(observe_test) if observe_test[i][0]==cell]
u_test_points = u_test[idx_test]
t_markers_test = (observe_test[idx_test])[:,1]
## create figure for cell
plt.figure()
plt.rc('font', size= ftsz) #controls default text
fig, ax = plt.subplots()
Predicted, = ax.plot(t_axis, u_predict, c='r', label='Predicted',linewidth=lnw, zorder=0)
GT, = ax.plot(t_axis, u_GT, c='b', label='GT',linewidth=lnw, linestyle = 'dashed', zorder=5)
#plt.plot(t_axis, v_GT, c='b', label='GT')
#plt.plot(t_axis, v_predict, c='r', label='Predicted')
# If there are any trained data points for the current cell
if len(t_markers):
ax.scatter(t_markers, u_trained_points, marker='x', c='black',s=szm, label='Training points', zorder=10)
#plt.legend(loc='upper right', borderpad=0.2)
#if there are any test points for the current cell
if len(t_markers_test):
ax.scatter(t_markers_test, u_test_points, marker='x', c='green',s=szm, label='Testing points',zorder=10)
plt.title("_run_" + str(i) + "cell_at" + str(x_to_plot) + "mm")
plt.xlabel('t (TU)', fontsize = ftsz)
plt.ylabel('u (AU)', fontsize = ftsz)
plt.ylim((-0.2,1.2))
## save figure
plt.savefig("run_" + str(i) + "cell_at" + str(x_to_plot) + "mm_plot_1D.png", format="png", dpi=500, pad_inches = .1, bbox_inches = 'tight')
# png1 = io.BytesIO()
# plt.savefig(png1, format="png", dpi=500, pad_inches = .1, bbox_inches = 'tight')
# png2 = Image.open(png1)
# png2.save("run_" + str(i) + "cell_at" + str(x_to_plot) + "mm_plot_1D.tiff")
# png1.close()
return 0
def plot_1D_array(data_list, dynamics, model, fig_name, t_to_plot, i):
## Unpack data
observe_x, observe_train, v_train, v = data_list[0], data_list[1], data_list[2], data_list[3]
## Pick a point in time to show
obs_t = round(dynamics.max_t*t_to_plot)
lnw = 3.0 # line width
szm = 26 # marker size
ftsz = 20 # font size
## Get all array data for chosen time
idx = [i for i,ix in enumerate(observe_x) if observe_x[i][1]==obs_t]
observe_geomtime = observe_x[idx]
v_GT = v[idx]
v_predict = model.predict(observe_geomtime)[:,0:1]
x_ax = observe_geomtime[:,0]
## Get data for points used in training process
idx_train = [i for i,ix in enumerate(observe_train) if observe_train[i][1]==obs_t]
v_trained_points = v_train[idx_train]
x_markers = (observe_train[idx_train])[:,0]
## create figure
plt.figure()
plt.rc('font', size= ftsz) #controls default text size
plt.plot(x_ax, v_predict, c='r', label='Predicted',linewidth=lnw)
plt.plot(x_ax, v_GT, c='b', label='GT',linewidth=lnw, linestyle = 'dashed')
# If there are any trained data points for the current time step
if len(x_markers):
plt.scatter(x_markers, v_trained_points, marker='x', c='black',s=szm, label='Observed')
plt.legend(fontsize = ftsz, loc = 'lower center')
plt.xlabel('x (mm)', fontsize = ftsz)
plt.ylabel('u (AU)', fontsize = ftsz)
plt.ylim((-0.2,1.2))
## save figure
plt.savefig("run_" + str(i) + "_array_plot_1D.png", format="png", dpi=500, pad_inches = .1, bbox_inches = 'tight')
# png1 = io.BytesIO()
# plt.savefig(png1, format="png", dpi=500, pad_inches = .1, bbox_inches = 'tight')
# png2 = Image.open(png1)
# png2.save("run_" + str(i) + "_array_plot_1D.tiff")
# png1.close()
return 0
def plot_1D_grid(data_list, dynamics, model, fig_name, x_t_to_plot, i):
grid_size = 200
## Get data
x = np.linspace(dynamics.min_x,dynamics.max_x, grid_size)
t = np.linspace(dynamics.min_t,dynamics.max_t,grid_size)
X, T = np.meshgrid(x,t)
X_data = X.reshape(-1,1)
T_data = T.reshape(-1,1)
data = np.hstack((X_data, T_data))
v_pred = model.predict(data)[:,0:1]
Z = np.zeros((grid_size,grid_size))
for j in range(grid_size):
Z[j,:] = (v_pred[(j*grid_size):((j+1)*grid_size)]).reshape(-1)
## create figure
plt.figure()
contour = plt.contourf(T,X,Z, levels = np.arange(-0.15,1.06,0.15) , cmap=plt.cm.bone)
plt.plot(t, np.full(len(t), 2), '--', label = 'slice across t')
plt.vlines(round(dynamics.max_t * x_t_to_plot[1]), ymin = 0, ymax = dynamics.max_x,
colors = 'purple', linestyles = '--', label = 'slice across x')
plt.xlabel('t')
plt.ylabel('x')
plt.legend()
cbar = plt.colorbar(contour)
cbar.ax.set_ylabel('Membrane Potential U (AU)')
## save figure
plt.savefig("run_" + str(i) + "_grid_plot_1D.png", format="png", dpi=500, pad_inches = .1, bbox_inches = 'tight')
# png1 = io.BytesIO()
# plt.savefig(png1, format="png", dpi=500, pad_inches = .1, bbox_inches = 'tight')
# png2 = Image.open(png1)
# png2.save("run_" + str(i) + "_grid_plot_1D.tiff")
# png1.close()
return 0
return 0