Skip to content

Commit

Permalink
fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PicoCentauri committed Feb 13, 2025
1 parent 7f80d78 commit 75e03da
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 66 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ build/
dist/
docs/src/examples
sg_execution_times.rst
.coverage*
3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ include = [
[tool.coverage.xml]
output = 'tests/coverage.xml'

[tool.pytest.ini_options]
testpaths = "tests"

[tool.isort]
skip = "__init__.py"
profile = "black"
Expand Down
22 changes: 9 additions & 13 deletions src/skmatter/_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,17 +561,15 @@ def score(self, X, y=None):
Parameters
----------
X : numpy.ndarray of shape [n_samples, n_features]
The input samples.
X : ignored
y : ignored
Returns
-------
score : numpy.ndarray of (n_to_select_from_)
:math:`\pi` importance for the given samples or features
"""
X, y = validate_data(self, X, y, reset=False)

validate_data(self, X, y, reset=False) # present for API consistency
return self.pi_

def _init_greedy_search(self, X, y, n_to_select):
Expand Down Expand Up @@ -746,8 +744,7 @@ def score(self, X, y=None):
score : numpy.ndarray of (n_to_select_from_)
:math:`\pi` importance for the given samples or features
"""
X, y = validate_data(self, X, y, reset=False)

validate_data(self, X, y, reset=False) # present for API consistency
return self.pi_

def _init_greedy_search(self, X, y, n_to_select):
Expand Down Expand Up @@ -941,8 +938,7 @@ def score(self, X, y=None):
-------
hausdorff : Hausdorff distances
"""
X, y = validate_data(self, X, y, reset=False)

validate_data(self, X, y, reset=False)
return self.hausdorff_

def get_distance(self):
Expand Down Expand Up @@ -1079,15 +1075,16 @@ def __init__(
)

def fit(self, X, y=None, warm_start=False):

if self.mixing == 1.0:
raise ValueError(
"Mixing = 1.0 corresponds to traditional FPS."
"Please use the FPS class."
"Mixing = 1.0 corresponds to traditional FPS. Please use the FPS class."
)

return super().fit(X, y)

# docstring is inherited and set from the base class
fit.__doc__ = GreedySelector.fit.__doc__

def score(self, X, y=None):
"""Returns the Hausdorff distances of all samples to previous selections.
Expand All @@ -1104,8 +1101,7 @@ def score(self, X, y=None):
-------
hausdorff : Hausdorff distances
"""
X, y = validate_data(self, X, y, reset=False)

validate_data(self, X, y, reset=False)
return self.hausdorff_

def get_distance(self):
Expand Down
109 changes: 69 additions & 40 deletions src/skmatter/utils/_pcovr_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,30 @@


def check_lr_fit(regressor, X, y):
r"""
"""
Checks that a (linear) regressor is fitted, and if not,
fits it with the provided data
:param regressor: sklearn-style regressor
:type regressor: object
:param X: feature matrix with which to fit the regressor
if it is not already fitted
:type X: array
:param y: target values with which to fit the regressor
if it is not already fitted
:type y: array
fits it with the provided data.
Parameters
----------
regressor : object
sklearn-style regressor
X : array-like
Feature matrix with which to fit the regressor if it is not already fitted
y : array-like
Target values with which to fit the regressor if it is not already fitted
Returns
-------
fitted_regressor : object
The fitted regressor. If input regressor was already fitted and compatible with
the data, returns a deep copy. Otherwise returns a newly fitted regressor.
Raises
------
ValueError
If the fitted regressor's coefficients dimensions are incompatible with the
target space.
"""
try:
check_is_fitted(regressor)
Expand All @@ -32,18 +44,18 @@ def check_lr_fit(regressor, X, y):
# Check compatibility with y
if fitted_regressor.coef_.ndim != y.ndim:
raise ValueError(
"The regressor coefficients have a dimension incompatible "
"with the supplied target space. "
"The coefficients have dimension %d and the targets "
"have dimension %d" % (fitted_regressor.coef_.ndim, y.ndim)
"The regressor coefficients have a dimension incompatible with the "
"supplied target space. The coefficients have dimension "
f"{fitted_regressor.coef_.ndim} and the targets have dimension "
f"{y.ndim}"
)
elif y.ndim == 2:
if fitted_regressor.coef_.shape[0] != y.shape[1]:
raise ValueError(
"The regressor coefficients have a shape incompatible "
"with the supplied target space. "
"The coefficients have shape %r and the targets "
"have shape %r" % (fitted_regressor.coef_.shape, y.shape)
"The regressor coefficients have a shape incompatible with the "
"supplied target space. The coefficients have shape "
f"{fitted_regressor.coef_.shape} and the targets have shape "
f"{y.shape}"
)

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


def check_krr_fit(regressor, K, X, y):
r"""
"""
Checks that a (kernel ridge) regressor is fitted, and if not,
fits it with the provided data
:param regressor: sklearn-style regressor
:type regressor: object
:param K: kernel matrix with which to fit the regressor
if it is not already fitted
:type K: array
:param X: feature matrix with which to check the regressor
:type X: array
:param y: target values with which to fit the regressor
if it is not already fitted
:type y: array
fits it with the provided data.
Parameters
----------
regressor : object
sklearn-style regressor
K : array-like
Kernel matrix with which to fit the regressor if it is not already fitted
X : array-like
Feature matrix with which to check the regressor
y : array-like
Target values with which to fit the regressor if it is not already fitted
Returns
-------
fitted_regressor : object
The fitted regressor. If input regressor was already fitted and compatible with
the data, returns a deep copy. Otherwise returns a newly fitted regressor.
Raises
------
ValueError
If the fitted regressor's coefficients dimensions are incompatible with the
target space.
Notes
-----
For unfitted regressors, sets the kernel to "precomputed" before fitting with the
provided kernel matrix K to avoid recomputation.
"""
try:
check_is_fitted(regressor)
Expand All @@ -79,18 +108,18 @@ def check_krr_fit(regressor, K, X, y):
# Check compatibility with y
if fitted_regressor.dual_coef_.ndim != y.ndim:
raise ValueError(
"The regressor coefficients have a dimension incompatible "
"with the supplied target space. "
"The coefficients have dimension %d and the targets "
"have dimension %d" % (fitted_regressor.dual_coef_.ndim, y.ndim)
"The regressor coefficients have a dimension incompatible with the "
"supplied target space. The coefficients have dimension "
f"{fitted_regressor.dual_coef_.ndim} and the targets have dimension "
f"{y.ndim}"
)
elif y.ndim == 2:
if fitted_regressor.dual_coef_.shape[1] != y.shape[1]:
raise ValueError(
"The regressor coefficients have a shape incompatible "
"with the supplied target space. "
"The coefficients have shape %r and the targets "
"have shape %r" % (fitted_regressor.dual_coef_.shape, y.shape)
"The regressor coefficients have a shape incompatible with the "
"supplied target space. The coefficients have shape "
f"{fitted_regressor.dual_coef_.shape} and the targets have shape "
f"{y.shape}"
)

except NotFittedError:
Expand Down
Binary file not shown.
5 changes: 3 additions & 2 deletions tests/test_feature_pcov_fps.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ def test_restart(self):

def test_no_mixing_1(self):
"""Check that the model throws an error when mixing = 1.0."""
selector = PCovFPS(n_to_select=1, mixing=1.0)
with self.assertRaises(ValueError) as cm:
_ = PCovFPS(n_to_select=1, mixing=1.0)
selector.fit(self.X, y=self.y)
self.assertEqual(
str(cm.exception),
"Mixing = 1.0 corresponds to traditional FPS." "Please use the FPS class.",
"Mixing = 1.0 corresponds to traditional FPS. Please use the FPS class.",
)


Expand Down
2 changes: 1 addition & 1 deletion tests/test_kernel_pcovr.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def test_incompatible_coef_shape(self):

# Dimension mismatch
with self.assertRaises(ValueError) as cm:
kpcovr.fit(self.X, self.Y[:, 0])
kpcovr.fit(self.X, np.zeros(self.Y.shape[0] + (2,)))
self.assertTrue(
str(cm.exception),
"The regressor coefficients have a dimension incompatible "
Expand Down
9 changes: 4 additions & 5 deletions tests/test_pcovr.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,13 +491,12 @@ def test_incompatible_coef_shape(self):

# Dimension mismatch
with self.assertRaises(ValueError) as cm:
pcovr.fit(self.X, self.Y.squeeze())
pcovr.fit(self.X, np.zeros((self.Y.shape[0], 2)))
self.assertEqual(
str(cm.exception),
"The regressor coefficients have a dimension incompatible "
"with the supplied target space. "
"The coefficients have dimension %d and the targets "
"have dimension %d" % (regressor.coef_.ndim, self.Y.squeeze().ndim),
"The regressor coefficients have a dimension incompatible with the "
"supplied target space. The coefficients have dimension 1 and the targets "
"have dimension 2",
)

# Shape mismatch (number of targets)
Expand Down
5 changes: 3 additions & 2 deletions tests/test_sample_pcov_fps.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ def test_restart(self):

def test_no_mixing_1(self):
"""Check that the model throws an error when mixing = 1.0."""
selector = PCovFPS(n_to_select=1, mixing=1.0)
with self.assertRaises(ValueError) as cm:
_ = PCovFPS(n_to_select=1, mixing=1.0)
selector.fit(self.X, y=self.y)
self.assertEqual(
str(cm.exception),
"Mixing = 1.0 corresponds to traditional FPS." "Please use the FPS class.",
"Mixing = 1.0 corresponds to traditional FPS. Please use the FPS class.",
)


Expand Down

0 comments on commit 75e03da

Please sign in to comment.