dynamicslab / pysindy

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

[BUG] #419

Closed koop14 closed 8 months ago

koop14 commented 8 months ago

Issue with generalized library example here: https://github.com/dynamicslab/pysindy/blob/master/examples/1_feature_overview/example.ipynb

Reproducing code example:


poly_library = ps.PolynomialLibrary(include_bias=False)
fourier_library = ps.FourierLibrary()

# Initialize the default inputs, but
# don't use the x0 input for generating the Fourier library
inputs_per_library = [(0, 1, 2), (1, 2)]

# Tensor all the polynomial and Fourier library terms together
tensor_array = [[1, 1]]

# Initialize this generalized library, all the work hidden from the user!
generalized_library = ps.GeneralizedLibrary(
    [poly_library, fourier_library],
    tensor_array=tensor_array,
    exclude_libraries=[1],
    inputs_per_library=inputs_per_library,
)

# Fit the model and print the library feature names to check success
model = ps.SINDy(feature_library=generalized_library, feature_names=feature_names)
model.fit(x_train, t=dt)
model.print()
print("Feature names:\n", model.get_feature_names())```

### Error message:

```Traceback (most recent call last):

  Cell In[5915], line 12
    generalized_library = ps.GeneralizedLibrary(

  File ~\anaconda3\envs\k\lib\site-packages\pysindy\feature_library\generalized_library.py:124 in __init__
    if inputs_per_library.ndim != 2:

AttributeError: 'list' object has no attribute 'ndim'```

### PySINDy/Python version information:

import sys, pysindy; print(pysindy.__version__, sys.version)
1.7.5 3.10.12 | packaged by Anaconda, Inc. | (main, Jul  5 2023, 19:09:20) [MSC v.1916 64 bit (AMD64)]
Jacob-Stevens-Haas commented 8 months ago

Unable to reproduce on master.

You have a released version of python, but are running the example on the master branch. Try running the 1.7.5 version of this example.

koop14 commented 8 months ago

Thanks, the example works. I'm following the instructions and still getting the same error:

  1. N different libraries to add together 2. A list of inputs to use for each library. For two libraries with four inputs this would look like inputs_per_library = [[0, 1, 2, 3], [0, 1, 2, 3]] and to avoid using the first two input variables in the second library, you would change it to something like inputs_per_library = [[0, 1, 2, 3], [2, 2, 2, 3]], since duplicates are thrown out and [2, 2, 2, 3] will reduce to [2, 3].
poly1 = ps.PolynomialLibrary(degree=1, include_bias=True)
poly2 = ps.PolynomialLibrary(degree=1, include_bias=False)

#library 1: 1 bias, 3 state, 2 controls. library 2: 0 bias, 0 state, 2 controls
#The desired end result is 1 degree polynomial + interactions between state and controls
inputs_per_library = [[0, 1, 2, 3, 4, 5], [3,3,3,3,4]]

tensor_array = [[1, 1]]

generalized_library = ps.GeneralizedLibrary([poly1, poly2], 
                                            tensor_array=tensor_array, 
                                            inputs_per_library=inputs_per_library)

Traceback (most recent call last):

Cell In[5961], line 6 generalized_library = ps.GeneralizedLibrary([poly1, poly2],

File ~\anaconda3\envs\k\lib\site-packages\pysindy\feature_library\generalized_library.py:124 in init if inputs_per_library.ndim != 2:

AttributeError: 'list' object has no attribute 'ndim'

Jacob-Stevens-Haas commented 8 months ago

Cast inputs_per_library as a numpy array and it should work.