Esempio n. 1
0
def test_iterperiods():

    # timeseries with a default value and no points returns one period
    # with the default value from -infinity to infinity, or 
    ts = TimeSeries(default=0)
    assert list(ts.iterperiods()) == [(-inf, inf, 0)]

    # timeseries with no default value and no points raises error
    ts = TimeSeries()
    with nose.tools.assert_raises(KeyError):
        list(ts.iterperiods())
    
    ts = TimeSeries()
    ts.set(datetime.datetime(2015, 3, 1), 1)
    ts.set(datetime.datetime(2015, 3, 2), 0)
    ts.set(datetime.datetime(2015, 3, 3), 1)
    ts.set(datetime.datetime(2015, 3, 4), 2)

    answer = [
        (datetime.datetime(2015, 3, 1), datetime.datetime(2015, 3, 2), 1),
        (datetime.datetime(2015, 3, 2), datetime.datetime(2015, 3, 3), 0),
        (datetime.datetime(2015, 3, 3), datetime.datetime(2015, 3, 4), 1)]
    result = []
    for (t0, t1, v0) in ts.iterperiods(
            start=datetime.datetime(2015, 3, 1),
            end=datetime.datetime(2015, 3, 4)
    ):
        result.append((t0, t1, v0))
    assert answer == result

    answer = [
        (datetime.datetime(2015, 3, 1), datetime.datetime(2015, 3, 2), 1),
        (datetime.datetime(2015, 3, 3), datetime.datetime(2015, 3, 4), 1),
    ]
    result = []
    for (t0, t1, v0) in ts.iterperiods(
            start=datetime.datetime(2015, 3, 1),
            end=datetime.datetime(2015, 3, 4),
            value=1,
    ):
        result.append((t0, t1, v0))
    assert answer == result

    def filter(t0, t1, value):
        return True if not value else False

    answer = [
        (datetime.datetime(2015, 3, 2), datetime.datetime(2015, 3, 3), 0),
    ]
    result = []
    for (t0, t1, v0) in ts.iterperiods(
            start=datetime.datetime(2015, 3, 1),
            end=datetime.datetime(2015, 3, 4),
            value=filter,
    ):
        result.append((t0, t1, v0))
    assert answer == result
Esempio n. 2
0
def test_iterperiods():

    # timeseries with no points raises a KeyError
    ts = TimeSeries()
    with nose.tools.assert_raises(KeyError):
        next(ts.iterperiods())

    ts.set(datetime.datetime(2015, 3, 1), 1)
    ts.set(datetime.datetime(2015, 3, 2), 0)
    ts.set(datetime.datetime(2015, 3, 3), 1)
    ts.set(datetime.datetime(2015, 3, 4), 2)

    answer = [(datetime.datetime(2015, 3, 1), datetime.datetime(2015, 3,
                                                                2), 1),
              (datetime.datetime(2015, 3, 2), datetime.datetime(2015, 3,
                                                                3), 0),
              (datetime.datetime(2015, 3, 3), datetime.datetime(2015, 3,
                                                                4), 1)]
    result = []
    for (t0, t1, v0) in ts.iterperiods(start=datetime.datetime(2015, 3, 1),
                                       end=datetime.datetime(2015, 3, 4)):
        result.append((t0, t1, v0))
    assert answer == result

    answer = [
        (datetime.datetime(2015, 3, 1), datetime.datetime(2015, 3, 2), 1),
        (datetime.datetime(2015, 3, 3), datetime.datetime(2015, 3, 4), 1),
    ]
    result = []
    for (t0, t1, v0) in ts.iterperiods(
            start=datetime.datetime(2015, 3, 1),
            end=datetime.datetime(2015, 3, 4),
            value=1,
    ):
        result.append((t0, t1, v0))
    assert answer == result

    def filter(t0, t1, value):
        return True if not value else False

    answer = [
        (datetime.datetime(2015, 3, 2), datetime.datetime(2015, 3, 3), 0),
    ]
    result = []
    for (t0, t1, v0) in ts.iterperiods(
            start=datetime.datetime(2015, 3, 1),
            end=datetime.datetime(2015, 3, 4),
            value=filter,
    ):
        result.append((t0, t1, v0))
    assert answer == result
Esempio n. 3
0
 def _beh_interval(self, ts: TimeSeries):
     if ts.is_empty():
         return 0
     return sum([
         en - st for st, en, _ in ts.iterperiods()
         if en - st < self.decay_time
     ])
Esempio n. 4
0
def test_iterperiods():

    ts = TimeSeries()
    ts.set(datetime.datetime(2015, 3, 1), 1)
    ts.set(datetime.datetime(2015, 3, 2), 0)
    ts.set(datetime.datetime(2015, 3, 3), 1)
    ts.set(datetime.datetime(2015, 3, 4), 2)

    answer = [
        (datetime.datetime(2015, 3, 1), datetime.datetime(2015, 3, 2), 1),
        (datetime.datetime(2015, 3, 2), datetime.datetime(2015, 3, 3), 0),
        (datetime.datetime(2015, 3, 3), datetime.datetime(2015, 3, 4), 1)]
    result = []
    for (t0, t1, v0) in ts.iterperiods(
            start=datetime.datetime(2015, 3, 1),
            end=datetime.datetime(2015, 3, 4)
    ):
        result.append((t0, t1, v0))
    assert answer == result

    answer = [
        (datetime.datetime(2015, 3, 1), datetime.datetime(2015, 3, 2), 1),
        (datetime.datetime(2015, 3, 3), datetime.datetime(2015, 3, 4), 1),
    ]
    result = []
    for (t0, t1, v0) in ts.iterperiods(
            start=datetime.datetime(2015, 3, 1),
            end=datetime.datetime(2015, 3, 4),
            value=1,
    ):
        result.append((t0, t1, v0))
    assert answer == result

    def filter(t0, t1, value):
        return True if not value else False

    answer = [
        (datetime.datetime(2015, 3, 2), datetime.datetime(2015, 3, 3), 0),
    ]
    result = []
    for (t0, t1, v0) in ts.iterperiods(
            start=datetime.datetime(2015, 3, 1),
            end=datetime.datetime(2015, 3, 4),
            value=filter,
    ):
        result.append((t0, t1, v0))
    assert answer == result
Esempio n. 5
0
def test_iterperiods():
    ts = TimeSeries([[1, 2], [2, 3], [6, 1], [8, 4]],
                    domain=Domain([1, 2], [3, 5], [6, 8]))

    answers = [(1, 2, 2), (3, 5, 3), (6, 8, 1)]
    i = 0
    for t0, t1, value in ts.iterperiods():
        match = (t0, t1, value) == answers[i]
        assert match
        i += 1

    answers = [(1, 2, 2), (3, 5, 3), (6, 8, 1)]
    i = 0
    for t0, t1, value in ts.iterperiods(0, 9):
        match = (t0, t1, value) == answers[i]
        assert match
        i += 1