1.1.1.1. PyRates.pyrates.backend.base
1.1.1.1.1. pyrates.backend.base module
default PyRates backend. Contains all pyrates-internal function definitions and links to numpy functions.
1.1.1.1.2. pyrates.backend.base.base_backend module
Contains wrapper classes for different backends that are needed by the parser module.
A new backend needs to implement the following methods: - __init__ - run - add_var - add_op - add_layer
Currently supported backends: - Numpy: BaseBackend. - Torch: TorchBackend. - Fortran: FortranBackend (experimental). - Julia: JuliaBackend. - Matlab: MatlabBackend.
- class pyrates.backend.base.base_backend.BaseBackend(ops: Dict[str, str] | None = None, imports: List[str] | None = None, **kwargs)[source]
Bases:
CodeGenDefault backend class. Transforms all network equations into their numpy equivalents. Based on a Python code generator.
- SUPPORTED_SOLVERS: Tuple[str, ...] = ('euler', 'heun', 'scipy')
- SUPPORTS_EDGE_DELAY_BUFFER: bool = True
- SUPPORTS_SPARSE_JACOBIAN: bool = True
- add_var_hist(lhs: str, delay: ComputeVar | float, state_idx: str, dt: float | None = None, dt_adapt: bool = True, **kwargs)[source]
- add_var_update(lhs: ComputeVar, rhs: str, lhs_idx: str | None = None, rhs_shape: tuple | None = ())[source]
- create_index_str(idx: str | int | tuple, separator: str = ',', apply: bool = True, **kwargs) Tuple[str, dict][source]
- declare_local_array_imports() None[source]
Register the imports needed by
emit_local_array_alloc().
- emit_local_array_alloc(name: str, shape: tuple, dtype: str) None[source]
Emit
name = zeros(shape, dtype=dtype).Requires
declare_local_array_imports()to have been called earlier.
- emit_local_array_assign(name: str, indices: tuple, expr: str) None[source]
Emit
name[i, j, ...] = exprfor a previously-allocated array.
- static finalize_idx_str(var: ComputeVar, idx: str)[source]
- generate_func_head(func_name: str, state_var: str = 'y', return_var: str = 'dy', func_args: list | None = None, add_hist_func: bool | None = None)[source]
Generate the function header for the RHS file.
add_hist_funcWhether to include the
histcallable in the generated function signature. IfNone(default), falls back to the backend’sadd_hist_argconstructor flag — making that flag the single source of truth. Callers that already know whether the model is a DDE (e.g.ComputeGraph.to_func) pass the resolved bool explicitly and skip the fallback.
- static get_hist_func(y: ndarray, t0: float = 0.0) DDEHistory[source]
- get_var(v: ComputeVar)[source]
- class pyrates.backend.base.base_backend.CodeGen[source]
Bases:
objectGenerates python code. Can add code lines, line-breaks, indents and remove indents.
- class pyrates.backend.base.base_backend.DDEHistory(y0: ndarray, t0: float = 0.0, max_steps: int | None = None)[source]
Bases:
objectCallable history buffer for delay-differential equations.
Stores (time, state) pairs and returns linearly-interpolated past state. Pre-history (t <= t0) always returns the initial condition.
1.1.1.1. Implementation
The previous list-of-ndarrays implementation (PyRates <= 1.1.x) allocated a fresh
ndarrayon everyupdate()call viay.copy()— for a 100k-step simulation that was 100k small heap allocations. We replace the_ystorage with a single pre-allocated 2-D numpy buffer that holds one row per step; row assignmentself._y[i] = ystill constitutes a copy, so the “update takes ownership of its y argument” contract is unchanged._tstays as a Python list becausebisect.bisect_righton a list is noticeably faster thannp.searchsortedon the equivalent numpy view when called once per RHS evaluation in the integration loop (per-call CPython overhead is the dominant cost here, not the underlying binary search).- param y0:
Initial state vector. Defines the
shapeanddtypeof all subsequent history rows.- param t0:
Initial time. History returns
y0for any queryt <= t0.- param max_steps:
Optional hard cap on the number of stored steps. If given, the buffer is allocated once at this size and
update()raisesIndexErrorpast the cap. IfNone(default) the buffer grows geometrically.
- pyrates.backend.base.base_backend.clear_compile_cache() None[source]
Drop all cached compiled RHS modules.
Call this if you have generated many distinct models in a long-running process and want to reclaim memory. Has no effect on already-returned callables; only future
BaseBackend.generate_func()calls are affected.
1.1.1.1.3. pyrates.backend.base.base_funcs module
Contains function definitions that may be used for PyRates model equations.