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:00<16:05, 1.04it/s]
1%| | 9/1000 [00:01<01:29, 11.12it/s]
3%|▎ | 29/1000 [00:01<00:24, 40.15it/s]
5%|▌ | 53/1000 [00:01<00:12, 75.11it/s]
8%|▊ | 77/1000 [00:01<00:08, 106.32it/s]
10%|▉ | 97/1000 [00:01<00:07, 126.98it/s]
12%|█▏ | 121/1000 [00:01<00:05, 150.17it/s]
14%|█▍ | 145/1000 [00:01<00:04, 171.44it/s]
17%|█▋ | 169/1000 [00:01<00:04, 179.47it/s]
19%|█▉ | 193/1000 [00:01<00:04, 189.44it/s]
21%|██▏ | 214/1000 [00:02<00:04, 194.66it/s]
24%|██▎ | 235/1000 [00:02<00:03, 192.01it/s]
26%|██▌ | 256/1000 [00:02<00:03, 196.64it/s]
28%|██▊ | 277/1000 [00:02<00:03, 197.33it/s]
30%|██▉ | 299/1000 [00:02<00:03, 203.70it/s]
32%|███▏ | 320/1000 [00:02<00:03, 205.02it/s]
34%|███▍ | 341/1000 [00:02<00:03, 204.35it/s]
36%|███▋ | 365/1000 [00:02<00:03, 205.85it/s]
39%|███▊ | 386/1000 [00:02<00:02, 206.24it/s]
41%|████ | 409/1000 [00:02<00:02, 212.29it/s]
43%|████▎ | 433/1000 [00:03<00:02, 211.84it/s]
46%|████▌ | 455/1000 [00:03<00:02, 213.97it/s]
48%|████▊ | 477/1000 [00:03<00:02, 215.00it/s]
50%|█████ | 501/1000 [00:03<00:02, 215.24it/s]
52%|█████▎ | 525/1000 [00:03<00:02, 215.56it/s]
55%|█████▍ | 549/1000 [00:03<00:02, 215.61it/s]
57%|█████▋ | 573/1000 [00:03<00:01, 214.56it/s]
60%|█████▉ | 595/1000 [00:03<00:01, 211.76it/s]
62%|██████▏ | 617/1000 [00:03<00:01, 202.45it/s]
64%|██████▍ | 641/1000 [00:04<00:01, 203.75it/s]
66%|██████▋ | 665/1000 [00:04<00:01, 207.20it/s]
69%|██████▉ | 689/1000 [00:04<00:01, 210.15it/s]
71%|███████ | 711/1000 [00:04<00:01, 207.46it/s]
73%|███████▎ | 733/1000 [00:04<00:01, 207.40it/s]
76%|███████▌ | 757/1000 [00:04<00:01, 214.66it/s]
78%|███████▊ | 780/1000 [00:04<00:01, 218.03it/s]
80%|████████ | 804/1000 [00:04<00:00, 224.01it/s]
83%|████████▎ | 827/1000 [00:04<00:00, 217.19it/s]
85%|████████▌ | 850/1000 [00:05<00:00, 217.49it/s]
87%|████████▋ | 873/1000 [00:05<00:00, 212.46it/s]
90%|████████▉ | 897/1000 [00:05<00:00, 208.51it/s]
92%|█████████▏| 918/1000 [00:05<00:00, 208.04it/s]
94%|█████████▍| 941/1000 [00:05<00:00, 213.89it/s]
96%|█████████▋| 965/1000 [00:05<00:00, 219.65it/s]
99%|█████████▉| 989/1000 [00:05<00:00, 220.37it/s]
100%|██████████| 1000/1000 [00:05<00:00, 174.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!