{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Getting started" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Install and import `sme`" ] }, { "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", "import numpy as np\n", "\n", "print(\"sme version:\", sme.__version__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Importing a model\n", "- to load an existing sme or xml file: `sme.open_file('model_filename.xml')`\n", "- to load a built-in example model: `sme.open_example_model()`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "LLPnn1h1Yee7" }, "outputs": [], "source": [ "my_model = sme.open_example_model()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting help\n", "- to see the type of an object: `type(object)`\n", "- to print a one line description of an object: `repr(object)`\n", "- to print a multi-line description of an object: `print(object)`\n", "- to get help on an object, its methods and properties: `help(object)`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(my_model)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "repr(my_model)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(my_model)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "help(my_model)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Viewing model contents\n", "- the compartments in a model can be accessed as a list: `model.compartments`\n", "- the list can be iterated over, or an item looked up by index or name\n", "- other lists of objects, such as species in a compartment, or parameters in a reaction, behave in the same way\n", "\n", "### Iterating over compartments" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for compartment in my_model.compartments:\n", " print(repr(compartment))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Get compartment by name" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cell_compartment = my_model.compartments[\"Cell\"]\n", "print(repr(cell_compartment))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Get compartment by list index" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "last_compartment = my_model.compartments[-1]\n", "print(repr(last_compartment))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Display geometry of compartments" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, axs = plt.subplots(nrows=1, ncols=len(my_model.compartments), figsize=(18, 12))\n", "for ax, compartment in zip(axs, my_model.compartments):\n", " ax.imshow(compartment.geometry_mask[0], interpolation=\"none\")\n", " ax.set_title(f\"{compartment.name}\")\n", " ax.set_xlabel(\"x\")\n", " ax.set_ylabel(\"y\")\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Display parameter names and values" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_reac = my_model.compartments[\"Nucleus\"].reactions[\"A to B conversion\"]\n", "print(my_reac)\n", "for param in my_reac.parameters:\n", " print(param)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Editing model contents\n", "- Parameter values and object names can be changed by assigning new values to them" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Names" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f\"Model name: {my_model.name}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_model.name = \"New model name!\"\n", "print(f\"Model name: {my_model.name}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Model Parameters" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "param = my_model.parameters[0]\n", "print(f\"{param.name} = {param.value}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "param.value = \"2.5\"\n", "print(f\"{param.name} = {param.value}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Reaction Parameters" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "k1 = my_model.compartments[\"Nucleus\"].reactions[\"A to B conversion\"].parameters[\"k1\"]\n", "print(f\"{k1.name} = {k1.value}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "k1.value = 0.72\n", "print(f\"{k1.name} = {k1.value}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Species Initial Concentrations\n", "- can be Uniform (`float`), Analytic (`str`) or Image (`np.ndarray`)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "species = my_model.compartments[\"Cell\"].species[0]\n", "print(\n", " f\"Species '{species.name}' has initial concentration of type '{species.concentration_type}', value '{species.uniform_concentration}'\"\n", ")\n", "species.uniform_concentration = 1.3\n", "print(\n", " f\"Species '{species.name}' has initial concentration of type '{species.concentration_type}', value '{species.uniform_concentration}'\"\n", ")\n", "plt.imshow(species.concentration_image[0])\n", "plt.colorbar()\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "species.analytic_concentration = \"3 + 2*cos(x/2)+sin(y/3)\"\n", "print(\n", " f\"Species '{species.name}' has initial concentration of type '{species.concentration_type}', expression '{species.analytic_concentration}'\"\n", ")\n", "plt.imshow(species.concentration_image[0])\n", "plt.colorbar()\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# generate concentration image with concentration = x + y\n", "new_concentration_image = np.zeros(species.concentration_image.shape)\n", "for index, _ in np.ndenumerate(new_concentration_image):\n", " new_concentration_image[index] = index[0] + index[1]\n", "\n", "species.concentration_image = new_concentration_image\n", "print(\n", " f\"Species '{species.name}' has initial concentration of type '{species.concentration_type}'\"\n", ")\n", "plt.imshow(species.concentration_image[0])\n", "plt.colorbar()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exporting a model\n", "- to save the model, including any simulation results: `model.export_sme_file('model_filename.sme')`\n", "- to export the model as an SBML file (no simulation results): `model.export_sbml_file('model_filename.xml')`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "LLPnn1h1Yee7" }, "outputs": [], "source": [ "my_model.export_sme_file(\"model.sme\")" ] }, { "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 }