File size: 1,942 Bytes
6c9fc9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# models.py

import numpy as np
from scipy.optimize import curve_fit
from sympy import symbols, sympify, lambdify
import warnings

class BioprocessModel:
    def __init__(self):
        self.params = {}
        self.models = {}
        self.r2 = {}
        self.rmse = {}

    def set_model(self, model_type, equation_str, param_str):
        equation_str = equation_str.strip()
        if '=' in equation_str:
            equation_str = equation_str.split('=', 1)[1].strip()

        params = [param.strip() for param in param_str.split(',')]
        self.models[model_type] = {
            'equation_str': equation_str,
            'params': params
        }

        t = symbols('t')
        param_symbols = symbols(params)
        expr = sympify(equation_str)
        func = lambdify((t, *param_symbols), expr, 'numpy')
        self.models[model_type]['function'] = func

    def fit_model(self, model_type, time, data, bounds):
        func = self.models[model_type]['function']
        params = self.models[model_type]['params']

        p0 = np.ones(len(params))
        lower_bounds, upper_bounds = bounds

        lower_bounds = np.array(lower_bounds)
        upper_bounds = np.array(upper_bounds)

        if len(lower_bounds) != len(params):
            lower_bounds = np.full(len(params), -np.inf)
        if len(upper_bounds) != len(params):
            upper_bounds = np.full(len(params), np.inf)

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            popt, _ = curve_fit(func, time, data, p0=p0, bounds=(lower_bounds, upper_bounds), maxfev=10000)

        self.params[model_type] = dict(zip(params, popt))
        y_pred = func(time, *popt)
        ss_res = np.sum((data - y_pred) ** 2)
        ss_tot = np.sum((data - np.mean(data)) ** 2)
        self.r2[model_type] = 1 - (ss_res / ss_tot)
        self.rmse[model_type] = np.sqrt(np.mean((data - y_pred) ** 2))
        return y_pred