PyRates.pyrates.backend¶
pyrates.backend module¶
PyRates backend module including the following sub-modules:
- parser
- Used to parse string-based equations into symbolic backend representations.
- computegraph
- Used to realize hierarchical structure between mathematical equations.
- backend_wrapper
- Interface to the backends available in PyRates.
pyrates.backend.computegraph module¶
This module provides the backend class that should be used to set up any backend in pyrates.
-
class
pyrates.backend.computegraph.
ComputeGraph
(backend: str, **kwargs)[source]¶ Bases:
networkx.classes.multidigraph.MultiDiGraph
Creates a compute graph where nodes are all constants and variables of the network and edges are the mathematical operations linking those variables/constants together to form equations.
-
add_op
(inputs: Union[list, tuple], label: str, expr: sympy.core.expr.Expr, func: Callable, **kwargs)[source]¶
-
run
(func: Callable, func_args: tuple, T: float, dt: float, dts: Optional[float] = None, outputs: Optional[dict] = None, **kwargs)[source]¶
-
state_vars
¶
-
-
class
pyrates.backend.computegraph.
ComputeNode
(name: str, symbol: Union[sympy.core.symbol.Symbol, sympy.core.expr.Expr, sympy.core.function.Function], dtype: Optional[str] = None, shape: Optional[tuple] = None, def_shape: Optional[tuple] = None)[source]¶ Bases:
object
Base class for adding variables to the compute graph. Creates a numpy array with additional attributes for variable identification/retrieval from graph. Should be used as parent class for custom variable classes.
Parameters: - name – Full name of the variable in the original NetworkGraph (including the node and operator it belongs to).
- dtype – Data-type of the variable. For valid data-types, check the documentation of the backend in use.
- shape – Shape of the variable.
-
dtype
¶
-
is_complex
¶
-
is_constant
¶
-
is_float
¶
-
name
¶
-
shape
¶
-
symbol
¶
-
value
¶ Returns current value of BaseVar.
-
class
pyrates.backend.computegraph.
ComputeOp
(name: str, symbol: Union[sympy.core.symbol.Symbol, sympy.core.expr.Expr, sympy.core.function.Function], func: Callable, expr: sympy.core.expr.Expr, dtype: Optional[str] = None, shape: Optional[str] = None)[source]¶ Bases:
pyrates.backend.computegraph.ComputeNode
Class for ComputeGraph nodes that represent mathematical operations.
-
dtype
¶
-
expr
¶
-
func
¶
-
is_constant
¶
-
name
¶
-
shape
¶
-
symbol
¶
-
-
class
pyrates.backend.computegraph.
ComputeVar
(name: str, symbol: Union[sympy.core.symbol.Symbol, sympy.core.expr.Expr, sympy.core.function.Function], vtype: str, dtype: Optional[str] = None, shape: Optional[str] = None, value: Union[list, numpy.ndarray, None] = None, def_shape: Optional[tuple] = None)[source]¶ Bases:
pyrates.backend.computegraph.ComputeNode
Class for variables and vector-valued constants in the ComputeGraph.
-
dtype
¶
-
is_constant
¶
-
name
¶
-
shape
¶
-
symbol
¶
-
vtype
¶
-
pyrates.backend.parser module¶
This module provides parser classes and functions to parse string-based equations into symbolic representations of operations.
-
class
pyrates.backend.parser.
ExpressionParser
(expr_str: str, args: dict, cg: pyrates.backend.computegraph.ComputeGraph, def_shape: Optional[tuple] = None, parsing_method: str = 'sympy')[source]¶ Bases:
object
Class for parsing mathematical expressions from a string format into a symbolic representation of the mathematical operation expressed by it.
Parameters: - expr_str – Mathematical expression in string format.
- args – Dictionary containing all variables and functions needed to evaluate the expression.
- cg – ComputeGraph instance in which to parse all variables and operations. See pyrates.backend.computegraph.ComputeGraph for a full documentation of its methods and attributes.
- parsing_method – Name of the parsing method to use. Valid options: sympy for a sympy-based parser, pyrates for the pyrates-internal, pyparsing-based parser.
-
lhs
¶ Boolean, indicates whether expression is left-hand side or right-hand side of an equation
-
rhs
¶ PyRatesOp for the evaluation of the right-hand side of the equation
-
vars
¶ Dictionary containing the variables of an expression
-
expr_str
¶ String representation of the mathematical expression
-
expr_stack
¶ List representation of the syntax tree of the (parsed) mathematical expression.
-
parse_func
¶ Function that will be used to deduce an operation stack/tree from a given expression.
-
cg
¶ Passed ComputeGraph instance into which the expression will be parsed.
-
pyrates.backend.parser.
is_diff_eq
(eq: str) → bool[source]¶ Checks whether eq is a differential equation or not.
Parameters: eq – Equation string. Returns: True, if eq is a differential equation. Return type: bool
-
pyrates.backend.parser.
parse_equations
(equations: list, equation_args: dict, cg: pyrates.backend.computegraph.ComputeGraph, def_shape: tuple, **kwargs) → dict[source]¶ Parses a system (list) of equations into the backend. Transforms differential equations into the appropriate set of right-hand side evaluations that can be solved later on.
Parameters: - equations – Collection of equations that describe the dynamics of the nodes and edges.
- equation_args – Key-value pairs of arguments needed for parsing the equations.
- cg – ComputeGraph instance that all equations will be parsed into.
- def_shape – Default shape of variables that are scalar. Can either be (1,) or ().
- kwargs – Additional keyword arguments to be passed to the backend methods.
Returns: The updated equations args (in-place manipulation of all variables in equation_args happens during equation parsing).
Return type: dict
-
pyrates.backend.parser.
replace
(eq: str, term: str, replacement: str, rhs_only: Optional[bool] = False, lhs_only: Optional[bool] = False) → str[source]¶ Replaces a term in an equation with a replacement term (save replacement).
Parameters: - eq – Equation that includes the term.
- term – Term that should be replaced.
- replacement – Replacement for all occurences of term.
- rhs_only – If True, replacements will only be performed in right-hand side of the equation.
- lhs_only – IF True, replacements will only be performed in left-hand side of the equation.
Returns: The updated equation.
Return type: str
-
pyrates.backend.parser.
split_equation
(expr: str) → tuple[source]¶ Splits an equation string into a left-hand side, right-and side and an assign type.
Parameters: expr – Equation string. Should contain a left-hand side and a right-hand side, separated by some form of assign symbol. Returns: left-hand side string, right-hand side string, assign operation string. Return type: tuple