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
BaseBackendBaseBackend.SUPPORTED_SOLVERSBaseBackend.SUPPORTS_EDGE_DELAY_BUFFERBaseBackend.SUPPORTS_SPARSE_JACOBIANBaseBackend.add_import()BaseBackend.add_var_hist()BaseBackend.add_var_update()BaseBackend.clear()BaseBackend.create_index_str()BaseBackend.declare_local_array_imports()BaseBackend.emit_local_array_alloc()BaseBackend.emit_local_array_assign()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()
CodeGenDDEHistoryclear_compile_cache()
- 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
FortranBackendFortranBackend.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_endFortranBackend.linebreak_startFortranBackend.n1FortranBackend.n2FortranBackend.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:
MultiDiGraphCreates 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: list | tuple, label: str, expr: Expr, func: Callable | None = None, func_args: list | None = None, backend_funcs: dict | None = None, **kwargs)[source]
- get_jacobian_func(func_name: str, to_file: bool = True, sparse: bool = False, dt_adapt: bool = True, dt: float | None = None, **kwargs) tuple[source]
Generate a function that evaluates the Jacobian of the vector field.
For ODE systems the generated function has signature
J(t, y, *params) -> ndarray (n, n). For DDE systems it returns(J0, [J_tau1, J_tau2, ...])whereJ0is the instantaneous Jacobian andJ_tauKis the partial derivative with respect toy(t - tau_k). Ifsparse=Trueeach matrix is ascipy.sparse.csr_matrixinstead.The Jacobian entries are computed symbolically via
sympy.diffusing the reconstructed vector-field expressions from the compute graph. Vector-valued state variables fall back to zero blocks (aUserWarninglists the affected indices).- Parameters:
func_name – Name of the generated function.
to_file – Write source to a file (same semantics as
to_func).sparse – Return
scipy.sparse.csr_matrixinstead of densendarray.dt_adapt – Whether the circuit uses adaptive time-stepping (controls
tdtype).dt – Fixed time-step (passed through to backend).
- Returns:
(func, args, arg_names, state_var_indices)— same structure asto_func.- Return type:
tuple
- 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 = (), def_shape: tuple = ())[source]
Bases:
objectBase 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, expr: Expr, func: Callable | None = None, func_args: list | None = None, backend_funcs: dict | None = None, dtype: str | None = None, shape: tuple = ())[source]
Bases:
ComputeNodeClass for ComputeGraph nodes that represent mathematical operations.
- backend_funcs
- dtype
- expr
- func
- func_args
- property is_constant
- name
- shape
- symbol
- class pyrates.backend.computegraph.ComputeVar(name: str, symbol: Symbol | Expr | Function, vtype: str, dtype: str | None = None, shape: tuple = (), value: list | ndarray | None = None, def_shape: tuple = ())[source]
Bases:
ComputeNodeClass 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 = (), parsing_method: str = 'sympy')[source]
Bases:
objectClass 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