Skip to content

MAINT Parameter validation for PatchExtractor #24215

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 7 commits into from
Sep 2, 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
24 changes: 18 additions & 6 deletions sklearn/feature_extraction/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
# License: BSD 3 clause

from itertools import product
import numbers
from numbers import Integral, Number, Real
import numpy as np
from scipy import sparse
from numpy.lib.stride_tricks import as_strided

from ..utils import check_array, check_random_state
from ..utils._param_validation import Interval
from ..base import BaseEstimator

__all__ = [
Expand Down Expand Up @@ -249,11 +250,11 @@ def _compute_n_patches(i_h, i_w, p_h, p_w, max_patches=None):
all_patches = n_h * n_w

if max_patches:
if isinstance(max_patches, (numbers.Integral)) and max_patches < all_patches:
if isinstance(max_patches, (Integral)) and max_patches < all_patches:
return max_patches
elif isinstance(max_patches, (numbers.Integral)) and max_patches >= all_patches:
elif isinstance(max_patches, (Integral)) and max_patches >= all_patches:
return all_patches
elif isinstance(max_patches, (numbers.Real)) and 0 < max_patches < 1:
elif isinstance(max_patches, (Real)) and 0 < max_patches < 1:
return int(max_patches * all_patches)
else:
raise ValueError("Invalid value for max_patches: %r" % max_patches)
Expand Down Expand Up @@ -299,9 +300,9 @@ def _extract_patches(arr, patch_shape=8, extraction_step=1):

arr_ndim = arr.ndim

if isinstance(patch_shape, numbers.Number):
if isinstance(patch_shape, Number):
patch_shape = tuple([patch_shape] * arr_ndim)
if isinstance(extraction_step, numbers.Number):
if isinstance(extraction_step, Number):
extraction_step = tuple([extraction_step] * arr_ndim)

patch_strides = arr.strides
Expand Down Expand Up @@ -502,6 +503,16 @@ class PatchExtractor(BaseEstimator):
Patches shape: (545706, 2, 2)
"""

_parameter_constraints: dict = {
"patch_size": [tuple, None],
"max_patches": [
None,
Interval(Real, 0, 1, closed="neither"),
Interval(Integral, 1, None, closed="left"),
],
"random_state": ["random_state"],
}

def __init__(self, *, patch_size=None, max_patches=None, random_state=None):
self.patch_size = patch_size
self.max_patches = max_patches
Expand All @@ -526,6 +537,7 @@ def fit(self, X, y=None):
self : object
Returns the instance itself.
"""
self._validate_params()
return self

def transform(self, X):
Expand Down
1 change: 0 additions & 1 deletion sklearn/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,6 @@ def test_estimators_do_not_raise_errors_in_init_or_set_params(Estimator):
"OPTICS",
"OneVsOneClassifier",
"OneVsRestClassifier",
"PatchExtractor",
"RANSACRegressor",
"RidgeCV",
"RidgeClassifierCV",
Expand Down