|
12 | 12 | from sklearn.model_selection import train_test_split |
13 | 13 |
|
14 | 14 | import skrub |
| 15 | +from skrub import ApplyToCols |
15 | 16 | from skrub import selectors as s |
16 | 17 | from skrub._data_ops import _data_ops |
17 | 18 | from skrub._utils import PassThrough |
@@ -329,45 +330,65 @@ class A(_data_ops.DataOpImpl): |
329 | 330 | a.skb.eval() |
330 | 331 |
|
331 | 332 |
|
332 | | -@pytest.mark.parametrize("why_full_frame", ["numpy", "predictor", "how"]) |
| 333 | +@pytest.mark.parametrize("why_no_wrap", ["numpy", "predictor", "how"]) |
333 | 334 | @pytest.mark.parametrize("bad_param", ["cols", "how", "allow_reject"]) |
334 | | -def test_apply_bad_params(why_full_frame, bad_param): |
| 335 | +def test_apply_bad_params(why_no_wrap, bad_param): |
335 | 336 | # When the estimator is a predictor or the input is a numpy array (not a |
336 | | - # dataframe) (or how='full_frame') the estimator can only be applied to the |
| 337 | + # dataframe) (or how='no_wrap') the estimator can only be applied to the |
337 | 338 | # full input without wrapping in ApplyToCols or ApplyToFrame. In this case |
338 | 339 | # if the user passed a parameter that would require wrapping, such as |
339 | 340 | # passing a value for `cols` that is not `all()`, or passing |
340 | | - # how='columnwise' or allow_reject=True, we get an error. |
| 341 | + # how='cols' or allow_reject=True, we get an error. |
341 | 342 |
|
342 | | - if why_full_frame == bad_param == "how": |
| 343 | + if why_no_wrap == bad_param == "how": |
343 | 344 | return |
344 | 345 | X_a, y_a = make_classification(random_state=0) |
345 | 346 | X_df = pd.DataFrame(X_a, columns=[f"col_{i}" for i in range(X_a.shape[1])]) |
346 | 347 |
|
347 | | - X = skrub.X(X_a) if why_full_frame == "numpy" else skrub.X(X_df) |
348 | | - if why_full_frame == "predictor": |
| 348 | + X = skrub.X(X_a) if why_no_wrap == "numpy" else skrub.X(X_df) |
| 349 | + if why_no_wrap == "predictor": |
349 | 350 | estimator = LogisticRegression() |
350 | 351 | y = skrub.y(y_a) |
351 | 352 | else: |
352 | 353 | estimator = PassThrough() |
353 | 354 | y = None |
354 | | - how = "full_frame" if why_full_frame == "how" else "auto" |
355 | | - # X is a numpy array: how must be full_frame and selecting columns is not |
| 355 | + how = "no_wrap" if why_no_wrap == "how" else "auto" |
| 356 | + # X is a numpy array: how must be no_wrap and selecting columns is not |
356 | 357 | # allowed. |
357 | 358 | if bad_param == "cols": |
358 | | - if why_full_frame == "numpy": |
| 359 | + if why_no_wrap == "numpy": |
359 | 360 | cols = [0, 1] |
360 | 361 | else: |
361 | 362 | cols = ["col_0", "col_1"] |
362 | 363 | else: |
363 | 364 | cols = s.all() |
364 | | - how = "columnwise" if bad_param == "how" else how |
| 365 | + how = "cols" if bad_param == "how" else how |
365 | 366 | allow_reject = True if bad_param == "allow_reject" else False |
366 | 367 |
|
367 | | - with pytest.raises((ValueError, RuntimeError), match=""): |
| 368 | + with pytest.raises( |
| 369 | + (ValueError, RuntimeError), |
| 370 | + match=( |
| 371 | + r"(`cols` must be `all\(\)`|`how` must be 'auto'|`allow_reject` must be" |
| 372 | + r" False)" |
| 373 | + ), |
| 374 | + ): |
368 | 375 | X.skb.apply(estimator, y=y, how=how, allow_reject=allow_reject, cols=cols) |
369 | 376 |
|
370 | 377 |
|
| 378 | +def test_apply_invalid_how(): |
| 379 | + df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) |
| 380 | + X = skrub.var("X", df) |
| 381 | + t = PassThrough() |
| 382 | + for how in ["auto", "cols", "frame", "no_wrap"]: |
| 383 | + assert list(X.skb.apply(t, how=how).skb.eval().columns) == ["a", "b"] |
| 384 | + with pytest.raises(RuntimeError, match="`how` must be one of"): |
| 385 | + X.skb.apply(t, how="bad value") |
| 386 | + # TODO: remove when old names are dropped in 0.7.0 |
| 387 | + with pytest.warns(FutureWarning, match="'columnwise' has been renamed to 'cols'"): |
| 388 | + wrapper = X.skb.apply(t, how="columnwise").skb.applied_estimator.skb.eval() |
| 389 | + assert isinstance(wrapper, ApplyToCols) |
| 390 | + |
| 391 | + |
371 | 392 | class Mul(BaseEstimator): |
372 | 393 | def __init__(self, factor=1): |
373 | 394 | self.factor = factor |
|
0 commit comments