Bas Jansen
Bas Jansen

Reputation: 3343

Unable to suppress UserWarning returned by interpret

I am using interpret for a project, and during the tests I provide to few samples (just to speed up tests) which results in a UserWarning

/usr/local/lib/python3.8/site-packages/interpret/glassbox/ebm/utils.py:91: UserWarning: Too few samples per class, adapting test size to guarantee 1 sample per class.
  warnings.warn(

I have been trying to suppress this warning so far with no success, I have even tried using a carte blanch ignore::UserWarning in my pytest filterwarnings, as follows:

[tool:pytest]
addopts = -s --maxfail=1 --failed-first --color=yes
filterwarnings = ignore::_pytest.warning_types.PytestUnknownMarkWarning
                 ignore::UserWarning
                 ;ignore::UserWarning:interpret
norecursedirs = docs .mypy_cache
python_files = test_*.py

I have delved in to the file that threw the warning, where the actual lines that throw the warning are as follows:

if n_test_samples < y_uniq:  # pragma: no cover
    warnings.warn(
        "Too few samples per class, adapting test size to guarantee 1 sample per class."
    )
    test_size = y_uniq

which should be a UserWarning, as that's the default for warnings.warn. The question then is, how would one suppress such a warning as what I have used previously doesn't work for this case (in the pytest config)?

Here is an as isolated as possible example of the above listed problem (using the mark option to mimic our setup):

from interpret.glassbox import ExplainableBoostingClassifier
from numpy import array, ravel
from pytest import mark


@mark.filterwarnings("ignore::UserWarning")
def test():
    features = array([
        [0.4, 0.6, 0.8],
        [0.3, 0.7, 0.9],
        [0.1, 0.5, 1.1],
        [0.2, 0.8, 1.0],
        [0.2, 6.4, 1.2],
        [0.5, 8.6, 0.9],
        [0.5, 7.9, 0.7],
        [0.4, 7.5, 0.9],
    ])
    labels = ravel(array([
        ["class_1"],
        ["class_1"],
        ["class_1"],
        ["class_1"],
        ["class_2"],
        ["class_2"],
        ["class_2"],
        ["class_2"],
    ]))

    model = ExplainableBoostingClassifier()
    model.fit(X=features, y=labels)

# call funcy, should be done through pytest though
test()

Upvotes: 1

Views: 517

Answers (1)

MrBean Bremen
MrBean Bremen

Reputation: 16825

Most likely your problem is that you have a pytest.ini or tox.ini that is read by pytest. If any of these files exist in your path and contains a [pytest] section, this supersedes the [tool:pytest] section of setup.cfg. So the solution is either to remove the pytest.ini / tox.ini, or to move the filterwarnings line into pytest.ini:

[pytest]
filterwarnings = ignore::UserWarning:interpret

As mentioned in the comments, the pytest documentation states:

Usage of setup.cfg is not recommended unless for very simple use cases. .cfg files use a different parser than pytest.ini and tox.ini which might cause hard to track down problems.

So the second option is probably the better one.

Upvotes: 1

Related Questions