-
Notifications
You must be signed in to change notification settings - Fork 8
Improving Spectral #53
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
Merged
Jacob-Stevens-Haas
merged 14 commits into
andgoldschmidt:master
from
pavelkomarov:chebyshev
Apr 2, 2025
Merged
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
957caaf
spitballing
pavelkomarov 859a87a
my interval was way wrong
pavelkomarov 6766e52
updated spectral-derivatives such that some of the checks I had in th…
pavelkomarov ae65554
added some road signs
pavelkomarov 2688f07
updated to spectral-derivatives 0.7
pavelkomarov cb59792
this test code encourages not-thorough testing
pavelkomarov 0226b3b
fixed caching test
pavelkomarov b1154da
added tests for chebyshev case and removed caching check for spectral…
pavelkomarov cb184a0
added Chebyshev examples
pavelkomarov 8bb4df7
removed print statement
pavelkomarov 18926e3
bumped version number
pavelkomarov d11d1a7
added axis param to tests so they pass now
pavelkomarov 22865d3
improved docstring to account for the fact filtering in chebyshev bas…
pavelkomarov 825e6dd
added back a newline
pavelkomarov 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
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
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
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 |
|---|---|---|
|
|
@@ -5,6 +5,8 @@ | |
| from derivative.differentiation import _gen_method | ||
|
|
||
|
|
||
| # Utilities for tests | ||
| # =================== | ||
| def default_args(kind): | ||
| """ The assumption is that the function will have dt = 1/100 over a range of 1 and not vary much. The goal is to | ||
| to set the parameters such that we obtain effective derivatives under these conditions. | ||
|
|
@@ -26,8 +28,7 @@ def default_args(kind): | |
| return {"sigma": 1, "lmbd": .01, "kernel": "gaussian"} | ||
| else: | ||
| raise ValueError('Unimplemented default args for kind {}.'.format(kind)) | ||
|
|
||
|
|
||
|
|
||
| class NumericalExperiment: | ||
| def __init__(self, fn, fn_str, t, kind, args): | ||
| self.fn = fn | ||
|
|
@@ -40,7 +41,6 @@ def __init__(self, fn, fn_str, t, kind, args): | |
| def run(self): | ||
| return dxdt(self.fn(self.t), self.t, self.kind, self.axis, **self.kwargs) | ||
|
|
||
|
|
||
| def compare(experiment, truth, rel_tol, abs_tol, shape_only=False): | ||
| """ Compare a numerical experiment to theoretical expectations. Issue warnings for derivative methods that fail, | ||
| use asserts for implementation requirements. | ||
|
|
@@ -60,8 +60,8 @@ def mean_sq(x): | |
| assert np.linalg.norm(residual, ord=np.inf) < max(abs_tol, np.linalg.norm(truth, ord=np.inf) * rel_tol) | ||
|
|
||
|
|
||
| # Check that numbers are returned | ||
| # =============================== | ||
| # Check that only numbers are returned | ||
| # ==================================== | ||
| @pytest.mark.parametrize("m", methods) | ||
| def test_notnan(m): | ||
| t = np.linspace(0, 1, 100) | ||
|
|
@@ -71,8 +71,8 @@ def test_notnan(m): | |
| assert not np.any(np.isnan(values)), message | ||
|
|
||
|
|
||
| # Test some basic functions | ||
| # ========================= | ||
| # Test that basic functions are differentiated correctly | ||
| # ====================================================== | ||
| funcs_and_derivs = ( | ||
|
Contributor
Author
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. This is a good library of examples, well thought out. |
||
| (lambda t: np.ones_like(t), "f(t) = 1", lambda t: np.zeros_like(t), "const1"), | ||
| (lambda t: np.zeros_like(t), "f(t) = 0", lambda t: np.zeros_like(t), "const0"), | ||
|
|
@@ -112,6 +112,8 @@ def test_fn(m, func_spec): | |
| compare(nexp, deriv(t), 1e-1, 1e-1, bad_combo) | ||
pavelkomarov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| # Test smoothing for those that do it | ||
| # =================================== | ||
| @pytest.mark.parametrize("kind", ("kalman", "trend_filtered")) | ||
| def test_smoothing_x(kind): | ||
| t = np.linspace(0, 1, 100) | ||
|
|
@@ -122,7 +124,6 @@ def test_smoothing_x(kind): | |
| # MSE | ||
| assert np.linalg.norm(x_est - np.sin(t)) ** 2 / len(t) < 1e-1 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("kind", ("kalman", "trend_filtered")) | ||
| def test_smoothing_functional(kind): | ||
| t = np.linspace(0, 1, 100) | ||
|
|
@@ -133,13 +134,14 @@ def test_smoothing_functional(kind): | |
| assert np.linalg.norm(x_est - np.sin(t)) ** 2 / len(t) < 1e-1 | ||
|
|
||
|
|
||
| # Test caching of the expensive _gen_method using a dummy | ||
| # ======================================================= | ||
| @pytest.fixture | ||
| def clean_gen_method_cache(): | ||
| _gen_method.cache_clear() | ||
| yield | ||
| _gen_method.cache_clear() | ||
|
|
||
|
|
||
| def test_gen_method_caching(clean_gen_method_cache): | ||
| x = np.ones(3) | ||
| t = np.arange(3) | ||
|
|
@@ -150,7 +152,6 @@ def test_gen_method_caching(clean_gen_method_cache): | |
| assert _gen_method.cache_info().currsize == 1 | ||
| assert id(expected) == id(result) | ||
|
|
||
|
|
||
| def test_gen_method_kwarg_caching(clean_gen_method_cache): | ||
| x = np.ones(3) | ||
| t = np.arange(3) | ||
|
|
@@ -164,6 +165,8 @@ def test_gen_method_kwarg_caching(clean_gen_method_cache): | |
| assert id(expected) != id(result) | ||
|
|
||
|
|
||
| # Test caching of the expensive private _global methods using a dummy | ||
| # =================================================================== | ||
| @pytest.fixture | ||
| def method_inst(request): | ||
| x = np.ones(3) | ||
|
|
@@ -173,8 +176,7 @@ def method_inst(request): | |
| yield x, t, method | ||
| method._global.cache_clear() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("method_inst", ["kalman", "trend_filtered"], indirect=True) | ||
| @pytest.mark.parametrize("method_inst", ["kalman", "trend_filtered", "spectral"], indirect=True) | ||
pavelkomarov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| def test_dglobal_caching(method_inst): | ||
| # make sure we're not recomputing expensive _global() method | ||
| x, t, method = method_inst | ||
|
|
@@ -184,8 +186,7 @@ def test_dglobal_caching(method_inst): | |
| assert method._global.cache_info().misses == 1 | ||
| assert method._global.cache_info().currsize == 1 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("method_inst", ["kalman", "trend_filtered"], indirect=True) | ||
| @pytest.mark.parametrize("method_inst", ["kalman", "trend_filtered", "spectral"], indirect=True) | ||
| def test_cached_global_order(method_inst): | ||
| x, t, method = method_inst | ||
| x = np.vstack((x, -x)) | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.