-
Notifications
You must be signed in to change notification settings - Fork 4
implement flip-based array manipulations #39
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b4c5217
ENH: add flip and rot90
ev-br 0297cdc
MAINT: moveaxis
ev-br 1ac48e3
ENH: add rollaxis
ev-br 513163c
TST: move TestArgwhere back to numpy_tests/core/test_numeric.py
ev-br 353698f
TST: reenable TestStdVar
ev-br 3da48a2
TST: move TestNonzero to numpy_tests/core/test_numeric.py
ev-br 679e053
lint
ev-br ded65f2
MAINT: address review comments
ev-br 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
"""Implementations of flip-based routines and related animals. | ||
""" | ||
|
||
import torch | ||
|
||
from . import _scalar_types, _util | ||
|
||
|
||
def flip(m_tensor, axis=None): | ||
# XXX: semantic difference: np.flip returns a view, torch.flip copies | ||
if axis is None: | ||
axis = tuple(range(m_tensor.ndim)) | ||
else: | ||
axis = _util.normalize_axis_tuple(axis, m_tensor.ndim) | ||
return torch.flip(m_tensor, axis) | ||
|
||
|
||
def flipud(m_tensor): | ||
return torch.flipud(m_tensor) | ||
|
||
|
||
def fliplr(m_tensor): | ||
return torch.fliplr(m_tensor) | ||
|
||
|
||
def rot90(m_tensor, k=1, axes=(0, 1)): | ||
axes = _util.normalize_axis_tuple(axes, m_tensor.ndim) | ||
return torch.rot90(m_tensor, k, axes) | ||
|
||
|
||
def swapaxes(tensor, axis1, axis2): | ||
return torch.swapaxes(tensor, axis1, axis2) | ||
|
||
|
||
# Straight vendor from: | ||
# https://github.com/numpy/numpy/blob/v1.24.0/numpy/core/numeric.py#L1259 | ||
# | ||
# Also note this function in NumPy is mostly retained for backwards compat | ||
# (https://stackoverflow.com/questions/29891583/reason-why-numpy-rollaxis-is-so-confusing) | ||
# so let's not touch it unless hard pressed. | ||
def rollaxis(tensor, axis, start=0): | ||
n = tensor.ndim | ||
axis = _util.normalize_axis_index(axis, n) | ||
if start < 0: | ||
start += n | ||
msg = "'%s' arg requires %d <= %s < %d, but %d was passed in" | ||
if not (0 <= start < n + 1): | ||
raise _util.AxisError(msg % ("start", -n, "start", n + 1, start)) | ||
if axis < start: | ||
# it's been removed | ||
start -= 1 | ||
if axis == start: | ||
# numpy returns a view, here we try returning the tensor itself | ||
# return tensor[...] | ||
return tensor | ||
axes = list(range(0, n)) | ||
axes.remove(axis) | ||
axes.insert(start, axis) | ||
return tensor.view(axes) |
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
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.