Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
michaelkalz opened this issue Oct 11, 2024 · 8 comments

Comments

@michaelkalz
Copy link

michaelkalz commented Oct 11, 2024

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_control_features_ = 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
Copy link

humatic commented Oct 13, 2024

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
Copy link

humatic commented Oct 13, 2024

Related: #551

@ChidiOkoene
Copy link

I had the same issue as @humatic, but after fixing the numpy bug, the major problem was the self differentiation function and that of the optimizer and I don't know how to go about it.

@djpasseyjr
Copy link

I'm having the same error.

You can see it in the logs for my unit tests of my interfere package

For what it's worth, this stack exchange makes it sound like numpy.math is actually numpy's import of the builtin package math. (But it is from six years ago)

@djpasseyjr
Copy link

Also, most of my unit tests for SINDY passed. Only the tests that use a mix of differentiation methods failed.

@achalmahajan1
Copy link

Facing the same issue. Downgrading numpy to "1.26.4" as mentioned here does the trick #551.

@jesusmunozjmj
Copy link

Here's what worked for me:

import math
import numpy as np
np.math = math

@minimental
Copy link

When using customized libraries (pysindy.CustomLibrary), I also ran into a scikit-learn version mismatch issue. The following installation resolved the above given issue and the scikit-learn related one:

conda create -n pysindy conda-forge:pysindy numpy=1.26.4 scikit-learn=1.5.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants