Skip to main content

sc_neurocore_engine/bindings/
ei_network.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 — Excitatory/inhibitory network PyO3 binding
8
9//! Python binding for the seeded excitatory/inhibitory network simulator.
10
11use numpy::IntoPyArray;
12use pyo3::prelude::*;
13use pyo3::types::PyDict;
14
15use crate::ei_network;
16
17/// Register the excitatory/inhibitory network simulator with the extension module.
18pub(crate) fn register(module: &Bound<'_, PyModule>) -> PyResult<()> {
19    module.add_function(wrap_pyfunction!(py_simulate_ei_network, module)?)?;
20    Ok(())
21}
22
23#[pyfunction]
24#[pyo3(signature = (
25    n_exc=80, n_inh=20,
26    w_ee=0.1, w_ei=0.4, w_ie=0.1, w_ii=0.4,
27    p_conn=0.2, ext_rate=5.0,
28    duration=200.0, dt=0.1, seed=42
29))]
30fn py_simulate_ei_network<'py>(
31    py: Python<'py>,
32    n_exc: usize,
33    n_inh: usize,
34    w_ee: f64,
35    w_ei: f64,
36    w_ie: f64,
37    w_ii: f64,
38    p_conn: f64,
39    ext_rate: f64,
40    duration: f64,
41    dt: f64,
42    seed: u64,
43) -> PyResult<Py<PyAny>> {
44    let r = ei_network::simulate_ei(
45        n_exc, n_inh, w_ee, w_ei, w_ie, w_ii, p_conn, ext_rate, duration, dt, seed,
46    );
47    let n_spikes = r.spike_times.len();
48    let d = PyDict::new(py);
49    d.set_item("spike_times", r.spike_times.into_pyarray(py))?;
50    d.set_item(
51        "spike_neurons",
52        r.spike_neurons
53            .iter()
54            .map(|&x| x as i64)
55            .collect::<Vec<_>>()
56            .into_pyarray(py),
57    )?;
58    d.set_item("n_exc", r.n_exc)?;
59    d.set_item("n_inh", r.n_inh)?;
60    d.set_item("n_total", r.n_exc + r.n_inh)?;
61    d.set_item("n_spikes", n_spikes)?;
62    d.set_item("rate_time", r.rate_time.into_pyarray(py))?;
63    d.set_item("exc_rates", r.exc_rates.into_pyarray(py))?;
64    d.set_item("inh_rates", r.inh_rates.into_pyarray(py))?;
65    d.set_item("duration", duration)?;
66    d.set_item("dt", dt)?;
67    d.set_item("mean_exc_rate", r.mean_exc_rate)?;
68    d.set_item("mean_inh_rate", r.mean_inh_rate)?;
69    Ok(d.into_any().unbind())
70}