Skip to content

Commit

Permalink
Issue #46/#44 add basic test for whittaker_f
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Apr 14, 2022
1 parent ee7f512 commit bf8342b
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tests/test_whittaker.py
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)

0 comments on commit bf8342b

Please sign in to comment.