API Reference

There are two ways to interact with the code. The first way is to do a specific import of the desired derivative object and explicitely construct the implementation,

from derivative import FiniteDifference
result = FiniteDifference(k=1).d(x,t)

The second way is top use the functional interface and pass the kind of derivative as an argument along with any required parameters,

from derivative import dxdt
result = dxdt(x, t, "finite_difference", k=1)


Main module (derivative.differentiation)

dxdt (functional interface)

derivative.differentiation.dxdt(x, t, kind=None, axis=1, **kwargs)

Compute the derivative of x with respect to t along axis using the numerical derivative specified by “kind”. This is the functional interface of the Derivative class.

This function requires that X and t have equal length along axis. An empty X results in an empty derivative. If X.shape[axis] == 1, then the derivative cannot be computed in a reasonable way and X is returned.

The implementation ‘kind’, an instance of the Derivative class, is responsbile for determining the behavior.

Parameters
  • x (ndarray of float) – Ordered measurement values.

  • t (ndarray of float) – Ordered measurement times.

  • kind (string) – Derivative method name (see available kinds).

  • axis ({0,1}) – Axis of x along which to differentiate. Default 1.

  • **kwargs – Keyword arguments for the derivative method “kind”.

Available kinds
  • finite_difference. Required kwargs: k (symmetric window size as index).

  • savitzky_golay. Required kwargs: order (of a fit polynomial), left, right (window size).

  • spectral. Required kwargs: None.

  • spline. Required kwargs: s (smoothing).

  • trend_filtered. Required kwargs: order (of a fit polynomial), alpha (regularization).

Returns

Returns dx/dt along axis.

Return type

ndarray of float

Derivative (base class)

class derivative.differentiation.Derivative

Interface for computing numerical derivatives.

abstract compute(t, x, i)

Compute the derivative of one-dimensional data x with respect to t at the index i of x, (dx/dt)[i].

Computation of a derivative should fail explicitely if the implementation is unable to compute a derivative at the desired index. Used for global differentiation methods, for example.

This requires that x and t have equal lengths >= 2, and that the index i is a valid index.

For each implementation, any exceptions raised by a valid input should either be handled or denoted in the implementation docstring. For example, some implementations may raise an exception when x and t have length 2.

Parameters
  • t (ndarray of float) – Ordered measurement times.

  • x (ndarray of float) – Ordered measurement values.

  • i (int) – Index i at which to compute (dx/dt)[i]

Returns

(dx/dt)[i]

Return type

float

compute_for(t, x, indices)

Compute derivative (dx/dt)[i] for i in indices. Overload this if desiring a more efficient computation over a list of indices.

This function requires that x and t have equal length along axis, and that all of the indicies are valid.

Parameters
  • t (ndarray of float) – Ordered measurement times.

  • x (ndarray of float) – Ordered measurement values.

  • indices (ndarray of int) – Indices i at which to compute (dx/dt)[i]

Returns

yields (dx/dt)[i] for i in indices

Return type

Generator[float]

compute_x(t, x, i)

Compute smoothed values of one-dimensional data x at the index i of x. Overload this if subclass actually smooths values.

This requires that x and t have equal lengths >= 2, and that the index i is a valid index.

For each implementation, any exceptions raised by a valid input should either be handled or denoted in the implementation docstring. For example, some implementations may raise an exception when x and t have length 2.

Parameters
  • t (ndarray of float) – Ordered measurement times.

  • x (ndarray of float) – Ordered measurement values.

  • i (int) – Index i at which to returned smoothed values

Returns

float

compute_x_for(t, x, indices)

Compute smoothed values of x at each i in indices. Overload this if desiring a more efficient computation over a list of indices.

This function requires that x and t have equal length along axis, and that all of the indicies are valid.

Parameters
  • t (ndarray of float) – Ordered measurement times.

  • x (ndarray of float) – Ordered measurement values.

  • indices (ndarray of int) – Indices i at which to compute (dx/dt)[i]

Returns

yields (dx/dt)[i] for i in indices

Return type

Generator[float]

d(X, t, axis=1)

Compute the derivative of measurements X taken at times t.

An empty X results in an empty derivative. If X.shape[axis] == 1, then the derivative cannot be computed in a reasonable way and X is returned.

Parameters
  • X (ndarray of float) – Ordered measurements values. Multiple measurements allowed.

  • t (ndarray of float) – Ordered measurement times.

  • axis ({0,1}) –

Returns

Returns dX/dt along axis.

Return type

ndarray of float

Raises

ValueError – Requires that X.shape[axis] equals len(t). If X is flat, requires that len(X) equals len(t).

x(X, t, axis=1)

Compute the smoothed X values from measurements X taken at times t.

Not all methods perform smoothing when calculating derivatives. In these cases, X is returned unmodified

Parameters
  • X (ndarray of float) – Ordered measurements values. Multiple measurements allowed.

  • t (ndarray of float) – Ordered measurement times.

  • axis ({0,1}) –

Returns

Returns dX/dt along axis.

Return type

ndarray of float