Repository navigation
FIX Metadata routing tests for check_recorded_metadata - #33285
StefanieSenger wants to merge 11 commits into
Conversation
learning_curvelearning_curve and module tests
|
In 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 |
| 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)}, | ||
| ) | ||
|
|
There was a problem hiding this comment.
I have removed these tests:
- The first test now raises earlier (at
fit_params_iter) and so this test has lost its purpose. - The second test should also raise, because
sample_weightof shape (2,) gets passed together with sample size 30. This should not be allowed. This test is wrong.
There was a problem hiding this comment.
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:
scikit-learn/sklearn/model_selection/_validation.py
Lines 2159 to 2162 in dce0550
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 |
There was a problem hiding this comment.
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.
| (cross_val_predict, {}), | ||
| (learning_curve, {}), | ||
| (permutation_test_score, {}), | ||
| (permutation_test_score, {"n_permutations": 20}), |
There was a problem hiding this comment.
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) =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.
There was a problem hiding this comment.
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.
| check_recorded_metadata( | ||
| pipeline.steps_[0].transformer_, | ||
| method="transform", | ||
| parent="fit", | ||
| parent="wrapped", | ||
| sample_weight=w3, | ||
| ) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
|
The tests in I will split this PR into "repair tests" and "fix routing in |
learning_curve and module testscheck_recorded_metadata
|
Closing as superseded by #34179. |
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: