sbmlmath.mathml_parser

SBML mathml to sympy.

Classes

SBMLMathMLParser([sbml_level, sbml_version, ...])

MathML parser for sympy.

class sbmlmath.mathml_parser.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.