dynamicslab / pysindy

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

[BUG] AttributeError: module 'numpy' has no attribute 'math' in 1_feature_overview [5] # Instantiate and fit the SINDy model #569

Open michaelkalz opened 2 weeks ago

michaelkalz commented 2 weeks ago

I get "AttributeError: module 'numpy' has no attribute 'math'" when I run cell [5] in the binder notebook

Reproducing code example:

model = ps.SINDy()
model.fit(x_train, t=dt)
model.print()

Error message:


AttributeError Traceback (most recent call last) Cell In[5], line 3 1 # Instantiate and fit the SINDy model 2 model = ps.SINDy() ----> 3 model.fit(x_train, t=dt) 4 model.print()

File /srv/conda/envs/notebook/lib/python3.10/site-packages/pysindy/pysindy.py:343, in SINDy.fit(self, x, t, x_dot, u, multiple_trajectories, unbias, quiet, ensemble, library_ensemble, replace, n_candidates_to_drop, n_subset, n_models, ensemble_aggregator) 337 u = validate_control_variables( 338 x, 339 u, 340 trim_last_point=(self.discrete_time and x_dot is None), 341 ) 342 self.n_controlfeatures = u[0].shape[u[0].ax_coord] --> 343 x, x_dot = self._process_multiple_trajectories(x, t, x_dot) 345 # Set ensemble variables 346 self.ensemble = ensemble

File /srv/conda/envs/notebook/lib/python3.10/site-packages/pysindy/pysindy.py:665, in SINDy._process_multiple_trajectories(self, x, t, x_dot) 663 x = [xi[:-1] for xi in x] 664 else: --> 665 x_dot = [ 666 self.feature_library.calc_trajectory( 667 self.differentiation_method, xi, ti 668 ) 669 for xi, ti in _zip_like_sequence(x, t) 670 ] 671 return x, x_dot

File /srv/conda/envs/notebook/lib/python3.10/site-packages/pysindy/pysindy.py:666, in (.0) 663 x = [xi[:-1] for xi in x] 664 else: 665 x_dot = [ --> 666 self.feature_library.calc_trajectory( 667 self.differentiation_method, xi, ti 668 ) 669 for xi, ti in _zip_like_sequence(x, t) 670 ] 671 return x, x_dot

File /srv/conda/envs/notebook/lib/python3.10/site-packages/pysindy/feature_library/base.py:87, in BaseFeatureLibrary.calc_trajectory(self, diff_method, x, t) 85 def calc_trajectory(self, diff_method, x, t): 86 axes = x.dict ---> 87 x_dot = diff_method(x, t=t) 88 return AxesArray(x_dot, axes)

File /srv/conda/envs/notebook/lib/python3.10/site-packages/pysindy/differentiation/base.py:49, in BaseDifferentiation.call(self, x, t) 48 def call(self, x, t=1): ---> 49 return self._differentiate(x, t)

File /srv/conda/envs/notebook/lib/python3.10/site-packages/pysindy/differentiation/finite_difference.py:237, in FiniteDifference._differentiate(self, x, t) 234 if not np.isscalar(t): 235 dt = t[1] - t[0] --> 237 coeffs = self._constant_coefficients(dt) 238 dims = np.array(x.shape) 239 dims[self.axis] = x.shape[self.axis] - (self.n_stencil - 1)

File /srv/conda/envs/notebook/lib/python3.10/site-packages/pysindy/differentiation/finite_difference.py:199, in FiniteDifference._constant_coefficients(self, dt) 195 matrices = (dt * (np.arange(self.n_stencil) - (self.n_stencil - 1) // 2))[ 196 np.newaxis, : 197 ] ** pows 198 b = np.zeros(self.n_stencil) --> 199 b[self.d] = np.math.factorial(self.d) 200 return np.linalg.solve(matrices, b)

File /srv/conda/envs/notebook/lib/python3.10/site-packages/numpy/init.py:414, in getattr(attr) 411 import numpy.char as char 412 return char.chararray --> 414 raise AttributeError("module {!r} has no attribute " 415 "{!r}".format(name, attr))

AttributeError: module 'numpy' has no attribute 'math'

PySINDy/Python version information:

Binder Notebook from here.

humatic commented 2 weeks ago

I have the same issue with the notebook for the original paper.

I'm on Python 3.10, Numpy 2.0, Pysindy 1.7.5

Monkey patching may fix the immediate problem:

# Monkeypatch
from scipy.special import factorial
np.math = type('math', (), {})()
np.math.factorial = factorial

However for me the notebook still failed to execute due to subsequent errors.

BTW, I've noticed that latest dev doesn't seem to have this np.math.factorial code, so maybe a fix is underway..?

humatic commented 2 weeks ago

Related: #551