コード例 #1
0
ファイル: test_operations.py プロジェクト: vlsd/traces
def example_mean():

    l = TimeSeries()
    l[datetime.datetime(2010, 1, 1)] = 0
    l[datetime.datetime(2010, 1, 3, 10)] = 1
    l[datetime.datetime(2010, 1, 5)] = 0
    l[datetime.datetime(2010, 1, 8)] = 1
    l[datetime.datetime(2010, 1, 17)] = 0
    l[datetime.datetime(2010, 1, 19)] = 1
    l[datetime.datetime(2010, 1, 23)] = 0
    l[datetime.datetime(2010, 1, 26)] = 1
    l[datetime.datetime(2010, 1, 28)] = 0
    l[datetime.datetime(2010, 1, 31)] = 1
    l[datetime.datetime(2010, 2, 5)] = 0

    for time, value in l:
        print time.isoformat(), 0.1 * value + 1.1

    print ''

    timestep = {'hours': 25}
    start_time = datetime.datetime(2010, 1, 1)
    while start_time <= datetime.datetime(2010, 2, 5):
        end_time = start_time + datetime.timedelta(**timestep)
        print start_time.isoformat(), l.mean(start_time, end_time)
        start_time = end_time

    print ''

    start_time = datetime.datetime(2010, 1, 1)
    while start_time <= datetime.datetime(2010, 2, 5):
        end_time = start_time + datetime.timedelta(**timestep)
        print start_time.isoformat(), -0.2
        print start_time.isoformat(), 1.2
        start_time = end_time
コード例 #2
0
def test_mask():

    start = datetime.datetime(2015, 3, 1)

    # v. simple
    a = TimeSeries()
    a.set(start, 1)
    a.set(datetime.datetime(2015, 4, 2), 0)
    a.set(datetime.datetime(2015, 4, 3), 1)
    a.set(datetime.datetime(2015, 4, 4), 0)
    end = datetime.datetime(2015, 4, 5)

    mask = Domain()
    mask[datetime.datetime(2015, 4, 1)] = True
    mask[datetime.datetime(2015, 4, 3)] = False

    # not normalized
    distribution = a.distribution(
        start=start,
        end=end,
        normalized=False,
        mask=mask,
    )
    assert distribution[0] == 24 * 60 * 60  # one day
    assert distribution[1] == 24 * 60 * 60

    # normalized
    distribution = a.distribution(
        start=start,
        end=end,
        mask=mask,
    )
    assert distribution[0] == 0.5
    assert distribution[1] == 0.5
コード例 #3
0
def test_distribution():

    start = datetime.datetime(2015, 3, 1)

    # v. simple
    a = TimeSeries()
    a.set(start, 1)
    a.set(datetime.datetime(2015, 3, 2), 0)
    a.set(datetime.datetime(2015, 3, 3), 1)
    a.set(datetime.datetime(2015, 3, 4), 0)
    end = datetime.datetime(2015, 3, 5)

    # not normalized
    distribution = a.distribution(
        start=start,
        end=end,
        normalized=False,
    )
    assert distribution[0] == 24 * 60 * 60 * 2  # two days
    assert distribution[1] == 24 * 60 * 60 * 2

    # normalized
    distribution = a.distribution(start=start, end=end)
    assert distribution[0] == 0.5
    assert distribution[1] == 0.5
コード例 #4
0
def test_set_domain():

    ts = TimeSeries()
    assert ts.domain == Domain([-inf, inf])

    ts.domain = None
    assert ts.domain == Domain([-inf, inf])

    ts.domain = [-inf, 5]
    assert ts.domain == Domain([-inf, 5])

    ts.domain = [5, inf]
    assert ts.domain == Domain([5, inf])

    ts.domain = [-inf, inf]
    assert ts.domain == Domain([-inf, inf])

    ts.domain = [2, 5]
    assert ts.domain == Domain([2, 5])

    ts.domain = [[2, 5], [9, 10]]
    assert ts.domain == Domain([2, 5], [9, 10])

    ts = TimeSeries(data=[(1, 2), (2, 3), (6, 1), (8, 4)])

    nose.tools.assert_raises(ValueError, setattr, ts, 'domain', [1.5, 7])
    nose.tools.assert_raises(ValueError, setattr, ts, 'domain', [0, 7])
    nose.tools.assert_raises(ValueError, setattr, ts, 'domain', [1.5, 9])
コード例 #5
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
     ])
コード例 #6
0
def example_mean():

    l = TimeSeries()
    l[datetime.datetime(2010, 1, 1)] = 0
    l[datetime.datetime(2010, 1, 3, 10)] = 1
    l[datetime.datetime(2010, 1, 5)] = 0
    l[datetime.datetime(2010, 1, 8)] = 1
    l[datetime.datetime(2010, 1, 17)] = 0
    l[datetime.datetime(2010, 1, 19)] = 1
    l[datetime.datetime(2010, 1, 23)] = 0
    l[datetime.datetime(2010, 1, 26)] = 1
    l[datetime.datetime(2010, 1, 28)] = 0
    l[datetime.datetime(2010, 1, 31)] = 1
    l[datetime.datetime(2010, 2, 5)] = 0

    for time, value in l:
        print(time.isoformat(), 0.1 * value + 1.1)

    print('')

    timestep = {'hours': 25}
    start = datetime.datetime(2010, 1, 1)
    while start <= datetime.datetime(2010, 2, 5):
        end = start + datetime.timedelta(**timestep)
        print(start.isoformat(), l.mean(start, end))
        start = end

    print('')

    start = datetime.datetime(2010, 1, 1)
    while start <= datetime.datetime(2010, 2, 5):
        end = start + datetime.timedelta(**timestep)
        print(start.isoformat(), -0.2)
        print(start.isoformat(), 1.2)
        start = end
コード例 #7
0
    def add_annotation(self, file: os.PathLike, **kwargs):
        self.file_list.append(file)

        # setup default kwargs #
        if self.ordinal:
            kwargs.update({'value_transform': lambda cls: self.order.get(cls)})
        if not kwargs.get('time_transform', None):
            kwargs.update({'time_transform': lambda t: float(t)})
        if not kwargs.get('skip_header', None):
            kwargs.update({'skip_header': False})
        if not os.path.isfile(file):
            raise FileNotFoundError(f'File {file} not found.')
        if os.path.getsize(file):
            ts = TimeSeries.from_csv(file,
                                     **kwargs).slice(self.start_time,
                                                     self.end_time)
            meta = pd.read_csv(file, names=COLUMNS.keys())
            meta.drop(meta[meta['time'] < self.start_time].index)
            meta.drop(meta[meta['time'] > self.end_time].index)
        else:
            ts = TimeSeries()
            meta = pd.DataFrame()
        self.metadata.append(meta)
        self.series.append(ts)
        self._data = self._report()
コード例 #8
0
def example_arrow():

    l = TimeSeries()
    l[arrow.Arrow(2010, 1, 1)] = 0
    l[arrow.Arrow(2010, 1, 3, 10)] = 1
    l[arrow.Arrow(2010, 1, 5)] = 0
    l[arrow.Arrow(2010, 1, 8)] = 1
    l[arrow.Arrow(2010, 1, 17)] = 0
    l[arrow.Arrow(2010, 1, 19)] = 1
    l[arrow.Arrow(2010, 1, 23)] = 0
    l[arrow.Arrow(2010, 1, 26)] = 1
    l[arrow.Arrow(2010, 1, 28)] = 0
    l[arrow.Arrow(2010, 1, 31)] = 1
    l[arrow.Arrow(2010, 2, 5)] = 0

    for time, value in l:
        print(time.naive.isoformat(), 0.1 * value + 1.1)

    print('')

    start = arrow.Arrow(2010, 1, 1)
    end = arrow.Arrow(2010, 2, 5)
    unit = {'hours': 25}
    for start, end in span_range(start, end, unit):
        print(start.naive.isoformat(), l.mean(start, end))

    print('')

    for start, end in span_range(start, end, unit):
        print(start.naive.isoformat(), -0.2)
        print(start.naive.isoformat(), 1.2)
コード例 #9
0
def test_init_data():
    ts = TimeSeries([(1, 2), (2, 3), (6, 1), (8, 4)])

    assert ts[0] == 2
    assert ts[1] == 2
    assert ts[1.5] == 2
    assert ts[6] == 1
    assert ts[7] == 1
    assert ts[8] == 4
    assert ts[10] == 4

    ts = TimeSeries([[1, 2], [2, 3], [6, 1], [8, 4]])

    assert ts[0] == 2
    assert ts[1] == 2
    assert ts[1.5] == 2
    assert ts[6] == 1
    assert ts[7] == 1
    assert ts[8] == 4
    assert ts[10] == 4

    ts = TimeSeries({1: 2, 2: 3, 6: 1, 8: 4})

    assert ts[0] == 2
    assert ts[1] == 2
    assert ts[1.5] == 2
    assert ts[6] == 1
    assert ts[7] == 1
    assert ts[8] == 4
    assert ts[10] == 4
コード例 #10
0
def test_mean():
    ts = TimeSeries([[1, 2], [2, 3], [6, 1], [8, 4]], domain=Domain(0, 9))
    assert ts.mean() == 22. / 9

    ts = TimeSeries([[1, 2], [2, 3], [6, 1], [8, 4]],
                    domain=Domain([1, 2], [3, 5], [6, 8]))
    nose.tools.assert_raises(NotImplementedError, ts.mean)
コード例 #11
0
def test_difference():

    a = TimeSeries(data=[(0, 0), (2, 2)], default=0)
    b = TimeSeries(data=[(1, 1), (3, 2)], default=0)

    c = a - b
    nose.tools.eq_(list(c.items()), [(0, 0), (1, -1), (2, 1), (3, 0)])
コード例 #12
0
ファイル: test_operations.py プロジェクト: vlsd/traces
def example_arrow():

    l = TimeSeries()
    l[arrow.Arrow(2010, 1, 1)] = 0
    l[arrow.Arrow(2010, 1, 3, 10)] = 1
    l[arrow.Arrow(2010, 1, 5)] = 0
    l[arrow.Arrow(2010, 1, 8)] = 1
    l[arrow.Arrow(2010, 1, 17)] = 0
    l[arrow.Arrow(2010, 1, 19)] = 1
    l[arrow.Arrow(2010, 1, 23)] = 0
    l[arrow.Arrow(2010, 1, 26)] = 1
    l[arrow.Arrow(2010, 1, 28)] = 0
    l[arrow.Arrow(2010, 1, 31)] = 1
    l[arrow.Arrow(2010, 2, 5)] = 0

    for time, value in l:
        print time.naive.isoformat(), 0.1 * value + 1.1

    print ''

    start = arrow.Arrow(2010, 1, 1)
    end = arrow.Arrow(2010, 2, 5)
    unit = {'hours': 25}
    for start_time, end_time in span_range(start, end, unit):
        print start_time.naive.isoformat(), l.mean(start_time, end_time)

    print ''

    for start_time, end_time in span_range(start, end, unit):
        print start_time.naive.isoformat(), -0.2
        print start_time.naive.isoformat(), 1.2
コード例 #13
0
ファイル: test_traces.py プロジェクト: vishalbelsare/traces
def test_exists():
    ts = TimeSeries([(-5, 0), (0, 23), (5, None)])

    ts_exists = ts.exists()
    assert ts_exists[-10] is False
    assert ts_exists[-2] is True
    assert ts_exists[3] is True
    assert ts_exists[10] is False
コード例 #14
0
def test_none_handling():
    ts = TimeSeries()
    ts[1] = (0, 1)
    ts[2] = (None, 0)
    ts[3] = (2, 0)

    # print(ts.distribution(normalized=False))
    assert (ts.distribution()[(0, 1)] == 0.5)
    assert (ts.distribution()[(None, 0)] == 0.5)
コード例 #15
0
def test_is_data_in_domain():
    data1 = [(1, 2), (2, 3), (6, 1), (7, 4)]
    ts = TimeSeries(data=data1)

    # this shouldn't work
    nose.tools.assert_raises(ValueError, setattr, ts, 'domain', [3, 4])

    # this should work
    ts.domain = [0, 8]
コード例 #16
0
def test_merge():
    ts_a = TimeSeries(default=None)
    ts_b = TimeSeries(default=None)
    ts_a[0] = None
    ts_b[0] = True
    ts_merge = TimeSeries.merge([ts_a, ts_b])

    assert True in ts_merge[0]
    assert None in ts_merge[0]
コード例 #17
0
def test_set_interval_datetime():
    ts = TimeSeries(default=400)
    ts[datetime(2012, 1, 4, 12)] = 5
    ts[datetime(2012, 1, 9, 18)] = 10
    ts[datetime(2012, 1, 8):datetime(2012, 1, 10)] = 100

    assert ts.items() == [(datetime(2012, 1, 4, 12, 0), 5),
                          (datetime(2012, 1, 8, 0, 0), 100),
                          (datetime(2012, 1, 10, 0, 0), 10)]
コード例 #18
0
 def group_total_beh(self):
     """
     Returns: float of group total behavior index as a fraction of interval
     """
     if len(self.ts_list) > 1:
         ts = TimeSeries()
         ts = ts.merge(self.ts_list)
         return self.beh_index(ts)
     else:
         return self.individual_total_beh()
コード例 #19
0
def test_default():
    ts = TimeSeries(data=[(0, 0), (1, 2)])
    ts_no_default = ts.operation(ts, lambda a, b: a + b)
    assert ts_no_default.default == None

    ts_default = ts.operation(ts, lambda a, b: a + b, default=1)
    assert ts_default.default == 1

    ts_none = ts.operation(ts, lambda a, b: a + b, default=None)
    assert ts_none.default == None
コード例 #20
0
def test_scalar_ops():
    a = TimeSeries()
    a.set(datetime.datetime(2015, 3, 1), 1)
    a.set(datetime.datetime(2015, 3, 2), 0)
    a.set(datetime.datetime(2015, 3, 3), 3)
    a.set(datetime.datetime(2015, 3, 4), 2)

    ts_half = a.multiply(0.5)
    ts_bool = a.to_bool(invert=False)
    ts_threshold = a.threshold(value=1.1)

    # test before domain, should give default value
    assert ts_half[datetime.datetime(2015, 2, 24)] is None
    assert ts_bool[datetime.datetime(2015, 2, 24)] == None
    assert ts_threshold[datetime.datetime(2015, 2, 24)] == None

    # test values throughout series
    assert ts_half[datetime.datetime(2015, 3, 1, 6)] == 0.5
    assert ts_bool[datetime.datetime(2015, 3, 1, 6)] == True
    assert ts_threshold[datetime.datetime(2015, 3, 1, 6)] == False

    assert ts_half[datetime.datetime(2015, 3, 2, 6)] == 0
    assert ts_bool[datetime.datetime(2015, 3, 2, 6)] == False
    assert ts_threshold[datetime.datetime(2015, 3, 2, 6)] == False

    assert ts_half[datetime.datetime(2015, 3, 3, 6)] == 1.5
    assert ts_bool[datetime.datetime(2015, 3, 3, 6)] == True
    assert ts_threshold[datetime.datetime(2015, 3, 3, 6)] == True

    # test after domain, should give last value
    assert ts_half[datetime.datetime(2015, 3, 4, 18)] == 1
    assert ts_bool[datetime.datetime(2015, 3, 4, 18)] == True
    assert ts_threshold[datetime.datetime(2015, 3, 4, 18)] == True
コード例 #21
0
def test_distribution():
    ts = TimeSeries([[1, 1], [2, 0], [6, 1], [8, 0]],
                    domain=Domain([1, 2], [3, 5], [6, 8]))

    distribution = ts.distribution()
    assert distribution[0] == 2. / 5
    assert distribution[1] == 3. / 5

    distribution = ts.distribution(0, 9)
    assert distribution[0] == 2. / 5
    assert distribution[1] == 3. / 5
コード例 #22
0
def test_merge():
    ts_a = TimeSeries()
    ts_b = TimeSeries()
    # ts_a[0] = None
    ts_b[0] = True
    ts_merge = TimeSeries.merge([ts_a, ts_b])

    assert True in ts_merge[0]
    assert None in ts_merge[0]

    ts_c = TimeSeries.merge([])
    assert list(ts_c.items()) == []
コード例 #23
0
ファイル: test_traces.py プロジェクト: vishalbelsare/traces
def test_csv():
    def time_parse(value):
        return int(value)

    def value_parse(value):
        try:
            return int(value)
        except ValueError:
            return None

    filename = 'sample.csv'
    with open(filename, 'w') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(['hour', 'value'])
        writer.writerow(['10', '15'])
        writer.writerow(['11', '34'])
        writer.writerow(['12', '19'])
        writer.writerow(['13', 'nan'])
        writer.writerow(['14', '18'])
        writer.writerow(['15', 'nan'])

    ts = TimeSeries.from_csv(filename,
                             time_column=0,
                             time_transform=time_parse,
                             value_column=1,
                             value_transform=value_parse,
                             default=None)
    os.remove(filename)

    assert ts[9] is None
    assert ts[20] is None
    assert ts[13.5] is None

    histogram = ts.distribution()
    nose.tools.assert_almost_equal(histogram.mean(), (15 + 34 + 19 + 18) / 4.0)

    filename = 'sample.csv'
    with open(filename, 'w') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(['hour', 'value'])
        writer.writerow(['2000-01-01 10:00:00', '15'])
        writer.writerow(['2000-01-01 11:00:00', '34'])
        writer.writerow(['2000-01-01 12:00:00', '19'])
        writer.writerow(['2000-01-01 13:00:00', 'nan'])
        writer.writerow(['2000-01-01 14:00:00', '18'])
        writer.writerow(['2000-01-01 15:00:00', 'nan'])

    ts = TimeSeries.from_csv(filename)
    os.remove(filename)

    assert ts[datetime(2000, 1, 1, 9)] is None
    assert ts[datetime(2000, 1, 1, 10, 30)] == '15'
    assert ts[datetime(2000, 1, 1, 20)] == 'nan'
コード例 #24
0
def test_integer_times():

    # v. simple
    a = TimeSeries()
    a[0] = 1
    a[1] = 0
    a[3] = 1
    a[4] = 0

    distribution = a.distribution(start=0, end=6)

    assert distribution[0] == 2.0 / 3
    assert distribution[1] == 1.0 / 3
コード例 #25
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
コード例 #26
0
ファイル: test_operations.py プロジェクト: vlsd/traces
def test_scalar_ops():
    a = TimeSeries()
    a.set(datetime.datetime(2015, 3, 1), 1)
    a.set(datetime.datetime(2015, 3, 2), 0)
    a.set(datetime.datetime(2015, 3, 3), 3)
    a.set(datetime.datetime(2015, 3, 4), 2)

    ts_half = a.scale_by(0.5)
    ts_bool = a.to_bool(invert=False)
    ts_threshold = a.threshold(value=1.1)

    # test before domain, should give default value
    assert ts_half[datetime.datetime(2015, 2, 24)] == 0
    assert ts_bool[datetime.datetime(2015, 2, 24)] == False
    assert ts_threshold[datetime.datetime(2015, 2, 24)] == False

    # test values throughout series
    assert ts_half[datetime.datetime(2015, 3, 1, 6)] == 0.5
    assert ts_bool[datetime.datetime(2015, 3, 1, 6)] == True
    assert ts_threshold[datetime.datetime(2015, 3, 1, 6)] == False

    assert ts_half[datetime.datetime(2015, 3, 2, 6)] == 0
    assert ts_bool[datetime.datetime(2015, 3, 2, 6)] == False
    assert ts_threshold[datetime.datetime(2015, 3, 2, 6)] == False

    assert ts_half[datetime.datetime(2015, 3, 3, 6)] == 1.5
    assert ts_bool[datetime.datetime(2015, 3, 3, 6)] == True
    assert ts_threshold[datetime.datetime(2015, 3, 3, 6)] == True

    # test after domain, should give last value
    assert ts_half[datetime.datetime(2015, 3, 4, 18)] == True
    assert ts_bool[datetime.datetime(2015, 3, 4, 18)] == True
    assert ts_threshold[datetime.datetime(2015, 3, 4, 18)] == True
コード例 #27
0
ファイル: test_iterators.py プロジェクト: nivertech/traces
def test_slice():

    ts = TimeSeries(int)
    ts[0] = 1
    ts[1] = 5
    ts[4] = 0
    ts[6] = 2

    assert ts.slice(0.5, 2.5).items() == [(0.5, 1), (1, 5)]
    assert ts.slice(1.0, 2.5).items() == [(1.0, 5)]
    assert ts.slice(-1, 1).items() == [(-1, 1), (0, 1), (1, 5)]
    assert ts.slice(-1, 0.5).items() == [(-1, 1), (0, 1)]

    nose.tools.assert_raises(ValueError, ts.slice, 2.5, 0)
コード例 #28
0
def test_distribution_set():
    time_series = TimeSeries()
    time_series[1.2] = {'broccoli'}
    time_series[1.4] = {'broccoli', 'orange'}
    time_series[1.7] = {'broccoli', 'orange', 'banana'}
    time_series[2.2] = {'orange', 'banana'}
    time_series[3.5] = {'orange', 'banana', 'beets'}
コード例 #29
0
def example_dictlike():

    # test overwriting keys
    ts = TimeSeries()
    ts[datetime.datetime(2010, 1, 1)] = 5
    ts[datetime.datetime(2010, 1, 2)] = 4
    ts[datetime.datetime(2010, 1, 3)] = 3
    ts[datetime.datetime(2010, 1, 7)] = 2
    ts[datetime.datetime(2010, 1, 4)] = 1
    ts[datetime.datetime(2010, 1, 4)] = 10
    ts[datetime.datetime(2010, 1, 4)] = 5
    ts[datetime.datetime(2010, 1, 1)] = 1
    ts[datetime.datetime(2010, 1, 7)] = 1.2
    ts[datetime.datetime(2010, 1, 8)] = 1.3
    ts[datetime.datetime(2010, 1, 12)] = 1.3

    # do some wackiness with a bunch of points
    dt = datetime.datetime(2010, 1, 12)
    for i in range(1000):
        dt += datetime.timedelta(hours=random.random())
        ts[dt] = math.sin(i / float(math.pi))

    dt -= datetime.timedelta(hours=500)
    dt -= datetime.timedelta(minutes=30)
    for i in range(1000):
        dt += datetime.timedelta(hours=random.random())
        ts[dt] = math.cos(i / float(math.pi))

    # output the time series
    for i, j in ts:
        print(i.isoformat(), j)
コード例 #30
0
def test_remove_points_from_interval():
    ts = TimeSeries(default=0)
    ts[0] = 0
    ts[1] = 2
    ts[3] = 1
    ts[4] = 0

    assert ts[5] == 0

    del ts[3.5:4.5]

    assert ts[5] == 1

    ts[4] = 0

    del ts[3:4.5]

    assert ts[5] == 2

    ts[3] = 1
    ts[4] = 0

    del ts[3.5:4]

    assert ts[5] == 0
コード例 #31
0
def example_dictlike():

    # test overwriting keys
    l = TimeSeries()
    l[datetime.datetime(2010, 1, 1)] = 5
    l[datetime.datetime(2010, 1, 2)] = 4
    l[datetime.datetime(2010, 1, 3)] = 3
    l[datetime.datetime(2010, 1, 7)] = 2
    l[datetime.datetime(2010, 1, 4)] = 1
    l[datetime.datetime(2010, 1, 4)] = 10
    l[datetime.datetime(2010, 1, 4)] = 5
    l[datetime.datetime(2010, 1, 1)] = 1
    l[datetime.datetime(2010, 1, 7)] = 1.2
    l[datetime.datetime(2010, 1, 8)] = 1.3
    l[datetime.datetime(2010, 1, 12)] = 1.3

    # do some wackiness with a bunch of points
    dt = datetime.datetime(2010, 1, 12)
    for i in range(1000):
        dt += datetime.timedelta(hours=random.random())
        l[dt] = math.sin(i / float(math.pi))

    dt -= datetime.timedelta(hours=500)
    dt -= datetime.timedelta(minutes=30)
    for i in range(1000):
        dt += datetime.timedelta(hours=random.random())
        l[dt] = math.cos(i / float(math.pi))

    # what does this get?
    print >> sys.stderr, l[datetime.datetime(2010, 1, 3, 23, 59, 59)]

    # output the time series
    for i, j in l:
        print(i.isoformat(), j)
コード例 #32
0
ファイル: test_iterators.py プロジェクト: nivertech/traces
def test_merge():

    # since this is random, do it a bunch of times
    for n_trial in range(1000):

        # make a list of TimeSeries that is anywhere from 0 to 5
        # long. Each TimeSeries is of random length between 0 and 20,
        # with random time points and random values.
        ts_list = []
        for i in range(random.randint(1, 5)):
            ts_list.append(make_random_timeseries())

        method_a = list(TimeSeries.merge(ts_list, compact=False))
        method_b = list(TimeSeries.iter_merge(ts_list))

        msg = '%s != %s' % (pprint.pformat(method_a), pprint.pformat(method_b))
        assert method_a == method_b, msg
コード例 #33
0
def from_pandas(df, compact=True):
    '''TODO'''
    domain = Domain(df.index[0], df.index[-1])
    data = {col: TimeSeries(df[col].T, domain=domain) for col in df.columns}
    if compact:
        for val in data.values():
            val.compact()
    return data
コード例 #34
0
ファイル: test_distribution.py プロジェクト: vlsd/traces
def test_distribution():

    start_time = datetime.datetime(2015, 3, 1)

    # v. simple
    a = TimeSeries()
    a.set(start_time, 1)
    a.set(datetime.datetime(2015, 3, 2), 0)
    a.set(datetime.datetime(2015, 3, 3), 1)
    a.set(datetime.datetime(2015, 3, 4), 0)
    end_time = datetime.datetime(2015, 3, 5)

    # not normalized
    distribution = a.distribution(
        start_time=start_time, end_time=end_time, normalized=False,
    )
    assert distribution[0] == 24 * 60 * 60 * 2 # two days
    assert distribution[1] == 24 * 60 * 60 * 2

    # normalized
    distribution = a.distribution(start_time=start_time, end_time=end_time)
    assert distribution[0] == 0.5
    assert distribution[1] == 0.5
コード例 #35
0
ファイル: test_distribution.py プロジェクト: vlsd/traces
def test_default_values():

    # v. simple
    a = TimeSeries()
    a.set(datetime.datetime(2015, 3, 1), 1)
    a.set(datetime.datetime(2015, 3, 2), 0)
    a.set(datetime.datetime(2015, 3, 3), 1)
    a.set(datetime.datetime(2015, 3, 4), 0)

    # not normalized
    distribution = a.distribution()

    total = 24 * 60 * 60 * 3
    assert distribution[0] == 24 * 60 * 60 * 1 / float(total)
    assert distribution[1] == 24 * 60 * 60 * 2 / float(total)
コード例 #36
0
ファイル: test_distribution.py プロジェクト: vlsd/traces
def test_mask():

    start_time = datetime.datetime(2015, 3, 1)

    # v. simple
    a = TimeSeries()
    a.set(start_time, 1)
    a.set(datetime.datetime(2015, 3, 2), 0)
    a.set(datetime.datetime(2015, 3, 3), 1)
    a.set(datetime.datetime(2015, 3, 4), 0)
    end_time = datetime.datetime(2015, 3, 5)

    mask = TimeSeries()
    mask.set(datetime.datetime(2015, 3, 1), 1)
    mask.set(datetime.datetime(2015, 3, 3), 0)
    
    # not normalized
    distribution = a.distribution(
        start_time=start_time, end_time=end_time, normalized=False, mask=mask,
    )
    assert distribution[0] == 24 * 60 * 60 # one day
    assert distribution[1] == 24 * 60 * 60

    # normalized
    distribution = a.distribution(
        start_time=start_time, end_time=end_time, mask=mask,
    )
    assert distribution[0] == 0.5
    assert distribution[1] == 0.5
コード例 #37
0
ファイル: test_operations.py プロジェクト: vlsd/traces
def example_sum():

    a = TimeSeries()
    a.set(datetime.datetime(2015, 3, 1), 1)
    a.set(datetime.datetime(2015, 3, 2), 0)
    a.set(datetime.datetime(2015, 3, 3), 1)
    a.set(datetime.datetime(2015, 3, 5), 0)
    a.set(datetime.datetime(2015, 3, 6), 0)

    b = TimeSeries()
    b.set(datetime.datetime(2015, 3, 1), 0)
    b.set(datetime.datetime(2015, 3, 2, 12), 1)
    b.set(datetime.datetime(2015, 3, 3, 13, 13), 0)
    b.set(datetime.datetime(2015, 3, 4), 1)
    b.set(datetime.datetime(2015, 3, 5), 0)
    b.set(datetime.datetime(2015, 3, 5, 12), 1)
    b.set(datetime.datetime(2015, 3, 5, 19), 0)

    c = TimeSeries()
    c.set(datetime.datetime(2015, 3, 1, 17), 0)
    c.set(datetime.datetime(2015, 3, 1, 21), 1)
    c.set(datetime.datetime(2015, 3, 2, 13, 13), 0)
    c.set(datetime.datetime(2015, 3, 4, 18), 1)
    c.set(datetime.datetime(2015, 3, 5, 4), 0)

    # output the three time series
    for i, ts in enumerate([a, b, c]):

        for (t0, v0), (t1, v1) in ts.iterintervals(1):
            print t0.isoformat(), i
            print t1.isoformat(), i

        print ''

        for (t0, v0), (t1, v1) in ts.iterintervals(0):
            print t0.isoformat(), i
            print t1.isoformat(), i

        print ''

    # output the sum
    # for dt, i in sum([a, b, c]):
    #     print dt.isoformat(), i
    # print ''
    for dt, i in TimeSeries.from_many_sum([a, b, c]):
        print dt.isoformat(), i
コード例 #38
0
ファイル: test_operations.py プロジェクト: vlsd/traces
def test_sum():

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

    b = TimeSeries()
    b.set(datetime.datetime(2015, 3, 1), 0)
    b.set(datetime.datetime(2015, 3, 1, 12), 1)
    b.set(datetime.datetime(2015, 3, 2), 0)
    b.set(datetime.datetime(2015, 3, 2, 12), 1)
    b.set(datetime.datetime(2015, 3, 3), 0)

    c = TimeSeries()
    c.set(datetime.datetime(2015, 3, 1), 0)
    c.set(datetime.datetime(2015, 3, 1, 18), 1)
    c.set(datetime.datetime(2015, 3, 5), 0)

    ts_sum = TimeSeries.from_many_sum([a, b, c])

    # test before domain, should give default value
    assert ts_sum[datetime.datetime(2015, 2, 24)] == 0

    # test values throughout sum
    assert ts_sum[datetime.datetime(2015, 3, 1)] == 1
    assert ts_sum[datetime.datetime(2015, 3, 1, 6)] == 1
    assert ts_sum[datetime.datetime(2015, 3, 1, 12)] == 2
    assert ts_sum[datetime.datetime(2015, 3, 1, 13)] == 2
    assert ts_sum[datetime.datetime(2015, 3, 1, 17)] == 2
    assert ts_sum[datetime.datetime(2015, 3, 1, 18)] == 3
    assert ts_sum[datetime.datetime(2015, 3, 1, 19)] == 3
    assert ts_sum[datetime.datetime(2015, 3, 3)] == 2
    assert ts_sum[datetime.datetime(2015, 3, 4)] == 1
    assert ts_sum[datetime.datetime(2015, 3, 4, 18)] == 1
    assert ts_sum[datetime.datetime(2015, 3, 5)] == 0

    # test after domain, should give last value
    assert ts_sum[datetime.datetime(2015, 3, 6)] == 0

    baseline = 0
    for ts in [a, b, c]:
        for i, j in ts:
            print i.isoformat(), j + baseline
        print ''
        baseline += 1.2

    ts_sum = TimeSeries.from_many_sum([a, b, c])
    for i, j in ts_sum:
        print i.isoformat(), j + baseline