colab-icon Interactive online version

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())
2021-09-30 18:37:37,895 - matplotlib.animation - INFO - Animation.save using <class 'matplotlib.animation.FFMpegWriter'>
2021-09-30 18:37:37,898 - matplotlib.animation - INFO - MovieWriter._run: running command: ffmpeg -f rawvideo -vcodec rawvideo -s 432x288 -pix_fmt rgba -r 5.0 -loglevel error -i pipe: -vcodec h264 -pix_fmt yuv420p -y /tmp/tmpslbz2qc6/temp.m4v
[3]:
[4]:
anim = gray_scott_anim(0.051, 0.061)
HTML(anim.to_html5_video())
2021-09-30 18:37:43,545 - matplotlib.animation - INFO - Animation.save using <class 'matplotlib.animation.FFMpegWriter'>
2021-09-30 18:37:43,548 - matplotlib.animation - INFO - MovieWriter._run: running command: ffmpeg -f rawvideo -vcodec rawvideo -s 432x288 -pix_fmt rgba -r 5.0 -loglevel error -i pipe: -vcodec h264 -pix_fmt yuv420p -y /tmp/tmpz54nvaln/temp.m4v
[4]:
[5]:
anim = gray_scott_anim(0.028, 0.062)
HTML(anim.to_html5_video())
2021-09-30 18:37:49,108 - matplotlib.animation - INFO - Animation.save using <class 'matplotlib.animation.FFMpegWriter'>
2021-09-30 18:37:49,110 - matplotlib.animation - INFO - MovieWriter._run: running command: ffmpeg -f rawvideo -vcodec rawvideo -s 432x288 -pix_fmt rgba -r 5.0 -loglevel error -i pipe: -vcodec h264 -pix_fmt yuv420p -y /tmp/tmptsxten8u/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"]
    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()
2021-09-30 18:37:53,815 - 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=14.8
2021-09-30 18:38:07,459 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 14.830282569401321, best pos: [0.0324925  0.05942445]
../_images/notebooks_optimize_10_1.png
../_images/notebooks_optimize_10_2.png
[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()
2021-09-30 18:38:09,996 - 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=106
2021-09-30 18:38:23,582 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 106.48928135796089, best pos: [0.05674477 0.06116345]
../_images/notebooks_optimize_11_1.png
../_images/notebooks_optimize_11_2.png
[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()
2021-09-30 18:38:26,123 - 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
2021-09-30 18:38:39,644 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 252.40159938485198, best pos: [0.01099724 0.05348453]
../_images/notebooks_optimize_12_1.png
../_images/notebooks_optimize_12_2.png
[ ]: