Skip to content

MAINT:Fix assert raises in sklearn/preprocessing/tests/#14717

Merged
rth merged 13 commits intoscikit-learn:masterfrom
sameshl:tests_preprocessing
Aug 29, 2019
Merged

MAINT:Fix assert raises in sklearn/preprocessing/tests/#14717
rth merged 13 commits intoscikit-learn:masterfrom
sameshl:tests_preprocessing

Conversation

@sameshl
Copy link
Copy Markdown
Contributor

@sameshl sameshl commented Aug 22, 2019

replaced assert_raises and assert_raises_regex with pytest.raises context manager.

related to #14216

@glemaitre
Copy link
Copy Markdown
Member

Same as other PR: you can also change assert_raise_message

" greater than the number of samples used. Got"
" 1000 quantiles and 10 samples.",
QuantileTransformer(subsample=10).fit, X)
with pytest.raises(ValueError, match="Invalid value for "
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

a nicer way to be able to read it is to move the error message above pytest.raises

err_msg = "Invalid value for 'n_quantiles':0."
with pytest.raises(ValueError, match=err_msg
)

You can even make a for loop (you should parametrize but let's keep for another PR)

all_err_msg = ("Invalid value for 'n_quantiles': 0.",
               "Invalid value for 'subsample': 0.",
               ...)
all_params = ({'n_quantiles': 0.}, {'subsample': 0.}, {'subsample': 10})
for params, err_msg in zip(all_params, all_err_msg):
    with pytest.raises(ValueError, match=err_msg):
        QuantileTransformer(**params).fit(X)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion @glemaitre. Will move the errror msg out of pytest.raises. Will open another PR for parameterization purpose.

assert_raises_regex(ValueError, "QuantileTransformer only accepts "
"non-negative sparse matrices.",
transformer.fit, X_neg)
with pytest.raises(ValueError, match="QuantileTransformer only accepts "
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same comments regarding the error message below. Try to move them out of raises and when possible you can loop.

ValueError,
"`drop` should have length equal to the number",
enc.fit, [['abc', 2, 55], ['def', 1, 55], ['def', 3, 59]])
with pytest.raises(ValueError,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

move out the message

Copy link
Copy Markdown
Member

@glemaitre glemaitre left a comment

Choose a reason for hiding this comment

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

Otherwise, looks good.

@sameshl
Copy link
Copy Markdown
Contributor Author

sameshl commented Aug 28, 2019

@glemaitre I have made the required changes. Let me know if anything else needs to be done

Copy link
Copy Markdown
Member

@rth rth left a comment

Choose a reason for hiding this comment

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

Few comments, appart from that LGTM. Thanks

assert_raise_message(ValueError,
"n_bins must be a scalar or array of shape "
"(n_features,).", est.fit_transform, X)
err_msg = "n_bins must be a scalar or array of shape (n_features,)."
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
err_msg = "n_bins must be a scalar or array of shape (n_features,)."
err_msg = r"n_bins must be a scalar or array of shape \(n_features,\)."

then remove the double .replace below.

"(n_features,).", est.fit_transform, X)
err_msg = "n_bins must be a scalar or array of shape (n_features,)."
with pytest.raises(ValueError,
match=err_msg.replace('(', r'\(').replace(')', r'\)')):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same as above

"(n_features,).", est.fit_transform, X)
err_msg = "n_bins must be a scalar or array of shape (n_features,)."
with pytest.raises(ValueError,
match=err_msg.replace('(', r'\(').replace(')', r'\)')):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same as above

"('onehot', 'onehot-dense', 'ordinal'). "
"Got encode='invalid-encode' instead.")
with pytest.raises(ValueError,
match=err_msg.replace('(', r'\(').replace(')', r'\)')):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

same as above

"('uniform', 'quantile', 'kmeans'). "
"Got strategy='invalid-strategy' instead.")
with pytest.raises(ValueError,
match=err_msg.replace('(', r'\(').replace(')', r'\)')):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

same as above

@sameshl
Copy link
Copy Markdown
Contributor Author

sameshl commented Aug 28, 2019

Thanks for the suggestions @rth. I have made them

@sameshl
Copy link
Copy Markdown
Contributor Author

sameshl commented Aug 29, 2019

@rth Let me know if anything else needs to be done.

@rth rth dismissed glemaitre’s stale review August 29, 2019 09:38

Comments were addressed I think.

Copy link
Copy Markdown
Member

@rth rth left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @sameshl !

@rth rth merged commit 1c11680 into scikit-learn:master Aug 29, 2019
@sameshl sameshl deleted the tests_preprocessing branch August 29, 2019 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants