Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optimize example notebook #9

Merged
merged 1 commit into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add optimize example notebook
  • Loading branch information
lkeegan committed Aug 31, 2021
commit 13ca6175440004d4a978b4535ed84eec6d8f7170
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ API Reference
:caption: Example jupyter notebooks

notebooks/plot
notebooks/optimize

Source code
-----------
Expand Down
230 changes: 230 additions & 0 deletions docs/notebooks/optimize.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# sme_contrib.optimize"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "zISJVa_IlpA7"
},
"outputs": [],
"source": [
"!pip install -q sme_contrib\n",
"import sme\n",
"import sme_contrib.optimize as smeopt\n",
"import sme_contrib.plot as smeplot\n",
"import numpy as np\n",
"from matplotlib import pyplot as plt\n",
"from IPython.display import HTML\n",
"from PIL import Image"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Gray-Scott model\n",
"A simple two-species model with two reaction rate parameters, that forms spatial patterns and eventually reaches a steady state"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def simulated_gray_scott(f, k):\n",
" m = sme.open_example_model(\"gray-scott\")\n",
" m.compartments[0].species[\n",
" \"V\"\n",
" ].analytic_concentration = \"exp(-((x-49.5)^2+(y-49.5)^2))\"\n",
" m.parameters[\"f\"].value = f\"{f}\"\n",
" m.parameters[\"k\"].value = f\"{k}\"\n",
" m.simulate(5000, 50, return_results=False)\n",
" return m\n",
"\n",
"\n",
"def gray_scott_anim(f, k):\n",
" gray_scott = simulated_gray_scott(f, k)\n",
" return smeplot.concentration_heatmap_animation(\n",
" gray_scott.simulation_results(), [\"V\"]\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"anim = gray_scott_anim(0.04, 0.06)\n",
"HTML(anim.to_html5_video())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"anim = gray_scott_anim(0.051, 0.061)\n",
"HTML(anim.to_html5_video())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"anim = gray_scott_anim(0.028, 0.062)\n",
"HTML(anim.to_html5_video())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Try to fit to the target steady state"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"def create_target_image(f, k):\n",
" gray_scott = simulated_gray_scott(f, k)\n",
" conc = gray_scott.simulation_results()[-1].species_concentration[\"V\"]\n",
" conc = 255 * conc / np.max(conc)\n",
" Image.fromarray(conc.astype(\"uint8\")).save(\"tmp.png\")\n",
" gray_scott.export_sbml_file(\"tmp.xml\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"def apply_params(model, params):\n",
" model.parameters[\"f\"].value = f\"{params[0]}\"\n",
" model.parameters[\"k\"].value = f\"{params[1]}\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"create_target_image(0.04, 0.06)\n",
"ss = smeopt.SteadyState(\n",
" \"tmp.xml\",\n",
" \"tmp.png\",\n",
" [\"V\"],\n",
" apply_params,\n",
" [0.01, 0.05],\n",
" [0.06, 0.07],\n",
" 5000,\n",
" 2000,\n",
" 90,\n",
")\n",
"ss.find(10, 20)\n",
"ss.plot_all()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"create_target_image(0.051, 0.061)\n",
"ss = smeopt.SteadyState(\n",
" \"tmp.xml\",\n",
" \"tmp.png\",\n",
" [\"V\"],\n",
" apply_params,\n",
" [0.01, 0.05],\n",
" [0.06, 0.07],\n",
" 5000,\n",
" 2000,\n",
" 90,\n",
")\n",
"ss.find(10, 20)\n",
"ss.plot_all()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"create_target_image(0.028, 0.062)\n",
"ss = smeopt.SteadyState(\n",
" \"tmp.xml\",\n",
" \"tmp.png\",\n",
" [\"V\"],\n",
" apply_params,\n",
" [0.01, 0.05],\n",
" [0.06, 0.07],\n",
" 5000,\n",
" 2000,\n",
" 90,\n",
")\n",
"ss.find(10, 20)\n",
"ss.plot_all()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "sme_image_comparison.ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
matplotlib
numpy
pillow
pyswarms
Expand Down