-
Notifications
You must be signed in to change notification settings - Fork 53
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 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 e4c4b56
refactor: consolidate `axis` guidance
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,21 +79,28 @@ 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). A valid axis position **must** reside on the closed-interval ``[-N-1, N]``, where ``N`` is the number of dimensions in ``x``. If an axis position 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 position, the function **must** raise an exception. | ||
|
kgryte marked this conversation as resolved.
Outdated
|
||
|
|
||
| If ``axis`` is a tuple, | ||
|
|
||
| - each entry of ``axis`` must resolve to a unique axis position. If an entry is a negative integer, the entry **must** resolve to a positive axis position according to the rules described above. | ||
| - if provided an invalid axis position, the function **must** raise an exception. | ||
|
Member
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. numpy raises AxisError, which derives from IndexError (which torch.unsqueeze raises) and ValueError (which jax.numpy raises). So short of adding AxisError with a prescribed inheritance hierarchy we cannot be more specific on what exception to raise. |
||
| - 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. | ||
|
|
||
| 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 | ||
| ------ | ||
|
|
||
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