1#[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#[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#[cfg(feature = "python")]
59pub type NumpyCompatArrayD<T> = ArrayD<T>;
60
61#[cfg(feature = "python")]
63pub type NumpyCompatArray1<T> = Array1<T>;
64
65#[cfg(feature = "python")]
67pub type NumpyCompatArray2<T> = Array2<T>;
68
69#[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 Ok(array_ref.to_owned())
88}
89
90#[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#[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#[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#[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#[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#[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#[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#[cfg(feature = "python")]
182pub fn is_numpy_compatible<T: Element>(array: &Bound<'_, PyArrayDyn<T>>) -> bool {
183 array.shape().iter().all(|&dim| dim > 0)
185}
186
187#[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#[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 let scirs_array = array![[1.0f32, 2.0], [3.0, 4.0]];
220 let scirs_arrayd = scirs_array.into_dyn();
221
222 let numpy_array =
224 scirs_to_numpy_arrayd(scirs_arrayd.clone(), py).expect("Operation failed");
225
226 let result = numpy_to_scirs_arrayd(&numpy_array.bind(py)).expect("Operation failed");
228
229 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 let readonly = numpy_array.bind(py).readonly();
244 let view = numpy_readonly_to_scirs_view(&readonly);
245
246 assert_eq!(view.shape(), scirs_array.shape());
248 assert_eq!(view[[0, 0]], 1.0f32);
249 });
250 }
251}