sbmlmath¶
Functions
|
Convert SBML MathML to sympy expression. |
|
Set the math expression of an SBML object. |
|
Convert sympy expression to SBML math ASTNode. |
- class sbmlmath.CFunction(*args, definition_url=None, encoding='text', **kwargs)[source]¶
Bases:
UndefinedFunctionRepresents
<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:
DummyRepresents 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.
- class sbmlmath.Delay(*args, **kwargs)[source]¶
Bases:
CFunctionProduces 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:
CFunctionProduces 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:
objectMathML 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.ureg (
UnitRegistry) – Optionalpint.UnitRegistryto 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. IfFalse, all math elements with units are converted topint.Quantityobjects.symbol_kwargs – Additional keyword arguments for constructing
sympy.Symbol. For example, for passing custom assumptions such asreal=True.
- handle_ci(element)[source]¶
Handle identifiers.
See also: numerical constants in MathML.
- Return type:
- Parameters:
element (_Element)
- handle_cn(element)[source]¶
Handle numbers.
See also: numerical constants in MathML.
- Return type:
- 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 MathMLmathelement.- Return type:
- 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 MathMLmathelement.- Returns:
The sympy representation of the MathML expression.
- class sbmlmath.SBMLMathMLPrinter(*args, literals_dimensionless=True, sbml_level=3, sbml_version=2, **kwargs)[source]¶
Bases:
MathMLContentPrinterMathML code printer.
This printer converts SymPy expressions to MathML code that can be used in SBML models.
Note:
assumes all constants are dimensionless
- 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:
>>> 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:
DummyRepresents 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.Dummyto avoid caching issues, due to using the same name but with different attributes / meanings.- Parameters:
>>> 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:
CSymbolThe 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:
- 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:
- 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:
- Returns:
The resulting SBML math ASTNode.
- Raises:
ValueError – If there is an error in converting the math expression.
Modules
Handling of |
|
Handling of <csymbol> constants |
|
SBML mathml to sympy. |
|
Convenience functions for libsbml core |
|
SBML MathML related functionality |