Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pynumdiff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from warnings import warn
warn("tvrdiff, robustdiff, and lineardiff not available due to lack of convex solver. To use those, install CVXPY.")
else: # executes if try is successful
from .total_variation_regularization import tvrdiff, velocity, acceleration, jerk, smooth_acceleration, jerk_sliding
from .total_variation_regularization import tvrdiff, velocity, acceleration, jerk, smooth_acceleration
from .kalman_smooth import robustdiff, convex_smooth
from .linear_model import lineardiff

Expand Down
3 changes: 1 addition & 2 deletions pynumdiff/optimize/_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ..smooth_finite_difference import kerneldiff, mediandiff, meandiff, gaussiandiff, friedrichsdiff, butterdiff
from ..polynomial_fit import polydiff, savgoldiff, splinediff
from ..basis_fit import spectraldiff, rbfdiff
from ..total_variation_regularization import tvrdiff, velocity, acceleration, jerk, iterative_velocity, smooth_acceleration, jerk_sliding
from ..total_variation_regularization import tvrdiff, velocity, acceleration, jerk, iterative_velocity, smooth_acceleration
from ..kalman_smooth import rtsdiff, constant_velocity, constant_acceleration, constant_jerk, robustdiff
from ..linear_model import lineardiff

Expand Down Expand Up @@ -110,7 +110,6 @@
method_params_and_bounds[method] = method_params_and_bounds[meandiff]
for method in [acceleration, jerk]: # Deprecated, redundant methods
method_params_and_bounds[method] = method_params_and_bounds[velocity]
method_params_and_bounds[jerk_sliding] = method_params_and_bounds[smooth_acceleration]
for method in [constant_acceleration, constant_jerk]: # Deprecated, redundant methods
method_params_and_bounds[method] = method_params_and_bounds[constant_velocity]

Expand Down
9 changes: 1 addition & 8 deletions pynumdiff/tests/test_diff_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from ..linear_model import lineardiff
from ..basis_fit import spectraldiff, rbfdiff
from ..polynomial_fit import polydiff, savgoldiff, splinediff
from ..total_variation_regularization import velocity, acceleration, jerk, iterative_velocity, smooth_acceleration, jerk_sliding
from ..total_variation_regularization import velocity, acceleration, jerk, iterative_velocity, smooth_acceleration
from ..kalman_smooth import rtsdiff, constant_velocity, constant_acceleration, constant_jerk, robustdiff
from ..smooth_finite_difference import mediandiff, meandiff, gaussiandiff, friedrichsdiff, butterdiff
# Function aliases for testing cases where parameters change the behavior in a big way, so error limits can be indexed in dict
Expand Down Expand Up @@ -57,7 +57,6 @@ def spline_irreg_step(*args, **kwargs): return splinediff(*args, **kwargs)
(jerk, {'gamma':10}), (jerk, [10]),
(iterative_velocity, {'num_iterations':5, 'gamma':0.05}), (iterative_velocity, [5, 0.05]),
(smooth_acceleration, {'gamma':2, 'window_size':5}), (smooth_acceleration, [2, 5]),
(jerk_sliding, {'gamma':1, 'window_size':15}), (jerk_sliding, [1], {'window_size':15}),
(lineardiff, {'order':3, 'gamma':5, 'window_size':11, 'solver':'CLARABEL'}), (lineardiff, [3, 5, 11], {'solver':'CLARABEL'})
]

Expand Down Expand Up @@ -193,12 +192,6 @@ def spline_irreg_step(*args, **kwargs): return splinediff(*args, **kwargs)
[(0, 0), (1, 0), (0, -1), (1, 0)],
[(1, 1), (2, 2), (1, 1), (2, 2)],
[(1, 1), (3, 3), (1, 1), (3, 3)]],
jerk_sliding: [[(-25, -25), (-16, -17), (0, -1), (1, 0)],
[(-14, -14), (-14, -14), (0, -1), (0, 0)],
[(-14, -14), (-14, -14), (0, -1), (0, 0)],
[(-1, -1), (0, 0), (0, -1), (0, 0)],
[(1, 0), (2, 2), (1, 0), (2, 2)],
[(1, 1), (3, 3), (1, 1), (3, 3)]],
constant_velocity: [[(-25, -25), (-25, -25), (0, -1), (1, 1)],
[(-4, -5), (-3, -3), (0, -1), (1, 1)],
[(-3, -3), (0, 0), (0, -1), (1, 1)],
Expand Down
2 changes: 1 addition & 1 deletion pynumdiff/total_variation_regularization/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""This module implements some common total variation regularization methods
"""
from ._total_variation_regularization import tvrdiff, velocity, acceleration, jerk, jerk_sliding, smooth_acceleration, iterative_velocity
from ._total_variation_regularization import tvrdiff, velocity, acceleration, jerk, smooth_acceleration, iterative_velocity
Original file line number Diff line number Diff line change
Expand Up @@ -225,41 +225,3 @@ def smooth_acceleration(x, dt, params=None, options=None, gamma=None, window_siz
x_hat = x_hat + x0

return x_hat, dxdt_hat


def jerk_sliding(x, dt, params=None, options=None, gamma=None, solver=None, window_size=101):
"""Use convex optimization (cvxpy) to solve for the jerk total variation regularized derivative in a
sliding window.

:param np.array[float] x: data to differentiate
:param float dt: step size
:param params: (**deprecated**, prefer :code:`gamma`)
:param dict options: (**deprecated**, prefer :code:`solver`) a dictionary consisting of {'solver': (str)}
:param float gamma: the regularization parameter
:param str solver: the solver CVXPY should use, 'MOSEK', 'CVXOPT', 'CLARABEL', 'ECOS', etc.
In testing, 'MOSEK' was the most robust. If not given, fall back to CVXPY's default.
:param int window_size: how wide to make the kernel

:return: - **x_hat** (np.array) -- estimated (smoothed) x
- **dxdt_hat** (np.array) -- estimated derivative of x
"""
if params != None: # Warning to support old interface for a while. Remove these lines along with params in a future release.
warn("`params` and `options` parameters will be removed in a future version. Use `gamma` " +
"and `solver` instead.", DeprecationWarning)
gamma = params[0] if isinstance(params, list) else params
if options != None:
if 'solver' in options: solver = options['solver']
if 'window_size' in options: window_size = options['window_size']
elif gamma == None:
raise ValueError("`gamma` must be given.")

if len(x) < window_size or window_size < 15:
warn("len(x) should be > window_size >= 15, calling standard jerk() without sliding")
return tvrdiff(x, dt, 3, gamma, solver=solver)

if window_size % 2 == 0:
window_size += 1 # has to be odd
warn("Kernel window size should be odd. Added 1 to length.")
ramp = window_size//5
kernel = np.hstack((np.arange(1, ramp+1)/ramp, np.ones(window_size - 2*ramp), np.arange(ramp, 0, -1)/ramp))
return utility.slide_function(tvrdiff, x, dt, kernel, 3, gamma, stride=ramp, solver=solver)