in scheme_adapters/polyfit_adapter/polyfit_adapter.py [0:0]
def polynomial_fit(self, x, y, min_range, max_range, degree):
x, y = zip(*sorted(zip(x, y)))
x = list(x)
y = list(y)
mini = 10000000
for el in y:
if el != -1000 and mini > el:
mini = el
for i in range(len(y)):
if y[i] == -1000:
y[i] = mini
with warnings.catch_warnings():
warnings.simplefilter('ignore', np.RankWarning)
poly = np.poly1d(np.polyfit(x, y, degree))
poly_coeff = np.polyfit(x, y, degree)
num = int(len(poly_coeff))
derivative = []
for exp in range(0, len(poly_coeff) - 1, 1):
num = num - 1
derivative.append(poly_coeff[exp] * num)
roots = np.roots(derivative)
roots = np.append(roots, min_range)
roots = np.append(roots, max_range)
best_val = -10000000000
best_point = -1
for el in roots:
if np.iscomplex(el):
continue
if max_range >= el >= min_range:
if best_val < poly(el):
best_val = max(best_val, poly(el))
best_point = np.absolute(el)
print("Best point:", best_point)
return best_val, best_point