Skip to content

sbmlmath

Conversion of CellML maths to libsbml ASTs.

The libcellml analyser gives every equation as a binary tree of AnalyserEquationAst nodes with the variables resolved. ast_to_sbml walks such a tree into a libsbml.ASTNode; the variable of integration becomes the SBML time symbol, variables get their SBML id. mathml_to_sbml reads the MathML of a reset with libsbml and remaps the ci names the same way.

The analyser AST has the values of the numbers but not their units. NumberUnits reads them from the MathML of the components and finds the equation of an analyser AST by its variables and numbers.

MathConversionError

Bases: ValueError

A CellML expression cannot be converted to SBML.

NumberUnits

NumberUnits(model, ids, unit_id)

Units of the numbers of the equations of a CellML model.

The equations are read from the MathML of the components. An analyser AST is matched with the equation which has the same variables and numbers in the same order; the analyser may have rearranged an equation, which is then found by its variables and numbers in any order and gives the units by the value of the numbers. An equation is handed out once, so equations which only differ in their units stay apart.

Read the numbers of all equations.

Parameters:

Name Type Description Default
model Model

the (flattened) CellML model.

required
ids VariableIds

SBML ids of the model.

required
unit_id Callable[[str], str]

SBML unit id of a CellML units name.

required

units_of

units_of(ast)

Units of the numbers of an equation.

Parameters:

Name Type Description Default
ast Any

root of the analyser AST of the equation.

required

Returns:

Type Description
_Units

The units of its numbers from the left to the right, for

_Units

ast_to_sbml; None when the AST has no numbers or the

_Units

equation is not found (logged).

variable_node

variable_node(variable, ids)

AST node referencing a CellML variable.

Parameters:

Name Type Description Default
variable Any

libcellml variable.

required
ids VariableIds

SBML ids of the model.

required

Returns:

Type Description
ASTNode

The time symbol for the variable of integration, else the name node

ASTNode

with the SBML id.

count_numbers

count_numbers(node)

Number of cn nodes of an analyser AST.

Parameters:

Name Type Description Default
node Any

libcellml.AnalyserEquationAst, may be None.

required

Returns:

Type Description
int

How many units of NumberUnits.units_of belong to node.

ast_to_sbml

ast_to_sbml(node, ids, units=None)

Convert an analyser AST into a libsbml AST.

Parameters:

Name Type Description Default
node Any

libcellml.AnalyserEquationAst, e.g. the right child of the EQUALITY root of an equation.

required
ids VariableIds

SBML ids of the model.

required
units _Units

SBML units of the numbers below node, from the left to the right (NumberUnits.units_of); a number takes the next one, no units when it is None.

None

Returns:

Type Description
ASTNode

The libsbml AST.

Raises:

Type Description
MathConversionError

for a node type without SBML counterpart, e.g. DIFF or BVAR outside the left side of an ODE.

mathml_to_sbml

mathml_to_sbml(mathml, component_name, ids, unit_id=None)

Read CellML MathML with libsbml and remap the variable names.

Used for the maths of resets, which the analyser does not cover. Units on numbers (cellml:units) become sbml:units.

Parameters:

Name Type Description Default
mathml str

complete math element.

required
component_name str

component the MathML belongs to, resolves the names.

required
ids VariableIds

SBML ids of the model.

required
unit_id Callable[[str], str] | None

SBML unit id of a CellML units name, the name itself when None.

None

Returns:

Type Description
ASTNode

The libsbml AST.

Raises:

Type Description
MathConversionError

if the MathML does not parse or a name is unknown.