Skip to content

Commit 57b968e

Browse files
committed
addressing #71 for this file while I'm touching it
1 parent 488d6eb commit 57b968e

File tree

1 file changed

+23
-44
lines changed

1 file changed

+23
-44
lines changed

pynumdiff/linear_model/_linear_model.py

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -96,47 +96,41 @@ def _slide_function(func, x, dt, args, window_size, step_size, kernel_name):
9696
#########################
9797
# Savitzky-Golay filter #
9898
#########################
99-
def savgoldiff(x, dt, params, options=None):
100-
"""
101-
Use the Savitzky-Golay to smooth the data and calculate the first derivative. It wses scipy.signal.savgol_filter. The Savitzky-Golay is very similar to the sliding polynomial fit, but slightly noisier, and much faster
102-
103-
:param x: array of time series to differentiate
104-
:type x: np.array (float)
105-
106-
:param dt: time step size
107-
:type dt: float
108-
109-
:param params: a list of three elements:
110-
111-
- N: order of the polynomial
112-
- window_size: size of the sliding window, must be odd (if not, 1 is added)
113-
- smoothing_win: size of the window used for gaussian smoothing, a good default is window_size, but smaller for high frequnecy data
114-
115-
:type params: list (int)
116-
117-
:return: a tuple consisting of:
118-
119-
- x_hat: estimated (smoothed) x
120-
- dxdt_hat: estimated derivative of x
99+
def savgoldiff(x, dt, params=None, options=None, polynomial_order=None, window_size=None, smoothing_win=None):
100+
"""Use the Savitzky-Golay to smooth the data and calculate the first derivative. It wses scipy.signal.savgol_filter. The Savitzky-Golay is very similar to the sliding polynomial fit, but slightly noisier, and much faster
121101
102+
:param np.array[float] x: array of time series to differentiate
103+
:param float dt: time step size
104+
:param list params: (**deprecated**, prefer :code:`polynomial_order`, :code:`window_size`, and :code:`smoothing_win`)
105+
:param dict options: (**deprecated**)
106+
:param int polynomial_order: order of the polynomial
107+
:param int window_size: size of the sliding window, must be odd (if not, 1 is added)
108+
:param int smoothing_win: size of the window used for gaussian smoothing, a good default is window_size, but smaller for high frequnecy data
122109
123-
:rtype: tuple -> (np.array, np.array)
110+
:return: tuple[np.array, np.array] of\n
111+
- **x_hat** -- estimated (smoothed) x
112+
- **dxdt_hat** -- estimated derivative of x
124113
"""
125-
n, window_size, smoothing_win = params
114+
if params != None: # Warning to support old interface for a while. Remove these lines along with params in a future release.
115+
warn("""`params` and `options` parameters will be removed in a future version. Use `polynomial_order`,
116+
`window_size`, and `smoothing_win` instead.""", DeprecationWarning)
117+
polynomial_order, window_size, smoothing_win = params
118+
elif polynomial_order == None or window_size == None or smoothing_win == None:
119+
raise ValueError("`polynomial_order`, `window_size`, and `smoothing_win` must be given.")
126120

127121
if window_size > len(x)-1:
128122
window_size = len(x)-1
129123

130124
if smoothing_win > len(x)-1:
131125
smoothing_win = len(x)-1
132126

133-
if window_size <= n:
134-
window_size = n + 1
127+
if window_size <= polynomial_order:
128+
window_size = polynomial_order + 1
135129

136130
if not window_size % 2: # then make odd
137131
window_size += 1
138132

139-
dxdt_hat = scipy.signal.savgol_filter(x, window_size, n, deriv=1) / dt
133+
dxdt_hat = scipy.signal.savgol_filter(x, window_size, polynomial_order, deriv=1) / dt
140134

141135
kernel = utility._gaussian_kernel(smoothing_win)
142136
dxdt_hat = utility.convolutional_smoother(dxdt_hat, kernel, 1)
@@ -306,18 +300,7 @@ def polydiff(x, dt, params=None, options=None, polynomial_order=None, window_siz
306300

307301
def __solve_for_A_and_C_given_X_and_Xdot__(X, Xdot, num_integrations, dt, gammaC=1e-1, gammaA=1e-6,
308302
solver='MOSEK', A_known=None, epsilon=1e-6, rows_of_interest='all'):
309-
"""
310-
:param X:
311-
:param Xdot:
312-
:param num_integrations:
313-
:param dt:
314-
:param gammaC:
315-
:param gammaA:
316-
:param solver:
317-
:param A_known:
318-
:param epsilon:
319-
:param rows_of_interest:
320-
:return:
303+
"""Given state and the derivative, find the system evolution and measurement matrices.
321304
"""
322305

323306
if rows_of_interest == 'all':
@@ -362,12 +345,8 @@ def __solve_for_A_and_C_given_X_and_Xdot__(X, Xdot, num_integrations, dt, gammaC
362345

363346

364347
def __integrate_dxdt_hat_matrix__(dxdt_hat, dt):
348+
"""Do integration analogous to integrate_dxdt_hat in the utilities, except on a 2D matrix.
365349
"""
366-
:param dxdt_hat:
367-
:param dt:
368-
:return:
369-
"""
370-
#assert isinstance(dxdt_hat, np.matrix)
371350
if len(dxdt_hat.shape) == 1:
372351
dxdt_hat = np.reshape(dxdt_hat, [1, len(dxdt_hat)])
373352
x = np.array(scipy.integrate.cumulative_trapezoid(dxdt_hat, axis=1))

0 commit comments

Comments
 (0)