-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
API: Add lib.array_utils
namespace
#24540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
52db499
API: Add lib.array_utils namespace
mtsokol f764f89
Add *_impl file
mtsokol 744f05d
DOC: Add array_utils to autogenerated docs
mtsokol 0ff4ac7
Fix after rebase
mtsokol 4dd54b5
Add changelog file
mtsokol 906468f
Apply review comments
mtsokol 383abbc
update module field for array_util functions
mtsokol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
* ``np.lib.array_utils`` public module has been introduced and in its initial version | ||
it hosts three functions: ``byte_bounds`` (moved from ``np.lib.utils``), | ||
``normalize_axis_tuple`` and ``normalize_axis_index`` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from numpy.core import asarray | ||
from numpy.core.numeric import normalize_axis_tuple, normalize_axis_index | ||
from numpy._utils import set_module | ||
|
||
__all__ = ["byte_bounds", "normalize_axis_tuple", "normalize_axis_index"] | ||
|
||
|
||
@set_module("numpy.lib.array_utils") | ||
def byte_bounds(a): | ||
""" | ||
Returns pointers to the end-points of an array. | ||
|
||
Parameters | ||
---------- | ||
a : ndarray | ||
Input array. It must conform to the Python-side of the array | ||
interface. | ||
|
||
Returns | ||
------- | ||
(low, high) : tuple of 2 integers | ||
The first integer is the first byte of the array, the second | ||
integer is just past the last byte of the array. If `a` is not | ||
contiguous it will not use every byte between the (`low`, `high`) | ||
values. | ||
|
||
Examples | ||
-------- | ||
>>> I = np.eye(2, dtype='f'); I.dtype | ||
dtype('float32') | ||
>>> low, high = np.byte_bounds(I) | ||
>>> high - low == I.size*I.itemsize | ||
True | ||
>>> I = np.eye(2); I.dtype | ||
dtype('float64') | ||
>>> low, high = np.byte_bounds(I) | ||
>>> high - low == I.size*I.itemsize | ||
True | ||
|
||
""" | ||
ai = a.__array_interface__ | ||
a_data = ai['data'][0] | ||
astrides = ai['strides'] | ||
ashape = ai['shape'] | ||
bytes_a = asarray(a).dtype.itemsize | ||
|
||
a_low = a_high = a_data | ||
if astrides is None: | ||
# contiguous case | ||
a_high += a.size * bytes_a | ||
else: | ||
for shape, stride in zip(ashape, astrides): | ||
if stride < 0: | ||
a_low += (shape-1)*stride | ||
else: | ||
a_high += (shape-1)*stride | ||
a_high += bytes_a | ||
return a_low, a_high |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from typing import Any, Iterable, Tuple | ||
|
||
from numpy import generic | ||
from numpy.typing import NDArray | ||
|
||
__all__: list[str] | ||
|
||
# NOTE: In practice `byte_bounds` can (potentially) take any object | ||
# implementing the `__array_interface__` protocol. The caveat is | ||
# that certain keys, marked as optional in the spec, must be present for | ||
# `byte_bounds`. This concerns `"strides"` and `"data"`. | ||
def byte_bounds(a: generic | NDArray[Any]) -> tuple[int, int]: ... | ||
|
||
def normalize_axis_tuple( | ||
axis: int | Iterable[int], | ||
ndim: int = ..., | ||
argname: None | str = ..., | ||
allow_duplicate: None | bool = ..., | ||
) -> Tuple[int, int]: ... | ||
|
||
def normalize_axis_index( | ||
axis: int = ..., | ||
ndim: int = ..., | ||
msg_prefix: None | str = ..., | ||
) -> int: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from ._array_utils_impl import ( | ||
__all__, | ||
byte_bounds, | ||
normalize_axis_index, | ||
normalize_axis_tuple, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from ._array_utils_impl import ( | ||
__all__ as __all__, | ||
byte_bounds as byte_bounds, | ||
normalize_axis_index as normalize_axis_index, | ||
normalize_axis_tuple as normalize_axis_tuple, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import numpy as np | ||
|
||
from numpy.lib import array_utils | ||
from numpy.testing import assert_equal | ||
|
||
|
||
class TestByteBounds: | ||
def test_byte_bounds(self): | ||
# pointer difference matches size * itemsize | ||
# due to contiguity | ||
a = np.arange(12).reshape(3, 4) | ||
low, high = array_utils.byte_bounds(a) | ||
assert_equal(high - low, a.size * a.itemsize) | ||
|
||
def test_unusual_order_positive_stride(self): | ||
a = np.arange(12).reshape(3, 4) | ||
b = a.T | ||
low, high = array_utils.byte_bounds(b) | ||
assert_equal(high - low, b.size * b.itemsize) | ||
|
||
def test_unusual_order_negative_stride(self): | ||
a = np.arange(12).reshape(3, 4) | ||
b = a.T[::-1] | ||
low, high = array_utils.byte_bounds(b) | ||
assert_equal(high - low, b.size * b.itemsize) | ||
|
||
def test_strided(self): | ||
a = np.arange(12) | ||
b = a[::2] | ||
low, high = array_utils.byte_bounds(b) | ||
# the largest pointer address is lost (even numbers only in the | ||
# stride), and compensate addresses for striding by 2 | ||
assert_equal(high - low, b.size * 2 * b.itemsize - b.itemsize) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import numpy as np | ||
import numpy.lib.array_utils as array_utils | ||
|
||
np.byte_bounds(1) # E: incompatible type | ||
rgommers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
array_utils.byte_bounds(1) # E: incompatible type |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.