Skip to content

Commit a176c07

Browse files
committed
fixed jerk sliding tests
1 parent 7ec39c5 commit a176c07

1 file changed

Lines changed: 24 additions & 16 deletions

File tree

pynumdiff/tests/test_diff_methods.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111
# Function aliases for testing cases where parameters change the behavior in a big way
1212
def iterated_first_order(*args, **kwargs): return first_order(*args, **kwargs)
1313

14-
dt = 0.1
15-
t = np.arange(0, 3+dt, dt) # sample locations, including the endpoint
14+
t = np.linspace(0, 3, 31) # sample locations, including the endpoint
1615
tt = np.linspace(0, 3) # full domain, for visualizing denser plots
17-
ttt = np.linspace(0, 3, 101) # for testing jerk_sliding, which requires > 1001 points
16+
ttt = np.linspace(0, 3, 201) # for testing jerk_sliding, which requires > 1001 points
1817
np.random.seed(7) # for repeatability of the test, so we don't get random failures
1918
noise = 0.05*np.random.randn(*t.shape)
20-
19+
long_noise = 0.05*np.random.randn(*ttt.shape)
2120

2221
# Analytic (function, derivative) pairs on which to test differentiation methods.
2322
test_funcs_and_derivs = [
@@ -53,8 +52,8 @@ def iterated_first_order(*args, **kwargs): return first_order(*args, **kwargs)
5352
(acceleration, {'gamma':1}), (acceleration, [1]),
5453
(jerk, {'gamma':10}), (jerk, [10]),
5554
(iterative_velocity, {'num_iterations':5, 'gamma':0.05}), (iterative_velocity, [5, 0.05]),
56-
(smooth_acceleration, {'gamma':2, 'window_size':5}), (smooth_acceleration, [2, 5])
57-
# TODO (jerk_sliding), because with the test cases here (len < 1000) it would just be a duplicate of jerk
55+
(smooth_acceleration, {'gamma':2, 'window_size':5}), (smooth_acceleration, [2, 5]),
56+
(jerk_sliding, {'gamma':1e2}), (jerk_sliding, [1e2])
5857
]
5958

6059
# All the testing methodology follows the exact same pattern; the only thing that changes is the
@@ -186,7 +185,13 @@ def iterated_first_order(*args, **kwargs): return first_order(*args, **kwargs)
186185
[(-2, -2), (-1, -1), (-1, -1), (0, -1)],
187186
[(0, 0), (1, 0), (0, -1), (1, 0)],
188187
[(1, 1), (2, 2), (1, 1), (2, 2)],
189-
[(1, 1), (3, 3), (1, 1), (3, 3)]]
188+
[(1, 1), (3, 3), (1, 1), (3, 3)]],
189+
jerk_sliding: [[(-14, -15), (-14, -14), (0, -1), (1, 0)],
190+
[(-4, -4), (-3, -3), (0, -1), (1, 0)],
191+
[(-4, -4), (-3, -3), (0, -1), (1, 0)],
192+
[(-3, -4), (-1, -2), (0, -1), (1, 0)],
193+
[(0, 0), (2, 1), (0, 0), (2, 1)],
194+
[(1, 1), (3, 3), (1, 1), (3, 3)]]
190195
}
191196

192197
# Essentially run the cartesian product of [diff methods] x [test functions] through this one test
@@ -203,15 +208,18 @@ def test_diff_method(diff_method_and_params, test_func_and_deriv, request): # re
203208
if diff_method in [lineardiff, velocity, acceleration, jerk, smooth_acceleration]:
204209
try: import cvxpy
205210
except: warn(f"Cannot import cvxpy, skipping {diff_method} test."); return
206-
if diff_method == jerk_sliding:
207-
t = ttt
208-
dt = 0.03
209-
noise = 0.05*np.random.randn(*t.shape)
210211

211212
# sample the true function and make noisy samples, and sample true derivative
212-
x = f(t)
213-
x_noisy = x + noise
214-
dxdt = df(t)
213+
if diff_method != jerk_sliding:
214+
x = f(t)
215+
x_noisy = x + noise
216+
dxdt = df(t)
217+
dt = t[1] - t[0]
218+
else: # different density for jerk_sliding
219+
x = f(ttt)
220+
x_noisy = x + long_noise
221+
dxdt = df(ttt)
222+
dt = ttt[1] - ttt[0]
215223

216224
# differentiate without and with noise, accounting for new and old styles of calling functions
217225
x_hat, dxdt_hat = diff_method(x, dt, **params) if isinstance(params, dict) \
@@ -249,8 +257,8 @@ def test_diff_method(diff_method_and_params, test_func_and_deriv, request): # re
249257

250258
# bounds-printing for establishing bounds
251259
if request.config.getoption("--bounds"):
252-
print(f"({l2_error},{linf_error})", end=", ")
253-
#print(f"({int(np.ceil(np.log10(l2_error))) if l2_error > 0 else -25}, {int(np.ceil(np.log10(linf_error))) if linf_error > 0 else -25})", end=", ")
260+
#print(f"({l2_error},{linf_error})", end=", ")
261+
print(f"({int(np.ceil(np.log10(l2_error))) if l2_error > 0 else -25}, {int(np.ceil(np.log10(linf_error))) if linf_error > 0 else -25})", end=", ")
254262
# bounds checking
255263
else:
256264
log_l2_bound, log_linf_bound = error_bounds[diff_method][i][j]

0 commit comments

Comments
 (0)