intervaltree
intervaltree copied to clipboard
add method to find complement
It would be great to have a method to find the complement of the intervals in the tree, with the option of specifying the true bounds of the universe the tree lives within.
Example (=
is interval; .
is gap):
....===..==.....=====...===..... <<<Interval tree
.......==..=====.....===........ <<<Complement
====...==..=====.....===...===== <<<Complement w/ bounds
Here's a basic algorithm, which you can test and modify. I'm having some issues with the automated tests in Travis, so I can't easily add this method at the moment, but I hope this will get you unstuck.
def complement(tree):
result = IntervalTree()
result.addi(tree.begin(), tree.end()) # using input tree bounds
for iv in tree:
result.chop(iv)
return result
@chaimleib thanks for the sample code, it does not seem to work anymore. Here is an updated version, if someone needs bounds then you have to set start and end.
def complement(tree, start=None, end=None):
result = IntervalTree()
if start is None:
start = tree.begin()
if end is None:
end = tree.end()
result.addi(start, end) # using input tree bounds
for iv in tree:
result.chop(iv[0], iv[1])
return result
Thanks @reinoldus
Thanks @chaimleib and @reinoldus! Super helpful!