Commit 2569b175 authored by Kirill Smelkov's avatar Kirill Smelkov

X xnumpy.restructure

Currently fails with:

/home/kirr/src/wendelin/wendelin.core/lib/xnumpy.py in restructure(arr, dtype)
     82     print 'stridev:', stridev
     83     #return np.ndarray.__new__(type(arr), shape, dtype, buffer(arr), 0, stridev)
---> 84     return np.ndarray(shape, dtype, buffer(arr), 0, stridev)

TypeError: expected a single-segment buffer object
parent c1f5bb19
# -*- coding: utf-8 -*-
# NumPy-related utilities
# 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.
import numpy as np
# restructure 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 fully covered by dtype in size.
#
# Restructure is similar to arr.view(dtype) + corresponding reshape, but does
# not have limitations of ndarray.view(). For example:
#
# In [1]: a = np.arange(3*3, dtype=np.int32).reshape((3,3))
#
# In [2]: a
# Out[2]:
# array([[0, 1, 2],
# [3, 4, 5],
# [6, 7, 8]], dtype=int32)
#
# In [3]: b = a[:2,:2]
#
# In [4]: b
# Out[4]:
# array([[0, 1],
# [3, 4]], dtype=int32)
#
# In [5]: b.view(np.int64)
# ---------------------------------------------------------------------------
# ValueError Traceback (most recent call last)
# <ipython-input-66-af98529aa150> in <module>()
# ----> 1 b.view(np.int64)
#
# ValueError: To change to a dtype of a different size, the array must be C-contiguous
#
# In [6]: restructure(b, np.int64)
# TODO
#
# restructure always creates view and never copies data.
#
# TODO tests
def restructure(arr, dtype):
dtype = np.dtype(dtype) # convenience
atype = arr.dtype
# m* denotes minor *
maxis = np.argmin(np.abs(arr.strides))
mstride = arr.strides[maxis]
if mstride < 0:
raise ValueError("minor-axis is not C-contiguous: stride (%d) < 0" % mstride)
if mstride != atype.itemsize:
raise ValueError("minor-axis is not C-contiguous: stride (%d) != itemsize (%d)" % (mstride, atype.itemsize))
# verify dtype fully covers whole minor axis
mnelem = arr.shape[maxis]
msize = mnelem * atype.itemsize
if dtype.itemsize != msize:
raise ValueError("dtype.itemsize (%d) != sizeof(minor-axis) (%d)" % (dtype.itemsize, msize))
# 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)
#!/bin/sh -e
#!/bin/bash -e
# tfault-run <tfault> <arg> <mustdie>
# run `<tfault> <arg>` and verify that it produces correct coredump, dieing for
# SIGSEGV in function <mustdie>.
......
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