ival.py 14.9 KB
 Mikael Boden committed Sep 03, 2017 1 2 3 4 5 6 7 8 9 10 11 12 ``````import random class IntervalTree: """ Binary search tree for storing long integer intervals, and for performing queries on them. See https://en.wikipedia.org/wiki/Interval_tree, specifically the Augmented kind. The present implementation balances the tree by using randomisation. """ root = None # pointer to the root node of the binary search tree stack = None def __iter__(self): `````` Mikael Boden committed Jul 01, 2018 13 14 15 16 `````` """ Create an iterator for the tree; this will iterate through nodes of the tree. Note that a node has one interval and (potentially) multiple values. :return: iterator of IntervalNodes stored in this tree """ `````` Mikael Boden committed Sep 03, 2017 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 `````` self.current = self.root self.stack = Stack() return self def __next__(self): while self.current != None: self.stack.push(self.current) self.current = self.current.left if self.stack.isEmpty(): raise StopIteration self.current = self.stack.pop() ret = self.current self.current = self.current.right return ret def __len__(self): `````` Mikael Boden committed Jul 01, 2018 33 34 35 36 `````` if self.root != None: return self.root.N else: return 0 `````` Mikael Boden committed Sep 03, 2017 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 `````` def __contains__(self, ival): return self.get(ival) != None def get(self, ival): return self._get(self.root, ival) def _get(self, node, ival): if node == None: return None if ival < node.ival: return self._get(node.left, ival) elif ival > node.ival: return self._get(node.right, ival) else: return node def isect(self, ival, node = None): """ Look for intersecting interval in subtree rooted at specified node (root by default). Returns node of intersecting interval. """ if self.root == None: return None if node == None: return self.isect(ival, self.root) while node != None: if isect(ival, node.ival): return node elif node.left == None: node = node.right elif node.left.max < ival.min: node = node.right else: node = node.left return None def isectall(self, ival): """ Look for all intersecting intervals in the subtree rooted at specified node (root by default). Returns nodes of intersecting intervals. """ return _isectall(ival, self.root) def closest(self, query): """ Retrieve the interval Y stored in the tree that is closest to the given interval X. If the given interval overlaps with one or more stored intervals, one is returned: the interval Y with the greatest Jaccard index to X. If multiple intervals are equally close, only one is returned (the one before I think). :param query: the interval for which the closest is sought :return: the interval closest to the given query interval """ ovlap = self.isectall(query) if len(ovlap) == 0: # overlapping intervals are not in the tree return _closest(query, self.root) else: best_iv = None best_ji = 0 for node in ovlap: ji = jaccard(node.ival, query) if best_iv == None or ji > best_ji: best_iv = node best_ji = ji return best_iv def put(self, ival, value = None): nodex = self.get(ival) if nodex: nodex.values.add(value) else: self.root = self._randomizedInsert(self.root, ival, value) `````` Mikael Boden committed Jul 01, 2018 98 99 `````` def putAll(self, tree): for i in tree: `````` Mikael Boden committed Aug 02, 2020 100 `````` self.put(i.getInterval(), tree.__getitem__(i.getInterval())) `````` Mikael Boden committed Jul 01, 2018 101 `````` `````` Mikael Boden committed Sep 03, 2017 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 `````` def _randomizedInsert(self, node, ival, value): if node == None: return IntervalNode(ival, value) if random.uniform(0,1) * node.N < 1.0: return self._rootInsert(node, ival, value) if ival < node.ival: node.left = self._randomizedInsert(node.left, ival, value) else: node.right = self._randomizedInsert(node.right, ival, value) _fix(node) return node def _rootInsert(self, node, ival, value): if node == None: return IntervalNode(ival, value) if ival < node.ival: node.left = self._rootInsert(node.left, ival, value) node = _rotR(node) else: node.right = self._rootInsert(node.right, ival, value) node = _rotL(node) return node def _isectall(ival, node): """ Look for all intersecting intervals in the subtree rooted at specified node (root by default). Returns nodes of intersecting intervals. """ if node == None: return [] found = [] if isect(ival, node.ival): found = [node] if node.left and node.left.max >= ival.min: found.extend(_isectall(ival, node.left)) if len(found) > 0 or node.left == None or node.left.max < ival.min: found.extend(_isectall(ival, node.right)) return found def _closest(query, cand): """ Recursively find the interval with the minimum distance to that given. This internal function does not guarantee that distances are sensible when overlapping intervals exist; essentially it assumes that overlaps have been eliminated prior. :param query: interval :param cand: node from which search starts :return: closest interval """ fav = None favdist = -1 while cand != None: if query == cand.ival: return cand distx = query.dist(cand.ival) if fav == None or distx <= favdist: fav = cand favdist = distx if cand.left == None: cand = cand.right elif cand.right == None: cand = cand.left elif cand.ival.min > query.max: cand = cand.left # the smallest, indexed value (on left) is AFTER the query min else: # no way to choose without looking in the intervals below favleft = None distleft = query.dist(Interval(cand.left.min, cand.left.max)) if distleft < favdist: favleft = _closest(query, cand.left) distleft = query.dist(favleft.ival) if favleft != None else MAX_VALUE distright = query.dist(Interval(cand.right.min, cand.right.max)) if distright < favdist: favright = _closest(query, cand.right) distright = query.dist(favright.ival) if favright != None else MAX_VALUE if distleft < distright: return favleft if distleft < favdist else fav else: return favright if distright < favdist else fav return fav class IntervalNode: """ Defines the node of the interval search tree. Manages values associated with intervals. """ ival = None # the actual interval values = None # values associated with the interval left = None # subtree on the left (lesser) right = None # subtree on the right (greater) N = 1 # number of nodes under (and including) this one min = 0 # min point of subtree rooted at this node max = 0 # max point of subtree rooted at this node def __init__(self, interval, value = None): self.ival = interval self.min = interval.min self.max = interval.max self.values = set() if value != None: self.values.add(value) `````` Mikael Boden committed Jul 01, 2018 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 `````` def getInterval(self): """ Retrieve the interval that defines this node :return: the interval """ return self.ival def getMin(self): """ Retrieve the min value for the interval of this node :return: the min value of the interval """ return self.ival.min def getMax(self): """ Retrieve the max value for the interval of this node :return: the max value of the interval """ return self.ival.max `````` Mikael Boden committed Sep 03, 2017 211 `````` def add(self, value): `````` Mikael Boden committed Jul 01, 2018 212 213 214 215 216 `````` """ Add a new value to this node, to the set of values associated with the interval :param value: :return: """ `````` Mikael Boden committed Sep 03, 2017 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 `````` if value: self.values.add(value) def __str__(self): leftstr = 'o' if self.left else 'x' rightstr = 'o' if self.right else 'x' return leftstr + self.ival.__str__() + rightstr def __unicode__(self): leftstr = 'o' if self.left else 'x' rightstr = 'o' if self.right else 'x' return leftstr + self.ival.__unicode__() + rightstr def size(node): if node == None: return 0 else: return node.N def _fix(node): if node == None: return node.N = 1 + size(node.left) + size(node.right) node.min = _min3(node.ival.min, _min(node.left), _min(node.right)) node.max = _max3(node.ival.max, _max(node.left), _max(node.right)) MAX_VALUE = 9E30 MIN_VALUE = -9E30 def _min(node): return MAX_VALUE if node == None else node.min def _max(node): return MIN_VALUE if node == None else node.max def _min3(a, b, c): return min(a, min(b, c)) def _max3(a, b, c): return max(a, max(b, c)) def _rotR(node): y = node.left node.left = y.right y.right = node _fix(node) _fix(y) return y def _rotL(node): y = node.right node.right = y.left y.left = node _fix(node) _fix(y) return y class Stack: """ A stack to support an iterator over IntervalNodes in the IntervalTree. """ def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def push(self, item): self.items.append(item) def pop(self): return self.items.pop() def peek(self): return self.items[len(self.items) - 1] def size(self): return len(self.items) class Interval: """ Define a one-dimensional interval. """ def __init__(self, min, max): if (min <= max): self.min = min self.max = max else: raise RuntimeError def isect(self, that): if (that.max < self.min): return False if (self.max < that.min): return False return True def contains(self, x): return (min <= x) and (x <= max) def __eq__(self, other): if not isinstance(other, Interval): return False return True if (self.min == other.min and self.max == other.max) else False def __ne__(self, other): return not self.__eq__(other) def __lt__(self, other): if not isinstance(other, Interval): return False return True if (self.min < other.min or (self.min == other.min and self.max < other.max)) else False def __gt__(self, other): if not isinstance(other, Interval): return False return True if (self.min > other.min or (self.min == other.min and self.max > other.max)) else False def __str__(self): return '[' + str(self.min) + ', ' + str(self.max) +']' def __unicode__(self): return '[' + str(self.min) + ', ' + str(self.max) +']' def __sizeof__(self): return self.max - self.min `````` Mikael Boden committed Jul 01, 2018 334 335 336 `````` def __len__(self): return self.max - self.min `````` Mikael Boden committed Sep 04, 2017 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 `````` def dist(self, that, signed = False, centre2centre = False): """ Calculate and return the closest distance (from one end of the interval of this to the end of the interval of that). If centre2centre is True, use the centre-to-centre distance instead. If signed is True, the distance is negative if this interval is after the that. """ if not centre2centre: if not signed: if (self.min > that.max): return self.min - that.max # that interval is BEFORE this if (self.max < that.min): return that.min - self.max # that interval is AFTER this else: # distance is signed if (self.min > that.max): return that.max - self.min # that interval is BEFORE this if (self.max < that.min): return that.min - self.max # that interval is AFTER this return 0 else: thiscentre = (self.max - self.min) / 2 + self.min thatcentre = (that.max - that.min) / 2 + that.min return thatcentre - thiscentre if signed else abs(thatcentre - thiscentre) def dist(first, second, signed = False, centre2centre = False): """ Calculate and return the closest distance (from one end of the interval to the other). If centre2centre is True, use the centre-to-centre distance instead. If signed is True, the distance is negative if the first is after the second. """ `````` Mikael Boden committed Sep 03, 2017 360 `````` if isinstance(first, Interval) and isinstance(second, Interval): `````` Mikael Boden committed Sep 04, 2017 361 `````` return first.dist(second, signed, centre2centre) `````` Mikael Boden committed Sep 03, 2017 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 `````` return RuntimeError def union(first, second): if (first.isect(second)): min = first.min if (first.min < second.min) else second.min max = second.max if (first.max < second.max) else first.max return Interval(min, max) else: raise RuntimeError def isect(first, second): if (first.isect(second)): min = first.min if (first.min > second.min) else second.min max = second.max if (first.max > second.max) else first.max return Interval(min, max) else: return None def jaccard(first, second): if (isect(first, second)): isect_min = first.min if (first.min > second.min) else second.min isect_max = second.max if (first.max > second.max) else first.max union_min = first.min if (first.min < second.min) else second.min union_max = second.max if (first.max < second.max) else first.max denom = union_max - union_min if (denom > 0): return (isect_max - isect_min) / denom return 0 else: return 0 if __name__ == '__main__': a = Interval(13, 20) b = Interval(25, 30) c = Interval(27, 33) d = Interval(40, 50) e = Interval(21, 22) f = Interval(36, 38) g = Interval(16, 19) h = Interval(28, 31) i = Interval(55, 66) j = Interval(-3, 0) k = Interval(24, 24) l = Interval(52, 55) `````` Mikael Boden committed Sep 04, 2017 407 408 409 410 `````` print('dist(b,a,signed=False,centre2centre=False)=', dist(b, a, signed = False, centre2centre=False)) print('dist(b,a,signed=True,centre2centre=False)=', dist(b, a, signed = True, centre2centre=False)) print('dist(b,a,signed=False,centre2centre=True)=', dist(b, a, signed = False, centre2centre=True)) print('dist(b,a,signed=True,centre2centre=True)=', dist(b, a, signed = True, centre2centre=True)) `````` Mikael Boden committed Sep 03, 2017 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 `````` t = IntervalTree() t.put(a, 'A') t.put(b, 'B') t.put(c, 'C') t.put(d, 'D') t.put(e, 'E') t.put(b, 123) t.put(b, 'blah') t.get(d).add('x999') t.put(i) t.put(j) t.put(g) t.put(k) print(c in t) print(e in t) print(t.get(a).values) print(t.get(d).values) print(t.get(b).values) print(t.isect(f)) print(t.isect(g)) tryme = f all = t.isectall(tryme) print("Intersect with " + str(tryme) + ": ") for n in all: print('\t' + str(n)) print("Closest to " + str(tryme) + ": ") print(t.closest(tryme)) print('Iterate through tree: ') for n in t: print('\t' + str(n))``````