A mutable, self-balancing interval tree for Python 2 and 3. Queries may be by point, by range overlap, or by range envelopment.
This library was designed to allow tagging text and time intervals, where the intervals include the lower bound but not the upper bound.
This means that 0-length intervals are null and overlap nothing.
If you see errors about null intervals,
make sure end is strictly greater than begin.
Our Intervals are like string ranges:
'hello'[0:0] == '', but 'hello'[0:1] == 'h'.
Version 3 changes!
The search(begin, end, strict) method no longer exists. Instead, use one of these:
at(point)
overlap(begin, end)
envelop(begin, end)
The extend(items) method no longer exists. Instead, use update(items).
Methods like merge_overlaps() which took a strict argument consistently default to strict=True. Before, some methods defaulted to True and others to False.
Installing
pip install intervaltree
Features
Supports Python 2.7 and Python 3.5+ (Tested under 2.7, and 3.5 thru 3.14)
Initializing
blank tree = IntervalTree()
from an iterable of Interval objects (tree = IntervalTree(intervals))
from an iterable of tuples (tree = IntervalTree.from_tuples(interval_tuples))
Insertions
tree[begin:end] = data
tree.add(interval)
tree.addi(begin, end, data)
Deletions
tree.remove(interval) (raises ValueError if not present)
tree.discard(interval) (quiet if not present)
tree.removei(begin, end, data) (short for tree.remove(Interval(begin, end, data)))
tree.discardi(begin, end, data) (short for tree.discard(Interval(begin, end, data)))
tree.remove_overlap(point)
tree.remove_overlap(begin, end) (removes all overlapping the range)
tree.remove_envelop(begin, end) (removes all enveloped in the range)
Point queries
tree[point]
tree.at(point) (same as previous)
Overlap queries
tree[begin:end]
tree.overlap(begin, end) (same as previous)
Envelop queries
tree.envelop(begin, end)
Membership queries
interval_obj in tree (this is fastest, O(1))
tree.containsi(begin, end, data)
tree.overlaps(point)
tree.overlaps(begin, end)
Iterable
for interval_obj in tree:
tree.items()
Sizing
len(tree)
tree.is_empty()
not tree
tree.begin() (the begin coordinate of the leftmost interval)
tree.end() (the end coordinate of the rightmost interval)
Set-like operations
union
result_tree = tree.union(iterable)
result_tree = tree1 | tree2
tree.update(iterable)
tree |= other_tree
difference
result_tree = tree.difference(iterable)
result_tree = tree1 - tree2
tree.difference_update(iterable)
tree -= other_tree
intersection
result_tree = tree.intersection(iterable)
result_tree = tree1 & tree2
tree.intersection_update(iterable)
tree &= other_tree
symmetric difference
result_tree = tree.symmetric_difference(iterable)
result_tree = tree1 ^ tree2
tree.symmetric_difference_update(iterable)
tree ^= other_tree
comparison
tree1.issubset(tree2) or tree1 <= tree2
tree1 <= tree2
tree1.issuperset(tree2) or tree1 > tree2
tree1 >= tree2
tree1 == tree2
Restructuring
chop(begin, end) (slice intervals and remove everything between begin and end, optionally modifying the data fields of the chopped-up intervals)
slice(point) (slice intervals at point)
split_overlaps() (slice at all interval boundaries, optionally modifying the data field)
merge_overlaps() (joins overlapping intervals into a single interval, optionally merging the data fields)
merge_equals() (joins intervals with matching ranges into a single interval, optionally merging the data fields)
merge_neighbors() (joins adjacent intervals into a single interval if the distance between their range terminals is less than or equal to a given distance. Optionally merges overlapping intervals. Can also merge the data fields.)
Copying and typecasting
IntervalTree(tree) (Interval objects are same as those in tree)
tree.copy() (Interval objects are shallow copies of those in tree)
set(tree) (can later be fed into IntervalTree())
list(tree) (ditto)
Pickle-friendly
Automatic AVL balancing
Examples
Getting started
>>> from intervaltree import Interval, IntervalTree
>>> t = IntervalTree()
>>> t
IntervalTree()
Note that ranges are inclusive of the lower limit, but non-inclusive of the upper limit. So:
>>> sorted(t[2:4])
[]
Since our search was over 2 ≤ x < 4, neither Interval(1, 2) nor Interval(4, 7)
was included. The first interval, 1 ≤ x < 2 does not include x = 2. The second
interval, 4 ≤ x < 7, does include x = 4, but our search interval excludes it. So,
there were no overlapping intervals. However:
intervaltree
A mutable, self-balancing interval tree for Python 2 and 3. Queries may be by point, by range overlap, or by range envelopment.
This library was designed to allow tagging text and time intervals, where the intervals include the lower bound but not the upper bound.
Version 3 changes!
search(begin, end, strict)method no longer exists. Instead, use one of these:at(point)overlap(begin, end)envelop(begin, end)extend(items)method no longer exists. Instead, useupdate(items).merge_overlaps()which took astrictargument consistently default tostrict=True. Before, some methods defaulted toTrueand others toFalse.Installing
Features
Supports Python 2.7 and Python 3.5+ (Tested under 2.7, and 3.5 thru 3.14)
Initializing
tree = IntervalTree()Intervalobjects (tree = IntervalTree(intervals))tree = IntervalTree.from_tuples(interval_tuples))Insertions
tree[begin:end] = datatree.add(interval)tree.addi(begin, end, data)Deletions
tree.remove(interval)(raisesValueErrorif not present)tree.discard(interval)(quiet if not present)tree.removei(begin, end, data)(short fortree.remove(Interval(begin, end, data)))tree.discardi(begin, end, data)(short fortree.discard(Interval(begin, end, data)))tree.remove_overlap(point)tree.remove_overlap(begin, end)(removes all overlapping the range)tree.remove_envelop(begin, end)(removes all enveloped in the range)Point queries
tree[point]tree.at(point)(same as previous)Overlap queries
tree[begin:end]tree.overlap(begin, end)(same as previous)Envelop queries
tree.envelop(begin, end)Membership queries
interval_obj in tree(this is fastest, O(1))tree.containsi(begin, end, data)tree.overlaps(point)tree.overlaps(begin, end)Iterable
for interval_obj in tree:tree.items()Sizing
len(tree)tree.is_empty()not treetree.begin()(thebegincoordinate of the leftmost interval)tree.end()(theendcoordinate of the rightmost interval)Set-like operations
union
result_tree = tree.union(iterable)result_tree = tree1 | tree2tree.update(iterable)tree |= other_treedifference
result_tree = tree.difference(iterable)result_tree = tree1 - tree2tree.difference_update(iterable)tree -= other_treeintersection
result_tree = tree.intersection(iterable)result_tree = tree1 & tree2tree.intersection_update(iterable)tree &= other_treesymmetric difference
result_tree = tree.symmetric_difference(iterable)result_tree = tree1 ^ tree2tree.symmetric_difference_update(iterable)tree ^= other_treecomparison
tree1.issubset(tree2)ortree1 <= tree2tree1 <= tree2tree1.issuperset(tree2)ortree1 > tree2tree1 >= tree2tree1 == tree2Restructuring
chop(begin, end)(slice intervals and remove everything betweenbeginandend, optionally modifying the data fields of the chopped-up intervals)slice(point)(slice intervals atpoint)split_overlaps()(slice at all interval boundaries, optionally modifying the data field)merge_overlaps()(joins overlapping intervals into a single interval, optionally merging the data fields)merge_equals()(joins intervals with matching ranges into a single interval, optionally merging the data fields)merge_neighbors()(joins adjacent intervals into a single interval if the distance between their range terminals is less than or equal to a given distance. Optionally merges overlapping intervals. Can also merge the data fields.)Copying and typecasting
IntervalTree(tree)(Intervalobjects are same as those in tree)tree.copy()(Intervalobjects are shallow copies of those in tree)set(tree)(can later be fed intoIntervalTree())list(tree)(ditto)Pickle-friendly
Automatic AVL balancing
Examples
Getting started
Adding intervals - any object works!
Query by point
The result of a query is a
setobject, so if ordering is important, you must sort it first.Query by range
Note that ranges are inclusive of the lower limit, but non-inclusive of the upper limit. So:
Since our search was over
2 ≤ x < 4, neitherInterval(1, 2)norInterval(4, 7)was included. The first interval,1 ≤ x < 2does not includex = 2. The second interval,4 ≤ x < 7, does includex = 4, but our search interval excludes it. So, there were no overlapping intervals. However:To only return intervals that are completely enveloped by the search range:
Accessing an
IntervalobjectConstructing from lists of intervals
We could have made a similar tree this way:
Or, if we don’t need the data fields:
Or even:
Removing intervals
We could also empty a tree entirely:
Or remove intervals that overlap a range:
We can also remove only those intervals completely enveloped in a range:
Chopping
We could also chop out parts of the tree:
To modify the new intervals’ data fields based on which side of the interval is being chopped:
Slicing
You can also slice intervals in the tree without removing them:
You can also set the data fields, for example, re-using
datafunc()from above:Future improvements
See the issue tracker on GitHub.
Based on
Copyright
Licensed under the Apache License, version 2.0.
The source code for this project is at https://github.com/chaimleib/intervaltree