def test_identity_matrix_representation(data_random): matrix = data_random basis = Identity() basis.fit(matrix) np.testing.assert_allclose(matrix.T, basis.matrix_representation())
def test_prefit_basis(data_random): data = data_random basis = Identity() basis.fit(data) # This data should be ignored during the fit data_to_ignore = nan * data_random model = SensorSelector(basis=basis) model.fit(data_to_ignore, prefit_basis=True) assert not any(isnan(model.get_selected_sensors()))
def test_update_n_basis_modes_refit(data_random): data = data_random n_basis_modes = 5 model = SensorSelector(basis=Identity(n_basis_modes=n_basis_modes)) model.fit(data) assert model.basis_matrix_.shape[1] == n_basis_modes model.update_n_basis_modes(n_basis_modes + 1, data) assert model.basis_matrix_.shape[1] == n_basis_modes + 1
def test_prefit_basis(data_binary_classification): x, y, _ = data_binary_classification basis = Identity().fit(x) model_prefit = SSPOC(basis=basis) model_prefit.fit(x, y, prefit_basis=True, quiet=True) model_standard = SSPOC().fit(x, y, quiet=True) np.testing.assert_allclose(model_prefit.sensor_coef_, model_standard.sensor_coef_)
def test_update_n_basis_modes_errors(data_random): data = data_random n_basis_modes = 5 model = SensorSelector(basis=Identity(n_basis_modes=n_basis_modes)) model.fit(data) with pytest.raises(ValueError): model.update_n_basis_modes(0) with pytest.raises(ValueError): model.update_n_basis_modes("5") with pytest.raises(ValueError): model.update_n_basis_modes(data.shape[0] + 1) # Need to pass x when increasing n_basis_modes beyond capacity # of the original basis with pytest.raises(ValueError): model.update_n_basis_modes(n_basis_modes + 1)
"""Unit tests for basis classes""" import numpy as np import pytest from sklearn.exceptions import NotFittedError from pysensors.basis import Identity from pysensors.basis import RandomProjection from pysensors.basis import SVD @pytest.mark.parametrize("basis", [Identity(), SVD(), RandomProjection()]) def test_not_fitted(basis): with pytest.raises(NotFittedError): basis.matrix_representation() def test_identity_matrix_representation(data_random): matrix = data_random basis = Identity() basis.fit(matrix) np.testing.assert_allclose(matrix.T, basis.matrix_representation()) @pytest.mark.parametrize("basis", [SVD, RandomProjection]) def test_matrix_representation(basis, data_random): data = data_random n_features = data.shape[1] n_components = 5
pytest.lazy_fixture("data_vandermonde"), pytest.lazy_fixture("data_random") ], ) def test_get_all_sensors(data): x = data max_sensors = x.shape[1] model = SensorSelector() model.fit(x) assert len(model.get_all_sensors()) == max_sensors @pytest.mark.parametrize( "basis", [Identity(), SVD(), RandomProjection(n_basis_modes=5)]) def test_basis_compatibility(data_vandermonde, basis): x = data_vandermonde model = SensorSelector(basis=basis) model.fit(x) check_is_fitted(model) def test_n_sensors(data_random): # Check for bad inputs with pytest.raises(ValueError): model = SensorSelector(n_sensors=0) with pytest.raises(ValueError): model = SensorSelector(n_sensors=5.4) with pytest.raises(ValueError):