pysb-units
PySB add-on providing utilities to add units to models and perform dimensional analysis.
Science Score: 67.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
✓DOI references
Found 1 DOI reference(s) in README -
✓Academic publication links
Links to: zenodo.org -
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (15.9%) to scientific vocabulary
Keywords
Repository
PySB add-on providing utilities to add units to models and perform dimensional analysis.
Basic Info
Statistics
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 5
- Releases: 4
Topics
Metadata Files
README.md
pysb-units
pysb-units streamlines unit management and helps ensure unit consistency in PySB models.
Table of Contents
Install
| ! Note | | :--- | | psyb-units is still in version zero development so new versions may not be backwards compatible. |
pysb-units installs as the pysb.units Python (namespace) package. It is has been developed with Python 3.11.3 and PySB 1.15.0.
Dependencies
Note that pysb-units has the following core dependencies:
* PySB - developed using version 1.15.0.
* astropy - developed using version 5.3.4.
* sympy - developed using version 1.11.1.
pip install
You can install pysb-units version 0.4.1 with pip:
sh
pip install pysb-units
Manual install
First, download the repository. Then from the pysb-units folder/directory run
pip install .
License
This project is licensed under the BSD 2-Clause License - see the LICENSE file for details
Change Log
See: CHANGELOG
Documentation and Usage
Quick Overview
pysb-units is an add-on for the PySB modeling framework that provides tools to manage units. The key features of pysb-units are a new Unit object derived from pysb annotations, a new SimulationUnits object, and drop-in replacements for core model components, including Model, Parameter, Expression, Rule, Initial, and Observable that include new features to help manage units. Additionally, pysb-units defines a couple of useful utility functions, including the unitize and check functions, which make it easier to add units to model and run additional unit checks (such as unit consistency).
pysb-units introduces two new objects for defining and managing units in a pysb model. They are:
Unit(component, 'unit')- assigns a particular unit value to a model component such as Parameter or Observable. E.g., to assign a frequency unit to a rate constant parameter (as a single line):Unit(Parameter('k_f', 1e-1), '1/s'). This object is derived from pysb'sAnnotationobject but leverages theastropy.unitslibrary for unit management.- You can explicity define dimensionless quantities by passing
Nonefor the input units: e.g.,Unit(scaling_factor, None).Parameters accept an optional keywordunitthat can be used to assign aUnitthat parameter automatically with needing to explicity attach theUnitobject.
- You can explicity define dimensionless quantities by passing
SimulationUnits(concentration='concentration_unit', time='time_unit')- sets the concentration and time units that are to be used in simulations. E.g., to set the use of nM concentrations and time in seconds:SimulationUnits(concentration='nM', time='s'). Note that when this object is defined it will enforce conversion of all concentration and time units to the specified units.- Supports stochastic simulation units with
concentration='molecules'; it uses a unit conversion based on the equationmolecules = [molar concentraion] * volume * N_A, where N_A is Avogadro's number. - To define the appropriate volume for the conversion a call to the
pysb.units.set_molecule_volume(value[float], unit[str])can be added before or just after theSimulationUnitsinitialization. E.g.,pysb.units.set_molecule_volume(1.6, 'pL')would set a volume of 1.6 pL for the conversion used bySimulationUnitsto go from molar concentrations to number of molecules. Note that these conversions currently only work properly for non-compartmental models.
- Supports stochastic simulation units with
Additionally, pysb-units defines drop-in replacements for core model components, including Model, Parameter, Expression, Rule, Initial, and Observable that are integrated with new units-based features. pysb-units defines a couple of useful utility functions, including the unitize and check functions, which make it easier to add units to model and run additional unit checks (such as unit consistency).
Example
A simple model with one degradation reaction:
- Regular pysb: ```python
from pysb import Model, Parameter, Monomer, Initial, Observable, Expression
Initialize the PySB model:
Model()
Monomer(s):
Monomer('protein')
Model parameter(s):
Initial concentration of protein
Parameter('protein_0', 0.5) # uM
1st-order rate parameter for the degradation
Parameter('k_deg', 0.1) # 1/s
Initial concentration(s)
Initial(protein, protein_0)
Reaction rule(s)
Just the one degradation
Rule('degradation', protein() >> None, k_deg)
Observables
Time-dependent protein concentration:
Observable('protein_t', protein()) # uM
Expressions
The time-dependent degradation rate:
Expression('degrate', (proteint * k_deg)) # uM/s
```
- pysb with pysb-units ```python # Import the pysb components we need: from pysb import Model, Parameter, Monomer, Initial, Observable, Expression # Import pysb-units: import pysb.units as units
Activate units - replaces core model components
with the appropriate versions from pysb.units:
units.unitize()
Initialize the PySB model:
Model()
The primary units needed for simulating the model are
concentration (or amount) and time. We can define those
here with SimulationUnits:
SimulationUnits(concentration='uM', time='s')
Monomer(s):
Monomer('protein')
Model parameter(s):
Initial concentration of protein:
Parameter('protein_0', 500.)
Attach units to protein_0:
Unit(protein_0, 'nM')
1st-order rate parameter for the degradation
defined with frequency (1/time) units - here,
we chain Unit and Parameter definitions:
Unit(Parameter('k_deg', 0.1), '1/s')
Initial concentration(s)
Initial(protein, protein_0)
Reaction rule(s)
Just the one degradation
Rule('degradation', protein() >> None, k_deg)
Observables
Time-dependent protein concentration:
Observable('protein_t', protein())
Expressions
The time-dependent degradation rate:
Expression('degrate', (proteint * k_deg))
Apply additional unit checks, including unit duplication
and unit consistency checking:
units.check()
```
In the above unit-ed example, additional unit-based features and unit validation are applied:
* SimulationUnits(concentration='uM', time='s') - sets the the expected units for the concentration (or amount) and time globally for the model. Any parameters that have concentration or time units will be checked against these definitions and if necessary automatically converted to the expected units.
* Unit(protein_0, 'nM') - the unit 'nM' will be automatically converted to 'uM'since that is the global concentration unit we defined with SimulationUnits, and the appropriate scaling will be applied to protein_0.value.
* Initial(protein, protein_0) the units of protein_0 will be checked to make sure they are a valid concentration.
* Rule('degradation', protein >> None, k_deg) the reaction order of the rule will be determined and the units of k_deg will be checked to make sure they match the expected unit type corresponding to that reaction order. In this case, the degradation is a 1st-order reaction, so k_deg is expected to have
inverse time (i.e., frequency) units: [1 / time], such as, [1 / s] or [1 / h].
* Observable('protein_t', protein()) - here, the units of the observable will be automatically inferred based on those set with SimulationUnits, so the observable will have units of 'uM'.
* Expression('deg_rate', (protein_t * k_deg)) - units of expressions are automatically inferred from the units of parameters and observables, so in this case the Expression will have units of 'uM/s'.
* units.check() - this function applies additional unit checking and will issue warnings for duplicate units (two different units assigned to the same parameter), lack of consistency for units of the same physical type (e.g., concentration, time, etc.), and parameters without any
units context manager
In the previous example we added explicit calls to
the unitize and check functions. If you prefer, you can use the units context manager instead to achieve the same effects:
```python
Import the pysb components we need:
from pysb import Model, Parameter, Monomer, Initial, Observable, Expression
Import the pysb-units context manager:
from pysb.units import units
Activate units using the units context manager -
replaces core model components with the appropriate
versions from pysb.units (similar to unitize) and will
automatically call the check function when exiting the
context:
with units():
# Initialize the PySB model:
Model()
# The primary units needed for simulating the model are
# concentration (or amount) and time. We can define those
# here with SimulationUnits:
SimulationUnits(concentration='uM', time='s')
# Monomer(s):
Monomer('protein')
# Model parameter(s):
# Initial concentration of protein:
Parameter('protein_0', 500.)
# Attach units to protein_0:
Unit(protein_0, 'nM')
# 1st-order rate parameter for the degradation
# defined with frequency (1/time) units - here,
# we chain Unit and Parameter definitions:
Unit(Parameter('k_deg', 0.1), '1/s')
# Initial concentration(s)
Initial(protein, protein_0)
# Reaction rule(s)
# Just the one degradation
Rule('degradation', protein() >> None, k_deg)
# Observables
# Time-dependent protein concentration:
Observable('protein_t', protein())
# Expressions
# The time-dependent degradation rate:
Expression('deg_rate', (protein_t * k_deg))
```
Using units with pysb.macros
PySB contains some helpful macro functions, such as bind and degrade, that can be used to streamline rule creation for recurring motifs. To use these macros with the pysb.units add-on you can use the add_macro_units function as below:
```python
Import the pysb components we need:
from pysb import Model, Parameter, Monomer, Initial, Observable, Expression
Import the module with the wanted macros:
from pysb import macros
Import pysb-units:
import pysb.units as units
Activate units - replaces core model components
with the appropriate versions from pysb.units:
units.unitize()
Apply the units to the macros module:
units.addmacrounits(macros)
Initialize the PySB model:
Model()
The core units used when simulating the model are
concentration (or amount) and time. We can define those
here with SimulationUnits:
SimulationUnits(concentration='uM', time='s')
Monomer(s):
Monomer('protein')
Model parameter(s):
Initial concentration of protein:
Parameter('protein_0', 500.)
Attach units to protein_0:
Unit(protein_0, 'nM')
1st-order rate parameter for the degradation
defined with frequency (1/time) units - here,
we chain Unit and Parameter definitions:
Unit(Parameter('k_deg', 0.1), '1/s')
Initial concentration(s)
Initial(protein, protein_0)
Reaction rule(s)
Just the one degradation - instead
of defining a Rule that encodes the degradation reaction
we can take advantage of degrade macro:
macros.degrade(protein(), k_deg)
Observables
Time-dependent protein concentration:
Observable('protein_t', protein())
Expressions
The time-dependent degradation rate:
Expression('degrate', (proteint * k_deg))
Apply additional unit checks, including unit duplication
and unit consistency checking:
units.check()
```
Stochastic Simulation Units
pysb-units supports stochastic simulation units (number of molecules in place of a molar concentration) at the level of model definition via the set_molecule_volume function and the SimulationUnits object. To enforce automatic conversion from molar concentrations to number of molecules we can update our example model as follows:
```python
Import the pysb components we need:
from pysb import Model, Parameter, Monomer, Initial, Observable, Expression
Import pysb-units:
import pysb.units as units
Activate units - replaces core model components
with the appropriate versions from pysb.units:
units.unitize()
Initialize the PySB model:
Model()
The primary units needed for simulating the model are
concentration (or amount) and time. We can define those
here with SimulationUnits.
In this case, we want stochastic units so we can
set the concentration to 'molecules'
SimulationUnits(concentration='molecules', time='s')
Next, for stochastic units we need to set the volume
for the molar concentration to number of molecules conversion. Let's
Assume a cellular volume of 1 pL:
units.setmoleculevolume(1.0, 'pL')
Monomer(s):
Monomer('protein')
Model parameter(s):
Initial concentration of protein:
Parameter('protein_0', 500.)
Attach units to protein_0:
Unit(protein_0, 'nM')
1st-order rate parameter for the degradation
defined with frequency (1/time) units - here,
we chain Unit and Parameter definitions:
Unit(Parameter('k_deg', 0.1), '1/s')
Initial concentration(s)
Initial(protein, protein_0)
Reaction rule(s)
Just the one degradation
Rule('degradation', protein() >> None, k_deg)
Observables
Time-dependent protein concentration:
Observable('protein_t', protein())
Expressions
The time-dependent degradation rate:
Expression('degrate', (proteint * k_deg))
Apply additional unit checks, including unit duplication
and unit consistency checking:
units.check()
```
Now, when Unit(protein_0, 'uM) is evaluated the concentration of 500 micromolar will be automatically converted to the number of molecules ('molecules' unit).
Note that at the moment, this approach only works for non-compartmental models.
unit keyword for Parameter
The Parameter component can accept an optional keyword argument for the unit, which means you can define the unit along with the parameter without explicitly applying a Unit object to the parameter. So, you can define unit-ed parameters using something like (as of version 0.3.0)
python
Parameter('k_r', 0.1, unit="1/s")
instead of doing (earlier versions)
python
Unit(Parameter('k_r', 0.1), '1/s')
The outcome is the same either way, but the top version is a little more compact and easier to read.
Accessing all the Units for a model
You can get a list of Unit objects defined for a model with the Model.units property:
python
from my_model_with_units import model
print(model.units)
Additional Examples
Additional examples can be found in or imported from pysb.units.examples, including
- bngwiki_simple. - adapted from pysb example pysb.examples.bngwiki_simple:
python from pysb.units.examples.bngwiki_simple import model- jnk3noask1. - adapted from JARM jnk3noask1:
python from pysb.units.examples.jnk3_no_ask1 import model
- jnk3noask1. - adapted from JARM jnk3noask1:
Custom Units
pysb.units leverages the astropy.units package for unit parsing and as its physical units library, but adds the following custom units/unit-types for reaction model use:
- "M" = molar concentration : an alias for mole / L. Includes all fraction orders from femto to milli.
- "molecules" - number of molecules : useful for stochastic simulations.
- "1/cell" = number per cell : useful for stochastic simulations of cell signaling networks.
- "1 / (cell**-1 * s)" = cellular reaction rate : reaction rate corresponding to concentrations in number per cell
- "mcg" = micrograms : alias for "ug", often used in pharmaceuticals.
- "mole / m**2" = mole area density
- "g / s" = mass velocity : for reaction rates where mass is used in place of concentration.
Contact
Issues :bug: : Please open a GitHub Issue to report any problems/bugs with the code or its execution, or to make any feature requests.
Discussions :grey_question: : If you have questions, suggestions, or want to discuss anything else related to the project, feel free to use the pysb-units Discussions board.
Supporting
I'm very happy that you've chosen to use pysb-units. This add-on is a project that I develop and maintain on my own time, independently of the core PySB library, and without external funding. If you've found it helpful, here are a few ways you can support its ongoing development:
- Star :star: : Show your support by starring the pysb-units GitHub repository. It helps increase the project's visibility and lets others know it's useful. It also benefits my motivation to continue improving the package!
- Share :mega: : Sharing
pysb-unitson your social media, forums, or with your network is another great way to support the project. It helps more people discoverpysb-units, which in turn motivates me to keep developing! - Cite :books: : Citing or mentioning this software in your work, publications, or projects is another valuable way to support it. It helps spread the word and acknowledges the effort put into its development, which is greatly appreciated!
- Sponsor :dollar: : Even small financial contributions, such as spotting me the cost of a tea through Ko-fi so I can get my caffeine fix, can make a big difference! Every little bit can help me continue developing this and other open-source projects.
Other Useful Tools
Parameter estimation
Please see packages such as simplePSO, PyDREAM, Gleipnir, or GAlibrate for tools to do PySB model parameter estimation using stochastic optimization or Bayesian Monte Carlo approaches.
PKPD modeling with PySB
If you want to build PKPD models with PySB the pysb-pkpd add-on can help.
PD response models
If you want to separately fit response data independetly of PK data, then the pharmacodynamic-response-models package may also be useful.
PySB model visualization
pyvipr can be used for static and dynamic PySB model visualizations.
Owner
- Name: Borealis BioModeling
- Login: Borealis-BioModeling
- Kind: organization
- Location: United States of America
- Repositories: 1
- Profile: https://github.com/Borealis-BioModeling
Open-source and custom tools for biomodeling, simulation, and data analysis.
Citation (CITATION.cff)
cff-version: 1.2.0 message: "If this software is useful for your work, I'd appreciate it if you cited it as below." authors: - family-names: "Wilson" given-names: "Blake" orcid: "https://orcid.org/0000-0002-8269-2500" title: "pysb-units: Add-on for units and dimensional analysis in PySB models." version: 0.4.1 date-released: 2025-07-05 url: "https://github.com/Borealis-BioModeling/pysb-units"
GitHub Events
Total
- Create event: 2
- Release event: 2
- Issues event: 3
- Push event: 4
Last Year
- Create event: 2
- Release event: 2
- Issues event: 3
- Push event: 4
Issues and Pull Requests
Last synced: 6 months ago
All Time
- Total issues: 3
- Total pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Total issue authors: 1
- Total pull request authors: 0
- Average comments per issue: 0.0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 3
- Pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Issue authors: 1
- Pull request authors: 0
- Average comments per issue: 0.0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- blakeaw (4)
Pull Request Authors
- blakeaw (6)
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 1
- Total downloads: unknown
- Total dependent packages: 0
- Total dependent repositories: 0
- Total versions: 1
- Total maintainers: 1
pypi.org: pysb-units
PySB add-on providing utilities to add units to models.
- Documentation: https://pysb-units.readthedocs.io/
- License: BSD License
-
Latest release: 0.4.1
published 8 months ago
Rankings
Maintainers (1)
Dependencies
- astropy >=5.3.4
- pysb >=1.15.0
- sympy >=1.11.1