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?
correction - with the right 'data' value your
holderworks innp.array:np.arrayis 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.ndarraythat 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_strideduses__array_interface__for default stride and shape data, but gets the data from an array, not the__array_interface__dictionary.===========
An example of
ndarraywith a.dataattribute:Both of these arrays are views.
OK, with your
holderclass, I can make the same thing, using thisres.dataas the data buffer. Your class creates anobject exposing the array interface.