Skip to content
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

Print dpnp array #1279

Merged
merged 6 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions dpnp/dpnp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,10 @@ def __radd__(self, other):
# '__rdivmod__',
# '__reduce__',
# '__reduce_ex__',
# '__repr__',

def __repr__(self):
return dpt.usm_ndarray_repr(self._array_obj, prefix="array")

# '__rfloordiv__',
# '__rlshift__',

Expand Down Expand Up @@ -292,8 +295,7 @@ def __str__(self):

"""

return str(self.asnumpy())

return self._array_obj.__str__()
antonwolfy marked this conversation as resolved.
Show resolved Hide resolved

def __sub__(self, other):
return dpnp.subtract(self, other)
Expand Down
106 changes: 106 additions & 0 deletions tests/test_dparray.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,109 @@ def test_flags_strides(dtype, order, strides):
assert usm_array.flags == dpnp_array.flags
assert numpy_array.flags.c_contiguous == dpnp_array.flags.c_contiguous
assert numpy_array.flags.f_contiguous == dpnp_array.flags.f_contiguous

def test_print_dpnp_int():
result = repr(dpnp.array([1, 0, 2, -3, -1, 2, 21, -9], dtype='i4'))
expected = "array([ 1, 0, 2, -3, -1, 2, 21, -9], dtype=int32)"
assert(result==expected)

result = str(dpnp.array([1, 0, 2, -3, -1, 2, 21, -9], dtype='i4'))
expected = "[ 1 0 2 -3 -1 2 21 -9]"
assert(result==expected)
# int32
result = repr(dpnp.array([1, -1, 21], dtype=dpnp.int32))
expected = "array([ 1, -1, 21], dtype=int32)"
assert(result==expected)

result = str(dpnp.array([1, -1, 21], dtype=dpnp.int32))
expected = "[ 1 -1 21]"
assert(result==expected)
# uint8
result = repr(dpnp.array([1, 0, 3], dtype=numpy.uint8))
expected = "array([1, 0, 3], dtype=uint8)"
assert(result==expected)

result = str(dpnp.array([1, 0, 3], dtype=numpy.uint8))
expected = "[1 0 3]"
assert(result==expected)

def test_print_dpnp_float():
result = repr(dpnp.array([1, -1, 21], dtype=float))
expected = "array([ 1., -1., 21.])"
assert(result==expected)

result = str(dpnp.array([1, -1, 21], dtype=float))
expected = "[ 1. -1. 21.]"
assert(result==expected)
# float32
result = repr(dpnp.array([1, -1, 21], dtype=dpnp.float32))
expected = "array([ 1., -1., 21.], dtype=float32)"
assert(result==expected)

result = str(dpnp.array([1, -1, 21], dtype=dpnp.float32))
expected = "[ 1. -1. 21.]"
assert(result==expected)

def test_print_dpnp_complex():
result = repr(dpnp.array([1, -1, 21], dtype=complex))
expected = "array([ 1.+0.j, -1.+0.j, 21.+0.j])"
assert(result==expected)

result = str(dpnp.array([1, -1, 21], dtype=complex))
expected = "[ 1.+0.j -1.+0.j 21.+0.j]"
assert(result==expected)

def test_print_dpnp_boolean():
result = repr(dpnp.array([1, 0, 3], dtype=bool))
expected = "array([ True, False, True])"
assert(result==expected)

result = str(dpnp.array([1, 0, 3], dtype=bool))
expected = "[ True False True]"
assert(result==expected)

def test_print_dpnp_special_character():
# NaN
result = repr(dpnp.array([1., 0., dpnp.nan, 3.]))
expected = "array([ 1., 0., nan, 3.])"
assert(result==expected)

result = str(dpnp.array([1., 0., dpnp.nan, 3.]))
expected = "[ 1. 0. nan 3.]"
assert(result==expected)
# inf
result = repr(dpnp.array([1., 0., numpy.inf, 3.]))
expected = "array([ 1., 0., inf, 3.])"
assert(result==expected)

result = str(dpnp.array([1., 0., numpy.inf, 3.]))
expected = "[ 1. 0. inf 3.]"
assert(result==expected)

def test_print_dpnp_nd():
# 1D
result = repr(dpnp.arange(10000, dtype='float32'))
expected = "array([0.000e+00, 1.000e+00, 2.000e+00, ..., 9.997e+03, 9.998e+03,\n 9.999e+03], dtype=float32)"
assert(result==expected)

result = str(dpnp.arange(10000, dtype='float32'))
expected = "[0.000e+00 1.000e+00 2.000e+00 ... 9.997e+03 9.998e+03 9.999e+03]"
assert(result==expected)

# 2D
result = repr(dpnp.array([[1, 2], [3, 4]], dtype=float))
expected = "array([[1., 2.],\n [3., 4.]])"
assert(result==expected)

result = str(dpnp.array([[1, 2], [3, 4]]))
expected = "[[1 2]\n [3 4]]"
assert(result==expected)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably to add also a test case with ... in the string output.

# 0 shape
result = repr(dpnp.empty( shape=(0, 0) ))
expected = "array([])"
assert(result==expected)

result = str(dpnp.empty( shape=(0, 0) ))
expected = "[]"
assert(result==expected)