Skip to main content

sc_neurocore_engine/bindings/
cortical_column.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 — Cortical column PyO3 binding
8
9//! Python binding for the layered cortical-column simulator.
10
11use numpy::{IntoPyArray, PyReadonlyArray1};
12use pyo3::prelude::*;
13use pyo3::types::PyDict;
14
15use crate::cortical_column;
16
17/// Register the layered cortical-column simulator with the extension module.
18pub(crate) fn register(module: &Bound<'_, PyModule>) -> PyResult<()> {
19    module.add_class::<PyCorticalColumn>()?;
20    Ok(())
21}
22
23#[pyclass(
24    name = "CorticalColumnRust",
25    module = "sc_neurocore_engine.sc_neurocore_engine"
26)]
27pub struct PyCorticalColumn {
28    inner: cortical_column::CorticalColumnRust,
29}
30
31#[pymethods]
32impl PyCorticalColumn {
33    #[new]
34    fn new(n: usize, tau: f64, dt: f64, threshold: f64, w_exc: f64, w_inh: f64, seed: u64) -> Self {
35        Self {
36            inner: cortical_column::CorticalColumnRust::new(
37                n, tau, dt, threshold, w_exc, w_inh, seed,
38            ),
39        }
40    }
41
42    fn step<'py>(
43        &mut self,
44        py: Python<'py>,
45        thalamic_input: PyReadonlyArray1<'py, f64>,
46    ) -> PyResult<Py<PyDict>> {
47        let input = thalamic_input.as_slice()?;
48        let spikes = self.inner.step(input);
49        let dict = PyDict::new(py);
50        let names = ["l4", "l23_exc", "l23_inh", "l5", "l6"];
51        for (i, name) in names.iter().enumerate() {
52            dict.set_item(*name, spikes[i].clone().into_pyarray(py))?;
53        }
54        Ok(dict.into())
55    }
56
57    fn reset(&mut self) {
58        self.inner.reset();
59    }
60}