summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2018-11-14 18:08:30 -0600
committerGitHub <noreply@github.com>2018-11-14 18:08:30 -0600
commitd1e0a43ff910e15034997b9b646b38014382fe6e (patch)
tree14970cfe5421541fdd3fe0f4e77a23101e39abb1 /numpy
parent13a69b5e0fc409928dc81cdce405043e7c71cd0a (diff)
parenta0833836e07aeabde98eb3cfa1a583b3dd4a9439 (diff)
downloadnumpy-d1e0a43ff910e15034997b9b646b38014382fe6e.tar.gz
Merge pull request #12382 from shoyer/finish-set-module
ENH: set correct __module__ for objects in numpy's public API
Diffstat (limited to 'numpy')
-rw-r--r--numpy/_globals.py11
-rw-r--r--numpy/core/_add_newdocs.py178
-rw-r--r--numpy/core/_internal.py3
-rw-r--r--numpy/core/arrayprint.py10
-rw-r--r--numpy/core/defchararray.py2
-rw-r--r--numpy/core/function_base.py4
-rw-r--r--numpy/core/getlimits.py3
-rw-r--r--numpy/core/machar.py2
-rw-r--r--numpy/core/memmap.py3
-rw-r--r--numpy/core/multiarray.py490
-rw-r--r--numpy/core/numeric.py23
-rw-r--r--numpy/core/numerictypes.py14
-rw-r--r--numpy/core/overrides.py6
-rw-r--r--numpy/core/records.py3
-rw-r--r--numpy/core/src/multiarray/compiled_base.c20
-rw-r--r--numpy/core/tests/test_overrides.py3
-rw-r--r--numpy/fft/helper.py5
-rw-r--r--numpy/lib/_datasource.py7
-rw-r--r--numpy/lib/arraysetops.py5
-rw-r--r--numpy/lib/function_base.py9
-rw-r--r--numpy/lib/histograms.py6
-rw-r--r--numpy/lib/index_tricks.py6
-rw-r--r--numpy/lib/npyio.py14
-rw-r--r--numpy/lib/polynomial.py3
-rw-r--r--numpy/lib/twodim_base.py6
-rw-r--r--numpy/lib/type_check.py13
-rw-r--r--numpy/lib/utils.py5
-rw-r--r--numpy/linalg/linalg.py5
-rw-r--r--numpy/matrixlib/defmatrix.py6
-rw-r--r--numpy/tests/test_public_api.py77
30 files changed, 727 insertions, 215 deletions
diff --git a/numpy/_globals.py b/numpy/_globals.py
index 9a7b458f1..f5c0761b5 100644
--- a/numpy/_globals.py
+++ b/numpy/_globals.py
@@ -17,7 +17,6 @@ motivated this module.
"""
from __future__ import division, absolute_import, print_function
-
__ALL__ = [
'ModuleDeprecationWarning', 'VisibleDeprecationWarning', '_NoValue'
]
@@ -39,7 +38,9 @@ class ModuleDeprecationWarning(DeprecationWarning):
nose tester will let pass without making tests fail.
"""
- pass
+
+
+ModuleDeprecationWarning.__module__ = 'numpy'
class VisibleDeprecationWarning(UserWarning):
@@ -50,7 +51,10 @@ class VisibleDeprecationWarning(UserWarning):
the usage is most likely a user bug.
"""
- pass
+
+
+VisibleDeprecationWarning.__module__ = 'numpy'
+
class _NoValueType(object):
"""Special keyword value.
@@ -73,4 +77,5 @@ class _NoValueType(object):
def __repr__(self):
return "<no value>"
+
_NoValue = _NoValueType()
diff --git a/numpy/core/_add_newdocs.py b/numpy/core/_add_newdocs.py
index 12ef9d65a..bfb832dfc 100644
--- a/numpy/core/_add_newdocs.py
+++ b/numpy/core/_add_newdocs.py
@@ -4589,184 +4589,6 @@ add_newdoc('numpy.core.umath', 'seterrobj',
#
##############################################################################
-add_newdoc('numpy.core.multiarray', 'bincount',
- """
- bincount(x, weights=None, minlength=0)
-
- Count number of occurrences of each value in array of non-negative ints.
-
- The number of bins (of size 1) is one larger than the largest value in
- `x`. If `minlength` is specified, there will be at least this number
- of bins in the output array (though it will be longer if necessary,
- depending on the contents of `x`).
- Each bin gives the number of occurrences of its index value in `x`.
- If `weights` is specified the input array is weighted by it, i.e. if a
- value ``n`` is found at position ``i``, ``out[n] += weight[i]`` instead
- of ``out[n] += 1``.
-
- Parameters
- ----------
- x : array_like, 1 dimension, nonnegative ints
- Input array.
- weights : array_like, optional
- Weights, array of the same shape as `x`.
- minlength : int, optional
- A minimum number of bins for the output array.
-
- .. versionadded:: 1.6.0
-
- Returns
- -------
- out : ndarray of ints
- The result of binning the input array.
- The length of `out` is equal to ``np.amax(x)+1``.
-
- Raises
- ------
- ValueError
- If the input is not 1-dimensional, or contains elements with negative
- values, or if `minlength` is negative.
- TypeError
- If the type of the input is float or complex.
-
- See Also
- --------
- histogram, digitize, unique
-
- Examples
- --------
- >>> np.bincount(np.arange(5))
- array([1, 1, 1, 1, 1])
- >>> np.bincount(np.array([0, 1, 1, 3, 2, 1, 7]))
- array([1, 3, 1, 1, 0, 0, 0, 1])
-
- >>> x = np.array([0, 1, 1, 3, 2, 1, 7, 23])
- >>> np.bincount(x).size == np.amax(x)+1
- True
-
- The input array needs to be of integer dtype, otherwise a
- TypeError is raised:
-
- >>> np.bincount(np.arange(5, dtype=float))
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- TypeError: array cannot be safely cast to required type
-
- A possible use of ``bincount`` is to perform sums over
- variable-size chunks of an array, using the ``weights`` keyword.
-
- >>> w = np.array([0.3, 0.5, 0.2, 0.7, 1., -0.6]) # weights
- >>> x = np.array([0, 1, 1, 2, 2, 2])
- >>> np.bincount(x, weights=w)
- array([ 0.3, 0.7, 1.1])
-
- """)
-
-add_newdoc('numpy.core.multiarray', 'ravel_multi_index',
- """
- ravel_multi_index(multi_index, dims, mode='raise', order='C')
-
- Converts a tuple of index arrays into an array of flat
- indices, applying boundary modes to the multi-index.
-
- Parameters
- ----------
- multi_index : tuple of array_like
- A tuple of integer arrays, one array for each dimension.
- dims : tuple of ints
- The shape of array into which the indices from ``multi_index`` apply.
- mode : {'raise', 'wrap', 'clip'}, optional
- Specifies how out-of-bounds indices are handled. Can specify
- either one mode or a tuple of modes, one mode per index.
-
- * 'raise' -- raise an error (default)
- * 'wrap' -- wrap around
- * 'clip' -- clip to the range
-
- In 'clip' mode, a negative index which would normally
- wrap will clip to 0 instead.
- order : {'C', 'F'}, optional
- Determines whether the multi-index should be viewed as
- indexing in row-major (C-style) or column-major
- (Fortran-style) order.
-
- Returns
- -------
- raveled_indices : ndarray
- An array of indices into the flattened version of an array
- of dimensions ``dims``.
-
- See Also
- --------
- unravel_index
-
- Notes
- -----
- .. versionadded:: 1.6.0
-
- Examples
- --------
- >>> arr = np.array([[3,6,6],[4,5,1]])
- >>> np.ravel_multi_index(arr, (7,6))
- array([22, 41, 37])
- >>> np.ravel_multi_index(arr, (7,6), order='F')
- array([31, 41, 13])
- >>> np.ravel_multi_index(arr, (4,6), mode='clip')
- array([22, 23, 19])
- >>> np.ravel_multi_index(arr, (4,4), mode=('clip','wrap'))
- array([12, 13, 13])
-
- >>> np.ravel_multi_index((3,1,4,1), (6,7,8,9))
- 1621
- """)
-
-add_newdoc('numpy.core.multiarray', 'unravel_index',
- """
- unravel_index(indices, shape, order='C')
-
- Converts a flat index or array of flat indices into a tuple
- of coordinate arrays.
-
- Parameters
- ----------
- indices : array_like
- An integer array whose elements are indices into the flattened
- version of an array of dimensions ``shape``. Before version 1.6.0,
- this function accepted just one index value.
- shape : tuple of ints
- The shape of the array to use for unraveling ``indices``.
-
- .. versionchanged:: 1.16.0
- Renamed from ``dims`` to ``shape``.
-
- order : {'C', 'F'}, optional
- Determines whether the indices should be viewed as indexing in
- row-major (C-style) or column-major (Fortran-style) order.
-
- .. versionadded:: 1.6.0
-
- Returns
- -------
- unraveled_coords : tuple of ndarray
- Each array in the tuple has the same shape as the ``indices``
- array.
-
- See Also
- --------
- ravel_multi_index
-
- Examples
- --------
- >>> np.unravel_index([22, 41, 37], (7,6))
- (array([3, 6, 6]), array([4, 5, 1]))
- >>> np.unravel_index([31, 41, 13], (7,6), order='F')
- (array([3, 6, 6]), array([4, 5, 1]))
-
- >>> np.unravel_index(1621, (6,7,8,9))
- (3, 1, 4, 1)
-
- """)
-
add_newdoc('numpy.core.multiarray', 'add_docstring',
"""
add_docstring(obj, docstring)
diff --git a/numpy/core/_internal.py b/numpy/core/_internal.py
index a1a166b2c..59da60253 100644
--- a/numpy/core/_internal.py
+++ b/numpy/core/_internal.py
@@ -10,6 +10,7 @@ import re
import sys
from numpy.compat import unicode
+from numpy.core.overrides import set_module
from .multiarray import dtype, array, ndarray
try:
import ctypes
@@ -729,9 +730,11 @@ def _lcm(a, b):
return a // _gcd(a, b) * b
# Exception used in shares_memory()
+@set_module('numpy')
class TooHardError(RuntimeError):
pass
+@set_module('numpy')
class AxisError(ValueError, IndexError):
""" Axis supplied was invalid. """
def __init__(self, axis, ndim=None, msg_prefix=None):
diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py
index b578fab54..075d75340 100644
--- a/numpy/core/arrayprint.py
+++ b/numpy/core/arrayprint.py
@@ -48,7 +48,7 @@ from .fromnumeric import ravel, any
from .numeric import concatenate, asarray, errstate
from .numerictypes import (longlong, intc, int_, float_, complex_, bool_,
flexible)
-from .overrides import array_function_dispatch
+from .overrides import array_function_dispatch, set_module
import warnings
import contextlib
@@ -89,6 +89,8 @@ def _make_options_dict(precision=None, threshold=None, edgeitems=None,
return options
+
+@set_module('numpy')
def set_printoptions(precision=None, threshold=None, edgeitems=None,
linewidth=None, suppress=None, nanstr=None, infstr=None,
formatter=None, sign=None, floatmode=None, **kwarg):
@@ -250,6 +252,7 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None,
set_legacy_print_mode(0)
+@set_module('numpy')
def get_printoptions():
"""
Return the current print options.
@@ -279,6 +282,7 @@ def get_printoptions():
return _format_options.copy()
+@set_module('numpy')
@contextlib.contextmanager
def printoptions(*args, **kwargs):
"""Context manager for setting print options.
@@ -976,6 +980,8 @@ class LongFloatFormat(FloatingFormat):
DeprecationWarning, stacklevel=2)
super(LongFloatFormat, self).__init__(*args, **kwargs)
+
+@set_module('numpy')
def format_float_scientific(x, precision=None, unique=True, trim='k',
sign=False, pad_left=None, exp_digits=None):
"""
@@ -1043,6 +1049,8 @@ def format_float_scientific(x, precision=None, unique=True, trim='k',
trim=trim, sign=sign, pad_left=pad_left,
exp_digits=exp_digits)
+
+@set_module('numpy')
def format_float_positional(x, precision=None, unique=True,
fractional=True, trim='k', sign=False,
pad_left=None, pad_right=None):
diff --git a/numpy/core/defchararray.py b/numpy/core/defchararray.py
index e86086012..12ba3f02e 100644
--- a/numpy/core/defchararray.py
+++ b/numpy/core/defchararray.py
@@ -23,6 +23,7 @@ from .numerictypes import string_, unicode_, integer, object_, bool_, character
from .numeric import ndarray, compare_chararrays
from .numeric import array as narray
from numpy.core.multiarray import _vec_string
+from numpy.core.overrides import set_module
from numpy.core import overrides
from numpy.compat import asbytes, long
import numpy
@@ -1820,6 +1821,7 @@ def isdecimal(a):
return _vec_string(a, bool_, 'isdecimal')
+@set_module('numpy')
class chararray(ndarray):
"""
chararray(shape, itemsize=1, unicode=False, buffer=None, offset=0,
diff --git a/numpy/core/function_base.py b/numpy/core/function_base.py
index 799b1418d..b3dd313cf 100644
--- a/numpy/core/function_base.py
+++ b/numpy/core/function_base.py
@@ -7,6 +7,7 @@ from . import numeric as _nx
from .numeric import (result_type, NaN, shares_memory, MAY_SHARE_BOUNDS,
TooHardError,asanyarray)
from numpy.core.multiarray import add_docstring
+from numpy.core.overrides import set_module
__all__ = ['logspace', 'linspace', 'geomspace']
@@ -23,6 +24,7 @@ def _index_deprecate(i, stacklevel=2):
return i
+@set_module('numpy')
def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None):
"""
Return evenly spaced numbers over a specified interval.
@@ -154,6 +156,7 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None):
return y.astype(dtype, copy=False)
+@set_module('numpy')
def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None):
"""
Return numbers spaced evenly on a log scale.
@@ -238,6 +241,7 @@ def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None):
return _nx.power(base, y).astype(dtype)
+@set_module('numpy')
def geomspace(start, stop, num=50, endpoint=True, dtype=None):
"""
Return numbers spaced evenly on a log scale (a geometric progression).
diff --git a/numpy/core/getlimits.py b/numpy/core/getlimits.py
index 389f16ff5..544b8b35f 100644
--- a/numpy/core/getlimits.py
+++ b/numpy/core/getlimits.py
@@ -8,6 +8,7 @@ __all__ = ['finfo', 'iinfo']
import warnings
from .machar import MachAr
+from .overrides import set_module
from . import numeric
from . import numerictypes as ntypes
from .numeric import array, inf
@@ -289,6 +290,7 @@ def _discovered_machar(ftype):
params['title'])
+@set_module('numpy')
class finfo(object):
"""
finfo(dtype)
@@ -439,6 +441,7 @@ class finfo(object):
" max=%(_str_max)s, dtype=%(dtype)s)") % d)
+@set_module('numpy')
class iinfo(object):
"""
iinfo(type)
diff --git a/numpy/core/machar.py b/numpy/core/machar.py
index 7578544fe..91fb4eda8 100644
--- a/numpy/core/machar.py
+++ b/numpy/core/machar.py
@@ -11,9 +11,11 @@ __all__ = ['MachAr']
from numpy.core.fromnumeric import any
from numpy.core.numeric import errstate
+from numpy.core.overrides import set_module
# Need to speed this up...especially for longfloat
+@set_module('numpy')
class MachAr(object):
"""
Diagnosing machine parameters.
diff --git a/numpy/core/memmap.py b/numpy/core/memmap.py
index f5cc68bb9..82bc4707c 100644
--- a/numpy/core/memmap.py
+++ b/numpy/core/memmap.py
@@ -5,6 +5,7 @@ from .numeric import uint8, ndarray, dtype
from numpy.compat import (
long, basestring, os_fspath, contextlib_nullcontext, is_pathlib_path
)
+from numpy.core.overrides import set_module
__all__ = ['memmap']
@@ -19,6 +20,8 @@ mode_equivalents = {
"write":"w+"
}
+
+@set_module('numpy')
class memmap(ndarray):
"""Create a memory-map to an array stored in a *binary* file on disk.
diff --git a/numpy/core/multiarray.py b/numpy/core/multiarray.py
index 25debd2f8..78963b0aa 100644
--- a/numpy/core/multiarray.py
+++ b/numpy/core/multiarray.py
@@ -7,6 +7,7 @@ by importing from the extension module.
"""
import functools
+import warnings
from . import overrides
from . import _multiarray_umath
@@ -39,6 +40,26 @@ __all__ = [
'tracemalloc_domain', 'typeinfo', 'unpackbits', 'unravel_index', 'vdot',
'where', 'zeros']
+
+arange.__module__ = 'numpy'
+array.__module__ = 'numpy'
+datetime_data.__module__ = 'numpy'
+empty.__module__ = 'numpy'
+frombuffer.__module__ = 'numpy'
+fromfile.__module__ = 'numpy'
+fromiter.__module__ = 'numpy'
+frompyfunc.__module__ = 'numpy'
+fromstring.__module__ = 'numpy'
+geterrobj.__module__ = 'numpy'
+matmul.__module__ = 'numpy'
+may_share_memory.__module__ = 'numpy'
+nested_iters.__module__ = 'numpy'
+promote_types.__module__ = 'numpy'
+set_numeric_ops.__module__ = 'numpy'
+seterrobj.__module__ = 'numpy'
+zeros.__module__ = 'numpy'
+
+
array_function_dispatch = functools.partial(
overrides.array_function_dispatch, module='numpy')
@@ -832,6 +853,474 @@ def vdot(a, b):
return _multiarray_umath.vdot(a, b)
+def _bincount_dispatcher(x, weights=None, minlength=None):
+ return (x, weights)
+
+
+@array_function_dispatch(_bincount_dispatcher)
+def bincount(x, weights=None, minlength=0):
+ """
+ Count number of occurrences of each value in array of non-negative ints.
+
+ The number of bins (of size 1) is one larger than the largest value in
+ `x`. If `minlength` is specified, there will be at least this number
+ of bins in the output array (though it will be longer if necessary,
+ depending on the contents of `x`).
+ Each bin gives the number of occurrences of its index value in `x`.
+ If `weights` is specified the input array is weighted by it, i.e. if a
+ value ``n`` is found at position ``i``, ``out[n] += weight[i]`` instead
+ of ``out[n] += 1``.
+
+ Parameters
+ ----------
+ x : array_like, 1 dimension, nonnegative ints
+ Input array.
+ weights : array_like, optional
+ Weights, array of the same shape as `x`.
+ minlength : int, optional
+ A minimum number of bins for the output array.
+
+ .. versionadded:: 1.6.0
+
+ Returns
+ -------
+ out : ndarray of ints
+ The result of binning the input array.
+ The length of `out` is equal to ``np.amax(x)+1``.
+
+ Raises
+ ------
+ ValueError
+ If the input is not 1-dimensional, or contains elements with negative
+ values, or if `minlength` is negative.
+ TypeError
+ If the type of the input is float or complex.
+
+ See Also
+ --------
+ histogram, digitize, unique
+
+ Examples
+ --------
+ >>> np.bincount(np.arange(5))
+ array([1, 1, 1, 1, 1])
+ >>> np.bincount(np.array([0, 1, 1, 3, 2, 1, 7]))
+ array([1, 3, 1, 1, 0, 0, 0, 1])
+
+ >>> x = np.array([0, 1, 1, 3, 2, 1, 7, 23])
+ >>> np.bincount(x).size == np.amax(x)+1
+ True
+
+ The input array needs to be of integer dtype, otherwise a
+ TypeError is raised:
+
+ >>> np.bincount(np.arange(5, dtype=float))
+ Traceback (most recent call last):
+ File "<stdin>", line 1, in <module>
+ TypeError: array cannot be safely cast to required type
+
+ A possible use of ``bincount`` is to perform sums over
+ variable-size chunks of an array, using the ``weights`` keyword.
+
+ >>> w = np.array([0.3, 0.5, 0.2, 0.7, 1., -0.6]) # weights
+ >>> x = np.array([0, 1, 1, 2, 2, 2])
+ >>> np.bincount(x, weights=w)
+ array([ 0.3, 0.7, 1.1])
+
+ """
+ return _multiarray_umath.bincount(x, weights=weights, minlength=minlength)
+
+
+def _ravel_multi_index_dispatcher(multi_index, dims, mode=None, order=None):
+ return multi_index
+
+
+@array_function_dispatch(_ravel_multi_index_dispatcher)
+def ravel_multi_index(multi_index, dims, mode='raise', order='C'):
+ """
+ Converts a tuple of index arrays into an array of flat
+ indices, applying boundary modes to the multi-index.
+
+ Parameters
+ ----------
+ multi_index : tuple of array_like
+ A tuple of integer arrays, one array for each dimension.
+ dims : tuple of ints
+ The shape of array into which the indices from ``multi_index`` apply.
+ mode : {'raise', 'wrap', 'clip'}, optional
+ Specifies how out-of-bounds indices are handled. Can specify
+ either one mode or a tuple of modes, one mode per index.
+
+ * 'raise' -- raise an error (default)
+ * 'wrap' -- wrap around
+ * 'clip' -- clip to the range
+
+ In 'clip' mode, a negative index which would normally
+ wrap will clip to 0 instead.
+ order : {'C', 'F'}, optional
+ Determines whether the multi-index should be viewed as
+ indexing in row-major (C-style) or column-major
+ (Fortran-style) order.
+
+ Returns
+ -------
+ raveled_indices : ndarray
+ An array of indices into the flattened version of an array
+ of dimensions ``dims``.
+
+ See Also
+ --------
+ unravel_index
+
+ Notes
+ -----
+ .. versionadded:: 1.6.0
+
+ Examples
+ --------
+ >>> arr = np.array([[3,6,6],[4,5,1]])
+ >>> np.ravel_multi_index(arr, (7,6))
+ array([22, 41, 37])
+ >>> np.ravel_multi_index(arr, (7,6), order='F')
+ array([31, 41, 13])
+ >>> np.ravel_multi_index(arr, (4,6), mode='clip')
+ array([22, 23, 19])
+ >>> np.ravel_multi_index(arr, (4,4), mode=('clip','wrap'))
+ array([12, 13, 13])
+
+ >>> np.ravel_multi_index((3,1,4,1), (6,7,8,9))
+ 1621
+ """
+ return _multiarray_umath.ravel_multi_index(
+ multi_index, dims, mode=mode, order=order)
+
+
+def _deprecate_dims(shape, dims):
+ if dims is not None:
+ warnings.warn("'shape' argument should be used instead of 'dims'",
+ DeprecationWarning, stacklevel=3)
+ shape = dims
+ return shape
+
+
+def _unravel_index_dispatcher(indices, shape=None, order=None, dims=None):
+ shape = _deprecate_dims(shape, dims)
+ return (indices,)
+
+
+@array_function_dispatch(_unravel_index_dispatcher)
+def unravel_index(indices, shape=None, order='C', dims=None):
+ """
+ Converts a flat index or array of flat indices into a tuple
+ of coordinate arrays.
+
+ Parameters
+ ----------
+ indices : array_like
+ An integer array whose elements are indices into the flattened
+ version of an array of dimensions ``shape``. Before version 1.6.0,
+ this function accepted just one index value.
+ shape : tuple of ints
+ The shape of the array to use for unraveling ``indices``.
+
+ .. versionchanged:: 1.16.0
+ Renamed from ``dims`` to ``shape``.
+
+ order : {'C', 'F'}, optional
+ Determines whether the indices should be viewed as indexing in
+ row-major (C-style) or column-major (Fortran-style) order.
+
+ .. versionadded:: 1.6.0
+
+ Returns
+ -------
+ unraveled_coords : tuple of ndarray
+ Each array in the tuple has the same shape as the ``indices``
+ array.
+
+ See Also
+ --------
+ ravel_multi_index
+
+ Examples
+ --------
+ >>> np.unravel_index([22, 41, 37], (7,6))
+ (array([3, 6, 6]), array([4, 5, 1]))
+ >>> np.unravel_index([31, 41, 13], (7,6), order='F')
+ (array([3, 6, 6]), array([4, 5, 1]))
+
+ >>> np.unravel_index(1621, (6,7,8,9))
+ (3, 1, 4, 1)
+
+ """
+ shape = _deprecate_dims(shape, dims)
+ return _multiarray_umath.unravel_index(indices, shape, order=order)
+
+
+def _copyto_dispatcher(dst, src, casting=None, where=None):
+ return (dst, src, where)
+
+
+@array_function_dispatch(_copyto_dispatcher)
+def copyto(dst, src, casting='same_kind', where=True):
+ """
+ Copies values from one array to another, broadcasting as necessary.
+
+ Raises a TypeError if the `casting` rule is violated, and if
+ `where` is provided, it selects which elements to copy.
+
+ .. versionadded:: 1.7.0
+
+ Parameters
+ ----------
+ dst : ndarray
+ The array into which values are copied.
+ src : array_like
+ The array from which values are copied.
+ casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional
+ Controls what kind of data casting may occur when copying.
+
+ * 'no' means the data types should not be cast at all.
+ * 'equiv' means only byte-order changes are allowed.
+ * 'safe' means only casts which can preserve values are allowed.
+ * 'same_kind' means only safe casts or casts within a kind,
+ like float64 to float32, are allowed.
+ * 'unsafe' means any data conversions may be done.
+ where : array_like of bool, optional
+ A boolean array which is broadcasted to match the dimensions
+ of `dst`, and selects elements to copy from `src` to `dst`
+ wherever it contains the value True.
+ """
+ return _multiarray_umath.copyto(dst, src, casting=casting, where=where)
+
+
+def _putmask_dispatcher(a, mask, values):
+ return (a, mask, values)
+
+
+@array_function_dispatch(_putmask_dispatcher)
+def putmask(a, mask, values):
+ """
+ Changes elements of an array based on conditional and input values.
+
+ Sets ``a.flat[n] = values[n]`` for each n where ``mask.flat[n]==True``.
+
+ If `values` is not the same size as `a` and `mask` then it will repeat.
+ This gives behavior different from ``a[mask] = values``.
+
+ Parameters
+ ----------
+ a : array_like
+ Target array.
+ mask : array_like
+ Boolean mask array. It has to be the same shape as `a`.
+ values : array_like
+ Values to put into `a` where `mask` is True. If `values` is smaller
+ than `a` it will be repeated.
+
+ See Also
+ --------
+ place, put, take, copyto
+
+ Examples
+ --------
+ >>> x = np.arange(6).reshape(2, 3)
+ >>> np.putmask(x, x>2, x**2)
+ >>> x
+ array([[ 0, 1, 2],
+ [ 9, 16, 25]])
+
+ If `values` is smaller than `a` it is repeated:
+
+ >>> x = np.arange(5)
+ >>> np.putmask(x, x>1, [-33, -44])
+ >>> x
+ array([ 0, 1, -33, -44, -33])
+
+ """
+ return _multiarray_umath.putmask(a, mask, values)
+
+
+def _packbits_and_unpackbits_dispatcher(myarray, axis=None):
+ return (myarray,)
+
+
+@array_function_dispatch(_packbits_and_unpackbits_dispatcher)
+def packbits(myarray, axis=None):
+ """
+ Packs the elements of a binary-valued array into bits in a uint8 array.
+
+ The result is padded to full bytes by inserting zero bits at the end.
+
+ Parameters
+ ----------
+ myarray : array_like
+ An array of integers or booleans whose elements should be packed to
+ bits.
+ axis : int, optional
+ The dimension over which bit-packing is done.
+ ``None`` implies packing the flattened array.
+
+ Returns
+ -------
+ packed : ndarray
+ Array of type uint8 whose elements represent bits corresponding to the
+ logical (0 or nonzero) value of the input elements. The shape of
+ `packed` has the same number of dimensions as the input (unless `axis`
+ is None, in which case the output is 1-D).
+
+ See Also
+ --------
+ unpackbits: Unpacks elements of a uint8 array into a binary-valued output
+ array.
+
+ Examples
+ --------
+ >>> a = np.array([[[1,0,1],
+ ... [0,1,0]],
+ ... [[1,1,0],
+ ... [0,0,1]]])
+ >>> b = np.packbits(a, axis=-1)
+ >>> b
+ array([[[160],[64]],[[192],[32]]], dtype=uint8)
+
+ Note that in binary 160 = 1010 0000, 64 = 0100 0000, 192 = 1100 0000,
+ and 32 = 0010 0000.
+
+ """
+ return _multiarray_umath.packbits(myarray, axis)
+
+
+@array_function_dispatch(_packbits_and_unpackbits_dispatcher)
+def unpackbits(myarray, axis=None):
+ """
+ Unpacks elements of a uint8 array into a binary-valued output array.
+
+ Each element of `myarray` represents a bit-field that should be unpacked
+ into a binary-valued output array. The shape of the output array is either
+ 1-D (if `axis` is None) or the same shape as the input array with unpacking
+ done along the axis specified.
+
+ Parameters
+ ----------
+ myarray : ndarray, uint8 type
+ Input array.
+ axis : int, optional
+ The dimension over which bit-unpacking is done.
+ ``None`` implies unpacking the flattened array.
+
+ Returns
+ -------
+ unpacked : ndarray, uint8 type
+ The elements are binary-valued (0 or 1).
+
+ See Also
+ --------
+ packbits : Packs the elements of a binary-valued array into bits in a uint8
+ array.
+
+ Examples
+ --------
+ >>> a = np.array([[2], [7], [23]], dtype=np.uint8)
+ >>> a
+ array([[ 2],
+ [ 7],
+ [23]], dtype=uint8)
+ >>> b = np.unpackbits(a, axis=1)
+ >>> b
+ array([[0, 0, 0, 0, 0, 0, 1, 0],
+ [0, 0, 0, 0, 0, 1, 1, 1],
+ [0, 0, 0, 1, 0, 1, 1, 1]], dtype=uint8)
+
+ """
+ return _multiarray_umath.unpackbits(myarray, axis)
+
+
+def _shares_memory_dispatcher(a, b, max_work=None):
+ return (a, b)
+
+
+@array_function_dispatch(_shares_memory_dispatcher)
+def shares_memory(a, b, max_work=None):
+ """
+ Determine if two arrays share memory
+
+ Parameters
+ ----------
+ a, b : ndarray
+ Input arrays
+ max_work : int, optional
+ Effort to spend on solving the overlap problem (maximum number
+ of candidate solutions to consider). The following special
+ values are recognized:
+
+ max_work=MAY_SHARE_EXACT (default)
+ The problem is solved exactly. In this case, the function returns
+ True only if there is an element shared between the arrays.
+ max_work=MAY_SHARE_BOUNDS
+ Only the memory bounds of a and b are checked.
+
+ Raises
+ ------
+ numpy.TooHardError
+ Exceeded max_work.
+
+ Returns
+ -------
+ out : bool
+
+ See Also
+ --------
+ may_share_memory
+
+ Examples
+ --------
+ >>> np.may_share_memory(np.array([1,2]), np.array([5,8,9]))
+ False
+
+ """
+ return _multiarray_umath.shares_memory(a, b, max_work=max_work)
+
+
+@array_function_dispatch(_shares_memory_dispatcher)
+def may_share_memory(a, b, max_work=None):
+ """
+ Determine if two arrays might share memory
+
+ A return of True does not necessarily mean that the two arrays
+ share any element. It just means that they *might*.
+
+ Only the memory bounds of a and b are checked by default.
+
+ Parameters
+ ----------
+ a, b : ndarray
+ Input arrays
+ max_work : int, optional
+ Effort to spend on solving the overlap problem. See
+ `shares_memory` for details. Default for ``may_share_memory``
+ is to do a bounds check.
+
+ Returns
+ -------
+ out : bool
+
+ See Also
+ --------
+ shares_memory
+
+ Examples
+ --------
+ >>> np.may_share_memory(np.array([1,2]), np.array([5,8,9]))
+ False
+ >>> x = np.zeros([3, 4])
+ >>> np.may_share_memory(x[:,0], x[:,1])
+ True
+
+ """
+ return _multiarray_umath.may_share_memory(a, b, max_work=max_work)
+
+
def _is_busday_dispatcher(
dates, weekmask=None, holidays=None, busdaycal=None, out=None):
return (dates, weekmask, holidays, out)
@@ -1156,3 +1645,4 @@ def datetime_as_string(arr, unit=None, timezone='naive', casting='same_kind'):
datetime with units 'm' according to the rule 'safe'
"""
return _multiarray_umath.datetime_as_string(arr, unit, timezone, casting)
+
diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py
index 265c3636f..aa5be1af3 100644
--- a/numpy/core/numeric.py
+++ b/numpy/core/numeric.py
@@ -30,6 +30,7 @@ if sys.version_info[0] < 3:
from . import overrides
from . import umath
+from .overrides import set_module
from .umath import (multiply, invert, sin, UFUNC_BUFSIZE_DEFAULT,
ERR_IGNORE, ERR_WARN, ERR_RAISE, ERR_CALL, ERR_PRINT,
ERR_LOG, ERR_DEFAULT, PINF, NAN)
@@ -92,6 +93,7 @@ if sys.version_info[0] < 3:
__all__.extend(['getbuffer', 'newbuffer'])
+@set_module('numpy')
class ComplexWarning(RuntimeWarning):
"""
The warning raised when casting a complex dtype to a real dtype.
@@ -170,6 +172,7 @@ def zeros_like(a, dtype=None, order='K', subok=True):
return res
+@set_module('numpy')
def ones(shape, dtype=None, order='C'):
"""
Return a new array of given shape and type, filled with ones.
@@ -287,6 +290,7 @@ def ones_like(a, dtype=None, order='K', subok=True):
return res
+@set_module('numpy')
def full(shape, fill_value, dtype=None, order='C'):
"""
Return a new array of given shape and type, filled with `fill_value`.
@@ -462,6 +466,7 @@ def count_nonzero(a, axis=None):
return a_bool.sum(axis=axis, dtype=np.intp)
+@set_module('numpy')
def asarray(a, dtype=None, order=None):
"""Convert the input to an array.
@@ -533,6 +538,7 @@ def asarray(a, dtype=None, order=None):
return array(a, dtype, copy=False, order=order)
+@set_module('numpy')
def asanyarray(a, dtype=None, order=None):
"""Convert the input to an ndarray, but pass ndarray subclasses through.
@@ -585,6 +591,7 @@ def asanyarray(a, dtype=None, order=None):
return array(a, dtype, copy=False, order=order, subok=True)
+@set_module('numpy')
def ascontiguousarray(a, dtype=None):
"""
Return a contiguous array (ndim >= 1) in memory (C order).
@@ -625,6 +632,7 @@ def ascontiguousarray(a, dtype=None):
return array(a, dtype, copy=False, order='C', ndmin=1)
+@set_module('numpy')
def asfortranarray(a, dtype=None):
"""
Return an array (ndim >= 1) laid out in Fortran order in memory.
@@ -665,6 +673,7 @@ def asfortranarray(a, dtype=None):
return array(a, dtype, copy=False, order='F', ndmin=1)
+@set_module('numpy')
def require(a, dtype=None, requirements=None):
"""
Return an ndarray of the provided type that satisfies requirements.
@@ -763,6 +772,7 @@ def require(a, dtype=None, requirements=None):
return arr
+@set_module('numpy')
def isfortran(a):
"""
Returns True if the array is Fortran contiguous but *not* C contiguous.
@@ -1889,6 +1899,7 @@ def cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None):
little_endian = (sys.byteorder == 'little')
+@set_module('numpy')
def indices(dimensions, dtype=int):
"""
Return an array representing the indices of a grid.
@@ -1960,6 +1971,7 @@ def indices(dimensions, dtype=int):
return res
+@set_module('numpy')
def fromfunction(function, shape, **kwargs):
"""
Construct an array by executing a function over each coordinate.
@@ -2020,6 +2032,7 @@ def _frombuffer(buf, dtype, shape, order):
return frombuffer(buf, dtype=dtype).reshape(shape, order=order)
+@set_module('numpy')
def isscalar(num):
"""
Returns True if the type of `num` is a scalar type.
@@ -2096,6 +2109,7 @@ def isscalar(num):
or isinstance(num, numbers.Number))
+@set_module('numpy')
def binary_repr(num, width=None):
"""
Return the binary representation of the input number as a string.
@@ -2206,6 +2220,7 @@ def binary_repr(num, width=None):
return '1' * (outwidth - binwidth) + binary
+@set_module('numpy')
def base_repr(number, base=2, padding=0):
"""
Return a string representation of a number in the given base system.
@@ -2300,6 +2315,7 @@ def _maketup(descr, val):
return tuple(res)
+@set_module('numpy')
def identity(n, dtype=None):
"""
Return the identity array.
@@ -2640,6 +2656,7 @@ for key in _errdict.keys():
del key
+@set_module('numpy')
def seterr(all=None, divide=None, over=None, under=None, invalid=None):
"""
Set how floating-point errors are handled.
@@ -2741,6 +2758,7 @@ def seterr(all=None, divide=None, over=None, under=None, invalid=None):
return old
+@set_module('numpy')
def geterr():
"""
Get the current way of handling floating-point errors.
@@ -2792,6 +2810,7 @@ def geterr():
return res
+@set_module('numpy')
def setbufsize(size):
"""
Set the size of the buffer used in ufuncs.
@@ -2816,6 +2835,7 @@ def setbufsize(size):
return old
+@set_module('numpy')
def getbufsize():
"""
Return the size of the buffer used in ufuncs.
@@ -2829,6 +2849,7 @@ def getbufsize():
return umath.geterrobj()[0]
+@set_module('numpy')
def seterrcall(func):
"""
Set the floating-point error callback function or log object.
@@ -2921,6 +2942,7 @@ def seterrcall(func):
return old
+@set_module('numpy')
def geterrcall():
"""
Return the current callback function used on floating-point errors.
@@ -2973,6 +2995,7 @@ class _unspecified(object):
_Unspecified = _unspecified()
+@set_module('numpy')
class errstate(object):
"""
errstate(**kwargs)
diff --git a/numpy/core/numerictypes.py b/numpy/core/numerictypes.py
index 2fb841f7c..f00f92286 100644
--- a/numpy/core/numerictypes.py
+++ b/numpy/core/numerictypes.py
@@ -92,6 +92,7 @@ from numpy.core.multiarray import (
datetime_as_string, busday_offset, busday_count, is_busday,
busdaycalendar
)
+from numpy.core.overrides import set_module
# we add more at the bottom
__all__ = ['sctypeDict', 'sctypeNA', 'typeDict', 'typeNA', 'sctypes',
@@ -187,6 +188,8 @@ def maximum_sctype(t):
else:
return t
+
+@set_module('numpy')
def issctype(rep):
"""
Determines whether the given object represents a scalar data-type.
@@ -231,6 +234,8 @@ def issctype(rep):
except Exception:
return False
+
+@set_module('numpy')
def obj2sctype(rep, default=None):
"""
Return the scalar dtype or NumPy equivalent of Python type of an object.
@@ -285,6 +290,7 @@ def obj2sctype(rep, default=None):
return res.type
+@set_module('numpy')
def issubclass_(arg1, arg2):
"""
Determine if a class is a subclass of a second class.
@@ -323,6 +329,8 @@ def issubclass_(arg1, arg2):
except TypeError:
return False
+
+@set_module('numpy')
def issubsctype(arg1, arg2):
"""
Determine if the first argument is a subclass of the second argument.
@@ -353,6 +361,8 @@ def issubsctype(arg1, arg2):
"""
return issubclass(obj2sctype(arg1), obj2sctype(arg2))
+
+@set_module('numpy')
def issubdtype(arg1, arg2):
"""
Returns True if first argument is a typecode lower/equal in type hierarchy.
@@ -446,6 +456,8 @@ def _construct_lookups():
_construct_lookups()
+
+@set_module('numpy')
def sctype2char(sctype):
"""
Return the string representation of a scalar dtype.
@@ -586,6 +598,8 @@ def _register_types():
_register_types()
+
+@set_module('numpy')
def find_common_type(array_types, scalar_types):
"""
Determine common type following standard coercion rules.
diff --git a/numpy/core/overrides.py b/numpy/core/overrides.py
index e4d505f06..1cc1ff8d8 100644
--- a/numpy/core/overrides.py
+++ b/numpy/core/overrides.py
@@ -150,12 +150,12 @@ def verify_matching_signatures(implementation, dispatcher):
'default argument values')
-def override_module(module):
+def set_module(module):
"""Decorator for overriding __module__ on a function or class.
Example usage::
- @override_module('numpy')
+ @set_module('numpy')
def example():
pass
@@ -198,7 +198,7 @@ def array_function_dispatch(dispatcher, module=None, verify=True):
if not ENABLE_ARRAY_FUNCTION:
# __array_function__ requires an explicit opt-in for now
- return override_module(module)
+ return set_module(module)
def decorator(implementation):
if verify:
diff --git a/numpy/core/records.py b/numpy/core/records.py
index 1b596e4de..6fc282500 100644
--- a/numpy/core/records.py
+++ b/numpy/core/records.py
@@ -43,6 +43,7 @@ import warnings
from . import numeric as sb
from . import numerictypes as nt
from numpy.compat import isfileobj, bytes, long, unicode, os_fspath
+from numpy.core.overrides import set_module
from .arrayprint import get_printoptions
# All of the functions allow formats to be a dtype
@@ -82,6 +83,8 @@ def find_duplicate(list):
dup.append(list[i])
return dup
+
+@set_module('numpy')
class format_parser(object):
"""
Class to convert formats, names, titles description to a dtype.
diff --git a/numpy/core/src/multiarray/compiled_base.c b/numpy/core/src/multiarray/compiled_base.c
index e8380e3bc..17d8baf7b 100644
--- a/numpy/core/src/multiarray/compiled_base.c
+++ b/numpy/core/src/multiarray/compiled_base.c
@@ -1158,26 +1158,6 @@ arr_unravel_index(PyObject *self, PyObject *args, PyObject *kwds)
char *kwlist[] = {"indices", "shape", "order", NULL};
- /* Continue to support the older "dims" argument in place
- * of the "shape" argument. Issue an appropriate warning
- * if "dims" is detected in keywords, then replace it with
- * the new "shape" argument and continue processing as usual */
-
-
- if (kwds) {
- PyObject *dims_item, *shape_item;
- dims_item = PyDict_GetItemString(kwds, "dims");
- shape_item = PyDict_GetItemString(kwds, "shape");
- if (dims_item != NULL && shape_item == NULL) {
- if (DEPRECATE("'shape' argument should be"
- " used instead of 'dims'") < 0) {
- return NULL;
- }
- PyDict_SetItemString(kwds, "shape", dims_item);
- PyDict_DelItemString(kwds, "dims");
- }
- }
-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&|O&:unravel_index",
kwlist,
&indices0,
diff --git a/numpy/core/tests/test_overrides.py b/numpy/core/tests/test_overrides.py
index a32049ea1..7db551801 100644
--- a/numpy/core/tests/test_overrides.py
+++ b/numpy/core/tests/test_overrides.py
@@ -312,6 +312,7 @@ class TestArrayFunctionImplementation(object):
array = np.array(1)
assert_(func(array) is array)
+ assert_equal(func.__module__, 'my')
with assert_raises_regex(
TypeError, "no implementation found for 'my.func'"):
@@ -335,7 +336,7 @@ class TestNDArrayMethods(object):
class TestNumPyFunctions(object):
- def test_module(self):
+ def test_set_module(self):
assert_equal(np.sum.__module__, 'numpy')
assert_equal(np.char.equal.__module__, 'numpy.char')
assert_equal(np.fft.fft.__module__, 'numpy.fft')
diff --git a/numpy/fft/helper.py b/numpy/fft/helper.py
index e65883651..864768df5 100644
--- a/numpy/fft/helper.py
+++ b/numpy/fft/helper.py
@@ -11,7 +11,7 @@ except ImportError:
import dummy_threading as threading
from numpy.compat import integer_types
from numpy.core import integer, empty, arange, asarray, roll
-from numpy.core.overrides import array_function_dispatch
+from numpy.core.overrides import array_function_dispatch, set_module
# Created by Pearu Peterson, September 2002
@@ -128,6 +128,7 @@ def ifftshift(x, axes=None):
return roll(x, shift, axes)
+@set_module('numpy.fft')
def fftfreq(n, d=1.0):
"""
Return the Discrete Fourier Transform sample frequencies.
@@ -174,9 +175,9 @@ def fftfreq(n, d=1.0):
p2 = arange(-(n//2), 0, dtype=int)
results[N:] = p2
return results * val
- #return hstack((arange(0,(n-1)/2 + 1), arange(-(n/2),0))) / (n*d)
+@set_module('numpy.fft')
def rfftfreq(n, d=1.0):
"""
Return the Discrete Fourier Transform sample frequencies
diff --git a/numpy/lib/_datasource.py b/numpy/lib/_datasource.py
index ab00b1444..e34c376e5 100644
--- a/numpy/lib/_datasource.py
+++ b/numpy/lib/_datasource.py
@@ -41,8 +41,12 @@ import warnings
import shutil
import io
+from numpy.core.overrides import set_module
+
+
_open = open
+
def _check_mode(mode, encoding, newline):
"""Check mode and that encoding and newline are compatible.
@@ -262,7 +266,8 @@ def open(path, mode='r', destpath=os.curdir, encoding=None, newline=None):
return ds.open(path, mode, encoding=encoding, newline=newline)
-class DataSource (object):
+@set_module('numpy')
+class DataSource(object):
"""
DataSource(destpath='.')
diff --git a/numpy/lib/arraysetops.py b/numpy/lib/arraysetops.py
index 850e20123..fd64ecbd6 100644
--- a/numpy/lib/arraysetops.py
+++ b/numpy/lib/arraysetops.py
@@ -479,6 +479,11 @@ def setxor1d(ar1, ar2, assume_unique=False):
return aux[flag[1:] & flag[:-1]]
+def _in1d_dispatcher(ar1, ar2, assume_unique=None, invert=None):
+ return (ar1, ar2)
+
+
+@array_function_dispatch(_in1d_dispatcher)
def in1d(ar1, ar2, assume_unique=False, invert=False):
"""
Test whether each element of a 1-D array is also present in a second array.
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py
index fae6541bc..07bdb92ba 100644
--- a/numpy/lib/function_base.py
+++ b/numpy/lib/function_base.py
@@ -27,6 +27,7 @@ from numpy.core.fromnumeric import (
ravel, nonzero, partition, mean, any, sum
)
from numpy.core.numerictypes import typecodes
+from numpy.core.overrides import set_module
from numpy.core import overrides
from numpy.core.function_base import add_newdoc
from numpy.lib.twodim_base import diag
@@ -254,6 +255,7 @@ def flip(m, axis=None):
return m[indexer]
+@set_module('numpy')
def iterable(y):
"""
Check whether or not an object can be iterated over.
@@ -429,6 +431,7 @@ def average(a, axis=None, weights=None, returned=False):
return avg
+@set_module('numpy')
def asarray_chkfinite(a, dtype=None, order=None):
"""Convert the input to an array, checking for NaNs or Infs.
@@ -1891,6 +1894,7 @@ def _create_arrays(broadcast_shape, dim_sizes, list_of_core_dims, dtypes):
return arrays
+@set_module('numpy')
class vectorize(object):
"""
vectorize(pyfunc, otypes=None, doc=None, excluded=None, cache=False,
@@ -2555,6 +2559,7 @@ def corrcoef(x, y=None, rowvar=True, bias=np._NoValue, ddof=np._NoValue):
return c
+@set_module('numpy')
def blackman(M):
"""
Return the Blackman window.
@@ -2653,6 +2658,7 @@ def blackman(M):
return 0.42 - 0.5*cos(2.0*pi*n/(M-1)) + 0.08*cos(4.0*pi*n/(M-1))
+@set_module('numpy')
def bartlett(M):
"""
Return the Bartlett window.
@@ -2759,6 +2765,7 @@ def bartlett(M):
return where(less_equal(n, (M-1)/2.0), 2.0*n/(M-1), 2.0 - 2.0*n/(M-1))
+@set_module('numpy')
def hanning(M):
"""
Return the Hanning window.
@@ -2859,6 +2866,7 @@ def hanning(M):
return 0.5 - 0.5*cos(2.0*pi*n/(M-1))
+@set_module('numpy')
def hamming(M):
"""
Return the Hamming window.
@@ -3112,6 +3120,7 @@ def i0(x):
## End of cephes code for i0
+@set_module('numpy')
def kaiser(M, beta):
"""
Return the Kaiser window.
diff --git a/numpy/lib/histograms.py b/numpy/lib/histograms.py
index 1ff25b81f..06b30f978 100644
--- a/numpy/lib/histograms.py
+++ b/numpy/lib/histograms.py
@@ -3,15 +3,19 @@ Histogram-related functions
"""
from __future__ import division, absolute_import, print_function
+import functools
import operator
import warnings
import numpy as np
from numpy.compat.py3k import basestring
-from numpy.core.overrides import array_function_dispatch
+from numpy.core import overrides
__all__ = ['histogram', 'histogramdd', 'histogram_bin_edges']
+array_function_dispatch = functools.partial(
+ overrides.array_function_dispatch, module='numpy')
+
# range is a keyword argument to many functions, so save the builtin so they can
# use it.
_range = range
diff --git a/numpy/lib/index_tricks.py b/numpy/lib/index_tricks.py
index ff2e00d3e..56abe293a 100644
--- a/numpy/lib/index_tricks.py
+++ b/numpy/lib/index_tricks.py
@@ -13,6 +13,7 @@ from numpy.core.numerictypes import find_common_type, issubdtype
import numpy.matrixlib as matrixlib
from .function_base import diff
from numpy.core.multiarray import ravel_multi_index, unravel_index
+from numpy.core.overrides import set_module
from numpy.core import overrides, linspace
from numpy.lib.stride_tricks import as_strided
@@ -544,8 +545,11 @@ class CClass(AxisConcatenator):
def __init__(self):
AxisConcatenator.__init__(self, -1, ndmin=2, trans1d=0)
+
c_ = CClass()
+
+@set_module('numpy')
class ndenumerate(object):
"""
Multidimensional index iterator.
@@ -596,6 +600,7 @@ class ndenumerate(object):
next = __next__
+@set_module('numpy')
class ndindex(object):
"""
An N-dimensional iterator object to index arrays.
@@ -856,6 +861,7 @@ def fill_diagonal(a, val, wrap=False):
a.flat[:end:step] = val
+@set_module('numpy')
def diag_indices(n, ndim=2):
"""
Return the indices to access the main diagonal of an array.
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index 6fbb7e805..1da5b0a25 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -3,6 +3,7 @@ from __future__ import division, absolute_import, print_function
import sys
import os
import re
+import functools
import itertools
import warnings
import weakref
@@ -11,8 +12,9 @@ from operator import itemgetter, index as opindex
import numpy as np
from . import format
from ._datasource import DataSource
+from numpy.core import overrides
from numpy.core.multiarray import packbits, unpackbits
-from numpy.core.overrides import array_function_dispatch
+from numpy.core.overrides import set_module
from numpy.core._internal import recursive
from ._iotools import (
LineSplitter, NameValidator, StringConverter, ConverterError,
@@ -33,6 +35,7 @@ else:
from collections import Mapping
+@set_module('numpy')
def loads(*args, **kwargs):
# NumPy 1.15.0, 2017-12-10
warnings.warn(
@@ -48,6 +51,10 @@ __all__ = [
]
+array_function_dispatch = functools.partial(
+ overrides.array_function_dispatch, module='numpy')
+
+
class BagObj(object):
"""
BagObj(obj)
@@ -277,6 +284,7 @@ class NpzFile(Mapping):
return self.keys()
+@set_module('numpy')
def load(file, mmap_mode=None, allow_pickle=True, fix_imports=True,
encoding='ASCII'):
"""
@@ -784,6 +792,8 @@ def _getconv(dtype):
# amount of lines loadtxt reads in one chunk, can be overridden for testing
_loadtxt_chunksize = 50000
+
+@set_module('numpy')
def loadtxt(fname, dtype=float, comments='#', delimiter=None,
converters=None, skiprows=0, usecols=None, unpack=False,
ndmin=0, encoding='bytes', max_rows=None):
@@ -1424,6 +1434,7 @@ def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='',
fh.close()
+@set_module('numpy')
def fromregex(file, regexp, dtype, encoding=None):
"""
Construct an array from a text file, using regular expression parsing.
@@ -1522,6 +1533,7 @@ def fromregex(file, regexp, dtype, encoding=None):
#####--------------------------------------------------------------------------
+@set_module('numpy')
def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
skip_header=0, skip_footer=0, converters=None,
missing_values=None, filling_values=None, usecols=None,
diff --git a/numpy/lib/polynomial.py b/numpy/lib/polynomial.py
index c2702f0a7..7cbe80b46 100644
--- a/numpy/lib/polynomial.py
+++ b/numpy/lib/polynomial.py
@@ -16,6 +16,7 @@ import numpy.core.numeric as NX
from numpy.core import (isscalar, abs, finfo, atleast_1d, hstack, dot, array,
ones)
from numpy.core import overrides
+from numpy.core.overrides import set_module
from numpy.lib.twodim_base import diag, vander
from numpy.lib.function_base import trim_zeros
from numpy.lib.type_check import iscomplex, real, imag, mintypecode
@@ -26,6 +27,7 @@ array_function_dispatch = functools.partial(
overrides.array_function_dispatch, module='numpy')
+@set_module('numpy')
class RankWarning(UserWarning):
"""
Issued by `polyfit` when the Vandermonde matrix is rank deficient.
@@ -992,6 +994,7 @@ def _raise_power(astr, wrap=70):
return output + astr[n:]
+@set_module('numpy')
class poly1d(object):
"""
A one-dimensional polynomial class.
diff --git a/numpy/lib/twodim_base.py b/numpy/lib/twodim_base.py
index a05e68375..27d848608 100644
--- a/numpy/lib/twodim_base.py
+++ b/numpy/lib/twodim_base.py
@@ -10,6 +10,7 @@ from numpy.core.numeric import (
asarray, where, int8, int16, int32, int64, empty, promote_types, diagonal,
nonzero
)
+from numpy.core.overrides import set_module
from numpy.core import overrides
from numpy.core import iinfo, transpose
@@ -150,6 +151,7 @@ def flipud(m):
return m[::-1, ...]
+@set_module('numpy')
def eye(N, M=None, k=0, dtype=float, order='C'):
"""
Return a 2-D array with ones on the diagonal and zeros elsewhere.
@@ -343,6 +345,7 @@ def diagflat(v, k=0):
return wrap(res)
+@set_module('numpy')
def tri(N, M=None, k=0, dtype=float):
"""
An array with ones at and below the given diagonal and zeros elsewhere.
@@ -698,6 +701,7 @@ def histogram2d(x, y, bins=10, range=None, normed=None, weights=None,
return hist, edges[0], edges[1]
+@set_module('numpy')
def mask_indices(n, mask_func, k=0):
"""
Return the indices to access (n, n) arrays, given a masking function.
@@ -768,6 +772,7 @@ def mask_indices(n, mask_func, k=0):
return nonzero(a != 0)
+@set_module('numpy')
def tril_indices(n, k=0, m=None):
"""
Return the indices for the lower-triangle of an (n, m) array.
@@ -881,6 +886,7 @@ def tril_indices_from(arr, k=0):
return tril_indices(arr.shape[-2], k=k, m=arr.shape[-1])
+@set_module('numpy')
def triu_indices(n, k=0, m=None):
"""
Return the indices for the upper-triangle of an (n, m) array.
diff --git a/numpy/lib/type_check.py b/numpy/lib/type_check.py
index 9153e1692..1073613c9 100644
--- a/numpy/lib/type_check.py
+++ b/numpy/lib/type_check.py
@@ -11,7 +11,8 @@ __all__ = ['iscomplexobj', 'isrealobj', 'imag', 'iscomplex',
'common_type']
import numpy.core.numeric as _nx
-from numpy.core.numeric import asarray, asanyarray, array, isnan, zeros
+from numpy.core.numeric import asarray, asanyarray, isnan, zeros
+from numpy.core.overrides import set_module
from numpy.core import overrides
from .ufunclike import isneginf, isposinf
@@ -23,7 +24,8 @@ array_function_dispatch = functools.partial(
_typecodes_by_elsize = 'GDFgdfQqLlIiHhBb?'
-def mintypecode(typechars,typeset='GDFgdf',default='d'):
+@set_module('numpy')
+def mintypecode(typechars, typeset='GDFgdf', default='d'):
"""
Return the character for the minimum-size type to which given types can
be safely cast.
@@ -80,6 +82,12 @@ def mintypecode(typechars,typeset='GDFgdf',default='d'):
l.sort()
return l[0][1]
+
+def _asfarray_dispatcher(a, dtype=None):
+ return (a,)
+
+
+@array_function_dispatch(_asfarray_dispatcher)
def asfarray(a, dtype=_nx.float_):
"""
Return an array converted to a float type.
@@ -567,6 +575,7 @@ _namefromtype = {'S1': 'character',
'O': 'object'
}
+@set_module('numpy')
def typename(char):
"""
Return a description for the given data type code.
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):
"""
diff --git a/numpy/linalg/linalg.py b/numpy/linalg/linalg.py
index 771481e8e..dc2710dc0 100644
--- a/numpy/linalg/linalg.py
+++ b/numpy/linalg/linalg.py
@@ -29,6 +29,7 @@ from numpy.core import (
swapaxes, divide, count_nonzero, isnan
)
from numpy.core.multiarray import normalize_axis_index
+from numpy.core.overrides import set_module
from numpy.core import overrides
from numpy.lib.twodim_base import triu, eye
from numpy.linalg import lapack_lite, _umath_linalg
@@ -47,7 +48,8 @@ _L = b'L'
fortran_int = intc
-# Error object
+
+@set_module('numpy.linalg')
class LinAlgError(Exception):
"""
Generic Python-exception-derived object raised by linalg functions.
@@ -75,7 +77,6 @@ class LinAlgError(Exception):
numpy.linalg.LinAlgError: Singular matrix
"""
- pass
def _determine_error_states():
diff --git a/numpy/matrixlib/defmatrix.py b/numpy/matrixlib/defmatrix.py
index 7baa401a8..93b344cd4 100644
--- a/numpy/matrixlib/defmatrix.py
+++ b/numpy/matrixlib/defmatrix.py
@@ -7,6 +7,7 @@ import warnings
import ast
import numpy.core.numeric as N
from numpy.core.numeric import concatenate, isscalar
+from numpy.core.overrides import set_module
# While not in __all__, matrix_power used to be defined here, so we import
# it for backward compatibility.
from numpy.linalg import matrix_power
@@ -33,6 +34,8 @@ def _convert_from_string(data):
newdata.append(newrow)
return newdata
+
+@set_module('numpy')
def asmatrix(data, dtype=None):
"""
Interpret the input as a matrix.
@@ -67,6 +70,8 @@ def asmatrix(data, dtype=None):
"""
return matrix(data, dtype=dtype, copy=False)
+
+@set_module('numpy')
class matrix(N.ndarray):
"""
matrix(data, dtype=None, copy=True)
@@ -1023,6 +1028,7 @@ def _from_string(str, gdict, ldict):
return concatenate(rowtup, axis=0)
+@set_module('numpy')
def bmat(obj, ldict=None, gdict=None):
"""
Build a matrix object from a string, nested sequence, or array.
diff --git a/numpy/tests/test_public_api.py b/numpy/tests/test_public_api.py
new file mode 100644
index 000000000..856cca8eb
--- /dev/null
+++ b/numpy/tests/test_public_api.py
@@ -0,0 +1,77 @@
+from __future__ import division, absolute_import, print_function
+
+import sys
+
+import numpy as np
+import pytest
+
+
+def check_dir(module, module_name=None):
+ """Returns a mapping of all objects with the wrong __module__ attribute."""
+ if module_name is None:
+ module_name = module.__name__
+ results = {}
+ for name in dir(module):
+ item = getattr(module, name)
+ if (hasattr(item, '__module__') and hasattr(item, '__name__')
+ and item.__module__ != module_name):
+ results[name] = item.__module__ + '.' + item.__name__
+ return results
+
+
+@pytest.mark.skipif(
+ sys.version_info[0] < 3,
+ reason="NumPy exposes slightly different functions on Python 2")
+def test_numpy_namespace():
+ # None of these objects are publicly documented.
+ undocumented = {
+ 'Tester': 'numpy.testing._private.nosetester.NoseTester',
+ '_add_newdoc_ufunc': 'numpy.core._multiarray_umath._add_newdoc_ufunc',
+ 'add_docstring': 'numpy.core._multiarray_umath.add_docstring',
+ 'add_newdoc': 'numpy.core.function_base.add_newdoc',
+ 'add_newdoc_ufunc': 'numpy.core._multiarray_umath._add_newdoc_ufunc',
+ 'byte_bounds': 'numpy.lib.utils.byte_bounds',
+ 'compare_chararrays': 'numpy.core._multiarray_umath.compare_chararrays',
+ 'deprecate': 'numpy.lib.utils.deprecate',
+ 'deprecate_with_doc': 'numpy.lib.utils.<lambda>',
+ 'disp': 'numpy.lib.function_base.disp',
+ 'fastCopyAndTranspose': 'numpy.core._multiarray_umath._fastCopyAndTranspose',
+ 'get_array_wrap': 'numpy.lib.shape_base.get_array_wrap',
+ 'get_include': 'numpy.lib.utils.get_include',
+ 'int_asbuffer': 'numpy.core._multiarray_umath.int_asbuffer',
+ 'mafromtxt': 'numpy.lib.npyio.mafromtxt',
+ 'maximum_sctype': 'numpy.core.numerictypes.maximum_sctype',
+ 'ndfromtxt': 'numpy.lib.npyio.ndfromtxt',
+ 'recfromcsv': 'numpy.lib.npyio.recfromcsv',
+ 'recfromtxt': 'numpy.lib.npyio.recfromtxt',
+ 'safe_eval': 'numpy.lib.utils.safe_eval',
+ 'set_string_function': 'numpy.core.arrayprint.set_string_function',
+ 'show_config': 'numpy.__config__.show',
+ 'who': 'numpy.lib.utils.who',
+ }
+ # These built-in types are re-exported by numpy.
+ builtins = {
+ 'bool': 'builtins.bool',
+ 'complex': 'builtins.complex',
+ 'float': 'builtins.float',
+ 'int': 'builtins.int',
+ 'long': 'builtins.int',
+ 'object': 'builtins.object',
+ 'str': 'builtins.str',
+ 'unicode': 'builtins.str',
+ }
+ whitelist = dict(undocumented, **builtins)
+ bad_results = check_dir(np)
+ # pytest gives better error messages with the builtin assert than with
+ # assert_equal
+ assert bad_results == whitelist
+
+
+def test_numpy_linalg():
+ bad_results = check_dir(np.linalg)
+ assert bad_results == {}
+
+
+def test_numpy_fft():
+ bad_results = check_dir(np.fft)
+ assert bad_results == {}