Skip to content

FIX scorer metadata routing - #35421

Open
BabaSanfour wants to merge 15 commits into
scikit-learn:mainfrom
BabaSanfour:fix-scorer-metadata-routing
Open

BabaSanfour wants to merge 15 commits into
scikit-learn:mainfrom
BabaSanfour:fix-scorer-metadata-routing

Conversation

@BabaSanfour

Copy link
Copy Markdown

Reference Issues/PRs

Fixes #27977, built on top of #31654.

What does this implement/fix? Explain your changes.

With routing on, a scorer from make_scorer/get_scorer only received metadata for its score function. When it called predict, predict_proba or decision_function on the estimator, it passed nothing. #31654 only changed cross_validate, so the GridSearchCV + Pipeline case still got no metadata in transform at scoring time.

the fix is a new helper _get_scorer_routing(scorer, estimator) in metrics/_scorer.py that

  • returns a MetadataRouter with a self-request for the score function;
  • adds the estimator with score mapped to each response method the scorer may call;
  • changes a multimetric scorer to a router over its sub-scorers;
  • uses only existing primitives (add_self_request, add + MethodMapping).

Existing routers now use it instead of the bare scorer.

Introduce yourself

I am Hamza Abdelhedi, a PhD student at Mila, Quebec AI institute. I have been using sklearn regularly since years and now I am building a tool that uses functionalities for NeuroAI pipelines. I contributed to other packages like mne-tools and related packages.

AI usage disclosure

I used Opus5.5 for finding the issue in sklearn and it led me to the issue and open PR. I brainstormed ideas on how to fix the issue and it helped me identify other potential fixes to do. Code was generated by it and reviewed by me.

Any other comments?

We could also use a method on each scorer class (e.g.scorer._get_metadata_routing(estimator)) instead of a module-level function that branches on isinstance. However I went for a function because scoring can be a plain user callable without such a method, so routers would still need a fallback. I also avoided changing the public get_metadata_routing() signature, or storing the estimator in the scorer: one is an API change, the other would mutate user callables.

@github-actions

Copy link
Copy Markdown

Thank you for opening your first pull request to scikit-learn! 🎉

To help get your contribution reviewed, please make sure that:

  • You have filled out the pull request template.

  • The pull request addresses an existing issue that is ready for contribution (e.g. not tagged as 'Needs Triage', 'Needs Decision', ...). If you are proposing a new feature, please open an issue to discuss it first.

  • There are no other open pull requests already targeting the same issue.

  • You have followed the pull request checklist. In particular, linting and tests should pass.

@FrancoisPgm FrancoisPgm added the Metadata Routing all issues related to metadata routing, slep006, sample props label Nov 18, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Metadata Routing all issues related to metadata routing, slep006, sample props size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Routing metadata to the response_method used by a scorer

3 participants