utility

All kinds of utilities

pynumdiff.utils.utility.estimate_initial_condition(x, x_hat)

Integration leaves an unknown integration constant. This function finds a best fit integration constant given x, and x_hat (the integral of dxdt_hat)

Parameters
  • x (np.array) – timeseries of measurements

  • x_hat (np.array) – smoothed estiamte of x, for the purpose of this function this should have been determined by integrate_dxdt_hat

Returns

integration constant (i.e. initial condition) that best aligns x_hat with x

Return type

float

pynumdiff.utils.utility.finite_difference(x, dt)
Parameters
  • x – (np.array of floats, 1xN) time series to differentiate

  • dt – (float) time step

  • params – (list): (int, optional) number of iterations ignored if ‘iterate’ not in options

  • options – (bool) iterate the finite difference method (smooths the estimates)

Returns

x_hat: smoothed x; dxdt_hat: derivative of x

pynumdiff.utils.utility.get_filenames(path, contains, does_not_contain=('~', '.pyc'))

Create list of files found in given path that contain or do not contain certain strings.

Parameters
  • path (string (directory path)) – path in which to look for files

  • contains (string) – string that filenames must contain

  • does_not_contain (list of strings) – list of strings that filenames must not contain, optional

Returns

list of filenames

Return type

list of strings

pynumdiff.utils.utility.hankel_matrix(x, num_delays, pad=False)
Parameters
  • x – numpy array or matrix

  • num_delays – int, number of times to 1-step shift data

  • pad

Returns

a Hankel Matrix m

e.g. if

x = [a, b, c, d, e] and num_delays = 3

then with pad = False:
m = [[‘a’, ‘b’, ‘c’],

[‘b’, ‘c’, ‘d’], [‘c’, ‘d’, ‘e’]]

or pad = True:
m = [[‘a’, ‘b’, ‘c’, ‘d’, ‘e’],

[‘b’, ‘c’, ‘d’, ‘e’, 0], [‘c’, ‘d’, ‘e’, 0, 0]]

pynumdiff.utils.utility.integrate_dxdt_hat(dxdt_hat, dt)

Wrapper for scipy.integrate.cumtrapz to integrate dxdt_hat that ensures the integral has the same length

Parameters
  • dxdt_hat (np.array) – estimate derivative of timeseries

  • dt (float) – time step in seconds

Returns

integral of dxdt_hat

Return type

np.array

pynumdiff.utils.utility.is_odd(num)

tell if it is an odd number

Parameters

num – (integer) the number we need to tell

Returns

(boolean) true or false

pynumdiff.utils.utility.isnotebook()

Checks to see if the environment is an interactive notebook or not. :return: True or False

pynumdiff.utils.utility.matrix_inv(X, max_sigma=1e-16)

Stable (pseudo) matrix inversion using singular value decomposition

Parameters
  • X (np.matrix or np.array) – matrix to invert

  • max_sigma (float) – smallest singular values to take into account. matrix will be truncated prior to inversion based on this value.

Returns

matrix pseudo inverse

Return type

np.array or np.matrix

pynumdiff.utils.utility.peakdet(v, delta, x=None)

Find peaks and valleys of 1D array. A point is considered a maximum peak if it has the maximal value, and was preceded (to the left) by a value lower by delta.

Converted from MATLAB script at http://billauer.co.il/peakdet.html % Eli Billauer, 3.4.05 (Explicitly not copyrighted). % This function is released to the public domain; Any use is allowed.

Parameters
  • v – array for which to find peaks and valleys

  • delta (float) – threshold for finding peaks and valleys. A point is considered a maximum peak if it has the maximal value, and was preceded (to the left) by a value lower by delta.

Typpe v

np.array

Returns

tuple of min and max locations and values: - maxtab: array with locations (column 1) and values of maxima (column 2) - mintab: array with locations (column 1) and values of minima (column 2)

Return type

tuple -> (np.array, np.array)

pynumdiff.utils.utility.total_variation(x)

Calculate the total variation of an array

Parameters

x (np.array) – timeseries

Returns

total variation

Return type

float