This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.py
109 lines (86 loc) · 3.32 KB
/
testing.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
import json
from statistics import mean
import numpy as np
from keras.engine.saving import load_model
from CustomScaler import CustomScaler
from config import water_fraction
from interpolators.griddata import GriddataInterpolator
from interpolators.rbf import RbfInterpolator
from simulation import Simulation
from simulation_list import SimulationList
simulations = SimulationList.jsonlines_load()
scaler = CustomScaler()
scaler.fit(simulations.X)
model = load_model("model.hd5" if water_fraction else "model_mass.hd5")
def squared_error(inter: float, correct: float) -> float:
return (inter - correct) ** 2
def absolute_error(inter: float, correct: float) -> float:
return abs(inter - correct)
def neural_network_test(scaled_input) -> float:
nn_input = np.asarray([scaled_input])
testoutput = model.predict(nn_input)[0][0]
return testoutput
def rbf_test(scaled_parameters) -> float:
scaled_data = scaler.transform_data(simulations.X)
interpolator = RbfInterpolator(scaled_data, simulations.Y)
result = interpolator.interpolate(*scaled_parameters)
return result
def grid_test(scaled_parameters) -> float:
scaled_data = scaler.transform_data(simulations.X)
interpolator = GriddataInterpolator(scaled_data, simulations.Y)
result = interpolator.interpolate(*scaled_parameters)
return float(result)
nn_squared_errors = []
nn_errors = []
rbf_squared_errors = []
rbf_errors = []
grid_squared_errors = []
grid_errors = []
cachefile="grid-testing-cache.json" if water_fraction else "grid-testing-cache-mass.json"
try:
with open(cachefile) as f:
raw_data = json.load(f)
grid_testing_cache = {int(key): value for key, value in raw_data.items()}
except FileNotFoundError:
grid_testing_cache = {}
sim: Simulation
a = 0
for sim in simulations.simlist:
if not sim.testcase:
continue
a += 1
testinput = [sim.alpha, sim.v, sim.projectile_mass, sim.gamma,
sim.target_water_fraction, sim.projectile_water_fraction]
scaled_input = list(scaler.transform_parameters(testinput))
nn_output = neural_network_test(scaled_input)
nn_squared_errors.append(squared_error(nn_output, sim.water_retention_both))
nn_errors.append(absolute_error(nn_output, sim.water_retention_both))
rbf_output = rbf_test(scaled_input)
rbf_squared_errors.append(squared_error(rbf_output, sim.water_retention_both))
rbf_errors.append(absolute_error(rbf_output, sim.water_retention_both))
if sim.runid in grid_testing_cache:
grid_output = grid_testing_cache[sim.runid]
else:
grid_output = grid_test(scaled_input)
if np.isnan(grid_output):
grid_output = False
grid_testing_cache[sim.runid] = grid_output
with open(cachefile, "w") as f:
json.dump(grid_testing_cache, f)
if grid_output:
grid_squared_errors.append(squared_error(grid_output, sim.water_retention_both))
grid_errors.append(absolute_error(grid_output, sim.water_retention_both))
print(nn_output, rbf_output, grid_output, sim.water_retention_both)
print(a)
print()
# print(nn_squared_errors)
print(mean(nn_squared_errors))
print(mean(nn_errors))
print()
# print(rbf_squared_errors)
print(mean(rbf_squared_errors))
print(mean(rbf_errors))
print()
# print(grid_squared_errors)
print(mean(grid_squared_errors))
print(mean(grid_errors))