dev-python/numpy: Backport several fixes from 0.10.2

fixing regression in pandas, statsmodel and bottleneck

Package-Manager: portage-2.2.23
Signed-off-by: Justin Lecher <jlec@gentoo.org>
This commit is contained in:
Justin Lecher
2015-10-20 21:24:34 +02:00
parent f379e65bc7
commit bb027200ad
3 changed files with 354 additions and 0 deletions

View File

@@ -0,0 +1,127 @@
From 3a816a4db9b498eb64eb837fdcca0fa8ddbe063e Mon Sep 17 00:00:00 2001
From: Allan Haldane <allan.haldane@gmail.com>
Date: Sat, 17 Oct 2015 14:00:36 -0400
Subject: [PATCH] BUG: recarrays viewed as subarrays don't convert to np.record
type
Record array views were updated in #5943 to return np.record dtype
where possible, but forgot about the case of sub-arrays.
That's fixed here, so accessing subarray fields by attribute or index
works sensibly, as well as viewing a record array as a subarray dtype,
and printing subarrays.
This also happens to fix #6459, since it affects the same lines.
Fixes #6497 #6459
---
numpy/core/records.py | 30 +++++++++++++++++++-----------
numpy/core/tests/test_records.py | 23 +++++++++++++++++++++++
2 files changed, 42 insertions(+), 11 deletions(-)
diff --git a/numpy/core/records.py b/numpy/core/records.py
index 4a99553..4ce3fe9 100644
--- a/numpy/core/records.py
+++ b/numpy/core/records.py
@@ -448,12 +448,14 @@ def __getattribute__(self, attr):
# At this point obj will always be a recarray, since (see
# PyArray_GetField) the type of obj is inherited. Next, if obj.dtype is
- # non-structured, convert it to an ndarray. If obj is structured leave
- # it as a recarray, but make sure to convert to the same dtype.type (eg
- # to preserve numpy.record type if present), since nested structured
- # fields do not inherit type.
+ # non-structured, convert it to an ndarray. Then if obj is structured
+ # with void type convert it to the same dtype.type (eg to preserve
+ # numpy.record type if present), since nested structured fields do not
+ # inherit type. Don't do this for non-void structures though.
if obj.dtype.fields:
- return obj.view(dtype=(self.dtype.type, obj.dtype.fields))
+ if issubclass(obj.dtype.type, nt.void):
+ return obj.view(dtype=(self.dtype.type, obj.dtype))
+ return obj
else:
return obj.view(ndarray)
@@ -463,8 +465,9 @@ def __getattribute__(self, attr):
# Thus, you can't create attributes on-the-fly that are field names.
def __setattr__(self, attr, val):
- # Automatically convert (void) dtypes to records.
- if attr == 'dtype' and issubclass(val.type, nt.void):
+ # Automatically convert (void) structured types to records
+ # (but not non-void structures, subarrays, or non-structured voids)
+ if attr == 'dtype' and issubclass(val.type, nt.void) and val.fields:
val = sb.dtype((record, val))
newattr = attr not in self.__dict__
@@ -499,7 +502,9 @@ def __getitem__(self, indx):
# we might also be returning a single element
if isinstance(obj, ndarray):
if obj.dtype.fields:
- return obj.view(dtype=(self.dtype.type, obj.dtype.fields))
+ if issubclass(obj.dtype.type, nt.void):
+ return obj.view(dtype=(self.dtype.type, obj.dtype))
+ return obj
else:
return obj.view(type=ndarray)
else:
@@ -519,11 +524,14 @@ def __repr__(self):
# If this is a full record array (has numpy.record dtype),
# or if it has a scalar (non-void) dtype with no records,
# represent it using the rec.array function. Since rec.array
- # converts dtype to a numpy.record for us, use only dtype.descr,
- # not repr(dtype).
+ # converts dtype to a numpy.record for us, convert back
+ # to non-record before printing
+ plain_dtype = self.dtype
+ if plain_dtype.type is record:
+ plain_dtype = sb.dtype((nt.void, plain_dtype))
lf = '\n'+' '*len("rec.array(")
return ('rec.array(%s, %sdtype=%s)' %
- (lst, lf, repr(self.dtype.descr)))
+ (lst, lf, plain_dtype))
else:
# otherwise represent it using np.array plus a view
# This should only happen if the user is playing
diff --git a/numpy/core/tests/test_records.py b/numpy/core/tests/test_records.py
index 7a18f29..290bc4f 100644
--- a/numpy/core/tests/test_records.py
+++ b/numpy/core/tests/test_records.py
@@ -121,6 +121,23 @@ def test_recarray_views(self):
assert_equal(type(rv), np.recarray)
assert_equal(rv.dtype.type, np.record)
+ # check that accessing nested structures keep record type, but
+ # not for subarrays, non-void structures, non-structured voids
+ test_dtype = [('a', 'f4,f4'), ('b', 'V8'), ('c', ('f4',2)),
+ ('d', ('i8', 'i4,i4'))]
+ r = np.rec.array([((1,1), b'11111111', [1,1], 1),
+ ((1,1), b'11111111', [1,1], 1)], dtype=test_dtype)
+ assert_equal(r.a.dtype.type, np.record)
+ assert_equal(r.b.dtype.type, np.void)
+ assert_equal(r.c.dtype.type, np.float32)
+ assert_equal(r.d.dtype.type, np.int64)
+ # check the same, but for views
+ r = np.rec.array(np.ones(4, dtype='i4,i4'))
+ assert_equal(r.view('f4,f4').dtype.type, np.record)
+ assert_equal(r.view(('i4',2)).dtype.type, np.int32)
+ assert_equal(r.view('V8').dtype.type, np.void)
+ assert_equal(r.view(('i8', 'i4,i4')).dtype.type, np.int64)
+
#check that we can undo the view
arrs = [np.ones(4, dtype='f4,i4'), np.ones(4, dtype='f8')]
for arr in arrs:
@@ -135,6 +152,12 @@ def test_recarray_repr(self):
a = np.array(np.ones(4, dtype='f8'))
assert_(repr(np.rec.array(a)).startswith('rec.array'))
+ # check that the 'np.record' part of the dtype isn't shown
+ a = np.rec.array(np.ones(3, dtype='i4,i4'))
+ assert_equal(repr(a).find('numpy.record'), -1)
+ a = np.rec.array(np.ones(3, dtype='i4'))
+ assert_(repr(a).find('dtype=int32') != -1)
+
def test_recarray_from_names(self):
ra = np.rec.array([
(1, 'abc', 3.7000002861022949, 0),

View File

@@ -0,0 +1,73 @@
From 0d25dc4175e00cdaf9545e8b1b1a5b879cf67248 Mon Sep 17 00:00:00 2001
From: Ethan Kruse <eakruse@uw.edu>
Date: Mon, 19 Oct 2015 13:29:01 -0700
Subject: [PATCH 1/2] Potential fix for #6462
---
numpy/lib/function_base.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py
index 555d083..fef69df 100644
--- a/numpy/lib/function_base.py
+++ b/numpy/lib/function_base.py
@@ -3339,7 +3339,7 @@ def _median(a, axis=None, out=None, overwrite_input=False):
indexer[axis] = slice(index-1, index+1)
# Check if the array contains any nan's
- if np.issubdtype(a.dtype, np.inexact):
+ if np.issubdtype(a.dtype, np.inexact) and sz > 0:
# warn and return nans like mean would
rout = mean(part[indexer], axis=axis, out=out)
part = np.rollaxis(part, axis, part.ndim)
From 59d859fb2160950ac93267d7461ad952145c8724 Mon Sep 17 00:00:00 2001
From: Ethan Kruse <eakruse@uw.edu>
Date: Tue, 20 Oct 2015 11:40:49 -0700
Subject: [PATCH 2/2] Added tests for median of empty arrays
---
numpy/lib/tests/test_function_base.py | 30 ++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py
index 4516c92..aa41c1f 100644
--- a/numpy/lib/tests/test_function_base.py
+++ b/numpy/lib/tests/test_function_base.py
@@ -2597,6 +2597,36 @@ def test_nan_behavior(self):
assert_equal(np.median(a, (0, 2)), b)
assert_equal(len(w), 1)
+ def test_empty(self):
+ # empty arrays
+ a = np.array([], dtype=float)
+ with warnings.catch_warnings(record=True) as w:
+ warnings.filterwarnings('always', '', RuntimeWarning)
+ assert_equal(np.median(a), np.nan)
+ assert_(w[0].category is RuntimeWarning)
+
+ # multiple dimensions
+ a = np.array([], dtype=float, ndmin=3)
+ # no axis
+ with warnings.catch_warnings(record=True) as w:
+ warnings.filterwarnings('always', '', RuntimeWarning)
+ assert_equal(np.median(a), np.nan)
+ assert_(w[0].category is RuntimeWarning)
+
+ # axis 0 and 1
+ b = np.array([], dtype=float, ndmin=2)
+ with warnings.catch_warnings(record=True) as w:
+ warnings.filterwarnings('always', '', RuntimeWarning)
+ assert_equal(np.median(a, axis=0), b)
+ assert_equal(np.median(a, axis=1), b)
+
+ # axis 2
+ b = np.array(np.nan, dtype=float, ndmin=2)
+ with warnings.catch_warnings(record=True) as w:
+ warnings.filterwarnings('always', '', RuntimeWarning)
+ assert_equal(np.median(a, axis=2), b)
+ assert_(w[0].category is RuntimeWarning)
+
def test_object(self):
o = np.arange(7.)
assert_(type(np.median(o.astype(object))), float)

View File

@@ -0,0 +1,154 @@
# Copyright 1999-2015 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$
EAPI=5
PYTHON_COMPAT=( python2_7 python3_{3,4,5} )
FORTRAN_NEEDED=lapack
inherit distutils-r1 eutils flag-o-matic fortran-2 multilib multiprocessing toolchain-funcs versionator
DOC_PV="1.9.1"
DOC_P="${PN}-${DOC_PV}"
DESCRIPTION="Fast array and numerical python library"
HOMEPAGE="http://www.numpy.org/"
SRC_URI="mirror://pypi/${PN:0:1}/${PN}/${P}.tar.gz
doc? (
http://docs.scipy.org/doc/${DOC_P}/${PN}-html-${DOC_PV}.zip
http://docs.scipy.org/doc/${DOC_P}/${PN}-ref-${DOC_PV}.pdf
http://docs.scipy.org/doc/${DOC_P}/${PN}-user-${DOC_PV}.pdf
)"
# It appears the docs haven't been upgraded, still @ 1.8.1
LICENSE="BSD"
SLOT="0"
KEYWORDS="~alpha ~amd64 ~arm ~arm64 ~hppa ~ia64 ~mips ~ppc ~ppc64 ~s390 ~sh ~sparc ~x86 ~amd64-fbsd ~x86-fbsd ~x86-freebsd ~x86-interix ~amd64-linux ~arm-linux ~x86-linux ~ppc-macos ~x64-macos ~x86-macos ~sparc-solaris ~x64-solaris ~x86-solaris"
IUSE="doc lapack test"
RDEPEND="
dev-python/setuptools[${PYTHON_USEDEP}]
lapack? ( virtual/cblas virtual/lapack )"
DEPEND="${RDEPEND}
doc? ( app-arch/unzip )
lapack? ( virtual/pkgconfig )
test? ( >=dev-python/nose-1.0[${PYTHON_USEDEP}] )"
# Uses distutils.command.config.
DISTUTILS_IN_SOURCE_BUILD=1
PATCHES=(
"${FILESDIR}"/${PN}-1.9.2-no-hardcode-blas.patch
"${FILESDIR}"/${P}-backport-1.patch
"${FILESDIR}"/${P}-backport-2.patch
)
src_unpack() {
default
if use doc; then
unzip -qo "${DISTDIR}"/${PN}-html-${DOC_PV}.zip -d html || die
fi
}
pc_incdir() {
$(tc-getPKG_CONFIG) --cflags-only-I $@ | \
sed -e 's/^-I//' -e 's/[ ]*-I/:/g' -e 's/[ ]*$//' -e 's|^:||'
}
pc_libdir() {
$(tc-getPKG_CONFIG) --libs-only-L $@ | \
sed -e 's/^-L//' -e 's/[ ]*-L/:/g' -e 's/[ ]*$//' -e 's|^:||'
}
pc_libs() {
$(tc-getPKG_CONFIG) --libs-only-l $@ | \
sed -e 's/[ ]-l*\(pthread\|m\)\([ ]\|$\)//g' \
-e 's/^-l//' -e 's/[ ]*-l/,/g' -e 's/[ ]*$//' \
| tr ',' '\n' | sort -u | tr '\n' ',' | sed -e 's|,$||'
}
python_prepare_all() {
if use lapack; then
append-ldflags "$($(tc-getPKG_CONFIG) --libs-only-other cblas lapack)"
local libdir="${EPREFIX}"/usr/$(get_libdir)
# make sure _dotblas.so gets built
sed -i -e '/NO_ATLAS_INFO/,+1d' numpy/core/setup.py || die
cat >> site.cfg <<-EOF
[blas]
include_dirs = $(pc_incdir cblas)
library_dirs = $(pc_libdir cblas blas):${libdir}
blas_libs = $(pc_libs cblas blas)
[lapack]
library_dirs = $(pc_libdir lapack):${libdir}
lapack_libs = $(pc_libs lapack)
EOF
else
export {ATLAS,PTATLAS,BLAS,LAPACK,MKL}=None
fi
export CC="$(tc-getCC) ${CFLAGS}"
append-flags -fno-strict-aliasing
# See progress in http://projects.scipy.org/scipy/numpy/ticket/573
# with the subtle difference that we don't want to break Darwin where
# -shared is not a valid linker argument
if [[ ${CHOST} != *-darwin* ]]; then
append-ldflags -shared
fi
# only one fortran to link with:
# linking with cblas and lapack library will force
# autodetecting and linking to all available fortran compilers
append-fflags -fPIC
if use lapack; then
NUMPY_FCONFIG="config_fc --noopt --noarch"
# workaround bug 335908
[[ $(tc-getFC) == *gfortran* ]] && NUMPY_FCONFIG+=" --fcompiler=gnu95"
fi
# don't version f2py, we will handle it.
sed -i -e '/f2py_exe/s:+os\.path.*$::' numpy/f2py/setup.py || die
# we don't have f2py-3.3
sed \
-e "/f2py_cmd/s:'f2py'.*:'f2py':g" \
-i numpy/tests/test_scripts.py || die
distutils-r1_python_prepare_all
}
python_compile() {
distutils-r1_python_compile -j $(makeopts_jobs) ${NUMPY_FCONFIG}
}
python_test() {
distutils_install_for_testing ${NUMPY_FCONFIG}
cd "${TMPDIR}" || die
${EPYTHON} -c "
import numpy, sys
r = numpy.test(label='full', verbose=3)
sys.exit(0 if r.wasSuccessful() else 1)" || die "Tests fail with ${EPYTHON}"
}
python_install() {
distutils-r1_python_install ${NUMPY_FCONFIG}
}
python_install_all() {
distutils-r1_python_install_all
dodoc COMPATIBILITY DEV_README.txt THANKS.txt
if use doc; then
dohtml -r "${WORKDIR}"/html/*
dodoc "${DISTDIR}"/${PN}-{user,ref}-${DOC_PV}.pdf
fi
# absent in 1.9
#docinto f2py
#dodoc numpy/f2py/docs/*.txt
#doman numpy/f2py/f2py.1
}