forked from Bears-R-Us/arkouda
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes Bears-R-Us#3782: flip function to match numpy
- Loading branch information
Showing
6 changed files
with
180 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -96,3 +96,4 @@ | |
from arkouda.numpy.rec import * | ||
|
||
from ._numeric import * | ||
from ._manipulation_functions import * |
This file contains 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,88 @@ | ||
# from __future__ import annotations | ||
|
||
from typing import Optional | ||
from typing import Tuple | ||
from typing import Union | ||
from typing import cast | ||
|
||
from arkouda.client import generic_msg | ||
from arkouda.pdarrayclass import create_pdarray | ||
from arkouda.pdarrayclass import pdarray | ||
from arkouda.strings import Strings | ||
from arkouda.categorical import Categorical | ||
|
||
|
||
__all__ = ["flip"] | ||
|
||
|
||
def flip( | ||
x: Union[pdarray, Strings, Categorical], /, *, axis: Optional[Union[int, Tuple[int, ...]]] = None | ||
) -> Union[pdarray, Strings, Categorical]: | ||
""" | ||
Reverse an array's values along a particular axis or axes. | ||
Parameters | ||
---------- | ||
x : pdarray, Strings, or Categorical | ||
Reverse the order of elements in an array along the given axis. | ||
The shape of the array is preserved, but the elements are reordered. | ||
axis : int or Tuple[int, ...], optional | ||
The axis or axes along which to flip the array. If None, flip the array along all axes. | ||
Returns | ||
------- | ||
pdarray, Strings, or Categorical | ||
An array with the entries of axis reversed. | ||
Note | ||
---- | ||
This differs from numpy as it actually reverses the data, rather than presenting a view. | ||
""" | ||
axisList = [] | ||
if axis is not None: | ||
axisList = list(axis) if isinstance(axis, tuple) else [axis] | ||
|
||
if isinstance(x, pdarray): | ||
try: | ||
return create_pdarray( | ||
cast( | ||
str, | ||
generic_msg( | ||
cmd=( | ||
f"flipAll<{x.dtype},{x.ndim}>" | ||
if axis is None | ||
else f"flip<{x.dtype},{x.ndim}>" | ||
), | ||
args={ | ||
"name": x, | ||
"nAxes": len(axisList), | ||
"axis": axisList, | ||
}, | ||
), | ||
) | ||
) | ||
|
||
except RuntimeError as e: | ||
raise IndexError(f"Failed to flip array: {e}") | ||
elif isinstance(x, Categorical): | ||
if isinstance(x.permutation, pdarray): | ||
return Categorical.from_codes( | ||
codes=flip(x.codes), | ||
categories=x.categories, | ||
permutation=flip(x.permutation), | ||
segments=x.segments, | ||
) | ||
else: | ||
return Categorical.from_codes( | ||
codes=flip(x.codes), | ||
categories=x.categories, | ||
permutation=None, | ||
segments=x.segments, | ||
) | ||
|
||
elif isinstance(x, Strings): | ||
rep_msg = generic_msg( | ||
cmd="flipString", args={"objType": x.objType, "obj": x.entry, "size": x.size} | ||
) | ||
return Strings.from_return_msg(cast(str, rep_msg)) | ||
else: | ||
raise TypeError("flip only accepts type pdarray, Strings, or Categorical.") |
This file contains 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 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 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,56 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
import arkouda as ak | ||
from arkouda.categorical import Categorical | ||
from arkouda.testing import assert_equal | ||
|
||
seed = pytest.seed | ||
|
||
|
||
class TestJoin: | ||
|
||
@pytest.mark.parametrize("size", pytest.prob_size) | ||
@pytest.mark.parametrize("dtype", [int, ak.int64, ak.uint64, float, ak.float64, bool, ak.bool_]) | ||
def test_flip_pdarray(self, size, dtype): | ||
a = ak.arange(size, dtype=dtype) | ||
f = ak.flip(a) | ||
assert_equal(f, a[::-1]) | ||
|
||
@pytest.mark.skip_if_max_rank_less_than(3) | ||
@pytest.mark.parametrize("size", pytest.prob_size) | ||
@pytest.mark.parametrize("dtype", [ak.int64, ak.uint64, ak.float64]) | ||
def test_flip_multi_dim(self, size, dtype): | ||
a = ak.arange(size * 4, dtype=dtype).reshape((2, 2, size)) | ||
f = ak.flip(a) | ||
assert_equal(f, (size * 4 - 1) - a) | ||
|
||
@pytest.mark.skip_if_max_rank_less_than(3) | ||
@pytest.mark.parametrize("size", pytest.prob_size) | ||
def test_flip_multi_dim_bool(self, size): | ||
shape = (size, 2) | ||
|
||
vals = ak.array([True, False]) | ||
segs = ak.array([0, size]) | ||
perm = ak.concatenate([ak.arange(size) * 2, ak.arange(size) * 2 + 1]) | ||
a = ak.broadcast(segments=segs, values=vals, permutation=perm).reshape(shape) | ||
f = ak.flip(a) | ||
|
||
vals2 = ak.array([False, True]) | ||
f2 = ak.broadcast(segments=segs, values=vals2, permutation=perm).reshape(shape) | ||
assert_equal(f, f2) | ||
|
||
@pytest.mark.parametrize("size", pytest.prob_size) | ||
def test_flip_string(self, size): | ||
s = ak.random_strings_uniform(1, 2, size, seed=seed) | ||
assert_equal(ak.flip(s), s[::-1]) | ||
|
||
@pytest.mark.parametrize("size", pytest.prob_size) | ||
def test_flip_categorical(self, size): | ||
s = ak.random_strings_uniform(1, 2, size, seed=seed) | ||
c = Categorical(s) | ||
assert_equal(ak.flip(c), c[::-1]) | ||
|
||
# test case when c.permutation = None | ||
c2 = Categorical(c.to_pandas()) | ||
assert_equal(ak.flip(c2), c2[::-1]) |