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

fast_slow_proxy: Don't import assert_eq at top-level #16063

Merged
merged 8 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions docs/cudf/source/user_guide/api_docs/general_utilities.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Testing functions
:toctree: api/

cudf.testing.testing.assert_column_equal
cudf.testing.testing.assert_eq
cudf.testing.testing.assert_frame_equal
cudf.testing.testing.assert_index_equal
cudf.testing.testing.assert_neq
cudf.testing.testing.assert_series_equal
4 changes: 2 additions & 2 deletions python/cudf/cudf/_fuzz_testing/tests/fuzz_test_csv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020-2023, NVIDIA CORPORATION.
# Copyright (c) 2020-2024, NVIDIA CORPORATION.

import sys
from io import StringIO
Expand All @@ -13,7 +13,7 @@
compare_content,
run_test,
)
from cudf.testing._utils import assert_eq
from cudf.testing import assert_eq


@pythonfuzz(data_handle=CSVReader)
Expand Down
4 changes: 2 additions & 2 deletions python/cudf/cudf/_fuzz_testing/tests/fuzz_test_json.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020, NVIDIA CORPORATION.
# Copyright (c) 2020-2024, NVIDIA CORPORATION.

import io
import sys
Expand All @@ -9,7 +9,7 @@
from cudf._fuzz_testing.json import JSONReader, JSONWriter
from cudf._fuzz_testing.main import pythonfuzz
from cudf._fuzz_testing.utils import ALL_POSSIBLE_VALUES, run_test
from cudf.testing._utils import assert_eq
from cudf.testing import assert_eq


@pythonfuzz(data_handle=JSONReader)
Expand Down
2 changes: 1 addition & 1 deletion python/cudf/cudf/_fuzz_testing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pyarrow as pa

import cudf
from cudf.testing._utils import assert_eq
from cudf.testing import assert_eq
from cudf.utils.dtypes import (
pandas_dtypes_to_np_dtypes,
pyarrow_dtypes_to_pandas_dtypes,
Expand Down
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 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,
)
78 changes: 0 additions & 78 deletions python/cudf/cudf/testing/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@

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
Expand Down Expand Up @@ -113,81 +110,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
100 changes: 100 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,100 @@ 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

Parameters
----------
left
Object to compare
right
Object to compare
kwargs
Keyword arguments to control behaviour of comparisons. See
:func:`assert_frame_equal`, :func:`assert_series_equal`, and
:func:`assert_index_equal`.

Notes
-----
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.

Raises
------
AssertionError
If the two objects do not compare equal.
"""
# 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):
"""Assert that two cudf-like things are not equal.

Provides the negation of the meaning of :func:`assert_eq`.
"""
__tracebackhide__ = True
try:
assert_eq(left, right, **kwargs)
except AssertionError:
pass
else:
raise AssertionError
4 changes: 2 additions & 2 deletions python/cudf/cudf/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2022, NVIDIA CORPORATION.
# Copyright (c) 2019-2024, NVIDIA CORPORATION.

import itertools
import os
Expand All @@ -11,7 +11,7 @@
import rmm # noqa: F401

import cudf
from cudf.testing._utils import assert_eq
from cudf.testing import assert_eq

_CURRENT_DIRECTORY = str(pathlib.Path(__file__).resolve().parent)

Expand Down
4 changes: 2 additions & 2 deletions python/cudf/cudf/tests/dataframe/test_conversion.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Copyright (c) 2023, NVIDIA CORPORATION.
# Copyright (c) 2023-2024, NVIDIA CORPORATION.
import pandas as pd
import pytest

import cudf
from cudf.testing._utils import assert_eq
from cudf.testing import assert_eq


def test_convert_dtypes():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pytest

import cudf
from cudf.testing._utils import assert_eq
from cudf.testing import assert_eq


@pytest.mark.parametrize(
Expand Down
4 changes: 2 additions & 2 deletions python/cudf/cudf/tests/groupby/test_computation.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Copyright (c) 2023, NVIDIA CORPORATION.
# Copyright (c) 2023-2024, NVIDIA CORPORATION.
import pandas as pd
import pytest

import cudf
from cudf.testing._utils import assert_eq
from cudf.testing import assert_eq


@pytest.mark.parametrize("method", ["average", "min", "max", "first", "dense"])
Expand Down
2 changes: 1 addition & 1 deletion python/cudf/cudf/tests/groupby/test_groupby_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from numpy.testing import assert_array_equal

import cudf
from cudf.testing._utils import assert_eq
from cudf.testing import assert_eq


def test_groupby_14955():
Expand Down
2 changes: 1 addition & 1 deletion python/cudf/cudf/tests/groupby/test_indexing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (c) 2023-2024, NVIDIA CORPORATION.
import cudf
from cudf.testing._utils import assert_eq
from cudf.testing import assert_eq


def test_rank_return_type_compatible_mode():
Expand Down
2 changes: 1 addition & 1 deletion python/cudf/cudf/tests/groupby/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest

import cudf
from cudf.testing._utils import assert_eq
from cudf.testing import assert_eq


@pytest.fixture(params=[False, True], ids=["no-null-keys", "null-keys"])
Expand Down
2 changes: 1 addition & 1 deletion python/cudf/cudf/tests/indexes/datetime/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pandas as pd

import cudf
from cudf.testing._utils import assert_eq
from cudf.testing import assert_eq


def test_slice_datetimetz_index():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pandas as pd

import cudf
from cudf.testing._utils import assert_eq
from cudf.testing import assert_eq


def test_tz_localize():
Expand Down
2 changes: 1 addition & 1 deletion python/cudf/cudf/tests/indexes/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import cudf
from cudf.core._compat import PANDAS_CURRENT_SUPPORTED_VERSION, PANDAS_VERSION
from cudf.core.index import IntervalIndex, interval_range
from cudf.testing._utils import assert_eq
from cudf.testing import assert_eq


def test_interval_constructor_default_closed():
Expand Down
4 changes: 2 additions & 2 deletions python/cudf/cudf/tests/input_output/test_text.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Copyright (c) 2019-2023, NVIDIA CORPORATION.
# Copyright (c) 2019-2024, NVIDIA CORPORATION.

from io import StringIO

import pytest

import cudf
from cudf.testing._utils import assert_eq
from cudf.testing import assert_eq


@pytest.fixture(scope="module")
Expand Down
Loading
Loading