forked from pola-rs/polars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffi.py
39 lines (29 loc) · 836 Bytes
/
ffi.py
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
import numpy as np
import numpy.core
from numpy import ctypeslib
import ctypes
from typing import Any
# https://stackoverflow.com/questions/4355524/getting-data-from-ctypes-array-into-numpy
def _ptr_to_numpy(ptr: int, len: int, ptr_type: Any) -> np.ndarray:
"""
Parameters
----------
ptr
C/Rust ptr casted to usize
len
Length of the array values
ptr_type
Example:
f32: ctypes.c_float)
Returns
-------
View of memory block as numpy array
"""
ptr = ctypes.cast(ptr, ctypes.POINTER(ptr_type))
return ctypeslib.as_array(ptr, (len,))
def _as_float_ndarray(ptr, size):
"""
https://github.com/maciejkula/python-rustlearn
Turn a float* to a numpy array.
"""
return np.core.multiarray.int_asbuffer(ptr, size * np.float32.itemsize)