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

added SINDy library input option to WeakPDELibrary #433

Closed znicolaou closed 2 months ago

znicolaou commented 7 months ago

As a partial response to issue #351 and discussion #429 here, I'm adding an option to replace the library_functions and function_names input of WeakPDELibrary with library, which is an existing instance of BaseFeatureLibrary. I've only tested so far on the first example in the example notebook 12, which reproduces the previous result:

import numpy as np
import pysindy as ps
from scipy.integrate import solve_ivp
from pysindy.utils import lorenz

# Generate measurement data
dt = 0.002
t_train = np.arange(0, 10, dt)
t_train_span = (t_train[0], t_train[-1])
u0_train = [-8, 8, 27]
u_train = solve_ivp(
    lorenz, t_train_span, u0_train, t_eval=t_train
).y.T

# Define weak form ODE library
# defaults to derivative_order = 0 if not specified,
# and if spatial_grid is not specified, defaults to None,
# which allows weak form ODEs.
poly_lib = ps.PolynomialLibrary(
    degree=2,
    include_bias=False
)

ode_lib2 = ps.WeakPDELibrary(
    library=poly_lib,
    spatiotemporal_grid=t_train,
    is_uniform=True,
    K=100,
)

# Instantiate and fit the SINDy model with the integral of u_dot
model = ps.SINDy(feature_library=ode_lib2, optimizer)
model.fit(u_train)
model.print()

I'd also like to take some time to look through the PDE example notebooks to clean them up and make sure deprecated input has been removed, so I'm making this a draft for now. If anyone has other suggestions on modifications to the weak/pde input or functionality, this is a good chance. I may also look into #222 if it isn't a huge headache.

codecov[bot] commented 7 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (2633ee0) 93.87% compared to head (6642ca5) 93.90%. Report is 1 commits behind head on master.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #433 +/- ## ========================================== + Coverage 93.87% 93.90% +0.02% ========================================== Files 37 37 Lines 3626 3608 -18 ========================================== - Hits 3404 3388 -16 + Misses 222 220 -2 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

Jacob-Stevens-Haas commented 7 months ago

Thank you @znicolaou, this is a great feature! I'll review the code today or tomorrow. Do you have a simple test you could add for this?

I would defer #222 to a later PR, as it would help keep this PR simple.

znicolaou commented 7 months ago

Thanks, @Jacob-Stevens-Haas ! Yes, will revisit the tests and the example notebooks in the next day or two!

znicolaou commented 7 months ago

Last point about deprecating--I think we should probably implement the same change in PDELibrary in addition, since it also used the same library_functions convention that is essentially repeating the CustomLibrary. Again, will take just a few small changes to the code but a bit of time to not break examples...

znicolaou commented 7 months ago

I think this is done now, pending any other issues. Can take a look once more, but I have other work that I need to focus on now. If things look okay, happy to merge any time.

Jacob-Stevens-Haas commented 7 months ago

I think this is done now, pending any other issues. Can take a look once more, but I have other work that I need to focus on now. If things look okay, happy to merge any time.

If you need to move onto other things, would you like me to take this PR over the finish line?

Jacob-Stevens-Haas commented 5 months ago

With that PR and issue, are you ok if we close this, Zach? (But let's keep the branch around so that we can pull in TrimmedLibrary)