rewrite everything for precise values
This commit is contained in:
parent
e3075fc15a
commit
8caf3507e5
9 changed files with 92 additions and 52 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -8,3 +8,4 @@ logs/
|
|||
.ipynb_checkpoints/
|
||||
*.pdf
|
||||
*.npy
|
||||
accuracy.csv
|
||||
|
|
|
@ -11,8 +11,8 @@ class CustomScaler:
|
|||
def fit(self, data: np.ndarray) -> None:
|
||||
self.means = np.mean(data, 0)
|
||||
self.stds = np.std(data, 0)
|
||||
print(self.means)
|
||||
print(self.stds)
|
||||
# print(self.means)
|
||||
# print(self.stds)
|
||||
|
||||
def _check_fitted(self):
|
||||
if (self.means is None) or (self.stds is None):
|
||||
|
|
32
accuracy.py
Normal file
32
accuracy.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
import numpy as np
|
||||
|
||||
from CustomScaler import CustomScaler
|
||||
from interpolators.griddata import GriddataInterpolator
|
||||
from simulation_list import SimulationList
|
||||
|
||||
simlist = SimulationList.jsonlines_load()
|
||||
|
||||
data = np.array([
|
||||
[s.alpha, s.v, s.projectile_mass, s.gamma, s.target_water_fraction, s.projectile_water_fraction]
|
||||
for s in simlist.simlist if s.type == "original"
|
||||
])
|
||||
values = np.array([s.water_retention_both for s in simlist.simlist if s.type == "original"])
|
||||
|
||||
scaler = CustomScaler()
|
||||
scaler.fit(data)
|
||||
scaled_data = scaler.transform_data(data)
|
||||
# interpolator = RbfInterpolator(scaled_data, values)
|
||||
interpolator = GriddataInterpolator(scaled_data, values)
|
||||
|
||||
print("\t".join(["runid", "interpolated value", "real value", "alpha", "v", "m", "gamma"]))
|
||||
for sim in simlist.simlist:
|
||||
if sim.type == "original" or sim.v > 10 or int(sim.runid) < 10:
|
||||
continue
|
||||
print(int(sim.runid))
|
||||
parameters = [sim.alpha, sim.v, sim.projectile_mass, sim.gamma, sim.target_water_fraction,
|
||||
sim.projectile_water_fraction]
|
||||
scaled_parameters = list(scaler.transform_parameters(parameters))
|
||||
|
||||
result = interpolator.interpolate(*scaled_parameters)
|
||||
print("\t".join(map(str, [sim.runid, result, sim.water_retention_both, *parameters[:-2]])))
|
||||
# exit()
|
42
kerastest.py
42
kerastest.py
|
@ -10,26 +10,31 @@ from keras.utils import plot_model
|
|||
from matplotlib import pyplot as plt
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
|
||||
from simulation import Simulation
|
||||
from simulation_list import SimulationList
|
||||
|
||||
simulations = SimulationList.jsonlines_load()
|
||||
|
||||
random.shuffle(simulations.simlist)
|
||||
train_data = simulations.simlist
|
||||
test_data = simulations.simlist[800:]
|
||||
train_data = [s for s in simulations.simlist]
|
||||
test_data = [s for s in simulations.simlist if s.type != "original"]
|
||||
|
||||
sim: Simulation
|
||||
print(np.array([[s.mcode, s.wpcode, s.wtcode, s.gammacode, s.alphacode, s.vcode] for s in train_data[:10]]))
|
||||
X = np.array([[s.mcode, s.wpcode, s.wtcode, s.gammacode, s.alphacode, s.vcode] for s in train_data])
|
||||
X = np.array(
|
||||
[[s.alpha, s.v, s.projectile_mass, s.gamma, s.target_water_fraction, s.projectile_water_fraction] for s in
|
||||
train_data])
|
||||
scaler = StandardScaler()
|
||||
scaler.fit(X)
|
||||
x = scaler.transform(X)
|
||||
print(x.shape)
|
||||
Y = np.array([s.water_retention_both for s in train_data])
|
||||
print(Y.shape)
|
||||
X_test = np.array([[s.mcode, s.wpcode, s.wtcode, s.gammacode, s.alphacode, s.vcode] for s in test_data])
|
||||
Y_test = np.array([s.water_retention_both for s in test_data])
|
||||
X_test = np.array(
|
||||
[[s.alpha, s.v, s.projectile_mass, s.gamma, s.target_water_fraction, s.projectile_water_fraction] for s in
|
||||
test_data])
|
||||
Y_test = np.array([s.mass_retention_both for s in test_data])
|
||||
x_test = scaler.transform(X_test)
|
||||
# print(X_test)
|
||||
# print(X[0])
|
||||
# exit()
|
||||
tbCallBack = keras.callbacks.TensorBoard(log_dir='./logs', histogram_freq=0, batch_size=32, write_graph=True,
|
||||
write_grads=False, write_images=False, embeddings_freq=0,
|
||||
embeddings_layer_names=None, embeddings_metadata=None, embeddings_data=None,
|
||||
|
@ -47,22 +52,23 @@ else:
|
|||
|
||||
model.summary()
|
||||
plot_model(model, "model.png", show_shapes=True, show_layer_names=True)
|
||||
model.fit(x, Y, epochs=200, callbacks=[tbCallBack], validation_split=0.02)
|
||||
loss = model.evaluate(X_test, Y_test)
|
||||
model.fit(x, Y, epochs=400, callbacks=[tbCallBack], validation_split=0.02)
|
||||
loss = model.evaluate(x_test, Y_test)
|
||||
print(loss)
|
||||
|
||||
# print("-------------------------------------")
|
||||
# exit()
|
||||
model.save("model.hd5")
|
||||
|
||||
xrange = np.linspace(-0.5, 60.5, 100)
|
||||
yrange = np.linspace(0.5, 5.5, 100)
|
||||
xgrid, ygrid = np.meshgrid(xrange, yrange)
|
||||
mcode = 24.
|
||||
wpcode = 10
|
||||
wtcode = 10
|
||||
gammacode = 1
|
||||
testinput = np.array([[mcode, wpcode, wtcode, gammacode, np.nan, np.nan]] * 100 * 100)
|
||||
testinput[::, 4] = xgrid.flatten()
|
||||
testinput[::, 5] = ygrid.flatten()
|
||||
mcode = 23e10
|
||||
wpcode = 15 / 100
|
||||
wtcode = 15 / 100
|
||||
gammacode = 0.7
|
||||
testinput = np.array([[np.nan, np.nan, mcode, gammacode, wtcode, wpcode]] * 100 * 100)
|
||||
testinput[::, 0] = xgrid.flatten()
|
||||
testinput[::, 1] = ygrid.flatten()
|
||||
testinput = scaler.transform(testinput)
|
||||
|
||||
print(testinput)
|
||||
|
|
|
@ -32,7 +32,7 @@ for set_type, directories in simulation_sets.items():
|
|||
sim.load_params_from_aggregates_txt(aggregates_file)
|
||||
sim.assert_all_loaded()
|
||||
simulations.append(sim)
|
||||
print(vars(sim))
|
||||
# print(vars(sim))
|
||||
|
||||
print(len(simulations.simlist))
|
||||
|
||||
|
|
|
@ -46,22 +46,21 @@ class SimulationList:
|
|||
def as_matrix(self):
|
||||
entrylist = []
|
||||
for sim in self.simlist:
|
||||
entrylist.append(
|
||||
[sim.mcode, sim.wpcode, sim.wtcode, sim.gammacode, sim.alphacode, sim.vcode, sim.water_retention_both]
|
||||
)
|
||||
if sim.type == "original":
|
||||
entrylist.append(
|
||||
[sim.mcode, sim.wpcode, sim.wtcode, sim.gammacode, sim.alphacode, sim.vcode,
|
||||
sim.water_retention_both]
|
||||
)
|
||||
return np.asarray(entrylist)
|
||||
|
||||
@property
|
||||
def X(self):
|
||||
return np.array([
|
||||
[s.alphacode, s.vcode, 10 ** s.mcode, s.gammacode, s.wtcode, s.wpcode]
|
||||
[s.alpha, s.v, s.projectile_mass, s.gamma, s.target_water_fraction, s.projectile_water_fraction]
|
||||
for s in self.simlist
|
||||
])
|
||||
|
||||
@property
|
||||
def Y(self):
|
||||
return np.array([s.water_retention_both for s in self.simlist])
|
||||
return np.array([s.water_retention_both for s in self.simlist ])
|
||||
|
||||
@property
|
||||
def matrix_labels(self):
|
||||
return ["mcode", "wpcode", "wtcode", "gammacode", "alphacode", "vcode", "water retention"]
|
||||
|
|
|
@ -34,7 +34,7 @@ grid_alpha, grid_v = np.meshgrid(alpharange, vrange)
|
|||
fig, ax = plt.subplots()
|
||||
plt.subplots_adjust(bottom=0.35)
|
||||
t = np.arange(0.0, 1.0, 0.001)
|
||||
mcode_default, gamma_default, wt_default, wp_default = [24.0, 1, 10.0, 10.0]
|
||||
mcode_default, gamma_default, wt_default, wp_default = [24.0, 1, 15.0, 15.0]
|
||||
|
||||
datagrid = np.zeros_like(grid_alpha)
|
||||
|
||||
|
|
|
@ -8,32 +8,27 @@ from sklearn.preprocessing import StandardScaler
|
|||
from simulation_list import SimulationList
|
||||
|
||||
simlist = SimulationList.jsonlines_load()
|
||||
train_data = simlist.simlist
|
||||
|
||||
X = np.array([[s.mcode, s.wpcode, s.wtcode, s.gammacode, s.alphacode, s.vcode] for s in train_data])
|
||||
X = simlist.X
|
||||
scaler = StandardScaler()
|
||||
scaler.fit(X)
|
||||
|
||||
|
||||
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
plt.subplots_adjust(bottom=0.35)
|
||||
t = np.arange(0.0, 1.0, 0.001)
|
||||
mcode_default, gamma_default, wt_default, wp_default = [24.0, 1, 10.0, 10.0]
|
||||
|
||||
mcode_default, gamma_default, wt_default, wp_default = [24.0, 1, 15.0, 15.0]
|
||||
|
||||
xrange = np.linspace(-0.5, 60.5, 100)
|
||||
yrange = np.linspace(0.5, 5.5, 100)
|
||||
xgrid, ygrid = np.meshgrid(xrange, yrange)
|
||||
mcode = 24.
|
||||
wpcode = 10
|
||||
wtcode = 10
|
||||
wpcode = 15 / 100
|
||||
wtcode = 15 / 100
|
||||
gammacode = 1
|
||||
|
||||
testinput = np.array([[mcode, wpcode, wtcode, gammacode, np.nan, np.nan]] * 100 * 100)
|
||||
testinput[::, 4] = xgrid.flatten()
|
||||
testinput[::, 5] = ygrid.flatten()
|
||||
testinput = np.array([[np.nan, np.nan, mcode, gammacode, wtcode, wpcode]] * 100 * 100)
|
||||
testinput[::, 0] = xgrid.flatten()
|
||||
testinput[::, 1] = ygrid.flatten()
|
||||
testinput = scaler.transform(testinput)
|
||||
|
||||
model = load_model("model.hd5")
|
||||
|
@ -43,7 +38,6 @@ outgrid = np.reshape(testoutput, (100, 100))
|
|||
mesh = plt.pcolormesh(xgrid, ygrid, outgrid, cmap="Blues", vmin=0, vmax=1) # type:QuadMesh
|
||||
plt.colorbar()
|
||||
|
||||
|
||||
axcolor = 'lightgoldenrodyellow'
|
||||
ax_mcode = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
|
||||
ax_gamma = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor)
|
||||
|
@ -57,13 +51,13 @@ s_wp = Slider(ax_wp, 'wp', 10, 20, valinit=wp_default)
|
|||
|
||||
|
||||
def update(val):
|
||||
mcode = s_mcode.val
|
||||
mcode = 10 ** s_mcode.val
|
||||
gamma = s_gamma.val
|
||||
wt = s_wt.val
|
||||
wp = s_wp.val
|
||||
testinput = np.array([[mcode, wp, wt, gamma, np.nan, np.nan]] * 100 * 100)
|
||||
testinput[::, 4] = xgrid.flatten()
|
||||
testinput[::, 5] = ygrid.flatten()
|
||||
wt = s_wt.val / 100
|
||||
wp = s_wp.val / 100
|
||||
testinput = np.array([[np.nan, np.nan, mcode, gamma, wt, wp]] * 100 * 100)
|
||||
testinput[::, 0] = xgrid.flatten()
|
||||
testinput[::, 1] = ygrid.flatten()
|
||||
testinput = scaler.transform(testinput)
|
||||
|
||||
testoutput = model.predict(testinput)
|
||||
|
|
14
visualize.py
14
visualize.py
|
@ -8,10 +8,18 @@ from simulation_list import SimulationList
|
|||
|
||||
|
||||
def main():
|
||||
mcode, gamma, wt, wp = [10 ** 23, 1, 10.0, 10.0]
|
||||
mcode, gamma, wt, wp = [10 ** 23, 0.7, 15/100, 15/100]
|
||||
simlist = SimulationList.jsonlines_load()
|
||||
|
||||
# for s in simlist.simlist:
|
||||
# if s.type!="original":
|
||||
# continue
|
||||
# print(s.wpcode,s.projectile_water_fraction)
|
||||
# exit()
|
||||
data = simlist.X
|
||||
print("-----")
|
||||
print(len(data))
|
||||
# print(data[0])
|
||||
# exit()
|
||||
values = simlist.Y
|
||||
|
||||
scaler = CustomScaler()
|
||||
|
@ -29,7 +37,7 @@ def main():
|
|||
|
||||
grid_result = interpolator.interpolate(*scaled_parameters)
|
||||
|
||||
plt.title("m={:3.0f}, gamma={:3.1f}, wt={:2.0f}, wp={:2.0f}\n".format(mcode, gamma, wt, wp))
|
||||
plt.title("m={:3.0e}, gamma={:3.1f}, wt={:2.0f}%, wp={:2.0f}%\n".format(mcode, gamma, wt*100, wp*100))
|
||||
|
||||
# plt.contourf(grid_x, grid_y, grid_c, N, cmap="Blues", vmin=0, vmax=1)
|
||||
plt.pcolormesh(grid_alpha, grid_v, grid_result, cmap="Blues", vmin=0, vmax=1)
|
||||
|
|
Reference in a new issue