-
Notifications
You must be signed in to change notification settings - Fork 54
feat: add support for specifying a tuple of axis positions in expand_dims
#988
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
Open
kgryte
wants to merge
5
commits into
data-apis:main
Choose a base branch
from
kgryte:feat/expand-dims-tuple
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+31
−9
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d9f2a7a
fix: address signature regression in `expand_dims`
kgryte 5a5874c
feat: add support for specifying a tuple of axis positions
kgryte dceea5e
fix: clarify resolution rules
kgryte aa0b8a1
docs: rephrase copy
kgryte 67eaae1
docs: update copy
kgryte 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
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 |
|---|---|---|
|
|
@@ -79,26 +79,48 @@ def concat( | |
| """ | ||
|
|
||
|
|
||
| def expand_dims(x: array, /, *, axis: int = 0) -> array: | ||
| def expand_dims(x: array, /, axis: Union[int, Tuple[int, ...]]) -> array: | ||
| """ | ||
| Expands the shape of an array by inserting a new axis of size one at the position specified by ``axis``. | ||
| Expands the shape of an array by inserting a new axis of size one at the position (or positions) specified by ``axis``. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| x: array | ||
| input array. | ||
| axis: int | ||
| axis position (zero-based). A valid ``axis`` **must** reside on the closed-interval ``[-N-1, N]``, where ``N`` is the number of axes in ``x``. If an axis is specified as a negative integer, the axis position at which to insert a singleton dimension **must** be computed as ``N + axis + 1``. Hence, if provided ``-1``, the resolved axis position **must** be ``N`` (i.e., a singleton dimension **must** be appended to the input array ``x``). If provided ``-N-1``, the resolved axis position **must** be ``0`` (i.e., a singleton dimension **must** be prepended to the input array ``x``). If provided an invalid axis, the function **must** raise an exception. Default: ``0``. | ||
| axis: Union[int, Tuple[int, ...]] | ||
| axis position(s) (zero-based). If ``axis`` is an integer, | ||
|
|
||
| - a valid axis position **must** reside on the closed-interval ``[-N-1, N]``, where ``N`` is the number of dimensions in ``x``. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One idea: would it be clearer here to talk about valid indices in terms of the output dimensions? Then this would change to then the tuple version of this would be identical, except it would say |
||
| - if an axis position is specified as a negative integer, the axis position of the inserted singleton dimension in the output array **must** be computed as ``N + axis + 1``. For example, if provided ``-1``, the resolved axis position **must** be ``N`` (i.e., a singleton dimension **must** be appended to the input array ``x``). Similarly, if provided ``-N-1``, the resolved axis position **must** be ``0`` (i.e., a singleton dimension **must** be prepended to the input array ``x``). | ||
| - if provided an invalid axis position, the function **must** raise an exception. | ||
|
|
||
| If ``axis`` is a tuple, | ||
|
|
||
| - a valid axis position **must** reside on the closed-interval ``[-M-1, M]``, where ``M = N + len(axis) - 1`` and ``N`` is the number of dimensions in ``x``. | ||
| - if an entry is a negative integer, the axis position of the inserted singleton dimension in the output array **must** be computed as ``M + axis + 1``. | ||
| - each entry of ``axis`` must resolve to a unique positive axis position. | ||
| - for each entry of ``axis``, the corresponding dimension in the expanded output array **must** be a singleton dimension. | ||
| - for the remaining dimensions of the expanded output array, the output array dimensions **must** correspond to the dimensions of ``x`` in order. | ||
| - if provided an invalid axis position, the function **must** raise an exception. | ||
|
|
||
| Returns | ||
| ------- | ||
| out: array | ||
| an expanded output array. **Must** have the same data type as ``x``. | ||
| an expanded output array. **Must** have the same data type as ``x``. If ``axis`` is an integer, the output array must have ``N + 1`` dimensions. If ``axis`` is a tuple, the output array must have ``N + len(axis)`` dimensions. | ||
|
|
||
| Raises | ||
| ------ | ||
| IndexError | ||
| If provided an invalid ``axis``, an ``IndexError`` **should** be raised. | ||
|
|
||
| Notes | ||
| ----- | ||
|
|
||
| - Calling this function with a tuple of axis positions **must** be semantically equivalent to calling this function repeatedly with a single axis position only when the following three conditions are met: | ||
|
|
||
| - each entry of the tuple is normalized to positive axis positions according to the number of dimensions in the expanded output array. | ||
| - the normalized positive axis positions are sorted in ascending order. | ||
| - the normalized positive axis positions are unique. | ||
| """ | ||
|
|
||
|
|
||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SciPy doesn't look too badly impacted by the reversion, I think just https://github.com/scipy/scipy/blob/341152d40c3274c0e37068321cccfb08733e2707/scipy/signal/_filter_design.py#L87