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

dpnp.unique returns reshaped unique_inverse when axis is None #1999

Merged
merged 23 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0fa741e
Remove alias on numpy.complex_
antonwolfy Aug 13, 2024
b9d0acc
Remove alias on numpy.cfloat
antonwolfy Aug 13, 2024
f79ea50
Remove alias on numpy.float and numpy.float_
antonwolfy Aug 13, 2024
b8a85d7
Remove alias on numpy.float and numpy.singlecomplex
antonwolfy Aug 13, 2024
8e8c469
Remove aliases on numpy.Inf, numpy.Infinity and numpy.infty constants
antonwolfy Aug 13, 2024
ba74450
Get rid of dpnp.float in the code
antonwolfy Aug 13, 2024
654be28
Merge branch 'master' into allow-import-numpy-2.0
antonwolfy Aug 18, 2024
f9e7bd3
Remove aliases on numpy.NAN and numpy.NaN constants
antonwolfy Aug 18, 2024
9970a32
Remove aliases on numpy.NINF and numpy.PINF constants
antonwolfy Aug 18, 2024
bd93552
Remove aliases on numpy.NZERO and numpy.PZERO constants
antonwolfy Aug 18, 2024
ea0704b
Remove use of removed numpy.longcomplex dtype
antonwolfy Aug 18, 2024
4416544
Use proper import of numpy exceptions
antonwolfy Aug 18, 2024
c177299
Applied pre-commit hooks
antonwolfy Aug 18, 2024
4d43d84
Remove dpnp.issubsctype per numpy 2.0 migration guide
antonwolfy Aug 18, 2024
9ec5bcd
Skip tests for asfarray with numpy 2.0
antonwolfy Aug 18, 2024
21ef55e
Mute CFD relating tests
antonwolfy Aug 18, 2024
f2facd7
Applied pre-commit hooks
antonwolfy Aug 18, 2024
fffa78d
Add skip fixture in test_arraymanipulation.py::test_asfarray2
antonwolfy Aug 18, 2024
4d9856c
Mute tests for new umaths from numpy 2.0
antonwolfy Aug 18, 2024
6870076
dpnp.unique returns reshaped unique_inverse
antonwolfy Aug 18, 2024
005421e
Merge branch 'master' into align-unique-with-numpy-2.0
antonwolfy Aug 19, 2024
72bc2cc
Corrected note sentense
antonwolfy Aug 19, 2024
ba7e8ce
Update test_sycl_queue.py::test_unique
antonwolfy Aug 19, 2024
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
4 changes: 1 addition & 3 deletions doc/reference/dtype.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ Data type testing
:toctree: generated/
:nosignatures:

dpnp.issctype
dpnp.isdtype
dpnp.issubdtype
dpnp.issubsctype
dpnp.issubclass_

Miscellaneous
-------------
Expand Down
16 changes: 2 additions & 14 deletions doc/reference/dtypes_table.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,23 @@ Table below shows a list of all supported data types (dtypes) and constants of t
- :obj:`bool <numpy.bool_>`
- :obj:`int32 <numpy.int32>`
- :obj:`int64 <numpy.int64>`
- :obj:`float32 <numpy.float32>`
- :obj:`float64 <numpy.float64>`
- :obj:`complex64 <numpy.complex64>`
- :obj:`complex128 <numpy.complex128>`
-
- :obj:`bool_ <numpy.bool_>`
- :obj:`cdouble <numpy.cdouble>`
- :obj:`complex <numpy.complex_>`
- :obj:`cfloat <numpy.cfloat>`
- :obj:`csingle <numpy.csingle>`
- :obj:`double <numpy.double>`
- :obj:`float <numpy.float>`
- :obj:`float_ <numpy.float_>`
- :obj:`float16 <numpy.float16>`
- :obj:`int <numpy.int>`
- :obj:`int_ <numpy.int_>`
- :obj:`intc <numpy.intc>`
- :obj:`single <numpy.single>`
- :obj:`singlecomplex <numpy.singlecomplex>`
-
- :obj:`e <numpy.e>`
- :obj:`euler_gamma <numpy.euler_gamma>`
- :obj:`Inf <numpy.Inf>`
- :obj:`inf <numpy.inf>`
- :obj:`Infinity <numpy.Infinity>`
- :obj:`infty <numpy.infty>`
- :obj:`NAN <numpy.NAN>`
- :obj:`NaN <numpy.NaN>`
- :obj:`nan <numpy.nan>`
- :obj:`NINF <numpy.NINF>`
- :obj:`NZERO <numpy.NZERO>`
- :obj:`pi <numpy.pi>`
- :obj:`PINF <numpy.PINF>`
- :obj:`PZERO <numpy.PZERO>`
8 changes: 4 additions & 4 deletions dpnp/dpnp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ def conj(self):

"""

if not dpnp.issubsctype(self.dtype, dpnp.complexfloating):
if not dpnp.issubdtype(self.dtype, dpnp.complexfloating):
return self
else:
return dpnp.conjugate(self)
Expand All @@ -691,7 +691,7 @@ def conjugate(self):

"""

if not dpnp.issubsctype(self.dtype, dpnp.complexfloating):
if not dpnp.issubdtype(self.dtype, dpnp.complexfloating):
return self
else:
return dpnp.conjugate(self)
Expand Down Expand Up @@ -965,7 +965,7 @@ def imag(self, value):
array([1.+9.j, 3.+9.j, 5.+9.j])

"""
if dpnp.issubsctype(self.dtype, dpnp.complexfloating):
if dpnp.issubdtype(self.dtype, dpnp.complexfloating):
dpnp.copyto(self._array_obj.imag, value)
else:
raise TypeError("array does not have imaginary part to set")
Expand Down Expand Up @@ -1191,7 +1191,7 @@ def real(self):

"""

if dpnp.issubsctype(self.dtype, dpnp.complexfloating):
if dpnp.issubdtype(self.dtype, dpnp.complexfloating):
return dpnp_array._create_from_usm_ndarray(
dpnp.get_usm_ndarray(self).real
)
Expand Down
7 changes: 5 additions & 2 deletions dpnp/dpnp_iface_arraycreation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3266,7 +3266,7 @@ def tri(
/,
M=None,
k=0,
dtype=dpnp.float,
dtype=float,
*,
device=None,
usm_type="device",
Expand Down Expand Up @@ -3376,7 +3376,10 @@ def tri(
if _k is None:
raise TypeError(f"`k` must be a integer data type, but got {type(k)}")

_dtype = dpnp.default_float_type() if dtype in (dpnp.float, None) else dtype
sycl_dev = dpnp.get_normalized_queue_device(
sycl_queue=sycl_queue, device=device
).sycl_device
_dtype = map_dtype_to_device(dtype, sycl_dev)

if usm_type is None:
usm_type = "device"
Expand Down
2 changes: 1 addition & 1 deletion dpnp/dpnp_iface_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ def isclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False):
_b += atol
result = less_equal(dpnp.abs(a - b), _b)

# Handle "Inf" values: they are treated as equal if they are in the same
# Handle "inf" values: they are treated as equal if they are in the same
# place and of the same sign in both arrays
result &= isfinite(b)
result |= a == b
Expand Down
9 changes: 8 additions & 1 deletion dpnp/dpnp_iface_manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2229,6 +2229,14 @@ def unique(
This is done by making the specified axis the first dimension of the array
(move the axis to the first dimension to keep the order of the other axes)
and then flattening the subarrays in C order.
If NaN values are in the input array, a single NaN is put to the end of the
antonwolfy marked this conversation as resolved.
Show resolved Hide resolved
sorted unique values.
Also for complex arrays all NaN values are considered equivalent (no matter
whether the NaN is in the real or imaginary part). As the representant for
the returned array the smallest one in the lexicographical order is chosen.
For multi-dimensional inputs, `unique_inverse` is reshaped such that the
input can be reconstructed using
``dpnp.take(unique, unique_inverse, axis=axis)``.

Examples
--------
Expand Down Expand Up @@ -2272,7 +2280,6 @@ def unique(
"""

if axis is None:
ar = dpnp.ravel(ar)
return _unique_1d(
ar, return_index, return_inverse, return_counts, equal_nan
)
Expand Down
4 changes: 2 additions & 2 deletions dpnp/dpnp_iface_mathematical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2295,7 +2295,7 @@ def gradient(f, *varargs, axis=None, edge_order=1):
>>> np.maximum(x1, x2)
array([nan, nan, nan])

>>> np.maximum(np.array(np.Inf), 1)
>>> np.maximum(np.array(np.inf), 1)
array(inf)
"""

Expand Down Expand Up @@ -2375,7 +2375,7 @@ def gradient(f, *varargs, axis=None, edge_order=1):
>>> np.minimum(x1, x2)
array([nan, nan, nan])

>>> np.minimum(np.array(-np.Inf), 1)
>>> np.minimum(np.array(-np.inf), 1)
array(-inf)
"""

Expand Down
6 changes: 3 additions & 3 deletions dpnp/dpnp_iface_nanfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ def nanmax(a, axis=None, out=None, keepdims=False, initial=None, where=True):

When positive infinity and negative infinity are present:

>>> np.nanmax(np.array([1, 2, np.nan, np.NINF]))
>>> np.nanmax(np.array([1, 2, np.nan, -np.inf]))
array(2.)
>>> np.nanmax(np.array([1, 2, np.nan, np.inf]))
array(inf)
Expand Down Expand Up @@ -639,7 +639,7 @@ def nanmin(a, axis=None, out=None, keepdims=False, initial=None, where=True):

>>> np.nanmin(np.array([1, 2, np.nan, np.inf]))
array(1.)
>>> np.nanmin(np.array([1, 2, np.nan, np.NINF]))
>>> np.nanmin(np.array([1, 2, np.nan, -np.inf]))
array(-inf)

"""
Expand Down Expand Up @@ -831,7 +831,7 @@ def nansum(
array([2., 1.])
>>> np.nansum(np.array([1, np.nan, np.inf]))
array(inf)
>>> np.nansum(np.array([1, np.nan, np.NINF]))
>>> np.nansum(np.array([1, np.nan, -np.inf]))
array(-inf)
>>> # both +/- infinity present
>>> np.nansum(np.array([1, np.nan, np.inf, -np.inf]))
Expand Down
2 changes: 1 addition & 1 deletion dpnp/dpnp_iface_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def sort_complex(a):
"""

b = dpnp.sort(a)
if not dpnp.issubsctype(b.dtype, dpnp.complexfloating):
if not dpnp.issubdtype(b.dtype, dpnp.complexfloating):
if b.dtype.char in "bhBH":
b_dt = dpnp.complex64
else:
Expand Down
4 changes: 2 additions & 2 deletions dpnp/dpnp_iface_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ def max(a, axis=None, out=None, keepdims=False, initial=None, where=True):
array([1, 3])

>>> b = np.arange(5, dtype=float)
>>> b[2] = np.NaN
>>> b[2] = np.nan
>>> np.max(b)
array(nan)

Expand Down Expand Up @@ -736,7 +736,7 @@ def min(a, axis=None, out=None, keepdims=False, initial=None, where=True):
array([0, 2])

>>> b = np.arange(5, dtype=float)
>>> b[2] = np.NaN
>>> b[2] = np.nan
>>> np.min(b)
array(nan)

Expand Down
40 changes: 0 additions & 40 deletions dpnp/dpnp_iface_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,22 @@
"bool",
"bool_",
"cdouble",
"complex_",
"complex128",
"complex64",
"complexfloating",
"cfloat",
"csingle",
"double",
"dtype",
"e",
"euler_gamma",
"finfo",
"float",
"float_",
"float16",
"float32",
"float64",
"floating",
"iinfo",
"inexact",
"Inf",
"inf",
"Infinity",
"infty",
"int",
"int_",
"int32",
Expand All @@ -72,21 +65,13 @@
"intc",
"intp",
"issubdtype",
"issubsctype",
"is_type_supported",
"NAN",
"NaN",
"nan",
"newaxis",
"NINF",
"NZERO",
"number",
"pi",
"PINF",
"PZERO",
"signedinteger",
"single",
"singlecomplex",
]


Expand All @@ -97,16 +82,12 @@
bool = numpy.bool_
bool_ = numpy.bool_
cdouble = numpy.cdouble
complex_ = numpy.complex_
complex128 = numpy.complex128
complex64 = numpy.complex64
complexfloating = numpy.complexfloating
cfloat = numpy.cfloat
csingle = numpy.csingle
double = numpy.double
dtype = numpy.dtype
float = numpy.float_
float_ = numpy.float_
float16 = numpy.float16
float32 = numpy.float32
float64 = numpy.float64
Expand All @@ -122,27 +103,17 @@
number = numpy.number
signedinteger = numpy.signedinteger
single = numpy.single
singlecomplex = numpy.singlecomplex


# =============================================================================
# Constants (borrowed from NumPy)
# =============================================================================
e = numpy.e
euler_gamma = numpy.euler_gamma
Inf = numpy.Inf
inf = numpy.inf
Infinity = numpy.Infinity
infty = numpy.infty
NAN = numpy.NAN
NaN = numpy.NaN
nan = numpy.nan
newaxis = None
NINF = numpy.NINF
NZERO = numpy.NZERO
pi = numpy.pi
PINF = numpy.PINF
PZERO = numpy.PZERO


# pylint: disable=redefined-outer-name
Expand Down Expand Up @@ -240,17 +211,6 @@ def issubdtype(arg1, arg2):
return numpy.issubdtype(arg1, arg2)


def issubsctype(arg1, arg2):
"""
Determine if the first argument is a subclass of the second argument.

For full documentation refer to :obj:`numpy.issubsctype`.

"""

return numpy.issubsctype(arg1, arg2)


def is_type_supported(obj_type):
"""Return True if type is supported by DPNP python level."""

Expand Down
2 changes: 1 addition & 1 deletion dpnp/linalg/dpnp_utils_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ def _multi_dot_matrix_chain_order(n, arrays, return_costs=False):
for ll in range(1, n):
for i in range(n - ll):
j = i + ll
m[i, j] = dpnp.Inf
m[i, j] = dpnp.inf
for k in range(i, j):
q = m[i, k] + m[k + 1, j] + p[i] * p[k + 1] * p[j + 1]
if q < m[i, j]:
Expand Down
7 changes: 6 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
import numpy
import pytest

if numpy.lib.NumpyVersion(numpy.__version__) >= "2.0.0b1":
from numpy.exceptions import ComplexWarning
else:
from numpy import ComplexWarning

import dpnp

skip_mark = pytest.mark.skip(reason="Skipping test.")
Expand Down Expand Up @@ -101,7 +106,7 @@ def allow_fall_back_on_numpy(monkeypatch):
@pytest.fixture
def suppress_complex_warning():
sup = numpy.testing.suppress_warnings("always")
sup.filter(numpy.ComplexWarning)
sup.filter(ComplexWarning)
with sup:
yield

Expand Down
3 changes: 3 additions & 0 deletions tests/test_arraymanipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
)

import dpnp
from tests.third_party.cupy import testing

from .helper import get_all_dtypes, get_float_complex_dtypes

Expand Down Expand Up @@ -832,6 +833,7 @@ def test_generator(self):
dpnp.vstack(map(lambda x: x, dpnp.ones((3, 2))))


@testing.with_requires("numpy<2.0")
@pytest.mark.parametrize("dtype", get_all_dtypes())
@pytest.mark.parametrize(
"data", [[1, 2, 3], [1.0, 2.0, 3.0]], ids=["[1, 2, 3]", "[1., 2., 3.]"]
Expand All @@ -843,6 +845,7 @@ def test_asfarray(dtype, data):
assert_array_equal(result, expected)


@testing.with_requires("numpy<2.0")
@pytest.mark.usefixtures("suppress_complex_warning")
@pytest.mark.parametrize("dtype", get_all_dtypes())
@pytest.mark.parametrize("data", [[1.0, 2.0, 3.0]], ids=["[1., 2., 3.]"])
Expand Down
Loading
Loading