scirs2-numpy 0.4.2

PyO3-based Rust bindings of the NumPy C-API (SciRS2 fork with ndarray 0.17 support)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//! NumPy array protocol (`__array__` and `__array_interface__`) support.
//!
//! The NumPy array protocol enables Python objects to be converted to NumPy
//! arrays via two mechanisms:
//!
//! - `__array__(dtype=None)` — a method that returns a `numpy.ndarray`.
//! - `__array_interface__` — a property returning a Python dict describing
//!   the underlying buffer (shape, dtype string, raw pointer, etc.).
//!
//! The array interface dictionary format follows:
//! <https://numpy.org/doc/stable/reference/arrays.interface.html>

use pyo3::prelude::*;
use pyo3::types::{PyDict, PyList, PyTuple};
use thiserror::Error;

// ─── Error types ────────────────────────────────────────────────────────────

/// Errors produced by the array-protocol layer.
#[derive(Debug, Error)]
pub enum ArrayProtocolError {
    /// The requested element dtype is not supported.
    #[error("unsupported dtype: {0}")]
    UnsupportedDtype(String),

    /// The numpy typestr string could not be parsed.
    #[error("invalid typestr: {0}")]
    InvalidTypestr(String),

    /// A Python API call failed.
    #[error("python error: {0}")]
    PythonError(String),
}

impl From<PyErr> for ArrayProtocolError {
    fn from(e: PyErr) -> Self {
        Self::PythonError(e.to_string())
    }
}

impl From<ArrayProtocolError> for PyErr {
    fn from(e: ArrayProtocolError) -> Self {
        pyo3::exceptions::PyValueError::new_err(e.to_string())
    }
}

// ─── parse_typestr ───────────────────────────────────────────────────────────

/// Parse a NumPy type-string into `(kind_char, byte_count)`.
///
/// NumPy typestrings have the format `<endian><kind><bytes>`, where:
/// - endianness: `'<'` (little), `'>'` (big), `'='` (native), `'|'` (n/a)
/// - kind: `'f'` float, `'i'` signed int, `'u'` unsigned int, `'b'` bool,
///   `'c'` complex, etc.
/// - bytes: decimal byte count, e.g. `8` for 64-bit.
///
/// Returns `(kind, byte_count)`.
///
/// # Examples
///
/// ```
/// use scirs2_numpy::array_protocol::parse_typestr;
/// let (kind, bytes) = parse_typestr("<f8").unwrap();
/// assert_eq!(kind, 'f');
/// assert_eq!(bytes, 8);
/// ```
pub fn parse_typestr(typestr: &str) -> Result<(char, usize), ArrayProtocolError> {
    if typestr.len() < 3 {
        return Err(ArrayProtocolError::InvalidTypestr(format!(
            "too short: {typestr:?}"
        )));
    }
    let mut chars = typestr.chars();
    let endian = chars
        .next()
        .ok_or_else(|| ArrayProtocolError::InvalidTypestr(format!("empty typestr: {typestr:?}")))?;
    // Validate endianness character.
    if !matches!(endian, '<' | '>' | '=' | '|') {
        return Err(ArrayProtocolError::InvalidTypestr(format!(
            "unknown endianness character {endian:?} in {typestr:?}"
        )));
    }
    let kind = chars.next().ok_or_else(|| {
        ArrayProtocolError::InvalidTypestr(format!("missing kind in {typestr:?}"))
    })?;
    if !kind.is_ascii_alphabetic() {
        return Err(ArrayProtocolError::InvalidTypestr(format!(
            "invalid kind character {kind:?} in {typestr:?}"
        )));
    }
    let size_str: String = chars.collect();
    let byte_count = size_str.parse::<usize>().map_err(|_| {
        ArrayProtocolError::InvalidTypestr(format!(
            "invalid byte count {size_str:?} in {typestr:?}"
        ))
    })?;
    if byte_count == 0 {
        return Err(ArrayProtocolError::InvalidTypestr(format!(
            "byte count must be > 0 in {typestr:?}"
        )));
    }
    Ok((kind, byte_count))
}

// ─── ArrayProtocol trait ────────────────────────────────────────────────────

/// Mixin trait for types that support the NumPy array interface protocol.
///
/// Implementors must provide shape, stride, type-string, and a raw data
/// pointer, from which a complete [`ArrayInterfaceDict`] can be assembled.
pub trait ArrayProtocol {
    /// Returns the populated [`ArrayInterfaceDict`] for this object.
    fn array_interface(&self) -> ArrayInterfaceDict;

    /// Returns the NumPy dtype type-string (e.g. `"<f8"` for little-endian f64).
    fn dtype_str(&self) -> &'static str;

    /// Returns the logical shape of the array.
    fn shape(&self) -> Vec<usize>;

    /// Returns the strides of the array **in bytes**.
    fn strides(&self) -> Vec<usize>;

    /// Returns a raw pointer to the first byte of element data.
    fn data_ptr(&self) -> *const u8;

    /// Returns the total number of bytes occupied by the element buffer.
    fn nbytes(&self) -> usize;
}

// ─── ArrayInterfaceDict ──────────────────────────────────────────────────────

/// Data for the `__array_interface__` protocol dictionary.
///
/// See: <https://numpy.org/doc/stable/reference/arrays.interface.html>
pub struct ArrayInterfaceDict {
    /// Logical shape of the array.
    pub shape: Vec<usize>,
    /// NumPy dtype typestr (e.g. `"<f8"`).
    pub typestr: String,
    /// Raw pointer to element data, encoded as a Python integer.
    pub data_ptr: usize,
    /// Whether the buffer should be treated as read-only.
    pub readonly: bool,
    /// Optional per-dimension strides in bytes.
    pub strides: Option<Vec<usize>>,
    /// Protocol version; always 3.
    pub version: u8,
}

impl ArrayInterfaceDict {
    /// Serialize this descriptor into a Python dict suitable for `__array_interface__`.
    ///
    /// The resulting dict has the keys `shape`, `typestr`, `data`, `version`,
    /// and optionally `strides`.
    pub fn to_py_dict<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
        let dict = PyDict::new(py);

        // shape — tuple of usize
        let shape_tuple = PyTuple::new(py, self.shape.iter().copied())?;
        dict.set_item("shape", shape_tuple)?;

        // typestr — str
        dict.set_item("typestr", &self.typestr)?;

        // data — (ptr_as_int, readonly_bool)
        let data_tuple = PyTuple::new(py, [self.data_ptr, self.readonly as usize])?;
        dict.set_item("data", data_tuple)?;

        // version — always 3
        dict.set_item("version", self.version)?;

        // strides — optional tuple of usize
        if let Some(ref strides) = self.strides {
            let strides_tuple = PyTuple::new(py, strides.iter().copied())?;
            dict.set_item("strides", strides_tuple)?;
        }

        Ok(dict)
    }
}

// ─── NdArrayWrapper ──────────────────────────────────────────────────────────

/// A concrete array type implementing the NumPy `__array__` and
/// `__array_interface__` protocols.
///
/// Wraps an owned flat `Vec<f64>` buffer with a logical shape, and exposes
/// it to NumPy via the array interface protocol.
#[pyclass(name = "NdArrayWrapper")]
pub struct NdArrayWrapper {
    /// Flat element buffer in C (row-major) order.
    data: Vec<f64>,
    /// Logical shape.
    shape: Vec<usize>,
    /// Per-dimension strides **in bytes** (C-contiguous by default).
    strides: Vec<usize>,
    /// NumPy dtype typestr.
    dtype: String,
}

#[pymethods]
impl NdArrayWrapper {
    /// Construct a new `NdArrayWrapper` with C-contiguous strides.
    ///
    /// # Arguments
    /// * `data`  – flat element buffer; must have `shape.iter().product::<usize>()` elements.
    /// * `shape` – logical dimensions.
    #[new]
    pub fn new(data: Vec<f64>, shape: Vec<usize>) -> PyResult<Self> {
        let n: usize = shape.iter().product();
        if data.len() != n {
            return Err(pyo3::exceptions::PyValueError::new_err(format!(
                "data length {} does not match shape product {}",
                data.len(),
                n
            )));
        }
        let strides = compute_c_strides_bytes(&shape, std::mem::size_of::<f64>());
        Ok(Self {
            data,
            shape,
            strides,
            dtype: "<f8".to_owned(),
        })
    }

    /// Return a Python representation suitable for numpy consumption.
    ///
    /// Calls `numpy.array(list_of_floats).reshape(shape)` so that consumers
    /// that call `np.asarray(obj)` or `np.array(obj.__array__())` obtain the
    /// correct array.
    ///
    /// Note: requires NumPy to be installed in the active Python environment.
    #[pyo3(name = "__array__")]
    pub fn array_method(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
        let np = py.import("numpy").map_err(|e| {
            pyo3::exceptions::PyImportError::new_err(format!("numpy not available: {e}"))
        })?;
        // Build a flat Python list from the data buffer.
        let flat_list = PyList::new(py, &self.data)?;
        // numpy.array(flat_list, dtype='f8')
        let kwargs = PyDict::new(py);
        kwargs.set_item("dtype", "f8")?;
        let arr = np.call_method("array", (flat_list,), Some(&kwargs))?;
        // Reshape to logical shape.
        let shape_tuple = PyTuple::new(py, self.shape.iter().copied())?;
        let reshaped = arr.call_method1("reshape", (shape_tuple,))?;
        Ok(reshaped.unbind())
    }

    /// The `__array_interface__` property, returning a dict describing the buffer.
    #[getter]
    pub fn array_interface(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
        let desc = ArrayInterfaceDict {
            shape: self.shape.clone(),
            typestr: self.dtype.clone(),
            data_ptr: self.data.as_ptr() as usize,
            readonly: true,
            strides: Some(self.strides.clone()),
            version: 3,
        };
        let dict = desc.to_py_dict(py)?;
        Ok(dict.into_any().unbind())
    }

    /// Return the shape as a Python tuple.
    pub fn shape_tuple(&self, py: Python<'_>) -> Py<PyAny> {
        PyTuple::new(py, self.shape.iter().copied())
            .map(|t| t.into_any().unbind())
            .unwrap_or_else(|_| py.None())
    }

    /// Return the dtype typestr (e.g. `"<f8"`).
    pub fn dtype_str(&self) -> &str {
        &self.dtype
    }

    /// Return a flat copy of the data buffer.
    pub fn data(&self) -> Vec<f64> {
        self.data.clone()
    }

    /// Return the number of dimensions.
    pub fn ndim(&self) -> usize {
        self.shape.len()
    }
}

impl ArrayProtocol for NdArrayWrapper {
    fn array_interface(&self) -> ArrayInterfaceDict {
        ArrayInterfaceDict {
            shape: self.shape.clone(),
            typestr: self.dtype.clone(),
            data_ptr: self.data.as_ptr() as usize,
            readonly: true,
            strides: Some(self.strides.clone()),
            version: 3,
        }
    }

    fn dtype_str(&self) -> &'static str {
        "<f8"
    }

    fn shape(&self) -> Vec<usize> {
        self.shape.clone()
    }

    fn strides(&self) -> Vec<usize> {
        self.strides.clone()
    }

    fn data_ptr(&self) -> *const u8 {
        self.data.as_ptr() as *const u8
    }

    fn nbytes(&self) -> usize {
        self.data.len() * std::mem::size_of::<f64>()
    }
}

// ─── Register ───────────────────────────────────────────────────────────────

/// Register array-protocol classes into a PyO3 module.
pub fn register_array_protocol_module(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
    m.add_class::<NdArrayWrapper>()?;
    Ok(())
}

// ─── Helpers ─────────────────────────────────────────────────────────────────

/// Compute C-contiguous (row-major) strides in bytes for a given shape.
///
/// The last dimension has stride `elem_size`; each preceding dimension has stride
/// equal to the product of all following dimensions multiplied by `elem_size`.
fn compute_c_strides_bytes(shape: &[usize], elem_size: usize) -> Vec<usize> {
    let n = shape.len();
    if n == 0 {
        return Vec::new();
    }
    let mut strides = vec![elem_size; n];
    for i in (0..n - 1).rev() {
        strides[i] = strides[i + 1] * shape[i + 1];
    }
    strides
}

// ─── Tests ───────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    // --- parse_typestr ---

    #[test]
    fn test_parse_typestr_f64_le() {
        let (kind, bytes) = parse_typestr("<f8").expect("parse_typestr failed");
        assert_eq!(kind, 'f');
        assert_eq!(bytes, 8);
    }

    #[test]
    fn test_parse_typestr_i32_be() {
        let (kind, bytes) = parse_typestr(">i4").expect("parse_typestr failed");
        assert_eq!(kind, 'i');
        assert_eq!(bytes, 4);
    }

    #[test]
    fn test_parse_typestr_u16_native() {
        let (kind, bytes) = parse_typestr("=u2").expect("parse_typestr failed");
        assert_eq!(kind, 'u');
        assert_eq!(bytes, 2);
    }

    #[test]
    fn test_parse_typestr_bool_noendian() {
        let (kind, bytes) = parse_typestr("|b1").expect("parse_typestr failed");
        assert_eq!(kind, 'b');
        assert_eq!(bytes, 1);
    }

    #[test]
    fn test_parse_typestr_error_too_short() {
        assert!(parse_typestr("<f").is_err());
        assert!(parse_typestr("").is_err());
        assert!(parse_typestr("<").is_err());
    }

    #[test]
    fn test_parse_typestr_error_bad_endian() {
        assert!(parse_typestr("?f8").is_err());
    }

    #[test]
    fn test_parse_typestr_error_zero_bytes() {
        assert!(parse_typestr("<f0").is_err());
    }

    // --- ArrayInterfaceDict ---

    #[test]
    fn test_array_interface_dict_version() {
        let data = vec![1.0_f64, 2.0, 3.0, 4.0];
        let wrapper = NdArrayWrapper::new(data, vec![2, 2]).expect("NdArrayWrapper::new failed");
        let iface = ArrayProtocol::array_interface(&wrapper);
        assert_eq!(iface.version, 3, "version must be 3");
    }

    #[test]
    fn test_array_interface_dict_shape() {
        let data = vec![1.0_f64; 6];
        let wrapper = NdArrayWrapper::new(data, vec![2, 3]).expect("NdArrayWrapper::new failed");
        let iface = ArrayProtocol::array_interface(&wrapper);
        assert_eq!(iface.shape, vec![2, 3]);
    }

    #[test]
    fn test_array_interface_dict_typestr() {
        let data = vec![0.0_f64; 4];
        let wrapper = NdArrayWrapper::new(data, vec![4]).expect("NdArrayWrapper::new failed");
        let iface = ArrayProtocol::array_interface(&wrapper);
        assert_eq!(iface.typestr, "<f8");
    }

    #[test]
    fn test_array_interface_dict_data_ptr_nonzero() {
        let data = vec![1.0_f64, 2.0, 3.0];
        let wrapper = NdArrayWrapper::new(data, vec![3]).expect("NdArrayWrapper::new failed");
        let iface = ArrayProtocol::array_interface(&wrapper);
        assert_ne!(iface.data_ptr, 0, "data pointer must be non-null");
    }

    // --- NdArrayWrapper construction ---

    #[test]
    fn test_ndarray_wrapper_shape_mismatch() {
        // data has 4 elements but shape says 6
        let result = NdArrayWrapper::new(vec![1.0; 4], vec![2, 3]);
        assert!(result.is_err());
    }

    #[test]
    fn test_ndarray_wrapper_scalar() {
        // 0-d equivalent: shape = [1]
        let wrapper = NdArrayWrapper::new(vec![42.0], vec![1]).expect("scalar failed");
        assert_eq!(wrapper.ndim(), 1);
        assert_eq!(wrapper.data(), vec![42.0]);
    }

    #[test]
    fn test_ndarray_wrapper_strides_c_order() {
        // shape [3, 4] → strides [32, 8] (in bytes, f64=8)
        let data = vec![0.0_f64; 12];
        let wrapper = NdArrayWrapper::new(data, vec![3, 4]).expect("NdArrayWrapper::new failed");
        let strides = ArrayProtocol::strides(&wrapper);
        assert_eq!(strides, vec![32, 8]);
    }

    // --- Python-GIL tests (require auto-initialize feature) ---

    #[test]
    fn test_array_interface_py_dict_keys() {
        Python::attach(|py| {
            let data = vec![1.0_f64, 2.0, 3.0, 4.0];
            let wrapper =
                NdArrayWrapper::new(data, vec![2, 2]).expect("NdArrayWrapper::new failed");
            let iface = ArrayProtocol::array_interface(&wrapper);
            let dict = iface.to_py_dict(py).expect("to_py_dict failed");

            assert!(dict
                .get_item("shape")
                .expect("shape lookup failed")
                .is_some());
            assert!(dict
                .get_item("typestr")
                .expect("typestr lookup failed")
                .is_some());
            assert!(dict.get_item("data").expect("data lookup failed").is_some());
            assert!(dict
                .get_item("version")
                .expect("version lookup failed")
                .is_some());
        });
    }

    #[test]
    fn test_array_interface_py_dict_shape_values() {
        Python::attach(|py| {
            let data = vec![0.0_f64; 6];
            let wrapper =
                NdArrayWrapper::new(data, vec![2, 3]).expect("NdArrayWrapper::new failed");
            let iface = ArrayProtocol::array_interface(&wrapper);
            let dict = iface.to_py_dict(py).expect("to_py_dict failed");

            let shape_obj = dict
                .get_item("shape")
                .expect("shape lookup failed")
                .expect("shape missing");
            let shape_tuple = shape_obj.cast::<PyTuple>().expect("shape is not a tuple");
            assert_eq!(shape_tuple.len(), 2);
            let v0: usize = shape_tuple
                .get_item(0)
                .expect("item 0")
                .extract()
                .expect("extract[0]");
            let v1: usize = shape_tuple
                .get_item(1)
                .expect("item 1")
                .extract()
                .expect("extract[1]");
            assert_eq!(v0, 2);
            assert_eq!(v1, 3);
        });
    }
}