Skip to content

FIX Metadata routing tests for check_recorded_metadata - #33285

Closed
StefanieSenger wants to merge 11 commits into
scikit-learn:mainfrom
StefanieSenger:metadata_learning_curve
Closed

StefanieSenger wants to merge 11 commits into
scikit-learn:mainfrom
StefanieSenger:metadata_learning_curve

Conversation

@StefanieSenger

@StefanieSenger StefanieSenger commented Feb 15, 2026 •

Copy link
Copy Markdown
Member

What does this implement/fix? Explain your changes.

This fixes metadata routing tests that were broken and had accidentally compared the contents of an empty list with another empty list. Here, we pass the correct function names to check_recorded_metadata.

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

@StefanieSenger StefanieSenger changed the title FIX Metadata routing in learning_curve FIX Metadata routing in learning_curve and module tests Feb 15, 2026
@StefanieSenger

StefanieSenger commented Feb 15, 2026 •

Copy link
Copy Markdown
Member Author

In permutation_test_score running metadata routing tests now indicate problems that seem independent of this PR:

FAILED model_selection/tests/test_validation.py::test_validation_functions_routing[permutation_test_score-extra_args4] - worker 'gw0' crashed while running 'model_selection/tests/test_validation.py::test_validation_functions_routing[permutation_test_score-extra_args4]'

Locally, I can see that the traceback gets stuck somewhere between the cloning and the parallelisation:

  File "/home/stefanie/code/scikit-learn_dev/scikit-learn/sklearn/base.py", line 131, in _clone_parametrized
    new_object._metadata_request = copy.deepcopy(estimator._metadata_request)
  File "/home/stefanie/code/scikit-learn_dev/scikit-learn/sklearn/base.py", line 394, in __sklearn_clone__
    return _clone_parametrized(self)
  File "/home/stefanie/code/scikit-learn_dev/scikit-learn/sklearn/base.py", line 94, in clone
    return estimator.__sklearn_clone__()
  File "/home/stefanie/code/scikit-learn_dev/scikit-learn/sklearn/model_selection/_validation.py", line 1670, in <genexpr>
    clone(estimator),
  File "/home/stefanie/code/scikit-learn_dev/scikit-learn/sklearn/utils/parallel.py", line 83, in <genexpr>
    iterable_with_config_and_warning_filters = (
  File "/home/stefanie/code/scikit-learn_dev/scikit-learn/sklearn/utils/parallel.py", line 91, in __call__
    return super().__call__(iterable_with_config_and_warning_filters)
  File "/home/stefanie/code/scikit-learn_dev/scikit-learn/sklearn/model_selection/_validation.py", line 1668, in permutation_test_score

Comment on lines -1563 to -1586
err_msg = "Fit parameter sample_weight has length 3; expected"
with pytest.raises(AssertionError, match=err_msg):
learning_curve(
estimator,
X,
y,
cv=3,
exploit_incremental_learning=True,
train_sizes=np.linspace(0.1, 1.0, 10),
error_score="raise",
params={"sample_weight": np.ones(3)},
)

learning_curve(
estimator,
X,
y,
cv=3,
exploit_incremental_learning=True,
train_sizes=np.linspace(0.1, 1.0, 10),
error_score="raise",
params={"sample_weight": np.ones(2)},
)

@StefanieSenger StefanieSenger Feb 15, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I have removed these tests:

  1. The first test now raises earlier (at fit_params_iter) and so this test has lost its purpose.
  2. The second test should also raise, because sample_weight of shape (2,) gets passed together with sample size 30. This should not be allowed. This test is wrong.

@StefanieSenger StefanieSenger Feb 17, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

A bit of digging why this test existed in the first place: It might be that it was intended to fit prior code where we had passed **fit_params as a whole into partial_fit like here:

if classes is None:
partial_fit_func = partial(estimator.partial_fit, **fit_params)
else:
partial_fit_func = partial(estimator.partial_fit, classes=classes, **fit_params)

Users had to cut down their sample_weight before passing it, which is surprising to me at a glance.

all_records = (
getattr(obj, "_records", dict()).get(method, dict()).get(parent, list())
)
assert all_records

@StefanieSenger StefanieSenger Feb 15, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Adding this check to prevent an empty to empty comparison in this check for the future.

This creates a lot of new failures in test_metaestimators_metadata_routing.py and other metadata tests. I haven't checked these yet. Probably a lot of them are legitimate.

@github-project-automation github-project-automation Bot moved this to Todo in Labs Feb 15, 2026
@StefanieSenger StefanieSenger moved this from Todo to In progress in Labs Feb 15, 2026
@StefanieSenger StefanieSenger added the Metadata Routing all issues related to metadata routing, slep006, sample props label Feb 15, 2026
(cross_val_predict, {}),
(learning_curve, {}),
(permutation_test_score, {}),
(permutation_test_score, {"n_permutations": 20}),

@StefanieSenger StefanieSenger Feb 17, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This addresses the issue, that a pytest worker stops running for permutation_test_score.

 FAILED model_selection/tests/test_validation.py::test_validation_functions_routing[permutation_test_score-extra_args4] - worker 'gw1' crashed while running 'model_selection/tests/test_validation.py::test_validation_functions_routing[permutation_test_score-extra_args4]'
= 73 failed, 33876 passed, 9173 skipped, 149 xfailed, 66 xpassed, 3932 warnings in 1006.19s (0:16:46) =

See CI output: https://github.com/scikit-learn/scikit-learn/actions/runs/22061768316/job/63743567324?pr=33285

I assume the cause is that _permutation_test_score, when run in joblib.Parallel() by default runs n_permutations=100 different processes, that all access scorer.registry and want to add something to it. That is fine because the order in which registry items are added does not matter. But seems that 100 processes is a bit much and reducing this number helps to run this test more stable.

This is only a temporary fix, I am afraid.

@StefanieSenger StefanieSenger Feb 18, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I have learned from @lesteve that in fact we are not running n_permutations=100 different parallel jobs, but n_jobs=1 by default in which we do 100 calculations.
Still, reducing n_permutations to 20 fixes the issue for this test. Maybe fixing the test is enough as long as we don't receive bug reports from users.

Comment on lines 409 to 414
check_recorded_metadata(
pipeline.steps_[0].transformer_,
method="transform",
parent="fit",
parent="wrapped",
sample_weight=w3,
)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This test still legitimately fails with AssertionError: Expected [1] vs [3]. Method: transform.
Needs further investigation: Aliases for w1, w2, w3 were not set. Does it resolve when set?

@StefanieSenger StefanieSenger Jun 1, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

After further investigation:

This still fails because parent="wrapped" above in line 398 comes from fit in the call stack, while parent="wrapped" here means coming from the predict method (it was wrongly labled "fit" before).

"wrapped" is not a unique function name in an estimator. The way record_metadata() tries to get the caller from the callstack is broken.

As a fix, we could route caller and callee as part of the metadata, but that seems a bit exaggerated, because we only need to fix a bunch of (important) tests.

I will check if we can do better educated guesses on inspect.stack() in record_metadata() than positionally.

@adrinjalali adrinjalali moved this from In progress to Todo in Labs Mar 2, 2026
@StefanieSenger StefanieSenger moved this from Todo to In progress in Labs Mar 9, 2026
@adrinjalali adrinjalali moved this from In progress to Todo in Labs Mar 16, 2026
@StefanieSenger

StefanieSenger commented Mar 23, 2026 •

Copy link
Copy Markdown
Member Author

The tests in sklearn/tests/test_metaestimators_metadata_routing.py maybe partially broken for the same reason.

I will split this PR into "repair tests" and "fix routing in learning_curve" and other single PR's if necessary.

@StefanieSenger StefanieSenger changed the title FIX Metadata routing in learning_curve and module tests FIX Metadata routing tests for `check_recorded_metadata?? May 18, 2026
@StefanieSenger
StefanieSenger marked this pull request as draft May 18, 2026 09:27
@StefanieSenger StefanieSenger changed the title FIX Metadata routing tests for `check_recorded_metadata?? FIX Metadata routing tests for check_recorded_metadata May 18, 2026
@StefanieSenger StefanieSenger moved this from Todo to In progress in Labs May 18, 2026
@StefanieSenger StefanieSenger added the Superseded PR has been replace by a newer PR label Jun 2, 2026
@StefanieSenger

Copy link
Copy Markdown
Member Author

Closing as superseded by #34179.

@github-project-automation github-project-automation Bot moved this from In progress to Done in Labs Jun 10, 2026
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 module:model_selection Superseded PR has been replace by a newer PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant