correction - with the right 'data' value your holder
works in np.array
:
np.array
is definitely not going to work since it expects an iterable, some things like a list of lists, and parses the individual values.
There is a low level constructor, np.ndarray
that takes a buffer parameter. And a np.frombuffer
.
But my impression is that x.__array_interface__['data'][0]
is a integer representation of the data buffer location, but not directly a pointer to the buffer. I've only used it to verify that a view shares the same databuffer, not to construct anything from it.
np.lib.stride_tricks.as_strided
uses __array_interface__
for default stride and shape data, but gets the data from an array, not the __array_interface__
dictionary.
===========
An example of ndarray
with a .data
attribute:
In [303]: res
Out[303]:
array([[ 0, 20, 50, 30],
[ 0, 50, 50, 0],
[ 0, 0, 75, 25]])
In [304]: res.__array_interface__
Out[304]:
{'data': (178919136, False),
'descr': [('', '<i4')],
'shape': (3, 4),
'strides': None,
'typestr': '<i4',
'version': 3}
In [305]: res.data
Out[305]: <memory at 0xb13ef72c>
In [306]: np.ndarray(buffer=res.data, shape=(4,3),dtype=int)
Out[306]:
array([[ 0, 20, 50],
[30, 0, 50],
[50, 0, 0],
[ 0, 75, 25]])
In [324]: np.frombuffer(res.data,dtype=int)
Out[324]: array([ 0, 20, 50, 30, 0, 50, 50, 0, 0, 0, 75, 25])
Both of these arrays are views.
OK, with your holder
class, I can make the same thing, using this res.data
as the data buffer. Your class creates an object exposing the array interface
.
In [379]: holder=numpy_holder()
In [380]: buff={'data':res.data, 'shape':(4,3), 'typestr':'<i4'}
In [381]: holder.__array_interface__ = buff
In [382]: np.array(holder, copy=False)
Out[382]:
array([[ 0, 20, 50],
[30, 0, 50],
[50, 0, 0],
[ 0, 75, 25]])