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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
use std::mem::size_of;
use std::os::raw::{
c_char, c_int, c_long, c_longlong, c_short, c_uint, c_ulong, c_ulonglong, c_ushort,
};
use std::ptr;
#[cfg(feature = "half")]
use half::f16;
use num_traits::{Bounded, Zero};
use pyo3::{
exceptions::{PyIndexError, PyValueError},
ffi::{self, PyTuple_Size},
pyobject_native_type_extract, pyobject_native_type_named,
types::{PyDict, PyTuple, PyType},
AsPyPointer, FromPyObject, FromPyPointer, IntoPyPointer, PyAny, PyNativeType, PyObject,
PyResult, PyTypeInfo, Python, ToPyObject,
};
use crate::npyffi::{
NpyTypes, PyArray_Descr, NPY_ALIGNED_STRUCT, NPY_BYTEORDER_CHAR, NPY_ITEM_HASOBJECT, NPY_TYPES,
PY_ARRAY_API,
};
pub use num_complex::{Complex32, Complex64};
#[repr(transparent)]
pub struct PyArrayDescr(PyAny);
pyobject_native_type_named!(PyArrayDescr);
unsafe impl PyTypeInfo for PyArrayDescr {
type AsRefTarget = Self;
const NAME: &'static str = "PyArrayDescr";
const MODULE: Option<&'static str> = Some("numpy");
#[inline]
fn type_object_raw(py: Python) -> *mut ffi::PyTypeObject {
unsafe { PY_ARRAY_API.get_type_object(py, NpyTypes::PyArrayDescr_Type) }
}
fn is_type_of(ob: &PyAny) -> bool {
unsafe { ffi::PyObject_TypeCheck(ob.as_ptr(), Self::type_object_raw(ob.py())) > 0 }
}
}
pyobject_native_type_extract!(PyArrayDescr);
pub fn dtype<T: Element>(py: Python) -> &PyArrayDescr {
T::get_dtype(py)
}
impl PyArrayDescr {
#[inline]
pub fn new<'py, T: ToPyObject + ?Sized>(py: Python<'py>, ob: &T) -> PyResult<&'py Self> {
fn inner<'py>(py: Python<'py>, obj: PyObject) -> PyResult<&'py PyArrayDescr> {
let mut descr: *mut PyArray_Descr = ptr::null_mut();
unsafe {
PY_ARRAY_API.PyArray_DescrConverter2(py, obj.as_ptr(), &mut descr as *mut _);
py.from_owned_ptr_or_err(descr as _)
}
}
inner(py, ob.to_object(py))
}
pub fn as_dtype_ptr(&self) -> *mut PyArray_Descr {
self.as_ptr() as _
}
pub fn into_dtype_ptr(&self) -> *mut PyArray_Descr {
self.into_ptr() as _
}
pub fn object(py: Python) -> &Self {
Self::from_npy_type(py, NPY_TYPES::NPY_OBJECT)
}
pub fn of<T: Element>(py: Python) -> &Self {
T::get_dtype(py)
}
pub fn is_equiv_to(&self, other: &Self) -> bool {
let self_ptr = self.as_dtype_ptr();
let other_ptr = other.as_dtype_ptr();
unsafe {
self_ptr == other_ptr
|| PY_ARRAY_API.PyArray_EquivTypes(self.py(), self_ptr, other_ptr) != 0
}
}
fn from_npy_type(py: Python, npy_type: NPY_TYPES) -> &Self {
unsafe {
let descr = PY_ARRAY_API.PyArray_DescrFromType(py, npy_type as _);
py.from_owned_ptr(descr as _)
}
}
pub(crate) fn new_from_npy_type(py: Python, npy_type: NPY_TYPES) -> &Self {
unsafe {
let descr = PY_ARRAY_API.PyArray_DescrNewFromType(py, npy_type as _);
py.from_owned_ptr(descr as _)
}
}
pub fn typeobj(&self) -> &PyType {
let dtype_type_ptr = unsafe { *self.as_dtype_ptr() }.typeobj;
unsafe { PyType::from_type_ptr(self.py(), dtype_type_ptr) }
}
pub fn num(&self) -> c_int {
unsafe { *self.as_dtype_ptr() }.type_num
}
pub fn itemsize(&self) -> usize {
unsafe { *self.as_dtype_ptr() }.elsize.max(0) as _
}
pub fn alignment(&self) -> usize {
unsafe { *self.as_dtype_ptr() }.alignment.max(0) as _
}
pub fn byteorder(&self) -> u8 {
unsafe { *self.as_dtype_ptr() }.byteorder.max(0) as _
}
pub fn char(&self) -> u8 {
unsafe { *self.as_dtype_ptr() }.type_.max(0) as _
}
pub fn kind(&self) -> u8 {
unsafe { *self.as_dtype_ptr() }.kind.max(0) as _
}
pub fn flags(&self) -> c_char {
unsafe { *self.as_dtype_ptr() }.flags
}
pub fn ndim(&self) -> usize {
if !self.has_subarray() {
return 0;
}
unsafe { PyTuple_Size((*((*self.as_dtype_ptr()).subarray)).shape).max(0) as _ }
}
pub fn base(&self) -> &PyArrayDescr {
if !self.has_subarray() {
self
} else {
unsafe {
Self::from_borrowed_ptr(self.py(), (*(*self.as_dtype_ptr()).subarray).base as _)
}
}
}
pub fn shape(&self) -> Vec<usize> {
if !self.has_subarray() {
Vec::new()
} else {
unsafe {
PyTuple::from_borrowed_ptr(self.py(), (*(*self.as_dtype_ptr()).subarray).shape)
}
.extract()
.unwrap()
}
}
pub fn has_object(&self) -> bool {
self.flags() & NPY_ITEM_HASOBJECT != 0
}
pub fn is_aligned_struct(&self) -> bool {
self.flags() & NPY_ALIGNED_STRUCT != 0
}
pub fn has_subarray(&self) -> bool {
unsafe { !(*self.as_dtype_ptr()).subarray.is_null() }
}
pub fn has_fields(&self) -> bool {
unsafe { !(*self.as_dtype_ptr()).names.is_null() }
}
pub fn is_native_byteorder(&self) -> Option<bool> {
match self.byteorder() {
b'=' => Some(true),
b'|' => None,
byteorder => Some(byteorder == NPY_BYTEORDER_CHAR::NPY_NATBYTE as u8),
}
}
pub fn names(&self) -> Option<Vec<&str>> {
if !self.has_fields() {
return None;
}
let names = unsafe { PyTuple::from_borrowed_ptr(self.py(), (*self.as_dtype_ptr()).names) };
FromPyObject::extract(names).ok()
}
pub fn get_field(&self, name: &str) -> PyResult<(&PyArrayDescr, usize)> {
if !self.has_fields() {
return Err(PyValueError::new_err(
"cannot get field information: type descriptor has no fields",
));
}
let dict = unsafe { PyDict::from_borrowed_ptr(self.py(), (*self.as_dtype_ptr()).fields) };
let tuple = dict
.get_item(name)
.ok_or_else(|| PyIndexError::new_err(name.to_owned()))?
.downcast::<PyTuple>()
.unwrap();
let dtype = FromPyObject::extract(tuple.as_ref().get_item(0).unwrap()).unwrap();
let offset = FromPyObject::extract(tuple.as_ref().get_item(1).unwrap()).unwrap();
Ok((dtype, offset))
}
}
pub unsafe trait Element: Clone + Send {
const IS_COPY: bool;
fn get_dtype(py: Python) -> &PyArrayDescr;
}
fn npy_int_type_lookup<T, T0, T1, T2>(npy_types: [NPY_TYPES; 3]) -> NPY_TYPES {
match size_of::<T>() {
x if x == size_of::<T0>() => npy_types[0],
x if x == size_of::<T1>() => npy_types[1],
x if x == size_of::<T2>() => npy_types[2],
_ => panic!("Unable to match integer type descriptor: {:?}", npy_types),
}
}
fn npy_int_type<T: Bounded + Zero + Sized + PartialEq>() -> NPY_TYPES {
let is_unsigned = T::min_value() == T::zero();
let bit_width = 8 * size_of::<T>();
match (is_unsigned, bit_width) {
(false, 8) => NPY_TYPES::NPY_BYTE,
(false, 16) => NPY_TYPES::NPY_SHORT,
(false, 32) => npy_int_type_lookup::<i32, c_long, c_int, c_short>([
NPY_TYPES::NPY_LONG,
NPY_TYPES::NPY_INT,
NPY_TYPES::NPY_SHORT,
]),
(false, 64) => npy_int_type_lookup::<i64, c_long, c_longlong, c_int>([
NPY_TYPES::NPY_LONG,
NPY_TYPES::NPY_LONGLONG,
NPY_TYPES::NPY_INT,
]),
(true, 8) => NPY_TYPES::NPY_UBYTE,
(true, 16) => NPY_TYPES::NPY_USHORT,
(true, 32) => npy_int_type_lookup::<u32, c_ulong, c_uint, c_ushort>([
NPY_TYPES::NPY_ULONG,
NPY_TYPES::NPY_UINT,
NPY_TYPES::NPY_USHORT,
]),
(true, 64) => npy_int_type_lookup::<u64, c_ulong, c_ulonglong, c_uint>([
NPY_TYPES::NPY_ULONG,
NPY_TYPES::NPY_ULONGLONG,
NPY_TYPES::NPY_UINT,
]),
_ => unreachable!(),
}
}
macro_rules! impl_element_scalar {
(@impl: $ty:ty, $npy_type:expr $(,#[$meta:meta])*) => {
$(#[$meta])*
unsafe impl Element for $ty {
const IS_COPY: bool = true;
fn get_dtype(py: Python) -> &PyArrayDescr {
PyArrayDescr::from_npy_type(py, $npy_type)
}
}
};
($ty:ty => $npy_type:ident $(,#[$meta:meta])*) => {
impl_element_scalar!(@impl: $ty, NPY_TYPES::$npy_type $(,#[$meta])*);
};
($($tys:ty),+) => {
$(impl_element_scalar!(@impl: $tys, npy_int_type::<$tys>());)+
};
}
impl_element_scalar!(bool => NPY_BOOL);
impl_element_scalar!(i8, i16, i32, i64);
impl_element_scalar!(u8, u16, u32, u64);
impl_element_scalar!(f32 => NPY_FLOAT);
impl_element_scalar!(f64 => NPY_DOUBLE);
#[cfg(feature = "half")]
impl_element_scalar!(f16 => NPY_HALF);
impl_element_scalar!(Complex32 => NPY_CFLOAT,
#[doc = "Complex type with `f32` components which maps to `numpy.csingle` (`numpy.complex64`)."]);
impl_element_scalar!(Complex64 => NPY_CDOUBLE,
#[doc = "Complex type with `f64` components which maps to `numpy.cdouble` (`numpy.complex128`)."]);
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
impl_element_scalar!(usize, isize);
unsafe impl Element for PyObject {
const IS_COPY: bool = false;
fn get_dtype(py: Python) -> &PyArrayDescr {
PyArrayDescr::object(py)
}
}
#[cfg(test)]
mod tests {
use super::*;
use pyo3::py_run;
use crate::npyffi::NPY_NEEDS_PYAPI;
#[test]
fn test_dtype_new() {
Python::with_gil(|py| {
assert!(PyArrayDescr::new(py, "float64")
.unwrap()
.is(dtype::<f64>(py)));
let dt = PyArrayDescr::new(py, [("a", "O"), ("b", "?")].as_ref()).unwrap();
assert_eq!(dt.names(), Some(vec!["a", "b"]));
assert!(dt.has_object());
assert!(dt.get_field("a").unwrap().0.is(dtype::<PyObject>(py)));
assert!(dt.get_field("b").unwrap().0.is(dtype::<bool>(py)));
assert!(PyArrayDescr::new(py, &123_usize).is_err());
});
}
#[test]
fn test_dtype_names() {
fn type_name<T: Element>(py: Python) -> &str {
dtype::<T>(py).typeobj().name().unwrap()
}
Python::with_gil(|py| {
assert_eq!(type_name::<bool>(py), "bool_");
assert_eq!(type_name::<i8>(py), "int8");
assert_eq!(type_name::<i16>(py), "int16");
assert_eq!(type_name::<i32>(py), "int32");
assert_eq!(type_name::<i64>(py), "int64");
assert_eq!(type_name::<u8>(py), "uint8");
assert_eq!(type_name::<u16>(py), "uint16");
assert_eq!(type_name::<u32>(py), "uint32");
assert_eq!(type_name::<u64>(py), "uint64");
assert_eq!(type_name::<f32>(py), "float32");
assert_eq!(type_name::<f64>(py), "float64");
assert_eq!(type_name::<Complex32>(py), "complex64");
assert_eq!(type_name::<Complex64>(py), "complex128");
#[cfg(target_pointer_width = "32")]
{
assert_eq!(type_name::<usize>(py), "uint32");
assert_eq!(type_name::<isize>(py), "int32");
}
#[cfg(target_pointer_width = "64")]
{
assert_eq!(type_name::<usize>(py), "uint64");
assert_eq!(type_name::<isize>(py), "int64");
}
});
}
#[test]
fn test_dtype_methods_scalar() {
Python::with_gil(|py| {
let dt = dtype::<f64>(py);
assert_eq!(dt.num(), NPY_TYPES::NPY_DOUBLE as c_int);
assert_eq!(dt.flags(), 0);
assert_eq!(dt.typeobj().name().unwrap(), "float64");
assert_eq!(dt.char(), b'd');
assert_eq!(dt.kind(), b'f');
assert_eq!(dt.byteorder(), b'=');
assert_eq!(dt.is_native_byteorder(), Some(true));
assert_eq!(dt.itemsize(), 8);
assert_eq!(dt.alignment(), 8);
assert!(!dt.has_object());
assert_eq!(dt.names(), None);
assert!(!dt.has_fields());
assert!(!dt.is_aligned_struct());
assert!(!dt.has_subarray());
assert!(dt.base().is_equiv_to(dt));
assert_eq!(dt.ndim(), 0);
assert_eq!(dt.shape(), vec![]);
});
}
#[test]
fn test_dtype_methods_subarray() {
Python::with_gil(|py| {
let locals = PyDict::new(py);
py_run!(
py,
*locals,
"dtype = __import__('numpy').dtype(('f8', (2, 3)))"
);
let dt = locals
.get_item("dtype")
.unwrap()
.downcast::<PyArrayDescr>()
.unwrap();
assert_eq!(dt.num(), NPY_TYPES::NPY_VOID as c_int);
assert_eq!(dt.flags(), 0);
assert_eq!(dt.typeobj().name().unwrap(), "void");
assert_eq!(dt.char(), b'V');
assert_eq!(dt.kind(), b'V');
assert_eq!(dt.byteorder(), b'|');
assert_eq!(dt.is_native_byteorder(), None);
assert_eq!(dt.itemsize(), 48);
assert_eq!(dt.alignment(), 8);
assert!(!dt.has_object());
assert_eq!(dt.names(), None);
assert!(!dt.has_fields());
assert!(!dt.is_aligned_struct());
assert!(dt.has_subarray());
assert_eq!(dt.ndim(), 2);
assert_eq!(dt.shape(), vec![2, 3]);
assert!(dt.base().is_equiv_to(dtype::<f64>(py)));
});
}
#[test]
fn test_dtype_methods_record() {
Python::with_gil(|py| {
let locals = PyDict::new(py);
py_run!(
py,
*locals,
"dtype = __import__('numpy').dtype([('x', 'u1'), ('y', 'f8'), ('z', 'O')], align=True)"
);
let dt = locals
.get_item("dtype")
.unwrap()
.downcast::<PyArrayDescr>()
.unwrap();
assert_eq!(dt.num(), NPY_TYPES::NPY_VOID as c_int);
assert_ne!(dt.flags() & NPY_ITEM_HASOBJECT, 0);
assert_ne!(dt.flags() & NPY_NEEDS_PYAPI, 0);
assert_ne!(dt.flags() & NPY_ALIGNED_STRUCT, 0);
assert_eq!(dt.typeobj().name().unwrap(), "void");
assert_eq!(dt.char(), b'V');
assert_eq!(dt.kind(), b'V');
assert_eq!(dt.byteorder(), b'|');
assert_eq!(dt.is_native_byteorder(), None);
assert_eq!(dt.itemsize(), 24);
assert_eq!(dt.alignment(), 8);
assert!(dt.has_object());
assert_eq!(dt.names(), Some(vec!["x", "y", "z"]));
assert!(dt.has_fields());
assert!(dt.is_aligned_struct());
assert!(!dt.has_subarray());
assert_eq!(dt.ndim(), 0);
assert_eq!(dt.shape(), vec![]);
assert!(dt.base().is_equiv_to(dt));
let x = dt.get_field("x").unwrap();
assert!(x.0.is_equiv_to(dtype::<u8>(py)));
assert_eq!(x.1, 0);
let y = dt.get_field("y").unwrap();
assert!(y.0.is_equiv_to(dtype::<f64>(py)));
assert_eq!(y.1, 8);
let z = dt.get_field("z").unwrap();
assert!(z.0.is_equiv_to(dtype::<PyObject>(py)));
assert_eq!(z.1, 16);
});
}
}