def __init__( self, optimizer=None, feature_library=None, differentiation_method=None, feature_names=None, t_default=1, discrete_time=False, n_jobs=1, ): if optimizer is None: optimizer = STLSQ() self.optimizer = optimizer if feature_library is None: feature_library = PolynomialLibrary() self.feature_library = feature_library if differentiation_method is None: differentiation_method = FiniteDifference() self.differentiation_method = differentiation_method if not isinstance(t_default, float) and not isinstance(t_default, int): raise ValueError("t_default must be a positive number") elif t_default <= 0: raise ValueError("t_default must be a positive number") else: self.t_default = t_default self.feature_names = feature_names self.discrete_time = discrete_time self.n_jobs = n_jobs
def test_bad_parameters(): with pytest.raises(ValueError): PolynomialLibrary(degree=-1) with pytest.raises(ValueError): PolynomialLibrary(degree=1.5) with pytest.raises(ValueError): PolynomialLibrary(include_interaction=False, interaction_only=True) with pytest.raises(ValueError): FourierLibrary(n_frequencies=-1) with pytest.raises(ValueError): FourierLibrary(n_frequencies=-1) with pytest.raises(ValueError): FourierLibrary(n_frequencies=2.2) with pytest.raises(ValueError): FourierLibrary(include_sin=False, include_cos=False) with pytest.raises(ValueError): library_functions = [lambda x: x, lambda x: x**2, lambda x: 0 * x] function_names = [lambda s: str(s), lambda s: "{}^2".format(s)] CustomLibrary(library_functions=library_functions, function_names=function_names)
def test_linear_constraints(data_lorenz): x, t = data_lorenz library = PolynomialLibrary().fit(x) constraint_rhs = np.ones(2) constraint_lhs = np.zeros((2, x.shape[1] * library.n_output_features_)) target_1, target_2 = 1, 3 constraint_lhs[0, 3] = target_1 constraint_lhs[1, library.n_output_features_] = target_2 optimizer = ConstrainedSR3(constraint_lhs=constraint_lhs, constraint_rhs=constraint_rhs) model = SINDy(feature_library=library, optimizer=optimizer).fit(x, t) coeffs = model.coefficients() np.testing.assert_allclose(np.array([coeffs[0, 3], coeffs[1, 0]]), np.array([1 / target_1, 1 / target_2]))
pytest.lazy_fixture("data_1d_bad_shape"), ], ) def test_simulate(data): x, t = data model = SINDy() model.fit(x, t) x1 = model.simulate(x[0], t) assert len(x1) == len(t) @pytest.mark.parametrize( "library", [ PolynomialLibrary(degree=3), FourierLibrary(n_frequencies=3), pytest.lazy_fixture("data_custom_library"), ], ) def test_libraries(data_lorenz, library): x, t = data_lorenz model = SINDy(feature_library=library) model.fit(x, t) s = model.score(x, t) assert s <= 1 @pytest.mark.parametrize( "data",
pytest.lazy_fixture("data_1d_bad_shape"), ], ) def test_simulate(data): x, t = data model = SINDy() model.fit(x, t) x1 = model.simulate(x[0], t) assert len(x1) == len(t) @pytest.mark.parametrize( "library", [ PolynomialLibrary(degree=3), FourierLibrary(n_frequencies=3), pytest.lazy_fixture("data_custom_library"), PolynomialLibrary() + FourierLibrary(), ], ) def test_libraries(data_lorenz, library): x, t = data_lorenz model = SINDy(feature_library=library) model.fit(x, t) s = model.score(x, t) assert s <= 1 def test_integration_smoothed_finite_difference(data_lorenz):
def test_concat(): ident_lib = IdentityLibrary() poly_lib = PolynomialLibrary() concat_lib = ident_lib + poly_lib assert isinstance(concat_lib, ConcatLibrary)
def test_polynomial_options(data_lorenz, kwargs, sparse_format): x, t = data_lorenz library = PolynomialLibrary(**kwargs) library.fit_transform(sparse_format(x)) check_is_fitted(library)
def test_polynomial_sparse_inputs(data_lorenz, sparse_format): x, t = data_lorenz library = PolynomialLibrary() library.fit_transform(sparse_format(x)) check_is_fitted(library)
with pytest.raises(ValueError): FourierLibrary(n_frequencies=2.2) with pytest.raises(ValueError): FourierLibrary(include_sin=False, include_cos=False) with pytest.raises(ValueError): library_functions = [lambda x: x, lambda x: x**2, lambda x: 0 * x] function_names = [lambda s: str(s), lambda s: "{}^2".format(s)] CustomLibrary(library_functions=library_functions, function_names=function_names) @pytest.mark.parametrize( "library", [ IdentityLibrary(), PolynomialLibrary(), FourierLibrary(), IdentityLibrary() + PolynomialLibrary(), pytest.lazy_fixture("data_custom_library"), ], ) def test_fit_transform(data_lorenz, library): x, t = data_lorenz library.fit_transform(x) check_is_fitted(library) @pytest.mark.parametrize( "library", [ IdentityLibrary(),
with pytest.raises(ValueError): FourierLibrary(n_frequencies=2.2) with pytest.raises(ValueError): FourierLibrary(include_sin=False, include_cos=False) with pytest.raises(ValueError): library_functions = [lambda x: x, lambda x: x**2, lambda x: 0 * x] function_names = [lambda s: str(s), lambda s: "{}^2".format(s)] CustomLibrary(library_functions=library_functions, function_names=function_names) @pytest.mark.parametrize( "library", [ IdentityLibrary(), PolynomialLibrary(), FourierLibrary(), pytest.lazy_fixture("data_custom_library"), ], ) def test_fit_transform(data_lorenz, library): x, t = data_lorenz library.fit_transform(x) check_is_fitted(library) @pytest.mark.parametrize( "library", [ IdentityLibrary(), PolynomialLibrary(),