dcclib.extraction.formulae ¶
DCCFunction
dataclass
¶
DCCFunction(name: str, expression: str | Expr, variables: dict[str, Value], bound_variables: set[str])
Bases: Generic[T]
Represents a named, parameterised DCC formula.
combinations
property
¶
combinations: list[Any]
Return one lambdified function per evaluation step (one per combination index).
The number of entries matches the number of results "evaluate()" would return.
FormulaExtractor ¶
FormulaExtractor(element: etree.Element)
Bases: Constructible, Extractable
extract ¶
extract() -> list[DCCFunction[DccQuantityType]]
Extract formulas from the XML. @return: a list of DCCFunction objects
from_file
classmethod
¶
from_file(path: str)
Create a FormulaExtractor from a file path. @param path: the path to the file @return: the FormulaExtractor
from_str
classmethod
¶
from_str(xml: str)
Create a FormulaExtractor from a string. @param xml: the XML string @return: the FormulaExtractor
from_tree
classmethod
¶
from_tree(tree: etree.Element)
Create a FormulaExtractor from an ElementTree. @param tree: the ElementTree @return: the FormulaExtractor
format_quantity ¶
format_quantity(value: Any) -> tuple[str, str]
Return (value_str, unit_str) for a DccQuantityType or plain value.
quantity_fields ¶
quantity_fields(value: Any) -> dict[str, Any]
Return machine-readable fields for a DccQuantityType or plain value.
Keys: values (list of floats), unit (str or None), and
uncertainties (list of floats or None). Scalars are returned as
single-element lists so consumers can treat all quantities uniformly.
wrap_variable_value ¶
wrap_variable_value(raw_value: float | list[float], original_var: Any) -> Any
Wrap a user-provided value in a DccQuantityType, inheriting the unit from the original formula variable so that unit-aware arithmetic stays consistent. Returns a plain float when the original variable has no unit information.
formula ¶
DCCFunction
dataclass
¶
DCCFunction(name: str, expression: str | Expr, variables: dict[str, Value], bound_variables: set[str])
Bases: Generic[T]
Represents a named, parameterised DCC formula.
combinations
property
¶
combinations: list[Any]
Return one lambdified function per evaluation step (one per combination index).
The number of entries matches the number of results "evaluate()" would return.
format_quantity ¶
format_quantity(value: Any) -> tuple[str, str]
Return (value_str, unit_str) for a DccQuantityType or plain value.
quantity_fields ¶
quantity_fields(value: Any) -> dict[str, Any]
Return machine-readable fields for a DccQuantityType or plain value.
Keys: values (list of floats), unit (str or None), and
uncertainties (list of floats or None). Scalars are returned as
single-element lists so consumers can treat all quantities uniformly.
sub_len ¶
sub_len(values: list[Value]) -> int
Return the common length of all multi-value items in values.
Scalars (len == 1) are compatible with any length. Raises "ValueError" on empty sublists or mismatched lengths.
wrap_variable_value ¶
wrap_variable_value(raw_value: float | list[float], original_var: Any) -> Any
Wrap a user-provided value in a DccQuantityType, inheriting the unit from the original formula variable so that unit-aware arithmetic stays consistent. Returns a plain float when the original variable has no unit information.
formula_extractor ¶
FormulaExtractor ¶
FormulaExtractor(element: etree.Element)
Bases: Constructible, Extractable
extract ¶
extract() -> list[DCCFunction[DccQuantityType]]
Extract formulas from the XML. @return: a list of DCCFunction objects
from_file
classmethod
¶
from_file(path: str)
Create a FormulaExtractor from a file path. @param path: the path to the file @return: the FormulaExtractor
from_str
classmethod
¶
from_str(xml: str)
Create a FormulaExtractor from a string. @param xml: the XML string @return: the FormulaExtractor
from_tree
classmethod
¶
from_tree(tree: etree.Element)
Create a FormulaExtractor from an ElementTree. @param tree: the ElementTree @return: the FormulaExtractor
mathml_sympy_converter ¶
convert_mathml_apply ¶
convert_mathml_apply(apply: etree.Element) -> sympy.Expr
Recursively evaluate a MathML 'apply' node into a SymPy expression.
It parses the operands and arguments, handling special MathML tags like 'bvar', 'lowlimit', 'uplimit', 'logbase', and 'degree' by passing them as keyword arguments to the mapped SymPy operation.
:param apply: The MathML 'apply' element. :return: The resulting SymPy expression. :raises NotImplementedError: If the MathML operand is not supported.
convert_mathml_node ¶
convert_mathml_node(node: etree.Element) -> DCCFunction | None
Process a single MathML element and return a DCCFunction object.
:param node: A lxml etree Element from a MathML formula. :return: A DCCFunction object or None if the element is not a function declaration.
get_localname ¶
get_localname(txt: str) -> str
Extract the local name from a Clark-notation XML tag (e.g., '{uri}tag' -> 'tag').
:param txt: The XML tag string. :return: The local name of the tag.
get_node_value ¶
get_node_value(node: etree.Element) -> sympy.Expr
Evaluate a MathML node and return its SymPy representation.
This function handles 'apply' nodes (recursive calls), 'cn' (numbers), and 'ci' (symbols/identifiers).
:param node: The lxml etree Element to evaluate. :return: A SymPy Expression (Float, Symbol, or result of an operation).
handle_mathml_fn ¶
handle_mathml_fn(fn_node) -> tuple[str, sympy.Expr, set[str]]
Handle a MathML function declaration.
:param fn_node: The MathML 'declare' element. :return: A tuple containing the function name, expression, and bound variables. :raises ValueError: If the function node is invalid.