Skip to content

Commit c45947e

Browse files
committed
Treat warnings as errors in tests
1 parent d733683 commit c45947e

9 files changed

Lines changed: 66 additions & 63 deletions

File tree

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The rules for CHANGELOG file:
1313

1414
Unreleased
1515
----------
16+
- Fix a couple of ``DeprecationWarnings`` and ``UserWarnings`` (#XXX)
1617
- Fix PCovC scaling (#270)
1718
- Refactor of reconstruction measures(#275)
1819
- Code cleanup of Base classes (#264)

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,16 @@ include = [
8383
output = 'tests/coverage.xml'
8484

8585
[tool.pytest.ini_options]
86-
testpaths = ["tests"]
8786
addopts = [
8887
"--cov",
8988
"--cov-append",
9089
"--cov-report=",
9190
"--import-mode=append",
9291
]
92+
filterwarnings = [
93+
"error",
94+
]
95+
testpaths = ["tests"]
9396

9497
[tool.ruff]
9598
exclude = ["docs/src/examples/", "src/torchpme/_version.py"]

src/skmatter/_selection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ def _update_hausdorff(self, X, y, last_selected):
10191019
)
10201020

10211021
# update in-place the Hausdorff distance list
1022-
np.minimum(self.hausdorff_, new_dist, self.hausdorff_)
1022+
np.minimum(self.hausdorff_, new_dist, out=self.hausdorff_)
10231023

10241024
def _update_post_selection(self, X, y, last_selected):
10251025
"""
@@ -1163,7 +1163,7 @@ def _update_hausdorff(self, X, y, last_selected):
11631163
)
11641164

11651165
# update in-place the Hausdorff distance list
1166-
np.minimum(self.hausdorff_, new_dist, self.hausdorff_)
1166+
np.minimum(self.hausdorff_, new_dist, out=self.hausdorff_)
11671167

11681168
def _update_post_selection(self, X, y, last_selected):
11691169
"""Saves the most recent selections, increments the counter, and, recomputes

src/skmatter/decomposition/_kpcov.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from sklearn.decomposition._pca import _infer_dimension
1313
from sklearn.utils import check_random_state
1414
from sklearn.utils._arpack import _init_arpack_v0
15-
from sklearn.utils.extmath import randomized_svd, stable_cumsum, svd_flip
15+
from sklearn.utils.extmath import randomized_svd, svd_flip
1616
from sklearn.utils.validation import check_is_fitted
1717
from sklearn.utils.validation import validate_data
1818
from sklearn.metrics.pairwise import pairwise_kernels
@@ -258,7 +258,7 @@ def _decompose_full(self, mat):
258258
# side='right' ensures that number of features selected
259259
# their variance is always greater than self.n_components_ float
260260
# passed. More discussion in issue: #15669
261-
ratio_cumsum = stable_cumsum(explained_variance_ratio_)
261+
ratio_cumsum = np.cumulative_sum(explained_variance_ratio_)
262262
self.n_components_ = (
263263
np.searchsorted(ratio_cumsum, self.n_components_, side="right") + 1
264264
)

src/skmatter/decomposition/_pcov.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from sklearn.linear_model._base import LinearModel
1515
from sklearn.utils import check_random_state
1616
from sklearn.utils._arpack import _init_arpack_v0
17-
from sklearn.utils.extmath import randomized_svd, stable_cumsum, svd_flip
17+
from sklearn.utils.extmath import randomized_svd, svd_flip
1818
from sklearn.utils.validation import check_is_fitted
1919

2020
from skmatter.utils import pcovr_covariance, pcovr_kernel
@@ -288,7 +288,7 @@ def _decompose_full(self, mat):
288288
# side='right' ensures that number of features selected
289289
# their variance is always greater than self.n_components_ float
290290
# passed. More discussion in issue: #15669
291-
ratio_cumsum = stable_cumsum(explained_variance_ratio_)
291+
ratio_cumsum = np.cumulative_sum(explained_variance_ratio_)
292292
self.n_components_ = (
293293
np.searchsorted(ratio_cumsum, self.n_components_, side="right") + 1
294294
)

src/skmatter/sample_selection/_voronoi_fps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ def _update_post_selection(self, X, y, last_selected):
312312

313313
updated_points = np.where(self.new_dist_ < self.hausdorff_)[0]
314314
np.minimum(
315-
self.hausdorff_, self.new_dist_, self.hausdorff_, casting="unsafe"
315+
self.hausdorff_, self.new_dist_, out=self.hausdorff_, casting="unsafe"
316316
)
317317
else:
318318
updated_points = np.array([])

tests/test_kernel_pcovc.py

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from sklearn.preprocessing import StandardScaler
1111
from sklearn.linear_model import LogisticRegression, RidgeClassifier
1212
from sklearn.metrics.pairwise import pairwise_kernels
13+
import pytest
1314

1415
from skmatter.decomposition import KernelPCovC
1516

@@ -333,10 +334,14 @@ def test_precomputed_classification(self):
333334
def test_scale_z_parameter(self):
334335
"""Check that changing scale_z changes the eigendecomposition."""
335336
kpcovc_scaled = self.model(scale_z=True)
336-
kpcovc_scaled.fit(self.X, self.Y)
337+
338+
with pytest.warns(m="class does not automatically center Z"):
339+
kpcovc_scaled.fit(self.X, self.Y)
337340

338341
kpcovc_unscaled = self.model(scale_z=False)
339-
kpcovc_unscaled.fit(self.X, self.Y)
342+
343+
with pytest.warns(m="class does not automatically center Z"):
344+
kpcovc_unscaled.fit(self.X, self.Y)
340345
assert not np.allclose(kpcovc_scaled.pkt_, kpcovc_unscaled.pkt_)
341346

342347
def test_z_scaling(self):
@@ -345,28 +350,10 @@ def test_z_scaling(self):
345350
if it is.
346351
"""
347352
kpcovc = self.model(n_components=2, scale_z=True)
348-
349-
with warnings.catch_warnings():
350-
kpcovc.fit(self.X, self.Y)
351-
warnings.simplefilter("error")
352-
self.assertEqual(1 + 1, 2)
353+
kpcovc.fit(self.X, self.Y)
353354

354355
kpcovc = self.model(n_components=2, scale_z=False, z_mean_tol=0, z_var_tol=0)
355-
356-
with warnings.catch_warnings(record=True) as w:
357-
kpcovc.fit(self.X, self.Y)
358-
self.assertEqual(
359-
str(w[0].message),
360-
"This class does not automatically center Z, and the column means "
361-
"of Z are greater than the supplied tolerance. We recommend scaling "
362-
"Z (and the weights) by setting `scale_z=True`.",
363-
)
364-
self.assertEqual(
365-
str(w[1].message),
366-
"This class does not automatically scale Z, and the column variances "
367-
"of Z are greater than the supplied tolerance. We recommend scaling "
368-
"Z (and the weights) by setting `scale_z=True`.",
369-
)
356+
kpcovc.fit(self.X, self.Y)
370357

371358

372359
class KernelTests(KernelPCovCBaseTest):
@@ -483,7 +470,9 @@ def test_bad_n_components(self):
483470
with self.subTest(type="0_ncomponents"):
484471
with self.assertRaises(ValueError) as cm:
485472
kpcovc = self.model(n_components=0, svd_solver="randomized")
486-
kpcovc.fit(self.X, self.Y)
473+
474+
with pytest.warns(match="class does not automatically center data"):
475+
kpcovc.fit(self.X, self.Y)
487476

488477
self.assertEqual(
489478
str(cm.exception),

tests/test_pcovc.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from email import message
12
import unittest
23
import warnings
34

@@ -10,6 +11,7 @@
1011
from sklearn.naive_bayes import GaussianNB
1112
from sklearn.preprocessing import StandardScaler
1213
from sklearn.utils.validation import check_X_y
14+
import pytest
1315

1416
from 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
)

tests/test_pcovr.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from email import message
12
import unittest
23
import warnings
34

@@ -9,6 +10,7 @@
910
from sklearn.linear_model import Ridge
1011
from sklearn.preprocessing import StandardScaler
1112
from sklearn.utils.validation import check_X_y
13+
import pytest
1214

1315
from skmatter.decomposition import PCovR
1416

@@ -167,9 +169,11 @@ def test_select_sample_space(self):
167169
pcovr = self.model(n_components=2, tol=1e-12)
168170

169171
n_samples = self.X.shape[1] - 1
170-
pcovr.fit(self.X[:n_samples], self.Y[:n_samples])
171172

172-
self.assertTrue(pcovr.space_ == "sample")
173+
with pytest.warns(match="class does not automatically center data"):
174+
pcovr.fit(self.X[:n_samples], self.Y[:n_samples])
175+
176+
assert pcovr.space_ == "sample"
173177

174178
def test_bad_space(self):
175179
"""
@@ -280,13 +284,11 @@ def test_good_n_components(self):
280284

281285
def test_bad_n_components(self):
282286
"""Check that PCovR will not work with any prohibited values of n_components."""
283-
with self.assertRaises(ValueError) as cm:
284-
pcovr = self.model(n_components="mle", svd_solver="full")
285-
pcovr.fit(self.X[:2], self.Y[:2])
286-
self.assertEqual(
287-
str(cm.exception),
288-
"n_components='mle' is only supported if n_samples >= n_features",
289-
)
287+
pcovr = self.model(n_components="mle", svd_solver="full")
288+
m = "n_components='mle' is only supported if n_samples >= n_features"
289+
with pytest.raises(ValueError, match=m):
290+
with pytest.warns(match="class does not automatically center data"):
291+
pcovr.fit(self.X[:2], self.Y[:2])
290292

291293
with self.subTest(type="negative_ncomponents"):
292294
with self.assertRaises(ValueError) as cm:
@@ -376,13 +378,12 @@ def test_centering(self):
376378
"""
377379
pcovr = self.model(n_components=2, tol=1e-12)
378380
X = self.X.copy() + np.random.uniform(-1, 1, self.X.shape[1])
379-
with warnings.catch_warnings(record=True) as w:
381+
m = (
382+
"This class does not automatically center data, and your data mean is "
383+
"greater than the supplied tolerance."
384+
)
385+
with pytest.warns(match=m):
380386
pcovr.fit(X, self.Y)
381-
self.assertEqual(
382-
str(w[0].message),
383-
"This class does not automatically center data, and your data mean is "
384-
"greater than the supplied tolerance.",
385-
)
386387

387388
def test_T_shape(self):
388389
"""Check that PCovR returns a latent space projection consistent with the shape

0 commit comments

Comments
 (0)