Commit fbebef94 authored by Kirill Smelkov's avatar Kirill Smelkov

X `expected a single-segment buffer object` problem gone

parent 2569b175
......@@ -19,6 +19,7 @@
# See COPYING file for full licensing terms.
# See https://www.nexedi.com/licensing for rationale and options.
import numpy as np
from numpy.lib import stride_tricks as npst
# restructure creates view of the array interpreting its minor axis as fully covered by dtype.
......@@ -52,7 +53,10 @@ import numpy as np
# ValueError: To change to a dtype of a different size, the array must be C-contiguous
#
# In [6]: restructure(b, np.int64)
# TODO
# Out[6]: array([ 4294967296, 17179869187])
#
# In [7]: restructure(b, [('x', np.int32), ('y', np.int32)])
# Out[7]: array([(0, 1), (3, 4)], dtype=[('x', '<i4'), ('y', '<i4')])
#
# restructure always creates view and never copies data.
#
......@@ -78,4 +82,26 @@ def restructure(arr, dtype):
# ok to go
shape = arr.shape[:maxis] + arr.shape[maxis+1:]
stridev = arr.strides[:maxis] + arr.strides[maxis+1:]
return np.ndarray.__new__(type(arr), shape, dtype, buffer(arr), 0, stridev)
# NOTE cannot use just np.ndarray because if arr is a slice it can give:
# TypeError: expected a single-segment buffer object
#return np.ndarray.__new__(type(arr), shape, dtype, buffer(arr), 0, stridev)
# XXX dup from ArrayRef
aiface = dict(arr.__array_interface__)
aiface['shape'] = shape
aiface['strides'] = stridev
# type: for now we only care that itemsize is the same
aiface['typestr'] = '|V%d' % dtype.itemsize
aiface['descr'] = [('', aiface['typestr'])]
a = np.asarray(npst.DummyArray(aiface, base=arr))
# restore full dtype - it should not raise here, since itemsize is the same
a.dtype = dtype
# restore full array type
a = a.view(type=type(arr))
# we are done
return a
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment