scirs2_core/python/
numpy_compat.rs

1//! NumPy compatibility layer for scirs2-core
2//!
3//! This module ensures that scirs2_core::ndarray types are fully compatible with
4//! PyO3's numpy crate, enabling seamless Python integration.
5//!
6//! # Problem Statement
7//!
8//! The PyO3 `numpy` crate (v0.27) expects types from `ndarray` v0.16. When scirs2-core
9//! uses `ndarray` v0.17 by default, there's a type incompatibility. Python integration
10//! modules must explicitly use `ndarray16` types to ensure compatibility with numpy.
11//!
12//! # Solution
13//!
14//! This module provides:
15//! 1. Explicit re-exports of ndarray types that numpy needs
16//! 2. Type aliases that guarantee compatibility
17//! 3. Conversion utilities for zero-copy operations
18//!
19//! # Usage in Python Bindings
20//!
21//! ```ignore
22//! use pyo3::prelude::*;
23//! use scirs2_core::python::numpy_compat::*;
24//!
25//! #[pyfunction]
26//! fn process_array(array: PyReadonlyArrayDyn<f32>) -> PyResult<Py<PyArrayDyn<f32>>> {
27//!     // Convert from NumPy to scirs2 array
28//!     let scirs_array = numpy_to_scirs_arrayd(array)?;
29//!
30//!     // Process with scirs2-core
31//!     let result = scirs_array.map(|x| x * 2.0);
32//!
33//!     // Convert back to NumPy
34//!     scirs_to_numpy_arrayd(result, array.py())
35//! }
36//! ```
37
38#[cfg(feature = "python")]
39use pyo3::prelude::*;
40
41#[cfg(feature = "python")]
42use scirs2_numpy::{
43    Element, PyArray, PyArray1, PyArray2, PyArrayDyn, PyArrayMethods, PyReadonlyArray,
44    PyReadonlyArrayDyn, PyUntypedArrayMethods,
45};
46
47// Re-export ndarray types for Python compatibility
48// IMPORTANT: Python integration uses scirs2-numpy with ndarray 0.17 support
49#[cfg(feature = "python")]
50pub use ::ndarray::{
51    arr1, arr2, array, s, Array, Array0, Array1, Array2, Array3, Array4, ArrayBase, ArrayD,
52    ArrayView, ArrayView1, ArrayView2, ArrayViewD, ArrayViewMut, ArrayViewMut1, ArrayViewMut2,
53    ArrayViewMutD, Axis, Data, DataMut, DataOwned, Dim, Dimension, Ix0, Ix1, Ix2, Ix3, Ix4, IxDyn,
54    IxDynImpl, OwnedRepr, RawData, ViewRepr, Zip,
55};
56
57/// Type alias for numpy-compatible dynamic arrays
58#[cfg(feature = "python")]
59pub type NumpyCompatArrayD<T> = ArrayD<T>;
60
61/// Type alias for numpy-compatible 1D arrays
62#[cfg(feature = "python")]
63pub type NumpyCompatArray1<T> = Array1<T>;
64
65/// Type alias for numpy-compatible 2D arrays
66#[cfg(feature = "python")]
67pub type NumpyCompatArray2<T> = Array2<T>;
68
69// ========================================
70// CONVERSION FUNCTIONS
71// ========================================
72
73/// Convert NumPy array to scirs2 ArrayD (zero-copy when possible)
74///
75/// This function attempts zero-copy conversion when the NumPy array is C-contiguous.
76/// Otherwise, it creates a owned copy.
77#[cfg(feature = "python")]
78pub fn numpy_to_scirs_arrayd<'py, T>(array: &Bound<'py, PyArrayDyn<T>>) -> PyResult<ArrayD<T>>
79where
80    T: Element + Clone,
81{
82    let readonly = array.readonly();
83    let array_ref = readonly.as_array();
84
85    // Always create owned array for safety
86    // TODO: Investigate true zero-copy with lifetimes
87    Ok(array_ref.to_owned())
88}
89
90/// Convert NumPy readonly array to scirs2 ArrayD
91#[cfg(feature = "python")]
92pub fn numpy_readonly_to_scirs_arrayd<T>(array: PyReadonlyArrayDyn<T>) -> PyResult<ArrayD<T>>
93where
94    T: Element + Clone,
95{
96    Ok(array.as_array().to_owned())
97}
98
99/// Convert scirs2 ArrayD to NumPy array
100#[cfg(feature = "python")]
101pub fn scirs_to_numpy_arrayd<T: Element>(
102    array: ArrayD<T>,
103    py: Python<'_>,
104) -> PyResult<Py<PyArrayDyn<T>>> {
105    Ok(PyArrayDyn::from_owned_array(py, array).unbind())
106}
107
108/// Convert scirs2 Array1 to NumPy array
109#[cfg(feature = "python")]
110pub fn scirs_to_numpy_array1<T: Element>(
111    array: Array1<T>,
112    py: Python<'_>,
113) -> PyResult<Py<PyArray1<T>>> {
114    Ok(PyArray1::from_owned_array(py, array).unbind())
115}
116
117/// Convert scirs2 Array2 to NumPy array
118#[cfg(feature = "python")]
119pub fn scirs_to_numpy_array2<T: Element>(
120    array: Array2<T>,
121    py: Python<'_>,
122) -> PyResult<Py<PyArray2<T>>> {
123    Ok(PyArray2::from_owned_array(py, array).unbind())
124}
125
126// ========================================
127// BATCH CONVERSION UTILITIES
128// ========================================
129
130/// Convert a vector of NumPy arrays to scirs2 ArrayD arrays
131#[cfg(feature = "python")]
132pub fn numpy_batch_to_scirs<T: Element + Clone>(
133    arrays: Vec<PyReadonlyArrayDyn<T>>,
134) -> PyResult<Vec<ArrayD<T>>> {
135    arrays
136        .into_iter()
137        .map(|arr| Ok(arr.as_array().to_owned()))
138        .collect()
139}
140
141/// Convert a vector of scirs2 arrays to NumPy arrays
142#[cfg(feature = "python")]
143pub fn scirs_batch_to_numpy<T: Element>(
144    arrays: Vec<ArrayD<T>>,
145    py: Python<'_>,
146) -> PyResult<Vec<Py<PyArrayDyn<T>>>> {
147    arrays
148        .into_iter()
149        .map(|arr| Ok(PyArrayDyn::from_owned_array(py, arr).unbind()))
150        .collect()
151}
152
153// ========================================
154// VIEW CONVERSION (ZERO-COPY)
155// ========================================
156
157/// Convert NumPy readonly array to scirs2 ArrayView (zero-copy)
158///
159/// This provides true zero-copy access to NumPy arrays. The readonly guard
160/// must be kept alive for the lifetime of the view.
161///
162/// # Example
163///
164/// ```ignore
165/// let readonly = numpy_array.readonly();
166/// let view = numpy_readonly_to_scirs_view(&readonly);
167/// // Use view while readonly is in scope
168/// ```
169#[cfg(feature = "python")]
170pub fn numpy_readonly_to_scirs_view<'a, T: Element>(
171    array: &'a PyReadonlyArrayDyn<'a, T>,
172) -> ArrayViewD<'a, T> {
173    array.as_array()
174}
175
176// ========================================
177// TYPE INFORMATION UTILITIES
178// ========================================
179
180/// Check if a NumPy array is compatible with scirs2 operations
181#[cfg(feature = "python")]
182pub fn is_numpy_compatible<T: Element>(array: &Bound<'_, PyArrayDyn<T>>) -> bool {
183    // Check if array is well-formed (has valid shape and strides)
184    array.shape().iter().all(|&dim| dim > 0)
185}
186
187/// Get the memory layout of a NumPy array
188#[cfg(feature = "python")]
189pub enum MemoryLayout {
190    CContiguous,
191    FContiguous,
192    Neither,
193}
194
195#[cfg(feature = "python")]
196pub fn get_numpy_layout<T: Element>(array: &Bound<'_, PyArrayDyn<T>>) -> MemoryLayout {
197    if array.is_c_contiguous() {
198        MemoryLayout::CContiguous
199    } else if array.is_fortran_contiguous() {
200        MemoryLayout::FContiguous
201    } else {
202        MemoryLayout::Neither
203    }
204}
205
206// ========================================
207// TESTS
208// ========================================
209
210#[cfg(all(test, feature = "python"))]
211mod tests {
212    use super::*;
213    use pyo3::Python;
214
215    #[test]
216    fn test_numpy_scirs_roundtrip() {
217        Python::with_gil(|py| {
218            // Create scirs2 array
219            let scirs_array = array![[1.0f32, 2.0], [3.0, 4.0]];
220            let scirs_arrayd = scirs_array.into_dyn();
221
222            // Convert to NumPy
223            let numpy_array =
224                scirs_to_numpy_arrayd(scirs_arrayd.clone(), py).expect("Operation failed");
225
226            // Convert back to scirs2
227            let result = numpy_to_scirs_arrayd(&numpy_array.bind(py)).expect("Operation failed");
228
229            // Verify equality
230            assert_eq!(result.shape(), scirs_arrayd.shape());
231            assert_eq!(result, scirs_arrayd);
232        });
233    }
234
235    #[test]
236    fn test_zero_copy_view() {
237        Python::with_gil(|py| {
238            let scirs_array = array![[1.0f32, 2.0], [3.0, 4.0]].into_dyn();
239            let numpy_array =
240                scirs_to_numpy_arrayd(scirs_array.clone(), py).expect("Operation failed");
241
242            // Get zero-copy view
243            let readonly = numpy_array.bind(py).readonly();
244            let view = numpy_readonly_to_scirs_view(&readonly);
245
246            // Verify it's the same data
247            assert_eq!(view.shape(), scirs_array.shape());
248            assert_eq!(view[[0, 0]], 1.0f32);
249        });
250    }
251}