Skip to content

Commit 933f65f

Browse files
committed
Merge branch 'master' of github.com:florisvb/PyNumDiff into fix-first-value
2 parents 0aad7da + b48d133 commit 933f65f

2 files changed

Lines changed: 58 additions & 58 deletions

File tree

pynumdiff/linear_model/_linear_model.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -403,16 +403,16 @@ def _lineardiff(x, dt, N, gamma, solver='MOSEK', weights=None):
403403
return x_hat, dxdt_hat
404404

405405

406-
def lineardiff(x, dt, params=None, options=None, polynomial_order=None, gamma=None, window_size=None,
406+
def lineardiff(x, dt, params=None, options=None, order=None, gamma=None, window_size=None,
407407
sliding=True, step_size=10, kernel='friedrichs', solver='MOSEK'):
408408
"""Slide a smoothing derivative function across a time series with specified window size.
409409
410410
:param np.array[float] x: array of time series to differentiate
411411
:param float dt: time step size
412-
:param list[int, float, int] params: (**deprecated**, prefer :code:`polynomial_order`, :code:`gamma`, and :code:`window_size`)
412+
:param list[int, float, int] params: (**deprecated**, prefer :code:`order`, :code:`gamma`, and :code:`window_size`)
413413
:param dict options: (**deprecated**, prefer :code:`sliding`, :code:`step_size`, :code:`kernel`, and :code:`solver`
414414
a dictionary consisting of {'sliding': (bool), 'step_size': (int), 'kernel_name': (str), 'solver': (str)}
415-
:param int polynomial_order: order of the polynomial
415+
:param int>1 order: order of the polynomial
416416
:param float gamma: regularization term
417417
:param int window_size: size of the sliding window (ignored if not sliding)
418418
:param bool sliding: whether to use sliding approach
@@ -425,22 +425,22 @@ def lineardiff(x, dt, params=None, options=None, polynomial_order=None, gamma=No
425425
- **dxdt_hat** -- estimated derivative of x
426426
"""
427427
if params != None:
428-
warn("""`params` and `options` parameters will be removed in a future version. Use `polynomial_order`,
428+
warn("""`params` and `options` parameters will be removed in a future version. Use `order`,
429429
`gamma`, and `window_size` instead.""", DeprecationWarning)
430-
polynomial_order, gamma, window_size = params
430+
order, gamma, window_size = params
431431
if options != None:
432432
if 'sliding' in options: sliding = options['sliding']
433433
if 'step_size' in options: step_size = options['step_size']
434434
if 'kernel_name' in options: kernel = options['kernel_name']
435435
if 'solver' in options: solver = options['solver']
436-
elif polynomial_order == None or gamma == None or window_size == None:
437-
raise ValueError("`polynomial_order`, `gamma`, and `window_size` must be given.")
436+
elif order == None or gamma == None or window_size == None:
437+
raise ValueError("`order`, `gamma`, and `window_size` must be given.")
438438

439439
if sliding:
440440
# forward and backward
441-
x_hat_forward, _ = _slide_function(_lineardiff, x, dt, [polynomial_order, gamma, solver], window_size, step_size,
441+
x_hat_forward, _ = _slide_function(_lineardiff, x, dt, [order, gamma, solver], window_size, step_size,
442442
kernel)
443-
x_hat_backward, _ = _slide_function(_lineardiff, x[::-1], dt, [polynomial_order, gamma, solver], window_size, step_size,
443+
x_hat_backward, _ = _slide_function(_lineardiff, x[::-1], dt, [order, gamma, solver], window_size, step_size,
444444
kernel)
445445

446446
# weights

pynumdiff/tests/test_diff_methods.py

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,69 +15,69 @@
1515
noise = 0.05*np.random.randn(*t.shape)
1616

1717
diff_methods_and_params = [
18-
#(lineardiff, {'polynomial_order':3, 'gamma':5, 'window_size':10, 'solver':'CVXOPT'}),
19-
(polydiff, {'polynomial_order':2, 'window_size':3}),
20-
(savgoldiff, {'polynomial_order':2, 'window_size':4, 'smoothing_win':4}),
21-
(spectraldiff, {'high_freq_cutoff':0.1})
22-
]
18+
#(lineardiff, {'order':3, 'gamma':5, 'window_size':10, 'solver':'CVXOPT'}),
19+
(polydiff, {'polynomial_order':2, 'window_size':3}),
20+
(savgoldiff, {'polynomial_order':2, 'window_size':4, 'smoothing_win':4}),
21+
(spectraldiff, {'high_freq_cutoff':0.1})
22+
]
2323

2424
# Analytic (function, derivative) pairs on which to test differentiation methods.
2525
test_funcs_and_derivs = [
26-
(0, lambda t: np.ones(t.shape), lambda t: np.zeros(t.shape)), # x(t) = 1
27-
(1, lambda t: t, lambda t: np.ones(t.shape)), # x(t) = t
28-
(2, lambda t: 2*t + 1, lambda t: 2*np.ones(t.shape)), # x(t) = 2t+1
29-
(3, lambda t: t**2 - t + 1, lambda t: 2*t - 1), # x(t) = t^2 - t + 1
30-
(4, lambda t: np.sin(t) + 1/2, lambda t: np.cos(t))] # x(t) = sin(t) + 1/2
26+
(0, lambda t: np.ones(t.shape), lambda t: np.zeros(t.shape)), # x(t) = 1
27+
(1, lambda t: t, lambda t: np.ones(t.shape)), # x(t) = t
28+
(2, lambda t: 2*t + 1, lambda t: 2*np.ones(t.shape)), # x(t) = 2t+1
29+
(3, lambda t: t**2 - t + 1, lambda t: 2*t - 1), # x(t) = t^2 - t + 1
30+
(4, lambda t: np.sin(t) + 1/2, lambda t: np.cos(t))] # x(t) = sin(t) + 1/2
3131

3232
# All the testing methodology follows the exact same pattern; the only thing that changes is the
3333
# closeness to the right answer various methods achieve with the given parameterizations. So index a
3434
# big ol' table by the method, then the test function, then the pair of quantities we're comparing.
3535
error_bounds = {
36-
lineardiff: [[(1e-25, 1e-25)]*4]*len(test_funcs_and_derivs),
37-
polydiff: [[(1e-14, 1e-15), (1e-12, 1e-13), (1, 0.1), (100, 100)],
38-
[(1e-13, 1e-14), (1e-12, 1e-13), (1, 0.1), (100, 100)],
39-
[(1e-13, 1e-14), (1e-11, 1e-12), (1, 0.1), (100, 100)],
40-
[(1e-13, 1e-14), (1e-12, 1e-12), (1, 0.1), (100, 100)],
41-
[(1e-6, 1e-7), (0.001, 0.0001), (1, 0.1), (100, 100)]],
42-
savgoldiff: [[(1e-7, 1e-8), (1e-12, 1e-13), (1, 0.1), (100, 10)],
43-
[(1e-5, 1e-7), (1e-12, 1e-13), (1, 0.1), (100, 10)],
44-
[(1e-7, 1e-8), (1e-11, 1e-12), (1, 0.1), (100, 10)],
45-
[(0.1, 0.01), (0.1, 0.01), (1, 0.1), (100, 10)],
46-
[(0.01, 1e-3), (0.01, 1e-3), (1, 0.1), (100, 10)]],
47-
spectraldiff: [[(1e-7, 1e-8), ( 1e-25 , 1e-25), (1, 0.1), (100, 10)],
48-
[(0.1, 0.1), (10, 10), (1, 0.1), (100, 10)],
49-
[(0.1, 0.1), (10, 10), (1, 0.1), (100, 10)],
50-
[(1, 1), (100, 10), (1, 1), (100, 10)],
51-
[(0.1, 0.1), (10, 10), (1, 0.1), (100, 10)]]
36+
lineardiff: [[(1e-25, 1e-25)]*4]*len(test_funcs_and_derivs),
37+
polydiff: [[(1e-14, 1e-15), (1e-12, 1e-13), (1, 0.1), (100, 100)],
38+
[(1e-13, 1e-14), (1e-12, 1e-13), (1, 0.1), (100, 100)],
39+
[(1e-13, 1e-14), (1e-11, 1e-12), (1, 0.1), (100, 100)],
40+
[(1e-13, 1e-14), (1e-12, 1e-12), (1, 0.1), (100, 100)],
41+
[(1e-6, 1e-7), (0.001, 0.0001), (1, 0.1), (100, 100)]],
42+
savgoldiff: [[(1e-7, 1e-8), (1e-12, 1e-13), (1, 0.1), (100, 10)],
43+
[(1e-5, 1e-7), (1e-12, 1e-13), (1, 0.1), (100, 10)],
44+
[(1e-7, 1e-8), (1e-11, 1e-12), (1, 0.1), (100, 10)],
45+
[(0.1, 0.01), (0.1, 0.01), (1, 0.1), (100, 10)],
46+
[(0.01, 1e-3), (0.01, 1e-3), (1, 0.1), (100, 10)]],
47+
spectraldiff: [[(1e-7, 1e-8), ( 1e-25 , 1e-25), (1, 0.1), (100, 10)],
48+
[(0.1, 0.1), (10, 10), (1, 0.1), (100, 10)],
49+
[(0.1, 0.1), (10, 10), (1, 0.1), (100, 10)],
50+
[(1, 1), (100, 10), (1, 1), (100, 10)],
51+
[(0.1, 0.1), (10, 10), (1, 0.1), (100, 10)]]
5252
}
5353

5454

5555
@mark.parametrize("diff_method_and_params", diff_methods_and_params)
5656
@mark.parametrize("test_func_and_deriv", test_funcs_and_derivs)
5757
def test_diff_method(diff_method_and_params, test_func_and_deriv):
58-
diff_method, params = diff_method_and_params # unpack
59-
i, f, df = test_func_and_deriv
58+
diff_method, params = diff_method_and_params # unpack
59+
i, f, df = test_func_and_deriv
6060

61-
# some methods rely on cvxpy, and we'd like to allow use of pynumdiff without convex optimization
62-
if diff_method in [lineardiff, velocity]:
63-
try: import cvxpy
64-
except: warn(f"Cannot import cvxpy, skipping {diff_method} test."); return
61+
# some methods rely on cvxpy, and we'd like to allow use of pynumdiff without convex optimization
62+
if diff_method in [lineardiff, velocity]:
63+
try: import cvxpy
64+
except: warn(f"Cannot import cvxpy, skipping {diff_method} test."); return
6565

66-
x = f(t) # sample the function
67-
x_noisy = x + noise # add a little noise
68-
dxdt = df(t) # true values of the derivative
66+
x = f(t) # sample the function
67+
x_noisy = x + noise # add a little noise
68+
dxdt = df(t) # true values of the derivative
6969

70-
# differentiate without and with noise
71-
x_hat, dxdt_hat = diff_method(x, dt, **params) if isinstance(params, dict) else diff_method(x, dt, params)
72-
x_hat_noisy, dxdt_hat_noisy = diff_method(x_noisy, dt, **params) if isinstance(params, dict) else diff_method(x_noisy, dt, params)
73-
74-
# check x_hat and x_hat_noisy are close to x and dxdt_hat and dxdt_hat_noisy are close to dxdt
75-
#print("]\n[", end="")
76-
for j,(a,b) in enumerate([(x,x_hat), (dxdt,dxdt_hat), (x,x_hat_noisy), (dxdt,dxdt_hat_noisy)]):
77-
l2_error = np.linalg.norm(a - b)
78-
linf_error = np.max(np.abs(a - b))
79-
80-
#print(f"({10 ** np.ceil(np.log10(l2_error)) if l2_error> 0 else 1e-25}, {10 ** np.ceil(np.log10(linf_error)) if linf_error > 0 else 1e-25})", end=", ")
81-
l2_bound, linf_bound = error_bounds[diff_method][i][j]
82-
assert np.linalg.norm(a - b) < l2_bound
83-
assert np.max(np.abs(a - b)) < linf_bound
70+
# differentiate without and with noise
71+
x_hat, dxdt_hat = diff_method(x, dt, **params) if isinstance(params, dict) else diff_method(x, dt, params)
72+
x_hat_noisy, dxdt_hat_noisy = diff_method(x_noisy, dt, **params) if isinstance(params, dict) else diff_method(x_noisy, dt, params)
73+
74+
# check x_hat and x_hat_noisy are close to x and dxdt_hat and dxdt_hat_noisy are close to dxdt
75+
#print("]\n[", end="")
76+
for j,(a,b) in enumerate([(x,x_hat), (dxdt,dxdt_hat), (x,x_hat_noisy), (dxdt,dxdt_hat_noisy)]):
77+
l2_error = np.linalg.norm(a - b)
78+
linf_error = np.max(np.abs(a - b))
79+
80+
#print(f"({10 ** np.ceil(np.log10(l2_error)) if l2_error> 0 else 1e-25}, {10 ** np.ceil(np.log10(linf_error)) if linf_error > 0 else 1e-25})", end=", ")
81+
l2_bound, linf_bound = error_bounds[diff_method][i][j]
82+
assert np.linalg.norm(a - b) < l2_bound
83+
assert np.max(np.abs(a - b)) < linf_bound

0 commit comments

Comments
 (0)