Repository navigation
Expand file tree
/
Copy pathtest_intervals_tuple.py
More file actions
282 lines (240 loc) · 13 KB
/
Copy pathtest_intervals_tuple.py
File metadata and controls
282 lines (240 loc) · 13 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
from unittest import TestCase
import numpy as np
import pytest
from numpy.testing import assert_array_equal
from foapy import binding, chain_mode
from foapy.core import intervals_chain, intervals_tuple, tuple_mode
class TestIntervalsTuple(TestCase):
"""
Test intervals_tuple(chain, tuple_mode) -> ndarray.
Verifies that boundary handling (lossy / normal / redundant) is applied
correctly to chains produced by intervals_chain.
"""
# -------------------------------------------------------------------------
# Empty chain — all modes return empty array
# -------------------------------------------------------------------------
def test_empty_normal(self):
chain = np.array([], dtype=np.intp)
assert_array_equal(
intervals_tuple(chain, binding.start, tuple_mode.normal),
np.array([], dtype=np.intp),
)
def test_empty_normal_end(self):
chain = np.array([], dtype=np.intp)
assert_array_equal(
intervals_tuple(chain, binding.end, tuple_mode.normal),
np.array([], dtype=np.intp),
)
def test_empty_lossy(self):
chain = np.array([], dtype=np.intp)
assert_array_equal(
intervals_tuple(chain, binding.start, tuple_mode.lossy),
np.array([], dtype=np.intp),
)
def test_empty_lossy_end(self):
chain = np.array([], dtype=np.intp)
assert_array_equal(
intervals_tuple(chain, binding.end, tuple_mode.lossy),
np.array([], dtype=np.intp),
)
def test_empty_redundant(self):
chain = np.array([], dtype=np.intp)
assert_array_equal(
intervals_tuple(chain, binding.start, tuple_mode.redundant),
np.array([], dtype=np.intp),
)
def test_empty_redundant_end(self):
chain = np.array([], dtype=np.intp)
assert_array_equal(
intervals_tuple(chain, binding.end, tuple_mode.redundant),
np.array([], dtype=np.intp),
)
# -------------------------------------------------------------------------
# Invalid tuple_mode raises ValueError
# -------------------------------------------------------------------------
def test_invalid_tuple_mode_raises(self):
chain = np.array([1, 2, 2], dtype=np.intp)
with pytest.raises(ValueError):
intervals_tuple(chain, binding.start, 99)
# -------------------------------------------------------------------------
# tuple_mode.normal — returns chain unchanged
# -------------------------------------------------------------------------
def test_normal_boundary_start(self):
X = ["b", "a", "b", "c", "b"]
chain = intervals_chain(X, binding.start, chain_mode.boundary)
result = intervals_tuple(chain, binding.start, tuple_mode.normal)
assert_array_equal(result, chain)
def test_normal_boundary_end(self):
X = ["b", "a", "b", "c", "b"]
chain = intervals_chain(X, binding.end, chain_mode.boundary)
result = intervals_tuple(chain, binding.end, tuple_mode.normal)
assert_array_equal(result, chain)
def test_normal_cycle_start(self):
X = ["b", "a", "b", "c", "b"]
chain = intervals_chain(X, binding.start, chain_mode.cycle)
result = intervals_tuple(chain, binding.start, tuple_mode.normal)
assert_array_equal(result, chain)
def test_normal_cycle_end(self):
X = ["b", "a", "b", "c", "b"]
chain = intervals_chain(X, binding.end, chain_mode.cycle)
result = intervals_tuple(chain, binding.end, tuple_mode.normal)
assert_array_equal(result, chain)
# -------------------------------------------------------------------------
# tuple_mode.lossy — removes boundary (first/last occurrence) intervals
# -------------------------------------------------------------------------
def test_lossy_boundary_start_mixed(self):
# chain = [1, 2, 2, 4, 2]; boundary at positions {0,1,3}; non-boundary = [2, 2]
X = ["b", "a", "b", "c", "b"]
chain = intervals_chain(X, binding.start, chain_mode.boundary)
result = intervals_tuple(chain, binding.start, tuple_mode.lossy)
# Only interior intervals: b's second interval (2) and b's third interval (2)
assert_array_equal(result, np.array([2, 2], dtype=np.intp))
def test_lossy_boundary_start_integers(self):
# X = [2, 4, 2, 2, 4]; chain = [1, 2, 2, 1, 3]
# boundary at positions {0, 1} (chain[i] > i)
# Wait: boundary = chain[i] > i:
# i=0: 1>0 T, i=1: 2>1 T, i=2: 2>2 F, i=3: 1>3 F, i=4: 3>4 F
# boundary at {0, 1}; non-boundary = [2, 1, 3] at positions {2, 3, 4}
X = [2, 4, 2, 2, 4]
chain = intervals_chain(X, binding.start, chain_mode.boundary)
result = intervals_tuple(chain, binding.start, tuple_mode.lossy)
assert_array_equal(result, np.array([2, 1, 3], dtype=np.intp))
def test_lossy_boundary_end_mixed(self):
# chain = [2, 4, 2, 2, 1]; boundary (chain[i] > n-1-i = 4,3,2,1,0):
# i=0: 2>4 F, i=1: 4>3 T, i=2: 2>2 F, i=3: 2>1 T, i=4: 1>0 T
# boundary at {1,3,4}; non-boundary at {0,2} → values [2, 2]
X = ["b", "a", "b", "c", "b"]
chain = intervals_chain(X, binding.end, chain_mode.boundary)
result = intervals_tuple(chain, binding.end, tuple_mode.lossy)
assert_array_equal(result, np.array([2, 2], dtype=np.intp))
def test_lossy_all_unique_start(self):
# All elements unique — all intervals are boundary intervals → lossy = empty
X = [1, 2, 3, 4, 5]
chain = intervals_chain(X, binding.start, chain_mode.boundary)
result = intervals_tuple(chain, binding.start, tuple_mode.lossy)
assert_array_equal(result, np.array([], dtype=np.intp))
def test_lossy_all_unique_end(self):
# binding.end: chain = [5,4,3,2,1]; all boundary → lossy = empty
X = [1, 2, 3, 4, 5]
chain = intervals_chain(X, binding.end, chain_mode.boundary)
result = intervals_tuple(chain, binding.end, tuple_mode.lossy)
assert_array_equal(result, np.array([], dtype=np.intp))
def test_lossy_all_identical_start(self):
# X = ["a","a","a"]; chain = [1,1,1]; boundary at {0}; non-boundary = [1,1]
X = ["a", "a", "a"]
chain = intervals_chain(X, binding.start, chain_mode.boundary)
result = intervals_tuple(chain, binding.start, tuple_mode.lossy)
assert_array_equal(result, np.array([1, 1], dtype=np.intp))
def test_lossy_all_identical_end(self):
# binding.end: chain = [1,1,1]; same structure → non-boundary = [1,1]
X = ["a", "a", "a"]
chain = intervals_chain(X, binding.end, chain_mode.boundary)
result = intervals_tuple(chain, binding.end, tuple_mode.lossy)
assert_array_equal(result, np.array([1, 1], dtype=np.intp))
def test_lossy_boundary_end_integers(self):
# X = [2,4,2,2,4]; chain (end) = [2,3,1,2,1]
# reversed chain for boundary detection: [1,2,1,3,2]
# boundary (chain[i] > i): i=0:1>0 T, i=1:2>1 T, i=2:1>2 F, i=3:3>3 F, i=4:2>4 F
# non-boundary at {2,3,4} → [1,2,1] in reversed order → [1,3,2] in original
X = [2, 4, 2, 2, 4]
chain = intervals_chain(X, binding.end, chain_mode.boundary)
result = intervals_tuple(chain, binding.end, tuple_mode.lossy)
assert_array_equal(result, np.array([1, 3, 2], dtype=np.intp))
def test_lossy_single_element_start(self):
# Single occurrence is always a boundary interval → lossy removes it → []
X = ["a"]
chain = intervals_chain(X, binding.start, chain_mode.boundary)
result = intervals_tuple(chain, binding.start, tuple_mode.lossy)
assert_array_equal(result, np.array([], dtype=np.intp))
def test_lossy_single_element_end(self):
X = ["a"]
chain = intervals_chain(X, binding.end, chain_mode.boundary)
result = intervals_tuple(chain, binding.end, tuple_mode.lossy)
assert_array_equal(result, np.array([], dtype=np.intp))
# -------------------------------------------------------------------------
# tuple_mode.redundant — both boundary intervals included
# -------------------------------------------------------------------------
def test_redundant_boundary_start_mixed(self):
# chain = [1, 2, 2, 4, 2]
# boundary at {0,1,3}; non-boundary at {2,4}: prev_pos = [2-2, 4-2] = [0, 2]
# is_prev = [T, F, T, F, F]; last_mask = [F, T, F, T, T]; last_pos = [1, 3, 4]
# trailing = 5 - [1, 3, 4] = [4, 2, 1]
# result = [1, 2, 2, 4, 2, 4, 2, 1]
# [1, 2, 2, 4, 2] -> [1, 2, 2, 4, 2, 1, 4, 2]
X = ["b", "a", "b", "c", "b"]
chain = intervals_chain(X, binding.start, chain_mode.boundary)
result = intervals_tuple(chain, binding.start, tuple_mode.redundant)
assert_array_equal(np.sort(result), np.sort(np.array([1, 2, 2, 4, 2, 1, 4, 2])))
def test_redundant_boundary_end_mixed(self):
# chain = [2, 4, 2, 2, 1]
# boundary_end at {1,3,4}; non-boundary at {0,2}: next_pos = [0+2, 2+2] = [2, 4]
# is_next = [F, F, T, F, T]; first_mask = [T, T, F, T, F]; first_pos = [0, 1, 3]
# leading = [1, 2, 4]
# result = [2, 4, 2, 2, 1, 1, 2, 4]
# [2, 4, 2, 2, 1] -> [2, 4, 1, 2, 4, 2, 2, 1]
X = ["b", "a", "b", "c", "b"]
chain = intervals_chain(X, binding.end, chain_mode.boundary)
result = intervals_tuple(chain, binding.end, tuple_mode.redundant)
assert_array_equal(np.sort(result), np.sort(np.array([2, 4, 1, 2, 4, 2, 2, 1])))
def test_redundant_all_unique_start(self):
# X = [1,2,3,4,5]; chain = [1,2,3,4,5]; all boundary; no non-boundary
# last_mask = all True; last_pos = [0,1,2,3,4]; trailing = [5,4,3,2,1]
# result = [1,2,3,4,5, 5,4,3,2,1]
X = [1, 2, 3, 4, 5]
chain = intervals_chain(X, binding.start, chain_mode.boundary)
result = intervals_tuple(chain, binding.start, tuple_mode.redundant)
assert_array_equal(
result, np.array([1, 2, 3, 4, 5, 5, 4, 3, 2, 1], dtype=np.intp)
)
def test_redundant_all_unique_end(self):
# binding.end: chain = [5,4,3,2,1]; all boundary; trailing = [1,2,3,4,5]
# result sorted = [1,1,2,2,3,3,4,4,5,5]
X = [1, 2, 3, 4, 5]
chain = intervals_chain(X, binding.end, chain_mode.boundary)
result = intervals_tuple(chain, binding.end, tuple_mode.redundant)
assert_array_equal(
np.sort(result), np.array([1, 1, 2, 2, 3, 3, 4, 4, 5, 5], dtype=np.intp)
)
def test_redundant_all_identical_start(self):
# X = ["a","a","a"]; chain = [1,1,1]; boundary at {0}; non-boundary at {1,2}
# prev_pos = [1-1, 2-1] = [0, 1]; is_prev = [T, T, F]
# last_mask = [F, F, T]; last_pos = [2]; trailing = [1]
# result = [1,1,1, 1]
X = ["a", "a", "a"]
chain = intervals_chain(X, binding.start, chain_mode.boundary)
result = intervals_tuple(chain, binding.start, tuple_mode.redundant)
assert_array_equal(result, np.array([1, 1, 1, 1], dtype=np.intp))
def test_redundant_all_identical_end(self):
# binding.end: chain = [1,1,1]; same structure → result = [1,1,1,1]
X = ["a", "a", "a"]
chain = intervals_chain(X, binding.end, chain_mode.boundary)
result = intervals_tuple(chain, binding.end, tuple_mode.redundant)
assert_array_equal(result, np.array([1, 1, 1, 1], dtype=np.intp))
def test_redundant_single_element(self):
# Single-element: only one occurrence — it's both first and last
# X = ["a"]; chain = [1]; boundary at {0} (1>0); non-boundary = empty
# last_mask: is_prev all False → last_mask all True → trailing = [1]
# result = [1, 1]
X = ["a"]
chain = intervals_chain(X, binding.start, chain_mode.boundary)
result = intervals_tuple(chain, binding.start, tuple_mode.redundant)
assert_array_equal(result, np.array([1, 1], dtype=np.intp))
def test_redundant_single_element_end(self):
# X = ["a"]; chain = [1] (same); binding.end → same chain
X = ["a"]
chain = intervals_chain(X, binding.end, chain_mode.boundary)
result = intervals_tuple(chain, binding.end, tuple_mode.redundant)
assert_array_equal(result, np.array([1, 1], dtype=np.intp))
# -------------------------------------------------------------------------
# Single-element sequence produces correct size
# -------------------------------------------------------------------------
def test_length_normal_equals_n(self):
X = [2, 4, 2, 2, 4]
chain = intervals_chain(X, binding.start, chain_mode.boundary)
result = intervals_tuple(chain, binding.start, tuple_mode.normal)
self.assertEqual(len(result), len(X))
def test_length_redundant_equals_n_plus_k(self):
X = [2, 4, 2, 2, 4] # 2 unique elements
chain = intervals_chain(X, binding.start, chain_mode.boundary)
result = intervals_tuple(chain, binding.start, tuple_mode.redundant)
self.assertEqual(len(result), len(X) + 2)