1.1. PyRates.pyrates.backend
1.1.1. 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.
- 1.1.1.1. PyRates.pyrates.backend.base
- 1.1.1.1.1. pyrates.backend.base module
- 1.1.1.1.2. pyrates.backend.base.base_backend module
BaseBackend
BaseBackend.add_import()
BaseBackend.add_var_hist()
BaseBackend.add_var_update()
BaseBackend.clear()
BaseBackend.create_index_str()
BaseBackend.expr_to_str()
BaseBackend.finalize_idx_str()
BaseBackend.generate_func()
BaseBackend.generate_func_head()
BaseBackend.generate_func_tail()
BaseBackend.get_fname()
BaseBackend.get_hist_func()
BaseBackend.get_op()
BaseBackend.get_var()
BaseBackend.register_vars()
BaseBackend.run()
BaseBackend.to_file()
CodeGen
- 1.1.1.1.3. pyrates.backend.base.base_funcs module
- 1.1.1.2. PyRates.pyrates.backend.fortran
- 1.1.1.2.1. pyrates.backend.fortran module
- 1.1.1.2.2. pyrates.backend.fortran.fortran_backend module
FortranBackend
FortranBackend.add_code_line()
FortranBackend.add_var_update()
FortranBackend.break_line()
FortranBackend.clear()
FortranBackend.create_index_str()
FortranBackend.expr_to_str()
FortranBackend.generate_func()
FortranBackend.generate_func_head()
FortranBackend.generate_func_tail()
FortranBackend.linebreak_end
FortranBackend.linebreak_start
FortranBackend.n1
FortranBackend.n2
FortranBackend.register_vars()
- 1.1.1.2.3. pyrates.backend.fortran.fortran_funcs module
- 1.1.1.3. PyRates.pyrates.backend.julia
- 1.1.1.4. PyRates.pyrates.backend.tensorflow
- 1.1.1.5. PyRates.pyrates.backend.torch
1.1.2. 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:
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.
- run(func: Callable, func_args: tuple, T: float, dt: float, dts: float | None = None, outputs: dict | None = None, **kwargs) dict [source]
- property state_vars
- class pyrates.backend.computegraph.ComputeGraphBackProp(backend: str, **kwargs)[source]
Bases:
ComputeGraph
- class pyrates.backend.computegraph.ComputeNode(name: str, symbol: Symbol | Expr | Function, dtype: str | None = None, shape: tuple | None = None, def_shape: tuple | None = 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
- property is_complex
- property is_constant
- property is_float
- name
- shape
- symbol
- property value
Returns current value of BaseVar.
- class pyrates.backend.computegraph.ComputeOp(name: str, symbol: Symbol | Expr | Function, func: Callable, expr: Expr, dtype: str | None = None, shape: str | None = None)[source]
Bases:
ComputeNode
Class for ComputeGraph nodes that represent mathematical operations.
- dtype
- expr
- func
- property is_constant
- name
- shape
- symbol
- class pyrates.backend.computegraph.ComputeVar(name: str, symbol: Symbol | Expr | Function, vtype: str, dtype: str | None = None, shape: str | None = None, value: list | ndarray | None = None, def_shape: tuple | None = None)[source]
Bases:
ComputeNode
Class for variables and vector-valued constants in the ComputeGraph.
- dtype
- property is_constant
- name
- shape
- symbol
- vtype
1.1.3. 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: ComputeGraph, def_shape: tuple | None = 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: 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: bool | None = False, lhs_only: bool | None = 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