def test_bad_optimizers(data_derivative_1d): x, x_dot = data_derivative_1d x = x.reshape(-1, 1) with pytest.raises(AttributeError): opt = SINDyOptimizer(DummyEmptyModel()) with pytest.raises(AttributeError): opt = SINDyOptimizer(DummyModelNoCoef()) opt.fit(x, x_dot)
def test_fit(data, optimizer): x, x_dot = data if len(x.shape) == 1: x = x.reshape(-1, 1) opt = SINDyOptimizer(optimizer, unbias=False) opt.fit(x, x_dot) check_is_fitted(opt) assert opt.complexity >= 0 if len(x_dot.shape) > 1: assert opt.coef_.shape == (x.shape[1], x_dot.shape[1]) else: assert opt.coef_.shape == (1, x.shape[1])
def test_unbias_external(data_derivative_1d): x, x_dot = data_derivative_1d x = x.reshape(-1, 1) optimizer_biased = SINDyOptimizer(Lasso(alpha=0.1, fit_intercept=False, max_iter=1), unbias=False) optimizer_biased.fit(x, x_dot) optimizer_unbiased = SINDyOptimizer(Lasso(alpha=0.1, fit_intercept=False, max_iter=1), unbias=True) optimizer_unbiased.fit(x, x_dot) assert (norm(optimizer_biased.coef_ - optimizer_unbiased.coef_) / (norm(optimizer_unbiased.coef_) + 1e-5) > 1e-9)
def test_unbias(data_derivative_1d): x, x_dot = data_derivative_1d x = x.reshape(-1, 1) optimizer_biased = SINDyOptimizer(STLSQ(threshold=0.01, alpha=0.1, max_iter=1), unbias=False) optimizer_biased.fit(x, x_dot) optimizer_unbiased = SINDyOptimizer(STLSQ(threshold=0.01, alpha=0.1, max_iter=1), unbias=True) optimizer_unbiased.fit(x, x_dot) assert (norm(optimizer_biased.coef_ - optimizer_unbiased.coef_) / norm(optimizer_unbiased.coef_) > 1e-9)
def test_sr3_trimming(optimizer, data_linear_oscillator_corrupted): X, X_dot, trimming_array = data_linear_oscillator_corrupted optimizer_without_trimming = SINDyOptimizer(optimizer(), unbias=False) optimizer_without_trimming.fit(X, X_dot) optimizer_trimming = SINDyOptimizer(optimizer(trimming_fraction=0.15), unbias=False) optimizer_trimming.fit(X, X_dot) # Check that trimming found the right samples to remove np.testing.assert_array_equal(optimizer_trimming.optimizer.trimming_array, trimming_array) # Check that the coefficients found by the optimizer with trimming # are closer to the true coefficients than the coefficients found by the # optimizer without trimming true_coef = np.array([[-2.0, 0.0], [0.0, 1.0]]) assert norm(true_coef - optimizer_trimming.coef_) < norm( true_coef - optimizer_without_trimming.coef_)
def test_sr3_trimming(data_linear_oscillator_corrupted): X, X_dot, trimming_array = data_linear_oscillator_corrupted optimizer_without_trimming = SINDyOptimizer(SR3(), unbias=False) optimizer_without_trimming.fit(X, X_dot) optimizer_trimming = SINDyOptimizer(SR3(trimming_fraction=0.15), unbias=False) optimizer_trimming.fit(X, X_dot) # Check that trimming found the right samples to remove assert (np.sum( np.abs(optimizer_trimming.optimizer.trimming_array - trimming_array)) == 0.0) # Check that the coefficients found by the optimizer with trimming are closer to # the true coefficients than the coefficients found by the optimizer without # trimming true_coef = np.array([[-2.0, 0.0], [0.0, 1.0]]) assert norm(true_coef - optimizer_trimming.coef_) < norm( true_coef - optimizer_without_trimming.coef_)