Exemplo n.º 1
0
    def test_max_dist(self):
        iv = self.intervals
        r = iv.right(Interval(1, 1), max_dist=0, n=10)
        self.assertEqual(len(r), 0)

        for n, d in enumerate(range(10, 1000, 10)):
            r = iv.right(Interval(1, 1), max_dist=d, n=10000)
            self.assertEqual(len(r), n + 1)
Exemplo n.º 2
0
 def setUp(self):
     iv = IntervalNode(Interval(50, 59))
     for i in range(0, 110, 10):
         if i == 50: continue
         f = Interval(i, i + 9)
         iv = iv.insert(f)
     self.intervals = iv
     print(iv)
Exemplo n.º 3
0
    def test_count(self):
        iv = self.intervals

        r = iv.right(Interval(1, 1), n=33)
        # print(len(r))
        self.assertEqual(len(r), 33)

        l = iv.left(Interval(1, 1), n=33)
        self.assertEqual(len(l), 1)
Exemplo n.º 4
0
    def test_left(self):
        iv = self.intervals
        # print(iv.left(Interval(60, 70)))
        self.assertEqual(str(iv.left(Interval(60, 70), n=2)), str([Interval(50, 59), Interval(40, 49)]))

        for i in range(10, 100, 10):
            f = Interval(i, i)
            r = iv.left(f, max_dist=10, n=1)
            self.assertEqual(r[0].end, i - 1)
Exemplo n.º 5
0
    def setUp(self):
        iv = IntervalNode(Interval(1, 2))
        self.max = 1000000
        for i in range(0, self.max, 10):
            f = Interval(i, i)
            iv = iv.insert(f)

        for i in range(6000):
            iv = iv.insert(Interval(0, 1))
        self.intervals = iv
Exemplo n.º 6
0
 def test_1(self):
     tree = IntervalTree()
     tree.add(1, 3, 100)
     tree.add(3, 7, 110)
     tree.add(2, 5, 120)
     tree.add(4, 6, 130)
     print(tree.pretty_print())
     print(tree.find(Interval(2, 5)))
     tree.remove(Interval(2, 5))
     print(tree.find(Interval(2, 5)))
     print(tree.pretty_print())
     self.assertEqual(True, True)
Exemplo n.º 7
0
 def test_n(self):
     iv = self.intervals
     for i in range(0, 90, 10):
         f = Interval(i + 1, i + 1)
         r = iv.right(f, max_dist=20, n=2)
         self.assertEqual(r[0].start, i + 10)
         self.assertEqual(r[1].start, i + 20)
Exemplo n.º 8
0
    def test_right(self):
        iv = self.intervals
        self.assertEqual(str(iv.left(Interval(60, 70), n=2)), str([Interval(50, 59), Interval(40, 49)]))

        def get_right_start(b10):
            r = iv.right(Interval(b10, b10 + 1), n=1)
            assert len(r) == 1
            return r[0].start

        for i in range(10, 100, 10):
            self.assertEqual(get_right_start(i), i + 10)

        for i in range(0, 100, 10):
            f = Interval(i - 1, i - 1)
            r = iv.right(f, max_dist=10, n=1)
            self.assertEqual(r[0].start, i)
Exemplo n.º 9
0
 def setUp(self) -> None:
     intervals = []
     for i in range(0, 30, 5):
         intervals.append(Interval(i, i + 7))
     iv = IntervalTree()
     for f in intervals:
         iv.insert(f)
     self.intervals = intervals
     self.tree = iv
     pass
Exemplo n.º 10
0
    def test_tree_pickle(self):
        a = IntervalTree()
        for ichr in range(5):
            for i in range(10, 100, 6):
                f = Interval(i - 4, i + 4)
                a.insert(f)

        a.dump('a.pkl')

        b = IntervalTree()
        b.load('a.pkl')
        for ichr in range(5):
            for i in range(10, 100, 6):
                f = Interval(i - 4, i + 4)
                af = sorted(a.find(f), key=operator.attrgetter('start'))
                bf = sorted(b.find(f), key=operator.attrgetter('start'))

                assert len(bf) > 0
                self.assertEqual(len(af), len(bf))
                self.assertEqual(af[0].start, bf[0].start)
                self.assertEqual(af[-1].start, bf[-1].start)
Exemplo n.º 11
0
    def setUp(self):
        intervals = []
        for i in range(11, 20000, 15):
            for zz in range(random.randint(2, 5)):
                m = random.randint(1, 10)
                p = random.randint(1, 10)
                intervals.append(Interval(i - m, i + p))
        iv = IntervalNode(intervals[0])
        for f in intervals[1:]:
            iv = iv.insert(f)

        self.intervals = intervals
        self.tree = iv
Exemplo n.º 12
0
    def addDeterminants(self, text, deter_rule, matches, match_begin,
                        match_end, current_position):
        deter_rule = deter_rule[FastCNER.END]
        end = current_position if match_end == 0 else match_end
        # in case the rules were not configured properly, this can ensure they won't break the execution.
        if match_begin > end:
            t = match_begin
            match_begin = end
            end = t
        current_span = Span(match_begin + self.offset, end + self.offset,
                            text[match_begin:end])

        current_spans_list = []
        overlap_checkers = self.overlap_checkers
        for key in deter_rule.keys():
            rule_id = deter_rule[key]
            if self.logger is not None:
                self.logger.debug('try add matched rule ({}-{})\t{}'.format(
                    match_begin, match_end, str(self.rule_store[rule_id])))
            current_span.rule_id = rule_id
            if key in overlap_checkers:
                current_spans_list = matches[key]
                overlap_checker = overlap_checkers[key]
                overlapped_pos = overlap_checker.search(
                    current_span.begin, current_span.end)
                if len(overlapped_pos) > 0:
                    pos = overlapped_pos.pop().data
                    overlapped_span = current_spans_list[pos]
                    if not self.compareSpan(current_span, overlapped_span):
                        continue
                    current_spans_list[pos] = current_span
                    overlap_checker.remove(
                        Interval(current_span.begin, current_span.end))
                    overlap_checker.add(current_span.begin, current_span.end,
                                        pos)
                else:
                    overlap_checker.add(current_span.begin, current_span.end,
                                        len(current_spans_list))
                    current_spans_list.append(current_span)
            else:
                matches[key] = current_spans_list
                overlap_checker = IntervalTree()
                # quickset's search will include both lower and upper bounds, so minus one from the end.
                overlap_checker.add(current_span.begin, current_span.end - 1,
                                    len(current_spans_list))
                current_spans_list.append(current_span)
                overlap_checkers[key] = overlap_checker

        pass
Exemplo n.º 13
0
    def test_p(self):
        print(self.tree.pretty_print())
        print(len(self.tree.search(1, 220)))
        # self.assertEqual(len(self.tree.search(1, 220)), 16)
        print(len(self.tree.search(0, 220)))
        print(self.tree.pretty_print())
        invs = []
        for i in range(0, 20, 5):
            invs.append(Interval(i, i + 7))
        print(len(invs))

        for i in invs:
            self.tree.remove(i)
            print("remove: {}".format(i))
            # print(self.tree.search(0, 220))
        print(self.tree.search(0, 220))
        # print(self.tree.remove(IntervalX(11, 18)))
        print(len(self.tree.search(0, 220)))
Exemplo n.º 14
0
    def test_left(self):
        max_dist = 200
        n = 15
        iv = self.tree
        for i in range(11, 20000, 25):
            for zz in range(random.randint(2, 5)):
                s1 = random.randint(i + 1, i + 20)
                f = Interval(s1, s1)

                bf = brute_force_find_left(self.intervals, f, max_dist, n)
                tf = iv.left(f, max_dist=max_dist, n=n)
                if len(tf) == 0:
                    assert len(bf) == 0, bf
                    continue

                mdist = max(distancex(f, t) for t in tf)
                self.assertTrue(set(bf).issuperset(tf))
                diff = set(bf).difference(tf)
                self.assertTrue(len(diff) == 0, (diff))
Exemplo n.º 15
0
 def test_left(self):
     self.tree.left(Interval(46, 47))
Exemplo n.º 16
0
 def test_n(self):
     iv = self.intervals
     for i in range(0, 90, 10):
         f = Interval(i + 1, i + 1)
         r = iv.right(f, max_dist=20, n=2)
         print(hasattr(iv, 'start'))
Exemplo n.º 17
0
# tree.add(16, 21, 160)
# tree.add(17, 19, 160)
# tree.add(19, 20, 150)
# tree.add(25, 30, 160)
# tree.add(26, 26, 160)
# tree.add(27, 28, 160)
# tree.add(27, 28, 160)
# tree.add(27, 28, 160)
# print(tree.pretty_print())
class MyTestCase(unittest.TestCase):
    def test_1(self):
        tree = IntervalTree()
        tree.add(1, 3, 100)
        tree.add(3, 7, 110)
        tree.add(2, 5, 120)
        tree.add(4, 6, 130)
        tree.add(4, 8, 140)
        tree.add(4, 8, 150)
        tree.add(5, 7, 160)
        print(tree.pretty_print())
        print(tree.find(Interval(2, 5)))
        tree.remove(Interval(2, 5))
        print(tree.find(Interval(2, 5)))
        print(tree.pretty_print())
        self.assertEqual(True, True)


if __name__ == '__main__':
    unittest.main()
print(Interval(1, 2).__reduce__())
Exemplo n.º 18
0
 def test_toomany(self):
     iv = self.intervals
     self.assertEqual(len(iv.left(Interval(60, 70), n=200)), 6)
Exemplo n.º 19
0
 def get_right_start(b10):
     r = iv.right(Interval(b10, b10 + 1), n=1)
     assert len(r) == 1
     return r[0].start
Exemplo n.º 20
0
 def test_1(self):
     print(self.tree.pretty_print())
     self.tree.remove(Interval(5, 12))
     print(self.tree.pretty_print())
     pass
Exemplo n.º 21
0
 def test_feature_pickle(self):
     f = Interval(22, 38, data={'a': 22})
     g = loads(dumps(f))
     self.assertEqual(f.start, g.start)
     self.assertEqual(g.data['a'], 22)
Exemplo n.º 22
0
 def test_find(self):
     self.tree.find(Interval(46, 47))
Exemplo n.º 23
0
 def test_error(self):
     with self.assertRaises(ValueError):
         Interval(3, 1)
Exemplo n.º 24
0
 def test_left(self):
     self.assertEqual(2, len(self.tree4.left(Interval(44, 55))))
     self.assertEqual(0, len(self.tree4.left(Interval(11, 12))))
Exemplo n.º 25
0
 def setUp(self):
     self.tree4 = IntervalTree()
     self.tree4.insert(Interval(22, 33, data='example1'))
     self.tree4.insert(Interval(22, 33, data='example2'))
Exemplo n.º 26
0
 def test_right(self):
     self.tree.right(Interval(46, 47))