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:03<1:05:01, 3.91s/it]
1%| | 8/1000 [00:04<06:05, 2.72it/s]
1%|▏ | 13/1000 [00:04<03:34, 4.61it/s]
2%|▏ | 21/1000 [00:04<02:12, 7.39it/s]
6%|▌ | 59/1000 [00:04<00:29, 31.45it/s]
9%|▉ | 92/1000 [00:04<00:16, 56.65it/s]
13%|█▎ | 133/1000 [00:05<00:09, 94.16it/s]
18%|█▊ | 177/1000 [00:05<00:05, 138.57it/s]
21%|██ | 209/1000 [00:05<00:05, 154.37it/s]
26%|██▌ | 256/1000 [00:05<00:03, 208.77it/s]
30%|███ | 301/1000 [00:05<00:02, 255.97it/s]
34%|███▍ | 339/1000 [00:05<00:02, 279.82it/s]
38%|███▊ | 382/1000 [00:05<00:01, 315.01it/s]
42%|████▏ | 422/1000 [00:05<00:01, 335.93it/s]
47%|████▋ | 466/1000 [00:05<00:01, 362.71it/s]
51%|█████ | 512/1000 [00:06<00:01, 388.68it/s]
55%|█████▌ | 554/1000 [00:06<00:01, 394.79it/s]
60%|█████▉ | 598/1000 [00:06<00:00, 406.44it/s]
64%|██████▍ | 645/1000 [00:06<00:00, 422.32it/s]
69%|██████▉ | 689/1000 [00:06<00:00, 421.64it/s]
73%|███████▎ | 732/1000 [00:06<00:00, 421.55it/s]
78%|███████▊ | 778/1000 [00:06<00:00, 432.10it/s]
82%|████████▏ | 822/1000 [00:06<00:00, 430.59it/s]
87%|████████▋ | 872/1000 [00:06<00:00, 449.51it/s]
92%|█████████▏| 918/1000 [00:06<00:00, 448.05it/s]
97%|█████████▋| 969/1000 [00:07<00:00, 447.74it/s]
1012it [00:07, 141.38it/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!