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] Issue with the fit function #583

Open
GregColetti19 opened this issue Nov 20, 2024 · 2 comments
Open

[BUG] Issue with the fit function #583

GregColetti19 opened this issue Nov 20, 2024 · 2 comments

Comments

@GregColetti19
Copy link

GregColetti19 commented Nov 20, 2024

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 ]

@GregColetti19 GregColetti19 changed the title [BUG] [BUG] Issue with the fit function Nov 20, 2024
@twhsu-stanley
Copy link

I am having the same issue

@ggonzm
Copy link

ggonzm commented Dec 3, 2024

Same here.

I temporarily solved it passing x_dot to the fitting process, but I know that it's not the desired solution:

model.fit(x, u, x_dot = np.gradient(x, axis=0))

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

3 participants