smfsb


Namesmfsb JSON
Version 1.0.2 PyPI version JSON
download
home_pageNone
SummaryPython code relating to the textbook, Stochastic modelling for systems biology, third edition
upload_time2024-09-08 15:26:46
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # smfsb for python

Python library for the book, [Stochastic modelling for systems biology, third edition](https://github.com/darrenjw/smfsb/). This library is a Python port of the R package associated with the book.

## Install

Latest stable version:
```bash
pip install smfsb
```
To upgrade already installed package:
```bash
pip install --upgrade smfsb
```

## Basic usage

Note that **the book**, and its associated [github repo](https://github.com/darrenjw/smfsb) is the main source of documentation for this library. The code in the book is in R, but the code in this library is supposed to mirror the R code, but in Python.

### Using a model built-in to the library

First, see how to simulate a built-in model:
```python
import smfsb

lv = smfsb.models.lv()
print(lv)
stepLv = lv.stepGillespie()
out = smfsb.simTs(lv.m, 0, 100, 0.1, stepLv)
```
Now, if `matplotlib` is installed, you can plot the output with
```python
import matplotlib.pyplot as plt
fig, axis = plt.subplots()
for i in range(2):
	axis.plot(range(out.shape[0]), out[:,i])

axis.legend(lv.n)
fig.savefig("lv.pdf")
```
Standard python docstring documentation is available. Usage information can be obtained from the python REPL with commands like `help(smfsb.Spn)`, `help(smfsb.Spn.stepGillespie)` or `help(smfsb.simTs)`. This documentation is also available on [ReadTheDocs](https://python-smfsb.readthedocs.io/). The API documentation contains very minimal usage examples. For more interesting examples, see the [demos directory](https://github.com/darrenjw/python-smfsb/tree/main/demos).

### Creating and simulating a model

Next, let's create and simulate our own model by specifying a stochastic Petri net explicitly.
```python
import numpy as np
sir = smfsb.Spn(["S", "I", "R"], ["S->I", "I->R"],
	[[1,1,0],[0,1,0]], [[0,2,0],[0,0,1]],
	lambda x, t: np.array([0.3*x[0]*x[1]/200, 0.1*x[1]]),
	[197, 3, 0])
stepSir = sir.stepPTS()
sample = smfsb.simSample(500, sir.m, 0, 20, stepSir)
fig, axis = plt.subplots()
axis.hist(sample[:,1],30)
axis.set_title("Infected at time 20")
plt.savefig("sIr.pdf")
```

### Reading and parsing models in SBML and SBML-shorthand

Note that you can read in SBML or SBML-shorthand models that have been designed for discrete stochastic simulation into a stochastic Petri net directly. To read and parse an SBML model, use
```python
m = smfsb.file2Spn("myModel.xml")
```
Note that if you are working with SBML models in Python using [libsbml](https://pypi.org/project/python-libsbml/), then there is also a function `model2Spn` which takes a libsbml model object.

To read and parse an SBML-shorthand model, use
```python
m = smfsb.mod2Spn("myModel.mod")
```
There is also a function `sh2Spn` which expects a python string containing a shorthand model. This is convenient for embedding shorthand models inside python scripts, and is particularly convenient when working with things like Jupyter notebooks. Below follows a complete session to illustrate the idea by creating and simulating a realisation from a discrete stochastic SEIR model.
```python
import smfsb
import numpy as np

seirSH = """
@model:3.1.1=SEIR "SEIR Epidemic model"
 s=item, t=second, v=litre, e=item
@compartments
 Pop
@species
 Pop:S=100 s
 Pop:E=0 s	  
 Pop:I=5 s
 Pop:R=0 s
@reactions
@r=Infection
 S + I -> E + I
 beta*S*I : beta=0.1
@r=Transition
 E -> I
 sigma*E : sigma=0.2
@r=Removal
 I -> R
 gamma*I : gamma=0.5
"""

seir = smfsb.sh2Spn(seirSH)
stepSeir = seir.stepGillespie()
out = smfsb.simTs(seir.m, 0, 40, 0.05, stepSeir)

import matplotlib.pyplot as plt
fig, axis = plt.subplots()
for i in range(len(seir.m)):
	axis.plot(np.arange(0, 40, 0.05), out[:,i])

axis.legend(seir.n)
fig.savefig("seir.pdf")
```


A [collection of appropriate models](https://github.com/darrenjw/smfsb/tree/master/models) is associated with the book.



You can see this package on [PyPI](https://pypi.org/project/smfsb/) or [GitHub](https://github.com/darrenjw/python-smfsb).


## Fast simulation and inference

If you like this library but find it a little slow, you should know that there is a [JAX](https://jax.readthedocs.io/) port of this package: [JAX-smfsb](https://github.com/darrenjw/jax-smfsb). It requires a JAX installalation, and the API is (very) slightly modified, but it has state-of-the-art performance for simulation and inference.


**Copyright (2023-2024) Darren J Wilkinson**



            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "smfsb",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": null,
    "author_email": "Darren Wilkinson <darrenjwilkinson@btinternet.com>",
    "download_url": "https://files.pythonhosted.org/packages/da/ff/565c22ab130d408d969f1aeddf3f646943466a16999f980e9277ea9df724/smfsb-1.0.2.tar.gz",
    "platform": null,
    "description": "# smfsb for python\n\nPython library for the book, [Stochastic modelling for systems biology, third edition](https://github.com/darrenjw/smfsb/). This library is a Python port of the R package associated with the book.\n\n## Install\n\nLatest stable version:\n```bash\npip install smfsb\n```\nTo upgrade already installed package:\n```bash\npip install --upgrade smfsb\n```\n\n## Basic usage\n\nNote that **the book**, and its associated [github repo](https://github.com/darrenjw/smfsb) is the main source of documentation for this library. The code in the book is in R, but the code in this library is supposed to mirror the R code, but in Python.\n\n### Using a model built-in to the library\n\nFirst, see how to simulate a built-in model:\n```python\nimport smfsb\n\nlv = smfsb.models.lv()\nprint(lv)\nstepLv = lv.stepGillespie()\nout = smfsb.simTs(lv.m, 0, 100, 0.1, stepLv)\n```\nNow, if `matplotlib` is installed, you can plot the output with\n```python\nimport matplotlib.pyplot as plt\nfig, axis = plt.subplots()\nfor i in range(2):\n\taxis.plot(range(out.shape[0]), out[:,i])\n\naxis.legend(lv.n)\nfig.savefig(\"lv.pdf\")\n```\nStandard python docstring documentation is available. Usage information can be obtained from the python REPL with commands like `help(smfsb.Spn)`, `help(smfsb.Spn.stepGillespie)` or `help(smfsb.simTs)`. This documentation is also available on [ReadTheDocs](https://python-smfsb.readthedocs.io/). The API documentation contains very minimal usage examples. For more interesting examples, see the [demos directory](https://github.com/darrenjw/python-smfsb/tree/main/demos).\n\n### Creating and simulating a model\n\nNext, let's create and simulate our own model by specifying a stochastic Petri net explicitly.\n```python\nimport numpy as np\nsir = smfsb.Spn([\"S\", \"I\", \"R\"], [\"S->I\", \"I->R\"],\n\t[[1,1,0],[0,1,0]], [[0,2,0],[0,0,1]],\n\tlambda x, t: np.array([0.3*x[0]*x[1]/200, 0.1*x[1]]),\n\t[197, 3, 0])\nstepSir = sir.stepPTS()\nsample = smfsb.simSample(500, sir.m, 0, 20, stepSir)\nfig, axis = plt.subplots()\naxis.hist(sample[:,1],30)\naxis.set_title(\"Infected at time 20\")\nplt.savefig(\"sIr.pdf\")\n```\n\n### Reading and parsing models in SBML and SBML-shorthand\n\nNote that you can read in SBML or SBML-shorthand models that have been designed for discrete stochastic simulation into a stochastic Petri net directly. To read and parse an SBML model, use\n```python\nm = smfsb.file2Spn(\"myModel.xml\")\n```\nNote that if you are working with SBML models in Python using [libsbml](https://pypi.org/project/python-libsbml/), then there is also a function `model2Spn` which takes a libsbml model object.\n\nTo read and parse an SBML-shorthand model, use\n```python\nm = smfsb.mod2Spn(\"myModel.mod\")\n```\nThere is also a function `sh2Spn` which expects a python string containing a shorthand model. This is convenient for embedding shorthand models inside python scripts, and is particularly convenient when working with things like Jupyter notebooks. Below follows a complete session to illustrate the idea by creating and simulating a realisation from a discrete stochastic SEIR model.\n```python\nimport smfsb\nimport numpy as np\n\nseirSH = \"\"\"\n@model:3.1.1=SEIR \"SEIR Epidemic model\"\n s=item, t=second, v=litre, e=item\n@compartments\n Pop\n@species\n Pop:S=100 s\n Pop:E=0 s\t  \n Pop:I=5 s\n Pop:R=0 s\n@reactions\n@r=Infection\n S + I -> E + I\n beta*S*I : beta=0.1\n@r=Transition\n E -> I\n sigma*E : sigma=0.2\n@r=Removal\n I -> R\n gamma*I : gamma=0.5\n\"\"\"\n\nseir = smfsb.sh2Spn(seirSH)\nstepSeir = seir.stepGillespie()\nout = smfsb.simTs(seir.m, 0, 40, 0.05, stepSeir)\n\nimport matplotlib.pyplot as plt\nfig, axis = plt.subplots()\nfor i in range(len(seir.m)):\n\taxis.plot(np.arange(0, 40, 0.05), out[:,i])\n\naxis.legend(seir.n)\nfig.savefig(\"seir.pdf\")\n```\n\n\nA [collection of appropriate models](https://github.com/darrenjw/smfsb/tree/master/models) is associated with the book.\n\n\n\nYou can see this package on [PyPI](https://pypi.org/project/smfsb/) or [GitHub](https://github.com/darrenjw/python-smfsb).\n\n\n## Fast simulation and inference\n\nIf you like this library but find it a little slow, you should know that there is a [JAX](https://jax.readthedocs.io/) port of this package: [JAX-smfsb](https://github.com/darrenjw/jax-smfsb). It requires a JAX installalation, and the API is (very) slightly modified, but it has state-of-the-art performance for simulation and inference.\n\n\n**Copyright (2023-2024) Darren J Wilkinson**\n\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Python code relating to the textbook, Stochastic modelling for systems biology, third edition",
    "version": "1.0.2",
    "project_urls": {
        "Documentation": "https://python-smfsb.readthedocs.io/",
        "Homepage": "https://github.com/darrenjw/python-smfsb",
        "Issues": "https://github.com/darrenjw/python-smfsb/issues"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "38da00911b58cbe6cd968f12810f8472583adf6961430cee34437142d32dd139",
                "md5": "d5640c8adb48e92ad3c94a14ad87c5c7",
                "sha256": "8b4dae9ed6f4c4d4bd2d5b0a97f9b1925038a9a27e16cc279bb55f2c656f81f8"
            },
            "downloads": -1,
            "filename": "smfsb-1.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d5640c8adb48e92ad3c94a14ad87c5c7",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 25829,
            "upload_time": "2024-09-08T15:26:41",
            "upload_time_iso_8601": "2024-09-08T15:26:41.419712Z",
            "url": "https://files.pythonhosted.org/packages/38/da/00911b58cbe6cd968f12810f8472583adf6961430cee34437142d32dd139/smfsb-1.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "daff565c22ab130d408d969f1aeddf3f646943466a16999f980e9277ea9df724",
                "md5": "d93ae61cf7630435df589ffe70882b5c",
                "sha256": "116b12d42788a469f392cea9ee3e1cba2d0a5bf6b0014ed4c94d1fa82e83ffb7"
            },
            "downloads": -1,
            "filename": "smfsb-1.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "d93ae61cf7630435df589ffe70882b5c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 7085630,
            "upload_time": "2024-09-08T15:26:46",
            "upload_time_iso_8601": "2024-09-08T15:26:46.388446Z",
            "url": "https://files.pythonhosted.org/packages/da/ff/565c22ab130d408d969f1aeddf3f646943466a16999f980e9277ea9df724/smfsb-1.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-08 15:26:46",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "darrenjw",
    "github_project": "python-smfsb",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "smfsb"
}
        
Elapsed time: 0.31126s