28
28
import pickle
29
29
30
30
31
+ # -----------------------------------------------------------------------------
32
+ # REMOVAL
33
+ # -----------------------------------------------------------------------------
31
34
def test_emptying_partial ():
32
35
t = IntervalTree .from_tuples (data .ivs1 .data )
33
36
assert t [7 :]
@@ -53,11 +56,13 @@ def test_remove_overlap():
53
56
t .verify ()
54
57
55
58
59
+ # -----------------------------------------------------------------------------
60
+ # MERGE_OVERLAPS
61
+ # -----------------------------------------------------------------------------
56
62
def test_merge_overlaps_empty ():
57
63
t = IntervalTree ()
58
64
t .merge_overlaps ()
59
65
t .verify ()
60
-
61
66
assert len (t ) == 0
62
67
63
68
@@ -76,9 +81,9 @@ def test_merge_overlaps_gapless():
76
81
assert len (t ) == 1
77
82
assert t .pop () == rng
78
83
84
+
79
85
def test_merge_overlaps_with_gap ():
80
86
t = IntervalTree .from_tuples (data .ivs1 .data )
81
-
82
87
t .merge_overlaps ()
83
88
t .verify ()
84
89
assert len (t ) == 2
@@ -94,7 +99,7 @@ def reducer(old, new):
94
99
e .verify ()
95
100
assert not e
96
101
97
- # One Interval in tree
102
+ # one Interval in tree
98
103
o = IntervalTree .from_tuples ([(1 , 2 , 'hello' )])
99
104
o .merge_overlaps (data_reducer = reducer )
100
105
o .verify ()
@@ -107,7 +112,7 @@ def reducer(old, new):
107
112
t .verify ()
108
113
assert len (t ) == 2
109
114
assert sorted (t ) == [
110
- Interval (1 , 2 ,'[1,2)' ),
115
+ Interval (1 , 2 , '[1,2)' ),
111
116
Interval (4 , 15 , '[4,7), [5,9), [6,10), [8,10), [8,15), [10,12), [12,14), [14,15)' )
112
117
]
113
118
@@ -121,7 +126,7 @@ def reducer(old, new):
121
126
e .verify ()
122
127
assert not e
123
128
124
- # One Interval in tree
129
+ # one Interval in tree
125
130
o = IntervalTree .from_tuples ([(1 , 2 , 'hello' )])
126
131
o .merge_overlaps (data_reducer = reducer , data_initializer = [])
127
132
o .verify ()
@@ -148,11 +153,13 @@ def reducer(old, new):
148
153
]
149
154
150
155
156
+ # -----------------------------------------------------------------------------
157
+ # MERGE_EQUALS
158
+ # -----------------------------------------------------------------------------
151
159
def test_merge_equals_empty ():
152
160
t = IntervalTree ()
153
161
t .merge_equals ()
154
162
t .verify ()
155
-
156
163
assert len (t ) == 0
157
164
158
165
@@ -163,7 +170,6 @@ def test_merge_equals_wo_dupes():
163
170
164
171
t .merge_equals ()
165
172
t .verify ()
166
-
167
173
assert orig == t
168
174
169
175
@@ -213,7 +219,7 @@ def reducer(old, new):
213
219
e .verify ()
214
220
assert not e
215
221
216
- # One Interval in tree, no change
222
+ # one Interval in tree, no change
217
223
o = IntervalTree .from_tuples ([(1 , 2 , 'hello' )])
218
224
o .merge_equals (data_reducer = reducer )
219
225
o .verify ()
@@ -250,7 +256,7 @@ def reducer(old, new):
250
256
e .verify ()
251
257
assert not e
252
258
253
- # One Interval in tree, no change
259
+ # one Interval in tree, no change
254
260
o = IntervalTree .from_tuples ([(1 , 2 , 'hello' )])
255
261
o .merge_equals (data_reducer = reducer , data_initializer = [])
256
262
o .verify ()
@@ -279,6 +285,183 @@ def reducer(old, new):
279
285
assert t .containsi (4 , 7 , ['[4,7)' , 'foo' ])
280
286
281
287
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
+ # -----------------------------------------------------------------------------
282
465
def test_chop ():
283
466
t = IntervalTree ([Interval (0 , 10 )])
284
467
t .chop (3 , 7 )
@@ -335,6 +518,9 @@ def datafunc(iv, islower):
335
518
assert len (t ) == 0
336
519
337
520
521
+ # -----------------------------------------------------------------------------
522
+ # SLICE
523
+ # -----------------------------------------------------------------------------
338
524
def test_slice ():
339
525
t = IntervalTree ([Interval (5 , 15 )])
340
526
t .slice (10 )
@@ -379,6 +565,9 @@ def datafunc(iv, islower):
379
565
assert sorted (t )[0 ] == Interval (5 , 15 )
380
566
381
567
568
+ # -----------------------------------------------------------------------------
569
+ # SPLIT
570
+ # -----------------------------------------------------------------------------
382
571
def test_split_overlap_empty ():
383
572
t = IntervalTree ()
384
573
t .split_overlaps ()
@@ -407,6 +596,9 @@ def test_split_overlap():
407
596
assert other .end == iv .end
408
597
409
598
599
+ # -----------------------------------------------------------------------------
600
+ # PICKLE
601
+ # -----------------------------------------------------------------------------
410
602
def test_pickle ():
411
603
t = IntervalTree .from_tuples (data .ivs1 .data )
412
604
0 commit comments