Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FIX Remove spurious UserWarning #25129

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

Merged
merged 5 commits into from
Dec 7, 2022
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
7 changes: 2 additions & 5 deletions sklearn/metrics/_pairwise_distances_reduction/_argkmin.pyx.tp
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,8 @@ cdef class EuclideanArgKmin{{name_suffix}}(ArgKmin{{name_suffix}}):
metric_kwargs=None,
):
if (
metric_kwargs is not None and
len(metric_kwargs) > 0 and (
"Y_norm_squared" not in metric_kwargs or
"X_norm_squared" not in metric_kwargs
)
isinstance(metric_kwargs, dict) and
(metric_kwargs.keys() - {"X_norm_squared", "Y_norm_squared"})
):
warnings.warn(
f"Some metric_kwargs have been passed ({metric_kwargs}) but aren't "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,8 @@ cdef class EuclideanRadiusNeighbors{{name_suffix}}(RadiusNeighbors{{name_suffix}
metric_kwargs=None,
):
if (
metric_kwargs is not None and
len(metric_kwargs) > 0 and (
"Y_norm_squared" not in metric_kwargs or
"X_norm_squared" not in metric_kwargs
)
isinstance(metric_kwargs, dict) and
(metric_kwargs.keys() - {"X_norm_squared", "Y_norm_squared"})
):
warnings.warn(
f"Some metric_kwargs have been passed ({metric_kwargs}) but aren't "
Expand Down
76 changes: 67 additions & 9 deletions sklearn/metrics/tests/test_pairwise_distances_reduction.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import itertools
import re
import warnings
from collections import defaultdict

import numpy as np
Expand Down Expand Up @@ -620,19 +621,44 @@ def test_argkmin_factory_method_wrong_usages():
with pytest.raises(ValueError, match="ndarray is not C-contiguous"):
ArgKmin.compute(X=np.asfortranarray(X), Y=Y, k=k, metric=metric)

# A UserWarning must be raised in this case.
unused_metric_kwargs = {"p": 3}

message = (
r"Some metric_kwargs have been passed \({'p': 3}\) but aren't usable for this"
r" case \("
r"EuclideanArgKmin64."
)
message = r"Some metric_kwargs have been passed \({'p': 3}\) but"

with pytest.warns(UserWarning, match=message):
ArgKmin.compute(
X=X, Y=Y, k=k, metric=metric, metric_kwargs=unused_metric_kwargs
)

# A UserWarning must be raised in this case.
metric_kwargs = {
"p": 3, # unused
"Y_norm_squared": sqeuclidean_row_norms(Y, num_threads=2),
}

message = r"Some metric_kwargs have been passed \({'p': 3, 'Y_norm_squared'"

with pytest.warns(UserWarning, match=message):
ArgKmin.compute(X=X, Y=Y, k=k, metric=metric, metric_kwargs=metric_kwargs)

# No user warning must be raised in this case.
metric_kwargs = {
"X_norm_squared": sqeuclidean_row_norms(X, num_threads=2),
}
with warnings.catch_warnings():
warnings.simplefilter("error", category=UserWarning)
ArgKmin.compute(X=X, Y=Y, k=k, metric=metric, metric_kwargs=metric_kwargs)

# No user warning must be raised in this case.
metric_kwargs = {
"X_norm_squared": sqeuclidean_row_norms(X, num_threads=2),
"Y_norm_squared": sqeuclidean_row_norms(Y, num_threads=2),
}
with warnings.catch_warnings():
warnings.simplefilter("error", category=UserWarning)
ArgKmin.compute(X=X, Y=Y, k=k, metric=metric, metric_kwargs=metric_kwargs)


def test_radius_neighbors_factory_method_wrong_usages():
rng = np.random.RandomState(1)
Expand Down Expand Up @@ -683,16 +709,48 @@ def test_radius_neighbors_factory_method_wrong_usages():

unused_metric_kwargs = {"p": 3}

message = (
r"Some metric_kwargs have been passed \({'p': 3}\) but aren't usable for this"
r" case \(EuclideanRadiusNeighbors64"
)
# A UserWarning must be raised in this case.
message = r"Some metric_kwargs have been passed \({'p': 3}\) but"

with pytest.warns(UserWarning, match=message):
RadiusNeighbors.compute(
X=X, Y=Y, radius=radius, metric=metric, metric_kwargs=unused_metric_kwargs
)

# A UserWarning must be raised in this case.
metric_kwargs = {
"p": 3, # unused
"Y_norm_squared": sqeuclidean_row_norms(Y, num_threads=2),
}

message = r"Some metric_kwargs have been passed \({'p': 3, 'Y_norm_squared'"

with pytest.warns(UserWarning, match=message):
RadiusNeighbors.compute(
X=X, Y=Y, radius=radius, metric=metric, metric_kwargs=metric_kwargs
)

# No user warning must be raised in this case.
metric_kwargs = {
"X_norm_squared": sqeuclidean_row_norms(X, num_threads=2),
"Y_norm_squared": sqeuclidean_row_norms(Y, num_threads=2),
}
with warnings.catch_warnings():
warnings.simplefilter("error", category=UserWarning)
RadiusNeighbors.compute(
X=X, Y=Y, radius=radius, metric=metric, metric_kwargs=metric_kwargs
)

# No user warning must be raised in this case.
metric_kwargs = {
"X_norm_squared": sqeuclidean_row_norms(X, num_threads=2),
}
with warnings.catch_warnings():
warnings.simplefilter("error", category=UserWarning)
RadiusNeighbors.compute(
X=X, Y=Y, radius=radius, metric=metric, metric_kwargs=metric_kwargs
)


@pytest.mark.parametrize(
"n_samples_X, n_samples_Y", [(100, 100), (500, 100), (100, 500)]
Expand Down