Commit 33ff0c80 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent b4a3a0bd
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
from numpy import ndarray, arange, dtype, int32 from numpy import ndarray, arange, dtype, int32
from numpy.lib.stride_tricks import DummyArray from numpy.lib.stride_tricks import DummyArray
from wendelin.lib.xnumpy import restructure from wendelin.lib.xnumpy import structured
from pytest import raises from pytest import raises
# xbase returns original object from which arr was _as_strided viewed. # xbase returns original object from which arr was _as_strided viewed.
...@@ -32,8 +32,8 @@ def xbase(arr): ...@@ -32,8 +32,8 @@ def xbase(arr):
b = b.base # -> origin b = b.base # -> origin
return b return b
# XXX text # verifies xnumpy.structured()
def test_restructure(): def test_structured():
dtxy = dtype([('x', int32), ('y', int32)]) dtxy = dtype([('x', int32), ('y', int32)])
# C order # C order
...@@ -44,14 +44,14 @@ def test_restructure(): ...@@ -44,14 +44,14 @@ def test_restructure():
# 9 10 11 # 9 10 11
with raises(ValueError, match="minor-axis is not C-contiguous: stride \(-4\) < 0"): with raises(ValueError, match="minor-axis is not C-contiguous: stride \(-4\) < 0"):
restructure(a[:3,:2][:,::-1], dtxy) structured(a[:3,:2][:,::-1], dtxy)
with raises(ValueError, match="minor-axis is not C-contiguous: stride \(8\) != itemsize \(4\)"): with raises(ValueError, match="minor-axis is not C-contiguous: stride \(8\) != itemsize \(4\)"):
restructure(a[:,::2], dtxy) structured(a[:,::2], dtxy)
with raises(ValueError, match="dtype.itemsize \(8\) != sizeof\(minor-axis\) \(12\)"): with raises(ValueError, match="dtype.itemsize \(8\) != sizeof\(minor-axis\) \(12\)"):
restructure(a, dtxy) structured(a, dtxy)
b = a[:3,:2] b = a[:3,:2]
bxy = restructure(b, dtxy) bxy = structured(b, dtxy)
assert xbase(bxy) is b assert xbase(bxy) is b
assert bxy.dtype == dtxy assert bxy.dtype == dtxy
assert bxy.shape == (3,) assert bxy.shape == (3,)
...@@ -77,7 +77,7 @@ def test_restructure(): ...@@ -77,7 +77,7 @@ def test_restructure():
# 6 7 8 # 6 7 8
# 9 10 11 # 9 10 11
b = a[:3,:2][::-1,:] b = a[:3,:2][::-1,:]
bxy = restructure(b, dtxy) bxy = structured(b, dtxy)
assert xbase(bxy) is b assert xbase(bxy) is b
assert bxy.dtype == dtxy assert bxy.dtype == dtxy
assert bxy.shape == (3,) assert bxy.shape == (3,)
...@@ -104,7 +104,7 @@ def test_restructure(): ...@@ -104,7 +104,7 @@ def test_restructure():
# 2 6 10 # 2 6 10
# 3 7 11 # 3 7 11
b = a[:2,:3] b = a[:2,:3]
bxy = restructure(b, dtxy) bxy = structured(b, dtxy)
assert xbase(bxy) is b assert xbase(bxy) is b
assert bxy.dtype == dtxy assert bxy.dtype == dtxy
assert bxy.shape == (3,) assert bxy.shape == (3,)
...@@ -136,7 +136,7 @@ def test_restructure(): ...@@ -136,7 +136,7 @@ def test_restructure():
a = a.view(type=MyArray) a = a.view(type=MyArray)
b = a[:3,:2] b = a[:3,:2]
bxy = restructure(b, dtxy) bxy = structured(b, dtxy)
assert xbase(bxy) is b assert xbase(bxy) is b
assert bxy.dtype == dtxy assert bxy.dtype == dtxy
assert bxy.shape == (3,) assert bxy.shape == (3,)
......
...@@ -60,11 +60,12 @@ def _as_strided(arr, shape, stridev, dtype): ...@@ -60,11 +60,12 @@ def _as_strided(arr, shape, stridev, dtype):
# we are done # we are done
return a return a
# restructure creates view of the array interpreting its minor axis as fully covered by dtype.
# structured creates view of the array interpreting its minor axis as fully covered by dtype.
# #
# The minor axis of the array must be C-contiguous and be of dtype.itemsize in size. # The minor axis of the array must be C-contiguous and be of dtype.itemsize in size.
# #
# Restructure is similar to arr.view(dtype) + corresponding reshape, but does # Structured is similar to arr.view(dtype) + corresponding reshape, but does
# not have limitations of ndarray.view(). For example: # not have limitations of ndarray.view(). For example:
# #
# In [1]: a = np.arange(3*3, dtype=np.int32).reshape((3,3)) # In [1]: a = np.arange(3*3, dtype=np.int32).reshape((3,3))
...@@ -86,7 +87,7 @@ def _as_strided(arr, shape, stridev, dtype): ...@@ -86,7 +87,7 @@ def _as_strided(arr, shape, stridev, dtype):
# #
# In [6]: dtxy # In [6]: dtxy
# Out[6]: dtype([('x', '<i4'), ('y', '<i4')]) # Out[6]: dtype([('x', '<i4'), ('y', '<i4')])
#
# In [7]: b.view(dtxy) # In [7]: b.view(dtxy)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# ValueError Traceback (most recent call last) # ValueError Traceback (most recent call last)
...@@ -95,11 +96,11 @@ def _as_strided(arr, shape, stridev, dtype): ...@@ -95,11 +96,11 @@ def _as_strided(arr, shape, stridev, dtype):
# #
# ValueError: To change to a dtype of a different size, the array must be C-contiguous # ValueError: To change to a dtype of a different size, the array must be C-contiguous
# #
# In [8]: restructure(b, dtxy) # In [8]: structured(b, dtxy)
# Out[8]: array([(0, 1), (3, 4)], dtype=[('x', '<i4'), ('y', '<i4')]) # Out[8]: array([(0, 1), (3, 4)], dtype=[('x', '<i4'), ('y', '<i4')])
# #
# Restructure always creates view and never copies data. # Structured always creates view and never copies data.
def restructure(arr, dtype): def structured(arr, dtype):
dtype = np.dtype(dtype) # convenience dtype = np.dtype(dtype) # convenience
atype = arr.dtype atype = arr.dtype
......
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