Package: pysindy
PySINDyis a sparse regression package with several implementations for the Sparse Identification of Nonlinear Dynamical systems (SINDy) method introduced in Brunton et al. (2016a), including the unified optimization approach of Champion et al. (2019), SINDy with control from Brunton et al. (2016b), Trapping SINDy from Kaptanoglu et al. (2021), SINDy-PI from Kaheman et al. (2020), PDE-FIND from Rudy et al. (2017), and so on. A comprehensive literature review is given in de Silva et al. (2020) and Kaptanoglu, de Silva et al. (2021).Table of contentsSystem identificationHow it worksRelation to PySINDyExampleInstallationInstalling with pipInstalling from sourceCaveatsDocumentationCommunity guidelinesContributing examplesContributing codeReporting issues or bugsGetting helpCiting PySINDyReferencesRelated packagesContributorsSystem identificationSystem identification refers to the process of leveraging measurement data to infer governing equations, in the form of dynamical systems, describing the data. Once discovered, these equations can make predictions about future states, can inform control inputs, or can enable the theoretical study using analytical techniques.
Dynamical systems are a flexible, well-studied class of mathematical objects for modeling systems evolving in time.
SINDy is a model discovery method which usessparse regressionto infer nonlinear dynamical systems from measurement data.
The resulting models are inherentlyinterpretableandgeneralizable.How it worksSuppose, for some physical system of interest, we have measurements of state variablesx(t)(a vector of length n) at different points in time. Examples of state variables include the position, velocity, or acceleration of objects; lift, drag, or angle of attack of aerodynamic objects; and concentrations of different chemical species. If we suspect that the system could be well-modeled by a dynamical system of the formx'(t) = f(x(t)),then we can use SINDy to learnf(x)from the data (x'(t)denotes the time derivative ofx(t)). Note that bothf(x)andx(t)are typically vectors. The fundamental assumption SINDy employs is that each component off(x),f_i(x)can be represented as asparselinear combination of basis functionstheta_j(x)f_i(x) = theta_1(x) * xi_{1,i} + theta_2(x) * xi_{2,i} + ... + theta_k * xi{k,i}Concatenating all the objects into matrices (denoted with capitalized names) helps to simplify things.
To this end we place all measurements of the state variables into a data matrixX(with a row per time measurement and a column per variable), the derivatives of the state variables into a matrixX', all basis functions evaluated at all points in time into a matrixTheta(X)(each basis function gets a column), and all coefficients into a third matrixXi(one column per state variable).
The approximation problem to be solved can then be compactly written asX' = Theta(X) * Xi.Each row of this matrix equation corresponds to one coordinate function off(x).
SINDy employs sparse regression techniques to find a solutionXiwith sparse column vectors.
For a more in-depth look at the mathematical foundations of SINDy, please see ourintroduction to SINDy.Relation to PySINDyThe PySINDy package revolves around theSINDyclass which consists of three primary components; one for each term in the above matrix approximation problem.differentiation_method: computesX', though if derivatives are known or measured directly, they can be used insteadfeature_library: specifies the candidate basis functions to be used to constructTheta(X)optimizer: implements a sparse regression method for solving forXiOnce aSINDyobject has been created it must be fit to measurement data, similar to ascikit-learnmodel. It can then be used to predict derivatives given new measurements, evolve novel initial conditions forward in time, and more. PySINDy has been written to be as compatible withscikit-learnobjects and methods as possible.ExampleSuppose we have measurements of the position of a particle obeying the following dynamical system at different points in timex' = -2x
y' = yNote that this system of differential equations decouples into two differential equations whose solutions are simplyx(t) = x_0 *exp(-2* t)andy(t) = y_0 * exp(t), wherex_0 = x(0)andy_0 = y(0)are the initial conditions.Using the initial conditionsx_0 = 3andy_0 = 0.5, we construct the data matrixX.importnumpyasnpimportpysindyaspst=np.linspace(0,1,100)x=3*np.exp(-2*t)y=0.5*np.exp(t)X=np.stack((x,y),axis=-1)# First column is x, second is yTo instantiate aSINDyobject with the default differentiation method, feature library, and optimizer and then fit it to the data, we invokemodel=ps.SINDy(feature_names=["x","y"])model.fit(X,t=t)We use thefeature_namesargument so that the model prints out the correct labels forxandy. We can inspect the governing equations discovered by the model and check whether they seem reasonable with theprintfunction.model.print()which prints the followingx' = -2.000 x
y' = 1.000 yPySINDy provides numerous other features not shown here. We recommend thefeature overviewsection of the documentation for a more exhaustive summary of additional features.InstallationInstalling with pipIf you are using Linux or macOS you can install PySINDy with pip:pipinstallpysindyInstalling from sourceFirst clone this repository:gitclonehttps://github.com/dynamicslab/pysindy.gitThen, to install the package, runpipinstall.If you do not have pip you can instead usepythonsetup.pyinstallIf you do not have root access, you should add the--useroption to the above lines.CaveatsIf you would like to use theSINDy-PIoptimizer, theTrapping SINDyoptimizer (TrappingSR3), or the other SR3 optimizations with inequality constraints, you will also need to install the cvxpy package, e.g. withpip install cvxpy.To run the unit tests, example notebooks, or build a local copy of the documentation, you should install the additional dependencies inrequirements-dev.txtpipinstall-rrequirements-dev.txtDocumentationThe documentation site for PySINDy can be foundhere. There are numerousexamplesof PySINDy in action to help you get started. Examples are also available asJupyter notebooks. A video overview of PySINDy can be found onYoutube. We have also created avideo playlistwith practical PySINDy tips.PySINDy implements a lot of advanced functionality that may be overwhelming for new users or folks who are unfamiliar with these methods. Below (see here if image does not renderhttps://github.com/dynamicslab/pysindy/blob/master/docs/JOSS2/Fig3.png), we provide a helpful flowchart for figuring out which methods to use, given the characteristics of your dataset:This flow chart summarizes howPySINDyusers can start with a dataset and systematically choose the proper candidate library and sparse regression optimizer that are tailored for a specific scientific task. TheGeneralizedLibraryclass allows for tensoring, concatenating, and otherwise combining many different candidate libraries.Community guidelinesContributing examplesWe love seeing examples of PySINDy being used to solve interesting problems! If you would like to contribute an example, reach out to us by creating an issue.At a minimum, we need to be able to run the example notebooks in the normal mode as well as in a test mode that uses smaller data in order to run faster and simply verify that cells execute without error.  In order to do that, your example should obey the following directory tree./<name_of_example>/
\
 |-example.py # save your notebook as a python script
 |-example_data.py # has functions to create/load data
 |-mock_data.py # has functions with same name as in example_data.py which create/load smaller datasets
 |-example.ipynb # run python examples/publish_notebook/<name_of_example> to generate this.  Needs packages in requirements-dev.txt
 |-utils.py (Any other names example.py needs to import.  Any additional local modules imported by example.py need to be submodules of utils.py, e.g. utils.plotting)You can optimize your notebook for testing by checking__name__.  When our tests runexample.pythey set the__name__global to"testing".  For instance, your notebook should determine whether to import frommock_dataorexample_datausing this method (another example: you could also use this method to setmax_iter).  It’s a bit arbitrary, but try to make your examples run in under ten seconds using the mock data.  You can use our test to verify your example in testing mode:pytest -k test_external --external-notebook="path/to/<name_of_example>"Contributing codeWe welcome contributions to PySINDy. To contribute a new feature please submit a pull request. To get started we recommend installing the packages inrequirements-dev.txtviapipinstall-rrequirements-dev.txtThis will allow you to run unit tests and automatically format your code. To be accepted your code should conform to PEP8 and pass all unit tests. Code can be tested by invokingpytestWe recommend usingpre-committo format your code. Once you have staged changes to commitgitaddpath/to/changed/file.pyyou can run the following to automatically reformat your staged codepre-commitNote that you will then need to re-stage any changespre-commitmade to your code.Building documentation requires [pandoc](https://pandoc.org/installing.html) as a separate install.  Once installed, runpython-msphinx-TEbhtml-d_build/doctrees-Dlanguage=en../buildOr check the build step in the most recent CI run or [RTD build](https://readthedocs.org/projects/pysindy/builds/).There are a number of SINDy variants and advanced functionality that would be great to implement in future releases:Bayesian SINDy, for instance that from Hirsh, Seth M., David A. Barajas-Solano, and J. Nathan Kutz. “Sparsifying Priors for Bayesian Uncertainty Quantification in Model Discovery.” arXiv preprint arXiv:2107.02107 (2021).Tensor SINDy, using the methods in Gelß, Patrick, et al. “Multidimensional approximation of nonlinear dynamical systems.” Journal of Computational and Nonlinear Dynamics 14.6 (2019).Stochastic SINDy, using the methods in Brückner, David B., Pierre Ronceray, and Chase P. Broedersz. “Inferring the dynamics of underdamped stochastic systems.” Physical review letters 125.5 (2020): 058103.Integration of PySINDy with a Python model-predictive control (MPC) code.The PySINDy weak formulation is based on the work in Reinbold, Patrick AK, Daniel R. Gurevich, and Roman O. Grigoriev. “Using noisy or incomplete data to discover models of spatiotemporal dynamics.” Physical Review E 101.1 (2020): 010203. It might be useful to additionally implement the weak formulation from Messenger, Daniel A., and David M. Bortz. “Weak SINDy for partial differential equations.” Journal of Computational Physics (2021): 110525. The weak formulation in PySINDy is also fairly slow and computationally intensive, so finding ways to speed up the code would be great.The blended conditional gradients (BCG) algorithm for solving the constrained LASSO problem, Carderera, Alejandro, et al. “CINDy: Conditional gradient-based Identification of Non-linear Dynamics–Noise-robust recovery.” arXiv preprint arXiv:2101.02630 (2021).Reporting issues or bugsIf you find a bug in the code or want to request a new feature, please open an issue.Getting helpFor help using PySINDy please consult thedocumentationand/or ourexamples, or create an issue.Citing PySINDyPySINDy has been published in the Journal of Open Source Software (JOSS). The paper can be foundhere.If you use PySINDy in your work, please cite it using the following two references:Brian M. de Silva, Kathleen Champion, Markus Quade, Jean-Christophe Loiseau, J. Nathan Kutz, and Steven L. Brunton., (2020).PySINDy: A Python package for the sparse identification of nonlinear dynamical systems from data.Journal of Open Source Software, 5(49), 2104,https://doi.org/10.21105/joss.02104Kaptanoglu et al., (2022). PySINDy: A comprehensive Python package for robust sparse system identification. Journal of Open Source Software, 7(69), 3994,https://doi.org/10.21105/joss.03994Bibtex:@article{desilva2020,
doi = {10.21105/joss.02104},
url = {https://doi.org/10.21105/joss.02104},
year = {2020},
publisher = {The Open Journal},
volume = {5},
number = {49},
pages = {2104},
author = {Brian de Silva and Kathleen Champion and Markus Quade and Jean-Christophe Loiseau and J. Kutz and Steven Brunton},
title = {PySINDy: A Python package for the sparse identification of nonlinear dynamical systems from data},
journal = {Journal of Open Source Software}
}Bibtex:@article{Kaptanoglu2022,
doi = {10.21105/joss.03994},
url = {https://doi.org/10.21105/joss.03994},
year = {2022},
publisher = {The Open Journal},
volume = {7},
number = {69},
pages = {3994},
author = {Alan A. Kaptanoglu and Brian M. de Silva and Urban Fasel and Kadierdan Kaheman and Andy J. Goldschmidt and Jared Callaham and Charles B. Delahunt and Zachary G. Nicolaou and Kathleen Champion and Jean-Christophe Loiseau and J. Nathan Kutz and Steven L. Brunton},
title = {PySINDy: A comprehensive Python package for robust sparse system identification},
journal = {Journal of Open Source Software}
}Referencesde Silva, Brian M., Kathleen Champion, Markus Quade,
Jean-Christophe Loiseau, J. Nathan Kutz, and Steven L. Brunton.PySINDy: a Python package for the sparse identification of
nonlinear dynamics from data.arXiv preprint arXiv:2004.08424 (2020)[arXiv]Kaptanoglu, Alan A., Brian M. de Silva, Urban Fasel, Kadierdan Kaheman, Andy J. Goldschmidt
Jared L. Callaham, Charles B. Delahunt, Zachary G. Nicolaou, Kathleen Champion,
Jean-Christophe Loiseau, J. Nathan Kutz, and Steven L. Brunton.PySINDy: A comprehensive Python package for robust sparse system identification.arXiv preprint arXiv:2111.08481 (2021).[arXiv]Brunton, Steven L., Joshua L. Proctor, and J. Nathan Kutz.Discovering governing equations from data by sparse identification
of nonlinear dynamical systems.Proceedings of the National
Academy of Sciences 113.15 (2016): 3932-3937.[DOI]Champion, K., Zheng, P., Aravkin, A. Y., Brunton, S. L., & Kutz, J. N. (2020).A unified sparse optimization framework to learn parsimonious physics-informed
models from data.IEEE Access, 8, 169259-169271.[DOI]Brunton, Steven L., Joshua L. Proctor, and J. Nathan Kutz.Sparse identification of nonlinear dynamics with control (SINDYc).IFAC-PapersOnLine 49.18 (2016): 710-715.[DOI]Kaheman, K., Kutz, J. N., & Brunton, S. L. (2020).SINDy-PI: a robust algorithm for parallel implicit sparse identification
of nonlinear dynamics.Proceedings of the Royal Society A, 476(2242), 20200279.[DOI]Kaptanoglu, A. A., Callaham, J. L., Aravkin, A., Hansen, C. J., & Brunton, S. L. (2021).Promoting global stability in data-driven models of quadratic nonlinear dynamics.Physical Review Fluids, 6(9), 094401.[DOI]Related packagesDeeptime- A Python library for the analysis of time series data with methods for dimension reduction, clustering, and Markov model estimation.PyDMD- A Python package using the Dynamic Mode Decomposition (DMD) for a data-driven model simplification based on spatiotemporal coherent structures. DMD is a great alternative to SINDy.PySINDyGUI- A slick-looking GUI for PySINDy.SEED- Software for the Extraction of Equations from Data: a GUI for many of the methods provided by PySINDy.SymINDy- A Python package combining SINDy with genetic programming-based symbolic regression, used for the functions library optimization.ContributorsThis repository is a fork fromoriginal workbyMarkus Quade.Thanks to the members of the community who have contributed to PySINDy!billtubbsBug fix#68kopytjukConcatenation feature for libraries#72andgoldschmidtderivativepackage for numerical differentiation#85