class Sequencer: sortkey = lambda n: n.start + n.length def __init__(self): self.notes = IntervalTree() def add(self, note): self.notes.addi(note.start, note.start + note.length, note) def remove(self, note): self.notes.removei(note.start, note.start + note.length, note) def length(self): return self.notes.end() def sample_at(self, t): # again, bad current = self.notes.at(t) acc = 0 for note in current: note_pos = t - note.begin acc += (osc.sine(note_pos, note.data.pitch) * note.data.velocity * adsr(note_pos, note.end - note.begin)) * (1 / len(current)) return acc
def test_sequence(): t = IntervalTree() t.addi(860, 917, 1) t.verify() t.addi(860, 917, 2) t.verify() t.addi(860, 917, 3) t.verify() t.addi(860, 917, 4) t.verify() t.addi(871, 917, 1) t.verify() t.addi(871, 917, 2) t.verify() t.addi(871, 917, 3) # Value inserted here t.verify() t.addi(961, 986, 1) t.verify() t.addi(1047, 1064, 1) t.verify() t.addi(1047, 1064, 2) t.verify() t.removei(961, 986, 1) t.verify() t.removei(871, 917, 3) # Deleted here t.verify()
class BorderModel(QObject): rangeChanged = pyqtSignal([BorderedRange]) def __init__(self, parent, color_theme=SolarizedColorTheme): super(BorderModel, self).__init__(parent) # data structure description: # _db is an interval tree that indexes on the start and end of bordered ranges # the values are BorderedRange instances. # given an index, determining its border is): # intervaltree lookup index in _db (which is O(log <num ranges>) ) # iterate containing ranges (worst case, O(<num ranges>), but typically small) # hash lookup on index to fetch border state (which is O(1)) self._db = IntervalTree() self._theme = color_theme def border_region(self, begin, end, color=None): if color is None: color = self._theme.get_accent(len(self._db)) range = BorderedRange(begin, end, BorderTheme(color), compute_region_border(begin, end)) # note we use (end + 1) to ensure the entire selection gets captured self._db.addi(range.begin, range.end + 1, range) self.rangeChanged.emit(range) def clear_region(self, begin, end): span = end - begin to_remove = [] for r in self._db[begin:end]: if r.end - r.begin - 1 == span: to_remove.append(r) for r in to_remove: self._db.removei(r.begin, r.end, r.data) self.rangeChanged.emit(r.data) def get_border(self, index): # ranges is a (potentially empty) list of intervaltree.Interval instances # we sort them here from shorted length to longest, because we want # the most specific border ranges = sorted(self._db[index], key=lambda r: r.end - r.begin) if len(ranges) > 0: range = ranges[0].data cell = range.cells.get(index, None) if cell is None: return None ret = BorderData(cell.top, cell.bottom, cell.left, cell.right, range.theme) return ret return None def is_index_bordered(self, index): return len(self._db[index]) > 0 def is_region_bordered(self, begin, end): span = end - begin for range in self._db[begin:end]: if range.end - range.begin == span: return True return False
class ColorModel(QObject): rangeChanged = pyqtSignal([ColoredRange]) def __init__(self, parent, color_theme=SolarizedColorTheme): super(ColorModel, self).__init__(parent) self._db = IntervalTree() self._theme = color_theme def color_region(self, begin, end, color=None): if color is None: color = self._theme.get_accent(len(self._db)) r = ColoredRange(begin, end, color) self.color_range(r) return r def clear_region(self, begin, end): span = end - begin to_remove = [] for r in self._db[begin:end]: if r.end - r.begin == span: to_remove.append(r) for r in to_remove: self.clear_range(r.data) def color_range(self, range_): self._db.addi(range_.begin, range_.end, range_) self.rangeChanged.emit(range_) def clear_range(self, range_): self._db.removei(range_.begin, range_.end, range_) self.rangeChanged.emit(range_) def get_color(self, index): # ranges is a (potentially empty) list of intervaltree.Interval instances # we sort them here from shorted length to longest, because we want # the most specific color ranges = sorted(self._db[index], key=lambda r: r.end - r.begin) if len(ranges) > 0: return ranges[0].data.color return None def get_region_colors(self, begin, end): if begin == end: results = self._db[begin] else: results = self._db[begin:end] return funcy.pluck_attr("data", results) def is_index_colored(self, index): return len(self._db[index]) > 0 def is_region_colored(self, begin, end): return len(self._db[begin:end]) > 0
def test_issue5(): # Issue #5, https://github.com/konstantint/PyIntervalTree/issues/5 from intervaltree import IntervalTree t = IntervalTree() t.addi(-46.0, 31.0, 'test') t.addi(-20.0, 29.0, 'test') t.addi(1.0, 9.0, 'test') t.addi(-3.0, 6.0, 'test') t.removei(1.0, 9.0, 'test') t.removei(-20.0, 29.0, 'test') t.removei(-46.0, 31.0, 'test') assert len(t) == 1
def test_removei(): # Empty tree e = IntervalTree() with pytest.raises(ValueError): e.removei(-1000, -999, "Doesn't exist") e.verify() assert len(e) == 0 # Non-existent member should raise ValueError t = trees['ivs1']() oldlen = len(t) with pytest.raises(ValueError): t.removei(-1000, -999, "Doesn't exist") t.verify() assert len(t) == oldlen # Should remove existing member assert Interval(1, 2, '[1,2)') in t t.removei(1, 2, '[1,2)') assert len(t) == oldlen - 1 assert Interval(1, 2, '[1,2)') not in t
def test_original_sequence(): t = IntervalTree() t.addi(17.89, 21.89) t.addi(11.53, 16.53) t.removei(11.53, 16.53) t.removei(17.89, 21.89) t.addi(-0.62, 4.38) t.addi(9.24, 14.24) t.addi(4.0, 9.0) t.removei(-0.62, 4.38) t.removei(9.24, 14.24) t.removei(4.0, 9.0) t.addi(12.86, 17.86) t.addi(16.65, 21.65) t.removei(12.86, 17.86)
def test_original_sequence(): t = IntervalTree() t.addi(17.89,21.89) t.addi(11.53,16.53) t.removei(11.53,16.53) t.removei(17.89,21.89) t.addi(-0.62,4.38) t.addi(9.24,14.24) t.addi(4.0,9.0) t.removei(-0.62,4.38) t.removei(9.24,14.24) t.removei(4.0,9.0) t.addi(12.86,17.86) t.addi(16.65,21.65) t.removei(12.86,17.86)
def test_sequence(): t = IntervalTree() t.addi(6.37, 11.37) t.verify() t.addi(12.09, 17.09) t.verify() t.addi(5.68, 11.58) t.verify() t.removei(6.37, 11.37) t.verify() t.addi(13.23, 18.23) t.verify() t.removei(12.09, 17.09) t.verify() t.addi(4.29, 8.29) t.verify() t.removei(13.23, 18.23) t.verify() t.addi(12.04, 17.04) t.verify() t.addi(9.39, 13.39) t.verify() t.removei(5.68, 11.58) t.verify() t.removei(4.29, 8.29) t.verify() t.removei(12.04, 17.04) t.verify() t.addi(5.66, 9.66) # Value inserted here t.verify() t.addi(8.65, 13.65) t.verify() t.removei(9.39, 13.39) t.verify() t.addi(16.49, 20.83) t.verify() t.addi(11.42, 16.42) t.verify() t.addi(5.38, 10.38) t.verify() t.addi(3.57, 9.47) t.verify() t.removei(8.65, 13.65) t.verify() t.removei(5.66, 9.66) # Deleted here t.verify()
def test_debug_sequence(): t = IntervalTree() t.verify() t.addi(17.89, 21.89) t.verify() t.addi(11.53, 16.53) t.verify() t.removei(11.53, 16.53) t.verify() t.removei(17.89, 21.89) t.verify() t.addi(-0.62, 4.38) t.verify() t.addi(9.24, 14.24) # t.print_structure() # Node<-0.62, depth=2, balance=1> # Interval(-0.62, 4.38) # >: Node<9.24, depth=1, balance=0> # Interval(9.24, 14.24) t.verify() t.addi(4.0, 9.0) # This line breaks the invariants, leaving an empty node # t.print_structure() t.verify() t.removei(-0.62, 4.38) t.verify() t.removei(9.24, 14.24) t.verify() t.removei(4.0, 9.0) t.verify() t.addi(12.86, 17.86) t.verify() t.addi(16.65, 21.65) t.verify() t.removei(12.86, 17.86)
def test_debug_sequence(): t = IntervalTree() t.verify() t.addi(17.89,21.89) t.verify() t.addi(11.53,16.53) t.verify() t.removei(11.53,16.53) t.verify() t.removei(17.89,21.89) t.verify() t.addi(-0.62,4.38) t.verify() t.addi(9.24,14.24) # t.print_structure() # Node<-0.62, depth=2, balance=1> # Interval(-0.62, 4.38) # >: Node<9.24, depth=1, balance=0> # Interval(9.24, 14.24) t.verify() t.addi(4.0,9.0) # This line breaks the invariants, leaving an empty node # t.print_structure() t.verify() t.removei(-0.62,4.38) t.verify() t.removei(9.24,14.24) t.verify() t.removei(4.0,9.0) t.verify() t.addi(12.86,17.86) t.verify() t.addi(16.65,21.65) t.verify() t.removei(12.86,17.86)
insertiontree[curr_insertion['start']: curr_insertion['end']] = curr_insertion curr_insertion = {} elif block['req'][source][ 'aln'] == 1 and block['req'][source]['rightCount'] > 0: if curr_insertion == {}: curr_insertion['start'] = start curr_insertion['part1'] = block else: logging.warning("insertion part already exist?") curr_insertion = {} elif block['req'][source]['aln'] == 0: if 'part' in curr_deletion and block['req'][ source] == curr_deletion['part']['req'][source]: deletiontree.removei(curr_deletion['start'], curr_deletion['end'], curr_deletion) curr_deletion['end'] = end deletiontree[curr_deletion['start']: curr_deletion['end']] = curr_deletion else: curr_deletion['start'] = start curr_deletion['end'] = end curr_deletion['part'] = block deletiontree[curr_deletion['start']: curr_deletion['end']] = curr_deletion with open(flank5k, 'w') as flanking_outfile: with open(exact, 'w') as exact_outfile: for Iobj in sorted(insertiontree): Dset = sorted(deletiontree.search(Iobj.begin, Iobj.end)) closeset = sorted(
def test_debug_sequence(): t = IntervalTree() t.addi(6.37,11.37) t.verify() t.addi(12.09,17.09) t.verify() t.addi(5.68,11.58) t.verify() t.removei(6.37,11.37) t.verify() t.addi(13.23,18.23) t.verify() t.removei(12.09,17.09) t.verify() t.addi(4.29,8.29) t.verify() t.removei(13.23,18.23) t.verify() t.addi(12.04,17.04) t.verify() t.addi(9.39,13.39) t.verify() t.removei(5.68,11.58) t.verify() t.removei(4.29,8.29) t.verify() t.removei(12.04,17.04) t.verify() t.addi(5.66,9.66) # Value inserted here t.verify() t.addi(8.65,13.65) t.verify() t.removei(9.39,13.39) t.verify() t.addi(16.49,20.83) t.verify() t.addi(11.42,16.42) t.verify() t.addi(5.38,10.38) t.verify() t.addi(3.57,9.47) t.verify() t.removei(8.65,13.65) t.verify() t.removei(5.66,9.66) # Deleted here t.verify()