debugging of fitter, many issues still persist

This commit is contained in:
ralf 2024-07-11 13:05:32 +03:00
parent 6e7a727b1a
commit 7cf6590f94

108
fitter.py
View File

@ -6,7 +6,8 @@ import re
from scipy.optimize import least_squares
from model import Model
from scipy.linalg import svd
from Model import Model
from Data import Data
@ -81,38 +82,38 @@ class Fitter:
plt.show()
# exit()
return res # , measured_fluo - calculated_fluo)
return res, err # , measured_fluo - calculated_fluo)
def optimize(self, init_parameters=None):
t0 = time.time()
self.iteration = 0
if init_parameters is None:
m = self.model()
K_pc_half = m.K_pc_half
tau_xfer = m.tau_xfer
tau_RC = 1.5
offset = 0
def optimize(self, init_parameters=None):
t0 = time.time()
self.iteration = 0
if init_parameters is None:
m = self.model()
K_pc_half = m.K_pc_half
tau_xfer = m.tau_xfer
tau_RC = 1.5
offset = 0
d = self.data
init_parameters = np.array([d.gGaL, d.ECal, K_pc_half, tau_xfer, tau_RC, offset])
print(init_parameters.tolist())
d = self.data
init_parameters = np.array([d.gGaL, d.ECal, K_pc_half, tau_xfer, tau_RC, offset])
print(init_parameters.tolist())
bounds = (
(0.01, 10, 0.1, 0.1, 0.1, -5, -10),
(10, 100, 100, 1, 100, 10, 10),
)
bounds = (
(0.01, 10, 0.1, 0.1, 0.1, -5, -10),
(10, 100, 100, 1, 100, 10, 10),
)
res = least_squares(self.cost_func, init_parameters, bounds=bounds, xtol=1e-10)
print()
print(" Parameters: [gGaL, ECal, K_pc_half, tau_xfer, tau_RC, offset]")
print(" Initial:", init_parameters.tolist())
print(" Optimized:", res.x.tolist())
print(" Optim status:", res.status)
print("Optim message:", res.message)
res, err = least_squares(self.cost_func, init_parameters, bounds=bounds, xtol=1e-10)
print()
print(" Parameters: [gGaL, ECal, K_pc_half, tau_xfer, tau_RC, offset]")
print(" Initial:", init_parameters.tolist())
print(" Optimized:", res.x.tolist())
print(" Optim status:", res.status)
print("Optim message:", res.message)
gGaL, ECal, K_pc_half, tau_xfer, tau_RC, offset = res.x
gGaL, ECal, K_pc_half, tau_xfer, tau_RC, offset = res.x
self.fit_results.update({
self.fit_results.update({
'gGaL': gGaL,
'ECal': ECal,
'K_pc_half': K_pc_half,
@ -121,35 +122,36 @@ class Fitter:
'offset': offset,
'mean_squared_error': err})
model = self.model()
model = self.model()
model.ECaL = ECal
model.gCaL = gGaL
model.K_pc_half = K_pc_half
model.tau_xfer = tau_xfer
model.ECaL = ECal
model.gCaL = gGaL
model.K_pc_half = K_pc_half
model.tau_xfer = tau_xfer
model.solve(times=self.time_points)
model.solve(times=self.time_points)
_calc_curr = model.calculated_current()
calculated_current = self.convolve_current(_calc_curr, tau=tau_RC) + offset
print("Elapsed time:", time.time() - t0)
_calc_curr = model.calculated_current()
calculated_current = self.convolve_current(_calc_curr, tau=tau_RC) + offset
print("Elapsed time:", time.time() - t0)
fig = plt.figure(figsize=(24, 12))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
ax1.plot(1000 * self.data.current_t, self.data.current, label="Measured")
ax1.plot(self.time_points, calculated_current, label="Calculated")
ax1.set_xlabel("time, ms")
ax1.set_ylabel("current, pA/pF")
ax1.legend(frameon=False)
fig = plt.figure(figsize=(24, 12))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
ax1.plot(1000 * self.data.current_t, self.data.current, label="Measured")
ax1.plot(self.time_points, calculated_current, label="Calculated")
ax1.set_xlabel("time, ms")
ax1.set_ylabel("current, pA/pF")
ax1.legend(frameon=False)
tp = self.time_points[self.current_time_indecies]
ax2.plot(tp, self.measured_current, label="Measured")
ax2.plot(tp, calculated_current[self.current_time_indecies], label="Calculated")
ax2.set_xlabel("time, ms")
ax2.set_ylabel("current, pA/pF")
ax2.legend(frameon=False)
return res, fig
tp = self.time_points[self.current_time_indecies]
ax2.plot(tp, self.measured_current, label="Measured")
ax2.plot(tp, calculated_current[self.current_time_indecies], label="Calculated")
ax2.set_xlabel("time, ms")
ax2.set_ylabel("current, pA/pF")
ax2.legend(frameon=False)
return res, fig
def covcor_from_lsq(res):
@ -173,8 +175,8 @@ class Fitter:
plt.ylabel('Variables')
plt.show()
if __name__ == "__main__":
if __name__ == "__main__":
filename = "ltcc_current.h5"
eid = "0033635a51b096dc449eb9964e70443a67fc16b9587ae3ff6564eea1fa0e3437_2018.06.18 14:48:40"
@ -183,6 +185,8 @@ if __name__ == "__main__":
fit = Fitter(Model, data)
res, fig = fit.optimize()
fit_hist = pd.DataFrame.from_dict(fit.fit_results, orient='index').T
fit_hist.index.name = 'Iterations'