pyrates

PyRates.pyrates.backend

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]
add_var(label: str, value: Any, vtype: str, **kwargs)[source]
add_var_update(var: str, update: str, differential_equation: bool = False)[source]
clear() → None[source]

Deletes build directory and removes all compute graph nodes

compile()[source]
eval_graph()[source]
eval_node(n)[source]
eval_nodes(nodes: Iterable[T_co])[source]
eval_subgraph(n)[source]
get_op(op: str, **kwargs) → dict[source]
get_var(var: str, from_backend: bool = False)[source]
remove_subgraph(n)[source]
run(func: Callable, func_args: tuple, T: float, dt: float, dts: Optional[float] = None, outputs: Optional[dict] = None, **kwargs)[source]
state_vars
to_func(func_name: str, to_file: bool = True, dt_adapt: bool = True, **kwargs)[source]
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
reshape(shape: tuple, **kwargs)[source]
set_value(v: Union[float, numpy.ndarray])[source]
shape
squeeze(axis=None)[source]
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.Algebra(**kwargs)[source]

Bases: object

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.

parse_expr() → dict[source]

Parses string-based mathematical expression/equation.

Returns:Variables of the parsed equation.
Return type:dict
pyrates.backend.parser.extract_var(var: str) → tuple[source]
pyrates.backend.parser.get_unique_label(label: str, labels: list) → str[source]
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.replace_in_expr(expr: sympy.core.expr.Expr, replacements: dict)[source]
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
pyrates.backend.parser.var_in_expression(var: str, expr: str) → bool[source]