1+ from email import message
12import unittest
23import warnings
34
1011from sklearn .naive_bayes import GaussianNB
1112from sklearn .preprocessing import StandardScaler
1213from sklearn .utils .validation import check_X_y
14+ import pytest
1315
1416from skmatter .decomposition import PCovC
1517
@@ -186,9 +188,10 @@ def test_select_sample_space(self):
186188 n_samples = 2
187189
188190 # 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 ])
191+ with pytest .warns (match = "class does not automatically center data" ):
192+ pcovc .fit (self .X [49 : 49 + n_samples ], self .Y [49 : 49 + n_samples ])
190193
191- self . assertTrue ( pcovc .space_ == "sample" )
194+ assert pcovc .space_ == "sample"
192195
193196 def test_bad_space (self ):
194197 """
@@ -397,13 +400,12 @@ def test_centering(self):
397400 """
398401 pcovc = self .model (n_components = 2 , tol = 1e-12 )
399402 X = self .X .copy () + np .random .uniform (- 1 , 1 , self .X .shape [1 ])
400- with warnings .catch_warnings (record = True ) as w :
403+ m = (
404+ "This class does not automatically center data, and your data mean is "
405+ "greater than the supplied tolerance."
406+ )
407+ with pytest .warns (match = m ):
401408 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- )
407409
408410 def test_z_scaling (self ):
409411 """
@@ -412,10 +414,8 @@ def test_z_scaling(self):
412414 """
413415 pcovc = self .model (n_components = 2 , scale_z = True )
414416
415- with warnings . catch_warnings ( ):
417+ with pytest . warns ( match = "class does not automatically center Z" ):
416418 pcovc .fit (self .X , self .Y )
417- warnings .simplefilter ("error" )
418- self .assertEqual (1 + 1 , 2 )
419419
420420 pcovc = self .model (n_components = 2 , scale_z = False , z_mean_tol = 0 , z_var_tol = 0 )
421421
@@ -491,7 +491,9 @@ def test_decision_function(self):
491491
492492 def test_default_ncomponents (self ):
493493 pcovc = PCovC (mixing = 0.5 )
494- pcovc .fit (self .X , self .Y )
494+
495+ with pytest .warns (match = "class does not automatically center data" ):
496+ pcovc .fit (self .X , self .Y )
495497
496498 self .assertEqual (pcovc .n_components_ , min (self .X .shape ))
497499
@@ -577,9 +579,11 @@ def test_incompatible_classifier(self):
577579 def test_none_classifier (self ):
578580 pcovc = PCovC (mixing = 0.5 , classifier = None )
579581
580- pcovc .fit (self .X , self .Y )
581- self .assertTrue (pcovc .classifier is None )
582- self .assertTrue (pcovc .classifier_ is not None )
582+ with pytest .warns (match = "class does not automatically scale Z" ):
583+ pcovc .fit (self .X , self .Y )
584+
585+ assert pcovc .classifier is None
586+ assert pcovc .classifier_ is not None
583587
584588 def test_incompatible_coef_shape (self ):
585589 cl_multi = LogisticRegression ()
@@ -613,10 +617,15 @@ def test_incompatible_coef_shape(self):
613617 def test_scale_z_parameter (self ):
614618 """Check that changing scale_z changes the eigendecomposition."""
615619 pcovc_scaled = self .model (scale_z = True )
616- pcovc_scaled .fit (self .X , self .Y )
620+
621+ with pytest .warns (m = "class does not automatically center Z" ):
622+ pcovc_scaled .fit (self .X , self .Y )
617623
618624 pcovc_unscaled = self .model (scale_z = False )
619- pcovc_unscaled .fit (self .X , self .Y )
625+
626+ with pytest .warns (m = "class does not automatically center Z" ):
627+ pcovc_unscaled .fit (self .X , self .Y )
628+
620629 assert not np .allclose (
621630 pcovc_scaled .singular_values_ , pcovc_unscaled .singular_values_
622631 )
0 commit comments