Update interface.py
Browse files- interface.py +42 -27
interface.py
CHANGED
|
@@ -131,7 +131,7 @@ class BioprocessModel:
|
|
| 131 |
for param in params:
|
| 132 |
if param not in used_params:
|
| 133 |
raise ValueError(f"El par谩metro '{param}' no se usa en la ecuaci贸n '{equation}'.")
|
| 134 |
-
|
| 135 |
if model_type == 'biomass':
|
| 136 |
# Biomasa como funci贸n de tiempo y par谩metros
|
| 137 |
func_expr = expr
|
|
@@ -177,7 +177,10 @@ class BioprocessModel:
|
|
| 177 |
# Definir la funci贸n de ajuste (asegurarse de que toma los par谩metros correctamente)
|
| 178 |
def fit_func(t, *args):
|
| 179 |
try:
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
| 181 |
except Exception as e:
|
| 182 |
print(f"Error in fit_func: {e}")
|
| 183 |
raise
|
|
@@ -185,12 +188,16 @@ class BioprocessModel:
|
|
| 185 |
# Depuraci贸n: Verificar el n煤mero de par谩metros que se espera ajustar
|
| 186 |
print(f"Number of parameters to fit: {len(params)}")
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
try:
|
| 189 |
# Verifica que curve_fit puede recibir la funci贸n correctamente
|
| 190 |
-
print(f"Calling curve_fit with time: {time}, data: {data}, bounds: {bounds}")
|
| 191 |
|
| 192 |
-
# Intentar ajustar el modelo usando curve_fit
|
| 193 |
-
popt, _ = curve_fit(fit_func, time, data, bounds=bounds, maxfev=10000)
|
| 194 |
print(f"Optimal parameters found: {popt}")
|
| 195 |
|
| 196 |
# Guardar los par谩metros ajustados en el modelo
|
|
@@ -212,28 +219,36 @@ class BioprocessModel:
|
|
| 212 |
line_style='-', marker_style='o'):
|
| 213 |
sns.set_style(style)
|
| 214 |
|
| 215 |
-
fig,
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
return fig
|
| 238 |
|
| 239 |
###############################
|
|
|
|
| 131 |
for param in params:
|
| 132 |
if param not in used_params:
|
| 133 |
raise ValueError(f"El par谩metro '{param}' no se usa en la ecuaci贸n '{equation}'.")
|
| 134 |
+
|
| 135 |
if model_type == 'biomass':
|
| 136 |
# Biomasa como funci贸n de tiempo y par谩metros
|
| 137 |
func_expr = expr
|
|
|
|
| 177 |
# Definir la funci贸n de ajuste (asegurarse de que toma los par谩metros correctamente)
|
| 178 |
def fit_func(t, *args):
|
| 179 |
try:
|
| 180 |
+
y = func(t, *args)
|
| 181 |
+
print(f"fit_func called with args: {args}")
|
| 182 |
+
print(f"y_pred: {y}")
|
| 183 |
+
return y
|
| 184 |
except Exception as e:
|
| 185 |
print(f"Error in fit_func: {e}")
|
| 186 |
raise
|
|
|
|
| 188 |
# Depuraci贸n: Verificar el n煤mero de par谩metros que se espera ajustar
|
| 189 |
print(f"Number of parameters to fit: {len(params)}")
|
| 190 |
|
| 191 |
+
# Definir una estimaci贸n inicial para los par谩metros
|
| 192 |
+
p0 = [1.0] * len(params) # Puedes ajustar estos valores seg煤n sea necesario
|
| 193 |
+
print(f"Initial parameter guesses (p0): {p0}")
|
| 194 |
+
|
| 195 |
try:
|
| 196 |
# Verifica que curve_fit puede recibir la funci贸n correctamente
|
| 197 |
+
print(f"Calling curve_fit with time: {time}, data: {data}, bounds: {bounds}, p0: {p0}")
|
| 198 |
|
| 199 |
+
# Intentar ajustar el modelo usando curve_fit con p0
|
| 200 |
+
popt, _ = curve_fit(fit_func, time, data, p0=p0, bounds=bounds, maxfev=10000)
|
| 201 |
print(f"Optimal parameters found: {popt}")
|
| 202 |
|
| 203 |
# Guardar los par谩metros ajustados en el modelo
|
|
|
|
| 219 |
line_style='-', marker_style='o'):
|
| 220 |
sns.set_style(style)
|
| 221 |
|
| 222 |
+
fig, axs = plt.subplots(3, 1, figsize=(10, 15))
|
| 223 |
+
|
| 224 |
+
# Gr谩fica de Biomasa
|
| 225 |
+
axs[0].plot(time, biomass, 'o', label='Datos de Biomasa')
|
| 226 |
+
for i, result in enumerate(biomass_results):
|
| 227 |
+
axs[0].plot(time, result['y_pred'], '-', label=f'Modelo de Biomasa {i+1}')
|
| 228 |
+
axs[0].set_xlabel('Tiempo')
|
| 229 |
+
axs[0].set_ylabel('Biomasa')
|
| 230 |
+
if show_legend:
|
| 231 |
+
axs[0].legend(loc=legend_position)
|
| 232 |
+
|
| 233 |
+
# Gr谩fica de Sustrato
|
| 234 |
+
axs[1].plot(time, substrate, 'o', label='Datos de Sustrato')
|
| 235 |
+
for i, result in enumerate(substrate_results):
|
| 236 |
+
axs[1].plot(time, result['y_pred'], '-', label=f'Modelo de Sustrato {i+1}')
|
| 237 |
+
axs[1].set_xlabel('Tiempo')
|
| 238 |
+
axs[1].set_ylabel('Sustrato')
|
| 239 |
+
if show_legend:
|
| 240 |
+
axs[1].legend(loc=legend_position)
|
| 241 |
+
|
| 242 |
+
# Gr谩fica de Producto
|
| 243 |
+
axs[2].plot(time, product, 'o', label='Datos de Producto')
|
| 244 |
+
for i, result in enumerate(product_results):
|
| 245 |
+
axs[2].plot(time, result['y_pred'], '-', label=f'Modelo de Producto {i+1}')
|
| 246 |
+
axs[2].set_xlabel('Tiempo')
|
| 247 |
+
axs[2].set_ylabel('Producto')
|
| 248 |
+
if show_legend:
|
| 249 |
+
axs[2].legend(loc=legend_position)
|
| 250 |
+
|
| 251 |
+
plt.tight_layout()
|
| 252 |
return fig
|
| 253 |
|
| 254 |
###############################
|