dynamicslab / pysindy

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

[BUG] UserWarnings cannot be suppressed #364

Closed B0Gec closed 1 year ago

B0Gec commented 1 year ago

when ignoring warnings with

import warnings
warnings.filterwarnings('ignore')

nothing happens, warnings are still displayed. This can clutter the output logs when running large scale experiments.

Reproducing code example using default tutorial notebook:

import numpy as np
import warnings
warnings.filterwarnings("ignore")
import pysindy as ps
import warnings
warnings.filterwarnings("ignore")

t = np.linspace(0, 1, 100)
x = 3 * np.exp(-2 * t)
y = 0.5 * np.exp(t)
X = np.stack((x, y), axis=-1)  # First column is x, second is y

threshold = 10.1
model = ps.SINDy(feature_names=["x", "y"],
                optimizer = ps.STLSQ(threshold=threshold))

model.fit(X, t=t)

model.print()

Error message:

It still prints out warning:

/home/pysindy/Documents/py-envs/venv/lib/python3.9/site-packages/pysindy/optimizers/stlsq.py:191: UserWarning: Sparsity parameter is too big (10.01) and eliminated all coefficients warnings.warn(

PySINDy/Python version information:

1.7.5 3.9.2 (default, Feb 28 2021, 17:03:44)

Jacob-Stevens-Haas commented 1 year ago

Could not recreate on master or with 1.7.5

before you run model.fit(), what is the value of warnings.filters?

B0Gec commented 1 year ago
[
    ('ignore', None, <class 'Warning'>, None, 0),
    ('ignore', None, <class 'UserWarning'>, None, 0),
    ('always', None, <class 'scipy.special._sf_error.SpecialFunctionWarning'>, None, 0),
    ('ignore', re.compile('the matrix subclass is not the recommended way', re.IGNORECASE), <class 'Warning'>, None, 0),
    ('ignore', re.compile('numpy.ndarray size changed', re.IGNORECASE), <class 'Warning'>, None, 0),
    ('ignore', re.compile('numpy.ufunc size changed', re.IGNORECASE), <class 'Warning'>, None, 0),
    ('ignore', re.compile('numpy.dtype size changed', re.IGNORECASE), <class 'Warning'>, None, 0),
    ('always', None, <class 'numpy.RankWarning'>, None, 0), ('default', None, <class 'DeprecationWarning'>, '__main__', 0),
    ('ignore', None, <class 'DeprecationWarning'>, None, 0), ('ignore', None, <class 'PendingDeprecationWarning'>, None, 0),
    ('ignore', None, <class 'ImportWarning'>, None, 0), ('ignore', None, <class 'ResourceWarning'>, None, 0),
    ('ignore', None, <class 'pkg_resources.PEP440Warning'>, None, 0)
]
Jacob-Stevens-Haas commented 1 year ago

Huh. Are you using Ipython? If so, try running the code in the REPL and let me know if you still see the warning.

B0Gec commented 1 year ago

I am running this in linux terminal, i.e. [GCC 10.2.1 20210110].

Tried running on replit, still warning: replit

What exactly do you mean by running in REPL?

Jacob-Stevens-Haas commented 1 year ago

REPL = Read, Eval, Print Loop, aka running it in the terminal.

I see where the issue is. This is fixed in #347, which will be in the next release.