Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
4 changes: 2 additions & 2 deletions src/skmatter/_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/skmatter/decomposition/_kpcov.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions src/skmatter/decomposition/_pcov.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
Expand Down
2 changes: 1 addition & 1 deletion src/skmatter/sample_selection/_voronoi_fps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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([])
Expand Down
8 changes: 3 additions & 5 deletions tests/test_kernel_pcovc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand All @@ -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)

Expand Down
32 changes: 16 additions & 16 deletions tests/test_pcovc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -397,25 +399,20 @@ 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):
"""
Check that PCovC raises a warning if Z is not of scale, and does not
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)

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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_
)
Expand Down
31 changes: 15 additions & 16 deletions tests/test_pcovr.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import unittest
import warnings

import numpy as np
from sklearn import exceptions
Expand All @@ -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

Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down