Commit 2ad300e8 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 009b9fb9
# -*- coding: utf-8 -*-
# Copyright (C) 2018 Nexedi SA and Contributors.
# Kirill Smelkov <kirr@nexedi.com>
#
# This program is free software: you can Use, Study, Modify and Redistribute
# it under the terms of the GNU General Public License version 3, or (at your
# option) any later version, as published by the Free Software Foundation.
#
# You can also Link and Combine this program with other software covered by
# the terms of any of the Free Software licenses or any of the Open Source
# Initiative approved licenses and Convey the resulting work. Corresponding
# source of such a combination shall include the source code for all other
# software used.
#
# This program is distributed WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See COPYING file for full licensing terms.
# See https://www.nexedi.com/licensing for rationale and options.
from numpy import arange, dtype, int32
from wendelin.lib.xnumpy import restructure
# XXX text
def test_restructure():
a = arange(3*3, dtype=int32).reshape((3,3))
b = a[:2,:2]
dtxy = dtype([('x', int32), ('y', int32)])
bxy = restructure(b, dtxy)
assert bxy.dtype == dtxy
assert bxy.shape == (2,)
assert (0, 1) == (0, 1)
assert bxy[0]['x'] == 0
assert bxy[0]['y'] == 1
assert bxy[1]['x'] == 3
assert bxy[1]['y'] == 4
bxy['x'][0] = 100
assert bxy[0]['x'] == 100
assert b[0,0] == 100
assert a[0,0] == 100
bxy['y'][1] = 200
assert bxy[1]['y'] == 200
assert b[1,1] == 200
assert a[1,1] == 200
......@@ -21,6 +21,7 @@
import numpy as np
from numpy.lib import stride_tricks as npst
# XXX move ArrayRef here.
# restructure creates view of the array interpreting its minor axis as fully covered by dtype.
#
......@@ -44,23 +45,23 @@ from numpy.lib import stride_tricks as npst
# array([[0, 1],
# [3, 4]], dtype=int32)
#
# In [5]: b.view(np.int64)
# In [5]: dtxy = np.dtype([('x', np.int32), ('y', np.int32)])
#
# In [6]: dtxy
# Out[6]: dtype([('x', '<i4'), ('y', '<i4')])
# In [7]: b.view(dtxy)
# ---------------------------------------------------------------------------
# ValueError Traceback (most recent call last)
# <ipython-input-66-af98529aa150> in <module>()
# ----> 1 b.view(np.int64)
# ----> 1 b.view(dtxy)
#
# ValueError: To change to a dtype of a different size, the array must be C-contiguous
#
# In [6]: restructure(b, np.int64)
# 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')])
# In [8]: restructure(b, dtxy)
# Out[8]: array([(0, 1), (3, 4)], dtype=[('x', '<i4'), ('y', '<i4')])
#
# restructure always creates view and never copies data.
#
# TODO tests
def restructure(arr, dtype):
dtype = np.dtype(dtype) # convenience
......
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