| Commit message (Collapse) | Author | Age | Files | Lines |
|
|
|
|
|
|
|
| |
As discussed in
https://mail.python.org/archives/list/numpy-discussion@python.org/thread/UKZJACAP5FUG7KP2AQDPE4P5ADNWLOHZ/
This flag was always meant to be temporary, and cleaning it up is
long overdue.
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
This moves dispatching for `__array_function__` into a C-wrapper. This
helps speed for multiple reasons:
* Avoids one additional dispatching function call to C
* Avoids the use of `*args, **kwargs` which is slower.
* For simple NumPy calls we can stay in the faster "vectorcall" world
This speeds up things generally a little, but can speed things up a lot
when keyword arguments are used on lightweight functions, for example::
np.can_cast(arr, dtype, casting="same_kind")
is more than twice as fast with this.
There is one alternative in principle to get best speed: We could inline
the "relevant argument"/dispatcher extraction. That changes behavior in
an acceptable but larger way (passes default arguments).
Unless the C-entry point seems unwanted, this should be a decent step
in the right direction even if we want to do that eventually, though.
Closes gh-20790
Closes gh-18547 (although not quite sure why)
|
|\
| |
| | |
MAINT: Move set_module from numpy.core to numpy._utils
|
| |
| |
| |
| |
| | |
Note that unfortunately, compat does expose _inspect as well,
so the import remains (just the definition place moves).
|
| | |
|
|/
|
|
| |
Closes #15544
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
This replaces the name in the TypeError with the actually raised
name. In principle we could add one more check, because a
signature related TypeError must have a traceback with exactly
one entry (so `sys.exc_info()[2].tb_next is None`).
In practice this seems unnecessary though.
This ensures the following message:
>>> np.histogram(asdf=3)
TypeError: histogram() got an unexpected keyword argument 'asdf'
Closes gh-21647
|
| |
|
|
|
|
|
|
| |
Import of 'histogram' is not used.
Import of 'histogramdd' is not used.
Import of 'textwrap' is not used.
|
|
|
|
| |
Co-authored-by: Eric Wieser <wieser.eric@gmail.com>
|
| |
|
|
|
|
|
|
|
|
|
|
| |
This accepts NEP 35 as final. There has been no discussion about it
in a long time. The current mode is strict about type input
(`like=` must be an array-like). So that most of the "open" points
are OK to remain open.
Unless we need to discuss the name `like` or the fact that we pass
an array-like itself, the previously noted open points gh-17075
all seem not very relevant anymore.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
This PR adds the implementation of NEP-35's like= argument, allowing dispatch of array creation functions with __array_function__ based on a reference array.
* ENH: Add like= kwarg via __array_function__ dispatcher to asarray
* ENH: Add new function for __array_function__ dispatching from C
This new function allows dispatching from C directly, while also
implementing the new `like=` argument, requiring only minimal
changes to existing array creation functions that need to add
support for that argument.
* ENH: Add like= support to numpy.array
The implementation uses array_implement_c_array_function, thus
introducing minimal complexity to the original _array_fromobject
code.
* BUG: Fix like= dispatcher for np.full
* ENH: Remove np.asarray like= dispatcher via Python
np.asarray can rely on np.array's C dispatcher instead.
* TST: Add some tests for like= argument
Tests comprise some of the functions that have been implemented already:
* np.array (C dispatcher)
* np.asarray (indirect C dispatcher via np.array)
* np.full (Python dispatcher)
* np.ones (Python dispatcher)
* ENH: Remove like= argument during array_implement_array_function
* ENH: Add like= kwarg to ones and full
* BUG: prevent duplicate removal of `like=` argument
* ENH: Make `like=` a keyword-only argument
* ENH: Use PyUnicode_InternFromString in arrayfunction_override
Replace PyUnicode_FromString by PyUnicode_InternFromString to cache
"like" string.
* ENH: Check for arrayfunction_override import errors
Check and handle errors on importing NumPy's Python functions
* BUG: Fix array_implement_c_array_function error handling
* ENH: Handle exceptions with C implementation of `like=`
* ENH: Add `like=` dispatch for all asarray functions
Using Python dispatcher for all of them. Using the C dispatcher
directly on the `np.array` call can result in incorrect behavior.
Incorrect behavior may happen if the downstream library's
implementation is different or if not all keyword arguments are
supported.
* ENH: Simplify handling of exceptions with `like=`
* TST: Add test for exception handling with `like=`
* ENH: Add support for `like=` to `np.empty` and `np.zeros`
* TST: Add `like=` tests for `np.empty` and `np.zeros`
* ENH: Add `like=` to remaining multiarraymodule.c functions
Functions are:
* np.arange
* np.frombuffer
* np.fromfile
* np.fromiter
* np.fromstring
* TST: Add tests for multiarraymodule.c functions with like=
Functions are:
* np.arange
* np.frombuffer
* np.fromfile
* np.fromiter
* np.fromstring
* ENH: Add `like=` support to more creation functions
Support for the following functions is added:
* np.eye
* np.fromfunction
* np.genfromtxt
* np.identity
* np.loadtxt
* np.tri
* TST: Add `like=` tests for multiple functions
Tests for the following functions are added:
* np.eye
* np.fromfunction
* np.genfromtxt
* np.identity
* np.loadtxt
* np.tri
* TST: Reduce code duplication in `like=` tests
* DOC: Document `like=` in functions that support it
Add documentations for the following functions:
* np.array
* np.arange
* np.asarray
* np.asanyarray
* np.ascontiguousarray
* np.asfortranarray
* np.require
* np.empty
* np.full
* np.ones
* np.zeros
* np.identity
* np.eye
* np.tri
* np.frombuffer
* np.fromfile
* np.fromiter
* np.fromstring
* np.loadtxt
* np.genfromtxt
* ENH: Add `like=` to numpy/__init__.pyi stubs
* BUG: Remove duplicate `like=` dispatching in as*array
Functions `np.asanyarray`, `np.contiguousarray` and `np.fortranarray`
were dispatching both via their definitions and `np.array` calls,
the latter should be avoided.
* BUG: Fix missing check in array_implement_array_function
* BUG: Add missing keyword-only markers in stubs
* BUG: Fix duplicate keyword-only marker in array stub
* BUG: Fix syntax error in numpy/__init__.pyi
* BUG: Fix more syntax errors in numpy/__init__.pyi
* ENH: Intern arrayfunction_override strings in multiarraymodule
* STY: Add missing brackets to arrayfunction_override.c
* MAINT: Remove arrayfunction_override dict check for kwarg
* TST: Assert that 'like' is not in TestArrayLike kwargs
* MAINT: Rename array_implement_c_array_function(_creation)
This is done to be more explicit as to its usage being intended for
array creation functions only.
* MAINT: Use NotImplemented to indicate fallback to default
* TST: Test that results with `like=np.array` are correct
* TST: Avoid duplicating MyArray code in TestArrayLike
* TST: Don't delete temp file, it may cause issues with Windows
* TST: Don't rely on eval in TestArrayLike
* TST: Use lambda with StringIO in TestArrayLike
* ENH: Avoid unnecessary Py_XDECREF in arrayfunction_override
* TST: Make TestArrayLike more readable
* ENH: Cleaner error handling in arrayfunction_override
* ENH: Simplify array_implement_c_array_function_creation
* STY: Add missing spaces to multiarraymodule.c
* STY: C99 declaration style in arrayfunction_override.c
* ENH: Simplify arrayfunction_override.c further
Remove cleanup label from array_implementation_c_array_function,
simplifying the code. Fix unitialized variable warning in
array_implementation_array_function_internal.
* DOC: Use string replacement for `like=` documentation
Avoid repeating the full text for the `like=` argument by storing it as
a variable and using `replace` on each docstring.
* DOC: Update `like=` docstring
* TST: Test like= with array not implementing __array_function__
* TST: Add missing asanyarray to TestArrayLike
* ENH: Use helper function for like= dispatching
Avoid dispatching like= from Python implementation functions to improve
their performance. This is achieved by only calling a dispatcher
function when like is passed by the users.
* ENH: Rename array_function_dispatch kwarg to public_api
* BUG: Add accidentally removed decorator for np.eye back
* DOC: Add set_array_function_like_doc function
The function keeps Python files cleaner and resolve errors when __doc__
is not defined due to PYTHONOPTIMIZE or -OO .
* DOC: Add mention to like= kwarg being experimental
* TST: Test like= with not implemented downstream function
* DOC: Fix like= docstring reference to NEP 35.
* ENH: Prevent silent errors if public_api is not callable
* ENH: Make set_array_function_like_doc a decorator
* ENH: Simplify `_*_with_like` functions
* BUG: Fix multiple `like=` dispatching in `require`
* MAINT: Remove now unused public_api from array_function_dispatch
Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>
|
|
|
|
|
|
|
|
| |
One of the header line was not long enough, make it the same length as
the title.
The section "Arguments" is usually called "Parameters". Update for
consistency.
|
|
|
|
| |
Avoid the repeated call of `dedent` when a single call is enough. This micro-optimizes
the import time from around 100ms (or slightly above) by 4-6 ms. (PR: gh-14095)
|
| |
|
| |
|
|
|
|
|
|
| |
xref GH-13624, GH-12028
TODO: update tests/CI for NUMPY_EXPERIMENTAL_ARRAY_FUNCTION=0
|
|\ |
|
| | |
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| | |
xref GH-12028
Current behavior:
>>> np.dot(None, None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/shoyer/dev/numpy/numpy/core/overrides.py", line 175, in public_api
implementation, public_api, relevant_args, args, kwargs)
TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType'
>>> np.stack([], invalid=True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/shoyer/dev/numpy/numpy/core/overrides.py", line 148, in public_api
relevant_args = dispatcher(*args, **kwargs)
TypeError: _stack_dispatcher() got an unexpected keyword argument 'invalid'
With this change:
>>> np.dot(None, None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 6, in dot
TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType'
>>> np.stack([], invalid=True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 4, in stack
TypeError: _stack_dispatcher() got an unexpected keyword argument 'invalid'
|
| | |
|
|/ |
|
| |
|
|
|
|
|
|
|
|
|
|
| |
- Always enable __array_function__ overrides.
- Remove special cases for Python 2 compatibility.
- Document these changes in 1.17.0-notes.rst.
It will be good to see ASV numbers to understand the performance implications
of these changes. If need be, we can speed up NumPy functions internally by
using non-dispatched functions (with ``.__wrapped__``).
|
| |
|
| |
|
|\
| |
| | |
MAINT: Allow subclasses in `ndarray.__array_function__`.
|
| |
| |
| |
| | |
The Liskov substitution principle suggests it should.
|
| | |
|
|/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
The original motivation for the style of these wrapper functions, introduced
in gh-12175, was to preserve introspection. But it turns out NumPy's functions
defined in C don't support introspection anyways, so the extra wrapper
functions are entirely pointless.
This version reverts the additional wrapper functions, which put default
arguments in two places and introduced slow-down due to the overhead of
another function call.
I've retained docstrings in multiarray.py, since it's definitely more readable
to keep docstrings and dispatchers together rather than leaving docstrings in
_add_newdocs.py.
One bonus of this approach is that dispatcher functions have the same name
as their implementations, so `np.concatenate(unknown=True)` gives an
error message mentioning "concatenate" rather than "_concatenate_dispatcher":
`TypeError: concatenate() got an unexpected keyword argument 'unknown'`
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
Fixes GH-12271
Tests verify that everything in ``dir(numpy)`` either has ``__module__`` set to
``'numpy'``, or appears in an explicit whitelist of undocumented functions and
exported bulitins. These should eventually be documented or removed.
I also identified a handful of functions for which I had accidentally not setup
dispatch for with ``__array_function__`` before, because they were listed under
"ndarray methods" in ``_add_newdocs.py``. I guess that should be a lesson in
trusting code comments :).
|
| |
|
| |
|
| |
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
| |
Per discussion on the mailing list, __array_function__ isn't quite ready to
release as part of NumPy 1.16:
https://mail.python.org/pipermail/numpy-discussion/2018-November/078949.html
We'd like to improve performance a bit, and it will be easier to support
introspection on NumPy functions if we support Python 3 only.
So for now, you need to set the environment variable
``NUMPY_EXPERIMENTAL_ARRAY_FUNCTION=1`` to enable dispatching.
|
|
|
|
|
| |
Mostly aim to ensure that the most common case, in which the only
override class is ndarray, is fast.
|
|\ |
|
| | |
|
| | |
|
| | |
|
|/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
* Validate dispatcher functions in array_function_dispatch
They should have the same signature as the decorated function.
Note: eventually these checks should be optional -- we really only need them
to be run as part of NumPy's test suite, not every time numpy is imported.
* ENH: make signature checking in array_function_dispatch optional
* Change verify_signature keyword argument to verify
|
| |
|
|
|
|
|
| |
I think this organization makes a little more sense, especially when thinking
about what the functions we want to write in C should look like.
|
| |
|
| |
|