Skip to content

Commit

Permalink
TST: Remove multiple_trajectories kwarg from all tests
Browse files Browse the repository at this point in the history
Also fix name of fixture trajctory -> trajectory
  • Loading branch information
Jacob-Stevens-Haas committed Jul 11, 2023
1 parent 9b5da85 commit 95e178a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 125 deletions.
2 changes: 1 addition & 1 deletion test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def data_lorenz():


@pytest.fixture
def data_multiple_trajctories():
def data_multiple_trajectories():

n_points = [100, 200, 500]
initial_conditions = [
Expand Down
4 changes: 2 additions & 2 deletions test/test_feature_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ def test_parameterized_library(diffuse_multiple_trajectories):
model = SINDy(
feature_library=pde_lib, optimizer=optimizer, feature_names=["u", "c"]
)
model.fit(xs, u=us, multiple_trajectories=True, t=t)
model.fit(xs, u=us, t=t)
assert abs(model.coefficients()[0][4] - 1) < 1e-2
assert np.all(model.coefficients()[0][:4] == 0)
assert np.all(model.coefficients()[0][5:] == 0)
Expand All @@ -657,7 +657,7 @@ def test_parameterized_library(diffuse_multiple_trajectories):
model = SINDy(
feature_library=weak_lib, optimizer=optimizer, feature_names=["u", "c"]
)
model.fit(xs, u=us, multiple_trajectories=True, t=t)
model.fit(xs, u=us, t=t)
assert abs(model.coefficients()[0][4] - 1) < 1e-2
assert np.all(model.coefficients()[0][:4] == 0)
assert np.all(model.coefficients()[0][5:] == 0)
Expand Down
97 changes: 36 additions & 61 deletions test/test_pysindy.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,66 +330,54 @@ def test_score_pde(data_1d_random_pde):
assert model.score(u, t) <= 1


def test_fit_multiple_trajectores(data_multiple_trajctories):
x, t = data_multiple_trajctories
def test_fit_multiple_trajectores(data_multiple_trajectories):
x, t = data_multiple_trajectories
model = SINDy()

# Should fail if multiple_trajectories flag is not set
with pytest.raises(ValueError):
model.fit(x, t=t)

model.fit(x, multiple_trajectories=True)
model.fit(x)
check_is_fitted(model)

model.fit(x, t=t, multiple_trajectories=True)
assert model.score(x, t=t, multiple_trajectories=True) > 0.8
model.fit(x, t=t)
assert model.score(x, t=t) > 0.8

model = SINDy()
model.fit(x, x_dot=x, multiple_trajectories=True)
model.fit(x, x_dot=x)
check_is_fitted(model)

model = SINDy()
model.fit(x, t=t, x_dot=x, multiple_trajectories=True)
model.fit(x, t=t, x_dot=x)
check_is_fitted(model)

# Test validate_input
t[0] = None
with pytest.raises(ValueError):
model.fit(x, t=t, multiple_trajectories=True)
model.fit(x, t=t)


def test_predict_multiple_trajectories(data_multiple_trajctories):
x, t = data_multiple_trajctories
def test_predict_multiple_trajectories(data_multiple_trajectories):
x, t = data_multiple_trajectories
model = SINDy()
model.fit(x, t=t, multiple_trajectories=True)

# Should fail if multiple_trajectories flag is not set
with pytest.raises(ValueError):
model.predict(x)
model.fit(x, t=t)

p = model.predict(x, multiple_trajectories=True)
p = model.predict(x)
assert len(p) == len(x)


def test_score_multiple_trajectories(data_multiple_trajctories):
x, t = data_multiple_trajctories
def test_score_multiple_trajectories(data_multiple_trajectories):
x, t = data_multiple_trajectories
model = SINDy()
model.fit(x, t=t, multiple_trajectories=True)

# Should fail if multiple_trajectories flag is not set
with pytest.raises(ValueError):
model.score(x)
model.fit(x, t=t)

s = model.score(x, multiple_trajectories=True)
s = model.score(x)
assert s <= 1

s = model.score(x, t=t, multiple_trajectories=True)
s = model.score(x, t=t)
assert s <= 1

s = model.score(x, x_dot=x, multiple_trajectories=True)
s = model.score(x, x_dot=x)
assert s <= 1

s = model.score(x, t=t, x_dot=x, multiple_trajectories=True)
s = model.score(x, t=t, x_dot=x)
assert s <= 1


Expand Down Expand Up @@ -449,17 +437,12 @@ def test_fit_discrete_time_multiple_trajectories(
data_discrete_time_multiple_trajectories,
):
x = data_discrete_time_multiple_trajectories

# Should fail if multiple_trajectories flag is not set
model = SINDy(discrete_time=True)
with pytest.raises(ValueError):
model.fit(x)

model.fit(x, multiple_trajectories=True)
model.fit(x)
check_is_fitted(model)

model = SINDy(discrete_time=True)
model.fit(x, x_dot=x, multiple_trajectories=True)
model.fit(x, x_dot=x)
check_is_fitted(model)


Expand All @@ -468,13 +451,9 @@ def test_predict_discrete_time_multiple_trajectories(
):
x = data_discrete_time_multiple_trajectories
model = SINDy(discrete_time=True)
model.fit(x, multiple_trajectories=True)

# Should fail if multiple_trajectories flag is not set
with pytest.raises(ValueError):
model.predict(x)
model.fit(x)

y = model.predict(x, multiple_trajectories=True)
y = model.predict(x)
assert len(y) == len(x)


Expand All @@ -483,17 +462,13 @@ def test_score_discrete_time_multiple_trajectories(
):
x = data_discrete_time_multiple_trajectories
model = SINDy(discrete_time=True)
model.fit(x, multiple_trajectories=True)

# Should fail if multiple_trajectories flag is not set
with pytest.raises(ValueError):
model.score(x)
model.fit(x)

s = model.score(x, multiple_trajectories=True)
s = model.score(x)
assert s > 0.75

# x is not its own derivative, so we expect bad performance here
s = model.score(x, x_dot=x, multiple_trajectories=True)
s = model.score(x, x_dot=x)
assert s < 1


Expand Down Expand Up @@ -534,22 +509,22 @@ def test_print_discrete_time_multiple_trajectories(
):
x = data_discrete_time_multiple_trajectories
model = SINDy(discrete_time=True)
model.fit(x, multiple_trajectories=True)
model.fit(x)

model.print()

out, _ = capsys.readouterr()
assert len(out) > 1


def test_differentiate(data_lorenz, data_multiple_trajctories):
def test_differentiate(data_lorenz, data_multiple_trajectories):
x, t = data_lorenz

model = SINDy()
model.differentiate(x, t)

x, t = data_multiple_trajctories
model.differentiate(x, t, multiple_trajectories=True)
x, t = data_multiple_trajectories
model.differentiate(x, t)

model = SINDy(discrete_time=True)
with pytest.raises(RuntimeError):
Expand Down Expand Up @@ -686,34 +661,34 @@ def test_multiple_trajectories_and_ensemble(diffuse_multiple_trajectories):

optimizer = STLSQ(threshold=0.1, alpha=1e-5, normalize_columns=False)
model = SINDy(feature_library=pde_lib, optimizer=optimizer, feature_names=["u"])
model.fit(u, multiple_trajectories=True, t=t)
model.fit(u, t=t)
print(model.coefficients(), model.coefficients()[0][-1])
assert abs(model.coefficients()[0][-1] - 1) < 1e-2
assert np.all(model.coefficients()[0][:-1] == 0)

model = SINDy(feature_library=weak_lib, optimizer=optimizer, feature_names=["u"])
model.fit(u, multiple_trajectories=True, t=t)
model.fit(u, t=t)
assert abs(model.coefficients()[0][-1] - 1) < 1e-2
assert np.all(model.coefficients()[0][:-1] == 0)

optimizer = EnsembleOptimizer(opt=optimizer, bagging=True, n_subset=len(t))

model = SINDy(feature_library=pde_lib, optimizer=optimizer, feature_names=["u"])
model.fit(u, multiple_trajectories=True, t=t)
model.fit(u, t=t)
assert abs(model.coefficients()[0][-1] - 1) < 1e-2
assert np.all(model.coefficients()[0][:-1] == 0)

model = SINDy(feature_library=weak_lib, optimizer=optimizer, feature_names=["u"])
model.fit(u, multiple_trajectories=True, t=t)
model.fit(u, t=t)
assert abs(model.coefficients()[0][-1] - 1) < 1e-2
assert np.all(model.coefficients()[0][:-1] == 0)

model = SINDy(feature_library=pde_lib, optimizer=optimizer, feature_names=["u"])
model.fit(u, multiple_trajectories=True, t=t)
model.fit(u, t=t)
assert abs(model.coefficients()[0][-1] - 1) < 1e-2
assert np.all(model.coefficients()[0][:-1] == 0)

model = SINDy(feature_library=weak_lib, optimizer=optimizer, feature_names=["u"])
model.fit(u, multiple_trajectories=True, t=t)
model.fit(u, t=t)
assert abs(model.coefficients()[0][-1] - 1) < 1e-2
assert np.all(model.coefficients()[0][:-1] == 0)
86 changes: 25 additions & 61 deletions test/test_sindyc.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,78 +235,55 @@ def test_score(data):
assert model.score(x, u=u, t=t, x_dot=x) <= 1


def test_fit_multiple_trajectores(data_multiple_trajctories):
x, t = data_multiple_trajctories
def test_fit_multiple_trajectores(data_multiple_trajectories):
x, t = data_multiple_trajectories
u = [np.ones((xi.shape[0], 2)) for xi in x]

model = SINDy()

# Should fail if multiple_trajectories flag is not set
with pytest.raises(ValueError):
model.fit(x, u=u, t=t)

# Should fail if x or u is not a list
with pytest.raises(TypeError):
model.fit(x, u=u[0], multiple_trajectories=True)

with pytest.raises(TypeError):
model.fit(x[0], u=u, multiple_trajectories=True)

# x and u should be lists of the same length
with pytest.raises(ValueError):
model.fit(x[:-1], u=u, multiple_trajectories=True)

model.fit(x, u=u, multiple_trajectories=True)
model.fit(x, u=u)
check_is_fitted(model)

model.fit(x, u=u, t=t, multiple_trajectories=True)
assert model.score(x, u=u, t=t, multiple_trajectories=True) > 0.8
model.fit(x, u=u, t=t)
assert model.score(x, u=u, t=t) > 0.8

model = SINDy()
model.fit(x, u=u, x_dot=x, multiple_trajectories=True)
model.fit(x, u=u, x_dot=x)
check_is_fitted(model)

model = SINDy()
model.fit(x, u=u, t=t, x_dot=x, multiple_trajectories=True)
model.fit(x, u=u, t=t, x_dot=x)
check_is_fitted(model)


def test_predict_multiple_trajectories(data_multiple_trajctories):
x, t = data_multiple_trajctories
def test_predict_multiple_trajectories(data_multiple_trajectories):
x, t = data_multiple_trajectories
u = [np.ones((xi.shape[0], 2)) for xi in x]

model = SINDy()
model.fit(x, u=u, t=t, multiple_trajectories=True)

# Should fail if multiple_trajectories flag is not set
with pytest.raises(ValueError):
model.predict(x, u=u)
model.fit(x, u=u, t=t)

p = model.predict(x, u=u, multiple_trajectories=True)
p = model.predict(x, u=u)
assert len(p) == len(x)


def test_score_multiple_trajectories(data_multiple_trajctories):
x, t = data_multiple_trajctories
def test_score_multiple_trajectories(data_multiple_trajectories):
x, t = data_multiple_trajectories
u = [np.ones((xi.shape[0], 2)) for xi in x]

model = SINDy()
model.fit(x, u=u, t=t, multiple_trajectories=True)

# Should fail if multiple_trajectories flag is not set
with pytest.raises(ValueError):
model.score(x, u=u)
model.fit(x, u=u, t=t)

s = model.score(x, u=u, multiple_trajectories=True)
s = model.score(x, u=u)
assert s <= 1

s = model.score(x, u=u, t=t, multiple_trajectories=True)
s = model.score(x, u=u, t=t)
assert s <= 1

s = model.score(x, u=u, x_dot=x, multiple_trajectories=True)
s = model.score(x, u=u, x_dot=x)
assert s <= 1

s = model.score(x, u=u, t=t, x_dot=x, multiple_trajectories=True)
s = model.score(x, u=u, t=t, x_dot=x)
assert s <= 1


Expand Down Expand Up @@ -382,17 +359,12 @@ def test_fit_discrete_time_multiple_trajectories(
data_discrete_time_multiple_trajectories_c,
):
x, u = data_discrete_time_multiple_trajectories_c

# Should fail if multiple_trajectories flag is not set
model = SINDy(discrete_time=True)
with pytest.raises(ValueError):
model.fit(x, u=u)

model.fit(x, u=u, multiple_trajectories=True)
model.fit(x, u=u)
check_is_fitted(model)

model = SINDy(discrete_time=True)
model.fit(x, u=u, x_dot=x, multiple_trajectories=True)
model.fit(x, u=u, x_dot=x)
check_is_fitted(model)


Expand All @@ -401,13 +373,9 @@ def test_predict_discrete_time_multiple_trajectories(
):
x, u = data_discrete_time_multiple_trajectories_c
model = SINDy(discrete_time=True)
model.fit(x, u=u, multiple_trajectories=True)

# Should fail if multiple_trajectories flag is not set
with pytest.raises(ValueError):
model.predict(x, u=u)
model.fit(x, u=u)

y = model.predict(x, u=u, multiple_trajectories=True)
y = model.predict(x, u=u)
assert len(y) == len(x)


Expand All @@ -416,17 +384,13 @@ def test_score_discrete_time_multiple_trajectories(
):
x, u = data_discrete_time_multiple_trajectories_c
model = SINDy(discrete_time=True)
model.fit(x, u=u, multiple_trajectories=True)

# Should fail if multiple_trajectories flag is not set
with pytest.raises(ValueError):
model.score(x, u=u)
model.fit(x, u=u)

s = model.score(x, u=u, multiple_trajectories=True)
s = model.score(x, u=u)
assert s > 0.75

# x is not its own derivative, so we expect bad performance here
s = model.score(x, u=u, x_dot=x, multiple_trajectories=True)
s = model.score(x, u=u, x_dot=x)
assert s < 1


Expand Down

0 comments on commit 95e178a

Please sign in to comment.