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: CodeGen

Default 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_import(line: str)[source]
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]
clear()[source]

Deletes all code lines from the memory of the generator.

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, ...] = expr for a previously-allocated array.

static expr_to_str(expr: str, args: tuple)[source]
static finalize_idx_str(var: ComputeVar, idx: str)[source]
generate_func(func_name: str, to_file: bool = True, **kwargs)[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_func

Whether to include the hist callable in the generated function signature. If None (default), falls back to the backend’s add_hist_arg constructor 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.

generate_func_tail(rhs_var: str = 'dy')[source]
get_fname(f: str) tuple[source]
static get_hist_func(y: ndarray, t0: float = 0.0) DDEHistory[source]
get_op(name: str, **kwargs) dict[source]
get_var(v: ComputeVar)[source]
static register_vars(variables: list)[source]
run(func: Callable, func_args: tuple, T: float, dt: float, dts: float, solver: str, **kwargs) tuple[source]
static to_file(fn: str, **kwargs)[source]
class pyrates.backend.base.base_backend.CodeGen[source]

Bases: object

Generates python code. Can add code lines, line-breaks, indents and remove indents.

add_code_line(code_str)[source]

Add code line string to code.

add_indent()[source]

Add an indent to the code.

add_linebreak()[source]

Add a line-break to the code.

clear()[source]

Deletes all code lines from the memory of the generator.

generate()[source]

Generates a single code string from its history of code additions.

remove_indent()[source]

Remove an indent to the code.

class pyrates.backend.base.base_backend.DDEHistory(y0: ndarray, t0: float = 0.0, max_steps: int | None = None)[source]

Bases: object

Callable 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 ndarray on every update() call via y.copy() — for a 100k-step simulation that was 100k small heap allocations. We replace the _y storage with a single pre-allocated 2-D numpy buffer that holds one row per step; row assignment self._y[i] = y still constitutes a copy, so the “update takes ownership of its y argument” contract is unchanged.

_t stays as a Python list because bisect.bisect_right on a list is noticeably faster than np.searchsorted on 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 shape and dtype of all subsequent history rows.

param t0:

Initial time. History returns y0 for any query t <= 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() raises IndexError past the cap. If None (default) the buffer grows geometrically.

update(t: float, y: ndarray) None[source]

Record state y at time t.

y is copied into the pre-allocated row buffer; the caller may free or overwrite its own y after this call returns.

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.

pyrates.backend.base.base_funcs.identity(x)[source]
pyrates.backend.base.base_funcs.index_1d(x, idx)[source]
pyrates.backend.base.base_funcs.index_2d(x, idx1, idx2)[source]
pyrates.backend.base.base_funcs.index_axis(x, idx=None, axis=0)[source]
pyrates.backend.base.base_funcs.index_range(x, idx1, idx2)[source]
pyrates.backend.base.base_funcs.past(x, tau)[source]