Ejemplo n.º 1
0
def test_timeserie_getitem():
    a = TimeSerie([1, 2, 3])
    assert a[0] == 1
    assert a[-1] == 3
    b = TimeSerie([1, 2, 3, 4, 5, 6, 7])
    assert b[1] == 2
    return True
Ejemplo n.º 2
0
def test_timeserie_toevents():
    a = TimeSerie([1, 2, 3], t0=1, fs=2, name="a")
    e1, e2, e3 = a.to_events()
    assert compare_events(e1, Event(1, t=1, name="a"))
    assert compare_events(e2, Event(2, 1.5, name="a"))
    assert compare_events(e3, Event(3, 2.0, name="a"))
    return True
Ejemplo n.º 3
0
def test_statechangearray_totimeseries():
    a = StateChangeArray([1, 3, 5, 7], t=[1, 2, 4, 7], name="a")
    ts_a = a.to_timeseries(fs=2)
    assert compare_timeseries(
        ts_a,
        TimeSerie([1, 1, 3, 3, 3, 3, 5, 5, 5, 5, 5, 5, 7],
                  t0=1,
                  fs=2,
                  name="a"))
    b = StateChangeArray([1, 3, 5, 7], t=[1, 2, 2.25, 4])
    with pytest.raises(ValueError):
        ts_b = b.to_timeseries(fs=2)
    with pytest.raises(NameError):
        b.to_timeseries(fs=2, method="nonExistingMethod")
    c = StateChangeArray([1, 3, 5, 7], t=[1, 2, 4, 8], name="c")
    ts_c = c.to_timeseries(fs=2, method="interpolate")
    assert compare_timeseries(
        ts_c,
        TimeSerie(
            [1, 2, 3, 3.5, 4, 4.5, 5, 5.25, 5.5, 5.75, 6, 6.25, 6.5, 6.75, 7],
            t0=1,
            fs=2,
            name="c",
        ),
    )
    return True
Ejemplo n.º 4
0
def test_timeserie_where():
    a = TimeSerie([1, 2, 3, 4, 5, 6], t0=1, fs=1, name="a")
    d = a.where(a.t >= 3)
    d_test = np.array([3, 4, 5, 6])
    assert len(d) == len(d_test)
    assert all(d == d_test)
    return True
Ejemplo n.º 5
0
def test_timeserie_rsub():
    a = TimeSerie([1, 2, 3], name="a", fs=1, t0=1)
    sub_1 = 1 - a
    assert compare_timeseries(sub_1, TimeSerie([0, -1, -2],
                                               name="",
                                               fs=1,
                                               t0=1))
    return True
Ejemplo n.º 6
0
def test_timeserie_invert():
    a = TimeSerie([True, False], name="a", fs=1, t0=1)
    b = TimeSerie([1, 2, 3, 4], name="b", fs=1, t0=1)
    invert_1 = ~a
    assert compare_timeseries(
        invert_1, BooleanTimeSerie([False, True], name="(~a)", fs=1, t0=1))
    with pytest.raises(ValueError):
        invert_2 = ~b
    return True
Ejemplo n.º 7
0
def test_timeserie_sub():
    a = TimeSerie([1, 2, 3], name="a", fs=1, t0=1)
    b = TimeSerie([3, 1, 3], name="b", fs=1, t0=1)
    sub_1 = a - b
    assert compare_timeseries(
        sub_1, TimeSerie([-2, 1, 0], name="(a - b)", fs=1, t0=1))
    sub_2 = a - 1
    assert compare_timeseries(sub_2, TimeSerie([0, 1, 2], name="", fs=1, t0=1))
    c = TimeSerie([7, 8, 9, 0], name="c", fs=1, t0=0)
    with pytest.raises(ValueError):
        sub_3 = a - c
    return True
Ejemplo n.º 8
0
def test_timeserie_add():
    a = TimeSerie([1, 2, 3], name="a", fs=1, t0=1)
    b = TimeSerie([3, 1, 3], name="b", fs=1, t0=1)
    add_1 = a + b
    assert compare_timeseries(add_1,
                              TimeSerie([4, 3, 6], name="(a + b)", fs=1, t0=1))
    add_2 = a + 1
    assert compare_timeseries(add_2, TimeSerie([2, 3, 4], name="", fs=1, t0=1))
    c = TimeSerie([7, 8, 9, 0], name="c", fs=1, t0=0)
    with pytest.raises(ValueError):
        add_3 = a + c
    return True
Ejemplo n.º 9
0
def test_timeserie_tobool():
    a = TimeSerie([0, 0, 0, 1, 2, 3], t0=1, fs=2, name="a")
    b = a.to_bool(inplace=False)
    assert compare_timeseries(
        b,
        BooleanTimeSerie([False, False, False, True, True, True],
                         t0=1,
                         fs=2,
                         name="a"),
    )
    a.to_bool(inplace=True)
    assert compare_timeseries(a, b)
    return True
Ejemplo n.º 10
0
def test_timeserie_neq():
    a = TimeSerie([1, 2, 3], name="a", fs=1, t0=1)
    eq_1 = a != a
    assert compare_timeseries(
        eq_1,
        BooleanTimeSerie([False, False, False], t0=1, name="(a != a)", fs=1))
    eq_2 = a != 1
    assert compare_timeseries(
        eq_2, BooleanTimeSerie([False, True, True], t0=1, fs=1, name=""))
    b = TimeSerie([1, 2, 3], fs=2, name="b", t0=1)
    with pytest.raises(ValueError):
        eq_4 = a != b
    return True
Ejemplo n.º 11
0
def test_timeserie_or():
    a = TimeSerie([True, True, False, False], name="a", fs=1, t0=1)
    b = BooleanTimeSerie([True, False, True, False], name="b", fs=1, t0=1)
    c = TimeSerie([1, 2, 3, 4], name="c", fs=1, t0=1)
    d = BooleanTimeSerie([True, True, True, True], name="d", fs=2, t0=3)
    and_1 = a | b
    assert compare_timeseries(
        and_1,
        BooleanTimeSerie([True, True, True, False], name="(a | b)", fs=1,
                         t0=1))
    with pytest.raises(ValueError):
        and_2 = a | c
    with pytest.raises(ValueError):
        and_3 = b | d
    return True
Ejemplo n.º 12
0
def test_report_totimeserie():
    a = Report(2, 4, name="a")
    ts_a = a.to_timeserie()
    assert compare_timeseries(
        ts_a, TimeSerie([0.0, 1.0, 1.0, 0.0], t0=1.0, fs=1, name="a"))
    b = Report(3, 8, name="b")
    ts_b = b.to_timeserie(fs=4, window=2)
    cmp_b = TimeSerie([0.0, 0.0] + (20 * [1.0]) + [0.0, 0.0],
                      t0=2.5,
                      fs=4,
                      name="b")
    ts_b._reprconfig["threshold"] = 100
    cmp_b._reprconfig["threshold"] = 100
    assert compare_timeseries(ts_b, cmp_b)
    return True
Ejemplo n.º 13
0
def test_timeserie_greaterequalthen():
    a = TimeSerie([1, 2, 3], name="a", fs=1, t0=1)
    b = TimeSerie([3, 1, 3], name="b", fs=1, t0=1)
    lt_1 = a >= b
    assert compare_timeseries(
        lt_1, BooleanTimeSerie([False, True, True],
                               t0=1,
                               fs=1,
                               name="(a >= b)"))
    lt_2 = a >= 2
    assert compare_timeseries(
        lt_2, BooleanTimeSerie([False, True, True], t0=1, fs=1, name=""))
    c = TimeSerie([7, 8, 9, 0], name="c", fs=1, t0=0)
    with pytest.raises(ValueError):
        eq_3 = a >= c
    return True
Ejemplo n.º 14
0
def test_timeserie_lessthen():
    a = TimeSerie([1, 2, 3], name="a", fs=1, t0=1)
    b = TimeSerie([3, 1, 5], name="b", fs=1, t0=1)
    lt_1 = b < a
    assert compare_timeseries(
        lt_1, BooleanTimeSerie([False, True, False],
                               t0=1,
                               fs=1,
                               name="(b < a)"))
    lt_2 = a < 2
    assert compare_timeseries(
        lt_2, BooleanTimeSerie([True, False, False], t0=1, fs=1, name=""))
    c = TimeSerie([7, 8, 9, 0], name="c", fs=1, t0=0)
    with pytest.raises(ValueError):
        eq_3 = a < c
    return True
Ejemplo n.º 15
0
def test_timeserie_init():
    a = TimeSerie([1, 2, 3], t0=0, fs=1, name="a")
    assert all(a.data == [1, 2, 3])
    assert a.t0 == 0
    assert a.fs == 1
    assert a.name == "a"
    return True
Ejemplo n.º 16
0
def test_timeserie_roundt0():
    a = TimeSerie(np.arange(10), name="a", fs=1, t0=1.1)
    assert a.t0 == 1.1
    a.round_t0()
    assert a.t0 == 1.0
    a = TimeSerie(np.arange(100), name="a", fs=10, t0=1.11)
    assert a.t0 == 1.11
    a.round_t0()
    assert a.t0 == 1.1
Ejemplo n.º 17
0
def test_multiple_plots():
    plt.ioff()
    a = StateChangeArray([0, 1, 0, 1, 0], t=[0, 5, 10, 20, 30],
                         name="a").to_bool()
    b = StateChangeArray([0, 1, 0, 1, 0], t=[0, 2, 5, 7, 15],
                         name="b").to_bool()
    c = TimeSerie(np.linspace(0, 10, 100), t0=0, fs=10, name="c")
    a.plot(b, c, align_x=True)
    return True
Ejemplo n.º 18
0
def test_timeserie_empty():
    a = TimeSerie.empty(1, 10, fs=10, name="a")
    assert len(a) == 90
    assert a.te == 9.9
    b = TimeSerie.empty(2, 5, fs=4, name="b", inclusive=True)
    assert len(b) == 13
    assert b.te == 5.0
    c = TimeSerie.empty(
        dt.datetime(2018, 1, 1, 12),
        dt.datetime(2018, 1, 1, 13),
        fs=1,
        name="c",
        inclusive=True,
    )
    assert c.te - c.t0 == 3600.0
    d = TimeSerie.empty(1.2, 4.8, fs=100, name="d")
    assert d.t0 == 1.2
    assert d.te == 4.79
    return True
Ejemplo n.º 19
0
def test_timeserie_toreports():
    a = TimeSerie(10 * np.sin(np.linspace(0, 2 * np.pi, 100)),
                  t0=1,
                  fs=10,
                  name="a")
    b = a > 4
    b.name = "a > 4"
    reports = b.to_reports()
    assert len(reports) == 1
    assert compare_reports(reports[0], Report(1.7, 5.4, name="a > 4"))
    return True
Ejemplo n.º 20
0
def test_timeserie_datetime_t0():
    a = TimeSerie([1, 2, 3],
                  t0=pytz.utc.localize(dt.datetime(2000, 1, 1)),
                  fs=1,
                  name="a")
    b = TimeSerie([1, 2, 3], t0=dt.datetime(2000, 1, 1), fs=1, name="b")
    assert a.t0 == 946684800.0
    assert a.te == 946684802.0
    assert a.dt0 == dt.datetime(2000, 1, 1)
    assert a.dte == dt.datetime(2000, 1, 1, 0, 0, 2)
    assert all(a.dt == np.array(
        ["2000-01-01T00:00:00", "2000-01-01T00:00:01", "2000-01-01T00:00:02"],
        dtype="datetime64",
    ))
    assert b.t0 == 946684800.0
    assert b.te == 946684802.0
    assert b.dt0 == dt.datetime(2000, 1, 1)
    assert b.dte == dt.datetime(2000, 1, 1, 0, 0, 2)
    assert all(b.dt == np.array(
        ["2000-01-01T00:00:00", "2000-01-01T00:00:01", "2000-01-01T00:00:02"],
        dtype="datetime64",
    ))
    return True
Ejemplo n.º 21
0
def test_timeserie_properties():
    a = TimeSerie([1, 2, 3], name="a", fs=2, t0=1)
    assert a.hz == a.fs
    a.hz = 1
    assert a.fs == 1
    b = TimeSerie([4, 5, 6], name="a", fs=1, t0=1)
    b.data = [1, 2, 3]
    assert isinstance(b.data, np.ndarray)
    assert all(b.data == [1, 2, 3])
    assert compare_timeseries(a, b)
    with pytest.raises(ValueError):
        a.channel = (1, 2, 3)
    return True
Ejemplo n.º 22
0
def test_timeserie_plot():
    plt.ioff()
    a = TimeSerie([-2, -1, 0, 1, 2, 3, 4, 5], name="a", fs=1, t0=1)
    f = a.plot()

    b = TimeSerie([-2, -1, 0, 1, 2, 3, 4, 5],
                  name="b",
                  fs=1,
                  t0=dt.datetime(2019, 1, 1))
    f = b.plot(as_dt=True)
    return True
Ejemplo n.º 23
0
def test_timeserie_interpolate():
    a = TimeSerie([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], t0=0, fs=1, name="a")
    t_new = np.arange(0, 9 + 0.5, 0.5)
    b = a.interpolate(t_new, inplace=False)
    assert compare_timeseries(
        b, TimeSerie(np.arange(1, 10.5, 0.5), t0=0, fs=2, name="a"))
    a.interpolate(t_new, inplace=True)
    assert compare_timeseries(a, b)
    c = BooleanTimeSerie([False, False, True, True, True, False, False],
                         t0=1,
                         fs=1,
                         name="c")
    t_new = np.arange(1, 7 + 0.5, 0.5)
    d = c.interpolate(t_new, inplace=False)
    assert compare_timeseries(
        d,
        BooleanTimeSerie(3 * [False] + 7 * [True] + 3 * [False],
                         t0=1,
                         fs=2,
                         name="c"),
    )
    c.interpolate(t_new, inplace=True)
    assert compare_timeseries(c, d)
    return True
Ejemplo n.º 24
0
def test_timeserie_nparray():
    a = TimeSerie(np.array([1, 2, 3]), t0=0, fs=1, name="a")
    assert isinstance(a.data, np.ndarray)
    b = TimeSerie([1, 2, 3], t0=0, fs=1, name="b")
    assert isinstance(b.data, np.ndarray)
    return True
Ejemplo n.º 25
0
def test_timeserie_len():
    a = TimeSerie([1, 2, 3])
    assert len(a) == 3
    b = TimeSerie([1, 2, 3, 4, 5, 6, 7])
    assert len(b) == 7
    return True
Ejemplo n.º 26
0
def test_timeserie_tostatechangearray():
    a = TimeSerie([1, 1, 1, 2, 2, 3, 4, 4, 4], t0=1, fs=2, name="a")
    sta_a = a.to_statechangearray()
    assert compare_statechangearrays(
        sta_a, StateChangeArray([1, 2, 3, 4], t=[1, 2.5, 3.5, 4], name="a"))
    return True
Ejemplo n.º 27
0
def test_timeserie_radd():
    a = TimeSerie([1, 2, 3], name="a", fs=1, t0=1)
    add_1 = 1 + a
    assert compare_timeseries(add_1, TimeSerie([2, 3, 4], name="", fs=1, t0=1))
    return True
Ejemplo n.º 28
0
def test_timeserie_at():
    a = TimeSerie([1, 2, 3, 4, 5, 6], t0=1, fs=1, name="a")
    assert a.at(2) == 2
    return True
Ejemplo n.º 29
0
def test_timserie_mod():
    a = TimeSerie([-2, -1, 0, 1, 2, 3, 4, 5], name="a", fs=1, t0=1)
    b = a.modify("default", inplace=False)
    assert compare_timeseries(b, a)
    a.modify("default", inplace=True)
    assert compare_timeseries(a, b)

    a = TimeSerie([-2, -1, 0, 1, 2, 3, 4, 5], name="a", fs=1, t0=1)
    b = a.modify("zero_negatives", inplace=False)
    assert compare_timeseries(
        b, TimeSerie([0, 0, 0, 1, 2, 3, 4, 5], name="a", fs=1, t0=1))
    a.modify("zero_negatives", inplace=True)
    assert compare_timeseries(a, b)

    a = TimeSerie([-2, -1, 0, 1, 2, 3, 4, 5], name="a", fs=1, t0=1)
    b = a.modify("correct_negatives", inplace=False)
    assert compare_timeseries(
        b, TimeSerie([0, 1, 2, 3, 4, 5, 6, 7], name="a", fs=1, t0=1))
    a.modify("correct_negatives", inplace=True)
    assert compare_timeseries(a, b)
    return True
Ejemplo n.º 30
0
def test_timeserie_tochannel():
    a = TimeSerie([-2, -1, 0, 1, 2, 3, 4, 5], name="a", fs=1, t0=3)
    b = TimeSerie.empty(0, 20, fs=1)
    a.to_channel(b)
    assert len(a) == 20
    assert a.t0 == 0
    assert a.te == 19
    assert a.at(3) == -2
    assert a.at(4) == -1
    assert a.at(5) == 0
    assert a.at(6) == 1
    assert a.at(7) == 2
    assert a.at(8) == 3
    assert a.at(9) == 4
    assert a.at(10) == 5
    c = TimeSerie([-2, -1, 0, 1, 2, 3, 4, 5], name="a", fs=1, t0=3)
    d = TimeSerie.empty(0, 20, fs=2)
    with pytest.raises(ValueError):
        c.to_channel(d)
    e = TimeSerie([-2, -1, 0, 1, 2, 3, 4, 5], name="a", fs=1, t0=3)
    f = TimeSerie.empty(4, 20, fs=1)
    with pytest.raises(DataLossError):
        e.to_channel(f)
    g = TimeSerie([-2, -1, 0, 1, 2, 3, 4, 5], name="a", fs=1, t0=3)
    h = TimeSerie.empty(0, 8, fs=1)
    with pytest.raises(DataLossError):
        g.to_channel(h)
    i = TimeSerie([-2, -1, 0, 1, 2, 3, 4, 5], name="a", fs=1, t0=3)
    j = TimeSerie.empty(0.5, 20.5, fs=1)
    with pytest.raises(ValueError):
        i.to_channel(j)
    return True