|
10 | 10 | from sklearn.naive_bayes import GaussianNB |
11 | 11 | from sklearn.preprocessing import StandardScaler |
12 | 12 | from sklearn.utils.validation import check_X_y |
| 13 | +import pytest |
13 | 14 |
|
14 | 15 | from skmatter.decomposition import PCovC |
15 | 16 |
|
@@ -186,9 +187,10 @@ def test_select_sample_space(self): |
186 | 187 | n_samples = 2 |
187 | 188 |
|
188 | 189 | # select range where there are at least 2 classes in Y |
189 | | - pcovc.fit(self.X[49 : 49 + n_samples], self.Y[49 : 49 + n_samples]) |
| 190 | + with pytest.warns(match="class does not automatically center data"): |
| 191 | + pcovc.fit(self.X[49 : 49 + n_samples], self.Y[49 : 49 + n_samples]) |
190 | 192 |
|
191 | | - self.assertTrue(pcovc.space_ == "sample") |
| 193 | + assert pcovc.space_ == "sample" |
192 | 194 |
|
193 | 195 | def test_bad_space(self): |
194 | 196 | """ |
@@ -397,25 +399,20 @@ def test_centering(self): |
397 | 399 | """ |
398 | 400 | pcovc = self.model(n_components=2, tol=1e-12) |
399 | 401 | X = self.X.copy() + np.random.uniform(-1, 1, self.X.shape[1]) |
400 | | - with warnings.catch_warnings(record=True) as w: |
| 402 | + m = ( |
| 403 | + "This class does not automatically center data, and your data mean is " |
| 404 | + "greater than the supplied tolerance." |
| 405 | + ) |
| 406 | + with pytest.warns(match=m): |
401 | 407 | pcovc.fit(X, self.Y) |
402 | | - self.assertEqual( |
403 | | - str(w[0].message), |
404 | | - "This class does not automatically center data, and your data " |
405 | | - "mean is greater than the supplied tolerance.", |
406 | | - ) |
407 | 408 |
|
408 | 409 | def test_z_scaling(self): |
409 | 410 | """ |
410 | 411 | Check that PCovC raises a warning if Z is not of scale, and does not |
411 | 412 | if it is. |
412 | 413 | """ |
413 | 414 | pcovc = self.model(n_components=2, scale_z=True) |
414 | | - |
415 | | - with warnings.catch_warnings(): |
416 | | - pcovc.fit(self.X, self.Y) |
417 | | - warnings.simplefilter("error") |
418 | | - self.assertEqual(1 + 1, 2) |
| 415 | + pcovc.fit(self.X, self.Y) |
419 | 416 |
|
420 | 417 | pcovc = self.model(n_components=2, scale_z=False, z_mean_tol=0, z_var_tol=0) |
421 | 418 |
|
@@ -577,9 +574,11 @@ def test_incompatible_classifier(self): |
577 | 574 | def test_none_classifier(self): |
578 | 575 | pcovc = PCovC(mixing=0.5, classifier=None) |
579 | 576 |
|
580 | | - pcovc.fit(self.X, self.Y) |
581 | | - self.assertTrue(pcovc.classifier is None) |
582 | | - self.assertTrue(pcovc.classifier_ is not None) |
| 577 | + with pytest.warns(match="class does not automatically scale Z"): |
| 578 | + pcovc.fit(self.X, self.Y) |
| 579 | + |
| 580 | + assert pcovc.classifier is None |
| 581 | + assert pcovc.classifier_ is not None |
583 | 582 |
|
584 | 583 | def test_incompatible_coef_shape(self): |
585 | 584 | cl_multi = LogisticRegression() |
@@ -617,6 +616,7 @@ def test_scale_z_parameter(self): |
617 | 616 |
|
618 | 617 | pcovc_unscaled = self.model(scale_z=False) |
619 | 618 | pcovc_unscaled.fit(self.X, self.Y) |
| 619 | + |
620 | 620 | assert not np.allclose( |
621 | 621 | pcovc_scaled.singular_values_, pcovc_unscaled.singular_values_ |
622 | 622 | ) |
|
0 commit comments