{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Visualization" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Open an example model & simulate" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "EtqqtL8VYdA5" }, "outputs": [], "source": [ "!pip install -q sme\n", "import sme\n", "from matplotlib import pyplot as plt\n", "from matplotlib import animation\n", "import numpy as np\n", "from IPython.display import HTML" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "LLPnn1h1Yee7" }, "outputs": [], "source": [ "my_model = sme.open_example_model()\n", "sim_results = my_model.simulate(simulation_time=250.0, image_interval=5.0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Animation of species concentrations" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "species = [\"B_cell\", \"B_out\"]\n", "\n", "fig, axs = plt.subplots(constrained_layout=True, ncols=len(species), figsize=(9, 4))\n", "\n", "# set normalization of each plot to maximum concentration of species over entire simulation\n", "norms = []\n", "for spec, ax in zip(species, axs):\n", " c_max = np.max([np.max(r.species_concentration[spec]) for r in sim_results])\n", " norms.append(plt.Normalize(vmin=0, vmax=c_max))\n", "\n", "# make a plot with correctly normalized colorbar for each species\n", "for ax, norm in zip(axs, norms):\n", " im = ax.imshow(np.zeros((1, 1)), norm=norm)\n", " fig.colorbar(im, ax=ax)\n", "\n", "# create a list of plot artists for each timepoint\n", "artists = []\n", "for sim_result in sim_results:\n", " artist = []\n", " for spec, ax, norm in zip(species, axs, norms):\n", " artist.append(\n", " ax.imshow(\n", " sim_result.species_concentration[spec][0],\n", " animated=True,\n", " norm=norm,\n", " interpolation=None,\n", " )\n", " )\n", " artist.append(\n", " ax.text(\n", " 0.5,\n", " 1.01,\n", " f\"{spec}: t = {sim_result.time_point}\",\n", " horizontalalignment=\"center\",\n", " verticalalignment=\"bottom\",\n", " transform=ax.transAxes,\n", " )\n", " )\n", " artists.append(artist)\n", "\n", "# make an animation from the list of artists\n", "anim = animation.ArtistAnimation(fig, artists, interval=200, blit=True, repeat=False)\n", "plt.close()\n", "HTML(anim.to_html5_video())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Gray-Scott Model" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# simulate for a few different reaction parameter values & store concentrations of species V\n", "gray_scott = sme.open_example_model(\"gray-scott\")\n", "fs = [\"0.03\", \"0.04\", \"0.05\"]\n", "vs = []\n", "for f in fs:\n", " gray_scott.parameters[\"f\"].value = f\n", " sim_results = gray_scott.simulate(\n", " simulation_time=10000.0,\n", " image_interval=100,\n", " simulator_type=sme.SimulatorType.Pixel,\n", " )\n", " times = [r.time_point for r in sim_results]\n", " vs.append([r.species_concentration[\"V\"][0] for r in sim_results])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, axs = plt.subplots(constrained_layout=True, nrows=len(fs), figsize=(9, 16))\n", "for ax, f in zip(axs, fs):\n", " ax.set_title(f\"f = {f}\")\n", "\n", "artists = []\n", "for i in range(len(vs[0])):\n", " artist = []\n", " for ax, v in zip(axs, vs):\n", " artist.append(ax.imshow(v[i], animated=True, interpolation=None))\n", " artists.append(artist)\n", "\n", "anim = animation.ArtistAnimation(fig, artists, interval=200, blit=True, repeat=False)\n", "plt.close()\n", "HTML(anim.to_html5_video())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "colab": { "name": "sme_getting_started.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.10.8" } }, "nbformat": 4, "nbformat_minor": 4 }