From aeb23741b1186e6ae525c4dd7f46ce9e1378e670 Mon Sep 17 00:00:00 2001 From: Steve Joachim Date: Sat, 10 Oct 2020 05:47:20 -0400 Subject: MAINT: Do not emit empty Methods heading in np.info (#17498) Fixes the incompatible type comparison found in #17490. This also corrects the logic to not print the heading when only private/magic methods are present. --- numpy/lib/utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index d511c2a40..5447608bf 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -587,11 +587,11 @@ def info(object=None, maxwidth=76, output=sys.stdout, toplevel='numpy'): print(inspect.getdoc(object), file=output) methods = pydoc.allmethods(object) - if methods != []: + + public_methods = [meth for meth in methods if meth[0] != '_'] + if public_methods: print("\n\nMethods:\n", file=output) - for meth in methods: - if meth[0] == '_': - continue + for meth in public_methods: thisobj = getattr(object, meth, None) if thisobj is not None: methstr, other = pydoc.splitdoc( -- cgit v1.2.1 From 5b63f260933672b7182daf4fb15ffcd15bae68bf Mon Sep 17 00:00:00 2001 From: kumudlakara <55556183+kumudlakara@users.noreply.github.com> Date: Thu, 17 Dec 2020 23:38:22 +0530 Subject: DOC: Doc for deprecate_with_doc (#17852) * Add doc for deprecate_with_doc Co-authored-by: Bas van Beek <43369155+BvB93@users.noreply.github.com> Co-authored-by: Matti Picus --- numpy/lib/utils.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 5447608bf..f7e176cf3 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -193,7 +193,32 @@ def deprecate(*args, **kwargs): else: return _Deprecate(*args, **kwargs) -deprecate_with_doc = lambda msg: _Deprecate(message=msg) + +def deprecate_with_doc(msg): + """ + Deprecates a function and includes the deprecation in its docstring. + + This function is used as a decorator. It returns an object that can be + used to issue a DeprecationWarning, by passing the to-be decorated + function as argument, this adds warning to the to-be decorated function's + docstring and returns the new function object. + + See Also + -------- + deprecate : Decorate a function such that it issues a `DeprecationWarning` + + Parameters + ---------- + msg : str + Additional explanation of the deprecation. Displayed in the + docstring after the warning. + + Returns + ------- + obj : object + + """ + return _Deprecate(message=msg) #-------------------------------------------- -- cgit v1.2.1 From 4152443b1d268951401e0be1351eccbddd572e97 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 4 Feb 2021 18:32:54 +0100 Subject: MAINT: Avoid moveaxis overhead in median. This change speeds up taking the median of 1001 floats by ~20%, as measured by `python -mtimeit -s 'import numpy as np; x = np.random.randn(1001)' -- 'np.median(x)'` --- numpy/lib/utils.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index f7e176cf3..8f5c6eea3 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -1026,8 +1026,7 @@ def _median_nancheck(data, result, axis, out): """ if data.size == 0: return result - data = np.moveaxis(data, axis, -1) - n = np.isnan(data[..., -1]) + n = np.isnan(data.take(-1, axis=axis)) # masked NaN values are ok if np.ma.isMaskedArray(n): n = n.filled(False) -- cgit v1.2.1 From 3cb10bc7178b3a753d7cc1f81414a8ab94b92e64 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 9 Feb 2021 22:26:51 +0100 Subject: DOC: Fix docstring of _median_nancheck. _median_nancheck doesn't support axis being anything other than an integer (otherwise the call to moveaxis would fail). This is fine, because median goes through _ureduce for multi-axis support. --- 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 f7e176cf3..9cf9c1e76 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -1015,8 +1015,8 @@ def _median_nancheck(data, result, axis, out): 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. + axis : int + Axis along which the median was computed. out : ndarray, optional Output array in which to place the result. Returns -- cgit v1.2.1 From 0ef6508397fcdd8f2bbace2a0b91a863b1617728 Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Fri, 16 Apr 2021 19:34:17 -0700 Subject: DOC: Add blank line before section. Otherwise Numpydoc does not see the section. --- numpy/lib/utils.py | 1 + 1 file changed, 1 insertion(+) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 24252c834..91e7cb8a3 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -1019,6 +1019,7 @@ def _median_nancheck(data, result, axis, out): Axis along which the median was computed. out : ndarray, optional Output array in which to place the result. + Returns ------- median : scalar or ndarray -- cgit v1.2.1 From 472b152fdaad523b451410165f126b473f6de0df Mon Sep 17 00:00:00 2001 From: Sayed Adel Date: Tue, 10 Nov 2020 16:16:50 +0000 Subject: ENH: add new function `_opt_info()` to utils provides the optimization info of NumPy build --- numpy/lib/utils.py | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 91e7cb8a3..12a7cacdc 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -197,20 +197,20 @@ def deprecate(*args, **kwargs): def deprecate_with_doc(msg): """ Deprecates a function and includes the deprecation in its docstring. - - This function is used as a decorator. It returns an object that can be - used to issue a DeprecationWarning, by passing the to-be decorated - function as argument, this adds warning to the to-be decorated function's + + This function is used as a decorator. It returns an object that can be + used to issue a DeprecationWarning, by passing the to-be decorated + function as argument, this adds warning to the to-be decorated function's docstring and returns the new function object. - + See Also -------- - deprecate : Decorate a function such that it issues a `DeprecationWarning` - + deprecate : Decorate a function such that it issues a `DeprecationWarning` + Parameters ---------- msg : str - Additional explanation of the deprecation. Displayed in the + Additional explanation of the deprecation. Displayed in the docstring after the warning. Returns @@ -218,7 +218,7 @@ def deprecate_with_doc(msg): obj : object """ - return _Deprecate(message=msg) + return _Deprecate(message=msg) #-------------------------------------------- @@ -1042,4 +1042,30 @@ def _median_nancheck(data, result, axis, out): result[n] = np.nan return result +def _opt_info(): + """ + Returns a string contains the supported CPU features by the current build. + + The string format can be explained as follows: + - dispatched features that are supported by the running machine + end with `*`. + - dispatched features that are "not" supported by the running machine + end with `?`. + - remained features are representing the baseline. + """ + from numpy.core._multiarray_umath import ( + __cpu_features__, __cpu_baseline__, __cpu_dispatch__ + ) + + if len(__cpu_baseline__) == 0 and len(__cpu_dispatch__) == 0: + return '' + + enabled_features = ' '.join(__cpu_baseline__) + for feature in __cpu_dispatch__: + if __cpu_features__[feature]: + enabled_features += f" {feature}*" + else: + enabled_features += f" {feature}?" + + return enabled_features #----------------------------------------------------------------------------- -- cgit v1.2.1 From 7949ba5ec0543d3b1fd68e95072838fbb3333492 Mon Sep 17 00:00:00 2001 From: Andrew Watson Date: Thu, 1 Jul 2021 14:20:57 -0600 Subject: MAINT: fix overly broad exception handling listed in LGTM Relates to ticket #19077 --- 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 12a7cacdc..bfc58c8bd 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -904,7 +904,7 @@ def _lookfor_generate_cache(module, import_modules, regenerate): sys.stdout = old_stdout sys.stderr = old_stderr # Catch SystemExit, too - except BaseException: + except SystemExit: continue for n, v in _getmembers(item): -- cgit v1.2.1 From 5a42312d1ebf2f86e7a9215513585bdd81b4d19b Mon Sep 17 00:00:00 2001 From: Andrew Watson Date: Sat, 3 Jul 2021 08:49:14 -0600 Subject: Update numpy/lib/utils.py Co-authored-by: Matti Picus --- 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 bfc58c8bd..b1a916d4a 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -904,7 +904,7 @@ def _lookfor_generate_cache(module, import_modules, regenerate): sys.stdout = old_stdout sys.stderr = old_stderr # Catch SystemExit, too - except SystemExit: + except (Exception, SystemExit): continue for n, v in _getmembers(item): -- cgit v1.2.1 From 64f15a94708095bf9d29af5dbfcc4b654a57248a Mon Sep 17 00:00:00 2001 From: Mike Taves Date: Mon, 30 Aug 2021 11:01:34 +1200 Subject: MAINT: refactor "for ... in range(len(" statements --- numpy/lib/utils.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index b1a916d4a..1f2cb66fa 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -351,8 +351,7 @@ def who(vardict=None): maxshape = 0 maxbyte = 0 totalbytes = 0 - for k in range(len(sta)): - val = sta[k] + for val in sta: if maxname < len(val[0]): maxname = len(val[0]) if maxshape < len(val[1]): @@ -369,8 +368,7 @@ def who(vardict=None): prval = "Name %s Shape %s Bytes %s Type" % (sp1*' ', sp2*' ', sp3*' ') print(prval + "\n" + "="*(len(prval)+5) + "\n") - for k in range(len(sta)): - val = sta[k] + for val in sta: print("%s %s %s %s %s %s %s" % (val[0], ' '*(sp1-len(val[0])+4), val[1], ' '*(sp2-len(val[1])+5), val[2], ' '*(sp3-len(val[2])+5), -- cgit v1.2.1 From 6ba48721e22622403a60b7f9d3ec5cae308ba3a9 Mon Sep 17 00:00:00 2001 From: Marten van Kerkwijk Date: Mon, 13 Sep 2021 15:50:54 -0400 Subject: BUG: ensure np.median does not drop subclass for NaN result. Currently, np.median is almost completely safe for subclasses, except if the result is NaN. In that case, it assumes the result is a scalar and substitutes a NaN with the right dtype. This PR fixes that, since subclasses like astropy's Quantity generally use array scalars to preserve subclass information such as the unit. --- numpy/lib/utils.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 1f2cb66fa..931669fc1 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -1029,14 +1029,12 @@ def _median_nancheck(data, result, axis, out): # masked NaN values are ok if np.ma.isMaskedArray(n): n = n.filled(False) - if result.ndim == 0: - if n == True: - 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: + if np.count_nonzero(n.ravel()) > 0: + # Without given output, it is possible that the current result is a + # numpy scalar, which is not writeable. If so, just return nan. + if isinstance(result, np.generic): + return data.dtype.type(np.nan) + result[n] = np.nan return result -- cgit v1.2.1 From 0dbc9ad1454aab5044ab0a14b9094db1a3c7c027 Mon Sep 17 00:00:00 2001 From: Marten van Kerkwijk Date: Tue, 14 Sep 2021 13:37:59 -0400 Subject: MAINT: remove unused argument in private function --- numpy/lib/utils.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 931669fc1..cd5bb51e7 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -1002,7 +1002,7 @@ def safe_eval(source): return ast.literal_eval(source) -def _median_nancheck(data, result, axis, out): +def _median_nancheck(data, result, axis): """ 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. @@ -1012,16 +1012,16 @@ def _median_nancheck(data, result, axis, out): data : array Input data to median function result : Array or MaskedArray - Result of median function + Result of median function. axis : int Axis 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. + result : scalar or ndarray + Median or NaN in axes which contained NaN in the input. If the input + was an array, NaN will be inserted in-place. If a scalar, either the + input itself or a scalar NaN. """ if data.size == 0: return result -- cgit v1.2.1 From 9377d360297e72680b2140af6b0d0951d6ae3d9e Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Tue, 14 Sep 2021 14:07:11 -0600 Subject: MAINT: Improve ``_median_nancheck`` docstring. --- 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 cd5bb51e7..1df2ab09b 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -1010,7 +1010,7 @@ def _median_nancheck(data, result, axis): Parameters ---------- data : array - Input data to median function + Sorted input data to median function result : Array or MaskedArray Result of median function. axis : int -- cgit v1.2.1 From 0855709348d2f1d8a84e1205fc414af1fbbe712b Mon Sep 17 00:00:00 2001 From: Andrei Batomunkuev Date: Wed, 24 Nov 2021 01:47:41 +0000 Subject: BUG: Fixed output overriding in numpy.info(), so that it correctly displays in Google Colab. Closes#20423 --- numpy/lib/utils.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 1df2ab09b..96f64ac3e 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -429,7 +429,7 @@ def _makenamedict(module='numpy'): return thedict, dictlist -def _info(obj, output=sys.stdout): +def _info(obj, output=None): """Provide information about ndarray obj. Parameters @@ -455,6 +455,9 @@ def _info(obj, output=sys.stdout): strides = obj.strides endian = obj.dtype.byteorder + if output is None: + output = sys.stdout + print("class: ", nm, file=output) print("shape: ", obj.shape, file=output) print("strides: ", strides, file=output) @@ -481,7 +484,7 @@ def _info(obj, output=sys.stdout): @set_module('numpy') -def info(object=None, maxwidth=76, output=sys.stdout, toplevel='numpy'): +def info(object=None, maxwidth=76, output=None, toplevel='numpy'): """ Get help information for a function, class, or module. @@ -541,6 +544,9 @@ def info(object=None, maxwidth=76, output=sys.stdout, toplevel='numpy'): elif hasattr(object, '_ppimport_attr'): object = object._ppimport_attr + if output is None: + output = sys.stdout + if object is None: info(info) elif isinstance(object, ndarray): -- cgit v1.2.1 From 504a477a637894309ed23d954554ce1ad8bf609e Mon Sep 17 00:00:00 2001 From: Andrei Batomunkuev Date: Wed, 24 Nov 2021 02:17:14 +0000 Subject: Fixed python docstring in info() --- 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 96f64ac3e..cd0e3f3cf 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -499,7 +499,7 @@ def info(object=None, maxwidth=76, output=None, toplevel='numpy'): Printing width. output : file like object, optional File like object that the output is written to, default is - ``stdout``. The object has to be opened in 'w' or 'a' mode. + ``None``. The object has to be opened in 'w' or 'a' mode. toplevel : str, optional Start search at this level. -- cgit v1.2.1 From 860c66c19866050bbf25f7f675ecff3ab88398e2 Mon Sep 17 00:00:00 2001 From: Andrei Batomunkuev Date: Wed, 24 Nov 2021 20:08:48 +0000 Subject: BUG: Fixed docstring of numpy.info() --- numpy/lib/utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index cd0e3f3cf..c74ee127d 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -499,7 +499,8 @@ def info(object=None, maxwidth=76, output=None, toplevel='numpy'): Printing width. output : file like object, optional File like object that the output is written to, default is - ``None``. The object has to be opened in 'w' or 'a' mode. + ``None``, in which case ``sys.stdout`` will be used. + The object has to be opened in 'w' or 'a' mode. toplevel : str, optional Start search at this level. -- cgit v1.2.1