Repository navigation
API Developer API for customising metadata requests in consumers - #34314
StefanieSenger wants to merge 52 commits into
Conversation
| "score": {"metadata2": False}, | ||
| } | ||
|
|
||
| class CustomMethodConsumer(_MetadataRequester): |
There was a problem hiding this comment.
CustomMethodConsumer and CustomFunctionConsumer are basically the same test. We can remove one. I'd tend to remove the second one since it already gets used in plot_metadata_routing.py.
| request : MetadataRequest | ||
| A :class:`~sklearn.utils.metadata_routing.MetadataRequest` instance. | ||
| """ | ||
| return self.__sklearn_get_metadata_request__() |
There was a problem hiding this comment.
I didn't expect this to be a trivial one-line change but everything else would be an overkill if we keep inheritance from _MetadataRequester.
| # %% | ||
| # The imports below are only required for the examples in this section. | ||
|
|
||
| from sklearn.utils._metadata_requests import MethodMetadataRequest |
There was a problem hiding this comment.
Should we expose MethodMetadataRequest in the public API? It looks a bit like we forgot to do that in the past, or is there a reason we didn't?
| from sklearn.utils._metadata_requests import MethodMetadataRequest | ||
| from sklearn.utils.metadata_routing import MetadataRequest, _MetadataRequester |
There was a problem hiding this comment.
Here, we still rely on inheriting from (and importing) _MetadataRequester, but that's part of the way people should implement custom requests, right?
(The real art would be to prepare this so that CustomConsumers that set requests on custom methods. But seems not at all trivial and we can do that later, if people want it, maybe.)
…nieSenger/scikit-learn into developer_API_get_metadata_request
|
@adrinjalali, I tried your suggestion in b0ee0ac, but it's not giving expected results.
But if we test if they do exactly the same, if hasattr(self, "_metadata_request"):
requests = self._metadata_request.__sklearn_clone__()in The problem is that I'd like to merge this PR (with the solution I had suggested before) and #31695 first and then take care of re-factoring our internal |
| assert consumer.__sklearn_get_metadata_request__()._serialize() == { | ||
| "fit": {"sample_weight": True}, | ||
| "score": {"metadata": False}, | ||
| } |
There was a problem hiding this comment.
I think this test is wrong. We don't want __sklearn_get_metadata_request__ to reflect user-set requests. It only reflects the "default" / "starting point" requests. For this test, we should simply do get_routing_for_object and see what it is.
There was a problem hiding this comment.
The test is not wrong. It is a design question how we want to define the API.
If it stay as is __sklearn_get_metadata_request__() (qua name) should do exactly the same as _get_metadata_request(), so in this sense the test would test the right thing.
But we can split __sklearn_get_metadata_request__() off into doing only the default part (and rename it so it reflects that. Then though it makes sense to split `_get_metadata_request() into two as well (in a future PR), see issue #34372).
I will do the split-off of __sklearn_get_metadata_request__() here and change the internal function in another PR later.
There was a problem hiding this comment.
I'm not sure why we'd need to split _get_metadata_request into two.
There was a problem hiding this comment.
Because it gets and sets and the same time.
There was a problem hiding this comment.
That function isn't setting anything, it's only returning the already set / cached values
There was a problem hiding this comment.
If users use set_fit_request, this function returns the user's requests on the instance over the class-level requests. Maybe that's not strictly setting, but it's combining too much stuff, which is hard to understand, think of and maintain. Of cause it works as it is, but the goal is a refactor to simplify metadata routing.
There was a problem hiding this comment.
I'd be happy to see where you'd put this logic. It has to be somewhere, and in my head this particular one can't get simpler than what it is after this PR
There was a problem hiding this comment.
For this PR, I agree that the _get_metadata_request logic is fine as is, and here we split __sklearn_get_metadata_request__ to only get class-level requests (rename accordingly).
Yes, combining the class-level requests and user-set requests needs to live somewhere, but maybe it can be fully done on set_{method}_request, so getting is simply getting. We can keep this for a follow-up PR and figure out the details then.
_get_metadata_request in consumers|
Thank you @antoinebaker for your recent review. I have gone through your suggestions and also self-reviewed this PR thereafter. This PR is ready for a new round of reviews now. Also CC @adrinjalali, if you can find some time. |
antoinebaker
left a comment
There was a problem hiding this comment.
Hi @StefanieSenger, thanks again for the PR! Apart from one nitpick, LGMT!
| def score(self, y_true, y_pred, metadata1): | ||
| return np.sum(y_true / y_pred) / len(y_true) # pragma: no cover |
There was a problem hiding this comment.
I would again use score(self, X, y, param1, param2, metadata1)
| ignore_params={ | ||
| "y_true", | ||
| "y_pred", | ||
| }, |
There was a problem hiding this comment.
and ignore_params = {"param1","param2"}
| def fit(self, X, y, metadata1, metadata2): | ||
| return self | ||
|
|
||
| def score(self, y_true, y_pred, metadata1): |
There was a problem hiding this comment.
As in the test, why not score(self, X, y, param1, param2, metadata1) and ignore_params={"param1","param2"} ? But if you prefer this signature, I'm fine with it :)
There was a problem hiding this comment.
I have just renamed the params as you had suggested (also in the test). I had wanted to show some customisation, but I agree it is cleaner like this.
| # Note that a declaration like `__metadata_request__score = {"y_true": True}` would | ||
| # override the requests derived by signature sniffing, even when `y_true` is in | ||
| # `ignore_params`. __metadata_request__* class attributes always override other | ||
| # class-level requests, including those modified in | ||
| # `__sklearn_build_declared_metadata_request__`. |
There was a problem hiding this comment.
I feel this is too much detail for a user/developer guide, I would remove it.
There was a problem hiding this comment.
I have removed it.
StefanieSenger
left a comment
There was a problem hiding this comment.
Thanks a lot @antoinebaker!
I applied and did your last few suggestions.
I feel this PR now is in a very good shape. Do you want to have another look, @adrinjalali?
| # Note that a declaration like `__metadata_request__score = {"y_true": True}` would | ||
| # override the requests derived by signature sniffing, even when `y_true` is in | ||
| # `ignore_params`. __metadata_request__* class attributes always override other | ||
| # class-level requests, including those modified in | ||
| # `__sklearn_build_declared_metadata_request__`. |
There was a problem hiding this comment.
I have removed it.
| def fit(self, X, y, metadata1, metadata2): | ||
| return self | ||
|
|
||
| def score(self, y_true, y_pred, metadata1): |
There was a problem hiding this comment.
I have just renamed the params as you had suggested (also in the test). I had wanted to show some customisation, but I agree it is cleaner like this.
Reference Issues/PRs
get_metadata_routingmethod in_MetadataRequester#31695What does this implement/fix? Explain your changes.
This adds a developer API using a new method
__sklearn_get_metadata_request__for metadata consumers that customise their requests (such as_BaseScorerdoes internally).This change is backwards compatible, as one can keep using the original
_get_metadata_request.AI usage disclosure
I used AI assistance for:
CC @adrinjalali, is that how you imagined it?