-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolyCurve_fit.py
164 lines (143 loc) · 4.79 KB
/
polyCurve_fit.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
import numpy as np
import pandas as pd
from scipy.optimize import curve_fit
import itertools
import json
import time
from multiprocessing import Pool, cpu_count
# This is the form for polynomial functions
def polynom(X, E, P):
result = 0
for i in range(len(P)):
term = P[i]
for j in range(X.shape[1]):
term *= X[:, j].astype(float) ** E[i, j]
result += term
return result
# This is the chunk generator.
def chunking_generator(lower, upper, rows, cols, num_workers, current_worker):
all_combinations = itertools.product(range(lower, upper + 1), repeat=rows * cols)
worker_combinations = itertools.islice(
all_combinations, current_worker, None, num_workers
)
for comb in worker_combinations:
matrix = np.array(comb).reshape(rows, cols)
if len(np.unique(matrix, axis=0)) == rows:
yield matrix
def findFit(
E, worker_id, input_data, output_data, P_initial, best_fit, lower, upper, filename
):
try:
P_opt, P_cov = curve_fit(
lambda X, *P: polynom(X, E, P),
input_data,
output_data,
p0=P_initial,
maxfev=5000,
)
residuals = output_data - polynom(input_data, E, P_opt)
SSR = np.sum(residuals**2)
if SSR < best_fit["SSR"]:
best_fit["SSR"] = SSR
best_fit["P_opt"] = P_opt
best_fit["P_cov"] = P_cov
best_fit["E"] = E
print(f"current best fit for worker:{worker_id} is {SSR}")
with open(
f"w_{worker_id}_{filename}_{len(P_initial)}_{lower}_{upper}_best.log",
"w",
) as f:
data = {
"worker_id": worker_id,
"SSR": best_fit["SSR"],
"P_opt": best_fit["P_opt"].tolist(),
"P_cov": best_fit["P_cov"].tolist(),
"E": best_fit["E"].tolist(),
}
json.dump(data, f)
except RuntimeError as e:
print(f"Optimization failed for this E: {e}")
def worker_func(
worker_id,
lower,
upper,
rows,
cols,
num_workers,
input_data,
output_data,
P_initial,
filename,
):
best_fit = {"SSR": float("inf"), "P_opt": None, "P_cov": None, "E": None}
generator = chunking_generator(lower, upper, rows, cols, num_workers, worker_id)
for Es in generator:
findFit(
Es,
worker_id,
input_data,
output_data,
P_initial,
best_fit,
lower,
upper,
filename,
)
# This is the main function.
def polyCurve_fit(**kwargs):
start_time = time.time()
filename = kwargs.get("filename", "data.csv")
P_initial_size = kwargs.get("Parameters", 2)
P_initial = np.zeros(P_initial_size)
lower = kwargs.get("lower", -3)
upper = kwargs.get("upper", 3)
data = pd.read_csv(filename, header=None)
input_data = data.iloc[:, :-1]
output_data = data.iloc[:, -1]
input_data = input_data.to_numpy()
output_data = output_data.to_numpy()
# There have been issues for input data beeing Zero this is prevented by addind a tiny value for it.
input_data[input_data == 0] += 1e-8
rows = len(P_initial)
cols = input_data.shape[1]
num_workers = cpu_count()
with Pool(num_workers) as p:
p.starmap(
worker_func,
[
(
i,
lower,
upper,
rows,
cols,
num_workers,
input_data,
output_data,
P_initial,
filename,
)
for i in range(num_workers)
],
)
end_time = time.time()
total_time = end_time - start_time
def read_best_fit(worker_id, P_initial, lower, upper, filename):
with open(
f"w_{worker_id}_{filename}_{len(P_initial)}_{lower}_{upper}_best.log", "r"
) as f:
return json.load(f)
best_fit_overall = {"SSR": float("inf")}
for i in range(num_workers):
worker_best_fit = read_best_fit(i, P_initial, lower, upper, filename)
if worker_best_fit["SSR"] < best_fit_overall["SSR"]:
best_fit_overall = worker_best_fit
print("##########################################")
print(f"polyCurve_fit has finished in{total_time}s:")
print("##########################################")
print("best_fit_overall")
print(best_fit_overall)
print("##########################################")
with open(f"best_fit_{filename}_{len(P_initial)}_{lower}_{upper}.log", "w") as f:
json.dump(best_fit_overall, f)
f.write(f"Total time: {total_time} seconds")