Skip to main content

sc_neurocore_engine/bindings/channels/
nmda_neuron.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 — Wang 1999 NMDA-autapse neuron PyO3 binding
8
9use pyo3::exceptions::PyValueError;
10use pyo3::prelude::*;
11use pyo3::types::PyDict;
12
13use crate::neurons;
14
15#[pyclass(
16    name = "NMDANeuron",
17    module = "sc_neurocore_engine.sc_neurocore_engine"
18)]
19#[derive(Clone)]
20pub struct PyNMDANeuron {
21    inner: neurons::NMDANeuron,
22}
23
24#[pymethods]
25impl PyNMDANeuron {
26    #[new]
27    fn new() -> Self {
28        Self {
29            inner: neurons::NMDANeuron::default(),
30        }
31    }
32
33    /// Advance one step; raises `ValueError` with the state unchanged for
34    /// a non-finite drive, an invalid configuration, or a non-finite
35    /// candidate.
36    fn step(&mut self, current: f64) -> PyResult<i32> {
37        self.inner.try_step(current).map_err(PyValueError::new_err)
38    }
39
40    /// Restore dynamic state to the initial values, preserving parameters.
41    fn reset(&mut self) {
42        self.inner.reset();
43    }
44
45    /// Return the complete dynamic state as a Python dictionary.
46    fn get_state(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
47        let state = PyDict::new(py);
48        state.set_item("v", self.inner.v)?;
49        state.set_item("x_nmda", self.inner.x_nmda)?;
50        state.set_item("s_nmda", self.inner.s_nmda)?;
51        state.set_item("ca", self.inner.ca)?;
52        state.set_item("refractory_remaining", self.inner.refractory_remaining)?;
53        Ok(state.into_any().unbind())
54    }
55}
56
57/// Register the source NMDA-autapse neuron class.
58pub(super) fn register(module: &Bound<'_, PyModule>) -> PyResult<()> {
59    module.add_class::<PyNMDANeuron>()?;
60    Ok(())
61}