dynamicslab / pysindy

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

[BUG] Issue with the fit function #583

Open GregColetti19 opened 3 days ago

GregColetti19 commented 3 days ago

I get an error in dimension whenever I try to fit the model to the data. The library is update to the last version, so I don't know where the conflict comes from. I'm using VS code on a MacOS Monterey 12.7.4 I used a basic code to highlight the issue

code example

import numpy as np
import pysindy as ps

# Generate data
t = np.linspace(0, 10, 100)
x = np.column_stack([np.sin(t), np.cos(t)])

print("x shape:", x.shape)
print("t shape:", t.shape)

# Instantiate and fit a SINDy model
model = ps.SINDy()
model.fit(x, t=t[1] - t[0])
model.print()

Error message:

ValueError                                Traceback (most recent call last)
Cell In[10], [line 10](vscode-notebook-cell:?execution_count=10&line=10)
      [8](vscode-notebook-cell:?execution_count=10&line=8) # Instantiate and fit a SINDy model
      [9](vscode-notebook-cell:?execution_count=10&line=9) model = ps.SINDy()
---> [10](vscode-notebook-cell:?execution_count=10&line=10) model.fit(x, t=t[1] - t[0])
     [11](vscode-notebook-cell:?execution_count=10&line=11) model.print()

File /opt/anaconda3/envs/tesi/lib/python3.9/site-packages/pysindy/pysindy.py:353, in SINDy.fit(self, x, t, x_dot, u)
    [347](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/tesi/lib/python3.9/site-packages/pysindy/pysindy.py:347)     u = validate_control_variables(
    [348](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/tesi/lib/python3.9/site-packages/pysindy/pysindy.py:348)         x,
    [349](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/tesi/lib/python3.9/site-packages/pysindy/pysindy.py:349)         u,
    [350](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/tesi/lib/python3.9/site-packages/pysindy/pysindy.py:350)         trim_last_point=(self.discrete_time and x_dot is None),
    [351](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/tesi/lib/python3.9/site-packages/pysindy/pysindy.py:351)     )
    [352](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/tesi/lib/python3.9/site-packages/pysindy/pysindy.py:352)     self.n_control_features_ = u[0].shape[u[0].ax_coord]
--> [353](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/tesi/lib/python3.9/site-packages/pysindy/pysindy.py:353) x, x_dot = self._process_trajectories(x, t, x_dot)
    [355](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/tesi/lib/python3.9/site-packages/pysindy/pysindy.py:355) # Append control variables
    [356](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/tesi/lib/python3.9/site-packages/pysindy/pysindy.py:356) if u is not None:

File /opt/anaconda3/envs/tesi/lib/python3.9/site-packages/pysindy/pysindy.py:561, in SINDy._process_trajectories(self, x, t, x_dot)
    [558](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/tesi/lib/python3.9/site-packages/pysindy/pysindy.py:558)         x = [xi[:-1] for xi in x]
    [559](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/tesi/lib/python3.9/site-packages/pysindy/pysindy.py:559)     else:
    [560](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/tesi/lib/python3.9/site-packages/pysindy/pysindy.py:560)         x, x_dot = zip(
--> [561](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/tesi/lib/python3.9/site-packages/pysindy/pysindy.py:561)             *[
    [562](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/tesi/lib/python3.9/site-packages/pysindy/pysindy.py:562)                 self.feature_library.calc_trajectory(
...
    [409](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/tesi/lib/python3.9/site-packages/numpy/linalg/_linalg.py:409)               over='ignore', divide='ignore', under='ignore'):
--> [410](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/tesi/lib/python3.9/site-packages/numpy/linalg/_linalg.py:410)     r = gufunc(a, b, signature=signature)
    [412](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/tesi/lib/python3.9/site-packages/numpy/linalg/_linalg.py:412) return wrap(r.astype(result_t, copy=False))

ValueError: solve: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (m,m),(m,n)->(m,n) (size 2 is different from 3)

PySINDy/Python version information:

1.7.6.dev507+g2ca37cb 3.9.20 (main, Oct 3 2024, 02:27:54) [Clang 14.0.6 ]