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:29:43, 5.39s/it]
0%| | 2/1000 [00:05<38:11, 2.30s/it]
0%| | 4/1000 [00:05<14:48, 1.12it/s]
1%| | 9/1000 [00:05<04:50, 3.41it/s]
2%|▏ | 17/1000 [00:05<02:02, 8.00it/s]
2%|▎ | 25/1000 [00:06<01:11, 13.68it/s]
3%|▎ | 33/1000 [00:06<00:49, 19.40it/s]
4%|▍ | 41/1000 [00:06<00:35, 26.70it/s]
5%|▌ | 51/1000 [00:06<00:25, 37.35it/s]
6%|▌ | 61/1000 [00:06<00:19, 47.27it/s]
7%|▋ | 69/1000 [00:06<00:18, 50.35it/s]
8%|▊ | 81/1000 [00:06<00:15, 60.40it/s]
9%|▉ | 89/1000 [00:06<00:14, 63.09it/s]
10%|▉ | 97/1000 [00:06<00:13, 64.85it/s]
10%|█ | 105/1000 [00:07<00:13, 67.94it/s]
11%|█▏ | 114/1000 [00:07<00:12, 73.02it/s]
12%|█▎ | 125/1000 [00:07<00:11, 77.68it/s]
13%|█▎ | 134/1000 [00:07<00:11, 76.10it/s]
14%|█▍ | 145/1000 [00:07<00:11, 76.81it/s]
16%|█▌ | 156/1000 [00:07<00:10, 81.80it/s]
16%|█▋ | 165/1000 [00:07<00:10, 79.99it/s]
17%|█▋ | 174/1000 [00:07<00:10, 82.18it/s]
18%|█▊ | 183/1000 [00:08<00:09, 82.27it/s]
19%|█▉ | 192/1000 [00:08<00:09, 83.81it/s]
20%|██ | 201/1000 [00:08<00:11, 71.87it/s]
21%|██▏ | 213/1000 [00:08<00:10, 77.16it/s]
22%|██▎ | 225/1000 [00:08<00:09, 82.77it/s]
23%|██▎ | 234/1000 [00:08<00:09, 84.35it/s]
24%|██▍ | 243/1000 [00:08<00:09, 81.86it/s]
25%|██▌ | 253/1000 [00:08<00:08, 83.08it/s]
26%|██▌ | 262/1000 [00:09<00:09, 77.36it/s]
27%|██▋ | 271/1000 [00:09<00:09, 78.59it/s]
28%|██▊ | 281/1000 [00:09<00:09, 79.29it/s]
29%|██▉ | 289/1000 [00:09<00:09, 73.99it/s]
30%|███ | 301/1000 [00:09<00:08, 80.62it/s]
31%|███ | 310/1000 [00:09<00:08, 81.84it/s]
32%|███▏ | 319/1000 [00:09<00:08, 81.62it/s]
33%|███▎ | 328/1000 [00:09<00:08, 83.23it/s]
34%|███▎ | 337/1000 [00:09<00:08, 76.09it/s]
35%|███▍ | 348/1000 [00:10<00:07, 84.06it/s]
36%|███▌ | 357/1000 [00:10<00:07, 82.85it/s]
37%|███▋ | 366/1000 [00:10<00:08, 76.54it/s]
38%|███▊ | 375/1000 [00:10<00:08, 75.85it/s]
38%|███▊ | 385/1000 [00:10<00:07, 78.90it/s]
40%|███▉ | 397/1000 [00:10<00:07, 84.27it/s]
41%|████ | 409/1000 [00:10<00:06, 86.74it/s]
42%|████▏ | 418/1000 [00:10<00:06, 85.14it/s]
43%|████▎ | 428/1000 [00:11<00:06, 88.93it/s]
44%|████▎ | 437/1000 [00:11<00:06, 86.56it/s]
45%|████▍ | 446/1000 [00:11<00:06, 85.53it/s]
46%|████▌ | 457/1000 [00:11<00:06, 85.77it/s]
47%|████▋ | 466/1000 [00:11<00:06, 86.42it/s]
48%|████▊ | 477/1000 [00:11<00:05, 90.77it/s]
49%|████▊ | 487/1000 [00:11<00:05, 88.98it/s]
50%|████▉ | 497/1000 [00:11<00:05, 87.96it/s]
51%|█████ | 506/1000 [00:11<00:05, 88.04it/s]
52%|█████▏ | 517/1000 [00:12<00:05, 89.52it/s]
53%|█████▎ | 529/1000 [00:12<00:05, 91.41it/s]
54%|█████▍ | 540/1000 [00:12<00:04, 94.92it/s]
55%|█████▌ | 550/1000 [00:12<00:04, 91.57it/s]
56%|█████▌ | 560/1000 [00:12<00:04, 92.75it/s]
57%|█████▋ | 570/1000 [00:12<00:04, 88.52it/s]
58%|█████▊ | 579/1000 [00:12<00:04, 85.00it/s]
59%|█████▉ | 589/1000 [00:12<00:04, 86.60it/s]
60%|██████ | 601/1000 [00:12<00:04, 88.56it/s]
61%|██████▏ | 613/1000 [00:13<00:04, 91.02it/s]
62%|██████▏ | 623/1000 [00:13<00:04, 93.28it/s]
63%|██████▎ | 633/1000 [00:13<00:04, 82.66it/s]
64%|██████▍ | 645/1000 [00:13<00:04, 85.85it/s]
66%|██████▌ | 655/1000 [00:13<00:03, 88.81it/s]
66%|██████▋ | 665/1000 [00:13<00:03, 88.93it/s]
68%|██████▊ | 677/1000 [00:13<00:03, 92.00it/s]
69%|██████▊ | 687/1000 [00:13<00:03, 89.42it/s]
70%|██████▉ | 697/1000 [00:14<00:03, 85.47it/s]
71%|███████ | 709/1000 [00:14<00:03, 89.04it/s]
72%|███████▏ | 718/1000 [00:14<00:03, 84.12it/s]
73%|███████▎ | 729/1000 [00:14<00:03, 82.25it/s]
74%|███████▍ | 738/1000 [00:14<00:03, 79.26it/s]
75%|███████▍ | 746/1000 [00:14<00:03, 77.77it/s]
75%|███████▌ | 754/1000 [00:14<00:03, 78.01it/s]
76%|███████▋ | 765/1000 [00:14<00:03, 77.80it/s]
78%|███████▊ | 777/1000 [00:15<00:02, 81.83it/s]
79%|███████▊ | 786/1000 [00:15<00:02, 83.31it/s]
80%|███████▉ | 795/1000 [00:15<00:02, 80.40it/s]
80%|████████ | 805/1000 [00:15<00:02, 82.09it/s]
82%|████████▏ | 817/1000 [00:15<00:02, 87.29it/s]
83%|████████▎ | 826/1000 [00:15<00:02, 82.47it/s]
84%|████████▎ | 837/1000 [00:15<00:01, 87.92it/s]
85%|████████▍ | 847/1000 [00:15<00:01, 88.85it/s]
86%|████████▌ | 856/1000 [00:15<00:01, 88.00it/s]
86%|████████▋ | 865/1000 [00:16<00:01, 83.73it/s]
88%|████████▊ | 877/1000 [00:16<00:01, 88.52it/s]
89%|████████▉ | 889/1000 [00:16<00:01, 88.75it/s]
90%|█████████ | 901/1000 [00:16<00:01, 94.58it/s]
91%|█████████ | 911/1000 [00:16<00:00, 94.83it/s]
92%|█████████▏| 921/1000 [00:16<00:00, 95.21it/s]
93%|█████████▎| 932/1000 [00:16<00:00, 99.12it/s]
94%|█████████▍| 942/1000 [00:16<00:00, 96.88it/s]
95%|█████████▌| 953/1000 [00:17<00:00, 98.00it/s]
96%|█████████▋| 965/1000 [00:17<00:00, 99.29it/s]
98%|█████████▊| 977/1000 [00:17<00:00, 99.54it/s]
99%|█████████▉| 993/1000 [00:17<00:00, 108.37it/s]
100%|██████████| 1000/1000 [00:17<00:00, 57.45it/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!