sc_neurocore_engine/bindings/
phi.rs1use numpy::{PyReadonlyArray2, PyUntypedArrayMethods};
12use pyo3::exceptions::PyValueError;
13use pyo3::prelude::*;
14
15pub(crate) fn register(module: &Bound<'_, PyModule>) -> PyResult<()> {
17 module.add_function(wrap_pyfunction!(py_phi_star, module)?)?;
18 Ok(())
19}
20
21#[pyfunction]
23fn py_phi_star(data: PyReadonlyArray2<'_, f64>, tau: usize) -> PyResult<f64> {
24 if !data.is_c_contiguous() {
25 return Err(PyValueError::new_err(
26 "py_phi_star requires C-contiguous array input",
27 ));
28 }
29 let shape = data.shape();
30 let n_channels = shape[0];
31 let n_timesteps = shape[1];
32 let flat = data.as_slice().map_err(|error| {
33 PyValueError::new_err(format!("py_phi_star requires C-contiguous array: {error}"))
34 })?;
35 let channels: Vec<Vec<f64>> = (0..n_channels)
36 .map(|index| flat[index * n_timesteps..(index + 1) * n_timesteps].to_vec())
37 .collect();
38 Ok(crate::phi::phi_star(&channels, tau))
39}