dynamicslab / pysindy

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

Failing to simulate and ending up with a straight line instead of the discovered model #285

Closed SM-CAU closed 1 year ago

SM-CAU commented 1 year ago

Hi , I am Sayeed. Studying Materials Science and Engineering and working on Magnetoelectric sensors. I am using SINDy method to predict Magnetoelectric sensor output . When I applied the SINDy-PI on my data it discovers the model closely (for example if I excite my sensor using a sinusoidal signal I get x' = cosx , that seems correct or close.) But when I sindy code sindy simulated simulate the model it always returns a straight line (I used Chirp and Sine signal to excite my sensors and in both cases I came up with a straight line.) It seems SINDy is simulatiing x'=x' in every case. How to get rid of that?

problem 2: I cant zoom in the figure I get after simulation to compare how well is the fit. But in Matlab it zooms well. Can anyone tell me How to solve this? I once accidentally got figures that can be zoomed but I can't reproduce it again. I am new to coding . Any help would be appreciated.

akaptano commented 1 year ago

I think the model is just giving you zeros because it was not able to find a good fit. You should plot the derivative of x_train (model.differentiate(x_train)) versus the model prediction (model.predict(x_train, t=t_train)), which will tell you if your model is fitting the data at all.

If this is a matplotlib plot, you should be able to zoom in with the magnifying glass icon.

SM-CAU commented 1 year ago

Thank you so much for your reply. I am not sure I could implement your hint properly , I ended up with the same plot. The change I made is attached below. Can you please have a short look? Thanks in advance. Have a great day.

On Sun, Feb 26, 2023 at 5:05 PM Alan Kaptanoglu @.***> wrote:

I think the model is just giving you zeros because it was not able to find a good fit. You should plot the derivative of x_train (model.differentiate(x_train)) versus the model prediction (model.predict(x_train, t=t_train)), which will tell you if your model is fitting the data at all.

If this is a matplotlib plot, you should be able to zoom in with the magnifying glass icon.

— Reply to this email directly, view it on GitHub https://github.com/dynamicslab/pysindy/issues/285#issuecomment-1445397652, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6BP2PZXUDOLGCCTENDZ2BLWZN5MPANCNFSM6AAAAAAVIO4ACI . You are receiving this because you authored the thread.Message ID: @.***>

akaptano commented 1 year ago

Sorry, I do not see a new plot here. Could you post the code you used too?

SM-CAU commented 1 year ago

Hi, I have attached the code I used!
problem description : Based on the assumption that our sensors motion of equation follows duffing equation which is a 2nd order differential equation, I am trying to predict its output via SINDy method. Problems facing: 1. Although I am using Matplotlib plot , there is no zooming option. I also tried xlim and ylim but did not actually do anything. I need to be able to zoom a high frequency signal (KHz , e.g.) so that I can see the individual portion of the signal. If I use the zoom button (magnifier glass) option , the image actually doesnt' resolve to anything , just become more hazy. problem 2: the model actually performig so bad that its correlation factor is almost 0. I think SINDy should give good result if I use only polynomial library

Sindy_Sine_1 sindy_sine_2 sindy_sine_3 sindy_sine_4 sindy_sine_5

akaptano commented 1 year ago

I think I agree with what you did here -- however if this is a Duffing system, presumably there is a control input that drives the system like cosine(omega * t). If this term is not modeled in the ODEs you will indeed get something very wrong. If that is the case, you can give that a try by using the FourierLibrary with Cosines, applied to the 't' variable. This functionality is provided for by the GeneralizedLibrary class. See the example 1 jupyter notebook for an example of the use case. If this system does not have a driving term, let me know and I will stare at the code a bit harder.

Best, Alan

SM-CAU commented 1 year ago

Hey, I tried to do apply this generalized library according to the example. i also watched your tutorial videos on you-tube several times, but it seems like I am stuck and to be honest I did not understand the implementation. how can I pass it to sindy so that it generates the fourier library using only t variable? How the tile function is linking to the inputs? How do they know 0 or 1= x_train (x and xdot stacked), or t_train? Your suggestion make sense as this term is also evolving with time and in every instance should ( or say it might) have different driving force on my magnetostrictive layer of the sensor. I added my implementation, but its actually didnt reflect my intention.

Best, Sayeed trying generalized library

SM-CAU commented 1 year ago

Hello, As I understood from the demostratation of Generalized Library Class in example 1 for Lorenz system, you have three input variable x,y,z. Thats why inputs_per_library was np.tile([0,1,2], 2) for the two libraries and then reshape for 2 libraries having 3 inputs each. I found no example where time variable can be trigerred for a library.

Howevere, as my data_x_train has 2 inputs (recorded data, their numerical derivatives), I tried to stack the t_train into x_train and when calling them in the library , for the poly I excluded t_train and for fourier I only used t_train. But I get an error like this 'IndexError: index 2 is out of bounds for axis 0 with size 2'. As inputs_per_library is a list I can't index them to take only the rows of my t_train variable. Can you please help me out? Best, sayeed index_error stact t_train

akaptano commented 1 year ago

Can you post your code as text/jupyter notebook here so I can copy and paste the full script to a jupyter notebook and check for issues?

SM-CAU commented 1 year ago

Hi, Sure! The code isd attached as a jupyter notebook file.

On Tue, Apr 4, 2023 at 6:41 PM Alan Kaptanoglu @.***> wrote:

Can you post your code as text/jupyter notebook here so I can copy and paste the full script to a jupyter notebook and check for issues?

— Reply to this email directly, view it on GitHub https://github.com/dynamicslab/pysindy/issues/285#issuecomment-1496284747, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6BP2P2MXN67QCZWLV3FFT3W7RFJ5ANCNFSM6AAAAAAVIO4ACI . You are receiving this because you authored the thread.Message ID: @.***>

akaptano commented 1 year ago

I do not see the file here or in the email you sent, double check that please :)

SM-CAU commented 1 year ago

sensor_chirp.txt Hey, sorry for the inconvenience. In the email the attachment is still there but in the github ithe json file appears not be supported with. I hope it works this time.

SM-CAU commented 1 year ago

Hi, I could apply the generalized library as you recommended but still end-up with very por performance. Can you please give me hint to make it perform better? ### sindy prediction of the collected da.txt

akaptano commented 1 year ago

I am still unable to help you. You have not provided a detailed error/issue message or pictures showing the poor model performance + I do not have the .mat file of the data you are using in this jupyter notebook, so I can only stare at the code you sent.

SM-CAU commented 1 year ago

Hello, The poor performance was the same as before using the generalized library which I have given before, that's why I thought it would be a repetition and didn't do so. Whatsoever, the data file is attached below. In github I am unable to share a .mat file , so I am providing it via email. Best , Sayeed.

On Sat, Apr 15, 2023 at 11:29 PM Alan Kaptanoglu @.***> wrote:

I am still unable to help you. You have not provided a detailed error/issue message or pictures showing the poor model performance + I do not have the .mat file of the data you are using in this jupyter notebook, so I can only stare at the code you sent.

— Reply to this email directly, view it on GitHub https://github.com/dynamicslab/pysindy/issues/285#issuecomment-1509980008, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6BP2P6Q5YN3CWSG6TVKCK3XBMHMJANCNFSM6AAAAAAVIO4ACI . You are receiving this because you authored the thread.Message ID: @.***>

SM-CAU commented 1 year ago

its solved.