blob: 9298792beb74a3a3bc28e26c03aa3ccdec470c0e [file] [log] [blame]
koder aka kdanilova732a602017-02-01 20:29:56 +02001import numpy
2from wally.statistic import rebin_histogram
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +03003from wally.result_classes import DataSource, TimeSeries
kdanylov aka koder45183182017-04-30 23:55:40 +03004from wally.data_selectors import c_interpolate_ts_on_seconds_border
5from wally.utils import unit_conversion_coef
koder aka kdanilova732a602017-02-01 20:29:56 +02006
7
8def array_eq(x: numpy.array, y: numpy.array, max_diff: float = 1E-3) -> bool:
9 return numpy.abs(x - y).max() <= max_diff
10
11
kdanylov aka koder45183182017-04-30 23:55:40 +030012def test_conversion_coef():
13 units = [
14 ('x', 'mx', 1000),
15 ('Gx', 'Kx', 1000 ** 2),
16 ('Gx', 'x', 1000 ** 3),
17 ('x', 'Kix', 1.0 / 1024),
18 ('x', 'Mix', 1.0 / 1024 ** 2),
19 ('mx', 'Mix', 0.001 / 1024 ** 2),
20 ('Mix', 'Kix', 1024),
21 ('Kix', 'ux', 1024 * 1000 ** 2),
22 ]
23
24 for unit1, unit2, coef in units:
25 cc = float(unit_conversion_coef(unit1, unit2))
26 assert abs(cc / coef - 1) < 1E-5, "{} => {} == {}".format(unit1, unit2, cc)
27 rcc = float(unit_conversion_coef(unit2, unit1))
28 assert abs(rcc * cc - 1) < 1E-5, "{} => {} == {}".format(unit1, unit2, rcc)
29
30
koder aka kdanilova732a602017-02-01 20:29:56 +020031def test_rebin_histo():
32 curr_histo = numpy.empty((100,), dtype=int)
33 curr_histo[:] = 1
34 edges = numpy.arange(100)
35 new_histo, new_edges = rebin_histogram(curr_histo, edges, 10)
36
37 assert new_edges.shape == (10,)
38 assert new_histo.shape == (10,)
39 assert new_edges.dtype.name.startswith('float')
40 assert new_histo.dtype.name.startswith('int')
41
42 assert array_eq(new_edges, numpy.arange(10) * 9.9)
43 assert new_histo.sum() == curr_histo.sum()
44 assert list(new_histo) == [10] * 10
45
46 new_histo, new_edges = rebin_histogram(curr_histo, edges, 3,
47 left_tail_idx=20,
48 right_tail_idx=50)
49
50 assert new_edges.shape == (3,)
51 assert new_histo.shape == (3,)
52 assert array_eq(new_edges, numpy.array([20, 30, 40]))
53 assert new_histo.sum() == curr_histo.sum()
54 assert list(new_histo) == [30, 10, 60]
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +030055
56
57SUITE_ID = "suite1"
58JOB_ID = "job1"
59NODE_ID = "node1"
60SENSOR = "sensor"
61DEV = "dev"
62METRIC = "metric"
63TAG = "csv"
64DATA_UNITS = "x"
65TIME_UNITS = "ms"
66
67
68def test_interpolate():
69 ds = DataSource(node_id=NODE_ID, sensor=SENSOR, dev=DEV, metric=METRIC)
70 samples = 200
71 ms_coef = 1000
72 s_offset = 377 * ms_coef
73 ms_offset = 300 + s_offset
kdanylov aka koder45183182017-04-30 23:55:40 +030074 borders = 10
75 block_size = 20
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +030076
77 for i in range(16):
78 source_times = numpy.random.randint(100, size=samples, dtype='uint64') + \
79 ms_coef * numpy.arange(samples, dtype='uint64') + s_offset + ms_offset
80 source_values = numpy.random.randint(30, 60, size=samples, dtype='uint64')
81
82 ts = TimeSeries("test", raw=None, data=source_values, times=source_times, units=DATA_UNITS,
83 source=ds, time_units=TIME_UNITS)
84
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +030085 ts2 = c_interpolate_ts_on_seconds_border(ts, nc=True)
86
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +030087 assert ts.time_units == 'ms'
88 assert ts2.time_units == 's'
89 assert ts2.times.dtype == ts.times.dtype
90 assert ts2.data.dtype == ts.data.dtype
91
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +030092 for begin_idx in numpy.random.randint(borders, samples - borders, size=20):
93 begin_idx = int(begin_idx)
94 end_idx = min(begin_idx + block_size, ts.times.size - 1)
95
96 first_cell_begin_time = ts.times[begin_idx - 1]
97 last_cell_end_time = ts.times[end_idx]
98 ts_sum = ts.data[begin_idx:end_idx].sum()
99
100 ts2_begin_idx = numpy.searchsorted(ts2.times, first_cell_begin_time // ms_coef)
101 ts2_end_idx = numpy.searchsorted(ts2.times, last_cell_end_time // ms_coef) + 1
102 ts2_max = ts.data[ts2_begin_idx: ts2_end_idx].sum()
103 ts2_min = ts.data[ts2_begin_idx + 1: ts2_end_idx - 1].sum()
104
105 assert ts2_min <= ts_sum <= ts2_max, "NOT {} <= {} <= {}".format(ts2_min, ts_sum, ts2_max)
kdanylov aka koder45183182017-04-30 23:55:40 +0300106
107
108def test_interpolate_qd():
109 ds = DataSource(node_id=NODE_ID, sensor=SENSOR, dev=DEV, metric=METRIC)
110 samples = 200
111 ms_coef = 1000
112 s_offset = 377 * ms_coef
113 ms_offset = 300 + s_offset
114
115 for i in range(16):
116 source_times = numpy.random.randint(100, size=samples, dtype='uint64') + \
117 ms_coef * numpy.arange(samples, dtype='uint64') + s_offset + ms_offset
118 source_values = numpy.random.randint(30, 60, size=samples, dtype='uint64')
119
120 ts = TimeSeries("test", raw=None, data=source_values, times=source_times, units=DATA_UNITS,
121 source=ds, time_units=TIME_UNITS)
122
123 ts2 = c_interpolate_ts_on_seconds_border(ts, nc=True, qd=True)
124
125 assert ts.time_units == 'ms'
126 assert ts2.time_units == 's'
127 assert ts2.times.dtype == ts.times.dtype
128 assert ts2.data.dtype == ts.data.dtype
129 assert ts2.data.size == ts2.times.size
130 assert abs(ts2.data.size - ts.data.size) <= 1
131
132 coef = unit_conversion_coef(ts2.time_units, ts.time_units)
133 assert isinstance(coef, int)
134
135 dtime = (ts2.times[1] - ts2.times[0]) * coef // 2
136
137 idxs = numpy.searchsorted(ts.times, ts2.times * coef - dtime)
138 assert (ts2.data == ts.data[idxs]).all()