pysindy.differentiation package

Submodules

pysindy.differentiation.base module

Base class for numerical differentiation methods

class pysindy.differentiation.base.BaseDifferentiation[source]

Bases: BaseEstimator

Base class for differentiation methods.

Simply forces differentiation methods to implement a _differentiate function.

Attributes:

smoothed_x_ – Methods that smooth x before differentiating save that value here. Methods that do not simply save x here.

pysindy.differentiation.finite_difference module

class pysindy.differentiation.finite_difference.FiniteDifference(order=2, d: int = 1, axis=0, is_uniform=False, drop_endpoints=False, periodic=False)[source]

Bases: BaseDifferentiation

Finite difference derivatives.

Parameters:
  • order (int, optional (default 2)) – The order of the finite difference method to be used. Currently only centered differences are implemented, for even order and left-off-centered differences for odd order.

  • d (int, optional (default 1)) – The order of derivative to take. Must be positive integer.

  • axis (int, optional (default 0)) – The axis to differentiate along.

  • is_uniform (boolean, optional (default False)) – Parameter to tell the differentiation that, although a N-dim grid is passed, it is uniform so can use dx instead of the full grid array.

  • drop_endpoints (boolean, optional (default False)) – Whether or not derivatives are computed for endpoints. If False, endpoints will be set to np.nan. Note that which points are endpoints depends on the method being used.

  • periodic (boolean, optional (default False)) – Whether to use periodic boundary conditions for endpoints. Use forward differences for periodic=False and periodic boundaries with centered differences for periodic=True on the boundaries. No effect if drop_endpoints=True

Examples

>>> import numpy as np
>>> from pysindy.differentiation import FiniteDifference
>>> t = np.linspace(0, 1, 5)
>>> X = np.vstack((np.sin(t), np.cos(t))).T
>>> fd = FiniteDifference()
>>> fd._differentiate(X, t)
array([[ 1.00114596,  0.00370551],
       [ 0.95885108, -0.24483488],
       [ 0.8684696 , -0.47444711],
       [ 0.72409089, -0.67456051],
       [ 0.53780339, -0.84443737]])

pysindy.differentiation.sindy_derivative module

Wrapper classes for differentiation methods from the derivative package.

Some default values used here may differ from those used in derivative.

class pysindy.differentiation.sindy_derivative.SINDyDerivative(save_smooth=True, **kwargs)[source]

Bases: BaseDifferentiation

Wrapper class for differentiation classes from the derivative package. This class is meant to provide all the same functionality as the dxdt method.

This class also has _differentiate and __call__ methods which are used by PySINDy.

Parameters:

derivative_kws (dictionary, optional) –

Keyword arguments to be passed to the dxdt method.

Notes

See the derivative documentation for acceptable keywords.

set_params(**params)[source]

Set the parameters of this estimator. Modification of the pysindy method to allow unknown kwargs. This allows using the full range of derivative parameters that are not defined as member variables in sklearn grid search.

Return type:

self

get_params(deep=True)[source]

Get parameters.

pysindy.differentiation.smoothed_finite_difference module

class pysindy.differentiation.smoothed_finite_difference.SmoothedFiniteDifference(smoother=<function savgol_filter>, smoother_kws={}, save_smooth=True, **kwargs)[source]

Bases: FiniteDifference

Smoothed finite difference derivatives.

Perform differentiation by smoothing input data then applying a finite difference method.

Parameters:
  • smoother (function, optional (default savgol_filter)) – Function to perform smoothing. Must be compatible with the following call signature: x_smoothed = smoother(x, **smoother_kws)

  • smoother_kws (dict, optional (default {})) – Arguments passed to smoother when it is invoked.

  • save_smooth (bool) – Whether to save the smoothed coordinate values or not.

  • **kwargs (kwargs) – Additional parameters passed to the pysindy.FiniteDifference.__init__ function.

Examples

>>> import numpy as np
>>> from pysindy.differentiation import SmoothedFiniteDifference
>>> t = np.linspace(0,1,10)
>>> X = np.vstack((np.sin(t),np.cos(t))).T
>>> sfd = SmoothedFiniteDifference(smoother_kws={'window_length': 5})
>>> sfd._differentiate(X, t)
array([[ 1.00013114e+00,  7.38006789e-04],
       [ 9.91779070e-01, -1.10702304e-01],
       [ 9.73376491e-01, -2.20038119e-01],
       [ 9.43001496e-01, -3.26517615e-01],
       [ 9.00981354e-01, -4.29066632e-01],
       [ 8.47849424e-01, -5.26323977e-01],
       [ 7.84260982e-01, -6.17090177e-01],
       [ 7.11073255e-01, -7.00180971e-01],
       [ 6.29013295e-01, -7.74740601e-01],
       [ 5.39752150e-01, -8.41980082e-01]])

pysindy.differentiation.spectral_derivative module

class pysindy.differentiation.spectral_derivative.SpectralDerivative(d=1, axis=0)[source]

Bases: BaseDifferentiation

Spectral derivatives. Assumes uniform grid, and utilizes FFT to approximate a derivative. Works well for derivatives in periodic dimensions. Equivalent to a maximal-order finite difference, but runs in O(NlogN).

Parameters:
  • d (int) – The order of derivative to take

  • axis (int, optional (default 0)) – The axis to differentiate along

Examples

>>> import numpy as np
>>> from pysindy.differentiation import SpectralDerivative
>>> t = np.arange(0,1,0.1)
>>> X = np.vstack((np.sin(t), np.cos(t))).T
>>> sd = SpectralDerivative()
>>> sd._differentiate(X, t)
array([[ 6.28318531e+00,  2.69942771e-16],
   [ 5.08320369e+00, -3.69316366e+00],
   [ 1.94161104e+00, -5.97566433e+00],
   [-1.94161104e+00, -5.97566433e+00],
   [-5.08320369e+00, -3.69316366e+00],
   [-6.28318531e+00,  7.10542736e-16],
   [-5.08320369e+00,  3.69316366e+00],
   [-1.94161104e+00,  5.97566433e+00],
   [ 1.94161104e+00,  5.97566433e+00],
   [ 5.08320369e+00,  3.69316366e+00]])

Module contents

class pysindy.differentiation.BaseDifferentiation[source]

Bases: BaseEstimator

Base class for differentiation methods.

Simply forces differentiation methods to implement a _differentiate function.

Attributes:

smoothed_x_ – Methods that smooth x before differentiating save that value here. Methods that do not simply save x here.

class pysindy.differentiation.FiniteDifference(order=2, d: int = 1, axis=0, is_uniform=False, drop_endpoints=False, periodic=False)[source]

Bases: BaseDifferentiation

Finite difference derivatives.

Parameters:
  • order (int, optional (default 2)) – The order of the finite difference method to be used. Currently only centered differences are implemented, for even order and left-off-centered differences for odd order.

  • d (int, optional (default 1)) – The order of derivative to take. Must be positive integer.

  • axis (int, optional (default 0)) – The axis to differentiate along.

  • is_uniform (boolean, optional (default False)) – Parameter to tell the differentiation that, although a N-dim grid is passed, it is uniform so can use dx instead of the full grid array.

  • drop_endpoints (boolean, optional (default False)) – Whether or not derivatives are computed for endpoints. If False, endpoints will be set to np.nan. Note that which points are endpoints depends on the method being used.

  • periodic (boolean, optional (default False)) – Whether to use periodic boundary conditions for endpoints. Use forward differences for periodic=False and periodic boundaries with centered differences for periodic=True on the boundaries. No effect if drop_endpoints=True

Examples

>>> import numpy as np
>>> from pysindy.differentiation import FiniteDifference
>>> t = np.linspace(0, 1, 5)
>>> X = np.vstack((np.sin(t), np.cos(t))).T
>>> fd = FiniteDifference()
>>> fd._differentiate(X, t)
array([[ 1.00114596,  0.00370551],
       [ 0.95885108, -0.24483488],
       [ 0.8684696 , -0.47444711],
       [ 0.72409089, -0.67456051],
       [ 0.53780339, -0.84443737]])
class pysindy.differentiation.SINDyDerivative(save_smooth=True, **kwargs)[source]

Bases: BaseDifferentiation

Wrapper class for differentiation classes from the derivative package. This class is meant to provide all the same functionality as the dxdt method.

This class also has _differentiate and __call__ methods which are used by PySINDy.

Parameters:

derivative_kws (dictionary, optional) –

Keyword arguments to be passed to the dxdt method.

Notes

See the derivative documentation for acceptable keywords.

set_params(**params)[source]

Set the parameters of this estimator. Modification of the pysindy method to allow unknown kwargs. This allows using the full range of derivative parameters that are not defined as member variables in sklearn grid search.

Return type:

self

get_params(deep=True)[source]

Get parameters.

class pysindy.differentiation.SmoothedFiniteDifference(smoother=<function savgol_filter>, smoother_kws={}, save_smooth=True, **kwargs)[source]

Bases: FiniteDifference

Smoothed finite difference derivatives.

Perform differentiation by smoothing input data then applying a finite difference method.

Parameters:
  • smoother (function, optional (default savgol_filter)) – Function to perform smoothing. Must be compatible with the following call signature: x_smoothed = smoother(x, **smoother_kws)

  • smoother_kws (dict, optional (default {})) – Arguments passed to smoother when it is invoked.

  • save_smooth (bool) – Whether to save the smoothed coordinate values or not.

  • **kwargs (kwargs) – Additional parameters passed to the pysindy.FiniteDifference.__init__ function.

Examples

>>> import numpy as np
>>> from pysindy.differentiation import SmoothedFiniteDifference
>>> t = np.linspace(0,1,10)
>>> X = np.vstack((np.sin(t),np.cos(t))).T
>>> sfd = SmoothedFiniteDifference(smoother_kws={'window_length': 5})
>>> sfd._differentiate(X, t)
array([[ 1.00013114e+00,  7.38006789e-04],
       [ 9.91779070e-01, -1.10702304e-01],
       [ 9.73376491e-01, -2.20038119e-01],
       [ 9.43001496e-01, -3.26517615e-01],
       [ 9.00981354e-01, -4.29066632e-01],
       [ 8.47849424e-01, -5.26323977e-01],
       [ 7.84260982e-01, -6.17090177e-01],
       [ 7.11073255e-01, -7.00180971e-01],
       [ 6.29013295e-01, -7.74740601e-01],
       [ 5.39752150e-01, -8.41980082e-01]])
class pysindy.differentiation.SpectralDerivative(d=1, axis=0)[source]

Bases: BaseDifferentiation

Spectral derivatives. Assumes uniform grid, and utilizes FFT to approximate a derivative. Works well for derivatives in periodic dimensions. Equivalent to a maximal-order finite difference, but runs in O(NlogN).

Parameters:
  • d (int) – The order of derivative to take

  • axis (int, optional (default 0)) – The axis to differentiate along

Examples

>>> import numpy as np
>>> from pysindy.differentiation import SpectralDerivative
>>> t = np.arange(0,1,0.1)
>>> X = np.vstack((np.sin(t), np.cos(t))).T
>>> sd = SpectralDerivative()
>>> sd._differentiate(X, t)
array([[ 6.28318531e+00,  2.69942771e-16],
   [ 5.08320369e+00, -3.69316366e+00],
   [ 1.94161104e+00, -5.97566433e+00],
   [-1.94161104e+00, -5.97566433e+00],
   [-5.08320369e+00, -3.69316366e+00],
   [-6.28318531e+00,  7.10542736e-16],
   [-5.08320369e+00,  3.69316366e+00],
   [-1.94161104e+00,  5.97566433e+00],
   [ 1.94161104e+00,  5.97566433e+00],
   [ 5.08320369e+00,  3.69316366e+00]])