intervaltree
A mutable, self-balancing interval tree. Queries may be by point, by range overlap, or by range containment.
Science Score: 23.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
○.zenodo.json file
-
○DOI references
-
○Academic publication links
-
✓Committers with academic emails
1 of 17 committers (5.9%) from academic institutions -
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (8.0%) to scientific vocabulary
Repository
A mutable, self-balancing interval tree. Queries may be by point, by range overlap, or by range containment.
Basic Info
- Host: GitHub
- Owner: chaimleib
- License: apache-2.0
- Language: Python
- Default Branch: master
- Size: 2.91 MB
Statistics
- Stars: 667
- Watchers: 15
- Forks: 114
- Open Issues: 57
- Releases: 4
Metadata Files
README.md
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!
- 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, useupdate(items). - Methods like
merge_overlaps()which took astrictargument consistently default tostrict=True. Before, some methods defaulted toTrueand others toFalse.
Installing
sh
pip install intervaltree
Features
- Supports Python 2.7 and Python 3.6+ (Tested under 2.7, and 3.6 thru 3.11)
Initializing
- blank
tree = IntervalTree() - from an iterable of
Intervalobjects (tree = IntervalTree(intervals)) - from an iterable of tuples (
tree = IntervalTree.from_tuples(interval_tuples))
- blank
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_tree
- difference
result_tree = tree.difference(iterable)result_tree = tree1 - tree2tree.difference_update(iterable)tree -= other_tree
- intersection
result_tree = tree.intersection(iterable)result_tree = tree1 & tree2tree.intersection_update(iterable)tree &= other_tree
- symmetric difference
result_tree = tree.symmetric_difference(iterable)result_tree = tree1 ^ tree2tree.symmetric_difference_update(iterable)tree ^= other_tree
- comparison
tree1.issubset(tree2)ortree1 <= tree2tree1 <= tree2tree1.issuperset(tree2)ortree1 > tree2tree1 >= tree2tree1 == tree2
- union
Restructuring
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
``` python
from intervaltree import Interval, IntervalTree t = IntervalTree() t IntervalTree()
```
Adding intervals - any object works!
``` python
t[1:2] = "1-2" t[4:7] = (4, 7) t[5:9] = {5: 9}
```
Query by point
The result of a query is a
setobject, so if ordering is important, you must sort it first.``` python
sorted(t[6]) [Interval(4, 7, (4, 7)), Interval(5, 9, {5: 9})] sorted(t[6])[0] Interval(4, 7, (4, 7))
```
Query by range
Note that ranges are inclusive of the lower limit, but non-inclusive of the upper limit. So:
``` python
sorted(t[2:4]) []
```
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:``` python
sorted(t[1:5]) [Interval(1, 2, '1-2'), Interval(4, 7, (4, 7))]
```
To only return intervals that are completely enveloped by the search range:
``` python
sorted(t.envelop(1, 5)) [Interval(1, 2, '1-2')]
```
Accessing an
Intervalobject``` python
iv = Interval(4, 7, (4, 7)) iv.begin 4 iv.end 7 iv.data (4, 7)
begin, end, data = iv begin 4 end 7 data (4, 7)
```
Constructing from lists of intervals
We could have made a similar tree this way:
``` python
ivs = [(1, 2), (4, 7), (5, 9)] t = IntervalTree( ... Interval(begin, end, "%d-%d" % (begin, end)) for begin, end in ivs ... )
```
Or, if we don't need the data fields:
``` python
t2 = IntervalTree(Interval(*iv) for iv in ivs)
```
Or even:
``` python
t2 = IntervalTree.from_tuples(ivs)
```
Removing intervals
``` python
t.remove(Interval(1, 2, "1-2")) sorted(t) [Interval(4, 7, '4-7'), Interval(5, 9, '5-9')]
t.remove(Interval(500, 1000, "Doesn't exist")) # raises ValueError Traceback (most recent call last): ValueError
t.discard(Interval(500, 1000, "Doesn't exist")) # quietly does nothing
del t[5] # same as t.remove_overlap(5) t IntervalTree()
```
We could also empty a tree entirely:
``` python
t2.clear() t2 IntervalTree()
```
Or remove intervals that overlap a range:
``` python
t = IntervalTree([ ... Interval(0, 10), ... Interval(10, 20), ... Interval(20, 30), ... Interval(30, 40)]) t.remove_overlap(25, 35) sorted(t) [Interval(0, 10), Interval(10, 20)]
```
We can also remove only those intervals completely enveloped in a range:
``` python
t.remove_envelop(5, 20) sorted(t) [Interval(0, 10)]
```
Chopping
We could also chop out parts of the tree:
``` python
t = IntervalTree([Interval(0, 10)]) t.chop(3, 7) sorted(t) [Interval(0, 3), Interval(7, 10)]
```
To modify the new intervals' data fields based on which side of the interval is being chopped:
``` python
def datafunc(iv, islower): ... oldlimit = iv[islower] ... return "oldlimit: {0}, islower: {1}".format(oldlimit, islower) t = IntervalTree([Interval(0, 10)]) t.chop(3, 7, datafunc) sorted(t)[0] Interval(0, 3, 'oldlimit: 10, islower: True') sorted(t)[1] Interval(7, 10, 'oldlimit: 0, islower: False')
```
Slicing
You can also slice intervals in the tree without removing them:
``` python
t = IntervalTree([Interval(0, 10), Interval(5, 15)]) t.slice(3) sorted(t) [Interval(0, 3), Interval(3, 10), Interval(5, 15)]
```
You can also set the data fields, for example, re-using
datafunc()from above:``` python
t = IntervalTree([Interval(5, 15)]) t.slice(10, datafunc) sorted(t)[0] Interval(5, 10, 'oldlimit: 15, islower: True') sorted(t)[1] Interval(10, 15, 'oldlimit: 5, islower: False')
```
Future improvements
See the issue tracker on GitHub.
Based on
- Eternally Confuzzled's AVL tree
- Wikipedia's Interval Tree
- Heavily modified from Tyler Kahn's Interval Tree implementation in Python (GitHub project)
- Incorporates contributions from:
- konstantint/Konstantin Tretyakov of the University of Tartu (Estonia)
- siniG/Avi Gabay
- lmcarril/Luis M. Carril of the Karlsruhe Institute for Technology (Germany)
- depristo/MarkDePristo
Copyright
- Chaim Leib Halbert, 2013-2023
- Modifications, Konstantin Tretyakov, 2014
Licensed under the Apache License, version 2.0.
The source code for this project is at https://github.com/chaimleib/intervaltree
Owner
- Name: Chaim Halbert
- Login: chaimleib
- Kind: user
- Location: United States
- Company: Evernote
- Website: https://chaimleib.com
- Repositories: 111
- Profile: https://github.com/chaimleib
GitHub Events
Total
- Issues event: 4
- Watch event: 33
- Issue comment event: 4
- Push event: 28
- Pull request event: 1
- Fork event: 7
- Create event: 1
Last Year
- Issues event: 4
- Watch event: 33
- Issue comment event: 4
- Push event: 28
- Pull request event: 1
- Fork event: 7
- Create event: 1
Committers
Last synced: about 1 year ago
Top Committers
| Name | Commits | |
|---|---|---|
| Chaim-Leib Halbert | c****t@g****m | 461 |
| Konstantin Tretyakov | kt@u****e | 13 |
| Musashiaharon | a****s@g****m | 6 |
| Nimrod Milo | m****d@g****m | 6 |
| Andrew Parsons | p****1@g****m | 4 |
| Luis M. Carril Rodriguez | l****z@k****u | 3 |
| Nicholas Escalona | n****a@g****m | 2 |
| Valentin Lorentz | p****t@p****t | 2 |
| Avi Gabay | a****y@g****m | 1 |
| Avinash Jois | a****h@n****m | 1 |
| Kurt Mosiejczuk | k****t@c****k | 1 |
| Miles Saul | m****l@g****m | 1 |
| Romain Pomier | r****n@c****m | 1 |
| Thomas Grainger | t****n@g****m | 1 |
| Sam Park | s****k@g****m | 1 |
| aleksey.shalamov | a****v@g****m | 1 |
| tuxzz | d****t@g****m | 1 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 10 months ago
All Time
- Total issues: 81
- Total pull requests: 37
- Average time to close issues: 8 months
- Average time to close pull requests: 8 months
- Total issue authors: 72
- Total pull request authors: 23
- Average comments per issue: 2.56
- Average comments per pull request: 1.43
- Merged pull requests: 23
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 4
- Pull requests: 1
- Average time to close issues: 17 days
- Average time to close pull requests: N/A
- Issue authors: 4
- Pull request authors: 1
- Average comments per issue: 0.5
- Average comments per pull request: 0.0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- chaimleib (4)
- johann-petrak (3)
- alexandersoto (2)
- alexlenail (2)
- mbhall88 (2)
- escalonn (2)
- 25thbamofthetower (1)
- buhtz (1)
- rob-myers (1)
- chananshgong (1)
- itaiperi (1)
- progval (1)
- Timmmm (1)
- brainfo (1)
- rLannes (1)
Pull Request Authors
- chaimleib (11)
- progval (3)
- Jeremiah-England (2)
- nazimisik (2)
- escalonn (2)
- adamcircle (1)
- pomier (1)
- iwismer (1)
- milonimrod (1)
- mhsaul (1)
- ssadedin (1)
- halicki (1)
- graingert (1)
- depristo (1)
- goodspark (1)
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 14
-
Total downloads:
- pypi 1,626,617 last-month
- Total docker downloads: 27,954,501
-
Total dependent packages: 136
(may contain duplicates) -
Total dependent repositories: 2,853
(may contain duplicates) - Total versions: 33
- Total maintainers: 3
pypi.org: intervaltree
Editable interval tree data structure for Python 2 and 3
- Homepage: https://github.com/chaimleib/intervaltree
- Documentation: https://intervaltree.readthedocs.io/
- License: Apache License, Version 2.0
-
Latest release: 3.1.0
published almost 6 years ago
Rankings
Maintainers (1)
spack.io: py-intervaltree
Editable interval tree data structure for Python 2 and 3.
- Homepage: https://github.com/chaimleib/intervaltree
- License: []
-
Latest release: 3.0.2
published over 4 years ago
Rankings
Maintainers (1)
alpine-edge: py3-intervaltree
Editable interval tree data structure
- Homepage: https://github.com/chaimleib/intervaltree
- License: Apache-2.0
-
Latest release: 3.1.0-r1
published about 2 years ago
Rankings
Maintainers (1)
alpine-edge: py3-intervaltree-pyc
Precompiled Python bytecode for py3-intervaltree
- Homepage: https://github.com/chaimleib/intervaltree
- License: Apache-2.0
-
Latest release: 3.1.0-r1
published about 2 years ago
Rankings
Maintainers (1)
conda-forge.org: intervaltree
- Homepage: https://github.com/chaimleib/intervaltree
- License: Apache-2.0
-
Latest release: 3.0.2
published over 7 years ago
Rankings
anaconda.org: intervaltree
- Homepage: https://github.com/chaimleib/intervaltree
- License: Apache-2.0
-
Latest release: 3.1.0
published almost 6 years ago
Rankings
alpine-v3.22: py3-intervaltree-pyc
Precompiled Python bytecode for py3-intervaltree
- Homepage: https://github.com/chaimleib/intervaltree
- License: Apache-2.0
-
Latest release: 3.1.0-r1
published about 2 years ago
Rankings
Maintainers (1)
alpine-v3.20: py3-intervaltree-pyc
Precompiled Python bytecode for py3-intervaltree
- Homepage: https://github.com/chaimleib/intervaltree
- License: Apache-2.0
-
Latest release: 3.1.0-r1
published about 2 years ago
Rankings
Maintainers (1)
alpine-v3.21: py3-intervaltree
Editable interval tree data structure
- Homepage: https://github.com/chaimleib/intervaltree
- License: Apache-2.0
-
Latest release: 3.1.0-r1
published about 2 years ago
Rankings
Maintainers (1)
alpine-v3.21: py3-intervaltree-pyc
Precompiled Python bytecode for py3-intervaltree
- Homepage: https://github.com/chaimleib/intervaltree
- License: Apache-2.0
-
Latest release: 3.1.0-r1
published about 2 years ago
Rankings
Maintainers (1)
alpine-v3.22: py3-intervaltree
Editable interval tree data structure
- Homepage: https://github.com/chaimleib/intervaltree
- License: Apache-2.0
-
Latest release: 3.1.0-r1
published about 2 years ago
Rankings
Maintainers (1)
alpine-v3.20: py3-intervaltree
Editable interval tree data structure
- Homepage: https://github.com/chaimleib/intervaltree
- License: Apache-2.0
-
Latest release: 3.1.0-r1
published about 2 years ago
Rankings
Maintainers (1)
alpine-v3.19: py3-intervaltree
Editable interval tree data structure
- Homepage: https://github.com/chaimleib/intervaltree
- License: Apache-2.0
-
Latest release: 3.1.0-r0
published almost 3 years ago
Maintainers (1)
alpine-v3.19: py3-intervaltree-pyc
Precompiled Python bytecode for py3-intervaltree
- Homepage: https://github.com/chaimleib/intervaltree
- License: Apache-2.0
-
Latest release: 3.1.0-r0
published almost 3 years ago
Dependencies
- sortedcontainers *