Fuzzy fitting
In [1]:
Copied!
import numpy as np
from example_models import get_upper_glycolysis
from mxlpy import Simulator, plot
from mxlpy.fuzzy import ThompsonState, thompson_sampling
# Create artificial data
p_orig = {
"k1": 0.25,
"k2": 1.2,
"k3": 1.6,
"k3m": 0.5,
"k4": 100,
"k5": 1.3,
"k6": 1.5,
"k7": 2.5,
}
m = get_upper_glycolysis().update_parameters(p_orig)
data, _ = (
Simulator(m)
.simulate_time_course(np.linspace(0, 10, 11, dtype=float))
.get_result()
.unwrap_or_err()
)
# Perform sampling
state = thompson_sampling(
m,
data=data,
state=ThompsonState.from_parameter_values(
{
"k1": np.geomspace(1e-1, 1e2, 21),
"k2": np.geomspace(1e-1, 1e2, 21),
"k3": np.geomspace(1e-1, 1e2, 21),
"k3m": np.geomspace(1e-1, 1e2, 21),
"k4": np.geomspace(1e-1, 1e2, 21),
"k5": np.geomspace(1e-1, 1e2, 21),
"k6": np.geomspace(1e-1, 1e2, 21),
"k7": np.geomspace(1e-1, 1e2, 21),
}
),
rtol=0.11,
n=1000,
parallel=True,
)
import numpy as np
from example_models import get_upper_glycolysis
from mxlpy import Simulator, plot
from mxlpy.fuzzy import ThompsonState, thompson_sampling
# Create artificial data
p_orig = {
"k1": 0.25,
"k2": 1.2,
"k3": 1.6,
"k3m": 0.5,
"k4": 100,
"k5": 1.3,
"k6": 1.5,
"k7": 2.5,
}
m = get_upper_glycolysis().update_parameters(p_orig)
data, _ = (
Simulator(m)
.simulate_time_course(np.linspace(0, 10, 11, dtype=float))
.get_result()
.unwrap_or_err()
)
# Perform sampling
state = thompson_sampling(
m,
data=data,
state=ThompsonState.from_parameter_values(
{
"k1": np.geomspace(1e-1, 1e2, 21),
"k2": np.geomspace(1e-1, 1e2, 21),
"k3": np.geomspace(1e-1, 1e2, 21),
"k3m": np.geomspace(1e-1, 1e2, 21),
"k4": np.geomspace(1e-1, 1e2, 21),
"k5": np.geomspace(1e-1, 1e2, 21),
"k6": np.geomspace(1e-1, 1e2, 21),
"k7": np.geomspace(1e-1, 1e2, 21),
}
),
rtol=0.11,
n=1000,
parallel=True,
)
0%| | 0/1000 [00:00<?, ?it/s]
0%| | 1/1000 [00:05<1:35:02, 5.71s/it]
0%| | 5/1000 [00:05<14:39, 1.13it/s]
1%| | 11/1000 [00:05<05:20, 3.09it/s]
2%|▏ | 18/1000 [00:06<02:41, 6.09it/s]
2%|▎ | 25/1000 [00:06<01:38, 9.90it/s]
3%|▎ | 33/1000 [00:06<01:04, 14.95it/s]
4%|▍ | 41/1000 [00:06<00:44, 21.34it/s]
5%|▍ | 49/1000 [00:06<00:33, 28.06it/s]
6%|▌ | 61/1000 [00:06<00:23, 39.59it/s]
7%|▋ | 70/1000 [00:06<00:19, 47.92it/s]
8%|▊ | 79/1000 [00:06<00:16, 55.49it/s]
9%|▉ | 89/1000 [00:07<00:14, 61.71it/s]
10%|▉ | 97/1000 [00:07<00:14, 60.83it/s]
11%|█ | 106/1000 [00:07<00:13, 65.17it/s]
11%|█▏ | 114/1000 [00:07<00:12, 68.68it/s]
12%|█▎ | 125/1000 [00:07<00:11, 74.41it/s]
13%|█▎ | 133/1000 [00:07<00:11, 73.77it/s]
14%|█▍ | 143/1000 [00:07<00:10, 80.44it/s]
15%|█▌ | 152/1000 [00:07<00:10, 77.65it/s]
16%|█▌ | 161/1000 [00:07<00:11, 70.34it/s]
17%|█▋ | 173/1000 [00:08<00:10, 80.49it/s]
18%|█▊ | 182/1000 [00:08<00:10, 80.49it/s]
19%|█▉ | 191/1000 [00:08<00:10, 77.74it/s]
20%|██ | 201/1000 [00:08<00:10, 78.10it/s]
21%|██▏ | 213/1000 [00:08<00:09, 86.34it/s]
22%|██▏ | 222/1000 [00:08<00:09, 86.09it/s]
23%|██▎ | 233/1000 [00:08<00:08, 86.17it/s]
24%|██▍ | 242/1000 [00:08<00:09, 82.17it/s]
25%|██▌ | 252/1000 [00:09<00:08, 86.54it/s]
26%|██▌ | 261/1000 [00:09<00:09, 75.98it/s]
27%|██▋ | 273/1000 [00:09<00:08, 83.40it/s]
28%|██▊ | 282/1000 [00:09<00:09, 75.64it/s]
29%|██▉ | 293/1000 [00:09<00:08, 78.92it/s]
30%|███ | 302/1000 [00:09<00:08, 78.24it/s]
31%|███▏ | 313/1000 [00:09<00:08, 83.56it/s]
32%|███▎ | 325/1000 [00:09<00:08, 83.97it/s]
34%|███▎ | 335/1000 [00:10<00:07, 87.75it/s]
34%|███▍ | 344/1000 [00:10<00:07, 85.89it/s]
35%|███▌ | 353/1000 [00:10<00:08, 80.54it/s]
36%|███▋ | 364/1000 [00:10<00:07, 88.12it/s]
37%|███▋ | 373/1000 [00:10<00:07, 81.98it/s]
38%|███▊ | 382/1000 [00:10<00:07, 82.38it/s]
39%|███▉ | 391/1000 [00:10<00:07, 81.41it/s]
40%|████ | 400/1000 [00:10<00:07, 82.69it/s]
41%|████ | 409/1000 [00:10<00:07, 74.34it/s]
42%|████▏ | 417/1000 [00:11<00:07, 74.07it/s]
42%|████▎ | 425/1000 [00:11<00:07, 74.80it/s]
44%|████▎ | 435/1000 [00:11<00:07, 78.84it/s]
44%|████▍ | 444/1000 [00:11<00:06, 81.11it/s]
45%|████▌ | 453/1000 [00:11<00:07, 75.65it/s]
46%|████▌ | 461/1000 [00:11<00:07, 72.04it/s]
47%|████▋ | 470/1000 [00:11<00:06, 76.44it/s]
48%|████▊ | 478/1000 [00:11<00:07, 72.96it/s]
49%|████▉ | 489/1000 [00:12<00:06, 79.11it/s]
50%|████▉ | 498/1000 [00:12<00:06, 81.22it/s]
51%|█████ | 509/1000 [00:12<00:06, 77.43it/s]
52%|█████▏ | 517/1000 [00:12<00:06, 76.18it/s]
52%|█████▎ | 525/1000 [00:12<00:06, 74.39it/s]
53%|█████▎ | 533/1000 [00:12<00:06, 72.98it/s]
54%|█████▍ | 541/1000 [00:12<00:06, 73.90it/s]
55%|█████▌ | 550/1000 [00:12<00:05, 77.43it/s]
56%|█████▌ | 560/1000 [00:12<00:05, 83.55it/s]
57%|█████▋ | 569/1000 [00:13<00:05, 76.30it/s]
58%|█████▊ | 577/1000 [00:13<00:05, 71.40it/s]
58%|█████▊ | 585/1000 [00:13<00:06, 67.85it/s]
60%|█████▉ | 597/1000 [00:13<00:05, 75.78it/s]
60%|██████ | 605/1000 [00:13<00:05, 73.48it/s]
61%|██████▏ | 613/1000 [00:13<00:05, 74.34it/s]
62%|██████▏ | 621/1000 [00:13<00:05, 75.49it/s]
63%|██████▎ | 630/1000 [00:13<00:04, 77.88it/s]
64%|██████▍ | 638/1000 [00:14<00:04, 74.07it/s]
65%|██████▍ | 649/1000 [00:14<00:04, 78.65it/s]
66%|██████▌ | 658/1000 [00:14<00:04, 79.29it/s]
67%|██████▋ | 667/1000 [00:14<00:04, 81.69it/s]
68%|██████▊ | 676/1000 [00:14<00:03, 83.82it/s]
68%|██████▊ | 685/1000 [00:14<00:04, 77.31it/s]
69%|██████▉ | 694/1000 [00:14<00:03, 79.15it/s]
70%|███████ | 703/1000 [00:14<00:03, 81.80it/s]
71%|███████ | 712/1000 [00:14<00:03, 83.17it/s]
72%|███████▏ | 721/1000 [00:15<00:03, 73.36it/s]
73%|███████▎ | 733/1000 [00:15<00:03, 78.03it/s]
74%|███████▍ | 742/1000 [00:15<00:03, 79.65it/s]
75%|███████▌ | 751/1000 [00:15<00:03, 78.99it/s]
76%|███████▌ | 761/1000 [00:15<00:03, 75.36it/s]
77%|███████▋ | 772/1000 [00:15<00:02, 83.78it/s]
78%|███████▊ | 781/1000 [00:15<00:02, 78.25it/s]
79%|███████▉ | 790/1000 [00:15<00:02, 75.35it/s]
80%|████████ | 801/1000 [00:16<00:02, 77.21it/s]
81%|████████▏ | 813/1000 [00:16<00:02, 82.31it/s]
82%|████████▎ | 825/1000 [00:16<00:02, 86.86it/s]
83%|████████▎ | 834/1000 [00:16<00:01, 83.16it/s]
84%|████████▍ | 843/1000 [00:16<00:01, 81.27it/s]
85%|████████▌ | 853/1000 [00:16<00:01, 80.72it/s]
86%|████████▋ | 864/1000 [00:16<00:01, 87.20it/s]
87%|████████▋ | 873/1000 [00:16<00:01, 80.52it/s]
88%|████████▊ | 882/1000 [00:17<00:01, 76.64it/s]
89%|████████▉ | 890/1000 [00:17<00:01, 72.47it/s]
90%|████████▉ | 898/1000 [00:17<00:01, 69.80it/s]
91%|█████████ | 907/1000 [00:17<00:01, 74.86it/s]
92%|█████████▏| 917/1000 [00:17<00:01, 72.41it/s]
93%|█████████▎| 929/1000 [00:17<00:00, 80.24it/s]
94%|█████████▍| 941/1000 [00:17<00:00, 86.23it/s]
95%|█████████▌| 950/1000 [00:17<00:00, 86.32it/s]
96%|█████████▌| 959/1000 [00:18<00:00, 78.14it/s]
97%|█████████▋| 967/1000 [00:18<00:00, 74.57it/s]
98%|█████████▊| 975/1000 [00:18<00:00, 73.70it/s]
98%|█████████▊| 984/1000 [00:18<00:00, 77.90it/s]
99%|█████████▉| 992/1000 [00:18<00:00, 72.06it/s]
100%|██████████| 1000/1000 [00:18<00:00, 72.62it/s]
100%|██████████| 1000/1000 [00:18<00:00, 53.68it/s]
In [2]:
Copied!
from scipy.interpolate import CubicSpline
# Visualize results
fig, axs = plot.grid_layout(len(state.state), n_cols=4)
ai = iter(axs)
for ax, (name, var) in zip(axs, state.state.items(), strict=False):
ax.set(title=f"{name}")
xticks = np.arange(len(var["x"]))
ax.bar(xticks, var["success"] / var["fail"])
ax.set_xticks(xticks)
ax.set_xticklabels((f"{i:.0e}" for i in var["x"]), rotation=90)
x_trans = CubicSpline(var["x"], xticks)
x_data = x_trans(p_orig[name])
y_data = ax.transData.inverted().transform(ax.transAxes.transform([0, 0.5]))[1]
ax.annotate(
p_orig[name],
xy=(x_data, 0), # type: ignore
xytext=(x_data, y_data), # type: ignore
ha="center",
arrowprops={"arrowstyle": "->"},
)
from scipy.interpolate import CubicSpline
# Visualize results
fig, axs = plot.grid_layout(len(state.state), n_cols=4)
ai = iter(axs)
for ax, (name, var) in zip(axs, state.state.items(), strict=False):
ax.set(title=f"{name}")
xticks = np.arange(len(var["x"]))
ax.bar(xticks, var["success"] / var["fail"])
ax.set_xticks(xticks)
ax.set_xticklabels((f"{i:.0e}" for i in var["x"]), rotation=90)
x_trans = CubicSpline(var["x"], xticks)
x_data = x_trans(p_orig[name])
y_data = ax.transData.inverted().transform(ax.transAxes.transform([0, 0.5]))[1]
ax.annotate(
p_orig[name],
xy=(x_data, 0), # type: ignore
xytext=(x_data, y_data), # type: ignore
ha="center",
arrowprops={"arrowstyle": "->"},
)
In [ ]:
Copied!
In [ ]:
Copied!