Repository navigation
[MRG+2] Add common test for set_params behavior - #7760
Conversation
| # check that we have a set_params and can clone | ||
| msg = "it does not implement a 'get_params' methods" | ||
| assert_raises_regex(TypeError, msg, check_estimator, object) | ||
| # check that properties can be set |
There was a problem hiding this comment.
wow I kinda forgot that I wrote this test. This looks great!
|
Maybe add a stronger test that ensures idempotence as you pointed out? You could have a getter and setter but they are not inverse of each other, like a |
|
Is it against sklearn's conventions if a set_param fails due to TypeError, etc? If the getter and setter do processing, then they can fail for all kinds of reasons. |
|
There is no conventions on what errors can be raised where, I think. I'm not quite sure what you're getting at. |
|
If Currently the contributor guidelines say:
What I am wondering is if property-params are subject to this rule, or not. Finally, the reason I ask this is because I'm not sure how to test invertibliilty/idempotence in a general way except to Something like If property-setters raise Exceptions then that wouldn't work. |
| # true for mixins | ||
| return | ||
| params = estimator.get_params() | ||
| estimator.set_params(**params) |
There was a problem hiding this comment.
Please put a comment in here to describe what this is testing.
Also: Should we do further checking that get_params() returns the same thing before and after this statement.
|
@absolutelyNoWarranty Sorry I don't get your point about raising errors.
Again, I'm not sure what you're asking. The rule says that you shouldn't do validation in |
f865379 to
96958a2
Compare
|
Can you please fix the pep8 error? |
|
The linter still says no: |
| estimator.set_params(**{param_name: value}) | ||
| get_value = estimator.get_params()[param_name] | ||
| except: | ||
| # triggered some parameter validation |
There was a problem hiding this comment.
Wait I don't get this. Shouldn't we error on this? Or is that legal as long as we don't touch it?
There was a problem hiding this comment.
Sorry, I didn't realize there had been comments.
Here we don't error because we just want to check that get_value is value if value is successfully set.
| pass | ||
| else: | ||
| errmsg = ("get_params does not match set_params: " | ||
| "called set_params of {0} with {1}={2} " |
There was a problem hiding this comment.
if you're gonna use explicit numbers you could have not passed param_name twice ;)
| param_name, get_value) | ||
| assert_equal(value, get_value, errmsg) | ||
|
|
||
| with ignore_warnings(category=DeprecationWarning): |
There was a problem hiding this comment.
Just to re-make estimator as in lines 1637-1641.
|
@jnothman are we now doing "approve changes" instead of "[MRG + 1]"? |
|
No, I currently haven't found an easy way to say "I don't want a cross next to my review so far because the changes I requested have been made, but I haven't done a full review of the code either". Perhaps I should just dismiss reviews more often. |
|
@jnothman Can't you change it to "comment"? (I don't actually know). I only use "comment" so far, though that is clearly also not ideal |
|
@absolutelyNoWarranty you need to rebase. |
5a89deb to
85da7cd
Compare
|
Updated and rebased. |
85da7cd to
7a7743a
Compare
7a7743a to
f66f1cf
Compare
| @ignore_warnings(category=(DeprecationWarning, FutureWarning)) | ||
| def check_set_params(name, estimator_orig): | ||
| if name in META_ESTIMATORS: | ||
| return |
There was a problem hiding this comment.
Why does this never get executed? And why is it necessary? Surely as long as the object has been constructed, this should work fine.
| try: | ||
| estimator.set_params(**{param_name: value}) | ||
| get_value = estimator.get_params()[param_name] | ||
| except: |
There was a problem hiding this comment.
Please use except Exception. bare except catches KeyboardInterrupt, for instance.
There was a problem hiding this comment.
Or perhaps except (ValueError, TypeError)?
| "but get_params returns {1}={3}") | ||
| errmsg = errmsg.format(name, param_name, value, | ||
| get_value) | ||
| assert_equal(value, get_value, errmsg) |
There was a problem hiding this comment.
should we be asserting equality (==) or identity (is)?
| get_value) | ||
| assert_equal(value, get_value, errmsg) | ||
|
|
||
| estimator = clone(estimator_orig) |
There was a problem hiding this comment.
why not put this at the beginning of the loop, rather than doing one more clone than necessary per estimator?
| pass | ||
| else: | ||
| new_params = estimator.get_params() | ||
| assert_equal(params.keys(), new_params.keys(), msg) |
There was a problem hiding this comment.
Testing for identity doesn't work or keys even if the dictionary really is the same dictionary.
>>> d = {'a':1, 'b':2}
>>> d is d
True
>>> d.keys() is d.keys()
False
There was a problem hiding this comment.
Sorry. I misread. But actually, I think that we are not assured that this equality check will work. In py2 this value is a list, in iteration order which is not to be assumed deterministic
| assert_greater = _dummy.assertGreater | ||
| assert_less_equal = _dummy.assertLessEqual | ||
| assert_greater_equal = _dummy.assertGreaterEqual | ||
| assert_is = _dummy.assertIs |
There was a problem hiding this comment.
Since we no longer use nose,
assert X is Y, msg
is actually sufficient without a helper.
| # before and after set_params() with some fuzz | ||
| estimator = clone(estimator_orig) | ||
|
|
||
| params = estimator.get_params() |
There was a problem hiding this comment.
I think we should be using deep=False, as we will test each estimator separately ...
| try: | ||
| estimator.set_params(**params) | ||
| except Exception: | ||
| # triggered some parameter validation |
There was a problem hiding this comment.
In the ideal world, we might not permit this validation outside of fit either. I think we should consider raising a warning of some kind where this case is over-used.
There was a problem hiding this comment.
I see your point but when is a developer expected to see the warning? As part of the (massive) output of the test suite?
There was a problem hiding this comment.
the output of the test-suite should not be massive. There are a lot of issues in master because we were not careful about handling warnings.
There was a problem hiding this comment.
+1 for raising a warning (or even error?) here. Did this error for all_estimators? where? Usually each new test in estimator_checks requires fixes to the codebase. This one here is probably the one that should have been fixed. Not having any errors on the codebase is suspicious to me.
There was a problem hiding this comment.
For a second I thought running this on a deep estimator would fail, but I guess we avoid this by doing deep=False. It would be really nice to run estimator_checks on some deep estimators though (I tried and ran into a whole host of problems)
|
Please add an entry to the change log at |
|
The idea is that developers run check_estimator on a smaller set of objects
than we do here, and that they may be more diligent than we are about
responding to warnings!
|
Always the optimist. Gonna do another round now, but maybe point out what the red tape is you're concerned about? |
|
|
||
| class TestCase(unittest.TestCase): | ||
| longMessage = False | ||
| longMessage = True |
There was a problem hiding this comment.
I don't understand your comment. If it's true by default, why would someone set it to True?
| try: | ||
| estimator.set_params(**params) | ||
| except Exception: | ||
| # triggered some parameter validation |
There was a problem hiding this comment.
the output of the test-suite should not be massive. There are a lot of issues in master because we were not careful about handling warnings.
| try: | ||
| estimator.set_params(**params) | ||
| except Exception: | ||
| # triggered some parameter validation |
There was a problem hiding this comment.
+1 for raising a warning (or even error?) here. Did this error for all_estimators? where? Usually each new test in estimator_checks requires fixes to the codebase. This one here is probably the one that should have been fixed. Not having any errors on the codebase is suspicious to me.
| try: | ||
| estimator.set_params(**params) | ||
| except Exception: | ||
| # triggered some parameter validation |
There was a problem hiding this comment.
For a second I thought running this on a deep estimator would fail, but I guess we avoid this by doing deep=False. It would be really nice to run estimator_checks on some deep estimators though (I tried and ran into a whole host of problems)
| assert_raises_regex(AssertionError, msg, check_estimator, | ||
| ModifiesValueInsteadOfRaisingError()) | ||
| check_estimator(RaisesError()) | ||
| assert_raises_regex(AssertionError, msg, check_estimator, |
There was a problem hiding this comment.
can you maybe run the test on a pipeline to make sure it works on deep estimators?
|
@jnothman @amueller |
|
yes, for good or bad, some estimators currently validate upon setting. I
don't think we need to be bureaucratic/frameworkish about this - such
exceptions are pretty harmless - and should focus on the essence of the PR:
testing that set_params can be used.
I'd be tempted, however, to make it only catch ValueErrors, not all
Exceptions.
|
| private settings apart from parameters during estimator initialization. | ||
| :issue:`9378` by :user:`Herilalaina Rakotoarison <herilalaina>` | ||
|
|
||
| - Added :func:`estimator_checks.check_set_params` test which checks that |
There was a problem hiding this comment.
This link won't work as we don't generate docs for estimator checks (though perhaps we should)
| :issue:`9378` by :user:`Herilalaina Rakotoarison <herilalaina>` | ||
|
|
||
| - Added :func:`estimator_checks.check_set_params` test which checks that | ||
| `set_params` is equivalent to passing parameters in `__init__` and |
|
Should I squash and rebase? |
|
Don't squash and rebase. We'll do that upon merge. Otherwise it makes it
hard to track what's changed.
|
No longer needed as we are using pytests
GaelVaroquaux
left a comment
There was a problem hiding this comment.
LGTM.
I pushed a change remove assert_is. Once the CI are green, I will merge.
|
Merged. Thank you! |
…? / check_estimator Pin 074a5216f8e35288045455ccda37f87a19d4cbde and its regression selectors for reproducible runs. Source: scikit-learn/scikit-learn#7760
Reference Issue
Fix #7738
What does this implement/fix? Explain your changes.
Testset_paramsby addingestimator.set_params(**params)tocheck_parameters_default_constructibleTest
set_paramsby adding acheck_set_paramsto theestimator_checksmodule.