Exemple #1
0
def test_update():
    p1 = Points([(0, 1), (1, 1)])
    p2 = Points([(2, 2), (4, 2)])

    f = Aggregate([p1, p2], tfm=lambda x, ys: sum(ys), union=False)
    assert f.domain.is_empty
    assert f(0) is None
    assert f(1.5) is None
    assert f(3) is None

    p1.append((2, 1))
    assert f.domain.start == 2
    assert f.domain.end == 2
    assert not f.domain.start_open
    assert not f.domain.end_open
    assert f(2) == 3
    assert f.is_updating is False

    p1.append((3, 1))
    assert f.domain.start == 2
    assert f.domain.end == 3
    assert not f.domain.start_open
    assert not f.domain.end_open
    assert f(2) == 3
    assert f(3) == 3
    assert f.is_updating is False

    p1.append((4, 1))
    assert f.domain.start == 2
    assert f.domain.end == 4
    assert not f.domain.start_open
    assert not f.domain.end_open
    assert f(4) == 3
    assert f.is_updating is False
Exemple #2
0
def test_multiple_nested_update():
    p1 = Points([(0, 1), (1, 1)])
    p2 = Points([(2, 2), (4, 2)])

    f = (p1.offset(10) * 2 + p2.offset(10) * 2) / 2
    assert f.domain.is_empty
    assert f(10) is None
    assert f(11.5) is None
    assert f(13) is None

    p1.append((2, 1))
    assert f.is_updating is False
    assert f.domain.start == 12
    assert f.domain.end == 12
    assert not f.domain.start_open
    assert not f.domain.end_open
    assert f(12) == 3

    p1.append((3, 1))
    assert f.is_updating is False
    assert f.domain.start == 12
    assert f.domain.end == 13
    assert not f.domain.start_open
    assert not f.domain.end_open
    assert f(12) == 3
    assert f(13) == 3
Exemple #3
0
def test_step():
    p1 = Points([(0, 1), (1, 1), (2, 1), (3, 1), (4, 1)])
    p2 = Points([(0, 2), (2, 2), (4, 2)])
    f = Aggregate([p1, p2], tfm=lambda x, ys: sum(ys))

    c = 100
    for i in range(c):
        assert f(i / float(c) * f.domain.length) == 3
        assert ((p2 + p1) - p2)(i / float(c) * f.domain.length) == 1

    assert f.x_next(-0.5) == 0
    assert f.x_next(0) == 1
    assert f.x_next(0.5) == 1
    assert f.x_next(1) == 2
    assert f.x_next(1.5) == 2
    assert f.x_next(3.5) == 4
    assert f.x_next(4) is None

    assert f.x_previous(0) is None
    assert f.x_previous(0.5) == 0
    assert f.x_previous(1) == 0
    assert f.x_previous(1.5) == 1
    assert f.x_previous(3.5) == 3
    assert f.x_previous(4) == 3
    assert f.x_previous(4.5) == 4
Exemple #4
0
def test_ema_alpha():
    f = Points(test_util.point_gen([1, 2, 1, 2])).ema(0.5, is_period=False)
    assert f(0) == 1
    assert f(1) == approx(1.5, abs=0.01)
    assert f(2) == approx(1.25, abs=0.01)
    assert f(3) == approx((1.25 + 2.0) * 0.5, abs=0.01)
    assert np.allclose(f.sample_points(), [(0, 1), (1, 1.5), (2, 1.25), (3, (1.25 + 2.0) * 0.5)])
    assert np.allclose(f.sample_points(step=2), [(0, 1), (2, 1.25)])

    f = Points(test_util.point_gen([1, 2, 1, 2])).ema(0.25, is_period=False)
    assert f(0) == 1
    assert f(1) == approx(0.75 + 2 * 0.25, abs=0.01)
    assert f(2) == approx((0.75 + 2 * 0.25) * 0.75 + 1 * 0.25, abs=0.01)
    assert f(3) == approx(((0.75 + 2 * 0.25) * 0.75 + 1 * 0.25) * 0.75 + 2 * 0.25, abs=0.01)
Exemple #5
0
def test_update_ref_func():
    ps = Points(test_util.point_gen([1, 3, 1.5, 3.5, 1]))
    ref_f = Constant(2)
    e = Extremas(ps, ref_f, min_step=1)
    assert np.allclose(e.sample_points(), [(0, 1), (1, 3), (2, 1.5), (3, 3.5)])
    ref_f.value = 1.25
    assert np.allclose(e.sample_points(), [(0, 1), (3, 3.5)])
Exemple #6
0
def test_update():
    points = Points(test_util.point_gen([3, 1, 2]))
    f = points.extension('tangent', start=True, end=True)
    assert f(2) == 2
    assert f(3) == 3
    points.append((3, 2))
    assert f(3) == 2
Exemple #7
0
def test_max():
    f = Curve.max([Points([(0, -1), (1, 0), (2, 1)]), 0])
    assert f.domain == Interval.closed(0, 2)
    assert f.y(-1) is None
    assert f.y(0) == 0
    assert f.y(1) == 0
    assert f.y(2) == 1
    assert f.y(3) is None

    f = Curve.max([0, Points([(0, -1), (1, 0), (2, 1)])])
    assert f.domain == Interval.closed(0, 2)
    assert f.y(-1) is None
    assert f.y(0) == 0
    assert f.y(1) == 0
    assert f.y(2) == 1
    assert f.y(3) is None
Exemple #8
0
def test_update():
    ps = Points([(0, 1), (1, 2)])
    assert ps.is_updating is False
    assert ps(2) is None
    ps.append((2, 3))
    assert ps.is_updating is False
    assert ps(2) == 3
Exemple #9
0
def test_offset_with_duration():
    points = Points(test_util.point_gen([1, 2, 3]))

    f = points.offset(1, duration='1s')
    assert f.domain.start == 1
    assert f.domain.end == 3
    assert np.allclose(f.sample_points(), [(1, 1), (2, 2), (3, 3)])
    assert f(0.5) is None
    assert f(1.4) == 1.4
    assert f(2.5) == 2.5
    assert f(3.5) is None
    assert f.x_next(0.5) == 1
    assert f.x_next(1.5) == 2
    assert f.x_next(2.5) == 3
    assert f.x_next(3.5) is None
    assert f.x_previous(0.5) is None
    assert f.x_previous(1.5) == 1
    assert f.x_previous(2.5) == 2
    assert f.x_previous(3.5) == 3

    f = points.offset(10, duration='1s')
    assert f.domain.start == 10
    assert f.domain.end == 12
    assert np.allclose(f.sample_points(), [(10, 1), (11, 2), (12, 3)])
    assert f(10.5) == 1.5
    assert f.x_next(9.5) == 10
    assert f.x_next(10.5) == 11
    assert f.x_next(11.5) == 12
    assert f.x_next(12.5) is None
    assert f.x_previous(9.5) is None
    assert f.x_previous(10.5) == 10
    assert f.x_previous(11.5) == 11
    assert f.x_previous(12.5) == 12
Exemple #10
0
def test_offset_none():
    points = Points(test_util.point_gen([None, 1, 2]))

    f = points.offset(10)
    assert f.domain.start == 10
    assert f.domain.end == 12
    assert np.array_equal(f.sample_points(), [(10, None), (11, 1), (12, 2)])
Exemple #11
0
def test_offset_update():
    begin_update_count = 0
    begin_update_interval = None

    def begin_update(domain):
        nonlocal begin_update_count
        nonlocal begin_update_interval
        begin_update_count += 1
        begin_update_interval = domain

    end_update_count = 0
    end_update_interval = None

    def end_update(domain):
        nonlocal end_update_count
        nonlocal end_update_interval
        end_update_count += 1
        end_update_interval = domain

    points = Points(test_util.point_gen([1, 2, 3]))
    f = points.offset(10)
    f.add_observer(begin=begin_update, end=end_update)

    points.append((3, 4))
    assert f(13) == 4
    assert begin_update_count == 1
    assert begin_update_interval.start == 12
    assert begin_update_interval.end == 13
    assert end_update_count == 1
    assert end_update_interval.start == 12
    assert end_update_interval.end == 13
Exemple #12
0
def test_none_union():
    p1 = Points([(0, None), (1, None)])
    f = Aggregate([p1], tfm=lambda x, ys: ys[0], union=True)
    assert f.domain.start == 0
    assert f.domain.end == 1
    assert f(0) is None
    assert f(1) is None
Exemple #13
0
def test_reset_start():
    f = Points(test_util.point_gen([1, 2, 3, 4, 5, 6, 7, 8])).sma(3, is_period=True)
    assert f(6) == 6
    assert len(f.accumulated_points._points) == 5
    assert f(3) == 3
    assert len(f.accumulated_points._points) == 4
    assert f(6) == 6
    assert len(f.accumulated_points._points) == 7
Exemple #14
0
def test_sample():
    f = Points(test_util.point_gen([3, 1, 2],
                                   t_start=10)).extension('tangent',
                                                          start=True,
                                                          end=True)
    assert np.allclose(f.sample_points(domain=(10, 14)), [(10, 3), (11, 1),
                                                          (12, 2), (13, 3),
                                                          (14, 4)])
Exemple #15
0
def test_missing_points():
    points = test_util.point_gen([1, 2, 3] * 10)
    del points[8]
    with pytest.raises(Exception):
        Points(points, uniform=True)

    points = test_util.point_gen([1, 3, 3, 4])
    del points[1]
    ps = Points(points, uniform=False)
    assert not ps.is_uniform
    assert ps(0) == 1
    assert ps(0.5) == 1.5
    assert ps(1) == 2
    assert ps(1.5) == 2.5
    assert ps(2) == 3
    assert ps(2.5) == 3.5
    assert ps(3) == 4
Exemple #16
0
def test_next_interpolation():
    ps = Points(test_util.point_gen([1, 2, 3], t_start=1),
                interpolation=Points.interpolation.next)
    assert ps(1) == 1
    assert ps(1.5) == 2
    assert ps(3) == 3
    assert ps(3.1) is None
    assert ps(0.9) is None
Exemple #17
0
def test_reset_open_interval():
    points = [(0, 1), (1, 5), (2, 3)]
    pf = Points(points, uniform=False)
    assert pf(1) == 5
    pf.reset(domain=Interval.open(0, 2))
    assert pf(0) == 1
    assert pf(1) == 2
    assert pf(2) == 3
Exemple #18
0
def test_regression_with_degree():
    noise = Points(test_util.point_gen([-0.01, 0.01] * 100, t_start=-50))
    ps = test_util.point_gen(list(reversed(range(50))) + list(range(50)),
                             t_start=-50)
    points = Points(ps)
    assert points(0) == 0
    f = (points + noise) \
        .extension(
            'tangent',
            regression_degree=40,
            start=True,
            end=True
        )
    assert f.domain.is_negative_infinite
    assert f.domain.is_positive_infinite
    assert f(60) == approx(60, abs=2)
    assert f(-60) == approx(60, abs=2)
Exemple #19
0
def test_extrema():
    f = Points(test_util.point_gen([1, 3, 1, 3]))
    ref_f = Constant(2)
    e = Extremas(f, ref_f, min_step=1)
    assert np.allclose(e.sample_points(), [(0, 1), (1, 3), (2, 1)])
    assert e(0.5) == 2
    assert e(2) == 1
    assert e(2.1) is None

    f = Points(test_util.point_gen([(1), 1.1, 2.9, (3), (1), 1.1, 3]))
    ref_f = Constant(2)
    e = Extremas(f, ref_f, min_step=1)
    e_points = e.sample_points()
    assert np.allclose(e_points, [(0, 1), (3, 3), (4, 1)])
    assert e(0) == 1
    assert e(3) == 3
    assert e(3.5) == 2
    assert e(4.1) is None
Exemple #20
0
def test_none():
    f = Points([(0, None), (1, 1), (2, 2)]).integral()
    assert f.domain.start == 0
    assert f.domain.end == 2
    assert f(-0.1) is None
    assert f(0) is None
    assert f(0.5) is None
    assert f(1) == 0
    assert f(2) == 1.5
Exemple #21
0
def test_tangent_extension_big_gaps():
    f = Points(test_util.point_gen([3, 1, 2], t_step=10)).extension('tangent',
                                                                    start=True,
                                                                    end=True)
    assert f(0) == 3
    assert f(10) == 1
    assert f(20) == 2
    assert f(30) == 3
    assert f(40) == 4
Exemple #22
0
def test_update_sma():
    points = Points(test_util.point_gen([1, 2, 3, 4, 5]))
    f = points.sma(2)
    assert f(4) == 4.5
    assert f(5) is None
    assert len(f.accumulated_points._points) == 3
    points.append((5, 6))
    assert f(5) == 5.5
    assert len(f.accumulated_points._points) == 4
Exemple #23
0
def test_update_from_empty():
    points = Points([])
    f = points.extension('tangent', start=True, end=True)
    assert f(0) is None
    points.append_list(test_util.point_gen([3, 1, 2]))
    assert f(0) == 3
    assert f(1) == 1
    assert f(2) == 2
    assert f(3) == 3
Exemple #24
0
def test_maximise():
    points = Points([(0, -4), (1, -3), (2, -2), (3, -1), (4, -2)])
    assert points.maximise(-0.5) is None
    assert points.maximise(0) == 3
    assert points.maximise(0.5) == 3
    assert points.maximise(1) == 3
    assert points.maximise(2) == 3
    assert points.maximise(3.1) == 3
    assert points.maximise(3) == 3
    assert points.maximise(4) == 3
Exemple #25
0
def test_points_init_outside():
    f = Points([(1, 1), (2, 1), (3, 1)]).integral()
    assert f.domain.start == 1
    assert f.domain.end == 3
    assert f(0.9) is None
    assert f(1) == 0
    assert f(1.5) == 0.5
    assert f(2) == 1
    assert f(2.5) == 1.5
    assert f(3) == 2
Exemple #26
0
def test_minimise():
    points = Points([(0, 4), (1, 3), (2, 2), (3, 1), (4, 2)])
    assert points.minimise(-0.5) is None
    assert points.minimise(0) == 3
    assert points.minimise(0.5) == 3
    assert points.minimise(1) == 3
    assert points.minimise(2) == 3
    assert points.minimise(3.1) == 3
    assert points.minimise(3) == 3
    assert points.minimise(4) == 3
Exemple #27
0
def test_points():
    f = Points([(0, 1), (1, 1), (2, 1)]).integral()
    assert f.domain.start == 0
    assert f.domain.end == 2
    assert f(-0.1) is None
    assert f(0) == 0
    assert f(0.5) == 0.5
    assert f(1) == 1
    assert f(1.5) == 1.5
    assert f(2) == 2
Exemple #28
0
def test_reset():
    update_interval = None

    def did_update(domain):
        nonlocal update_interval
        update_interval = domain

    points = [(0, 1), (1, 2), (2, 3)]
    pf = Points(points, uniform=False)
    pf.reset()
    assert pf.domain.is_empty

    pf = Points(points, uniform=False)
    update_interval = None
    pf.add_observer(end=did_update)
    assert pf(1) == 2
    assert pf(2) == 3
    pf.reset((2, 2))
    assert pf(1) == 2
    assert pf(2) is None
    assert update_interval.start == 1
    assert update_interval.start_open
    assert update_interval.end == 2
    assert not update_interval.end_open

    pf = Points(points, uniform=False)
    update_interval = None
    pf.add_observer(end=did_update)
    assert pf(0) == 1
    assert pf(1) == 2
    assert pf(2) == 3
    pf.reset((1, 1))
    assert pf(0) == 1
    assert pf(1) == 2
    assert pf(2) == 3
    assert update_interval.start == 0
    assert update_interval.start_open
    assert update_interval.end == 2
    assert update_interval.end_open

    pf = Points(points, uniform=True)
    with pytest.raises(Exception):
        pf.reset((1, 1))
Exemple #29
0
def test_points_next_interpolation():
    f = Points([(0, 2), (1, 4), (2, 6)]).integral(interpolation=Points.interpolation.next)
    assert f.domain.start == 0
    assert f.domain.end == 2
    assert f(-0.1) is None
    assert f(0) == 0
    assert f(0.5) == 2
    assert f(1) == 4
    assert f(1.5) == 7
    assert f(2) == 10
Exemple #30
0
def test_defaults():
    f = Constant(1).integral()
    assert f.domain.is_negative_infinite
    assert f.domain.is_positive_infinite
    assert f(0) == 0
    assert f(1) == 1

    f = Points(test_util.point_gen([1, 1])).integral()
    assert f.domain.start == 0
    assert f.domain.end == 1
    assert f(1) == 1