Suppose I have an __array_interface__
dictionary and I would like to create a numpy view of this data from the dictionary itself. For example:
buff = {'shape': (3, 3), 'data': (140546686381536, False), 'typestr': '<f8'}
view = np.array(buff, copy=False)
However, this does not work as np.array
searches for either the buffer or array interface as attributes. The simple workaround could be the following:
class numpy_holder(object):
pass
holder = numpy_holder()
holder.__array_interface__ = buff
view = np.array(holder, copy=False)
This seems a bit roundabout. Am I missing a straightforward way to do this?
Here's another approach:
Usage:
correction - with the right 'data' value your
holder
works innp.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 anp.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:Both of these arrays are views.
OK, with your
holder
class, I can make the same thing, using thisres.data
as the data buffer. Your class creates anobject exposing the array interface
.