Skip to content

Commit d00d983

Browse files
tomiockglemaitrejeremiedbb
authored
MAINT Parameters validation for cluster.compute_optics_graph (scikit-learn#25245)
Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com> Co-authored-by: jeremiedbb <jeremiedbb@yahoo.fr>
1 parent f86aae2 commit d00d983

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

‎sklearn/cluster/_optics.py‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,22 @@ def _compute_core_distances_(X, neighbors, min_samples, working_memory):
423423
return core_distances
424424

425425

426+
@validate_params(
427+
{
428+
"X": [np.ndarray, "sparse matrix"],
429+
"min_samples": [
430+
Interval(Integral, 2, None, closed="left"),
431+
Interval(Real, 0, 1, closed="both"),
432+
],
433+
"max_eps": [Interval(Real, 0, None, closed="both")],
434+
"metric": [StrOptions(set(_VALID_METRICS) | {"precomputed"}), callable],
435+
"p": [Interval(Real, 0, None, closed="right"), None],
436+
"metric_params": [dict, None],
437+
"algorithm": [StrOptions({"auto", "brute", "ball_tree", "kd_tree"})],
438+
"leaf_size": [Interval(Integral, 1, None, closed="left")],
439+
"n_jobs": [Integral, None],
440+
}
441+
)
426442
def compute_optics_graph(
427443
X, *, min_samples, max_eps, metric, p, metric_params, algorithm, leaf_size, n_jobs
428444
):
@@ -432,7 +448,7 @@ def compute_optics_graph(
432448
433449
Parameters
434450
----------
435-
X : ndarray of shape (n_samples, n_features), or \
451+
X : {ndarray, sparse matrix} of shape (n_samples, n_features), or \
436452
(n_samples, n_samples) if metric='precomputed'
437453
A feature array, or array of distances between samples if
438454
metric='precomputed'.

‎sklearn/tests/test_public_functions.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def _check_function_param_validation(
8585

8686
for constraint in constraints:
8787
try:
88-
bad_value = generate_invalid_param_val(constraint)
88+
bad_value = generate_invalid_param_val(constraint, constraints)
8989
except NotImplementedError:
9090
continue
9191

@@ -95,6 +95,7 @@ def _check_function_param_validation(
9595

9696
PARAM_VALIDATION_FUNCTION_LIST = [
9797
"sklearn.cluster.cluster_optics_dbscan",
98+
"sklearn.cluster.compute_optics_graph",
9899
"sklearn.cluster.estimate_bandwidth",
99100
"sklearn.cluster.kmeans_plusplus",
100101
"sklearn.covariance.empirical_covariance",

‎sklearn/utils/_param_validation.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,11 @@ def generate_valid_param(constraint):
886886
if constraint.type is np.ndarray:
887887
# special case for ndarray since it can't be instantiated without arguments
888888
return np.array([1, 2, 3])
889+
890+
if constraint.type in (Integral, Real):
891+
# special case for Integral and Real since they are abstract classes
892+
return 1
893+
889894
return constraint.type()
890895

891896
if isinstance(constraint, _Booleans):

0 commit comments

Comments
 (0)