Skip to content

Commit

Permalink
Merge pull request #334 from dynamicslab/simpler-tests
Browse files Browse the repository at this point in the history
Test improvements today
  • Loading branch information
Jacob-Stevens-Haas committed May 26, 2023
2 parents 7be9a4f + c9286d8 commit b2d83f8
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 66 deletions.
2 changes: 2 additions & 0 deletions examples/17_parameterized_pattern_formation/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pdf
data
10 changes: 5 additions & 5 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def data_1d_bad_shape():
@pytest.fixture
def data_lorenz():

t = np.linspace(0, 5, 500)
t = np.linspace(0, 1, 50)
x0 = [8, 27, -7]
x = solve_ivp(lorenz, (t[0], t[-1]), x0, t_eval=t).y.T

Expand Down Expand Up @@ -351,7 +351,7 @@ def data_generalized_library():


@pytest.fixture
def data_sindypi_library():
def data_sindypi_library(data_lorenz):
library_functions = [
lambda x: x,
lambda x: x**2,
Expand All @@ -362,7 +362,7 @@ def data_sindypi_library():
lambda s: str(s) + "^2",
lambda s, t: str(s) + " " + str(t),
]
t = np.linspace(0, 5, 500)
_, t = data_lorenz

return PDELibrary(
library_functions=library_functions,
Expand Down Expand Up @@ -393,8 +393,8 @@ def data_ode_library():


@pytest.fixture
def data_pde_library():
spatial_grid = np.linspace(0, 10, 500)
def data_pde_library(data_lorenz):
_, spatial_grid = data_lorenz
library_functions = [
lambda x: x,
lambda x: x**2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ def test_centered_difference_variable_timestep_length():
assert len(centered_difference(x, t) == len(x))


# Fixtures: data sets to be re-used in multiple tests
# data_derivative_1d and data_derivative_2d are defined
# in ../conftest.py
def test_nan_derivatives(data_lorenz):
x, t = data_lorenz
x_dot = FiniteDifference(drop_endpoints=False)(x, t)
x_dot_nans = FiniteDifference(drop_endpoints=True)(x, t)
np.testing.assert_allclose(x_dot_nans[1:-1], x_dot[1:-1])
assert np.isnan(x_dot_nans[:1]).all() and np.isnan(x_dot_nans[-1:]).all()


def test_forward_difference_1d(data_derivative_1d):
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions test/test_notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def purge_notebook_modules():
@pytest.mark.parametrize("directory", notebook_scripts)
def test_notebook_script(directory: Path, purge_notebook_modules):
# Run in native directory with modified sys.path for imports to work
if "17_" in directory or "5_" in directory:
pytest.skip("Notebook runs too slowly. Test manually")
with _cwd(notebook_dir / directory):
runpy.run_path(str(notebook_dir / directory / "example.py"), run_name="testing")

Expand Down
42 changes: 8 additions & 34 deletions test/optimizers/test_optimizers.py → test/test_optimizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1028,25 +1028,12 @@ def test_normalize_columns(data_derivative_1d, optimizer):
assert opt.coef_.shape == (1, x.shape[1])


@pytest.mark.parametrize(
"optimizer",
[
STLSQ,
SSR,
FROLS,
SR3,
ConstrainedSR3,
StableLinearSR3,
TrappingSR3,
MIOSR,
],
)
def test_legacy_ensemble_odes(data_lorenz, optimizer):
def test_legacy_ensemble_odes(data_lorenz):
x, t = data_lorenz
opt = optimizer(normalize_columns=True)
opt = STLSQ(normalize_columns=True)
model = SINDy(optimizer=opt)
model.fit(x, ensemble=True, n_models=10, n_subset=20)
assert np.shape(model.coef_list) == (10, 3, 10)
model.fit(x, ensemble=True, n_models=2, n_subset=2)
assert np.shape(model.coef_list) == (2, 3, 10)


@pytest.mark.parametrize(
Expand All @@ -1066,20 +1053,7 @@ def test_ensemble_optimizer(data_lorenz, optimizer_params):
assert model.coefficients().shape == (3, 10)


@pytest.mark.parametrize(
"optimizer",
[
STLSQ,
SSR,
FROLS,
SR3,
ConstrainedSR3,
StableLinearSR3,
TrappingSR3,
MIOSR,
],
)
def test_legacy_ensemble_pdes(optimizer):
def test_legacy_ensemble_pdes():
u = np.random.randn(10, 10, 2)
t = np.linspace(1, 10, 10)
x = np.linspace(1, 10, 10)
Expand All @@ -1097,11 +1071,11 @@ def test_legacy_ensemble_pdes(optimizer):
spatial_grid=x,
include_bias=True,
)
opt = optimizer(normalize_columns=True)
opt = STLSQ(normalize_columns=True)
model = SINDy(optimizer=opt, feature_library=pde_lib)
model.fit(u, x_dot=u_dot, ensemble=True, n_models=10, n_subset=20)
model.fit(u, x_dot=u_dot, ensemble=True, n_models=2, n_subset=2)
n_features = len(model.get_feature_names())
assert np.shape(model.coef_list) == (10, 2, n_features)
assert np.shape(model.coef_list) == (2, 2, n_features)


def test_ssr_criteria(data_lorenz):
Expand Down
File renamed without changes.
27 changes: 3 additions & 24 deletions test/test_pysindy.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from sklearn.utils.validation import check_is_fitted

from pysindy import SINDy
from pysindy.differentiation import FiniteDifference
from pysindy.differentiation import SINDyDerivative
from pysindy.differentiation import SmoothedFiniteDifference
from pysindy.feature_library import FourierLibrary
Expand Down Expand Up @@ -81,20 +80,6 @@ def test_improper_shape_input(data_1d):
check_is_fitted(model)


def test_nan_derivatives(data_lorenz):
x, t = data_lorenz
model0 = SINDy(differentiation_method=FiniteDifference(drop_endpoints=False))
model0.fit(x, t)

model = SINDy(differentiation_method=FiniteDifference(drop_endpoints=True))
model.fit(x, t)
check_is_fitted(model)

result = model.score(x, t=t)
expected = model0.score(x, t=t)
np.testing.assert_allclose(result, expected, rtol=1e-4)


@pytest.mark.parametrize(
"data",
[
Expand Down Expand Up @@ -561,19 +546,13 @@ def test_differentiate(data_lorenz, data_multiple_trajctories):
model.differentiate(x)


def test_coefficients(data_lorenz):
def test_coefficients_equals_complexity(data_lorenz):
x, t = data_lorenz
model = SINDy()
model.fit(x, t)
c = model.coefficients()
assert np.count_nonzero(c) < 10


def test_complexity(data_lorenz):
x, t = data_lorenz
model = SINDy()
model.fit(x, t)
assert model.complexity < 10
assert model.complexity == np.count_nonzero(c)
assert model.complexity < 30


def test_simulate_errors(data_lorenz):
Expand Down
File renamed without changes.

0 comments on commit b2d83f8

Please sign in to comment.