-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameter_finder_uniform.py
133 lines (100 loc) · 3.57 KB
/
parameter_finder_uniform.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
import argparse
import csv
import pathlib
from tarfile import TarInfo
import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize as optimize
from script_configurator import SIGMOID_CONFIG
from sigmoid_calculation import get_sigmoid
FINDER_CONFIG = SIGMOID_CONFIG["param_finder_props"]
def filter_data(data):
# filter data to stay within interpolation bounds
if FINDER_CONFIG["shift_time"] != 0:
data[:, 0] = data[:, 0] - np.min(data[:, 0])
data = data[data[:, 0] >= SIGMOID_CONFIG["t0"]]
data = data[data[:, 0] <= SIGMOID_CONFIG["t_final"]]
return data
def read_data_csv(path: pathlib.Path):
with open(path) as f:
reader = csv.reader(
f, delimiter=SIGMOID_CONFIG["csv_delimiter"], quoting=csv.QUOTE_NONNUMERIC
)
data = [row for row in reader]
return filter_data(np.array(data))
def calculate_resd_vector(trial_sol, data):
t_vals = data[:, 0]
s_vals = data[:, 1]
f_vals = np.squeeze(trial_sol(t_vals))
return 1000 * (s_vals - f_vals)
def uniform_cost(x, data):
d, g, mu = x
s = get_sigmoid(d, g, mu)
return np.max(np.abs(calculate_resd_vector(s, data)))
def plot(d, g, mu, data):
t_d = data[:, 0]
y_d = data[:, 1]
s = get_sigmoid(d, g, mu)
t_calc = np.linspace(SIGMOID_CONFIG["t0"], SIGMOID_CONFIG["t_final"], 1000)
y_calc = np.squeeze(s(t_calc))
plt.plot(t_calc, y_calc)
plt.plot(t_d, y_d, "ro")
plt.show()
def r2_calc(d, g, mu, data):
t_d = data[:, 0]
y_d = data[:, 1]
y_davg = np.average(y_d)
s = get_sigmoid(d, g, mu)
y_calc = np.squeeze(s(t_d))
ss_tot = np.sum((y_d-y_davg)**2)
ss_res = np.sum((y_d-y_calc)**2)
return 1 - (ss_res/ss_tot)
def fit_data(dat):
fit = optimize.dual_annealing(uniform_cost,
x0 = (FINDER_CONFIG["d_ini"], FINDER_CONFIG["g_ini"], FINDER_CONFIG["mu_ini"]),
bounds=((FINDER_CONFIG["d_min"], FINDER_CONFIG["d_max"]),
(FINDER_CONFIG["g_min"], FINDER_CONFIG["g_max"]),
(FINDER_CONFIG["mu_min"], FINDER_CONFIG["mu_max"])),
args=(dat,),
)
d = fit.x[0]
g = fit.x[1]
mu = fit.x[2]
return fit,d,g,mu
def main(cli_args=None):
parser = argparse.ArgumentParser()
parser.add_argument("file_path", type=pathlib.Path)
p = parser.parse_args(args=cli_args)
if not p.file_path.exists():
raise FileNotFoundError("Input File not found!")
print(f"Running optimization with config: \n{FINDER_CONFIG}")
dat = read_data_csv(p.file_path)
print("Data read successfully. Starting optimization.")
print(
"============================================================================"
)
fit, d, g, mu = fit_data(dat)
print(f"Uniform error {fit.fun}")
print(
"\n\n============================================================================"
)
print(fit)
print(
"============================================================================"
)
if fit.success:
print(
f"Uniform fit completed. "
f"Obtained d = {d}, g = {g}, mu = {mu}"
)
else:
print("!!!!!!!\nFit failed. Check the algorithm output above\n!!!!!!")
print(
"============================================================================"
)
print(f"R^2 = {r2_calc(d, g, mu, dat)*100}%")
if FINDER_CONFIG["plot_graphs_matplotlib"] != 0:
plot(d, g, mu, dat)
return d, g, mu
if __name__ == "__main__":
main()