C2MV commited on
Commit
09d69dc
verified
1 Parent(s): 090a5ef

Update interface.py

Browse files
Files changed (1) hide show
  1. interface.py +156 -0
interface.py CHANGED
@@ -21,6 +21,162 @@ model_path = MODEL_PATH
21
  tokenizer = AutoTokenizer.from_pretrained(model_path)
22
  model = AutoModelForCausalLM.from_pretrained(model_path)
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  # Decorador GPU aplicado para manejar la ejecuci贸n en GPU si est谩 disponible
25
  @gpu_decorator(duration=300)
26
  def generate_analysis(prompt, max_length=1024, device=None):
 
21
  tokenizer = AutoTokenizer.from_pretrained(model_path)
22
  model = AutoModelForCausalLM.from_pretrained(model_path)
23
 
24
+ ###############################
25
+
26
+
27
+ # bioprocess_model.py
28
+
29
+ import numpy as np
30
+ import pandas as pd
31
+ import matplotlib.pyplot as plt
32
+ from scipy.integrate import odeint
33
+ from scipy.optimize import curve_fit
34
+ from sklearn.metrics import mean_squared_error
35
+ import seaborn as sns
36
+
37
+ class BioprocessModel:
38
+ def __init__(self):
39
+ self.params = {}
40
+ self.r2 = {}
41
+ self.rmse = {}
42
+ self.datax = []
43
+ self.datas = []
44
+ self.datap = []
45
+ self.dataxp = []
46
+ self.datasp = []
47
+ self.datapp = []
48
+ self.datax_std = []
49
+ self.datas_std = []
50
+ self.datap_std = []
51
+
52
+ @staticmethod
53
+ def logistic(time, xo, xm, um):
54
+ return (xo * np.exp(um * time)) / (1 - (xo / xm) * (1 - np.exp(um * time)))
55
+
56
+ @staticmethod
57
+ def substrate(time, so, p, q, xo, xm, um):
58
+ return so - (p * xo * ((np.exp(um * time)) / (1 - (xo / xm) * (1 - np.exp(um * time))) - 1)) - \
59
+ (q * (xm / um) * np.log(1 - (xo / xm) * (1 - np.exp(um * time))))
60
+
61
+ @staticmethod
62
+ def product(time, po, alpha, beta, xo, xm, um):
63
+ return po + (alpha * xo * ((np.exp(um * time) / (1 - (xo / xm) * (1 - np.exp(um * time)))) - 1)) + \
64
+ (beta * (xm / um) * np.log(1 - (xo / xm) * (1 - np.exp(um * time))))
65
+
66
+ @staticmethod
67
+ def logistic_diff(X, t, params):
68
+ xo, xm, um = params
69
+ dXdt = um * X * (1 - X / xm)
70
+ return dXdt
71
+
72
+ def substrate_diff(self, S, t, params, biomass_params, X_func):
73
+ so, p, q = params
74
+ xo, xm, um = biomass_params
75
+ X_t = X_func(t)
76
+ dSdt = -p * (um * X_t * (1 - X_t / xm)) - q * X_t
77
+ return dSdt
78
+
79
+ def product_diff(self, P, t, params, biomass_params, X_func):
80
+ po, alpha, beta = params
81
+ xo, xm, um = biomass_params
82
+ X_t = X_func(t)
83
+ dPdt = alpha * (um * X_t * (1 - X_t / xm)) + beta * X_t
84
+ return dPdt
85
+
86
+ def process_data(self, df):
87
+ biomass_cols = [col for col in df.columns if 'Biomasa' in col]
88
+ substrate_cols = [col for col in df.columns if 'Sustrato' in col]
89
+ product_cols = [col for col in df.columns if 'Producto' in col]
90
+
91
+ time_col = [col for col in df.columns if 'Tiempo' in col][0]
92
+ time = df[time_col].values
93
+
94
+ data_biomass = np.array([df[col].values for col in biomass_cols])
95
+ self.datax.append(data_biomass)
96
+ self.dataxp.append(np.mean(data_biomass, axis=0))
97
+ self.datax_std.append(np.std(data_biomass, axis=0, ddof=1))
98
+
99
+ data_substrate = np.array([df[col].values for col in substrate_cols])
100
+ self.datas.append(data_substrate)
101
+ self.datasp.append(np.mean(data_substrate, axis=0))
102
+ self.datas_std.append(np.std(data_substrate, axis=0, ddof=1))
103
+
104
+ data_product = np.array([df[col].values for col in product_cols])
105
+ self.datap.append(data_product)
106
+ self.datapp.append(np.mean(data_product, axis=0))
107
+ self.datap_std.append(np.std(data_product, axis=0, ddof=1))
108
+
109
+ self.time = time
110
+
111
+ def fit_model(self, model_type='logistic'):
112
+ if model_type == 'logistic':
113
+ self.fit_biomass = self.fit_biomass_logistic
114
+ self.fit_substrate = self.fit_substrate_logistic
115
+ self.fit_product = self.fit_product_logistic
116
+
117
+ def fit_biomass_logistic(self, time, biomass, bounds):
118
+ popt, _ = curve_fit(self.logistic, time, biomass, bounds=bounds, maxfev=10000)
119
+ self.params['biomass'] = {'xo': popt[0], 'xm': popt[1], 'um': popt[2]}
120
+ y_pred = self.logistic(time, *popt)
121
+ self.r2['biomass'] = 1 - (np.sum((biomass - y_pred) ** 2) / np.sum((biomass - np.mean(biomass)) ** 2))
122
+ self.rmse['biomass'] = np.sqrt(mean_squared_error(biomass, y_pred))
123
+ return y_pred
124
+
125
+ def fit_substrate_logistic(self, time, substrate, biomass_params, bounds):
126
+ popt, _ = curve_fit(lambda t, so, p, q: self.substrate(t, so, p, q, *biomass_params.values()),
127
+ time, substrate, bounds=bounds)
128
+ self.params['substrate'] = {'so': popt[0], 'p': popt[1], 'q': popt[2]}
129
+ y_pred = self.substrate(time, *popt, *biomass_params.values())
130
+ self.r2['substrate'] = 1 - (np.sum((substrate - y_pred) ** 2) / np.sum((substrate - np.mean(substrate)) ** 2))
131
+ self.rmse['substrate'] = np.sqrt(mean_squared_error(substrate, y_pred))
132
+ return y_pred
133
+
134
+ def fit_product_logistic(self, time, product, biomass_params, bounds):
135
+ popt, _ = curve_fit(lambda t, po, alpha, beta: self.product(t, po, alpha, beta, *biomass_params.values()),
136
+ time, product, bounds=bounds)
137
+ self.params['product'] = {'po': popt[0], 'alpha': popt[1], 'beta': popt[2]}
138
+ y_pred = self.product(time, *popt, *biomass_params.values())
139
+ self.r2['product'] = 1 - (np.sum((product - y_pred) ** 2) / np.sum((product - np.mean(product)) ** 2))
140
+ self.rmse['product'] = np.sqrt(mean_squared_error(product, y_pred))
141
+ return y_pred
142
+
143
+ def plot_combined_results(self, time, biomass, substrate, product,
144
+ y_pred_biomass, y_pred_substrate, y_pred_product,
145
+ biomass_std=None, substrate_std=None, product_std=None,
146
+ experiment_name='', legend_position='best', params_position='upper right',
147
+ show_legend=True, show_params=True,
148
+ style='whitegrid', line_color='#0000FF', point_color='#000000',
149
+ line_style='-', marker_style='o'):
150
+ sns.set_style(style)
151
+
152
+ fig, ax1 = plt.subplots(figsize=(10, 7))
153
+ ax1.set_xlabel('Tiempo')
154
+ ax1.set_ylabel('Biomasa', color=line_color)
155
+
156
+ ax1.plot(time, biomass, marker=marker_style, linestyle='', color=point_color, label='Biomasa (Datos)')
157
+ ax1.plot(time, y_pred_biomass, linestyle=line_style, color=line_color, label='Biomasa (Modelo)')
158
+ ax1.tick_params(axis='y', labelcolor=line_color)
159
+
160
+ ax2 = ax1.twinx()
161
+ ax2.set_ylabel('Sustrato', color='green')
162
+ ax2.plot(time, substrate, marker=marker_style, linestyle='', color='green', label='Sustrato (Datos)')
163
+ ax2.plot(time, y_pred_substrate, linestyle=line_style, color='green', label='Sustrato (Modelo)')
164
+ ax2.tick_params(axis='y', labelcolor='green')
165
+
166
+ ax3 = ax1.twinx()
167
+ ax3.spines["right"].set_position(("axes", 1.1))
168
+ ax3.set_ylabel('Producto', color='red')
169
+ ax3.plot(time, product, marker=marker_style, linestyle='', color='red', label='Producto (Datos)')
170
+ ax3.plot(time, y_pred_product, linestyle=line_style, color='red', label='Producto (Modelo)')
171
+ ax3.tick_params(axis='y', labelcolor='red')
172
+
173
+ fig.tight_layout()
174
+ return fig
175
+
176
+
177
+
178
+ ###############################
179
+
180
  # Decorador GPU aplicado para manejar la ejecuci贸n en GPU si est谩 disponible
181
  @gpu_decorator(duration=300)
182
  def generate_analysis(prompt, max_length=1024, device=None):