Skip to content

ENH allow all main top level methods to have a corresponding set metadata request #23342

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
85 changes: 82 additions & 3 deletions sklearn/tests/test_metadata_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,9 +739,12 @@ def test_methodmapping():
str(mm)
== "[{'callee': 'fit', 'caller': 'fit'}, {'callee': 'partial_fit', 'caller':"
" 'partial_fit'}, {'callee': 'predict', 'caller': 'predict'}, {'callee':"
" 'score', 'caller': 'score'}, {'callee': 'split', 'caller': 'split'},"
" {'callee': 'transform', 'caller': 'transform'}, {'callee':"
" 'inverse_transform', 'caller': 'inverse_transform'}]"
" 'predict_proba', 'caller': 'predict_proba'}, {'callee':"
" 'predict_log_proba', 'caller': 'predict_log_proba'}, {'callee':"
" 'decision_function', 'caller': 'decision_function'}, {'callee': 'score',"
" 'caller': 'score'}, {'callee': 'split', 'caller': 'split'}, {'callee':"
" 'transform', 'caller': 'transform'}, {'callee': 'inverse_transform',"
" 'caller': 'inverse_transform'}]"
)

mm = MethodMapping.from_str("score")
Expand Down Expand Up @@ -844,3 +847,79 @@ def test_metadata_routing_get_param_names():
assert router._get_param_names(
method="fit", return_alias=True, ignore_self=True
) == router._get_param_names(method="fit", return_alias=False, ignore_self=True)


def test_method_generation():
# Test if all required request methods are generated.

# TODO: these test classes can be moved to sklearn.utils._testing once we
# have a better idea of what the commonly used classes are.
class SimpleEstimator(BaseEstimator):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering if we could move this estimator in sklearn.utils._testing next to the Minimal* estimator and test it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think after this PR we can slowly figure out what the common test estimators are and move them there. They have evolved quite a bit over time.

# This class should have no set_{method}_request
def fit(self, X, y):
pass

def partial_fit(self, X, y):
pass

def predict(self, X):
pass

def predict_proba(self, X):
pass

def predict_log_proba(self, X):
pass

def decision_function(self, X):
pass

def score(self, X, y):
pass

def split(self, X, y=None):
pass

def transform(self, X):
pass

def inverse_transform(self, X):
pass

for method in METHODS:
assert not hasattr(SimpleEstimator(), f"set_{method}_request")

class SimpleEstimator(BaseEstimator):
# This class should have every set_{method}_request
def fit(self, X, y, sample_weight=None):
pass

def partial_fit(self, X, y, sample_weight=None):
pass

def predict(self, X, sample_weight=None):
pass

def predict_proba(self, X, sample_weight=None):
pass

def predict_log_proba(self, X, sample_weight=None):
pass

def decision_function(self, X, sample_weight=None):
pass

def score(self, X, y, sample_weight=None):
pass

def split(self, X, y=None, sample_weight=None):
pass

def transform(self, X, sample_weight=None):
pass

def inverse_transform(self, X, sample_weight=None):
pass

for method in METHODS:
assert hasattr(SimpleEstimator(), f"set_{method}_request")
3 changes: 3 additions & 0 deletions sklearn/utils/_metadata_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ def is_valid(cls, item):
"fit",
"partial_fit",
"predict",
"predict_proba",
"predict_log_proba",
"decision_function",
"score",
"split",
"transform",
Expand Down