8000 ENH: change list-of-array to tuple-of-array returns (Numba compat) by rgommers · Pull Request #25570 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: change list-of-array to tuple-of-array returns (Numba compat) #25570

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 5 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ENH: tuple returns for ogrid/meshgrid/histogramdd
Also some small docstring improvements to make clearer what
is returned.
  • Loading branch information
rgommers committed Jan 19, 2024
commit 779d74d6847724aa0e71d69bc03b63ffea501083
6 changes: 3 additions & 3 deletions numpy/lib/_function_base_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -4986,7 +4986,7 @@ def _meshgrid_dispatcher(*xi, copy=None, sparse=None, indexing=None):
@array_function_dispatch(_meshgrid_dispatcher)
def meshgrid(*xi, copy=True, sparse=False, indexing='xy'):
"""
Return a list of coordinate matrices from coordinate vectors.
Return a tuple of coordinate matrices from coordinate vectors.
Make N-D coordinate arrays for vectorized evaluations of
N-D scalar/vector fields over N-D grids, given
Expand Down Expand Up @@ -5027,7 +5027,7 @@ def meshgrid(*xi, copy=True, sparse=False, indexing='xy'):
Returns
-------
X1, X2,..., XN : list of ndarrays
X1, X2,..., XN : tuple of ndarrays
For vectors `x1`, `x2`,..., `xn` with lengths ``Ni=len(xi)``,
returns ``(N1, N2, N3,..., Nn)`` shaped arrays if indexing='ij'
or ``(N2, N1, N3,..., Nn)`` shaped arrays if indexing='xy'
Expand Down Expand Up @@ -5136,7 +5136,7 @@ def meshgrid(*xi, copy=True, sparse=False, indexing='xy'):
output = np.broadcast_arrays(*output, subok=True)

8000
if copy:
output = [x.copy() for x in output]
output = tuple(x.copy() for x in output)

return output

Expand Down
2 changes: 1 addition & 1 deletion numpy/lib/_function_base_impl.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ def meshgrid(
copy: bool = ...,
sparse: bool = ...,
indexing: L["xy", "ij"] = ...,
) -> list[NDArray[Any]]: ...
) -> tuple[NDArray[Any]]: ...

@overload
def delete(
Expand Down
4 changes: 2 additions & 2 deletions numpy/lib/_histograms_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,8 +957,8 @@ def histogramdd(sample, bins=10, range=None, density=None, weights=None):
H : ndarray
The multidimensional histogram of sample x. See density and weights
for the different possible semantics.
edges : list
A list of D arrays describing the bin edges for each dimension.
edges : tuple of ndarrays
A tuple of D arrays describing the bin edges for each dimension.
See Also
--------
Expand Down
2 changes: 1 addition & 1 deletion numpy/lib/_histograms_impl.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ def histogramdd(
range: Sequence[tuple[float, float]] = ...,
density: None | bool = ...,
weights: None | ArrayLike = ...,
) -> tuple[NDArray[Any], list[NDArray[Any]]]: ...
) -> tuple[NDArray[Any], tuple[NDArray[Any]]]: ...
23 changes: 17 additions & 6 deletions numpy/lib/_index_tricks_impl.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ def __getitem__(self, key):
slobj[k] = slice(None, None)
nn[k] = nn[k][tuple(slobj)]
slobj[k] = _nx.newaxis
return nn
return tuple(nn) # ogrid -> tuple of arrays
return nn # mgrid -> ndarray
except (IndexError, TypeError):
step = key.step
stop = key.stop
Expand Down Expand Up @@ -225,8 +226,9 @@ class MGridClass(nd_grid):

Returns
-------
mesh-grid
`ndarray`\\ s all of the same dimensions
mesh-grid : ndarray
A single array, containing a set of `ndarray`\\ s all of the same
dimensions. stacked along the first axis.

See Also
--------
Expand All @@ -251,6 +253,13 @@ class MGridClass(nd_grid):
>>> np.mgrid[-1:1:5j]
array([-1. , -0.5, 0. , 0.5, 1. ])

>>> np.mgrid[0:4].shape
(4,)
>>> np.mgrid[0:4, 0:5].shape
(2, 4, 5)
>>> np.mgrid[0:4, 0:5, 0:6].shape
(3, 4, 5, 6)

"""

def __init__(self):
Expand All @@ -277,8 +286,10 @@ class OGridClass(nd_grid):

Returns
-------
mesh-grid
`ndarray`\\ s with only one dimension not equal to 1
mesh-grid : ndarray or tuple of ndarrays
If the input is a single slice, returns an array.
If the input is multiple slices, returns a tuple of arrays, with
only one dimension not equal to 1.

See Also
--------
Expand All @@ -292,7 +303,7 @@ class OGridClass(nd_grid):
>>> from numpy import ogrid
>>> ogrid[-1:1:5j]
array([-1. , -0.5, 0. , 0.5, 1. ])
>>> ogrid[0:5,0:5]
>>> ogrid[0:5, 0:5]
[array([[0],
[1],
[2],
Expand Down
2 changes: 1 addition & 1 deletion numpy/lib/_index_tricks_impl.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class nd_grid(Generic[_BoolType]):
def __getitem__(
self: nd_grid[Literal[True]],
key: slice | Sequence[slice],
) -> list[NDArray[Any]]: ...
) -> tuple[NDArray[Any]]: ...

class MGridClass(nd_grid[Literal[False]]):
def __init__(self) -> None: ...
Expand Down
8 changes: 4 additions & 4 deletions numpy/typing/tests/data/reveal/histograms.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ assert_type(np.histogram(AR_i8, bins="rice", range=(0, 3)), tuple[npt.NDArray[An
assert_type(np.histogram(AR_i8, bins="scott", weights=AR_f8), tuple[npt.NDArray[Any], npt.NDArray[Any]])
assert_type(np.histogram(AR_f8, bins=1, density=True), tuple[npt.NDArray[Any], npt.NDArray[Any]])

assert_type(np.histogramdd(AR_i8, bins=[1]), tuple[npt.NDArray[Any], list[npt.NDArray[Any]]])
assert_type(np.histogramdd(AR_i8, range=[(0, 3)]), tuple[npt.NDArray[Any], list[npt.NDArray[Any]]])
assert_type(np.histogramdd(AR_i8, weights=AR_f8), tuple[npt.NDArray[Any], list[npt.NDArray[Any]]])
assert_type(np.histogramdd(AR_f8, density=True), tuple[npt.NDArray[Any], list[npt.NDArray[Any]]])
assert_type(np.histogramdd(AR_i8, bins=[1]), tuple[npt.NDArray[Any], tuple[npt.NDArray[Any]]])
assert_type(np.histogramdd(AR_i8, range=[(0, 3)]), tuple[npt.NDArray[Any], tuple[npt.NDArray[Any]]])
assert_type(np.histogramdd(AR_i8, weights=AR_f8), tuple[npt.NDArray[Any], tuple[npt.NDArray[Any]]])
assert_type(np.histogramdd(AR_f8, density=True), tuple[npt.NDArray[Any], tuple[npt.NDArray[Any]]])
4 changes: 2 additions & 2 deletions numpy/typing/tests/data/reveal/index_tricks.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ assert_type(np.ravel_multi_index((3, 1, 4, 1), (6, 7, 8, 9)), np.intp)
assert_type(np.mgrid[1:1:2], npt.NDArray[Any])
assert_type(np.mgrid[1:1:2, None:10], npt.NDArray[Any])

assert_type(np.ogrid[1:1:2], list[npt.NDArray[Any]])
assert_type(np.ogrid[1:1:2, None:10], list[npt.NDArray[Any]])
assert_type(np.ogrid[1:1:2], tuple[npt.NDArray[Any]])
assert_type(np.ogrid[1:1:2, None:10], tuple[npt.NDArray[Any]])

assert_type(np.index_exp[0:1], tuple[slice])
assert_type(np.index_exp[0:1, None:3], tuple[slice, slice])
Expand Down
4 changes: 2 additions & 2 deletions numpy/typing/tests/data/reveal/lib_function_base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ assert_type(np.quantile(AR_f8, [0.5], keepdims=True), Any)
assert_type(np.quantile(AR_f8, [0.5], axis=[1]), Any)
assert_type(np.quantile(AR_f8, [0.5], out=AR_c16), npt.NDArray[np.complex128])

assert_type(np.meshgrid(AR_f8, AR_i8, copy=False), list[npt.NDArray[Any]])
assert_type(np.meshgrid(AR_f8, AR_i8, AR_c16, indexing="ij"), list[npt.NDArray[Any]])
assert_type(np.meshgrid(AR_f8, AR_i8, copy=False), tuple[npt.NDArray[Any]])
assert_type(np.meshgrid(AR_f8, AR_i8, AR_c16, indexing="ij"), tuple[npt.NDArray[Any]])

assert_type(np.delete(AR_f8, np.s_[:5]), npt.NDArray[np.float64])
assert_type(np.delete(AR_LIKE_f8, [0, 4, 9], axis=0), npt.NDArray[Any])
Expand Down
0