Expand description
NumPy compatibility layer for scirs2-core
This module ensures that scirs2_core::ndarray types are fully compatible with PyO3’s numpy crate, enabling seamless Python integration.
§Problem Statement
The PyO3 numpy crate (v0.27) expects types from ndarray v0.16. When scirs2-core
uses ndarray v0.17 by default, there’s a type incompatibility. Python integration
modules must explicitly use ndarray16 types to ensure compatibility with numpy.
§Solution
This module provides:
- Explicit re-exports of ndarray types that numpy needs
- Type aliases that guarantee compatibility
- Conversion utilities for zero-copy operations
§Usage in Python Bindings
ⓘ
use pyo3::prelude::*;
use scirs2_core::python::numpy_compat::*;
#[pyfunction]
fn process_array(array: PyReadonlyArrayDyn<f32>) -> PyResult<Py<PyArrayDyn<f32>>> {
// Convert from NumPy to scirs2 array
let scirs_array = numpy_to_scirs_arrayd(array)?;
// Process with scirs2-core
let result = scirs_array.map(|x| x * 2.0);
// Convert back to NumPy
scirs_to_numpy_arrayd(result, array.py())
}Macros§
- array
- Create an
Arraywith one, two, three, four, five, or six dimensions. - s
- Slice argument constructor.
Structs§
- Array
Base - An n-dimensional array.
- Axis
- An axis index.
- Dim
- Dimension description.
- IxDyn
Impl - Dynamic dimension or index type.
- Owned
Repr - Array’s representation.
- View
Repr - Array view’s representation.
- Zip
- Lock step function application across several arrays or other producers.
Enums§
- Memory
Layout - Get the memory layout of a NumPy array
Traits§
- Data
- Array representation trait.
- DataMut
- Array representation trait.
- Data
Owned - Array representation trait.
- Dimension
- Array shape and index trait.
- RawData
- Array representation trait.
Functions§
- Dim
- Create a new dimension value.
- Ix0
- Create a zero-dimensional index
- Ix1
- Create a one-dimensional index
- Ix2
- Create a two-dimensional index
- Ix3
- Create a three-dimensional index
- Ix4
- Create a four-dimensional index
- IxDyn
- Create a dynamic-dimensional index
- arr1
- Create a one-dimensional array with elements from
xs. - arr2
- Create a two-dimensional array with elements from
xs. - get_
numpy_ layout - is_
numpy_ compatible - Check if a NumPy array is compatible with scirs2 operations
- numpy_
batch_ to_ scirs - Convert a vector of NumPy arrays to scirs2 ArrayD arrays
- numpy_
readonly_ to_ scirs_ arrayd - Convert NumPy readonly array to scirs2 ArrayD
- numpy_
readonly_ to_ scirs_ view - Convert NumPy readonly array to scirs2 ArrayView (zero-copy)
- numpy_
to_ scirs_ arrayd - Convert NumPy array to scirs2 ArrayD (zero-copy when possible)
- scirs_
batch_ to_ numpy - Convert a vector of scirs2 arrays to NumPy arrays
- scirs_
to_ numpy_ array1 - Convert scirs2 Array1 to NumPy array
- scirs_
to_ numpy_ array2 - Convert scirs2 Array2 to NumPy array
- scirs_
to_ numpy_ arrayd - Convert scirs2 ArrayD to NumPy array
Type Aliases§
- Array
- An array that owns its data uniquely.
- Array0
- zero-dimensional array
- Array1
- one-dimensional array
- Array2
- two-dimensional array
- Array3
- three-dimensional array
- Array4
- four-dimensional array
- ArrayD
- dynamic-dimensional array
- Array
View - A read-only array view.
- Array
View1 - one-dimensional array view
- Array
View2 - two-dimensional array view
- Array
ViewD - dynamic-dimensional array view
- Array
View Mut - A read-write array view.
- Array
View Mut1 - one-dimensional read-write array view
- Array
View Mut2 - two-dimensional read-write array view
- Array
View MutD - dynamic-dimensional read-write array view
- Ix0
- zero-dimensionial
- Ix1
- one-dimensional
- Ix2
- two-dimensional
- Ix3
- three-dimensional
- Ix4
- four-dimensional
- IxDyn
- dynamic-dimensional
- Numpy
Compat Array1 - Type alias for numpy-compatible 1D arrays
- Numpy
Compat Array2 - Type alias for numpy-compatible 2D arrays
- Numpy
Compat ArrayD - Type alias for numpy-compatible dynamic arrays