diff options
| -rw-r--r-- | doc/release/1.14.0-notes.rst | 12 | ||||
| -rw-r--r-- | doc/release/1.15.0-notes.rst | 10 | ||||
| -rw-r--r-- | numpy/core/arrayprint.py | 22 | ||||
| -rw-r--r-- | numpy/core/numeric.py | 12 | ||||
| -rw-r--r-- | numpy/core/src/umath/ufunc_object.c | 69 | ||||
| -rw-r--r-- | numpy/core/tests/test_arrayprint.py | 11 | ||||
| -rw-r--r-- | numpy/core/tests/test_multiarray.py | 5 | ||||
| -rw-r--r-- | numpy/lib/function_base.py | 29 | ||||
| -rw-r--r-- | numpy/lib/nanfunctions.py | 33 | ||||
| -rw-r--r-- | numpy/lib/npyio.py | 9 | ||||
| -rw-r--r-- | numpy/ma/core.py | 18 |
11 files changed, 136 insertions, 94 deletions
diff --git a/doc/release/1.14.0-notes.rst b/doc/release/1.14.0-notes.rst index 793b20c6d..2aaf0246a 100644 --- a/doc/release/1.14.0-notes.rst +++ b/doc/release/1.14.0-notes.rst @@ -265,14 +265,14 @@ In summary, the major changes are: * Float arrays printed in scientific notation no longer use fixed-precision, and now instead show the shortest unique representation. * The ``str`` of floating-point scalars is no longer truncated in python2. - + * For other data types: * Non-finite complex scalars print like ``nanj`` instead of ``nan*j``. * ``NaT`` values in datetime arrays are now properly aligned. * Arrays and scalars of ``np.void`` datatype are now printed using hex notation. - + * For line-wrapping: * The "dtype" part of ndarray reprs will now be printed on the next line @@ -280,11 +280,11 @@ In summary, the major changes are: * The ``linewidth`` format option is now always respected. The `repr` or `str` of an array will never exceed this, unless a single element is too wide. - * All but the last line of array strings will contain the same number of - elements. * The last line of an array string will never have more elements than earlier lines. - + * An extra space is no longer inserted on the first line if the elements are + too wide. + * For summarization (the use of ``...`` to shorten long arrays): * A trailing comma is no longer inserted for ``str``. @@ -294,7 +294,7 @@ In summary, the major changes are: order to summarize any but the last axis, newlines are now appended to that line to match its leading newlines and a trailing space character is removed. - + * ``MaskedArray`` arrays now separate printed elements with commas, always print the dtype, and correctly wrap the elements of long arrays to multiple lines. If there is more than 1 dimension, the array attributes are now diff --git a/doc/release/1.15.0-notes.rst b/doc/release/1.15.0-notes.rst index 15d34e9fc..283c992ea 100644 --- a/doc/release/1.15.0-notes.rst +++ b/doc/release/1.15.0-notes.rst @@ -14,6 +14,16 @@ New functions Deprecations ============ +* Aliases of builtin `pickle` functions are deprecated, in favor of their + unaliased ``pickle.<func>`` names: + + * `np.loads` + * `np.core.numeric.load` + * `np.core.numeric.loads` + * `np.ma.loads`, `np.ma.dumps` + * `np.ma.load`, `np.ma.dump` - these functions already failed on python 3, + when called with a string. + Future Changes ============== diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index eaec91259..238e1782f 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -622,8 +622,14 @@ def array2string(a, max_line_width=None, precision=None, return _array2string(a, options, separator, prefix) -def _extendLine(s, line, word, line_width, next_line_prefix): - if len(line) + len(word) > line_width: +def _extendLine(s, line, word, line_width, next_line_prefix, legacy): + needs_wrap = len(line) + len(word) > line_width + if legacy != '1.13': + s# don't wrap lines if it won't help + if len(line) <= len(next_line_prefix): + needs_wrap = False + + if needs_wrap: s += line.rstrip() + "\n" line = next_line_prefix line += word @@ -682,11 +688,13 @@ def _formatArray(a, format_function, line_width, next_line_prefix, line = hanging_indent for i in range(leading_items): word = recurser(index + (i,), next_hanging_indent, next_width) - s, line = _extendLine(s, line, word, elem_width, hanging_indent) + s, line = _extendLine( + s, line, word, elem_width, hanging_indent, legacy) line += separator if show_summary: - s, line = _extendLine(s, line, summary_insert, elem_width, hanging_indent) + s, line = _extendLine( + s, line, summary_insert, elem_width, hanging_indent, legacy) if legacy == '1.13': line += ", " else: @@ -694,14 +702,16 @@ def _formatArray(a, format_function, line_width, next_line_prefix, for i in range(trailing_items, 1, -1): word = recurser(index + (-i,), next_hanging_indent, next_width) - s, line = _extendLine(s, line, word, elem_width, hanging_indent) + s, line = _extendLine( + s, line, word, elem_width, hanging_indent, legacy) line += separator if legacy == '1.13': # width of the seperator is not considered on 1.13 elem_width = curr_width word = recurser(index + (-1,), next_hanging_indent, next_width) - s, line = _extendLine(s, line, word, elem_width, hanging_indent) + s, line = _extendLine( + s, line, word, elem_width, hanging_indent, legacy) s += line diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index ac64b0537..431467a5a 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -42,7 +42,13 @@ else: import cPickle as pickle import __builtin__ as builtins -loads = pickle.loads + +def loads(*args, **kwargs): + # NumPy 1.15.0, 2017-12-10 + warnings.warn( + "np.core.numeric.loads is deprecated, use pickle.loads instead", + DeprecationWarning, stacklevel=2) + return pickle.loads(*args, **kwargs) __all__ = [ @@ -2134,6 +2140,10 @@ def load(file): load, save """ + # NumPy 1.15.0, 2017-12-10 + warnings.warn( + "np.core.numeric.load is deprecated, use pickle.load instead", + DeprecationWarning, stacklevel=2) if isinstance(file, type("")): file = open(file, "rb") return pickle.load(file) diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 35c7724b1..257067023 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -1751,7 +1751,7 @@ make_arr_prep_args(npy_intp nin, PyObject *args, PyObject *kwds) /* * Validate the core dimensions of all the operands, and collect all of * the labelled core dimensions into 'core_dim_sizes'. - * + * * Returns 0 on success, and -1 on failure * * The behavior has been changed in NumPy 1.10.0, and the following @@ -3656,7 +3656,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, int i, naxes=0, ndim; int axes[NPY_MAXDIMS]; PyObject *axes_in = NULL; - PyArrayObject *mp, *ret = NULL; + PyArrayObject *mp = NULL, *ret = NULL; PyObject *op, *res = NULL; PyObject *obj_ind, *context; PyArrayObject *indices = NULL; @@ -3707,24 +3707,22 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, PyDict_SetItem(kwds, npy_um_str_out, out_obj); } } - + if (operation == UFUNC_REDUCEAT) { PyArray_Descr *indtype; indtype = PyArray_DescrFromType(NPY_INTP); if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO&O&:reduceat", reduceat_kwlist, - &op, - &obj_ind, - &axes_in, - PyArray_DescrConverter2, &otype, - PyArray_OutputConverter, &out)) { - Py_XDECREF(otype); - return NULL; + &op, + &obj_ind, + &axes_in, + PyArray_DescrConverter2, &otype, + PyArray_OutputConverter, &out)) { + goto fail; } indices = (PyArrayObject *)PyArray_FromAny(obj_ind, indtype, 1, 1, NPY_ARRAY_CARRAY, NULL); if (indices == NULL) { - Py_XDECREF(otype); - return NULL; + goto fail; } } else if (operation == UFUNC_ACCUMULATE) { @@ -3734,8 +3732,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, &axes_in, PyArray_DescrConverter2, &otype, PyArray_OutputConverter, &out)) { - Py_XDECREF(otype); - return NULL; + goto fail; } } else { @@ -3746,8 +3743,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, PyArray_DescrConverter2, &otype, PyArray_OutputConverter, &out, &keepdims)) { - Py_XDECREF(otype); - return NULL; + goto fail; } } /* Ensure input is an array */ @@ -3760,7 +3756,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, mp = (PyArrayObject *)PyArray_FromAny(op, NULL, 0, 0, 0, context); Py_XDECREF(context); if (mp == NULL) { - return NULL; + goto fail; } ndim = PyArray_NDIM(mp); @@ -3771,9 +3767,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, PyErr_Format(PyExc_TypeError, "cannot perform %s with flexible type", _reduce_type[operation]); - Py_XDECREF(otype); - Py_DECREF(mp); - return NULL; + goto fail; } /* Convert the 'axis' parameter into a list of axes */ @@ -3793,22 +3787,16 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, if (naxes < 0 || naxes > NPY_MAXDIMS) { PyErr_SetString(PyExc_ValueError, "too many values for 'axis'"); - Py_XDECREF(otype); - Py_DECREF(mp); - return NULL; + goto fail; } for (i = 0; i < naxes; ++i) { PyObject *tmp = PyTuple_GET_ITEM(axes_in, i); int axis = PyArray_PyIntAsInt(tmp); if (error_converting(axis)) { - Py_XDECREF(otype); - Py_DECREF(mp); - return NULL; + goto fail; } if (check_and_adjust_axis(&axis, ndim) < 0) { - Py_XDECREF(otype); - Py_DECREF(mp); - return NULL; + goto fail; } axes[i] = (int)axis; } @@ -3818,16 +3806,14 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, int axis = PyArray_PyIntAsInt(axes_in); /* TODO: PyNumber_Index would be good to use here */ if (error_converting(axis)) { - Py_XDECREF(otype); - Py_DECREF(mp); - return NULL; + goto fail; } /* Special case letting axis={0 or -1} slip through for scalars */ if (ndim == 0 && (axis == 0 || axis == -1)) { axis = 0; } else if (check_and_adjust_axis(&axis, ndim) < 0) { - return NULL; + goto fail; } axes[0] = (int)axis; naxes = 1; @@ -3847,9 +3833,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, (naxes == 0 || (naxes == 1 && axes[0] == 0)))) { PyErr_Format(PyExc_TypeError, "cannot %s on a scalar", _reduce_type[operation]); - Py_XDECREF(otype); - Py_DECREF(mp); - return NULL; + goto fail; } } @@ -3895,9 +3879,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, if (naxes != 1) { PyErr_SetString(PyExc_ValueError, "accumulate does not allow multiple axes"); - Py_XDECREF(otype); - Py_DECREF(mp); - return NULL; + goto fail; } ret = (PyArrayObject *)PyUFunc_Accumulate(ufunc, mp, out, axes[0], otype->type_num); @@ -3906,9 +3888,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, if (naxes != 1) { PyErr_SetString(PyExc_ValueError, "reduceat does not allow multiple axes"); - Py_XDECREF(otype); - Py_DECREF(mp); - return NULL; + goto fail; } ret = (PyArrayObject *)PyUFunc_Reduceat(ufunc, mp, indices, out, axes[0], otype->type_num); @@ -3941,6 +3921,11 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, } } return PyArray_Return(ret); + +fail: + Py_XDECREF(otype); + Py_XDECREF(mp); + return NULL; } /* diff --git a/numpy/core/tests/test_arrayprint.py b/numpy/core/tests/test_arrayprint.py index 950004508..d491d53aa 100644 --- a/numpy/core/tests/test_arrayprint.py +++ b/numpy/core/tests/test_arrayprint.py @@ -289,6 +289,17 @@ class TestArray2String(object): ' 11\n' ' 11]]]') + def test_wide_element(self): + a = np.array(['xxxxx']) + assert_equal( + np.array2string(a, max_line_width=5), + "['xxxxx']" + ) + assert_equal( + np.array2string(a, max_line_width=5, legacy='1.13'), + "[ 'xxxxx']" + ) + class TestPrintOptions(object): """Test getting and setting global print options.""" diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 690828cc8..3bddfe2ae 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -3434,10 +3434,11 @@ class TestPickling(object): assert_equal(a, pickle.loads(a.dumps()), err_msg="%r" % a) def _loads(self, obj): + import pickle if sys.version_info[0] >= 3: - return np.loads(obj, encoding='latin1') + return pickle.loads(obj, encoding='latin1') else: - return np.loads(obj) + return pickle.loads(obj) # version 0 pickles, using protocol=2 to pickle # version 0 doesn't have a version field diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index c9a23350d..fb37801b9 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -4188,27 +4188,24 @@ def percentile(a, q, axis=None, out=None, ---------- a : array_like Input array or object that can be converted to an array. - q : float in range of [0,100] (or sequence of floats) - Percentile to compute, which must be between 0 and 100 inclusive. - axis : {int, sequence of int, None}, optional + q : array_like of float + Percentile or sequence of percentiles to compute, which must be between + 0 and 100 inclusive. + axis : {int, tuple of int, None}, optional Axis or axes along which the percentiles are computed. The default is to compute the percentile(s) along a flattened - version of the array. A sequence of axes is supported since - version 1.9.0. + version of the array. + + .. versionchanged:: 1.9.0 + A tuple of axes is supported out : ndarray, optional Alternative output array in which to place the result. It must have the same shape and buffer length as the expected output, but the type (of the output) will be cast if necessary. overwrite_input : bool, optional - If True, then allow use of memory of input array `a` - calculations. The input array will be modified by the call to - `percentile`. This will save memory when you do not need to - preserve the contents of the input array. In this case you - should not make any assumptions about the contents of the input - `a` after this function completes -- treat it as undefined. - Default is False. If `a` is not already an array, this parameter - will have no effect as `a` will be converted to an array - internally regardless of the value of this parameter. + If True, then allow the input array `a` to be modified by intermediate + calculations, to save memory. In this case, the contents of the input + `a` after this function completes is undefined. interpolation : {'linear', 'lower', 'higher', 'midpoint', 'nearest'} This optional parameter specifies the interpolation method to use when the desired quantile lies between two data points @@ -4243,7 +4240,9 @@ def percentile(a, q, axis=None, out=None, See Also -------- - mean, median, nanpercentile + mean + median : equivalent to ``percentile(..., 50)`` + nanpercentile Notes ----- diff --git a/numpy/lib/nanfunctions.py b/numpy/lib/nanfunctions.py index b3f3bfc69..ca8c10031 100644 --- a/numpy/lib/nanfunctions.py +++ b/numpy/lib/nanfunctions.py @@ -1039,35 +1039,28 @@ def nanpercentile(a, q, axis=None, out=None, overwrite_input=False, ---------- a : array_like Input array or object that can be converted to an array. - q : float in range of [0,100] (or sequence of floats) - Percentile to compute, which must be between 0 and 100 - inclusive. - axis : {int, sequence of int, None}, optional + q : array_like of float + Percentile or sequence of percentiles to compute, which must be between + 0 and 100 inclusive. + axis : {int, tuple of int, None}, optional Axis or axes along which the percentiles are computed. The default is to compute the percentile(s) along a flattened - version of the array. A sequence of axes is supported since - version 1.9.0. + version of the array. out : ndarray, optional Alternative output array in which to place the result. It must have the same shape and buffer length as the expected output, but the type (of the output) will be cast if necessary. overwrite_input : bool, optional - If True, then allow use of memory of input array `a` for - calculations. The input array will be modified by the call to - `percentile`. This will save memory when you do not need to - preserve the contents of the input array. In this case you - should not make any assumptions about the contents of the input - `a` after this function completes -- treat it as undefined. - Default is False. If `a` is not already an array, this parameter - will have no effect as `a` will be converted to an array - internally regardless of the value of this parameter. + If True, then allow the input array `a` to be modified by intermediate + calculations, to save memory. In this case, the contents of the input + `a` after this function completes is undefined. interpolation : {'linear', 'lower', 'higher', 'midpoint', 'nearest'} This optional parameter specifies the interpolation method to use when the desired quantile lies between two data points ``i < j``: - * linear: ``i + (j - i) * fraction``, where ``fraction`` is - the fractional part of the index surrounded by ``i`` and - ``j``. + * linear: ``i + (j - i) * fraction``, where ``fraction`` + is the fractional part of the index surrounded by ``i`` + and ``j``. * lower: ``i``. * higher: ``j``. * nearest: ``i`` or ``j``, whichever is nearest. @@ -1097,7 +1090,9 @@ def nanpercentile(a, q, axis=None, out=None, overwrite_input=False, See Also -------- - nanmean, nanmedian, percentile, median, mean + nanmean + nanmedian : equivalent to ``nanpercentile(..., 50)`` + percentile, median, mean Notes ----- diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index e4d827334..7b51cb9c7 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -30,7 +30,14 @@ else: import cPickle as pickle from future_builtins import map -loads = pickle.loads + +def loads(*args, **kwargs): + # NumPy 1.15.0, 2017-12-10 + warnings.warn( + "np.loads is deprecated, use pickle.loads instead", + DeprecationWarning, stacklevel=2) + return pickle.loads(*args, **kwargs) + __all__ = [ 'savetxt', 'loadtxt', 'genfromtxt', 'ndfromtxt', 'mafromtxt', diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 407869362..dad675600 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -7859,6 +7859,16 @@ def asanyarray(a, dtype=None): ############################################################################## # Pickling # ############################################################################## + +def _pickle_warn(method): + # NumPy 1.15.0, 2017-12-10 + warnings.warn( + "np.ma.{method} is deprecated, use pickle.{method} instead" + .format(method), + DeprecationWarning, + stacklevel=3) + + def dump(a, F): """ Pickle a masked array to a file. @@ -7873,6 +7883,7 @@ def dump(a, F): The file to pickle `a` to. If a string, the full path to the file. """ + _pickle_warn('dump') if not hasattr(F, 'readline'): with open(F, 'w') as F: pickle.dump(a, F) @@ -7893,6 +7904,7 @@ def dumps(a): returned. """ + _pickle_warn('dumps') return pickle.dumps(a) @@ -7916,11 +7928,12 @@ def load(F): the NumPy binary .npy format. """ + _pickle_warn('load') if not hasattr(F, 'readline'): with open(F, 'r') as F: - pickle.load(F) + return pickle.load(F) else: - pickle.load(F) + return pickle.load(F) def loads(strg): @@ -7939,6 +7952,7 @@ def loads(strg): dumps : Return a string corresponding to the pickling of a masked array. """ + _pickle_warn('loads') return pickle.loads(strg) |
