From 250861059b106371cb232456eeccd6d9e97d8f00 Mon Sep 17 00:00:00 2001 From: Tyler Reddy Date: Wed, 14 Nov 2018 11:36:59 -0800 Subject: TST, DOC: enable refguide_check * ported the refguide_check module from SciPy for usage in NumPy docstring execution/ verification; added the refguide_check run to Azure Mac OS CI * adjusted NumPy docstrings such that refguide_check passes --- numpy/core/arrayprint.py | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) (limited to 'numpy/core/arrayprint.py') diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index 6a71de226..7d8785c32 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -201,21 +201,21 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None, Floating point precision can be set: >>> np.set_printoptions(precision=4) - >>> print(np.array([1.123456789])) - [ 1.1235] + >>> np.array([1.123456789]) + [1.1235] Long arrays can be summarised: >>> np.set_printoptions(threshold=5) - >>> print(np.arange(10)) - [0 1 2 ..., 7 8 9] + >>> np.arange(10) + array([0, 1, 2, ..., 7, 8, 9]) Small results can be suppressed: >>> eps = np.finfo(float).eps >>> x = np.arange(4.) >>> x**2 - (x + eps)**2 - array([ -4.9304e-32, -4.4409e-16, 0.0000e+00, 0.0000e+00]) + array([-4.9304e-32, -4.4409e-16, 0.0000e+00, 0.0000e+00]) >>> np.set_printoptions(suppress=True) >>> x**2 - (x + eps)**2 array([-0., -0., 0., 0.]) @@ -299,9 +299,10 @@ def printoptions(*args, **kwargs): Examples -------- + >>> from numpy.testing import assert_equal >>> with np.printoptions(precision=2): - ... print(np.array([2.0])) / 3 - [0.67] + ... np.array([2.0]) / 3 + array([0.67]) The `as`-clause of the `with`-statement gives the current print options: @@ -644,9 +645,9 @@ def array2string(a, max_line_width=None, precision=None, Examples -------- >>> x = np.array([1e-16,1,2,3]) - >>> print(np.array2string(x, precision=2, separator=',', - ... suppress_small=True)) - [ 0., 1., 2., 3.] + >>> np.array2string(x, precision=2, separator=',', + ... suppress_small=True) + '[0.,1.,2.,3.]' >>> x = np.arange(3.) >>> np.array2string(x, formatter={'float_kind':lambda x: "%.2f" % x}) @@ -654,7 +655,7 @@ def array2string(a, max_line_width=None, precision=None, >>> x = np.arange(3) >>> np.array2string(x, formatter={'int':lambda x: hex(x)}) - '[0x0L 0x1L 0x2L]' + '[0x0 0x1 0x2]' """ legacy = kwarg.pop('legacy', None) @@ -1357,7 +1358,7 @@ def dtype_is_implied(dtype): >>> np.core.arrayprint.dtype_is_implied(np.int8) False >>> np.array([1, 2, 3], np.int8) - array([1, 2, 3], dtype=np.int8) + array([1, 2, 3], dtype=int8) """ dtype = np.dtype(dtype) if _format_options['legacy'] == '1.13' and dtype.type == bool_: @@ -1377,6 +1378,7 @@ def dtype_short_repr(dtype): The intent is roughly that the following holds >>> from numpy import * + >>> dt = np.int64([1, 2]).dtype >>> assert eval(dtype_short_repr(dt)) == dt """ if dtype.names is not None: @@ -1480,13 +1482,13 @@ def array_repr(arr, max_line_width=None, precision=None, suppress_small=None): >>> np.array_repr(np.array([1,2])) 'array([1, 2])' >>> np.array_repr(np.ma.array([0.])) - 'MaskedArray([ 0.])' + 'MaskedArray([0.])' >>> np.array_repr(np.array([], np.int32)) 'array([], dtype=int32)' >>> x = np.array([1e-6, 4e-7, 2, 3]) >>> np.array_repr(x, precision=6, suppress_small=True) - 'array([ 0.000001, 0. , 2. , 3. ])' + 'array([0.000001, 0. , 2. , 3. ])' """ return _array_repr_implementation( @@ -1597,8 +1599,8 @@ def set_string_function(f, repr=True): >>> a = np.arange(10) >>> a HA! - What are you going to do now? - >>> print(a) - [0 1 2 3 4 5 6 7 8 9] + >>> _ = a + >>> # [0 1 2 3 4 5 6 7 8 9] We can reset the function to the default: @@ -1616,7 +1618,7 @@ def set_string_function(f, repr=True): >>> x.__str__() 'random' >>> x.__repr__() - 'array([ 0, 1, 2, 3])' + 'array([0, 1, 2, 3])' """ if f is None: -- cgit v1.2.1 From 247cbb8ed8a925349eb1d2b12809493488c4d5c5 Mon Sep 17 00:00:00 2001 From: Mike Lui Date: Thu, 25 Apr 2019 22:11:47 -0400 Subject: DOC: clarify array_{2string,str,repr} defaults Point documentation on default values for array2string, array_str, and array_repr to set_printoptions. set_printoptions uses the same defaulting scheme. --- numpy/core/arrayprint.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'numpy/core/arrayprint.py') diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index 7d8785c32..4d9ec954c 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -163,7 +163,8 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None, - 'str_kind' : sets 'str' and 'numpystr' floatmode : str, optional Controls the interpretation of the `precision` option for - floating-point types. Can take the following values: + floating-point types. Can take the following values + (default maxprec_equal): * 'fixed': Always print exactly `precision` fractional digits, even if this would print more or fewer digits than @@ -532,14 +533,16 @@ def array2string(a, max_line_width=None, precision=None, max_line_width : int, optional The maximum number of columns the string should span. Newline characters splits the string appropriately after array elements. + See `linewidth` in `set_printoptions` for defaults. precision : int or None, optional - Floating point precision. Default is the current printing - precision (usually 8), which can be altered using `set_printoptions`. + Floating point precision. Default is the current printing precision + (usually 8), which can be altered using `set_printoptions`. suppress_small : bool, optional Represent very small numbers as zero. A number is "very small" if it is smaller than the current printing precision. + See `suppress` in `set_printoptions` for defaults. separator : str, optional - Inserted between elements. + Inserted between elements (default ' '). prefix : str, optional suffix: str, optional The length of the prefix and suffix strings are used to respectively @@ -584,17 +587,21 @@ def array2string(a, max_line_width=None, precision=None, threshold : int, optional Total number of array elements which trigger summarization rather than full repr. + See `set_printoptions` for defaults. edgeitems : int, optional Number of array items in summary at beginning and end of each dimension. + See `set_printoptions` for defaults. sign : string, either '-', '+', or ' ', optional Controls printing of the sign of floating-point types. If '+', always print the sign of positive values. If ' ', always prints a space (whitespace character) in the sign position of positive values. If '-', omit the sign character of positive values. + See `set_printoptions` for defaults. floatmode : str, optional Controls the interpretation of the `precision` option for - floating-point types. Can take the following values: + floating-point types. See `set_printoptions` for defaults. + Can take the following values: - 'fixed': Always print exactly `precision` fractional digits, even if this would print more or fewer digits than @@ -1460,6 +1467,7 @@ def array_repr(arr, max_line_width=None, precision=None, suppress_small=None): max_line_width : int, optional The maximum number of columns the string should span. Newline characters split the string appropriately after array elements. + See `linewidth` in `set_printoptions` for defaults. precision : int, optional Floating point precision. Default is the current printing precision (usually 8), which can be altered using `set_printoptions`. @@ -1467,6 +1475,7 @@ def array_repr(arr, max_line_width=None, precision=None, suppress_small=None): Represent very small numbers as zero, default is False. Very small is defined by `precision`, if the precision is 8 then numbers smaller than 5e-9 are represented as zero. + See `suppress` in `set_printoptions` for defaults. Returns ------- @@ -1537,16 +1546,16 @@ def array_str(a, max_line_width=None, precision=None, suppress_small=None): a : ndarray Input array. max_line_width : int, optional - Inserts newlines if text is longer than `max_line_width`. The - default is, indirectly, 75. + Inserts newlines if text is longer than `max_line_width`. + See `linewidth` in `set_printoptions` for defaults. precision : int, optional - Floating point precision. Default is the current printing precision + Floating point precision. Default is the current printing precision (usually 8), which can be altered using `set_printoptions`. suppress_small : bool, optional Represent numbers "very close" to zero as zero; default is False. Very close is defined by precision: if the precision is 8, e.g., numbers smaller (in absolute value) than 5e-9 are represented as - zero. + zero. See `suppress` in `set_printoptions` for defaults. See Also -------- -- cgit v1.2.1 From c9641f84a5fd77841e0d70c062a9457bd9e12e75 Mon Sep 17 00:00:00 2001 From: Mike Lui Date: Thu, 25 Apr 2019 23:36:32 -0400 Subject: DOC: clarify array_{2string,str,repr} defaults Point users to get_printoptions(), which returns the current print defaults. --- numpy/core/arrayprint.py | 55 +++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 26 deletions(-) (limited to 'numpy/core/arrayprint.py') diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index 4d9ec954c..3e8cdf6ab 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -531,18 +531,19 @@ def array2string(a, max_line_width=None, precision=None, a : array_like Input array. max_line_width : int, optional - The maximum number of columns the string should span. Newline - characters splits the string appropriately after array elements. - See `linewidth` in `set_printoptions` for defaults. + Inserts newlines if text is longer than `max_line_width`. + Defaults to ``numpy.get_printoptions()['linewidth']``. precision : int or None, optional - Floating point precision. Default is the current printing precision - (usually 8), which can be altered using `set_printoptions`. + Floating point precision. + Defaults to ``numpy.get_printoptions()['precision']``. suppress_small : bool, optional - Represent very small numbers as zero. A number is "very small" if it - is smaller than the current printing precision. - See `suppress` in `set_printoptions` for defaults. + Represent numbers "very close" to zero as zero; default is False. + Very close is defined by precision: if the precision is 8, e.g., + numbers smaller (in absolute value) than 5e-9 are represented as + zero. + Defaults to ``numpy.get_printoptions()['suppress']``. separator : str, optional - Inserted between elements (default ' '). + Inserted between elements. prefix : str, optional suffix: str, optional The length of the prefix and suffix strings are used to respectively @@ -587,20 +588,21 @@ def array2string(a, max_line_width=None, precision=None, threshold : int, optional Total number of array elements which trigger summarization rather than full repr. - See `set_printoptions` for defaults. + Defaults to ``numpy.get_printoptions()['threshold']``. edgeitems : int, optional Number of array items in summary at beginning and end of each dimension. - See `set_printoptions` for defaults. + Defaults to ``numpy.get_printoptions()['edgeitems']``. sign : string, either '-', '+', or ' ', optional Controls printing of the sign of floating-point types. If '+', always print the sign of positive values. If ' ', always prints a space (whitespace character) in the sign position of positive values. If '-', omit the sign character of positive values. - See `set_printoptions` for defaults. + Defaults to ``numpy.get_printoptions()['sign']``. floatmode : str, optional Controls the interpretation of the `precision` option for - floating-point types. See `set_printoptions` for defaults. + floating-point types. + Defaults to ``numpy.get_printoptions()['floatmode']``. Can take the following values: - 'fixed': Always print exactly `precision` fractional digits, @@ -1465,17 +1467,17 @@ def array_repr(arr, max_line_width=None, precision=None, suppress_small=None): arr : ndarray Input array. max_line_width : int, optional - The maximum number of columns the string should span. Newline - characters split the string appropriately after array elements. - See `linewidth` in `set_printoptions` for defaults. + Inserts newlines if text is longer than `max_line_width`. + Defaults to ``numpy.get_printoptions()['linewidth']``. precision : int, optional - Floating point precision. Default is the current printing precision - (usually 8), which can be altered using `set_printoptions`. + Floating point precision. + Defaults to ``numpy.get_printoptions()['precision']``. suppress_small : bool, optional - Represent very small numbers as zero, default is False. Very small - is defined by `precision`, if the precision is 8 then - numbers smaller than 5e-9 are represented as zero. - See `suppress` in `set_printoptions` for defaults. + Represent numbers "very close" to zero as zero; default is False. + Very close is defined by precision: if the precision is 8, e.g., + numbers smaller (in absolute value) than 5e-9 are represented as + zero. + Defaults to ``numpy.get_printoptions()['suppress']``. Returns ------- @@ -1547,15 +1549,16 @@ def array_str(a, max_line_width=None, precision=None, suppress_small=None): Input array. max_line_width : int, optional Inserts newlines if text is longer than `max_line_width`. - See `linewidth` in `set_printoptions` for defaults. + Defaults to ``numpy.get_printoptions()['linewidth']``. precision : int, optional - Floating point precision. Default is the current printing precision - (usually 8), which can be altered using `set_printoptions`. + Floating point precision. + Defaults to ``numpy.get_printoptions()['precision']``. suppress_small : bool, optional Represent numbers "very close" to zero as zero; default is False. Very close is defined by precision: if the precision is 8, e.g., numbers smaller (in absolute value) than 5e-9 are represented as - zero. See `suppress` in `set_printoptions` for defaults. + zero. + Defaults to ``numpy.get_printoptions()['suppress']``. See Also -------- -- cgit v1.2.1 From 94d6a3759d5b56b7c1c2ba4c327f891aedde2ebc Mon Sep 17 00:00:00 2001 From: MSeifert04 Date: Mon, 1 Jul 2019 21:19:51 +0200 Subject: MAINT: Replace integers in places where booleans are expected --- numpy/core/arrayprint.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'numpy/core/arrayprint.py') diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index 3e8cdf6ab..739ae7711 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -1641,5 +1641,5 @@ def set_string_function(f, repr=True): else: return multiarray.set_string_function(f, repr) -set_string_function(_default_array_str, 0) -set_string_function(_default_array_repr, 1) +set_string_function(_default_array_str, False) +set_string_function(_default_array_repr, True) -- cgit v1.2.1 From b93b0b879b1c6c35412e599786221dff5730bac0 Mon Sep 17 00:00:00 2001 From: Samesh Lakhotia <43701530+sameshl@users.noreply.github.com> Date: Wed, 10 Jul 2019 00:36:35 +0530 Subject: DOC:Update the description of set_printoptions in quickstart doc #13900 (#13948) Quickstart guide recommends np.set_printoptions(threshold=np.nan) but that fails. So changed that to np.set_printoptions(threshold=sys.maxsize) Closes #13900 * DOC:Update the docstring of set_printoptions Add preferred max value for threshold is sys.maxsize * Revert "DOC:Update the docstring of set_printoptions" This reverts commit 9641121fcb84b9ad5edb188a4a5b4ac9361fbf0b. * DOC:Update the docstring of set_printoptions Add preferred max value for threshold is sys.maxsize * DOC:Update the docstring of set_printoptions Better pharasing in docstring. * DOC:Update the description of set_printoptions in quickstart doc #13900 Fix indentation --- numpy/core/arrayprint.py | 1 + 1 file changed, 1 insertion(+) (limited to 'numpy/core/arrayprint.py') diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index 739ae7711..108364824 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -114,6 +114,7 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None, threshold : int, optional Total number of array elements which trigger summarization rather than full repr (default 1000). + To always use the full repr without summarization, pass `sys.maxsize`. edgeitems : int, optional Number of array items in summary at beginning and end of each dimension (default 3). -- cgit v1.2.1 From feee90d9e14490daa2439eb37c8511555d073f92 Mon Sep 17 00:00:00 2001 From: Kexuan Sun Date: Thu, 25 Jul 2019 11:26:06 -0700 Subject: MAINT: Change the type of error raised in set_printoptions (gh-13899) Previously an incorrect ``threshold`` raised ``ValueError``; it now raises ``TypeError`` for non-numeric types and ``ValueError`` for ``nan`` values. --- numpy/core/arrayprint.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'numpy/core/arrayprint.py') diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index 108364824..ecd05d3ac 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -89,8 +89,10 @@ def _make_options_dict(precision=None, threshold=None, edgeitems=None, "`False`", stacklevel=3) if threshold is not None: # forbid the bad threshold arg suggested by stack overflow, gh-12351 - if not isinstance(threshold, numbers.Number) or np.isnan(threshold): - raise ValueError("threshold must be numeric and non-NAN, try " + if not isinstance(threshold, numbers.Number): + raise TypeError("threshold must be numeric") + if np.isnan(threshold): + raise ValueError("threshold must be non-NAN, try " "sys.maxsize for untruncated representation") return options -- cgit v1.2.1