Coder Social home page Coder Social logo

python-smfsb's Introduction

smfsb for python

Python library for the book, Stochastic modelling for systems biology, third edition. This library is a Python port of the R package associated with the book.

Install

Latest stable version:

pip install smfsb

To upgrade already installed package:

pip install --upgrade smfsb

Basic usage

Note that the book, and its associated github repo 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:

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

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. The API documentation contains very minimal usage examples. For more interesting examples, see the demos directory.

Creating and simulating a model

Next, let's create and simulate our own model by specifying a stochastic Petri net explicitly.

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

m = smfsb.file2Spn("myModel.xml")

Note that if you are working with SBML models in Python using libsbml, then there is also a function model2Spn which takes a libsbml model object.

To read and parse an SBML-shorthand model, use

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.

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 is associated with the book.

You can see this package on PyPI or GitHub.

Copyright (2023-2024) Darren J Wilkinson

python-smfsb's People

Contributors

darrenjw avatar

Stargazers

 avatar  avatar

Watchers

 avatar

python-smfsb's Issues

stepLVc

Add C extension for fast simulation from the LV transition kernel.

Set up github CI

Set up github CI for automated building and testing on every commit. Automaticially build wheels for multiple platforms (including windows). This will then make it possible to include C code without breaking the package on windows.

Unit tests

Add a bunch of unit tests before refactoring.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.