Repository navigation
ENH Auto-requesting sample_weight on meta-estimators that also consume it - #35095
Conversation
sample_weight on meta-estimators that also consume it
| @@ -0,0 +1,14 @@ | |||
| - |Feature| The following consuming :term:`routers <router>` auto-request | |||
There was a problem hiding this comment.
Thank you, I have fixed this now.
|
I set the "No Changelog needed" label to prevent the CI from failing. It had passed before but after fixing the changelogs across several PRs in 476420f, it fails because it cannot match the file names with this PR anymore. |
OmarManzoor
left a comment
There was a problem hiding this comment.
Otherwise LGTM. Thank you @StefanieSenger
| @config_context(enable_metadata_routing=True) | ||
| def test_search_sample_weight_auto_request(): | ||
| # Check that auto-requesting `sample_weight` works on consuming routers if passed | ||
| # through `GridSearchCV`. | ||
| X, y = make_classification(random_state=42) | ||
| sample_weight = np.ones(len(y)) | ||
| meta_est = WeightedMetaClassifier(estimator=NonConsumingClassifier()) | ||
| search = GridSearchCV(meta_est, param_grid={"estimator__alpha": [0.0]}, cv=2) | ||
|
|
||
| with config_context(enable_metadata_auto_requests=False): | ||
| assert ( | ||
| get_routing_for_object(search) | ||
| ._route_mappings["estimator"] | ||
| .router._self_request.fit.requests.get("sample_weight") | ||
| is None | ||
| ) | ||
| assert ( | ||
| get_routing_for_object(search) | ||
| ._route_mappings["estimator"] | ||
| .router._self_request.score.requests.get("sample_weight") | ||
| is None | ||
| ) | ||
| with pytest.raises(UnsetMetadataPassedError, match="sample_weight"): | ||
| search.fit(X, y, sample_weight=sample_weight) | ||
|
|
||
| with config_context(enable_metadata_auto_requests=True): | ||
| assert ( | ||
| get_routing_for_object(search) | ||
| ._route_mappings["estimator"] | ||
| .router._self_request.fit.requests.get("sample_weight") | ||
| is True | ||
| ) | ||
| assert ( | ||
| get_routing_for_object(search) | ||
| ._route_mappings["estimator"] | ||
| .router._self_request.score.requests.get("sample_weight") | ||
| is True | ||
| ) | ||
| search.fit(X, y, sample_weight=sample_weight) |
There was a problem hiding this comment.
Might be better to parametrize?
@pytest.mark.parametrize(
"auto_request, expected",
[(False, None), (True, True)],
)
@config_context(enable_metadata_routing=True)
def test_search_sample_weight_auto_request(auto_request, expected):
# Check that auto-requesting `sample_weight` works on consuming routers if passed
# through `GridSearchCV`.
X, y = make_classification(random_state=42)
sample_weight = np.ones(len(y))
meta_est = WeightedMetaClassifier(estimator=NonConsumingClassifier())
search = GridSearchCV(meta_est, param_grid={"estimator__alpha": [0.0]}, cv=2)
with config_context(enable_metadata_auto_requests=auto_request):
assert (
get_routing_for_object(search)
._route_mappings["estimator"]
.router._self_request.fit.requests.get("sample_weight")
is expected
)
assert (
get_routing_for_object(search)
._route_mappings["estimator"]
.router._self_request.score.requests.get("sample_weight")
is expected
)
if auto_request:
search.fit(X, y, sample_weight=sample_weight)
else:
with pytest.raises(UnsetMetadataPassedError, match="sample_weight"):
search.fit(X, y, sample_weight=sample_weight)|
Hi @StefanieSenger, thanks for the PR. I have a couple of questions while reviewing:
TDLR: do we really need to declare auto request at all ? If so, how to make sure we cover all meta-estimators that need it (and only the ones that need it) ? |
|
For point 4, here's according to cursor the list of meta-estimators that consume
|
|
Besides I think we need more thorough testing, probably in follow-up PRs. For now the tests are mainly focused on the declaration part (is this
For 1. if the complex meta-estimator passes, then we will be reassured that the individual components and the metadata routing machinery are working as expected. But if it fails, it will be quite hard to debug ! So that's why I'm proposing 2. For 2. maybe a callback could be handy, at least for the Let me know what you think @StefanieSenger and @ogrisel, I can start to work on some of these tests if you think it's a good idea. |
I love your ideas. We already do quite some testing if Happy for you to take the lead there, @antoinebaker. I can review your work then. |
I was wondering about the same question. Yes, we had not taken care of the default scorer here (via Let me do a through check if we didn't miss anything. (An exception here is
I am actually not sure how we can be sure we covered everything. But surely if we could come up with a test for this, it would be an very valuable test. |
Once this is done, the Bagging* will need a And there may be more cases like these. |
I am not sure the additional tests are mandatory. As far as I understand, the auto-request is only about automatically adding the request, but when it is added it is, the estimator is in the same state as if the request had been manually added, and thus uses the same logical path. If that is the case, then the functional part should already be covered by the regular metadatarouting tests and checking that the request is present at the right time should be enough. Although maybe I misunderstood and the auto-request uses a different logical path than manual requests, in which case I agree that the additional functional tests are necessary. |
This is correct, they use the same logical path. This is why it makes sense to add these tests in a separate PR (because they are unrelated to the auto-requests). But we had really missed some bugs in the past with the tests that we have and a strategic check will be very valuable when we start thinking about moving metadata routing out of experimental (in a future release, not the 1.10 one). Edit: I forgot to answer to this:
A callback run by pytest (if I understand correctly) may be a challenge, at least if it uses a different thread. I am not sure if its worth it. |
Co-authored-by: Omar Salman <[email protected]>
…it-learn into auto-request_routers
StefanieSenger
left a comment
There was a problem hiding this comment.
Thank you @OmarManzoor for your review and suggestions.
| @config_context(enable_metadata_routing=True) | ||
| def test_search_sample_weight_auto_request(): | ||
| # Check that auto-requesting `sample_weight` works on consuming routers if passed | ||
| # through `GridSearchCV`. | ||
| X, y = make_classification(random_state=42) | ||
| sample_weight = np.ones(len(y)) | ||
| meta_est = WeightedMetaClassifier(estimator=NonConsumingClassifier()) | ||
| search = GridSearchCV(meta_est, param_grid={"estimator__alpha": [0.0]}, cv=2) | ||
|
|
||
| with config_context(enable_metadata_auto_requests=False): | ||
| assert ( | ||
| get_routing_for_object(search) | ||
| ._route_mappings["estimator"] | ||
| .router._self_request.fit.requests.get("sample_weight") | ||
| is None | ||
| ) | ||
| assert ( | ||
| get_routing_for_object(search) | ||
| ._route_mappings["estimator"] | ||
| .router._self_request.score.requests.get("sample_weight") | ||
| is None | ||
| ) | ||
| with pytest.raises(UnsetMetadataPassedError, match="sample_weight"): | ||
| search.fit(X, y, sample_weight=sample_weight) | ||
|
|
||
| with config_context(enable_metadata_auto_requests=True): | ||
| assert ( | ||
| get_routing_for_object(search) | ||
| ._route_mappings["estimator"] | ||
| .router._self_request.fit.requests.get("sample_weight") | ||
| is True | ||
| ) | ||
| assert ( | ||
| get_routing_for_object(search) | ||
| ._route_mappings["estimator"] | ||
| .router._self_request.score.requests.get("sample_weight") | ||
| is True | ||
| ) | ||
| search.fit(X, y, sample_weight=sample_weight) |
|
I've now checked and it is clear that #34966 already covers any Even though Happy to review if someone wants to add self_requests and auto-request-tests for Bagging*, Isolationforest and OutputCodeClassifier. If there is any other meta-estimator that consumes Adding auto-requests to scorers is still a different beast since they don't inherit from |
Thanks @FrancoisPgm, that's indeed a good point. The functional tests will check the metadata routing machinery and sample weight consistency as a whole, not the way the requests are made. To elaborate on @StefanieSenger answer, we were a bit discouraged in the past to try out sophisticated meta estimators, precisely because of the difficulty of manually writing all the |
Could you please try in this PR ? I think it could be as simple as setting the
For |
antoinebaker
left a comment
There was a problem hiding this comment.
Hi @StefanieSenger, thanks for the PR !
I think it would be good to fix the bagging estimators in this PR, as said in the previous comment.
But otherwise, LGMT !
| assert ( | ||
| get_routing_for_object(search) | ||
| ._route_mappings["estimator"] | ||
| .router._self_request.fit.requests.get("sample_weight") | ||
| is expected | ||
| ) | ||
| assert ( | ||
| get_routing_for_object(search) | ||
| ._route_mappings["estimator"] | ||
| .router._self_request.score.requests.get("sample_weight") | ||
| is expected | ||
| ) |
There was a problem hiding this comment.
The path to get to the meta_est self_request is quite intricate. Is there a simpler way ? Maybe
routing = get_routing_for_object(meta_est)
assert routing._self_request.fit.requests.get("sample_weight") is expected
assert routing._self_request.score.requests.get("sample_weight") is expectedThere was a problem hiding this comment.
That's nice. Thanks. :)
StefanieSenger
left a comment
There was a problem hiding this comment.
Could you please try in this PR ? I think it could be as simple as setting the add_self_request in BaseBagging.get_metadata_routing, and edit the self_request_sample_weight_methods entry for bagging estimators to check that they pass test_metaestimator_adds_auto_request_to_self_request. Then I think we cover all metaestimators for the fit method.
Yes, it's as simple as that. I did add all this to the current PR, so we have all the modifications in one place.
| assert ( | ||
| get_routing_for_object(search) | ||
| ._route_mappings["estimator"] | ||
| .router._self_request.fit.requests.get("sample_weight") | ||
| is expected | ||
| ) | ||
| assert ( | ||
| get_routing_for_object(search) | ||
| ._route_mappings["estimator"] | ||
| .router._self_request.score.requests.get("sample_weight") | ||
| is expected | ||
| ) |
There was a problem hiding this comment.
That's nice. Thanks. :)
|
I think we can merge this PR, is that alright for you, @OmarManzoor? |
ogrisel
left a comment
There was a problem hiding this comment.
LGTM as well. Thanks @StefanieSenger and reviewers.

Reference Issues/PRs
towards #34901
What does this implement/fix? Explain your changes.
This adds auto-requests forsample_weightto (all of our) 10 meta-estimators that also consumesample_weightinternally (infitand/orscore).#34966 already covers the functionality that we wanted to add here, which is why this PR now only adds the tests and an update to the changelog entry of the other PR.
AI usage disclosure
I used AI assistance for: