-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from datetime import datetime, timedelta | ||
|
||
import numpy as np | ||
import numpy.testing | ||
|
||
from fusets.whittaker import whittaker_f | ||
|
||
|
||
def test_whittaker_f(): | ||
def generate_data(xs: np.array): | ||
"""Generate test data from array of ints (day offsets)""" | ||
ts = [datetime(2022, 1, 1) + timedelta(days=int(x)) for x in xs] | ||
ys = np.cos(0.35 * xs) | ||
return ts, ys | ||
|
||
n = 32 | ||
# Input: unevenly spaced timestamps and missing data | ||
xs = np.array([(x + x // 3) for x in range(n)], dtype="int") | ||
ts, ys = generate_data(xs) | ||
ys_with_nan = ys.copy() | ||
ys_with_nan[xs % 5 >= 2] = np.nan | ||
assert 0.25 < np.isnan(ys_with_nan).mean() < 0.75 | ||
|
||
# Smooth | ||
ys_smooth, ts_smooth, ys_smooth_sampled, ts_smooth_sampled = whittaker_f(x=ts, y=ys, lmbd=1, d=4) | ||
|
||
xs_expected = np.arange(42) | ||
ts_expected, ys_expected = generate_data(xs_expected) | ||
assert ts_smooth == ts_expected | ||
assert np.isnan(ys_smooth).sum() == 0 | ||
numpy.testing.assert_allclose(ys_smooth, ys_expected, atol=0.1) | ||
|
||
xs_expected = np.arange(42)[::4] | ||
ts_expected, ys_expected = generate_data(xs_expected) | ||
assert ts_smooth_sampled == ts_expected | ||
assert np.isnan(ys_smooth_sampled).sum() == 0 | ||
numpy.testing.assert_allclose(ys_smooth_sampled, ys_expected, atol=0.1) |