Skip to content

Conversation

@sameshl
Copy link
Contributor

@sameshl sameshl commented Aug 14, 2019

This is a follow up on my PR #14645
This completes the replacement of assert_raises and assert_raises_regex with
the with pytest.raises context manager.

related to #14216

* from `sklearn/cluster/tests/test_affinity_propagation.py`
* from `sklearn/cluster/tests/test_bicluster.py`
* from `sklearn/cluster/tests/test_birch.py`
from `sklearn/cluster/tests/test_dbscan.py`
from `sklearn/cluster/tests/test_spectral.py`
* from `sklearn/cluster/tests/test_k_means.py`
Copy link
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.

2 parametrizations can be done easily. Otherwise LGTM.

@sameshl
Copy link
Contributor Author

sameshl commented Aug 15, 2019

@glemaitre Could you take a look at this? Let me know if anything else needs to be done.

Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

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

minimal comments but LGTM anyway.

Thanks for taking care of this @sameshl


model = SpectralBiclustering(n_components=3, n_best=4)
assert_raises(ValueError, model.fit, data)
with pytest.raises(ValueError):
Copy link
Member

Choose a reason for hiding this comment

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

Add this one to the parametrization?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@NicolasHug Can you explain how I can add this also in parametrization with already exsiting decorator?

Copy link
Member

Choose a reason for hiding this comment

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

By adding {'n_components': 3, 'n_best': 4} to the list

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By adding {'n_components': 3, 'n_best': 4} to the list

@NicolasHug I had some trouble making this parameterization. I already have

235 @pytest.mark.parametrize(
236     "args",
237     [{'n_clusters': (3, 3, 3)}, {'n_clusters': 'abc'},
238      {'n_clusters': (3, 'abc')}, {'method': 'unknown'},
239      {'n_components': 0}, {'n_best': 0},
240      {'svd_method': 'unknown'}]
241 )    
242 def test_errors(args):
243     data = np.arange(25).reshape((5, 5))
244 
245     model = SpectralBiclustering(**args)
246     with pytest.raises(ValueError):
247         model.fit(data)
248 
249     model = SpectralBiclustering(n_components=3, n_best=4)
250     with pytest.raises(ValueError):
251         model.fit(data)
252 
253     model = SpectralBiclustering()
254     data = np.arange(27).reshape((3, 3, 3))
255     with pytest.raises(ValueError):
256         model.fit(data)

Can you show me how the parameterization can be done?

Copy link
Member

@NicolasHug NicolasHug Aug 20, 2019

Choose a reason for hiding this comment

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

Here is how I would split this test:

@pytest.mark.parametrize(
    "args",
    [{'n_clusters': (3, 3, 3)},
     {'n_clusters': 'abc'},
     {'n_clusters': (3, 'abc')},
     {'method': 'unknown'},
     {'n_components': 0},
     {'n_best': 0},
     {'svd_method': 'unknown'},
     {'n_components': 3, 'n_best': 4}]
)
def test_errors(args):
    data = np.arange(25).reshape((5, 5))

    model = SpectralBiclustering(**args)
    with pytest.raises(ValueError):
        model.fit(data)


def test_wrong_shape():

    model = SpectralBiclustering()
    data = np.arange(27).reshape((3, 3, 3))
    with pytest.raises(ValueError):
        model.fit(data)

I added the {'n_components': 3, 'n_best': 4} case as a dict to the parametrization, and created a separate test function for the wrong shape test (it would be repeated for each case if we kept it in the original function)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I understand now! Thanks for the nice explanation 😃

@sameshl
Copy link
Contributor Author

sameshl commented Aug 20, 2019

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

@NicolasHug NicolasHug merged commit 82e06af into scikit-learn:master Aug 22, 2019
@NicolasHug
Copy link
Member

Merging, thanks @sameshl !

@sameshl sameshl deleted the cluster_assert_raises branch August 22, 2019 16:10
@sameshl
Copy link
Contributor Author

sameshl commented Aug 22, 2019

Thanks for helping me out @NicolasHug!

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