Skip to main content

Python Interface for Spatial Stochastic Biochemical Simulations

Project description

SpatialPy is a Python 3 package for simulation of spatial deterministic/stochastic reaction-diffusion-advection problems embedded in Lagrangian reference frame particle based fluid dynamics domain

This package is intended to replace the PyURDME software https://github.com/pyurdme/pyurdme and will feature both a NSM solver for RDME simulation on static domains and a sSSA-SDPD particle based fluid dynamics solver as described in the publication "A hybrid smoothed dissipative particle dynamics (SDPD) spatial stochastic simulation algorithm (sSSA) for advection–diffusion–reaction problems" by Drawert, Jacob, Li, Yi, Petzold https://www.sciencedirect.com/science/article/pii/S0021999118307101

PLEASE REGISTER AS A USER, so that we can prove SpatialPy has many users when we seek funding to support development. SpatialPy is part of the StochSS project.

PyPI - License PyPI - Python Version PyPI PyPI - Downloads

Table of contents

Installation

SpatialPy can be installed on your computer using different methods, as described below.

Using PyPI

On Linux, macOS, and Windows operating systems, you should be able to install SpatialPy with pip. Please review the official pip documentation for installation instructions and additional information.

Then, to install SpatialPy from the Python package repository, run the following command:

python3 -m pip install spatialpy --user --upgrade

Using the source code repository

As an alternative to getting it from PyPI, you can instruct pip to install SpatialPy directly from the GitHub repository:

python3 -m pip install https://github.com/StochSS/SpatialPy/archive/main.zip --user --upgrade

As a final alternative, you can first use git to clone a copy of the SpatialPy source tree from the GitHub repository to your local computer disk, and then install SpatialPy using that copy:

git clone https://github.com/StochSS/SpatialPy.git
cd SpatialPy
python3 -m pip install  .  --user --upgrade

Usage

SpatialPy provides simple object-oriented abstractions for defining a model of a biochemical system and simulating that model using efficient stochastic simulation algorithms. The basic steps to use SpatialPy are:

  1. Create a SpatialPy.Model containing molecular species, parameters, and reactions.
  2. Invoke the model's .run() method.

The run() method can be customized using keyword arguments to select different solvers, random seed, data return type and more. For more detailed examples on how to use SpatialPy, please see the Jupyter notebooks contained in the examples subdirectory.

Simple example to illustrate the use of SpatialPy

In SpatialPy, a model is expressed as an object. Components, such as the domains, reactions, biochemical species, and characteristics such as the time span for simulation, are all defined within the model. The following Python code represents our spatial birth death model using SpatialPy's facility:

def create_birth_death(parameter_values=None):
    # First call the gillespy2.Model initializer.
    model = spatialpy.Model(name='Spatial Birth-Death')
    
    # Define Domain Type IDs as constants of the Model
    model.HABITAT = "Habitat"

    # Define domain points and attributes of a regional space for simulation.
    domain = spatialpy.Domain.create_2D_domain(
        xlim=(0, 1), ylim=(0, 1), numx=10, numy=10, type_id=model.HABITAT, fixed=True
    )
    model.add_domain(domain)

    # Define variables for the biochemical species representing Rabbits.
    Rabbits = spatialpy.Species(name='Rabbits', diffusion_coefficient=0.1)
    model.add_species(Rabbits)

    # Scatter the initial condition for Rabbits randomly over all types.
    init_rabbit_pop = spatialpy.ScatterInitialCondition(species='Rabbits', count=100)
    model.add_initial_condition(init_rabbit_pop)

    # Define parameters for the rates of creation and destruction.
    kb = spatialpy.Parameter(name='k_birth', expression=10)
    kd = spatialpy.Parameter(name='k_death', expression=0.1)
    model.add_parameter([kb, kd])

    # Define reactions channels which cause the system to change over time.
    # The list of reactants and products for a Reaction object are each a
    # Python dictionary in which the dictionary keys are Species objects
    # and the values are stoichiometries of the species in the reaction.
    birth = spatialpy.Reaction(name='birth', reactants={}, products={"Rabbits":1}, rate="k_birth")
    death = spatialpy.Reaction(name='death', reactants={"Rabbits":1}, products={}, rate="k_death")
    model.add_reaction([birth, death])

    # Set the timespan of the simulation.
    tspan = spatialpy.TimeSpan.linspace(t=10, num_points=11, timestep_size=1)
    model.timespan(tspan)
    return model

Given the model creation function above, the model can be simulated by first instantiating the model object, and then invoking the run() method on the object. The following code will run the model once to produce a sample trajectory:

model = create_birth_death()
results = model.run()

The results are then stored in a class Results object for single trajectory or for multiple trajectories. Results can be plotted with plotly (offline) using plot_species() or in matplotlib using plot_species(use_matplotlib=True). For additional plotting options such as plotting from a selection of species, or statistical plotting, please see the documentation.:

results.plot_species(species='Rabbits', t_val=10, use_matplotlib=True)

Getting help

SpatialPy's online documentation provides more details about using the software. If you find any problem with SpatialPy or the documentation, please report it using the GitHub issue tracker for this repository. You can also contact Dr. Brian Drawert directly with questions and suggestions.

Contributing

We would be happy to receive your help and participation with enhancing SpatialPy! Please follow the guidelines described in CONTRIBUTING.md.

New developments happen primarily in the develop branch. New releases are put in the main branch.

Main Branch Develop Branch
Build Status Build Status

License

SpatialPy is licensed under the GNU General Public License version 3. Please see the file LICENSE for more information.

Authors and history

Acknowledgments

This work has been funded by National Institutes of Health (NIH) NIBIB Award No. 2R01EB014877-04A1.

SpatialPy uses numerous open-source packages, without which it would have been effectively impossible to develop this software with the resources we had. We want to acknowledge this debt. In alphabetical order, the packages are:

  • Jupyter – web application for creating documents containing code, visualizations and narrative text
  • MatplotLib – Python plotting library
  • Plotly – Graphing library for making interactive, publication-quality graphs
  • Numpy – the fundamental package for scientific computing with Python
  • Scipy – Python-based ecosystem of open-source software for mathematics, science, and engineering

Finally, we are grateful for institutional resources made available by the University of North Carolina at Asheville, the University of California at Santa Barbara, Uppsala University, and the California Institute of Technology.

           

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spatialpy-1.2.2.tar.gz (718.4 kB view hashes)

Uploaded Source

Built Distribution

spatialpy-1.2.2-py3-none-any.whl (808.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page