Skip to main content

sc_neurocore_engine/bindings/
pernarowski.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Commercial license available
3// © Concepts 1996–2026 Miroslav Šotek. All rights reserved.
4// © Code 2020–2026 Miroslav Šotek. All rights reserved.
5// ORCID: 0009-0009-3560-0851
6// Contact: www.anulum.li | protoscience@anulum.li
7// SC-NeuroCore — Pernarowski neuron PyO3 binding
8
9//! Python binding for the Pernarowski autonomous beta-cell burster.
10
11use numpy::{IntoPyArray, PyArray1};
12use pyo3::exceptions::PyFloatingPointError;
13use pyo3::prelude::*;
14use pyo3::types::PyDict;
15
16use crate::neurons::PernarowskiNeuron;
17
18type PyPernarowskiBatch<'py> = (Bound<'py, PyArray1<f64>>, i64, f64, f64, f64);
19
20py_neuron_default!("PernarowskiNeuron", PyPernarowskiNeuron, PernarowskiNeuron, state v, state w, state z);
21
22/// Register the Pernarowski class and simulator with the extension module.
23pub(super) fn register(module: &Bound<'_, PyModule>) -> PyResult<()> {
24    module.add_class::<PyPernarowskiNeuron>()?;
25    module.add_function(wrap_pyfunction!(py_pernarowski_simulate, module)?)?;
26    Ok(())
27}
28
29/// N-step Pernarowski (1994) pancreatic beta-cell burster simulation.
30///
31/// Parity contract with
32/// `sc_neurocore.neurons.models.pernarowski.PernarowskiNeuron.simulate`: for the
33/// same parameters and constant input the returned `v` trace, upward-crossing
34/// spike count, and final `(v, w, z)` state are bit-identical to the Python RK4
35/// reference (the cubic uses `v.powi(3)` = `v*v*v`, matching the Python `v*v*v`;
36/// no transcendental functions).
37#[pyfunction]
38#[pyo3(signature = (v0, w0, z0, alpha, beta, eps1, eps2, gamma, dt, v_threshold, n_steps, current))]
39#[allow(clippy::too_many_arguments)]
40fn py_pernarowski_simulate<'py>(
41    py: Python<'py>,
42    v0: f64,
43    w0: f64,
44    z0: f64,
45    alpha: f64,
46    beta: f64,
47    eps1: f64,
48    eps2: f64,
49    gamma: f64,
50    dt: f64,
51    v_threshold: f64,
52    n_steps: usize,
53    current: f64,
54) -> PyResult<PyPernarowskiBatch<'py>> {
55    let mut neuron = PernarowskiNeuron {
56        v: v0,
57        w: w0,
58        z: z0,
59        alpha,
60        beta,
61        eps1,
62        eps2,
63        gamma,
64        dt,
65        v_threshold,
66    };
67    let Some((trace, spikes)) = neuron.try_simulate(n_steps, current) else {
68        return Err(PyFloatingPointError::new_err(
69            "Pernarowski Rust batch rejected an invalid candidate",
70        ));
71    };
72    Ok((trace.into_pyarray(py), spikes, neuron.v, neuron.w, neuron.z))
73}