Skip to content
/ cudf Public
forked from rapidsai/cudf

Commit

Permalink
Move assert_eq to testing submodule
Browse files Browse the repository at this point in the history
This way, importing it won't require pytest.

- Closes rapidsai#16062
  • Loading branch information
wence- committed Jun 24, 2024
1 parent c83e5b3 commit 35223e9
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 80 deletions.
2 changes: 1 addition & 1 deletion python/cudf/cudf/pandas/fast_slow_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import numpy as np

from ..options import _env_get_bool
from ..testing._utils import assert_eq
from ..testing.testing import assert_eq
from .annotation import nvtx


Expand Down
4 changes: 3 additions & 1 deletion python/cudf/cudf/testing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Copyright (c) 2020, NVIDIA CORPORATION.
# Copyright (c) 2020-2024, NVIDIA CORPORATION.

from cudf.testing.testing import (
assert_eq,
assert_frame_equal,
assert_index_equal,
assert_neq,
assert_series_equal,
)
79 changes: 1 addition & 78 deletions python/cudf/cudf/testing/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,24 @@

import itertools
import string
import warnings
from collections import abc
from contextlib import contextmanager
from decimal import Decimal

import cupy
import numpy as np
import pandas as pd
import pytest
from numba.core.typing import signature as nb_signature
from numba.core.typing.templates import AbstractTemplate
from numba.cuda.cudadecl import registry as cuda_decl_registry
from numba.cuda.cudaimpl import lower as cuda_lower
from pandas import testing as tm

import cudf
from cudf._lib.null_mask import bitmask_allocation_size_bytes
from cudf.core.column.timedelta import _unit_to_nanoseconds_conversion
from cudf.core.udf.strings_lowering import cast_string_view_to_udf_string
from cudf.core.udf.strings_typing import StringView, string_view, udf_string
from cudf.testing.testing import assert_eq, assert_neq # noqa: F401
from cudf.utils import dtypes as dtypeutils

supported_numpy_dtypes = [
Expand Down Expand Up @@ -113,81 +111,6 @@ def count_zero(arr):
return np.count_nonzero(arr == 0)


def assert_eq(left, right, **kwargs):
"""Assert that two cudf-like things are equivalent
This equality test works for pandas/cudf dataframes/series/indexes/scalars
in the same way, and so makes it easier to perform parametrized testing
without switching between assert_frame_equal/assert_series_equal/...
functions.
"""
# dtypes that we support but Pandas doesn't will convert to
# `object`. Check equality before that happens:
if kwargs.get("check_dtype", True):
if hasattr(left, "dtype") and hasattr(right, "dtype"):
if isinstance(
left.dtype, cudf.core.dtypes._BaseDtype
) and not isinstance(
left.dtype, cudf.CategoricalDtype
): # leave categorical comparison to Pandas
assert_eq(left.dtype, right.dtype)

if hasattr(left, "to_pandas"):
left = left.to_pandas()
if hasattr(right, "to_pandas"):
right = right.to_pandas()
if isinstance(left, cupy.ndarray):
left = cupy.asnumpy(left)
if isinstance(right, cupy.ndarray):
right = cupy.asnumpy(right)

if isinstance(left, (pd.DataFrame, pd.Series, pd.Index)):
# TODO: A warning is emitted from the function
# pandas.testing.assert_[series, frame, index]_equal for some inputs:
# "DeprecationWarning: elementwise comparison failed; this will raise
# an error in the future."
# or "FutureWarning: elementwise ..."
# This warning comes from a call from pandas to numpy. It is ignored
# here because it cannot be fixed within cudf.
with warnings.catch_warnings():
warnings.simplefilter(
"ignore", (DeprecationWarning, FutureWarning)
)
if isinstance(left, pd.DataFrame):
tm.assert_frame_equal(left, right, **kwargs)
elif isinstance(left, pd.Series):
tm.assert_series_equal(left, right, **kwargs)
else:
tm.assert_index_equal(left, right, **kwargs)

elif isinstance(left, np.ndarray) and isinstance(right, np.ndarray):
if np.issubdtype(left.dtype, np.floating) and np.issubdtype(
right.dtype, np.floating
):
assert np.allclose(left, right, equal_nan=True)
else:
assert np.array_equal(left, right)
else:
# Use the overloaded __eq__ of the operands
if left == right:
return True
elif any(np.issubdtype(type(x), np.floating) for x in (left, right)):
np.testing.assert_almost_equal(left, right)
else:
np.testing.assert_equal(left, right)
return True


def assert_neq(left, right, **kwargs):
__tracebackhide__ = True
try:
assert_eq(left, right, **kwargs)
except AssertionError:
pass
else:
raise AssertionError


def assert_exceptions_equal(
lfunc,
rfunc,
Expand Down
78 changes: 78 additions & 0 deletions python/cudf/cudf/testing/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

from __future__ import annotations

import warnings

import cupy as cp
import numpy as np
import pandas as pd
from pandas import testing as tm

import cudf
from cudf._lib.unary import is_nan
Expand Down Expand Up @@ -708,3 +711,78 @@ def assert_frame_equal(
atol=atol,
obj=f'Column name="{col}"',
)


def assert_eq(left, right, **kwargs):
"""Assert that two cudf-like things are equivalent
This equality test works for pandas/cudf dataframes/series/indexes/scalars
in the same way, and so makes it easier to perform parametrized testing
without switching between assert_frame_equal/assert_series_equal/...
functions.
"""
# dtypes that we support but Pandas doesn't will convert to
# `object`. Check equality before that happens:
if kwargs.get("check_dtype", True):
if hasattr(left, "dtype") and hasattr(right, "dtype"):
if isinstance(
left.dtype, cudf.core.dtypes._BaseDtype
) and not isinstance(
left.dtype, cudf.CategoricalDtype
): # leave categorical comparison to Pandas
assert_eq(left.dtype, right.dtype)

if hasattr(left, "to_pandas"):
left = left.to_pandas()
if hasattr(right, "to_pandas"):
right = right.to_pandas()
if isinstance(left, cp.ndarray):
left = cp.asnumpy(left)
if isinstance(right, cp.ndarray):
right = cp.asnumpy(right)

if isinstance(left, (pd.DataFrame, pd.Series, pd.Index)):
# TODO: A warning is emitted from the function
# pandas.testing.assert_[series, frame, index]_equal for some inputs:
# "DeprecationWarning: elementwise comparison failed; this will raise
# an error in the future."
# or "FutureWarning: elementwise ..."
# This warning comes from a call from pandas to numpy. It is ignored
# here because it cannot be fixed within cudf.
with warnings.catch_warnings():
warnings.simplefilter(
"ignore", (DeprecationWarning, FutureWarning)
)
if isinstance(left, pd.DataFrame):
tm.assert_frame_equal(left, right, **kwargs)
elif isinstance(left, pd.Series):
tm.assert_series_equal(left, right, **kwargs)
else:
tm.assert_index_equal(left, right, **kwargs)

elif isinstance(left, np.ndarray) and isinstance(right, np.ndarray):
if np.issubdtype(left.dtype, np.floating) and np.issubdtype(
right.dtype, np.floating
):
assert np.allclose(left, right, equal_nan=True)
else:
assert np.array_equal(left, right)
else:
# Use the overloaded __eq__ of the operands
if left == right:
return True
elif any(np.issubdtype(type(x), np.floating) for x in (left, right)):
np.testing.assert_almost_equal(left, right)
else:
np.testing.assert_equal(left, right)
return True


def assert_neq(left, right, **kwargs):
__tracebackhide__ = True
try:
assert_eq(left, right, **kwargs)
except AssertionError:
pass
else:
raise AssertionError

0 comments on commit 35223e9

Please sign in to comment.