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

lin reg: only warn on superfluous predictors #361

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion mesmer/stats/_linear_regression.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import warnings
from typing import Mapping, Optional

import numpy as np
Expand Down Expand Up @@ -88,7 +89,7 @@ def predict(
if available_predictors - required_predictors:
superfluous = sorted(available_predictors - required_predictors)
superfluous = "', '".join(superfluous)
raise ValueError(f"Superfluous predictors: '{superfluous}'")
warnings.warn(f"Superfluous predictors: '{superfluous}'")

if "intercept" in exclude:
prediction = xr.zeros_like(params.intercept)
Expand Down
16 changes: 12 additions & 4 deletions tests/unit/test_linear_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,25 @@ def test_lr_predict_missing_superfluous():
)
lr.params = params

da = xr.DataArray([0, 1, 2], dims="time")

with pytest.raises(ValueError, match="Missing predictors: 'tas', 'tas2'"):
lr.predict({})

with pytest.raises(ValueError, match="Missing predictors: 'tas'"):
lr.predict({"tas2": None})

with pytest.raises(ValueError, match="Superfluous predictors: 'something else'"):
lr.predict({"tas": None, "tas2": None, "something else": None})
with pytest.warns(UserWarning, match="Superfluous predictors: 'something else'"):
result = lr.predict({"tas": da, "tas2": da, "something else": None})

expected = xr.DataArray([[5, 9, 13]], dims=("x", "time"))
xr.testing.assert_equal(result, expected)

with pytest.raises(ValueError, match="Superfluous predictors: 'bar', 'foo'"):
lr.predict({"tas": None, "tas2": None, "foo": None, "bar": None})
with pytest.warns(UserWarning, match="Superfluous predictors: 'bar', 'foo'"):
result = lr.predict({"tas": da, "tas2": da, "foo": None, "bar": None})

expected = xr.DataArray([[5, 9, 13]], dims=("x", "time"))
xr.testing.assert_equal(result, expected)


@pytest.mark.parametrize("as_2D", [True, False])
Expand Down