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 From e28731668251ee7323c11b8afa5c6f8425289ad3 Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Fri, 11 Feb 2022 22:19:50 +0100 Subject: DOC: disambiguate :: in rst. (#21037) Having the `::` on new line can be ambiguous for RST parsers, as `:` is a valid character for header underlines. And as underlines do not have to be as long as the title for some rst parser this appears to be a title. Workaround is to have either a blank line, or put the `::` at the end of previous one. --- 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 c74ee127d..e8f4952d3 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -25,8 +25,7 @@ def get_include(): Notes ----- - When using ``distutils``, for example in ``setup.py``. - :: + When using ``distutils``, for example in ``setup.py``:: import numpy as np ... -- cgit v1.2.1 From fe6369961f4ff136161b0dac2ae2ec620dbce5fb Mon Sep 17 00:00:00 2001 From: ganesh-k13 Date: Thu, 18 Aug 2022 19:17:09 +0530 Subject: ENH: Added `show_runtime` 1. Information is derived with the help of `threadpoolctl` library. 2. In case `threadpoolctl` is not installed, a message is displayed with help on how to install it. 3. SIMD related information is derived from `__cpu_features__`, `__cpu_baseline__` and `__cpu_dispatch__` --- numpy/lib/utils.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index e8f4952d3..479535dcc 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -13,9 +13,86 @@ import numpy as np __all__ = [ 'issubclass_', 'issubsctype', 'issubdtype', 'deprecate', 'deprecate_with_doc', 'get_include', 'info', 'source', 'who', - 'lookfor', 'byte_bounds', 'safe_eval' + 'lookfor', 'byte_bounds', 'safe_eval', 'show_runtime' ] + +def show_runtime(): + """ + Print information about various resources in the system + including available intrinsic support and BLAS/LAPACK library + in use + + See Also + -------- + show_config : Show libraries in the system on which NumPy was built. + + Notes + ----- + 1. Information is derived with the help of `threadpoolctl` + library. + 2. SIMD related information is derived from `__cpu_features__`, + `__cpu_baseline__` and `__cpu_dispatch__` + + Examples + -------- + >>> import numpy as np + >>> np.show_runtime() + [{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'], + 'found': ['SSSE3', + 'SSE41', + 'POPCNT', + 'SSE42', + 'AVX', + 'F16C', + 'FMA3', + 'AVX2'], + 'not_found': ['AVX512F', + 'AVX512CD', + 'AVX512_KNL', + 'AVX512_KNM', + 'AVX512_SKX', + 'AVX512_CLX', + 'AVX512_CNL', + 'AVX512_ICL']}}, + {'architecture': 'Zen', + 'filepath': '/usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so', + 'internal_api': 'openblas', + 'num_threads': 12, + 'prefix': 'libopenblas', + 'threading_layer': 'pthreads', + 'user_api': 'blas', + 'version': '0.3.20'}] + """ + from numpy.core._multiarray_umath import ( + __cpu_features__, __cpu_baseline__, __cpu_dispatch__ + ) + from pprint import pprint + config_found = [] + features_found, features_not_found = [], [] + for feature in __cpu_dispatch__: + if __cpu_features__[feature]: + features_found.append(feature) + else: + features_not_found.append(feature) + config_found.append({ + "simd_extensions": { + "baseline": __cpu_baseline__, + "found": features_found, + "not_found": features_not_found + } + }) + try: + from threadpoolctl import threadpool_info + config_found.extend(threadpool_info()) + except ImportError: + print("WARNING: `threadpoolctl` not found in system!" + " Install it by `pip install threadpoolctl`." + " Once installed, try `np.show_runtime` again" + " for more detailed build information") + pprint(config_found) + + def get_include(): """ Return the directory that contains the NumPy \\*.h header files. -- cgit v1.2.1 From 3d2fe4ec796be6ae63c482e4c4f74e3c7867ff1d Mon Sep 17 00:00:00 2001 From: Ganesh Kathiresan Date: Sun, 21 Aug 2022 11:05:30 +0530 Subject: DOC: Fixed links for np.show_runtime (#21468) --- 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 479535dcc..2fcf270c4 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -29,10 +29,10 @@ def show_runtime(): Notes ----- - 1. Information is derived with the help of `threadpoolctl` + 1. Information is derived with the help of `threadpoolctl `_ library. - 2. SIMD related information is derived from `__cpu_features__`, - `__cpu_baseline__` and `__cpu_dispatch__` + 2. SIMD related information is derived from ``__cpu_features__``, + ``__cpu_baseline__`` and ``__cpu_dispatch__`` Examples -------- -- cgit v1.2.1 From 7e9f225ce23fd6df6893020f54b1b5daf883f9b9 Mon Sep 17 00:00:00 2001 From: Josh Wilson Date: Sat, 19 Jan 2019 13:45:26 -0800 Subject: MAINT: update function's `__module__` attribute in `deprecate` Currently the location of the function definition is always reported to be `numpy.lib.utils`; this changes it to be the location of the actual definition when possible. --- numpy/lib/utils.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 2fcf270c4..c0c55ea8e 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -122,11 +122,6 @@ def get_include(): return d -def _set_function_name(func, name): - func.__name__ = name - return func - - class _Deprecate: """ Decorator class to deprecate old functions. @@ -172,7 +167,7 @@ class _Deprecate: warnings.warn(depdoc, DeprecationWarning, stacklevel=2) return func(*args, **kwds) - newfunc = _set_function_name(newfunc, old_name) + newfunc.__name__ = old_name doc = func.__doc__ if doc is None: doc = depdoc @@ -200,6 +195,10 @@ class _Deprecate: pass else: newfunc.__dict__.update(d) + try: + newfunc.__module__ = func.__module__ + except AttributeError: + pass return newfunc -- cgit v1.2.1 From 052db426f5139936e578f9ccba31a2d6f98bc2fd Mon Sep 17 00:00:00 2001 From: Josh Wilson Date: Tue, 22 Jan 2019 20:25:46 -0800 Subject: MAINT: use `functools.wraps` in `np.deprecate` --- numpy/lib/utils.py | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index c0c55ea8e..497827e75 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -4,6 +4,7 @@ import textwrap import types import re import warnings +import functools from numpy.core.numerictypes import issubclass_, issubsctype, issubdtype from numpy.core.overrides import set_module @@ -149,10 +150,7 @@ class _Deprecate: message = self.message if old_name is None: - try: - old_name = func.__name__ - except AttributeError: - old_name = func.__name__ + old_name = func.__name__ if new_name is None: depdoc = "`%s` is deprecated!" % old_name else: @@ -162,8 +160,8 @@ class _Deprecate: if message is not None: depdoc += "\n" + message - def newfunc(*args,**kwds): - """`arrayrange` is deprecated, use `arange` instead!""" + @functools.wraps(func) + def newfunc(*args, **kwds): warnings.warn(depdoc, DeprecationWarning, stacklevel=2) return func(*args, **kwds) @@ -189,16 +187,7 @@ class _Deprecate: depdoc = textwrap.indent(depdoc, ' ' * indent) doc = '\n\n'.join([depdoc, doc]) newfunc.__doc__ = doc - try: - d = func.__dict__ - except AttributeError: - pass - else: - newfunc.__dict__.update(d) - try: - newfunc.__module__ = func.__module__ - except AttributeError: - pass + return newfunc -- cgit v1.2.1 From b654752c240936596d23b5bfb2e00d2bacbcf50f Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Sat, 1 Oct 2022 02:55:12 +0200 Subject: REV: Losen `lookfor`'s import try/except again (#22356) Some BaseExceptions (at least the Skipped that pytest uses) need to be caught as well. It seems easiest to be practical and keep ignoring almost all exception in this particular code path. Effectively reverts parts of gh-19393 Closes gh-22345 Co-authored-by: Sebastian Berg --- numpy/lib/utils.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 497827e75..f9d29e89d 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -972,8 +972,12 @@ def _lookfor_generate_cache(module, import_modules, regenerate): finally: sys.stdout = old_stdout sys.stderr = old_stderr - # Catch SystemExit, too - except (Exception, SystemExit): + except KeyboardInterrupt: + # Assume keyboard interrupt came from a user + raise + except BaseException: + # Ignore also SystemExit and pytests.importorskip + # `Skipped` (these are BaseExceptions; gh-22345) continue for n, v in _getmembers(item): -- cgit v1.2.1 From 81bc4565b50c6cebb21c95c685285e32e1fb9b65 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Tue, 4 Oct 2022 13:36:08 +0200 Subject: MAINT: Ensure graceful handling of large header sizes This ensures graceful handling of large header files. Unfortunately, it may be a bit inconvenient for users, thus the new kwarg and the work-around of also accepting allow-pickle. See also the documation here: https://docs.python.org/3.10/library/ast.html#ast.literal_eval --- numpy/lib/utils.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 497827e75..8aefe2cab 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -1032,6 +1032,12 @@ def safe_eval(source): Evaluate a string containing a Python literal expression without allowing the execution of arbitrary non-literal code. + .. warning:: + + This function is identical to :py:meth:`ast.literal_eval` and + has the same security implications. It may not always be safe + to evaluate large input strings. + Parameters ---------- source : str -- cgit v1.2.1 From 7c361420b4f81713f593ebbb5c924121c1f2d19d Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Fri, 18 Nov 2022 15:11:25 +0100 Subject: MAINT: Move set_module to numpy.core to use without C import --- 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 afde8cc60..2a9d30b16 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -6,8 +6,8 @@ import re import warnings import functools +from .._utils import set_module 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 -- cgit v1.2.1 From 67b20a5065d634d6f7301fbc2fc5e5e263400ac9 Mon Sep 17 00:00:00 2001 From: mattip Date: Wed, 21 Dec 2022 11:55:34 +0200 Subject: MAINT: expand show_rutime, add it to issue template [skip ci] --- numpy/lib/utils.py | 40 +++++++++------------------------------- 1 file changed, 9 insertions(+), 31 deletions(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 2a9d30b16..8d2d2fe33 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -5,6 +5,7 @@ import types import re import warnings import functools +import platform from .._utils import set_module from numpy.core.numerictypes import issubclass_, issubsctype, issubdtype @@ -24,6 +25,8 @@ def show_runtime(): including available intrinsic support and BLAS/LAPACK library in use + .. versionadded:: 1.24.0 + See Also -------- show_config : Show libraries in the system on which NumPy was built. @@ -31,45 +34,20 @@ def show_runtime(): Notes ----- 1. Information is derived with the help of `threadpoolctl `_ - library. + library if available. 2. SIMD related information is derived from ``__cpu_features__``, ``__cpu_baseline__`` and ``__cpu_dispatch__`` - Examples - -------- - >>> import numpy as np - >>> np.show_runtime() - [{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'], - 'found': ['SSSE3', - 'SSE41', - 'POPCNT', - 'SSE42', - 'AVX', - 'F16C', - 'FMA3', - 'AVX2'], - 'not_found': ['AVX512F', - 'AVX512CD', - 'AVX512_KNL', - 'AVX512_KNM', - 'AVX512_SKX', - 'AVX512_CLX', - 'AVX512_CNL', - 'AVX512_ICL']}}, - {'architecture': 'Zen', - 'filepath': '/usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so', - 'internal_api': 'openblas', - 'num_threads': 12, - 'prefix': 'libopenblas', - 'threading_layer': 'pthreads', - 'user_api': 'blas', - 'version': '0.3.20'}] """ from numpy.core._multiarray_umath import ( __cpu_features__, __cpu_baseline__, __cpu_dispatch__ ) from pprint import pprint - config_found = [] + config_found = [{ + "numpy_version": np.__version__, + "python": sys.version, + "uname": platform.uname(), + }] features_found, features_not_found = [], [] for feature in __cpu_dispatch__: if __cpu_features__[feature]: -- cgit v1.2.1 From d559513a682ae91a85d823b414df7588c04b1e5a Mon Sep 17 00:00:00 2001 From: warren Date: Tue, 7 Mar 2023 09:36:37 -0500 Subject: DOC: Document that info() handles array instances. [skip actions] [skip travis] [skip azp] [skip cirrus] --- numpy/lib/utils.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'numpy/lib/utils.py') diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 8d2d2fe33..5d5770c71 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -533,10 +533,11 @@ def info(object=None, maxwidth=76, output=None, toplevel='numpy'): Parameters ---------- object : object or str, optional - Input object or name to get information about. If `object` is a - numpy object, its docstring is given. If it is a string, available - modules are searched for matching objects. If None, information - about `info` itself is returned. + Input object or name to get information about. If `object` is + an `ndarray` instance, information about the array is printed. + If `object` is a numpy object, its docstring is given. If it is + a string, available modules are searched for matching objects. + If None, information about `info` itself is returned. maxwidth : int, optional Printing width. output : file like object, optional @@ -575,6 +576,22 @@ def info(object=None, maxwidth=76, output=None, toplevel='numpy'): *** Repeat reference found in numpy.fft.fftpack *** *** Total of 3 references found. *** + When the argument is an array, information about the array is printed. + + >>> a = np.array([[1 + 2j, 3, -4], [-5j, 6, 0]], dtype=np.complex64) + >>> np.info(a) + class: ndarray + shape: (2, 3) + strides: (24, 8) + itemsize: 8 + aligned: True + contiguous: True + fortran: False + data pointer: 0x562b6e0d2860 + byteorder: little + byteswap: False + type: complex64 + """ global _namedict, _dictlist # Local import to speed up numpy's import time. -- cgit v1.2.1 From 3b57b3294e594d3635fecc13a0923994e8ab36fc Mon Sep 17 00:00:00 2001 From: warren Date: Tue, 7 Mar 2023 12:00:11 -0500 Subject: DOC: Edit top line of info() docstring to mention array. [skip actions] [skip travis] [skip azp] [skip cirrus] --- 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 5d5770c71..b5add0ace 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -528,7 +528,7 @@ def _info(obj, output=None): @set_module('numpy') def info(object=None, maxwidth=76, output=None, toplevel='numpy'): """ - Get help information for a function, class, or module. + Get help information for an array, function, class, or module. Parameters ---------- -- cgit v1.2.1 From 700c0342a8dbfd5649e72b3ddfdd98f7c9cb58dc Mon Sep 17 00:00:00 2001 From: warren Date: Tue, 7 Mar 2023 18:04:01 -0500 Subject: DOC: Add 'may vary' markup in info() docstring. [skip actions] [skip travis] [skip cirrus] --- 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 b5add0ace..095c914db 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -587,7 +587,7 @@ def info(object=None, maxwidth=76, output=None, toplevel='numpy'): aligned: True contiguous: True fortran: False - data pointer: 0x562b6e0d2860 + data pointer: 0x562b6e0d2860 # may vary byteorder: little byteswap: False type: complex64 -- cgit v1.2.1