From a6de10e20e10c838c2e4b6a613612afce95127b7 Mon Sep 17 00:00:00 2001 From: Philip Loche Date: Tue, 23 Dec 2025 09:49:00 +0100 Subject: [PATCH] Fix some test warnings --- CHANGELOG | 1 + pyproject.toml | 5 ++- src/skmatter/_selection.py | 4 +-- src/skmatter/decomposition/_kpcov.py | 4 +-- src/skmatter/decomposition/_pcov.py | 4 +-- src/skmatter/sample_selection/_voronoi_fps.py | 2 +- tests/test_kernel_pcovc.py | 8 ++--- tests/test_pcovc.py | 32 +++++++++---------- tests/test_pcovr.py | 31 +++++++++--------- 9 files changed, 46 insertions(+), 45 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 8786d6aa9..1a8f0a1a4 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -13,6 +13,7 @@ The rules for CHANGELOG file: Unreleased ---------- +- Fix a couple of ``DeprecationWarnings`` and ``UserWarnings`` (#280) - Fix PCovC scaling (#270) - Refactor of reconstruction measures(#275) - Code cleanup of Base classes (#264) diff --git a/pyproject.toml b/pyproject.toml index 28cb577ec..54317824f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -83,13 +83,16 @@ include = [ output = 'tests/coverage.xml' [tool.pytest.ini_options] -testpaths = ["tests"] addopts = [ "--cov", "--cov-append", "--cov-report=", "--import-mode=append", ] +# filterwarnings = [ +# "error", +# ] +testpaths = ["tests"] [tool.ruff] exclude = ["docs/src/examples/", "src/torchpme/_version.py"] diff --git a/src/skmatter/_selection.py b/src/skmatter/_selection.py index dca3a7810..27dd0226c 100644 --- a/src/skmatter/_selection.py +++ b/src/skmatter/_selection.py @@ -1019,7 +1019,7 @@ def _update_hausdorff(self, X, y, last_selected): ) # update in-place the Hausdorff distance list - np.minimum(self.hausdorff_, new_dist, self.hausdorff_) + np.minimum(self.hausdorff_, new_dist, out=self.hausdorff_) def _update_post_selection(self, X, y, last_selected): """ @@ -1163,7 +1163,7 @@ def _update_hausdorff(self, X, y, last_selected): ) # update in-place the Hausdorff distance list - np.minimum(self.hausdorff_, new_dist, self.hausdorff_) + np.minimum(self.hausdorff_, new_dist, out=self.hausdorff_) def _update_post_selection(self, X, y, last_selected): """Saves the most recent selections, increments the counter, and, recomputes diff --git a/src/skmatter/decomposition/_kpcov.py b/src/skmatter/decomposition/_kpcov.py index 5436d3ef4..2a7851b21 100644 --- a/src/skmatter/decomposition/_kpcov.py +++ b/src/skmatter/decomposition/_kpcov.py @@ -12,7 +12,7 @@ from sklearn.decomposition._pca import _infer_dimension from sklearn.utils import check_random_state from sklearn.utils._arpack import _init_arpack_v0 -from sklearn.utils.extmath import randomized_svd, stable_cumsum, svd_flip +from sklearn.utils.extmath import randomized_svd, svd_flip from sklearn.utils.validation import check_is_fitted from sklearn.utils.validation import validate_data from sklearn.metrics.pairwise import pairwise_kernels @@ -258,7 +258,7 @@ def _decompose_full(self, mat): # side='right' ensures that number of features selected # their variance is always greater than self.n_components_ float # passed. More discussion in issue: #15669 - ratio_cumsum = stable_cumsum(explained_variance_ratio_) + ratio_cumsum = np.cumulative_sum(explained_variance_ratio_) self.n_components_ = ( np.searchsorted(ratio_cumsum, self.n_components_, side="right") + 1 ) diff --git a/src/skmatter/decomposition/_pcov.py b/src/skmatter/decomposition/_pcov.py index e490b774d..005c531d7 100644 --- a/src/skmatter/decomposition/_pcov.py +++ b/src/skmatter/decomposition/_pcov.py @@ -14,7 +14,7 @@ from sklearn.linear_model._base import LinearModel from sklearn.utils import check_random_state from sklearn.utils._arpack import _init_arpack_v0 -from sklearn.utils.extmath import randomized_svd, stable_cumsum, svd_flip +from sklearn.utils.extmath import randomized_svd, svd_flip from sklearn.utils.validation import check_is_fitted from skmatter.utils import pcovr_covariance, pcovr_kernel @@ -288,7 +288,7 @@ def _decompose_full(self, mat): # side='right' ensures that number of features selected # their variance is always greater than self.n_components_ float # passed. More discussion in issue: #15669 - ratio_cumsum = stable_cumsum(explained_variance_ratio_) + ratio_cumsum = np.cumulative_sum(explained_variance_ratio_) self.n_components_ = ( np.searchsorted(ratio_cumsum, self.n_components_, side="right") + 1 ) diff --git a/src/skmatter/sample_selection/_voronoi_fps.py b/src/skmatter/sample_selection/_voronoi_fps.py index aeedbce97..4c6935434 100644 --- a/src/skmatter/sample_selection/_voronoi_fps.py +++ b/src/skmatter/sample_selection/_voronoi_fps.py @@ -312,7 +312,7 @@ def _update_post_selection(self, X, y, last_selected): updated_points = np.where(self.new_dist_ < self.hausdorff_)[0] np.minimum( - self.hausdorff_, self.new_dist_, self.hausdorff_, casting="unsafe" + self.hausdorff_, self.new_dist_, out=self.hausdorff_, casting="unsafe" ) else: updated_points = np.array([]) diff --git a/tests/test_kernel_pcovc.py b/tests/test_kernel_pcovc.py index 0809a480c..d632139f8 100644 --- a/tests/test_kernel_pcovc.py +++ b/tests/test_kernel_pcovc.py @@ -10,6 +10,7 @@ from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LogisticRegression, RidgeClassifier from sklearn.metrics.pairwise import pairwise_kernels +import pytest from skmatter.decomposition import KernelPCovC @@ -337,6 +338,7 @@ def test_scale_z_parameter(self): kpcovc_unscaled = self.model(scale_z=False) kpcovc_unscaled.fit(self.X, self.Y) + assert not np.allclose(kpcovc_scaled.pkt_, kpcovc_unscaled.pkt_) def test_z_scaling(self): @@ -345,11 +347,7 @@ def test_z_scaling(self): if it is. """ kpcovc = self.model(n_components=2, scale_z=True) - - with warnings.catch_warnings(): - kpcovc.fit(self.X, self.Y) - warnings.simplefilter("error") - self.assertEqual(1 + 1, 2) + kpcovc.fit(self.X, self.Y) kpcovc = self.model(n_components=2, scale_z=False, z_mean_tol=0, z_var_tol=0) diff --git a/tests/test_pcovc.py b/tests/test_pcovc.py index f552323ee..3c768347c 100644 --- a/tests/test_pcovc.py +++ b/tests/test_pcovc.py @@ -10,6 +10,7 @@ from sklearn.naive_bayes import GaussianNB from sklearn.preprocessing import StandardScaler from sklearn.utils.validation import check_X_y +import pytest from skmatter.decomposition import PCovC @@ -186,9 +187,10 @@ def test_select_sample_space(self): n_samples = 2 # select range where there are at least 2 classes in Y - pcovc.fit(self.X[49 : 49 + n_samples], self.Y[49 : 49 + n_samples]) + with pytest.warns(match="class does not automatically center data"): + pcovc.fit(self.X[49 : 49 + n_samples], self.Y[49 : 49 + n_samples]) - self.assertTrue(pcovc.space_ == "sample") + assert pcovc.space_ == "sample" def test_bad_space(self): """ @@ -397,13 +399,12 @@ def test_centering(self): """ pcovc = self.model(n_components=2, tol=1e-12) X = self.X.copy() + np.random.uniform(-1, 1, self.X.shape[1]) - with warnings.catch_warnings(record=True) as w: + m = ( + "This class does not automatically center data, and your data mean is " + "greater than the supplied tolerance." + ) + with pytest.warns(match=m): pcovc.fit(X, self.Y) - self.assertEqual( - str(w[0].message), - "This class does not automatically center data, and your data " - "mean is greater than the supplied tolerance.", - ) def test_z_scaling(self): """ @@ -411,11 +412,7 @@ def test_z_scaling(self): if it is. """ pcovc = self.model(n_components=2, scale_z=True) - - with warnings.catch_warnings(): - pcovc.fit(self.X, self.Y) - warnings.simplefilter("error") - self.assertEqual(1 + 1, 2) + pcovc.fit(self.X, self.Y) pcovc = self.model(n_components=2, scale_z=False, z_mean_tol=0, z_var_tol=0) @@ -577,9 +574,11 @@ def test_incompatible_classifier(self): def test_none_classifier(self): pcovc = PCovC(mixing=0.5, classifier=None) - pcovc.fit(self.X, self.Y) - self.assertTrue(pcovc.classifier is None) - self.assertTrue(pcovc.classifier_ is not None) + with pytest.warns(match="class does not automatically scale Z"): + pcovc.fit(self.X, self.Y) + + assert pcovc.classifier is None + assert pcovc.classifier_ is not None def test_incompatible_coef_shape(self): cl_multi = LogisticRegression() @@ -617,6 +616,7 @@ def test_scale_z_parameter(self): pcovc_unscaled = self.model(scale_z=False) pcovc_unscaled.fit(self.X, self.Y) + assert not np.allclose( pcovc_scaled.singular_values_, pcovc_unscaled.singular_values_ ) diff --git a/tests/test_pcovr.py b/tests/test_pcovr.py index 0b5dfcb1d..37de2e650 100644 --- a/tests/test_pcovr.py +++ b/tests/test_pcovr.py @@ -1,5 +1,4 @@ import unittest -import warnings import numpy as np from sklearn import exceptions @@ -9,6 +8,7 @@ from sklearn.linear_model import Ridge from sklearn.preprocessing import StandardScaler from sklearn.utils.validation import check_X_y +import pytest from skmatter.decomposition import PCovR @@ -167,9 +167,11 @@ def test_select_sample_space(self): pcovr = self.model(n_components=2, tol=1e-12) n_samples = self.X.shape[1] - 1 - pcovr.fit(self.X[:n_samples], self.Y[:n_samples]) - self.assertTrue(pcovr.space_ == "sample") + with pytest.warns(match="class does not automatically center data"): + pcovr.fit(self.X[:n_samples], self.Y[:n_samples]) + + assert pcovr.space_ == "sample" def test_bad_space(self): """ @@ -280,13 +282,11 @@ def test_good_n_components(self): def test_bad_n_components(self): """Check that PCovR will not work with any prohibited values of n_components.""" - with self.assertRaises(ValueError) as cm: - pcovr = self.model(n_components="mle", svd_solver="full") - pcovr.fit(self.X[:2], self.Y[:2]) - self.assertEqual( - str(cm.exception), - "n_components='mle' is only supported if n_samples >= n_features", - ) + pcovr = self.model(n_components="mle", svd_solver="full") + m = "n_components='mle' is only supported if n_samples >= n_features" + with pytest.raises(ValueError, match=m): + with pytest.warns(match="class does not automatically center data"): + pcovr.fit(self.X[:2], self.Y[:2]) with self.subTest(type="negative_ncomponents"): with self.assertRaises(ValueError) as cm: @@ -376,13 +376,12 @@ def test_centering(self): """ pcovr = self.model(n_components=2, tol=1e-12) X = self.X.copy() + np.random.uniform(-1, 1, self.X.shape[1]) - with warnings.catch_warnings(record=True) as w: + m = ( + "This class does not automatically center data, and your data mean is " + "greater than the supplied tolerance." + ) + with pytest.warns(match=m): pcovr.fit(X, self.Y) - self.assertEqual( - str(w[0].message), - "This class does not automatically center data, and your data mean is " - "greater than the supplied tolerance.", - ) def test_T_shape(self): """Check that PCovR returns a latent space projection consistent with the shape