Skip to content

MAINT Parameters validation for sklearn.neighbors.sort_graph_by_row_values #26173

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
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
12 changes: 8 additions & 4 deletions sklearn/neighbors/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from ..utils.multiclass import check_classification_targets
from ..utils.validation import check_is_fitted
from ..utils.validation import check_non_negative
from ..utils._param_validation import Interval, StrOptions
from ..utils._param_validation import Interval, StrOptions, validate_params
from ..utils.parallel import delayed, Parallel
from ..utils.fixes import parse_version, sp_base_version, sp_version
from ..exceptions import DataConversionWarning, EfficiencyWarning
Expand Down Expand Up @@ -197,6 +197,13 @@ def _check_precomputed(X):
return graph


@validate_params(
{
"graph": ["sparse matrix"],
"copy": ["boolean"],
"warn_when_not_sorted": ["boolean"],
}
)
def sort_graph_by_row_values(graph, copy=False, warn_when_not_sorted=True):
"""Sort a sparse graph such that each row is stored with increasing values.

Expand Down Expand Up @@ -224,9 +231,6 @@ def sort_graph_by_row_values(graph, copy=False, warn_when_not_sorted=True):
Distance matrix to other samples, where only non-zero elements are
considered neighbors. Matrix is in CSR format.
"""
if not issparse(graph):
raise TypeError(f"Input graph must be a sparse matrix, got {graph!r} instead.")

if graph.format == "csr" and _is_sorted_by_data(graph):
return graph

Expand Down
4 changes: 0 additions & 4 deletions sklearn/neighbors/tests/test_neighbors.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,10 +497,6 @@ def test_sort_graph_by_row_values_copy():
with pytest.raises(ValueError, match="Use copy=True to allow the conversion"):
sort_graph_by_row_values(X.tocsc(), copy=False)

# raise if X is not even sparse
with pytest.raises(TypeError, match="Input graph must be a sparse matrix"):
sort_graph_by_row_values(X.toarray())


def test_sort_graph_by_row_values_warning():
# Test that the parameter warn_when_not_sorted works as expected.
Expand Down
1 change: 1 addition & 0 deletions sklearn/tests/test_public_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ def _check_function_param_validation(
"sklearn.metrics.top_k_accuracy_score",
"sklearn.metrics.zero_one_loss",
"sklearn.model_selection.train_test_split",
"sklearn.neighbors.sort_graph_by_row_values",
"sklearn.preprocessing.add_dummy_feature",
"sklearn.preprocessing.binarize",
"sklearn.preprocessing.label_binarize",
Expand Down