sme_contrib.optimize
[1]:
!pip install -q sme_contrib
import sme
import sme_contrib.optimize as smeopt
import sme_contrib.plot as smeplot
import numpy as np
from matplotlib import pyplot as plt
from IPython.display import HTML
from PIL import Image
Gray-Scott model
A simple two-species model with two reaction rate parameters, that forms spatial patterns and eventually reaches a steady state
[2]:
def simulated_gray_scott(f, k):
m = sme.open_example_model("gray-scott")
m.compartments[0].species[
"V"
].analytic_concentration = "exp(-((x-49.5)^2+(y-49.5)^2))"
m.parameters["f"].value = f"{f}"
m.parameters["k"].value = f"{k}"
m.simulate(5000, 50, return_results=False)
return m
def gray_scott_anim(f, k):
gray_scott = simulated_gray_scott(f, k)
return smeplot.concentration_heatmap_animation(
gray_scott.simulation_results(), ["V"]
)
[3]:
anim = gray_scott_anim(0.04, 0.06)
HTML(anim.to_html5_video())
2023-10-02 08:08:42,894 - matplotlib.animation - INFO - Animation.save using <class 'matplotlib.animation.FFMpegWriter'>
2023-10-02 08:08:42,898 - matplotlib.animation - INFO - MovieWriter._run: running command: ffmpeg -f rawvideo -vcodec rawvideo -s 640x480 -pix_fmt rgba -framerate 5.0 -loglevel error -i pipe: -vcodec h264 -pix_fmt yuv420p -y /tmp/tmpsgbx_snl/temp.m4v
[3]:
[4]:
anim = gray_scott_anim(0.051, 0.061)
HTML(anim.to_html5_video())
2023-10-02 08:08:50,234 - matplotlib.animation - INFO - Animation.save using <class 'matplotlib.animation.FFMpegWriter'>
2023-10-02 08:08:50,235 - matplotlib.animation - INFO - MovieWriter._run: running command: ffmpeg -f rawvideo -vcodec rawvideo -s 640x480 -pix_fmt rgba -framerate 5.0 -loglevel error -i pipe: -vcodec h264 -pix_fmt yuv420p -y /tmp/tmpwi0dsxdz/temp.m4v
[4]:
[5]:
anim = gray_scott_anim(0.028, 0.062)
HTML(anim.to_html5_video())
2023-10-02 08:08:57,672 - matplotlib.animation - INFO - Animation.save using <class 'matplotlib.animation.FFMpegWriter'>
2023-10-02 08:08:57,673 - matplotlib.animation - INFO - MovieWriter._run: running command: ffmpeg -f rawvideo -vcodec rawvideo -s 640x480 -pix_fmt rgba -framerate 5.0 -loglevel error -i pipe: -vcodec h264 -pix_fmt yuv420p -y /tmp/tmpa0enihmi/temp.m4v
[5]:
Try to fit to the target steady state
Increasing the number of particles and the number of iterations will improve the fit, but take longer to run.
[6]:
def create_target_image(f, k):
gray_scott = simulated_gray_scott(f, k)
conc = gray_scott.simulation_results()[-1].species_concentration["V"][0, :]
conc = 255 * conc / np.max(conc)
Image.fromarray(conc.astype("uint8")).save("tmp.png")
gray_scott.export_sbml_file("tmp.xml")
[7]:
def apply_params(model, params):
model.parameters["f"].value = f"{params[0]}"
model.parameters["k"].value = f"{params[1]}"
[8]:
create_target_image(0.04, 0.06)
ss = smeopt.SteadyState(
"tmp.xml",
"tmp.png",
["V"],
apply_params,
[0.01, 0.05],
[0.06, 0.07],
5000,
2000,
90,
)
ss.find(5, 5)
ss.plot_all()
2023-10-02 08:09:03,988 - pyswarms.single.global_best - INFO - Optimize for 5 iters with {'c1': 0.5, 'c2': 0.3, 'w': 0.9}
pyswarms.single.global_best: 100%|██████████|5/5, best_cost=9.45
2023-10-02 08:09:18,276 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 9.445471459844493, best pos: [0.0411991 0.06036438]
[9]:
create_target_image(0.051, 0.061)
ss = smeopt.SteadyState(
"tmp.xml",
"tmp.png",
["V"],
apply_params,
[0.01, 0.05],
[0.06, 0.07],
5000,
2000,
90,
)
ss.find(5, 5)
ss.plot_all()
2023-10-02 08:09:21,185 - pyswarms.single.global_best - INFO - Optimize for 5 iters with {'c1': 0.5, 'c2': 0.3, 'w': 0.9}
pyswarms.single.global_best: 100%|██████████|5/5, best_cost=171
2023-10-02 08:09:35,442 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 171.34894002727606, best pos: [0.04917876 0.06013575]
[10]:
create_target_image(0.028, 0.062)
ss = smeopt.SteadyState(
"tmp.xml",
"tmp.png",
["V"],
apply_params,
[0.01, 0.05],
[0.06, 0.07],
5000,
2000,
90,
)
ss.find(5, 5)
ss.plot_all()
2023-10-02 08:09:38,313 - pyswarms.single.global_best - INFO - Optimize for 5 iters with {'c1': 0.5, 'c2': 0.3, 'w': 0.9}
pyswarms.single.global_best: 100%|██████████|5/5, best_cost=252
2023-10-02 08:09:52,532 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 252.40159938485198, best pos: [0.01605637 0.06764789]
[ ]: