From eb8913d1a39e09b739a6e9449dc841a52ecbac37 Mon Sep 17 00:00:00 2001 From: Endolith Date: Wed, 11 May 2016 20:09:04 -0400 Subject: DOC: Fix some incorrect RST definition lists --- numpy/lib/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 3f29699e9..a2191468f 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -393,9 +393,9 @@ def _info(obj, output=sys.stdout): Parameters ---------- - obj: ndarray + obj : ndarray Must be ndarray, not checked. - output: + output Where printed output goes. Notes -- cgit v1.2.1 From 7884a8c9f5f5c6657413dbeaa59ad969280d38ea Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Sat, 23 Jan 2016 15:58:58 +0100 Subject: ENH: Add stacklevel to all (or almost all) our function calls --- numpy/lib/utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index a2191468f..133704d13 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -96,7 +96,7 @@ class _Deprecate(object): def newfunc(*args,**kwds): """`arrayrange` is deprecated, use `arange` instead!""" - warnings.warn(depdoc, DeprecationWarning) + warnings.warn(depdoc, DeprecationWarning, stacklevel=2) return func(*args, **kwds) newfunc = _set_function_name(newfunc, old_name) @@ -152,7 +152,7 @@ def deprecate(*args, **kwargs): >>> olduint(6) /usr/lib/python2.5/site-packages/numpy/lib/utils.py:114: DeprecationWarning: uint32 is deprecated - warnings.warn(str1, DeprecationWarning) + warnings.warn(str1, DeprecationWarning, stacklevel=2) 6 """ @@ -1016,7 +1016,7 @@ class SafeEval(object): def __init__(self): # 2014-10-15, 1.10 warnings.warn("SafeEval is deprecated in 1.10 and will be removed.", - DeprecationWarning) + DeprecationWarning, stacklevel=2) def visit(self, node): cls = node.__class__ -- cgit v1.2.1 From 2a55233b81a6ea18a57d1dd4f7bc5fff9f2fb681 Mon Sep 17 00:00:00 2001 From: Pierre de Buyl Date: Tue, 6 Sep 2016 14:42:08 +0200 Subject: DOC: change Numpy to NumPy in dosctrings and comments The strings in error messages were left untouched --- numpy/lib/utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 133704d13..97b93cace 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -241,10 +241,10 @@ def byte_bounds(a): def who(vardict=None): """ - Print the Numpy arrays in the given dictionary. + Print the NumPy arrays in the given dictionary. If there is no dictionary passed in or `vardict` is None then returns - Numpy arrays in the globals() dictionary (all Numpy arrays in the + NumPy arrays in the globals() dictionary (all NumPy arrays in the namespace). Parameters @@ -646,7 +646,7 @@ def info(object=None, maxwidth=76, output=sys.stdout, toplevel='numpy'): def source(object, output=sys.stdout): """ - Print or write to a file the source code for a Numpy object. + Print or write to a file the source code for a NumPy object. The source code is only returned for objects written in Python. Many functions and classes are defined in C and will therefore not return -- cgit v1.2.1 From 15f52f530c4dc1af69e93aae60d81c2008101639 Mon Sep 17 00:00:00 2001 From: Alexandr Shadchin Date: Thu, 8 Dec 2016 17:15:52 +0500 Subject: MAINT: Other cleanup Python < 2.7 and Python3 < 3.4 --- numpy/lib/utils.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 97b93cace..5c364268c 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -916,13 +916,6 @@ def _lookfor_generate_cache(module, import_modules, regenerate): if to_import == '__init__': continue - try: - # Catch SystemExit, too - base_exc = BaseException - except NameError: - # Python 2.4 doesn't have BaseException - base_exc = Exception - try: old_stdout = sys.stdout old_stderr = sys.stderr @@ -933,7 +926,8 @@ def _lookfor_generate_cache(module, import_modules, regenerate): finally: sys.stdout = old_stdout sys.stderr = old_stderr - except base_exc: + # Catch SystemExit, too + except BaseException: continue for n, v in _getmembers(item): -- cgit v1.2.1 From ff4758ff8432077c06cb580b97c6080f9b312c5a Mon Sep 17 00:00:00 2001 From: Julian Taylor Date: Tue, 6 Dec 2016 00:37:53 +0100 Subject: BUG: handle unmasked NaN in ma.median like normal median This requires to base masked median on sort(endwith=False) as we need to distinguish Inf and NaN. Using Inf as filler element of the sort does not work as then the mask is not guaranteed to be at the end. Closes gh-8340 Also fixed 1d ma.median not handling np.inf correctly, the nd variant was ok. --- numpy/lib/utils.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 5c364268c..61aa5e33b 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -8,6 +8,7 @@ import warnings from numpy.core.numerictypes import issubclass_, issubsctype, issubdtype from numpy.core import ndarray, ufunc, asarray +import numpy as np # getargspec and formatargspec were removed in Python 3.6 from numpy.compat import getargspec, formatargspec @@ -1113,4 +1114,49 @@ def safe_eval(source): import ast return ast.literal_eval(source) + + +def _median_nancheck(data, result, axis, out): + """ + Utility function to check median result from data for NaN values at the end + and return NaN in that case. Input result can also be a MaskedArray. + + Parameters + ---------- + data : array + Input data to median function + result : Array or MaskedArray + Result of median function + axis : {int, sequence of int, None}, optional + Axis or axes along which the median was computed. + out : ndarray, optional + Output array in which to place the result. + Returns + ------- + median : scalar or ndarray + Median or NaN in axes which contained NaN in the input. + """ + if data.size == 0: + return result + data = np.rollaxis(data, axis, data.ndim) + n = np.isnan(data[..., -1]) + # masked NaN values are ok + if np.ma.isMaskedArray(n): + n = n.filled(False) + if result.ndim == 0: + if n == True: + warnings.warn("Invalid value encountered in median", + RuntimeWarning, stacklevel=3) + if out is not None: + out[...] = data.dtype.type(np.nan) + result = out + else: + result = data.dtype.type(np.nan) + elif np.count_nonzero(n.ravel()) > 0: + warnings.warn("Invalid value encountered in median for" + + " %d results" % np.count_nonzero(n.ravel()), + RuntimeWarning, stacklevel=3) + result[n] = np.nan + return result + #----------------------------------------------------------------------------- -- cgit v1.2.1 From ef5684564e3074daf614846f30bfdd7f15f5254f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 9 May 2017 12:16:14 +0300 Subject: ENH: Spelling fixes --- numpy/lib/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 61aa5e33b..fad159c7e 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -339,7 +339,7 @@ def who(vardict=None): #----------------------------------------------------------------------------- -# NOTE: pydoc defines a help function which works simliarly to this +# NOTE: pydoc defines a help function which works similarly to this # except it uses a pager to take over the screen. # combine name and arguments and split to multiple lines of width -- cgit v1.2.1 From 1608e53072b035bd40de7a202e75354f0e802120 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Sat, 3 Jun 2017 13:41:26 +0100 Subject: BUG: KeyboardInterrupt is swallowed all over the place Bare except is very rarely the right thing --- numpy/lib/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index fad159c7e..6e150add3 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -557,7 +557,7 @@ def info(object=None, maxwidth=76, output=sys.stdout, toplevel='numpy'): if len(arglist) > 1: arglist[1] = "("+arglist[1] arguments = ", ".join(arglist[1:]) - except: + except Exception: pass if len(name+arguments) > maxwidth: @@ -689,7 +689,7 @@ def source(object, output=sys.stdout): try: print("In file: %s\n" % inspect.getsourcefile(object), file=output) print(inspect.getsource(object), file=output) - except: + except Exception: print("Not available for this object.", file=output) -- cgit v1.2.1 From 029863eae86b9df2de4b9a9843ca8f88c99130df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nico=20Schl=C3=B6mer?= Date: Fri, 11 Aug 2017 03:01:06 +0200 Subject: MAINT: Use moveaxis instead of rollaxis internally (#9475) Also add a hint to the documentation advising the use of moveaxis over rollaxis. Tests for rollaxis are left alone. --- numpy/lib/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 6e150add3..e18eda0fb 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -1138,7 +1138,7 @@ def _median_nancheck(data, result, axis, out): """ if data.size == 0: return result - data = np.rollaxis(data, axis, data.ndim) + data = np.moveaxis(data, axis, -1) n = np.isnan(data[..., -1]) # masked NaN values are ok if np.ma.isMaskedArray(n): -- cgit v1.2.1 From 7cb22f954ed50217f7eb483f4fbdb67953f6482d Mon Sep 17 00:00:00 2001 From: "luz.paz" Date: Thu, 4 Jan 2018 20:48:10 -0500 Subject: More misc. typos Found via `codespell` --- numpy/lib/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index e18eda0fb..1ecd334af 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -707,7 +707,7 @@ def lookfor(what, module=None, import_modules=True, regenerate=False, """ Do a keyword search on docstrings. - A list of of objects that matched the search is displayed, + A list of objects that matched the search is displayed, sorted by relevance. All given keywords need to be found in the docstring for it to be returned as a result, but the order does not matter. -- cgit v1.2.1 From 83828f52b287fefb3d8753a21bd3441997a4d687 Mon Sep 17 00:00:00 2001 From: Mike Toews Date: Sat, 16 Jun 2018 18:18:19 +1200 Subject: HTTP -> HTTPS, and other linkrot fixes --- numpy/lib/utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 1ecd334af..9678bab76 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -982,12 +982,12 @@ def _getmembers(item): #----------------------------------------------------------------------------- # The following SafeEval class and company are adapted from Michael Spencer's -# ASPN Python Cookbook recipe: -# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/364469 +# ASPN Python Cookbook recipe: https://code.activestate.com/recipes/364469/ +# # Accordingly it is mostly Copyright 2006 by Michael Spencer. # The recipe, like most of the other ASPN Python Cookbook recipes was made # available under the Python license. -# http://www.python.org/license +# https://en.wikipedia.org/wiki/Python_License # It has been modified to: # * handle unary -/+ -- cgit v1.2.1 From b88a6785b44844b0b4054d9c77aa4a7d7b6bb1ff Mon Sep 17 00:00:00 2001 From: Emil Hessman Date: Sun, 30 Sep 2018 20:38:02 +0200 Subject: MAINT: remove redundant imports --- numpy/lib/utils.py | 1 - 1 file changed, 1 deletion(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 9678bab76..249873654 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -80,7 +80,6 @@ class _Deprecate(object): new_name = self.new_name message = self.message - import warnings if old_name is None: try: old_name = func.__name__ -- cgit v1.2.1 From 4d24bbda32d133d51940b0691bd9b428d4198eaa Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Tue, 13 Nov 2018 09:38:07 -0800 Subject: ENH: set correct __module__ for objects in numpy's public API Fixes GH-12271 Tests verify that everything in ``dir(numpy)`` either has ``__module__`` set to ``'numpy'``, or appears in an explicit whitelist of undocumented functions and exported bulitins. These should eventually be documented or removed. I also identified a handful of functions for which I had accidentally not setup dispatch for with ``__array_function__`` before, because they were listed under "ndarray methods" in ``_add_newdocs.py``. I guess that should be a lesson in trusting code comments :). --- numpy/lib/utils.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 249873654..ce42c53b9 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -7,6 +7,7 @@ import re import warnings from numpy.core.numerictypes import issubclass_, issubsctype, issubdtype +from numpy.core.overrides import set_module from numpy.core import ndarray, ufunc, asarray import numpy as np @@ -439,6 +440,7 @@ def _info(obj, output=sys.stdout): print("type: %s" % obj.dtype, file=output) +@set_module('numpy') def info(object=None, maxwidth=76, output=sys.stdout, toplevel='numpy'): """ Get help information for a function, class, or module. @@ -644,6 +646,7 @@ def info(object=None, maxwidth=76, output=sys.stdout, toplevel='numpy'): print(inspect.getdoc(object), file=output) +@set_module('numpy') def source(object, output=sys.stdout): """ Print or write to a file the source code for a NumPy object. @@ -701,6 +704,8 @@ _lookfor_caches = {} # signature _function_signature_re = re.compile(r"[a-z0-9_]+\(.*[,=].*\)", re.I) + +@set_module('numpy') def lookfor(what, module=None, import_modules=True, regenerate=False, output=None): """ -- cgit v1.2.1 From b697a9ad15c8c3b44bdf0b67888e0ba98ec0b31e Mon Sep 17 00:00:00 2001 From: mattip Date: Wed, 14 Nov 2018 18:47:34 -0800 Subject: DEV remove shim added in 1.4 --- numpy/lib/utils.py | 7 ------- 1 file changed, 7 deletions(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index ce42c53b9..84edf4021 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -165,13 +165,6 @@ def deprecate(*args, **kwargs): fn = args[0] args = args[1:] - # backward compatibility -- can be removed - # after next release - if 'newname' in kwargs: - kwargs['new_name'] = kwargs.pop('newname') - if 'oldname' in kwargs: - kwargs['old_name'] = kwargs.pop('oldname') - return _Deprecate(*args, **kwargs)(fn) else: return _Deprecate(*args, **kwargs) -- cgit v1.2.1