-
Notifications
You must be signed in to change notification settings - Fork 79
/
test_intervals.py
188 lines (132 loc) · 5.45 KB
/
test_intervals.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import pandas as pd
from traffic.core.intervals import Interval, IntervalCollection
dates = pd.date_range("2022-03-21 11:10", "2022-03-21 11:20", freq="1 min")
h0, h1, h2, h3, h4, h5, h6, h7, h8, *_ = dates
i1 = Interval(h0, h2)
i2 = Interval(h3, h6)
i3 = Interval(h6, h8)
i4 = Interval(h1, h5)
i5 = Interval(h0, h8)
c1 = IntervalCollection(start=[h0, h1], stop=[h2, h5])
c2 = IntervalCollection(start=[h1, h0], stop=[h5, h2])
c3 = IntervalCollection(start=[h1, h0], stop=[h5, h8])
c4 = IntervalCollection(start=[h0, h1], stop=[h8, h5])
c5 = IntervalCollection(start=[h0, h3], stop=[h2, h6])
c6 = IntervalCollection(start=[h3, h0], stop=[h6, h2])
c7 = IntervalCollection(start=[h7], stop=[h8])
class TestInterval:
def test_eq(self) -> None:
assert i1 is not None
assert i1 == i1
assert i1 != i2
assert i1 != []
def test_duration(self) -> None:
assert i1.duration() == pd.Timedelta("2 minutes")
class TestIntervalAdd:
def test_partialsup(self) -> None:
assert i1 + i4 == IntervalCollection(start=[h0, h1], stop=[h2, h5])
def test_totalsup(self) -> None:
assert i2 + i5 == IntervalCollection(start=[h0, h3], stop=[h8, h6])
def test_nosup(self) -> None:
assert i1 + i2 == IntervalCollection(start=[h0, h3], stop=[h2, h6])
class TestIntervalSubtract:
def test_partialsupleft(self) -> None:
assert i1 - i4 == IntervalCollection(h0, h1)
def test_partialsupright(self) -> None:
assert i2 - i4 == IntervalCollection(h5, h6)
def test_totalsup(self) -> None:
assert i2 - i5 is None
def test_partialsupmiddle(self) -> None:
assert i5 - i2 == IntervalCollection(start=[h0, h6], stop=[h3, h8])
def test_nosup_left(self) -> None:
assert i1 - i2 == IntervalCollection(i1)
def test_nosup_right(self) -> None:
assert i2 - i1 == IntervalCollection(i2)
def test_equal(self) -> None:
assert i1 - i1 is None
class TestIntervalUnion:
def test_partialsupleft(self) -> None:
assert i1 | i4 == IntervalCollection(h0, h5)
def test_partialsupright(self) -> None:
assert i2 | i4 == IntervalCollection(h1, h6)
def test_totalsup(self) -> None:
assert i2 | i5 == IntervalCollection(h0, h8)
def test_partialsupmiddle(self) -> None:
assert i5 | i2 == IntervalCollection(h0, h8)
def test_nosup_left(self) -> None:
assert i1 | i2 == IntervalCollection(start=[h0, h3], stop=[h2, h6])
def test_nosup_right(self) -> None:
assert i2 | i1 == IntervalCollection(start=[h0, h3], stop=[h2, h6])
def test_equal(self) -> None:
assert i1 | i1 == IntervalCollection(h0, h2)
class TestIntervalIntersection:
def test_partialsupleft(self) -> None:
assert i1 & i4 == Interval(h1, h2)
def test_partialsupright(self) -> None:
assert i2 & i4 == Interval(h3, h5)
def test_totalsup(self) -> None:
assert i2 & i5 == Interval(h3, h6)
def test_partialsupmiddle(self) -> None:
assert i5 & i2 == Interval(h3, h6)
def test_nosup_left(self) -> None:
assert i1 & i2 is None
def test_nosup_right(self) -> None:
assert i2 & i1 is None
def test_equal(self) -> None:
assert i1 & i1 == Interval(h0, h2)
class TestCollection:
def test_duration_simple(self) -> None:
assert c7.total_duration() == pd.Timedelta(seconds=60)
def test_consolidate(self) -> None:
assert c1.consolidate() == IntervalCollection(h0, h5)
assert c2.consolidate() == IntervalCollection(h0, h5)
assert c3.consolidate() == IntervalCollection(h0, h8)
assert c5.consolidate() == c5
assert c6.consolidate() == c6
other = IntervalCollection(i1, i2, i3, i4)
res = other.consolidate()
assert res is not None
assert next(iter(res)) == Interval(h0, h8)
class TestCollectionAdd:
def test_same(self) -> None:
assert c1 + c1 == (
IntervalCollection(start=[h0, h0, h1, h1], stop=[h2, h2, h5, h5])
)
def test_diffsup(self) -> None:
assert c1 + c4 == (
IntervalCollection(start=[h0, h0, h1, h1], stop=[h2, h8, h5, h5])
)
def test_diffnosup(self) -> None:
assert c1 + c7 == IntervalCollection([h0, h1, h7], [h2, h5, h8])
class TestCollectionSub:
def test_same(self) -> None:
assert c1 == c1
assert c1 - c1 is None
def test_partminustot(self) -> None:
assert c1 - c4 is None
def test_smthminusinv(self) -> None:
assert c5 - c6 is None
def test_supleft(self) -> None:
assert c3 - c2 == IntervalCollection(h5, h8)
def test_supright(self) -> None:
assert c3 - c7 == IntervalCollection(h0, h7)
def test_hole(self) -> None:
assert c6 - c2 == IntervalCollection(h5, h6)
assert c6 - IntervalCollection(
start=[h0, h4], stop=[h1, h5]
) == IntervalCollection(
Interval(h1, h2), Interval(h3, h4), Interval(h5, h6)
)
def test_nosup(self) -> None:
# no overlap between ic01 and ic07, nothing to substract
assert c1 - c7 == c1.consolidate()
class TestCollectionUnion:
def test_sup(self) -> None:
assert c1 | c2 == IntervalCollection(h0, h5)
def test_nosup(self) -> None:
assert c5 | c7 == (c5 + c7)
class TestCollectionIntersection:
def test_sup(self) -> None:
assert c1 & c2 == IntervalCollection(h0, h5)
def test_nosup(self) -> None:
assert c5 & c7 is None