Beispiel #1
0
def test_fixtime_simple_despike():
    dt = 0.001
    t = np.arange(1000) * dt
    y = np.empty(1000)
    y[::2] = 0.1
    y[1::2] = -0.1
    ycopy = y.copy()
    ycopy[100] = y[99]
    y[100] = 10.0

    t2, y2 = dsp.fixtime((t, y),
                         "auto",
                         delspikes=dict(method="simple", sigma=4),
                         verbose=False)
    assert np.allclose(t2, t)
    assert np.all(y2 == ycopy)

    t2, y2 = dsp.fixtime(
        (t, y),
        "auto",
        delspikes=dict(method="simple", sigma=4, maxiter=1),
        verbose=False,
    )
    assert np.allclose(t2, t)
    assert np.all(y2 == ycopy)
Beispiel #2
0
def test_fixtime_exclude_point_change2():
    dt = 0.001
    t = np.arange(1000) * dt
    y = np.empty(1000)
    y[::2] = 0.1
    y[1::2] = -0.1
    ycopy = y.copy()
    ycopy[100:105] = y[99]
    y[100:105] = 10.0

    t2, y2 = dsp.fixtime((t, y),
                         "auto",
                         delspikes=dict(method="despike"),
                         verbose=False)
    assert np.allclose(t2, t)
    assert np.all(y2 == ycopy)

    t2, y2 = dsp.fixtime(
        (t, y),
        "auto",
        delspikes=dict(exclude_point="last", method="despike"),
        verbose=False,
    )
    assert np.allclose(t2, t)
    assert np.all(y2 == ycopy)

    t2, y2 = dsp.fixtime(
        (t, y),
        "auto",
        delspikes=dict(exclude_point="middle", method="despike"),
        verbose=False,
    )
    assert np.allclose(t2, t)
    assert np.all(y2 == y)

    # some of a ramp up should be deleted:
    y[100:105] = np.arange(0.0, 5.0) * 3
    ycopy = y.copy()
    ycopy[102:105] = y[101]
    t2, y2 = dsp.fixtime((t, y),
                         "auto",
                         delspikes=dict(sigma=35, method="despike"),
                         verbose=False)
    assert np.allclose(t2, t)
    assert np.all(y2 == ycopy)

    # none of a ramp down should be deleted:
    y[100:105] = np.arange(5.0, 0.0, -1.0) * 3
    t2, y2 = dsp.fixtime((t, y),
                         "auto",
                         delspikes=dict(sigma=35, method="despike"),
                         verbose=False)
    assert np.allclose(t2, t)
    assert np.all(y2 == y)
Beispiel #3
0
def test_fixtime_despike_edge_conditions():
    dt = 0.001
    n = 45
    t = np.arange(n) * dt
    y = np.empty(n)
    for i in range(1, n - 5):
        y[::2] = 0.1
        y[1::2] = -0.1
        ycopy = y.copy()
        s = slice(i, i + 5)
        ycopy[s] = y[i - 1]
        y[s] = 10.0

        # with despike_diff:
        t2, y2 = dsp.fixtime((t, y), "auto", delspikes=True, verbose=False)
        assert np.allclose(t2, t)
        if i < n - 15:
            assert np.allclose(y2, ycopy)
        else:
            assert np.allclose(y2, y)

        t2, y2 = dsp.fixtime((t, y),
                             "auto",
                             delspikes=dict(exclude_point="last"),
                             verbose=False)
        assert np.allclose(t2, t)
        if i > 10:
            assert np.allclose(y2, ycopy)
        else:
            assert np.allclose(y2, y)

        # now with despike:
        t2, y2 = dsp.fixtime((t, y),
                             "auto",
                             delspikes=dict(method="despike"),
                             verbose=False)
        assert np.allclose(t2, t)
        if i < n - 15 - 3:
            assert np.allclose(y2, ycopy)
        else:
            assert np.allclose(y2, y)

        t2, y2 = dsp.fixtime(
            (t, y),
            "auto",
            delspikes=dict(method="despike", exclude_point="last"),
            verbose=False,
        )
        assert np.allclose(t2, t)
        if i > 13:
            assert np.allclose(y2, ycopy)
        else:
            assert np.allclose(y2, y)
Beispiel #4
0
def test_fixtime_despike():
    import sys

    for v in list(sys.modules.values()):
        if getattr(v, "__warningregistry__", None):
            v.__warningregistry__ = {}

    t = np.arange(16)
    y = [
        1,
        2,
        3,
        4,
        np.nan,
        6,
        7,
        np.inf,
        9,
        10,
        -np.inf,
        12,
        -1.40130e-45,
        14,
        134,
        15,
    ]

    with assert_warns(RuntimeWarning):
        (tn, yn), info = dsp.fixtime((t, y),
                                     sr=1,
                                     getall=1,
                                     delspikes=dict(n=9, sigma=2))
        (tn2, yn2), info2 = dsp.fixtime((t, y),
                                        sr=1,
                                        getall=1,
                                        delspikes=dict(sigma=2))

    d = info.alldrops.dropouts
    assert np.all(d == range(4, 13))
    assert np.all(t == tn)
    assert np.all(t == tn2)
    assert np.allclose(yn, [1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 15])
    assert np.allclose(yn2, yn)
    assert info.despike_info.delspikes["n"] == 9
    assert info2.despike_info.delspikes["n"] == 15
    # spike is at pos 14
    assert np.all(info.alldrops.spikes == 14)
    assert np.all(info2.alldrops.spikes == 14)
    assert np.all(info.alldrops.alldrops == range(4, 15))
Beispiel #5
0
def test_fixtime_perfect():
    dt = 0.001
    t = np.arange(1000) * dt
    y = np.random.randn(len(t))
    t2, y2 = dsp.fixtime((t, y), "auto")
    assert np.allclose(t2, t)
    assert np.all(y2 == y)
Beispiel #6
0
def test_fix_edges2():
    dt = 0.01
    t = np.arange(50) * dt

    y = np.zeros(50)
    n = 44
    y[n:] = 1
    t2, y2 = dsp.fixtime((t, y),
                         "auto",
                         delspikes=dict(exclude_point="last"),
                         verbose=False)
    assert np.allclose(t2, t[:n])
    assert np.all(y2 == y[:n])

    y = np.zeros(50)
    n = 5
    y[:n] = 1
    t2, y2 = dsp.fixtime((t, y),
                         "auto",
                         delspikes=dict(exclude_point="first"),
                         verbose=False)
    assert np.allclose(t2, t[n:])
    assert np.all(y2 == y[n:])

    y = np.zeros(50)
    n = 44
    y[n:] = 1
    t2, y2 = dsp.fixtime(
        (t, y),
        "auto",
        delspikes=dict(exclude_point="last", method="despike"),
        verbose=False,
    )
    assert np.allclose(t2, t[:n])
    assert np.all(y2 == y[:n])

    y = np.zeros(50)
    n = 5
    y[:n] = 1
    t2, y2 = dsp.fixtime(
        (t, y),
        "auto",
        delspikes=dict(exclude_point="first", method="despike"),
        verbose=False,
    )
    assert np.allclose(t2, t[n:])
    assert np.all(y2 == y[n:])
Beispiel #7
0
def test_fixtime2():
    dt = 0.001
    sr = 1000
    t = np.arange(0, 1, dt)
    t[1:] += dt / 2
    # t[400:] -= dt/2
    noise = np.random.randn(t.size)
    noise = noise / noise.max() / 100000
    y = np.random.randn(t.size)
    told = t + noise

    t2, y2 = dsp.fixtime((t + noise, y), "auto", base=None)
    pv = locate.find_subseq(y2, y[1:-1])
    assert pv.size > 0

    t3, y3 = dsp.fixtime((t + noise, y), "auto", base=0.0)
    pv = locate.find_subseq(y2, y[1:-1])
    assert pv.size > 0
    assert not np.allclose(t2[0], t3[0])
    assert np.allclose(t2 - t2[0], t3 - t3[0])
    assert abs(t3 - 0.003).min() < 1e-14
Beispiel #8
0
def test_fixtime_drift():
    L = 10000
    for factor in (0.9995, 1.0005):
        t = np.arange(L) * (0.001 * factor)
        y = np.random.randn(L)
        t2, y2 = dsp.fixtime((t, y), 1000, base=0)
        t3, y3 = dsp.fixtime((t, y), 1000, fixdrift=1, base=0)

        i = np.argmax(y[3:100])
        i2 = np.nonzero(y2[:103] == y[i])[0][0]
        i3 = np.nonzero(y3[:103] == y[i])[0][0]
        d2 = abs(t2[i2] - t[i])
        d3 = abs(t3[i3] - t[i])

        L = len(y)
        i = L - 100 + np.argmax(y[-100:-10])
        L = len(y2)
        i2 = L - 110 + np.nonzero(y2[-110:] == y[i])[0][0]
        L = len(y3)
        i3 = L - 110 + np.nonzero(y3[-110:] == y[i])[0][0]
        d2 += abs(t2[i2] - t[i])
        d3 += abs(t3[i3] - t[i])
        assert d3 < d2
Beispiel #9
0
def test_fixtime_perfect_delspike():
    dt = 0.001
    t = np.arange(1000) * dt
    y = np.sin(2 * np.pi * 5 * t)
    (t2, y2), info = dsp.fixtime((t, y), "auto", delspikes=True, getall=1)
    assert np.allclose(t2, t)
    assert np.all(y2 == y)

    assert_raises(
        ValueError,
        dsp.fixtime,
        (t, y),
        "auto",
        delspikes=dict(method="badmethod"),
        getall=1,
    )
Beispiel #10
0
def test_fixtime_naninf():
    import sys

    for v in list(sys.modules.values()):
        if getattr(v, "__warningregistry__", None):
            v.__warningregistry__ = {}

    t = np.arange(15)
    y = [1, 2, 3, 4, np.nan, 6, 7, np.inf, 9, 10, -np.inf, 12, -1.40130e-45, 14, 15]

    with assert_warns(RuntimeWarning):
        (tn, yn), info = dsp.fixtime((t, y), sr=1, getall=1)

    d = info.alldrops.dropouts
    assert np.all(d == [4, 7, 10, 12])
    assert np.all(t == tn)
    assert np.all(yn == [1, 2, 3, 4, 4, 6, 7, 7, 9, 10, 10, 12, 12, 14, 15])
Beispiel #11
0
def test_fixtime_too_many_tp():
    # actual data:
    fd = np.array([
        [0.91878125, 1.17070317],
        [0.9226875, 1.17070317],
        [0.9226875, 1.17070317],
        [0.92659375, 1.17070317],
        [0.92659375, 1.17070317],
        [0.9305, 1.17070317],
        [0.9305, 1.17070317],
        [0.93440625, 1.17070317],
        [0.93440625, 1.17070317],
        [0.9383125, 1.17070317],
        [0.9383125, 1.36582029],
        [0.94221875, 1.29265141],
        [0.94221875, 0.97558594],
        [0.946125, 1.63410652],
        [0.946125, 0.92680663],
        [0.95003125, 1.5365479],
        [0.95003125, 1.07314456],
        [0.9539375, 1.26826179],
        [0.9539375, 1.31704104],
        [0.95784375, 1.1950928],
        [0.95784375, 1.29265141],
        [0.96175, 1.04875493],
        [0.96175, 1.34143066],
        [0.96565625, 0.92680663],
        [0.96565625, 1.36582029],
        [0.9695625, 0.87802738],
        [0.9695625, 1.24387205],
        [0.97346875, 0.97558594],
        [0.97346875, 1.14631355],
        [0.977375, 0.99997562],
        [0.977375, 1.14631355],
        [0.98128125, 1.07314456],
        [0.98128125, 1.07314456],
        [0.9851875, 1.1950928],
        [0.9851875, 1.09753418],
        [0.98909375, 1.1950928],
        [0.98909375, 1.1219238],
        [0.993, 1.1950928],
        [0.993, 1.17070317],
        [0.99690625, 1.1950928],
        [0.99690625, 1.14631355],
        [1.0008125, 1.17070317],
    ])

    import sys

    for v in list(sys.modules.values()):
        if getattr(v, "__warningregistry__", None):
            v.__warningregistry__ = {}

    assert_raises(ValueError, dsp.fixtime, ([1, 2, 3, 4], [1, 2, 3]))
    assert_raises(ValueError, dsp.fixtime, np.random.randn(3, 3, 3))

    with assert_warns(RuntimeWarning):
        t, d = dsp.fixtime((*fd.T, ), sr="auto")
    # assert np.all(fd[:, 1] == d)
    assert np.allclose(np.diff(t), 0.002)
    assert np.allclose(t[0], fd[0, 0])
Beispiel #12
0
def test_fixtime():
    import sys

    for v in list(sys.modules.values()):
        if getattr(v, "__warningregistry__", None):
            v.__warningregistry__ = {}

    assert_raises(ValueError, dsp.fixtime, ([1, 2, 3, 4], [1, 2, 3]))

    t = [0, 1, 6, 7]
    y = [1, 2, 3, 4]
    with assert_warns(RuntimeWarning):
        tn, yn = dsp.fixtime((t, y), sr=1)
        ty2 = dsp.fixtime(np.vstack([t, y]).T, sr=1)

    t2, y2 = ty2.T
    assert np.all(tn == t2)
    assert np.all(yn == y2)
    assert np.all(tn == np.arange(8))
    assert np.all(yn == [1, 2, 2, 2, 2, 2, 3, 4])

    with assert_warns(RuntimeWarning):
        (t2, y2), info = dsp.fixtime((t, y), getall=True, sr=1)
        # ((t2, y2), pv,
        # sr_stats, tp) = dsp.fixtime((t, y), getall=True,
        #                                sr=1)
    assert np.all(tn == t2)
    assert np.all(yn == y2)
    assert np.all(info.alldrops.dropouts == [])
    assert np.allclose(info.sr_stats, [1, 0.2, 3 / 7, 1, 200 / 3])
    assert np.all(info.tp == np.arange(4))
    assert info.despike_info is None
    assert_raises(ValueError, dsp.fixtime, (1, 1, 1))

    drop = -1.40130e-45
    t = [0, 0.5, 1, 6, 7]
    y = [1, drop, 2, 3, 4]

    with assert_warns(RuntimeWarning):
        t2, y2 = dsp.fixtime([t, y], sr=1)
    assert np.all(tn == t2)
    assert np.all(yn == y2)

    t = [1, 2, 3]
    y = [drop, drop, drop]
    assert_warns(RuntimeWarning, dsp.fixtime, (t, y), "auto")

    (t2, y2), info = dsp.fixtime((t, y), "auto", deldrops=False, getall=True)
    assert np.all(info.tp == [0, 2])
    assert np.all(t2 == t)
    assert np.all(y2 == y)
    assert info.alldrops.dropouts is None

    t = np.arange(100.0)
    t = np.hstack((t, 200.0))
    y = np.arange(101)
    with assert_warns(RuntimeWarning):
        t2, y2 = dsp.fixtime((t, y), "auto")
    assert np.all(t2 == np.arange(100.0))
    assert np.all(y2 == np.arange(100))

    with assert_warns(RuntimeWarning):
        t2, y2 = dsp.fixtime((t, y), "auto", delouttimes=0)
    assert np.all(t2 == np.arange(200.1))
    sbe = np.ones(201, int) * 99
    sbe[:100] = np.arange(100)
    sbe[-1] = 100
    assert np.all(y2 == sbe)

    t = [0, 1, 2, 3, 4, 5, 7, 6]
    y = [0, 1, 2, 3, 4, 5, 7, 6]
    with assert_warns(RuntimeWarning):
        t2, y2 = dsp.fixtime((t, y), 1)
    assert np.all(t2 == np.arange(8))
    assert np.all(y2 == np.arange(8))

    assert_raises(ValueError, dsp.fixtime, (t, y), 1, negmethod="stop")

    t = np.arange(8)[::-1]
    assert_raises(ValueError, dsp.fixtime, (t, y), 1)

    t = np.arange(0, 1, 0.001)
    noise = np.random.randn(t.size)
    noise = noise / noise.max() / 100000
    y = np.random.randn(t.size)
    t2, y2 = dsp.fixtime((t + noise, y), "auto", base=0)
    assert np.allclose(t, t2)
    assert np.all(y == y2)

    dt = np.ones(100)
    dt[80:] = 0.91 * dt[80:]
    t = dt.cumsum()
    y = np.arange(100)
    with assert_warns(RuntimeWarning):
        t2, y2 = dsp.fixtime((t, y), 1)

    dt = np.ones(100)
    dt[80:] = 1.08 * dt[80:]
    t = dt.cumsum()
    y = np.arange(100)
    with assert_warns(RuntimeWarning):
        t2, y2 = dsp.fixtime((t, y), 1)