dynamicslab / pysindy

A package for the sparse identification of nonlinear dynamical systems from data
https://pysindy.readthedocs.io/en/latest/
Other
1.36k stars 304 forks source link

Error in '3_original_Paper' Example: Missing 'quiet' Argument in pysindy.py #457

Closed munozdjp closed 5 months ago

munozdjp commented 5 months ago

Description:

While attempting to reproduce the examples in "3_original_Paper," I encountered an error on line 203. The issue appears to be related to the quiet argument in the fit method of pysindy.py.

Steps to Reproduce:

Navigate to the "3_original_Paper" example. Execute the code until line 203. Expected Behavior: The code should run without errors, fitting the SINDy models with varying noise levels.

Actual Behavior:

An error occurs, indicating that the quiet argument is not recognized in the fit method of pysindy.py.

Error Message:

TypeError: SINDy.fit() got an unexpected keyword argument 'quiet'

Reproducing code example:


#%%
import numpy as np
from scipy.integrate import solve_ivp

from pysindy.utils import lorenz

import pysindy as ps

# ignore user warnings
import warnings
warnings.filterwarnings("ignore", category=UserWarning)

np.random.seed(1000)  # Seed for reproducibility

# Integrator keywords for solve_ivp
integrator_keywords = {}
integrator_keywords['rtol'] = 1e-12
integrator_keywords['method'] = 'LSODA'
integrator_keywords['atol'] = 1e-12
# Generate training data

dt = 0.001
t_train = np.arange(0, 100, dt)
t_train_span = (t_train[0], t_train[-1])
x0_train = [-8, 8, 27]
x_train = solve_ivp(lorenz, t_train_span,
                    x0_train, t_eval=t_train, **integrator_keywords).y.T
x_dot_train_measured = np.array(
    [lorenz(0, x_train[i]) for i in range(t_train.size)]
)

# Fit the models and simulate

poly_order = 5
threshold = 0.05

noise_levels = [1e-4, 1e-3, 1e-2, 1e-1, 1.0]

models = []
t_sim = np.arange(0, 20, dt)
x_sim = []
for eps in noise_levels:
    model = ps.SINDy(
        optimizer=ps.STLSQ(threshold=threshold),
        feature_library=ps.PolynomialLibrary(degree=poly_order),
    )
    model.fit(
        x_train,
        t=dt,
        x_dot=x_dot_train_measured
        + np.random.normal(scale=eps, size=x_train.shape),
        quiet=True,
    )
    models.append(model)
    x_sim.append(model.simulate(x_train[0], t_sim))

Error message:

File "", line 46, in model.fit( TypeError: SINDy.fit() got an unexpected keyword argument 'quiet'

PySINDy/Python version information:

Output from 'import sys, pysindy; print(pysindy.version, sys.version)' 1.7.6.dev135+g638e4bb 3.12.1 | packaged by conda-forge | (main, Dec 23 2023, 08:05:03) [Clang 16.0.6 ]

Additional Context

Upon inspecting the pysindy.py file, I noticed that the fit method (line 178) does not include a quiet argument. Here is the relevant section of the code for reference:

def fit(
    self,
    x,
    t=None,
    x_dot=None,
    u=None,
):
Jacob-Stevens-Haas commented 5 months ago

Thanks. You are correct - quiet was removed. The stability guarantees of the examples/ isn't totally agreed upon and is something we're working towards. Some are truly meant as examples, others are completed research ran on an old version of pysindy, and thus may break on major version updates.

If you're interested in why quiet was removed, you can either search issues or for maximum style points, use git archaeology. Clone the repo and run git log --oneline -Squiet --pysindy/pysindy.py. This is called a git pickaxe and will give you a list of commits that modified the number of occurrences of the string "quiet". You can read the commit messages, and if you go to that commit on github (https://github.com/dynamicslab/pysindy/commit/), you can see which pull request it was a part of including any discussion or linked issues.