Skip to content

Commit

Permalink
Closes Bears-R-Us#3709: reshape to return a multi-dimensional array (B…
Browse files Browse the repository at this point in the history
…ears-R-Us#3715)

* Closes Bears-R-Us#3709: reshape to return a multi-dimensional array

Co-authored-by: drculhane <drculhane@users.noreply.github.com>

* remove order argument

---------

Co-authored-by: Amanda Potts <ajpotts@users.noreply.github.com>
Co-authored-by: drculhane <drculhane@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 3, 2024
1 parent e08b0db commit 2205c45
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 657 deletions.
1 change: 0 additions & 1 deletion arkouda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
del get_versions

from arkouda.numpy import *
from arkouda.array_view import *
from arkouda.client import *
from arkouda.client_dtypes import *
from arkouda.pdarrayclass import *
Expand Down
471 changes: 0 additions & 471 deletions arkouda/array_view.py

This file was deleted.

61 changes: 18 additions & 43 deletions arkouda/pdarrayclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,6 @@ def _create_scalar_array(value):
)


def _reshape(array: pdarray, shape: Tuple[int, ...]):
"""
Reshape the pdarray to the specified shape
Requires the ManipulationMsg server module
"""
return create_pdarray(
generic_msg(
cmd=f"reshape<{array.dtype},{array.ndim},{len(shape)}>",
args={
"name": array,
"shape": shape,
},
),
)


def _squeeze(array: pdarray, degen_axes: List[int]):
"""
Remove degenerate axes from a pdarray
Expand Down Expand Up @@ -980,7 +963,7 @@ def __getitem__(self, key):
if len(rs) > 0:
shape.append(rs.pop(0))

return _reshape(ret_array, tuple(shape))
return ret_array.reshape(shape)
else:
return ret_array

Expand Down Expand Up @@ -1147,7 +1130,7 @@ def __setitem__(self, key, value):
)

# reshape to add singleton dimensions as needed
_value_r = _reshape(_value, slice_shape)
_value_r = _value.reshape(slice_shape)
else:
raise ValueError(
f"value array must not have more dimensions ({_value.ndim}) than the"
Expand Down Expand Up @@ -1737,35 +1720,35 @@ def bigint_to_uint_arrays(self) -> List[pdarray]:
ret_list = json.loads(generic_msg(cmd="bigint_to_uint_list", args={"array": self}))
return list(reversed([create_pdarray(a) for a in ret_list]))

def reshape(self, *shape, order="row_major"):
def reshape(self, *shape):
"""
Gives a new shape to an array without changing its data.
Parameters
----------
shape : int, tuple of ints, or pdarray
The new shape should be compatible with the original shape.
order : str {'row_major' | 'C' | 'column_major' | 'F'}
Read the elements of the pdarray in this index order
By default, read the elements in row_major or C-like order where the last index
changes the fastest
If 'column_major' or 'F', read the elements in column_major or Fortran-like order where the
first index changes the fastest
Returns
-------
ArrayView
An arrayview object with the data from the array but with the new shape
pdarray
a pdarray with the same data, reshaped to the new shape
"""
from arkouda.array_view import ArrayView

# allows the elements of the shape parameter to be passed in as separate arguments
# For example, a.reshape(10, 11) is equivalent to a.reshape((10, 11))
if len(shape) == 1:
shape = shape[0]
elif not isinstance(shape, pdarray):
shape = [i for i in shape]
return ArrayView(base=self, shape=shape, order=order)
return create_pdarray(
generic_msg(
cmd=f"reshape<{self.dtype},{self.ndim},{len(shape)}>",
args={
"name": self.name,
"shape": shape,
},
),
)

def to_ndarray(self) -> np.ndarray:
"""
Expand Down Expand Up @@ -1828,9 +1811,7 @@ def to_ndarray(self) -> np.ndarray:
data = cast(
memoryview,
generic_msg(
cmd=f"tondarray<{self.dtype},{self.ndim}>",
args={"array": self},
recv_binary=True
cmd=f"tondarray<{self.dtype},{self.ndim}>", args={"array": self}, recv_binary=True
),
)
# Make sure the received data has the expected length
Expand Down Expand Up @@ -2601,7 +2582,7 @@ def create_pdarrays(repMsg: str) -> List[pdarray]:
# TODO: maybe add more robust json parsing here
try:
repMsg = repMsg.strip("[]")
responses = [r.strip().strip('\"') for r in repMsg.split("\",")]
responses = [r.strip().strip('"') for r in repMsg.split('",')]
return [create_pdarray(response) for response in responses]
except Exception as e:
raise ValueError(e)
Expand Down Expand Up @@ -3099,10 +3080,7 @@ def cov(x: pdarray, y: pdarray) -> np.float64:
``cov = ((x - x.mean()) * (y - y.mean())).sum() / (x.size - 1)``.
"""
return parse_single_value(
generic_msg(
cmd=f"cov<{x.dtype},{x.ndim},{y.dtype},{y.ndim}>",
args={"x": x, "y": y}
)
generic_msg(cmd=f"cov<{x.dtype},{x.ndim},{y.dtype},{y.ndim}>", args={"x": x, "y": y})
)


Expand Down Expand Up @@ -3140,10 +3118,7 @@ def corr(x: pdarray, y: pdarray) -> np.float64:
cov(x, y) / (x.std(ddof=1) * y.std(ddof=1))
"""
return parse_single_value(
generic_msg(
cmd=f"corr<{x.dtype},{x.ndim},{y.dtype},{y.ndim}>",
args={"x": x, "y": y}
)
generic_msg(cmd=f"corr<{x.dtype},{x.ndim},{y.dtype},{y.ndim}>", args={"x": x, "y": y})
)


Expand Down
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ testpaths =
tests/array_api/stats_functions.py
tests/array_api/util_functions.py
tests/alignment_test.py
tests/array_view_test.py
tests/bigint_agg_test.py
tests/bitops_test.py
tests/categorical_test.py
Expand All @@ -37,6 +36,7 @@ testpaths =
tests/numpy/numpy_test.py
tests/operator_test.py
tests/pdarray_creation_test.py
tests/pdarrayclass_test.py
tests/random_test.py
tests/regex_test.py
tests/scipy/scipy_test.py
Expand Down
141 changes: 0 additions & 141 deletions tests/array_view_test.py

This file was deleted.

1 change: 1 addition & 0 deletions tests/io_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1746,6 +1746,7 @@ def test_unsanitized_dataset_names(self, hdf_test_base_tmp):
ak.to_hdf(my_arrays, f"{tmp_dirname}/bad_dataset_names")
ak.read_hdf(f"{tmp_dirname}/bad_dataset_names*")


def test_hdf_groupby(self, hdf_test_base_tmp):
# test for categorical and multiple keys
string = ak.array(["a", "b", "a", "b", "c"])
Expand Down
13 changes: 13 additions & 0 deletions tests/pdarrayclass_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest

import arkouda as ak


class TestPdarrayClass:

@pytest.mark.skip_if_max_rank_less_than(3)
def test_reshape(self):
a = ak.arange(4)
r = a.reshape((2, 2))
assert r.shape == [2, 2]
assert isinstance(r, ak.pdarray)

0 comments on commit 2205c45

Please sign in to comment.