Package: smfsb
smfsb for pythonPython library for the book,Stochastic modelling for systems biology, third edition.Please note that this package is still in the early stages of development. There will be bugs, there is limited documentation, and the coverage of the book is still quite incomplete. Currently, the package covers the code from Chapters 1 to 8 (inclusive). I will be adding code to cover the rest of the book in due course, but I have not set a deadline.InstallLatest stable version:pipinstallsmfsbTo upgrade already installed package:pipinstall--upgradesmfsbBasic usageNote thatthe book, and its associatedgithub repois 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 libraryFirst, see how to simulate a built-in model:importsmfsblv=smfsb.models.lv()print(lv)stepLv=lv.stepGillespie()out=smfsb.simTs(lv.m,0,100,0.1,stepLv)Now, ifmatplotlibis installed, you can plot the output withimportmatplotlib.pyplotaspltfig,axis=plt.subplots()foriinrange(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 likehelp(smfsb.Spn),help(smfsb.Spn.stepGillespie)orhelp(smfsb.simTs). Much of this documentation is available onReadTheDocs, but the formatting is currently sub-optimal.Creating and simulating a modelNext, let's create and simulate our own model by specifying a stochastic Petri net explicitly.importnumpyasnpsir=smfsb.Spn(["S","I","R"],["S->I","I->R"],[[1,1,0],[0,1,0]],[[0,2,0],[0,0,1]],lambdax,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-shorthandNote 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, usem=smfsb.file2Spn("myModel.xml")Note that if you are working with SBML models in Python usinglibsbml, then there is also a functionmodel2Spnwhich takes a libsbml model object.To read and parse an SBML-shorthand model, usem=smfsb.mod2Spn("myModel.mod")There is also a functionsh2Spnwhich 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 SIER model.importsmfsbimportnumpyasnpseirSH="""@model:3.1.1=SEIR "SEIR Epidemic model"s=item, t=second, v=litre, e=item@compartmentsPop@speciesPop:S=100 sPop:E=0 sPop:I=5 sPop:R=0 s@reactions@r=InfectionS + I -> E + Ibeta*S*I : beta=0.1@r=TransitionE -> Isigma*E : sigma=0.2@r=RemovalI -> Rgamma*I : gamma=0.5"""seir=smfsb.sh2Spn(seirSH)stepSeir=seir.stepGillespie()out=smfsb.simTs(seir.m,0,40,0.05,stepSeir)importmatplotlib.pyplotaspltfig,axis=plt.subplots()foriinrange(len(seir.m)):axis.plot(np.arange(0,40,0.05),out[:,i])axis.legend(seir.n)fig.savefig("seir.pdf")Acollection of appropriate modelsis associated with the book.You can see this package onPyPIorGitHub.Copyright (2023-2024) Darren J Wilkinson