-
Notifications
You must be signed in to change notification settings - Fork 24
I added multidimensionality functions to the tvrdiff method by callin… #188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mariaprot
wants to merge
1
commit into
florisvb:master
Choose a base branch
from
mariaprot:tvrdiff
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,7 +52,49 @@ def iterative_velocity(x, dt, params=None, options=None, num_iterations=None, ga | |
|
|
||
| return x_hat, dxdt_hat | ||
|
|
||
| #N-d case: | ||
| def tvrdiff(x, dt, order, gamma, huberM=float('inf'), solver=None, axis=0): | ||
| """ | ||
| Generalized total variation regularized derivatives (cvxpy). Supports multidimensionality by differentiating along | ||
| 'axis', independently for each vector obtained by fixing all other indices. | ||
|
|
||
| :param np.array[float] x: data to differentiate | ||
| :param float dt: step size | ||
| :param int order: 1, 2, or 3, the derivative to regularize | ||
| :param float gamma: regularization parameter | ||
| :param float huberM: Huber loss parameter, in units of scaled median absolute deviation of input data. | ||
| :math:`M = \\infty` reduces to :math:`\\ell_2` loss squared on first, fidelity cost term, and | ||
| :math:`M = 0` reduces to :math:`\\ell_1` loss, which seeks sparse residuals. | ||
| :param str solver: Solver to use. Solver options include: 'MOSEK', 'CVXOPT', 'CLARABEL', 'ECOS'. | ||
| If not given, fall back to CVXPY's default. | ||
|
|
||
| :return: - **x_hat** (np.array) -- estimated (smoothed) x | ||
| - **dxdt_hat** (np.array) -- estimated derivative of x | ||
| """ | ||
|
|
||
| x0 = np.moveaxis(x, axis, 0) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of |
||
|
|
||
| # end quick if it's just 1d case | ||
| if x0.ndim == 1: | ||
| x_hat0, dxdt0 = tvrdiff(x0, dt, order, gamma, huberM, solver) | ||
| return x_hat0, dxdt0 | ||
|
|
||
| x_hat0 = np.empty_like(x0, dtype=float) | ||
| dxdt0 = np.empty_like(x0, dtype=float) | ||
| rest = x0.shape[1:] | ||
| print(rest) | ||
|
|
||
| # had to loop in python:( | ||
| for i in np.ndindex(rest): | ||
| slice = (slice(None),) + i | ||
| x_hat0[slice], dxdt0[slice] = tvrdiff(x0[slice], dt, order, gamma, huberM, solver) | ||
|
|
||
| x_hat = np.moveaxis(x_hat0, 0, axis) | ||
| dxdt_hat = np.moveaxis(dxdt0, 0, axis) | ||
|
|
||
| return x_hat, dxdt_hat | ||
|
|
||
| # 1-d case: | ||
| def tvrdiff(x, dt, order, gamma, huberM=float('inf'), solver=None): | ||
| """Generalized total variation regularized derivatives. Use convex optimization (cvxpy) to solve for a | ||
| total variation regularized derivative. Other convex-solver-based methods in this module call this function. | ||
|
|
@@ -70,6 +112,7 @@ def tvrdiff(x, dt, order, gamma, huberM=float('inf'), solver=None): | |
| :return: - **x_hat** (np.array) -- estimated (smoothed) x | ||
| - **dxdt_hat** (np.array) -- estimated derivative of x | ||
| """ | ||
|
|
||
| # Normalize for numerical consistency with convex solver | ||
| mu = np.mean(x) | ||
| sigma = median_abs_deviation(x, scale='normal') # robust alternative to std() | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep only one public-facing function. If there needs to be a 1d function, you can indent it and put it inside the outer one.