sbmlmath

Functions

sbml_math_to_sympy(sbml_obj, **kwargs)

Convert SBML MathML to sympy expression.

set_math(element, expr)

Set the math expression of an SBML object.

sympy_to_sbml_math(sp_expr)

Convert sympy expression to SBML math ASTNode.

class sbmlmath.CFunction(*args, definition_url=None, encoding='text', **kwargs)[source]

Bases: UndefinedFunction

Represents <csymbol> functions.

Examples: rateOf(), delay(), distributions from the distrib package.

See also https://www.w3.org/TR/MathML2/chapter4.html#contm.deffun.

class sbmlmath.CSymbol(*args, definition_url, encoding='text', **kwargs)[source]

Bases: Dummy

Represents a <csymbol> element.

Represents, for example the Avogadro constant, which is defined in SBML as:

<csymbol encoding="text" definitionURL="http://www.sbml.org/sbml/symbols/avogadro"> avogadro </csymbol>

and can be generated by:

>>> CSymbol("avogadro", definition_url="http://www.sbml.org/sbml/symbols/avogadro")
<avogadro(http://www.sbml.org/sbml/symbols/avogadro)>
>>> float(CSymbol("avogadro", definition_url="http://www.sbml.org/sbml/symbols/avogadro"))
6.02214179e+23

See also https://www.w3.org/TR/MathML2/chapter4.html#contm.csymbol.

Parameters:
  • definition_url (str)

  • encoding (str)

class sbmlmath.Delay(*args, **kwargs)[source]

Bases: CFunction

Produces a SBML delay() function.

Usually, it’s preferable to use the delay() function. This class can be used if a delay function with a different name is needed.

Examples

>>> from sympy.abc import a
>>> my_delay = Delay("my_delay")
>>> my_delay(a)
my_delay(a)
>>> delay(a) == my_delay(a)
True
class sbmlmath.RateOf(*args, **kwargs)[source]

Bases: CFunction

Produces a SBML rateOf() function.

Usually, it’s preferable to use the rate_of() function. This class can be used if a rateOf function with a different name is needed.

Examples

>>> from sympy.abc import a
>>> my_rate_of = RateOf("my_rate_of")
>>> my_rate_of(a)
my_rate_of(a)
>>> rate_of(a) == my_rate_of(a)
True
>>> rate_of(1)
0
class sbmlmath.SBMLMathMLParser(sbml_level=3, sbml_version=2, ureg=None, floats_as_rationals=True, ignore_units=False, symbol_kwargs=None, evaluate=False)[source]

Bases: object

MathML parser for sympy.

Parses the SBML subset of MathML 2.0.

For details, see SBML spec section 3.4: https://raw.githubusercontent.com/combine-org/combine-specifications/main/specifications/files/sbml.level-3.version-2.core.release-2.pdf.

See also: MathML 2.0 specification.

Parameters:
  • sbml_level (int | str) – SBML level. Note that SBML level < 3 has not been thoroughly tested.

  • sbml_version (int | str) – SBML version.

  • ureg (UnitRegistry) – Optional pint.UnitRegistry to use for unit conversion.

  • floats_as_rationals – Whether to convert floats to sympy.Rational. Improves precision.

  • ignore_units – Whether to ignore units. If True, all units are ignored and all numbers are converted to plain numbers. If False, all math elements with units are converted to pint.Quantity objects.

  • symbol_kwargs – Additional keyword arguments for constructing sympy.Symbol. For example, for passing custom assumptions such as real=True.

handle_apply(element)[source]

Handle <apply>

Return type:

Expr

Parameters:

element (_Element)

handle_ci(element)[source]

Handle identifiers.

See also: numerical constants in MathML.

Return type:

Expr

Parameters:

element (_Element)

handle_cn(element)[source]

Handle numbers.

See also: numerical constants in MathML.

Return type:

Expr

Parameters:

element (_Element)

parse_file(file_like)[source]

Parse a file-like object containing MathML.

Parameters:

file_like – File-like object (file, filename, …) containing MathML. Expected to contain the XML prolog <?xml [...]?> and the MathML math element.

Return type:

Expr

Returns:

The sympy representation of the MathML expression.

parse_str(mathml)[source]

Parse a string containing MathML.

Parameters:

mathml (str) – MathML string. Expected to contain the XML prolog <?xml [...]?> and the MathML math element.

Returns:

The sympy representation of the MathML expression.

preprocess_symbol_name(name, element=None)[source]

Preprocess symbol names.

Override this method to preprocess symbol names before parsing. For example, to handle reserved names.

Does nothing by default.

Parameters:
  • name (str) – Symbol name in the MathML element.

  • element (_Element) – MathML element.

Return type:

str

Returns:

Preprocessed symbol name.

class sbmlmath.SBMLMathMLPrinter(*args, literals_dimensionless=True, sbml_level=3, sbml_version=2, **kwargs)[source]

Bases: MathMLContentPrinter

MathML code printer.

This printer converts SymPy expressions to MathML code that can be used in SBML models.

Note:

  • assumes all constants are dimensionless

Parameters:
  • sbml_level (int)

  • sbml_version (int)

doprint(expr, with_prolog=True, with_math=True)[source]

Convert SymPy expression to MathML string.

Parameters:
  • expr – The SymPy expression to be converted.

  • with_prolog – Whether to include the XML prolog.

  • with_math – Whether to include the <math> tags.

Return type:

str

>>> SBMLMathMLPrinter().doprint(sp.sympify("3 * a"))
'<?xml version="1.0" encoding="UTF-8"?>\n<math xmlns="http://www.w3.org/1998/Math/MathML" xmlns:sbml="http://www.sbml.org/sbml/level3/version2/core">\n<apply><times/><cn type="integer" sbml:units="dimensionless">3</cn><ci>a</ci></apply></math>'
>>> SBMLMathMLPrinter().doprint(sp.sympify("cbrt(3)"), with_math=False, with_prolog=False)
'<apply><root/><degree><cn>3</cn></degree><cn type="integer" sbml:units="dimensionless">3</cn></apply>'
class sbmlmath.SpeciesSymbol(*args, representation_type=None, species_reference=None, **kwargs)[source]

Bases: Dummy

Represents a <ci> element containing a species.

Represents, for example, <ci multi:representationType="sum"> bla </ci> or <ci multi:speciesReference="refS"> S </ci> (see SBML multi specs 3.26).

Subclassed from sympy.Dummy to avoid caching issues, due to using the same name but with different attributes / meanings.

Parameters:
  • representation_type (Literal['sum', 'numericValue'] | None) – What the species symbol represents (see also SBML-multi spec 3.26.2).

  • species_reference (str) – ID of a species reference in the same reaction (see also SBML-multi spec 3.26.1).

>>> SpeciesSymbol("S") + SpeciesSymbol("S", representation_type="sum")
_S + _S
>>> SpeciesSymbol("S") == SpeciesSymbol("S", representation_type="sum")
False
>>> SpeciesSymbol("ref_to_S", species_reference="S")
<ref_to_S(species_reference=S)>
class sbmlmath.TimeSymbol(name)[source]

Bases: CSymbol

The current internal simulation time.

This symbol represents the current simulation time inside the model.

>>> TimeSymbol("t")
<t(http://www.sbml.org/sbml/symbols/time)>
Parameters:

name (str) – The name of the symbol.

class sbmlmath.delay(*args)

Bases: AppliedUndef

sbmlmath.rate_of

alias of rateOf

sbmlmath.sbml_math_to_sympy(sbml_obj, **kwargs)[source]

Convert SBML MathML to sympy expression.

Conversion is done using the default settings of SBMLMathMLParser.

Parameters:
  • sbml_obj (libsbml.SBase | libsbml.ASTNode) – The SBML object to be converted. (Either directly the ASTNode or the surrounding SBase object).

  • kwargs – Additional keyword arguments passed to SBMLMathMLParser.__init__.

Return type:

Expr

Returns:

The resulting sympy expression.

sbmlmath.set_math(element, expr)[source]

Set the math expression of an SBML object.

Parameters:
  • element (libsbml.SBase) – The SBML object to set the math expression for.

  • expr (Expr) – The sympy expression to set as the math expression.

Return type:

None

sbmlmath.sympy_to_sbml_math(sp_expr)[source]

Convert sympy expression to SBML math ASTNode.

This function takes a SymPy expression and converts it to an SBML math ASTNode.

Parameters:

sp_expr (Expr) – The SymPy expression to be converted.

Return type:

libsbml.ASTNode

Returns:

The resulting SBML math ASTNode.

Raises:

ValueError – If there is an error in converting the math expression.

Modules

cfunction

Handling of <csymbol> functions

csymbol

Handling of <csymbol> constants

mathml_parser

SBML mathml to sympy.

mathml_printer

Convenience functions for libsbml core

species_symbol

SBML MathML related functionality