Skip to content

Commit

Permalink
Closes Bears-R-Us#3714: pdarray.shape should be a tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
ajpotts committed Oct 1, 2024
1 parent b90155f commit 3b3c153
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
16 changes: 14 additions & 2 deletions arkouda/pdarrayclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def __init__(
self.dtype = dtype(mydtype)
self.size = size
self.ndim = ndim
self.shape = shape
self._shape = tuple(shape)
self.itemsize = itemsize
if max_bits:
self.max_bits = max_bits
Expand Down Expand Up @@ -399,6 +399,18 @@ def __repr__(self):

return generic_msg(cmd="repr", args={"array": self, "printThresh": pdarrayIterThresh})

@property
def shape(self):
"""
Return the shape of an array.
Returns
-------
tuple of int
The elements of the shape tuple give the lengths of the corresponding array dimensions.
"""
return tuple(self._shape)

@property
def max_bits(self):
if self.dtype == bigint:
Expand Down Expand Up @@ -962,7 +974,7 @@ def __getitem__(self, key):

# use 'None' values in the original key to expand the dimensions
shape = []
rs = ret_array.shape
rs = list(ret_array.shape)
for k in key_with_none:
if k is None:
shape.append(1)
Expand Down
16 changes: 8 additions & 8 deletions tests/pdarray_creation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_array_creation_multi_dim(self, size, dtype):
ak.array(np.ones(shape), dtype),
]:
assert isinstance(pda, ak.pdarray)
assert pda.shape == list(shape)
assert pda.shape == shape
assert dtype == pda.dtype

@pytest.mark.skip_if_max_rank_greater_than(3)
Expand Down Expand Up @@ -248,7 +248,7 @@ def test_randint_array_dtype(self, size, array_type):
assert isinstance(test_array, ak.pdarray)
assert size == len(test_array)
assert array_type == test_array.dtype
assert [size] == test_array.shape
assert (size,) == test_array.shape
assert ((0 <= test_array) & (test_array <= size)).all()

# (The above function tests randint with various ARRAY dtypes; the function below
Expand All @@ -259,7 +259,7 @@ def test_randint_num_dtype(self, dtype):
assert isinstance(test_array, ak.pdarray)
assert 1000 == len(test_array)
assert ak.int64 == test_array.dtype
assert [1000] == test_array.shape
assert (1000,) == test_array.shape
assert ((0 <= test_array) & (test_array <= 1000)).all()

@pytest.mark.parametrize("size", pytest.prob_size)
Expand Down Expand Up @@ -334,7 +334,7 @@ def test_uniform(self, size):
test_array = ak.uniform(size)
assert isinstance(test_array, ak.pdarray)
assert ak.float64 == test_array.dtype
assert [size] == test_array.shape
assert (size,) == test_array.shape

u_array = ak.uniform(size=3, low=0, high=5, seed=0)
assert [0.30013431967121934, 0.47383036230759112, 1.0441791878997098] == u_array.to_list()
Expand Down Expand Up @@ -372,7 +372,7 @@ def test_zeros_dtype_mult_dim(self, size, dtype):
zeros = ak.zeros(shape, dtype)
assert isinstance(zeros, ak.pdarray)
assert dtype == zeros.dtype
assert zeros.shape == list(shape)
assert zeros.shape == shape
assert (0 == zeros).all()

@pytest.mark.skip_if_max_rank_greater_than(3)
Expand Down Expand Up @@ -412,7 +412,7 @@ def test_ones_dtype_multi_dim(self, size, dtype):
shape = (2, 2, size)
ones = ak.ones(shape, dtype)
assert isinstance(ones, ak.pdarray)
assert ones.shape == list(shape)
assert ones.shape == shape
assert dtype == ones.dtype
assert (1 == ones).all()

Expand Down Expand Up @@ -464,7 +464,7 @@ def test_full_dtype_multi_dim(self, size, dtype):
type_full = ak.full(shape, 1, dtype)
assert isinstance(type_full, ak.pdarray)
assert dtype == type_full.dtype
assert type_full.shape == list(shape)
assert type_full.shape == shape
assert (1 == type_full).all()

@pytest.mark.skip_if_max_rank_greater_than(3)
Expand Down Expand Up @@ -737,7 +737,7 @@ def test_random_strings_lognormal_with_seed(self):
def test_mulitdimensional_array_creation(self):
a = ak.array([[0, 0], [0, 1], [1, 1]])
assert isinstance(a, ak.pdarray)
assert a.shape == [3, 2]
assert a.shape == (3, 2)

@pytest.mark.parametrize("size", pytest.prob_size)
@pytest.mark.parametrize("dtype", [bool, np.float64, np.int64, str])
Expand Down
18 changes: 16 additions & 2 deletions tests/pdarrayclass_test.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import pytest

import arkouda as ak
import numpy as np


class TestPdarrayClass:

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

def test_shape(self):
a = ak.arange(4)
np_a = np.arange(4)
assert isinstance(a.shape, tuple)
assert a.shape == np_a.shape

@pytest.mark.skip_if_max_rank_less_than(2)
def test_shape_multidim(self):
a = ak.arange(4).reshape((2,2))
np_a = np.arange(4).reshape((2,2))
assert isinstance(a.shape, tuple)
assert a.shape == np_a.shape

0 comments on commit 3b3c153

Please sign in to comment.