Skip to content

Commit 879057b

Browse files
committed
fix some tests
1 parent 7f80d78 commit 879057b

File tree

11 files changed

+97
-71
lines changed

11 files changed

+97
-71
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
*.coverage*
12
*.pyc
23
*.ipynb_checkpoints*
34
__pycache__

pyproject.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ include = [
7575
[tool.coverage.xml]
7676
output = 'tests/coverage.xml'
7777

78-
[tool.pytest.ini_options]
79-
testpaths = "tests"
80-
8178
[tool.isort]
8279
skip = "__init__.py"
8380
profile = "black"

src/skmatter/_selection.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -561,17 +561,15 @@ def score(self, X, y=None):
561561
562562
Parameters
563563
----------
564-
X : numpy.ndarray of shape [n_samples, n_features]
565-
The input samples.
564+
X : ignored
566565
y : ignored
567566
568567
Returns
569568
-------
570569
score : numpy.ndarray of (n_to_select_from_)
571570
:math:`\pi` importance for the given samples or features
572571
"""
573-
X, y = validate_data(self, X, y, reset=False)
574-
572+
validate_data(self, X, y, reset=False) # present for API consistency
575573
return self.pi_
576574

577575
def _init_greedy_search(self, X, y, n_to_select):
@@ -746,8 +744,7 @@ def score(self, X, y=None):
746744
score : numpy.ndarray of (n_to_select_from_)
747745
:math:`\pi` importance for the given samples or features
748746
"""
749-
X, y = validate_data(self, X, y, reset=False)
750-
747+
validate_data(self, X, y, reset=False) # present for API consistency
751748
return self.pi_
752749

753750
def _init_greedy_search(self, X, y, n_to_select):
@@ -941,8 +938,7 @@ def score(self, X, y=None):
941938
-------
942939
hausdorff : Hausdorff distances
943940
"""
944-
X, y = validate_data(self, X, y, reset=False)
945-
941+
validate_data(self, X, y, reset=False)
946942
return self.hausdorff_
947943

948944
def get_distance(self):
@@ -1079,15 +1075,16 @@ def __init__(
10791075
)
10801076

10811077
def fit(self, X, y=None, warm_start=False):
1082-
10831078
if self.mixing == 1.0:
10841079
raise ValueError(
1085-
"Mixing = 1.0 corresponds to traditional FPS."
1086-
"Please use the FPS class."
1080+
"Mixing = 1.0 corresponds to traditional FPS. Please use the FPS class."
10871081
)
10881082

10891083
return super().fit(X, y)
10901084

1085+
# docstring is inherited and set from the base class
1086+
fit.__doc__ = GreedySelector.fit.__doc__
1087+
10911088
def score(self, X, y=None):
10921089
"""Returns the Hausdorff distances of all samples to previous selections.
10931090
@@ -1104,8 +1101,7 @@ def score(self, X, y=None):
11041101
-------
11051102
hausdorff : Hausdorff distances
11061103
"""
1107-
X, y = validate_data(self, X, y, reset=False)
1108-
1104+
validate_data(self, X, y, reset=False)
11091105
return self.hausdorff_
11101106

11111107
def get_distance(self):

src/skmatter/decomposition/_kernel_pcovr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ def score(self, X, y):
474474
"""
475475
check_is_fitted(self, ["pkt_", "X_fit_"])
476476

477-
X, y = validate_data(self, X, y, reset=False)
477+
X = validate_data(self, X, reset=False)
478478

479479
K_NN = self._get_kernel(self.X_fit_, self.X_fit_)
480480
K_VN = self._get_kernel(X, self.X_fit_)

src/skmatter/utils/_pcovr_utils.py

Lines changed: 69 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,30 @@
99

1010

1111
def check_lr_fit(regressor, X, y):
12-
r"""
12+
"""
1313
Checks that a (linear) regressor is fitted, and if not,
14-
fits it with the provided data
15-
16-
:param regressor: sklearn-style regressor
17-
:type regressor: object
18-
:param X: feature matrix with which to fit the regressor
19-
if it is not already fitted
20-
:type X: array
21-
:param y: target values with which to fit the regressor
22-
if it is not already fitted
23-
:type y: array
14+
fits it with the provided data.
15+
16+
Parameters
17+
----------
18+
regressor : object
19+
sklearn-style regressor
20+
X : array-like
21+
Feature matrix with which to fit the regressor if it is not already fitted
22+
y : array-like
23+
Target values with which to fit the regressor if it is not already fitted
24+
25+
Returns
26+
-------
27+
fitted_regressor : object
28+
The fitted regressor. If input regressor was already fitted and compatible with
29+
the data, returns a deep copy. Otherwise returns a newly fitted regressor.
30+
31+
Raises
32+
------
33+
ValueError
34+
If the fitted regressor's coefficients dimensions are incompatible with the
35+
target space.
2436
"""
2537
try:
2638
check_is_fitted(regressor)
@@ -32,18 +44,18 @@ def check_lr_fit(regressor, X, y):
3244
# Check compatibility with y
3345
if fitted_regressor.coef_.ndim != y.ndim:
3446
raise ValueError(
35-
"The regressor coefficients have a dimension incompatible "
36-
"with the supplied target space. "
37-
"The coefficients have dimension %d and the targets "
38-
"have dimension %d" % (fitted_regressor.coef_.ndim, y.ndim)
47+
"The regressor coefficients have a dimension incompatible with the "
48+
"supplied target space. The coefficients have dimension "
49+
f"{fitted_regressor.coef_.ndim} and the targets have dimension "
50+
f"{y.ndim}"
3951
)
4052
elif y.ndim == 2:
4153
if fitted_regressor.coef_.shape[0] != y.shape[1]:
4254
raise ValueError(
43-
"The regressor coefficients have a shape incompatible "
44-
"with the supplied target space. "
45-
"The coefficients have shape %r and the targets "
46-
"have shape %r" % (fitted_regressor.coef_.shape, y.shape)
55+
"The regressor coefficients have a shape incompatible with the "
56+
"supplied target space. The coefficients have shape "
57+
f"{fitted_regressor.coef_.shape} and the targets have shape "
58+
f"{y.shape}"
4759
)
4860

4961
except NotFittedError:
@@ -54,20 +66,37 @@ def check_lr_fit(regressor, X, y):
5466

5567

5668
def check_krr_fit(regressor, K, X, y):
57-
r"""
69+
"""
5870
Checks that a (kernel ridge) regressor is fitted, and if not,
59-
fits it with the provided data
60-
61-
:param regressor: sklearn-style regressor
62-
:type regressor: object
63-
:param K: kernel matrix with which to fit the regressor
64-
if it is not already fitted
65-
:type K: array
66-
:param X: feature matrix with which to check the regressor
67-
:type X: array
68-
:param y: target values with which to fit the regressor
69-
if it is not already fitted
70-
:type y: array
71+
fits it with the provided data.
72+
73+
Parameters
74+
----------
75+
regressor : object
76+
sklearn-style regressor
77+
K : array-like
78+
Kernel matrix with which to fit the regressor if it is not already fitted
79+
X : array-like
80+
Feature matrix with which to check the regressor
81+
y : array-like
82+
Target values with which to fit the regressor if it is not already fitted
83+
84+
Returns
85+
-------
86+
fitted_regressor : object
87+
The fitted regressor. If input regressor was already fitted and compatible with
88+
the data, returns a deep copy. Otherwise returns a newly fitted regressor.
89+
90+
Raises
91+
------
92+
ValueError
93+
If the fitted regressor's coefficients dimensions are incompatible with the
94+
target space.
95+
96+
Notes
97+
-----
98+
For unfitted regressors, sets the kernel to "precomputed" before fitting with the
99+
provided kernel matrix K to avoid recomputation.
71100
"""
72101
try:
73102
check_is_fitted(regressor)
@@ -79,18 +108,18 @@ def check_krr_fit(regressor, K, X, y):
79108
# Check compatibility with y
80109
if fitted_regressor.dual_coef_.ndim != y.ndim:
81110
raise ValueError(
82-
"The regressor coefficients have a dimension incompatible "
83-
"with the supplied target space. "
84-
"The coefficients have dimension %d and the targets "
85-
"have dimension %d" % (fitted_regressor.dual_coef_.ndim, y.ndim)
111+
"The regressor coefficients have a dimension incompatible with the "
112+
"supplied target space. The coefficients have dimension "
113+
f"{fitted_regressor.dual_coef_.ndim} and the targets have dimension "
114+
f"{y.ndim}"
86115
)
87116
elif y.ndim == 2:
88117
if fitted_regressor.dual_coef_.shape[1] != y.shape[1]:
89118
raise ValueError(
90-
"The regressor coefficients have a shape incompatible "
91-
"with the supplied target space. "
92-
"The coefficients have shape %r and the targets "
93-
"have shape %r" % (fitted_regressor.dual_coef_.shape, y.shape)
119+
"The regressor coefficients have a shape incompatible with the "
120+
"supplied target space. The coefficients have shape "
121+
f"{fitted_regressor.dual_coef_.shape} and the targets have shape "
122+
f"{y.shape}"
94123
)
95124

96125
except NotFittedError:
-52 KB
Binary file not shown.

tests/test_feature_pcov_fps.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ def test_restart(self):
2424

2525
def test_no_mixing_1(self):
2626
"""Check that the model throws an error when mixing = 1.0."""
27+
selector = PCovFPS(n_to_select=1, mixing=1.0)
2728
with self.assertRaises(ValueError) as cm:
28-
_ = PCovFPS(n_to_select=1, mixing=1.0)
29+
selector.fit(self.X, y=self.y)
2930
self.assertEqual(
3031
str(cm.exception),
31-
"Mixing = 1.0 corresponds to traditional FPS." "Please use the FPS class.",
32+
"Mixing = 1.0 corresponds to traditional FPS. Please use the FPS class.",
3233
)
3334

3435

tests/test_greedy_selector.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_bad_transform(self):
7373
_ = selector.transform(self.X[:, :3])
7474
self.assertEqual(
7575
str(cm.exception),
76-
"X has a different shape than during fitting. Reshape your data.",
76+
"X has 3 features, but GreedyTester is expecting 10 features as input.",
7777
)
7878

7979
def test_no_nfeatures(self):
@@ -124,8 +124,8 @@ def test_size_input(self):
124124
selector_feature.fit(X)
125125
self.assertEqual(
126126
str(cm.exception),
127-
f"Found array with 1 feature(s) (shape={X.shape})"
128-
" while a minimum of 2 is required.",
127+
f"Found array with 1 feature(s) (shape={X.shape}) while a minimum of 2 is "
128+
"required by GreedyTester.",
129129
)
130130

131131
X = X.reshape(1, -1)
@@ -135,7 +135,7 @@ def test_size_input(self):
135135
self.assertEqual(
136136
str(cm.exception),
137137
f"Found array with 1 sample(s) (shape={X.shape}) while a minimum of 2 is "
138-
"required.",
138+
"required by GreedyTester.",
139139
)
140140

141141

tests/test_kernel_pcovr.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ def test_centerer(self):
182182
self.assertTrue(hasattr(kpcovr, "centerer_"))
183183
_ = kpcovr.predict(self.X)
184184
_ = kpcovr.transform(self.X)
185+
186+
print(self.Y.shape)
185187
_ = kpcovr.score(self.X, self.Y)
186188

187189
def test_prefit_regressor(self):
@@ -255,7 +257,7 @@ def test_incompatible_coef_shape(self):
255257

256258
# Dimension mismatch
257259
with self.assertRaises(ValueError) as cm:
258-
kpcovr.fit(self.X, self.Y[:, 0])
260+
kpcovr.fit(self.X, np.zeros(self.Y.shape + (2,)))
259261
self.assertTrue(
260262
str(cm.exception),
261263
"The regressor coefficients have a dimension incompatible "

tests/test_pcovr.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -491,13 +491,12 @@ def test_incompatible_coef_shape(self):
491491

492492
# Dimension mismatch
493493
with self.assertRaises(ValueError) as cm:
494-
pcovr.fit(self.X, self.Y.squeeze())
494+
pcovr.fit(self.X, np.zeros((self.Y.shape[0], 2)))
495495
self.assertEqual(
496496
str(cm.exception),
497-
"The regressor coefficients have a dimension incompatible "
498-
"with the supplied target space. "
499-
"The coefficients have dimension %d and the targets "
500-
"have dimension %d" % (regressor.coef_.ndim, self.Y.squeeze().ndim),
497+
"The regressor coefficients have a dimension incompatible with the "
498+
"supplied target space. The coefficients have dimension 1 and the targets "
499+
"have dimension 2",
501500
)
502501

503502
# Shape mismatch (number of targets)

0 commit comments

Comments
 (0)