-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_build_features.py
105 lines (88 loc) · 4.38 KB
/
test_build_features.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import pytest
import pandas as pd
import numpy as np
from ramprate.build_features import _find_uptime
def test__find_uptime_start_and_end_nonzero():
dt_idx = pd.date_range(start="2020-01-01 00:00", periods=6, freq="h", tz="UTC")
data = [2, 2, 0, 0, 0, 2]
# downtime=True
# first zero after non-zero
shutdown = pd.to_datetime(["2020-01-01 02:00"], utc=True)
# last zero before non-zero
startup = pd.to_datetime(["2020-01-01 04:00"], utc=True)
expected = pd.DataFrame({"shutdown": shutdown, "startup": startup})
actual = _find_uptime(pd.Series(data, index=dt_idx), downtime=True)
pd.testing.assert_frame_equal(actual, expected)
# end points ('startup') are after start points ('shutdown')
assert actual.diff(axis=1)["startup"].dt.total_seconds().fillna(1).ge(0).all()
# downtime=False
# last zero before non-zero
startup = pd.to_datetime([pd.NaT, "2020-01-01 04:00"], utc=True)
# first zero after non-zero
shutdown = pd.to_datetime(["2020-01-01 02:00", pd.NaT], utc=True)
expected = pd.DataFrame({"startup": startup, "shutdown": shutdown})
actual = _find_uptime(pd.Series(data, index=dt_idx))
pd.testing.assert_frame_equal(actual, expected)
# end points ('shutdown') are after start points ('startup')
assert actual.diff(axis=1)["shutdown"].dt.total_seconds().fillna(1).ge(0).all()
def test__find_uptime_all_zeros():
dt_idx = pd.date_range(start="2020-01-01 00:00", periods=6, freq="h", tz="UTC")
data = [0, 0, 0, 0, 0, 0]
# downtime=True
# first zero after non-zero
shutdown = pd.to_datetime([pd.NaT], utc=True)
# last zero before non-zero
startup = pd.to_datetime([pd.NaT], utc=True)
expected = pd.DataFrame({"shutdown": shutdown, "startup": startup})
actual = _find_uptime(pd.Series(data, index=dt_idx), downtime=True)
pd.testing.assert_frame_equal(actual, expected)
# downtime=False
# first zero after non-zero
shutdown = pd.to_datetime([], utc=True)
# last zero before non-zero
startup = pd.to_datetime([], utc=True)
expected = pd.DataFrame({"startup": startup, "shutdown": shutdown})
actual = _find_uptime(pd.Series(data, index=dt_idx))
pd.testing.assert_frame_equal(actual, expected)
def test__find_uptime_no_zeros():
dt_idx = pd.date_range(start="2020-01-01 00:00", periods=6, freq="h", tz="UTC")
data = [5, 5, 5, 5, 5, 5]
# downtime=True
# first zero after non-zero
shutdown = pd.to_datetime([], utc=True)
# last zero before non-zero
startup = pd.to_datetime([], utc=True)
expected = pd.DataFrame({"shutdown": shutdown, "startup": startup})
actual = _find_uptime(pd.Series(data, index=dt_idx), downtime=True)
pd.testing.assert_frame_equal(actual, expected)
# downtime=False
# first zero after non-zero
shutdown = pd.to_datetime([pd.NaT], utc=True)
# last zero before non-zero
startup = pd.to_datetime([pd.NaT], utc=True)
expected = pd.DataFrame({"startup": startup, "shutdown": shutdown})
actual = _find_uptime(pd.Series(data, index=dt_idx))
pd.testing.assert_frame_equal(actual, expected)
def test__find_uptime_start_zero_end_zero():
dt_idx = pd.date_range(start="2020-01-01 00:00", periods=6, freq="h", tz="UTC")
data = [0, 2, 2, 0, 2, 0]
# downtime=True
# first zero after non-zero
shutdown = pd.to_datetime([pd.NaT, "2020-01-01 03:00", "2020-01-01 05:00"], utc=True)
# last zero before non-zero
startup = pd.to_datetime(["2020-01-01 00:00", "2020-01-01 03:00", pd.NaT], utc=True)
expected = pd.DataFrame({"shutdown": shutdown, "startup": startup})
actual = _find_uptime(pd.Series(data, index=dt_idx), downtime=True)
pd.testing.assert_frame_equal(actual, expected)
# end points ('startup') are after start points ('shutdown')
assert actual.diff(axis=1)["startup"].dt.total_seconds().fillna(1).ge(0).all()
# downtime=False
# last zero before non-zero
startup = pd.to_datetime(["2020-01-01 00:00", "2020-01-01 03:00"], utc=True)
# first zero after non-zero
shutdown = pd.to_datetime(["2020-01-01 03:00", "2020-01-01 05:00"], utc=True)
expected = pd.DataFrame({"startup": startup, "shutdown": shutdown})
actual = _find_uptime(pd.Series(data, index=dt_idx))
pd.testing.assert_frame_equal(actual, expected)
# end points ('shutdown') are after start points ('startup')
assert actual.diff(axis=1)["shutdown"].dt.total_seconds().fillna(1).ge(0).all()