From 86b0efb7363f4fbe6e0a133b95c9b41b91d1b98e Mon Sep 17 00:00:00 2001 From: billtubbs Date: Sat, 25 Apr 2020 17:51:54 -0700 Subject: [PATCH 1/5] Fixed SINDy __init__ defaults --- pysindy/pysindy.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/pysindy/pysindy.py b/pysindy/pysindy.py index 4e317781f..894ba4c9e 100644 --- a/pysindy/pysindy.py +++ b/pysindy/pysindy.py @@ -91,18 +91,21 @@ class SINDy(BaseEstimator): 0.999999985520653 """ - def __init__( - self, - optimizer=STLSQ(), - feature_library=PolynomialFeatures(), - differentiation_method=FiniteDifference(), - feature_names=None, - discrete_time=False, - n_jobs=1, - ): - self.optimizer = optimizer - self.feature_library = feature_library - self.differentiation_method = differentiation_method + def __init__(self, optimizer=None, feature_library=None, + differentiation_method=None, feature_names=None, + discrete_time=False, n_jobs=1): + if optimizer is None: + self.optimizer = STLSQ() + else: + self.optimizer = optimizer + if feature_library is None: + self.feature_library = PolynomialFeatures() + else: + self.feature_library = feature_library + if differentiation_method is None: + self.differentiation_method = FiniteDifference() + else: + self.differentiation_method = differentiation_method self.feature_names = feature_names self.discrete_time = discrete_time self.n_jobs = n_jobs From 0a2fd46ca33f1e48286ecb82740f4a398467c0a2 Mon Sep 17 00:00:00 2001 From: billtubbs Date: Sat, 25 Apr 2020 18:00:56 -0700 Subject: [PATCH 2/5] Warning messages formatting --- pysindy/optimizers/stlsq.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/pysindy/optimizers/stlsq.py b/pysindy/optimizers/stlsq.py index fffdb381a..e2865c446 100644 --- a/pysindy/optimizers/stlsq.py +++ b/pysindy/optimizers/stlsq.py @@ -9,8 +9,7 @@ class STLSQ(BaseOptimizer): - """ - Sequentially thresholded least squares algorithm. + """Sequentially thresholded least squares algorithm. Attempts to minimize the objective function :math:`\\|y - Xw\\|^2_2 + alpha \\times \\|w\\|^2_2` @@ -128,8 +127,7 @@ def _no_change(self): return all(bool(i) == bool(j) for i, j in zip(this_coef, last_coef)) def _reduce(self, x, y): - """ - Iterates the thresholding. Assumes an initial guess is saved in + """Iterates the thresholding. Assumes an initial guess is saved in self.coef_ and self.ind_ """ ind = self.ind_ @@ -140,8 +138,8 @@ def _reduce(self, x, y): for _ in range(self.max_iter): if np.count_nonzero(ind) == 0: warnings.warn( - """Sparsity parameter is too big ({}) and eliminated all - coefficients""".format( + "Sparsity parameter is too big ({}) and eliminated all " + "coefficients".format( self.threshold ) ) @@ -152,8 +150,8 @@ def _reduce(self, x, y): for i in range(n_targets): if np.count_nonzero(ind[i]) == 0: warnings.warn( - """Sparsity parameter is too big ({}) and eliminated all - coefficients""".format( + "Sparsity parameter is too big ({}) and eliminated all " + "coefficients".format( self.threshold ) ) From 6b99173c55ba263e677e14c58f77b07fe0053199 Mon Sep 17 00:00:00 2001 From: Brian Date: Sun, 26 Apr 2020 13:09:29 -0700 Subject: [PATCH 3/5] Apply suggestions from code review Superficial rearranging of code to appease black formatter --- pysindy/optimizers/stlsq.py | 4 +--- pysindy/pysindy.py | 12 +++++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/pysindy/optimizers/stlsq.py b/pysindy/optimizers/stlsq.py index e2865c446..b7a5d5562 100644 --- a/pysindy/optimizers/stlsq.py +++ b/pysindy/optimizers/stlsq.py @@ -139,9 +139,7 @@ def _reduce(self, x, y): if np.count_nonzero(ind) == 0: warnings.warn( "Sparsity parameter is too big ({}) and eliminated all " - "coefficients".format( - self.threshold - ) + "coefficients".format(self.threshold) ) coef = np.zeros((n_targets, n_features)) break diff --git a/pysindy/pysindy.py b/pysindy/pysindy.py index 894ba4c9e..83a6b8fc1 100644 --- a/pysindy/pysindy.py +++ b/pysindy/pysindy.py @@ -91,9 +91,15 @@ class SINDy(BaseEstimator): 0.999999985520653 """ - def __init__(self, optimizer=None, feature_library=None, - differentiation_method=None, feature_names=None, - discrete_time=False, n_jobs=1): + def __init__( + self, + optimizer=None, + feature_library=None, + differentiation_method=None, + feature_names=None, + discrete_time=False, + n_jobs=1, + ): if optimizer is None: self.optimizer = STLSQ() else: From 6d1546c046b07e64d312974325f3cd5e98a06cd0 Mon Sep 17 00:00:00 2001 From: Brian Date: Sun, 26 Apr 2020 13:13:08 -0700 Subject: [PATCH 4/5] Update handling of default parameters --- pysindy/pysindy.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/pysindy/pysindy.py b/pysindy/pysindy.py index 83a6b8fc1..27aca208c 100644 --- a/pysindy/pysindy.py +++ b/pysindy/pysindy.py @@ -101,17 +101,14 @@ def __init__( n_jobs=1, ): if optimizer is None: - self.optimizer = STLSQ() - else: - self.optimizer = optimizer + optimizer = STLSQ() + self.optimizer = optimizer if feature_library is None: - self.feature_library = PolynomialFeatures() - else: - self.feature_library = feature_library + feature_library = PolynomialFeatures() + self.feature_library = feature_library if differentiation_method is None: - self.differentiation_method = FiniteDifference() - else: - self.differentiation_method = differentiation_method + differentiation_method = FiniteDifference() + self.differentiation_method = differentiation_method self.feature_names = feature_names self.discrete_time = discrete_time self.n_jobs = n_jobs From f9ac15b5d073c71bb210b77ff6a5579beb8ed94b Mon Sep 17 00:00:00 2001 From: Brian Date: Sun, 26 Apr 2020 13:20:00 -0700 Subject: [PATCH 5/5] Update pysindy/optimizers/stlsq.py Fix formatting for black --- pysindy/optimizers/stlsq.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pysindy/optimizers/stlsq.py b/pysindy/optimizers/stlsq.py index b7a5d5562..ac3ff79d7 100644 --- a/pysindy/optimizers/stlsq.py +++ b/pysindy/optimizers/stlsq.py @@ -149,9 +149,7 @@ def _reduce(self, x, y): if np.count_nonzero(ind[i]) == 0: warnings.warn( "Sparsity parameter is too big ({}) and eliminated all " - "coefficients".format( - self.threshold - ) + "coefficients".format(self.threshold) ) continue coef_i = self._regress(x[:, ind[i]], y[:, i])