Skip to content

Commit be1e193

Browse files
committed
Add tests for merge_neighbors()
1 parent 6228bb1 commit be1e193

File tree

1 file changed

+201
-9
lines changed

1 file changed

+201
-9
lines changed

test/intervaltree_methods/restructure_test.py

+201-9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
import pickle
2929

3030

31+
# -----------------------------------------------------------------------------
32+
# REMOVAL
33+
# -----------------------------------------------------------------------------
3134
def test_emptying_partial():
3235
t = IntervalTree.from_tuples(data.ivs1.data)
3336
assert t[7:]
@@ -53,11 +56,13 @@ def test_remove_overlap():
5356
t.verify()
5457

5558

59+
# -----------------------------------------------------------------------------
60+
# MERGE_OVERLAPS
61+
# -----------------------------------------------------------------------------
5662
def test_merge_overlaps_empty():
5763
t = IntervalTree()
5864
t.merge_overlaps()
5965
t.verify()
60-
6166
assert len(t) == 0
6267

6368

@@ -76,9 +81,9 @@ def test_merge_overlaps_gapless():
7681
assert len(t) == 1
7782
assert t.pop() == rng
7883

84+
7985
def test_merge_overlaps_with_gap():
8086
t = IntervalTree.from_tuples(data.ivs1.data)
81-
8287
t.merge_overlaps()
8388
t.verify()
8489
assert len(t) == 2
@@ -94,7 +99,7 @@ def reducer(old, new):
9499
e.verify()
95100
assert not e
96101

97-
# One Interval in tree
102+
# one Interval in tree
98103
o = IntervalTree.from_tuples([(1, 2, 'hello')])
99104
o.merge_overlaps(data_reducer=reducer)
100105
o.verify()
@@ -107,7 +112,7 @@ def reducer(old, new):
107112
t.verify()
108113
assert len(t) == 2
109114
assert sorted(t) == [
110-
Interval(1, 2,'[1,2)'),
115+
Interval(1, 2, '[1,2)'),
111116
Interval(4, 15, '[4,7), [5,9), [6,10), [8,10), [8,15), [10,12), [12,14), [14,15)')
112117
]
113118

@@ -121,7 +126,7 @@ def reducer(old, new):
121126
e.verify()
122127
assert not e
123128

124-
# One Interval in tree
129+
# one Interval in tree
125130
o = IntervalTree.from_tuples([(1, 2, 'hello')])
126131
o.merge_overlaps(data_reducer=reducer, data_initializer=[])
127132
o.verify()
@@ -148,11 +153,13 @@ def reducer(old, new):
148153
]
149154

150155

156+
# -----------------------------------------------------------------------------
157+
# MERGE_EQUALS
158+
# -----------------------------------------------------------------------------
151159
def test_merge_equals_empty():
152160
t = IntervalTree()
153161
t.merge_equals()
154162
t.verify()
155-
156163
assert len(t) == 0
157164

158165

@@ -163,7 +170,6 @@ def test_merge_equals_wo_dupes():
163170

164171
t.merge_equals()
165172
t.verify()
166-
167173
assert orig == t
168174

169175

@@ -213,7 +219,7 @@ def reducer(old, new):
213219
e.verify()
214220
assert not e
215221

216-
# One Interval in tree, no change
222+
# one Interval in tree, no change
217223
o = IntervalTree.from_tuples([(1, 2, 'hello')])
218224
o.merge_equals(data_reducer=reducer)
219225
o.verify()
@@ -250,7 +256,7 @@ def reducer(old, new):
250256
e.verify()
251257
assert not e
252258

253-
# One Interval in tree, no change
259+
# one Interval in tree, no change
254260
o = IntervalTree.from_tuples([(1, 2, 'hello')])
255261
o.merge_equals(data_reducer=reducer, data_initializer=[])
256262
o.verify()
@@ -279,6 +285,183 @@ def reducer(old, new):
279285
assert t.containsi(4, 7, ['[4,7)', 'foo'])
280286

281287

288+
# ------------------------------------------------------------------------------
289+
# MERGE_NEIGHBORS
290+
# ------------------------------------------------------------------------------
291+
def test_merge_neighbors_empty():
292+
t = IntervalTree()
293+
t.merge_neighbors()
294+
t.verify()
295+
assert len(t) == 0
296+
297+
298+
def test_merge_neighbors_gapless():
299+
t = IntervalTree.from_tuples(data.ivs2.data)
300+
t.merge_neighbors()
301+
t.verify()
302+
assert len(t) == 1
303+
for begin, end, _ in t.items():
304+
assert begin == data.ivs2.data[0][0]
305+
assert end == data.ivs2.data[-1][1]
306+
307+
308+
def test_merge_neighbors_with_gap_strict():
309+
def reducer(old, new):
310+
return "%s, %s" % (old, new)
311+
# default distance=1
312+
t = IntervalTree.from_tuples(data.ivs1.data)
313+
t.merge_neighbors(data_reducer=reducer, distance=1, strict=True)
314+
t.verify()
315+
assert len(t) == 7
316+
assert sorted(t) == [
317+
Interval(1, 2, '[1,2)'),
318+
Interval(4, 7, '[4,7)'),
319+
Interval(5, 9, '[5,9)'),
320+
Interval(6, 10, '[6,10)'),
321+
Interval(8, 10, '[8,10)'),
322+
Interval(8, 15, '[8,15)'),
323+
Interval(10, 15, '[10,12), [12,14), [14,15)'),
324+
]
325+
326+
# distance=2
327+
t = IntervalTree.from_tuples(data.ivs1.data)
328+
t.merge_neighbors(data_reducer=reducer, distance=2, strict=True)
329+
t.verify()
330+
assert len(t) == 6
331+
assert sorted(t) == [
332+
Interval(1, 7, '[1,2), [4,7)'),
333+
Interval(5, 9, '[5,9)'),
334+
Interval(6, 10, '[6,10)'),
335+
Interval(8, 10, '[8,10)'),
336+
Interval(8, 15, '[8,15)'),
337+
Interval(10, 15, '[10,12), [12,14), [14,15)'),
338+
]
339+
340+
341+
def test_merge_neighbors_with_gap_nonstrict():
342+
def reducer(old, new):
343+
return "%s, %s" % (old, new)
344+
345+
# default distance=1
346+
t = IntervalTree.from_tuples(data.ivs1.data)
347+
t.merge_neighbors(data_reducer=reducer, distance=1, strict=False)
348+
t.verify()
349+
assert len(t) == 2
350+
assert sorted(t) == [
351+
Interval(1, 2, '[1,2)'),
352+
Interval(4, 15, '[4,7), [5,9), [6,10), [8,10), [8,15), [10,12), [12,14), [14,15)'),
353+
]
354+
# distance=2
355+
t = IntervalTree.from_tuples(data.ivs1.data)
356+
t.merge_neighbors(data_reducer=reducer, distance=2, strict=False)
357+
t.verify()
358+
assert len(t) == 1
359+
assert sorted(t) == [
360+
Interval(1, 15, '[1,2), [4,7), [5,9), [6,10), [8,10), [8,15), [10,12), [12,14), [14,15)')
361+
]
362+
363+
364+
def test_merge_neighbors_reducer_wo_initializer():
365+
def reducer(old, new):
366+
return "%s, %s" % (old, new)
367+
# empty tree
368+
e = IntervalTree()
369+
e.merge_neighbors(data_reducer=reducer)
370+
e.verify()
371+
assert not e
372+
373+
# one Interval in tree
374+
o = IntervalTree.from_tuples([(1, 2, 'hello')])
375+
o.merge_neighbors(data_reducer=reducer)
376+
o.verify()
377+
assert len(o) == 1
378+
assert sorted(o) == [Interval(1, 2, 'hello')]
379+
380+
# many Intervals in tree, without gap
381+
_data_no_gap = (
382+
(1, 2, '[1,2)'),
383+
(2, 3, '[2,3)'),
384+
(3, 4, '[3,4)'),
385+
)
386+
t = IntervalTree.from_tuples(_data_no_gap)
387+
t.merge_neighbors(data_reducer=reducer)
388+
t.verify()
389+
assert len(t) == 1
390+
for begin, end, _data in t.items():
391+
assert begin == 1
392+
assert end == 4
393+
assert _data == '[1,2), [2,3), [3,4)'
394+
395+
# many Intervals in tree, with gap and distance=2
396+
_data_gap = (
397+
(1, 2, '[1,2)'),
398+
(4, 6, '[4,6)'),
399+
(5, 8, '[5,8)'),
400+
(13, 15, '[13,15)'),
401+
)
402+
t = IntervalTree.from_tuples(_data_gap)
403+
t.merge_neighbors(data_reducer=reducer, distance=2)
404+
t.verify()
405+
assert len(t) == 3
406+
assert sorted(t) == [
407+
Interval(1, 6, '[1,2), [4,6)'),
408+
Interval(5, 8, '[5,8)'),
409+
Interval(13, 15, '[13,15)'),
410+
]
411+
412+
413+
def test_merge_neighbors_reducer_with_initializer():
414+
def reducer(old, new):
415+
return old + [new]
416+
# empty tree
417+
e = IntervalTree()
418+
e.merge_neighbors(data_reducer=reducer, data_initializer=[])
419+
e.verify()
420+
assert not e
421+
422+
# one Interval in tree
423+
o = IntervalTree.from_tuples([(1, 2, 'hello')])
424+
o.merge_neighbors(data_reducer=reducer, data_initializer=[])
425+
o.verify()
426+
assert len(o) == 1
427+
assert sorted(o) == [Interval(1, 2, ['hello'])]
428+
429+
# many Intervals in tree, without gap
430+
_data_no_gap = (
431+
(1, 2, '[1,2)'),
432+
(2, 3, '[2,3)'),
433+
(3, 4, '[3,4)'),
434+
)
435+
t = IntervalTree.from_tuples(_data_no_gap)
436+
t.merge_neighbors(data_reducer=reducer, data_initializer=[])
437+
t.verify()
438+
assert len(t) == 1
439+
for begin, end, _data in t.items():
440+
assert begin == 1
441+
assert end == 4
442+
assert _data == ['[1,2)', '[2,3)', '[3,4)']
443+
444+
# many Intervals in tree, with gap and distance=2
445+
_data_gap = (
446+
(1, 2, '[1,2)'),
447+
(4, 6, '[4,6)'),
448+
(5, 8, '[5,8)'),
449+
(13, 15, '[13,15)'),
450+
)
451+
t = IntervalTree.from_tuples(_data_gap)
452+
t.merge_neighbors(data_reducer=reducer, data_initializer=[], distance=2)
453+
t.verify()
454+
assert len(t) == 3
455+
assert sorted(t) == [
456+
Interval(1, 6, ['[1,2)', '[4,6)']),
457+
Interval(5, 8, ['[5,8)']),
458+
Interval(13, 15, ['[13,15)']),
459+
]
460+
461+
462+
# -----------------------------------------------------------------------------
463+
# CHOP
464+
# -----------------------------------------------------------------------------
282465
def test_chop():
283466
t = IntervalTree([Interval(0, 10)])
284467
t.chop(3, 7)
@@ -335,6 +518,9 @@ def datafunc(iv, islower):
335518
assert len(t) == 0
336519

337520

521+
# -----------------------------------------------------------------------------
522+
# SLICE
523+
# -----------------------------------------------------------------------------
338524
def test_slice():
339525
t = IntervalTree([Interval(5, 15)])
340526
t.slice(10)
@@ -379,6 +565,9 @@ def datafunc(iv, islower):
379565
assert sorted(t)[0] == Interval(5, 15)
380566

381567

568+
# -----------------------------------------------------------------------------
569+
# SPLIT
570+
# -----------------------------------------------------------------------------
382571
def test_split_overlap_empty():
383572
t = IntervalTree()
384573
t.split_overlaps()
@@ -407,6 +596,9 @@ def test_split_overlap():
407596
assert other.end == iv.end
408597

409598

599+
# -----------------------------------------------------------------------------
600+
# PICKLE
601+
# -----------------------------------------------------------------------------
410602
def test_pickle():
411603
t = IntervalTree.from_tuples(data.ivs1.data)
412604

0 commit comments

Comments
 (0)