Skip to content

Commit 85bb3a5

Browse files
committed
Cleanup.
1 parent 268cd2c commit 85bb3a5

File tree

9 files changed

+32
-33
lines changed

9 files changed

+32
-33
lines changed

python-package/xgboost/testing/__init__.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -511,19 +511,6 @@ def non_decreasing(L: Sequence[float], tolerance: float = 1e-4) -> bool:
511511
return all((y - x) >= -tolerance for x, y in zip(L, L[1:]))
512512

513513

514-
def predictor_equal(lhs: xgb.DMatrix, rhs: xgb.DMatrix) -> bool:
515-
"""Assert whether two DMatrices contain the same predictors."""
516-
lcsr = lhs.get_data()
517-
rcsr = rhs.get_data()
518-
return all(
519-
(
520-
np.array_equal(lcsr.data, rcsr.data),
521-
np.array_equal(lcsr.indices, rcsr.indices),
522-
np.array_equal(lcsr.indptr, rcsr.indptr),
523-
)
524-
)
525-
526-
527514
M = TypeVar("M", xgb.Booster, xgb.XGBModel)
528515

529516

python-package/xgboost/testing/data_iter.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from ..compat import import_cupy
1010
from ..core import DataIter, DMatrix, ExtMemQuantileDMatrix, QuantileDMatrix
11+
from .utils import predictor_equal
1112

1213

1314
def run_mixed_sparsity(device: str) -> None:
@@ -36,7 +37,7 @@ def run_mixed_sparsity(device: str) -> None:
3637
y_arr = np.concatenate(y, axis=0)
3738
Xy_1 = QuantileDMatrix(X_arr, y_arr)
3839

39-
assert tm.predictor_equal(Xy_0, Xy_1)
40+
assert predictor_equal(Xy_0, Xy_1)
4041

4142

4243
def check_invalid_cat_batches(device: str) -> None:

python-package/xgboost/testing/ordinal.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
memory,
2424
)
2525
from .updater import get_basescore
26-
from .utils import Device, assert_allclose
26+
from .utils import Device, assert_allclose, predictor_equal
2727

2828

2929
@fcache
@@ -705,13 +705,7 @@ def run_recode_dmatrix(device: Device) -> None:
705705
assert cats_1 is not None
706706

707707
assert cats_0.to_arrow() == cats_1.to_arrow()
708-
709-
csr_0 = Xy_0.get_data()
710-
csr_1 = Xy_1.get_data()
711-
712-
np.testing.assert_allclose(csr_0.indptr, csr_1.indptr)
713-
np.testing.assert_allclose(csr_0.indices, csr_1.indices)
714-
np.testing.assert_allclose(csr_0.data, csr_1.data)
708+
assert predictor_equal(Xy_0, Xy_1)
715709

716710

717711
def run_training_continuation(device: Device) -> None:

python-package/xgboost/testing/utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import numpy as np
66

77
from ..compat import import_cupy
8+
from ..core import DMatrix
89
from ..data import _is_cupy_alike
910

1011
Device: TypeAlias = Literal["cpu", "cuda"]
@@ -19,3 +20,16 @@ def assert_allclose(
1920
else:
2021
cp = import_cupy()
2122
cp.testing.assert_allclose(a, b, atol=atol, rtol=rtol)
23+
24+
25+
def predictor_equal(lhs: DMatrix, rhs: DMatrix) -> bool:
26+
"""Assert whether two DMatrices contain the same predictors."""
27+
lcsr = lhs.get_data()
28+
rcsr = rhs.get_data()
29+
return all(
30+
(
31+
np.array_equal(lcsr.data, rcsr.data),
32+
np.array_equal(lcsr.indices, rcsr.indices),
33+
np.array_equal(lcsr.indptr, rcsr.indptr),
34+
)
35+
)

tests/python-gpu/test_device_quantile_dmatrix.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
check_categorical_strings,
1313
check_ref_quantile_cut,
1414
)
15+
from xgboost.testing.utils import predictor_equal
1516

1617
sys.path.append("tests/python")
1718
import test_quantile_dmatrix as tqd
@@ -70,7 +71,7 @@ def test_initialization(self, on_device: bool, device: str) -> None:
7071
)
7172
# query cuts from GIDX/Ellpack
7273
qXy = xgb.QuantileDMatrix(X[0], y[0], weight=w[0], max_bin=max_bin, ref=Xy)
73-
tm.predictor_equal(Xy, qXy)
74+
predictor_equal(Xy, qXy)
7475
with pytest.raises(ValueError, match="Inconsistent"):
7576
# max_bin changed.
7677
xgb.QuantileDMatrix(X[0], y[0], weight=w[0], max_bin=max_bin - 1, ref=Xy)
@@ -92,7 +93,7 @@ def test_initialization(self, on_device: bool, device: str) -> None:
9293
)
9394
# query cuts from GIDX/Ellpack
9495
qXy = xgb.QuantileDMatrix(X[0], y[0], weight=w[0], max_bin=max_bin, ref=Xy)
95-
tm.predictor_equal(Xy, qXy)
96+
predictor_equal(Xy, qXy)
9697
with pytest.raises(ValueError, match="Inconsistent"):
9798
# max_bin changed.
9899
xgb.QuantileDMatrix(X[0], y[0], weight=w[0], max_bin=max_bin - 1, ref=Xy)
@@ -247,7 +248,7 @@ def test_ltr(self) -> None:
247248
from_dm = xgb.QuantileDMatrix(X, weight=w, ref=Xy)
248249
from_qdm = xgb.QuantileDMatrix(X, weight=w, ref=Xy_qdm)
249250

250-
assert tm.predictor_equal(from_qdm, from_dm)
251+
assert predictor_equal(from_qdm, from_dm)
251252

252253
@pytest.mark.skipif(**tm.no_cupy())
253254
def test_check_inf(self) -> None:

tests/python/test_dmatrix.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from xgboost import testing as tm
1414
from xgboost.core import DataSplitMode
1515
from xgboost.testing.data import np_dtypes, run_base_margin_info
16+
from xgboost.testing.utils import predictor_equal
1617

1718
dpath = "demo/data/"
1819
rng = np.random.RandomState(1994)
@@ -397,7 +398,7 @@ def test_dtypes(self) -> None:
397398
for orig, x in np_dtypes(n_samples, n_features):
398399
m0 = xgb.DMatrix(orig)
399400
m1 = xgb.DMatrix(x)
400-
assert tm.predictor_equal(m0, m1)
401+
assert predictor_equal(m0, m1)
401402

402403

403404
@pytest.mark.skipif(tm.is_windows(), reason="Rabit does not run on windows")

tests/python/test_quantile_dmatrix.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
make_categorical,
1414
make_ltr,
1515
make_sparse_regression,
16-
predictor_equal,
1716
)
1817
from xgboost.testing.data import check_inf, np_dtypes
1918
from xgboost.testing.data_iter import run_mixed_sparsity
2019
from xgboost.testing.quantile_dmatrix import (
2120
check_categorical_strings,
2221
check_ref_quantile_cut,
2322
)
23+
from xgboost.testing.utils import predictor_equal
2424

2525

2626
class TestQuantileDMatrix:

tests/python/test_with_pandas.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from xgboost import testing as tm
88
from xgboost.core import DataSplitMode
99
from xgboost.testing.data import pd_arrow_dtypes, pd_dtypes, run_base_margin_info
10+
from xgboost.testing.utils import predictor_equal
1011

1112
try:
1213
import pandas as pd
@@ -482,9 +483,9 @@ def test_nullable_type(self, DMatrixT) -> None:
482483
if hasattr(orig.dtypes, "__iter__") and any(
483484
dtype == "bool" for dtype in orig.dtypes
484485
):
485-
assert not tm.predictor_equal(m_orig, m_etype)
486+
assert not predictor_equal(m_orig, m_etype)
486487
else:
487-
assert tm.predictor_equal(m_orig, m_etype)
488+
assert predictor_equal(m_orig, m_etype)
488489

489490
np.testing.assert_allclose(m_orig.get_label(), m_etype.get_label())
490491
np.testing.assert_allclose(m_etype.get_label(), y.values.astype(np.float32))
@@ -511,7 +512,7 @@ def test_pyarrow_type(self, DMatrixT: Type[xgb.DMatrix]) -> None:
511512
m_orig = DMatrixT(orig, enable_categorical=True, label=y_orig)
512513
m_etype = DMatrixT(df, enable_categorical=True, label=y)
513514

514-
assert tm.predictor_equal(m_orig, m_etype)
515+
assert predictor_equal(m_orig, m_etype)
515516
if y is not None:
516517
np.testing.assert_allclose(m_orig.get_label(), m_etype.get_label())
517518
np.testing.assert_allclose(m_etype.get_label(), y.values)

tests/python/test_with_scipy.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import scipy.sparse
88

99
import xgboost as xgb
10-
from xgboost import testing as tm
10+
from xgboost.testing.utils import predictor_equal
1111

1212

1313
@pytest.mark.filterwarnings("error")
@@ -59,7 +59,7 @@ def test_csc(DMatrixT: Type[xgb.DMatrix], CSC: Type) -> None:
5959
data = np.array([0, 1, 2, 3, 4])
6060
row_idx = np.array([0, 1, 2, 0, 2])
6161
X = CSC((data, row_idx, indptr), shape=(3, 2))
62-
assert tm.predictor_equal(DMatrixT(X.tocsr()), DMatrixT(X))
62+
assert predictor_equal(DMatrixT(X.tocsr()), DMatrixT(X))
6363

6464

6565
@pytest.mark.filterwarnings("error")
@@ -84,4 +84,4 @@ def test_coo(DMatrixT: Type[xgb.DMatrix], COO: Type) -> None:
8484
assert dtrain.num_col() == 3
8585
assert dtrain.num_nonmissing() == data.size
8686

87-
assert tm.predictor_equal(DMatrixT(X.tocsr()), DMatrixT(X))
87+
assert predictor_equal(DMatrixT(X.tocsr()), DMatrixT(X))

0 commit comments

Comments
 (0)