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:26:32, 5.20s/it]
0%| | 5/1000 [00:05<13:26, 1.23it/s]
1%|▏ | 13/1000 [00:05<04:03, 4.05it/s]
2%|▏ | 21/1000 [00:05<02:06, 7.72it/s]
3%|▎ | 33/1000 [00:05<01:05, 14.87it/s]
4%|▍ | 41/1000 [00:05<00:47, 20.20it/s]
5%|▌ | 53/1000 [00:05<00:31, 29.97it/s]
6%|▌ | 62/1000 [00:06<00:25, 37.32it/s]
7%|▋ | 73/1000 [00:06<00:19, 46.46it/s]
8%|▊ | 85/1000 [00:06<00:16, 56.80it/s]
9%|▉ | 94/1000 [00:06<00:14, 63.18it/s]
10%|█ | 105/1000 [00:06<00:12, 68.86it/s]
12%|█▏ | 117/1000 [00:06<00:11, 74.88it/s]
13%|█▎ | 126/1000 [00:06<00:11, 75.67it/s]
14%|█▎ | 135/1000 [00:06<00:11, 73.13it/s]
14%|█▍ | 145/1000 [00:07<00:11, 73.41it/s]
16%|█▌ | 157/1000 [00:07<00:10, 81.45it/s]
17%|█▋ | 166/1000 [00:07<00:10, 82.26it/s]
18%|█▊ | 175/1000 [00:07<00:10, 80.66it/s]
18%|█▊ | 185/1000 [00:07<00:09, 82.66it/s]
20%|█▉ | 196/1000 [00:07<00:09, 87.58it/s]
20%|██ | 205/1000 [00:07<00:10, 74.63it/s]
22%|██▏ | 217/1000 [00:07<00:09, 81.94it/s]
23%|██▎ | 229/1000 [00:08<00:09, 82.58it/s]
24%|██▍ | 239/1000 [00:08<00:08, 86.03it/s]
25%|██▍ | 248/1000 [00:08<00:09, 82.34it/s]
26%|██▌ | 257/1000 [00:08<00:09, 75.24it/s]
27%|██▋ | 268/1000 [00:08<00:08, 83.84it/s]
28%|██▊ | 277/1000 [00:08<00:08, 82.98it/s]
29%|██▊ | 286/1000 [00:08<00:08, 84.49it/s]
30%|██▉ | 295/1000 [00:08<00:08, 79.07it/s]
30%|███ | 305/1000 [00:08<00:08, 82.53it/s]
31%|███▏ | 314/1000 [00:09<00:08, 84.08it/s]
32%|███▏ | 323/1000 [00:09<00:08, 81.69it/s]
33%|███▎ | 332/1000 [00:09<00:08, 78.31it/s]
34%|███▍ | 341/1000 [00:09<00:08, 74.59it/s]
35%|███▌ | 353/1000 [00:09<00:08, 79.43it/s]
36%|███▋ | 365/1000 [00:09<00:08, 76.56it/s]
38%|███▊ | 377/1000 [00:09<00:07, 83.68it/s]
39%|███▉ | 389/1000 [00:09<00:06, 89.61it/s]
40%|████ | 401/1000 [00:10<00:06, 95.81it/s]
41%|████ | 411/1000 [00:10<00:06, 86.48it/s]
42%|████▏ | 420/1000 [00:10<00:06, 83.97it/s]
43%|████▎ | 429/1000 [00:10<00:07, 80.70it/s]
44%|████▍ | 438/1000 [00:10<00:06, 80.99it/s]
45%|████▍ | 447/1000 [00:10<00:06, 83.34it/s]
46%|████▌ | 456/1000 [00:10<00:06, 83.91it/s]
46%|████▋ | 465/1000 [00:10<00:06, 83.22it/s]
47%|████▋ | 474/1000 [00:11<00:06, 84.67it/s]
48%|████▊ | 483/1000 [00:11<00:06, 86.07it/s]
49%|████▉ | 493/1000 [00:11<00:05, 87.91it/s]
50%|█████ | 502/1000 [00:11<00:05, 85.41it/s]
51%|█████ | 511/1000 [00:11<00:05, 85.82it/s]
52%|█████▏ | 520/1000 [00:11<00:05, 85.36it/s]
53%|█████▎ | 529/1000 [00:11<00:06, 74.87it/s]
54%|█████▍ | 541/1000 [00:11<00:05, 80.57it/s]
55%|█████▌ | 551/1000 [00:11<00:05, 84.20it/s]
56%|█████▌ | 561/1000 [00:12<00:05, 83.69it/s]
57%|█████▋ | 573/1000 [00:12<00:05, 82.90it/s]
58%|█████▊ | 583/1000 [00:12<00:04, 85.44it/s]
59%|█████▉ | 592/1000 [00:12<00:04, 86.40it/s]
60%|██████ | 601/1000 [00:12<00:05, 79.28it/s]
61%|██████ | 610/1000 [00:12<00:04, 81.67it/s]
62%|██████▏ | 621/1000 [00:12<00:04, 84.18it/s]
63%|██████▎ | 633/1000 [00:12<00:04, 85.94it/s]
64%|██████▍ | 645/1000 [00:13<00:04, 87.53it/s]
66%|██████▌ | 657/1000 [00:13<00:03, 93.01it/s]
67%|██████▋ | 669/1000 [00:13<00:03, 99.35it/s]
68%|██████▊ | 680/1000 [00:13<00:03, 99.78it/s]
69%|██████▉ | 691/1000 [00:13<00:03, 93.53it/s]
70%|███████ | 705/1000 [00:13<00:02, 99.53it/s]
72%|███████▏ | 717/1000 [00:13<00:02, 102.33it/s]
73%|███████▎ | 729/1000 [00:13<00:02, 104.46it/s]
74%|███████▍ | 740/1000 [00:13<00:02, 101.57it/s]
75%|███████▌ | 751/1000 [00:14<00:02, 100.21it/s]
76%|███████▌ | 762/1000 [00:14<00:02, 94.32it/s]
77%|███████▋ | 773/1000 [00:14<00:02, 91.60it/s]
78%|███████▊ | 783/1000 [00:14<00:02, 89.85it/s]
79%|███████▉ | 793/1000 [00:14<00:02, 90.15it/s]
80%|████████ | 803/1000 [00:14<00:02, 85.16it/s]
81%|████████ | 812/1000 [00:14<00:02, 82.72it/s]
82%|████████▏ | 821/1000 [00:14<00:02, 77.87it/s]
83%|████████▎ | 833/1000 [00:15<00:01, 84.29it/s]
84%|████████▍ | 845/1000 [00:15<00:01, 89.98it/s]
86%|████████▌ | 857/1000 [00:15<00:01, 93.75it/s]
87%|████████▋ | 869/1000 [00:15<00:01, 96.56it/s]
88%|████████▊ | 879/1000 [00:15<00:01, 91.36it/s]
89%|████████▉ | 889/1000 [00:15<00:01, 84.62it/s]
90%|█████████ | 901/1000 [00:15<00:01, 88.04it/s]
91%|█████████ | 911/1000 [00:15<00:01, 88.46it/s]
92%|█████████▏| 921/1000 [00:16<00:00, 89.58it/s]
93%|█████████▎| 933/1000 [00:16<00:00, 96.29it/s]
94%|█████████▍| 944/1000 [00:16<00:00, 99.54it/s]
96%|█████████▌| 955/1000 [00:16<00:00, 91.17it/s]
96%|█████████▋| 965/1000 [00:16<00:00, 85.42it/s]
98%|█████████▊| 976/1000 [00:16<00:00, 91.68it/s]
99%|█████████▊| 986/1000 [00:16<00:00, 92.50it/s]
100%|█████████▉| 996/1000 [00:16<00:00, 91.83it/s]
100%|██████████| 1000/1000 [00:16<00:00, 59.29it/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!