Example #1
0
class TestSegmentListOverlap(GatTest):

    def setUp(self):
        self.a = SegmentList(iter=((x, x + 10)
                                   for x in range(0, 1000, 100)), normalize = True)

    def testOverlapFull(self):
        self.assertEqual(self.a.overlapWithRange(0, 1000), self.a.sum())

    def testOverlapHalf(self):
        self.assertEqual(self.a.overlapWithRange(0, 500), self.a.sum() / 2)
        self.assertEqual(self.a.overlapWithRange(500, 1000), self.a.sum() / 2)

    def testOverlapAfter(self):
        self.assertEqual(self.a.overlapWithRange(900, 910), 10)
        self.assertEqual(self.a.overlapWithRange(905, 915), 5)

    def testOverlapNone(self):
        self.assertEqual(self.a.overlapWithRange(1000, 2000), 0)
        self.assertEqual(self.a.overlapWithRange(2000, 3000), 0)

    def testOverlapOutOfRange(self):
        self.assertRaises(OverflowError, self.a.overlapWithRange, -100, 5)
        self.assertRaises(OverflowError, self.a.overlapWithRange, -100, -50)
        self.assertEqual(self.a.overlapWithRange(905, 1100), 5)
        self.assertEqual(self.a.overlapWithRange(1000, 1100), 0)

    def testOverlapAll(self):
        for x in range(0, 1000, 100):
            for y in range(0, 10):
                self.assertEqual(self.a.overlapWithRange(x + y, x + y + 1), 1,
                                 "no overlap failure at %i: %i" % (x + y, self.a.overlapWithRange(x + y, x + y + 1)))
            for y in range(10, 100):
                self.assertEqual(self.a.overlapWithRange(
                    x + y, x + y + 1), 0, "overlap failure at %i" % (x + y))
Example #2
0
    def testExtend(self):

        s1 = SegmentList(iter=[(x, x + 100) for x in range(0, 1000, 100)])
        s2 = SegmentList(iter=[(x, x + 100) for x in range(2000, 3000, 100)])
        s1.extend(s2)
        self.assertEqual(s1.sum(), s2.sum() * 2)
        self.assertEqual(len(s1), len(s2) * 2)
Example #3
0
    def testMergeNeighbours(self):

        for y in range(0, 5):
            ss = [(x, x + 100 - y) for x in range(0, 1000, 100)]
            random.shuffle(ss)
            for x in range(0, y + 1):
                s = SegmentList(iter=ss)
                s.merge(x)
                if x < y:
                    self.assertEqual(len(s), 10)
                    self.assertEqual(s.sum(), 1000 - 10 * y)
                else:
                    self.assertEqual(len(s), 1)
                    self.assertEqual(s.sum(), 1000 - y)
Example #4
0
    def testTrim(self):
        '''test trimming over full range of insertion points and deletions.'''

        for point in range(0, 1000):
            for size in range(0, 300):
                ss = [(x, x + 100) for x in range(0, 1000, 100)]
                s = SegmentList(iter=ss,
                                normalize=True)
                orig = s.sum()
                s.trim(point, size)
                self.assertEqual(orig - size, s.sum(),
                                 "trimming error at %i:%i: expected %i, got %i, %s" %
                                 (point, size,
                                  orig - size, s.sum(), str(s)))
Example #5
0
    def testGetFilledSegmentsFromEnd(self):
        ss = [(x, x + 10) for x in range(0, 120, 20)]
        s = SegmentList(iter=ss, normalize=True)
        for x in range(0, 120, 5):
            f = s.getFilledSegmentsFromEnd(x, 20)
            self.assertEqual(f.sum(), 20)
            if x == 0:
                self.assertEqual(f.max(), s.max())
                self.assertEqual(f.min(), 80)
            elif x < 30:
                self.assertEqual(f.max(), s.max())
                self.assertEqual(f.min(), s.min())
            else:
                if x in (40, 60, 80, 100):
                    self.assertEqual(f.min(), x - 40)
                    self.assertEqual(f.max(), x - 10)
                elif x in (30, 50, 70, 90):
                    self.assertEqual(f.min(), x - 30)
                    self.assertEqual(f.max(), x)
                elif x in (45, 65, 85):
                    self.assertEqual(f.min(), x - 40)
                    self.assertEqual(f.max(), x)
                elif x in (35, 55, 75, 95):
                    self.assertEqual(f.min(), x - 35)
                    self.assertEqual(f.max(), x - 5)

            self.assertEqual(s.sum(), s.getFilledSegmentsFromEnd(x, 100).sum())
Example #6
0
 def testGetFilledSegmentsFromStart(self):
     ss = [(x, x + 10) for x in range(0, 120, 20)]
     s = SegmentList(iter=ss, normalize=True)
     for x in range(0, 120, 5):
         f = s.getFilledSegmentsFromStart(x, 20)
         self.assertEqual(f.sum(), 20)
         if x >= 110:
             self.assertEqual(f.min(), s.min())
             self.assertEqual(f.max(), 30)
         elif x > 80:
             self.assertEqual(f.min(), s.min())
             self.assertEqual(f.max(), s.max())
         else:
             if x in (0, 20, 40, 60, 80):
                 self.assertEqual(f.min(), x)
                 self.assertEqual(f.max(), x + 30)
             elif x in (10, 30, 50, 70):
                 self.assertEqual(f.min(), x + 10)
                 self.assertEqual(f.max(), x + 40)
             elif x in (5, 25, 45, 65):
                 self.assertEqual(f.min(), x)
                 self.assertEqual(f.max(), x + 40)
             elif x in (15, 35, 55, 75):
                 self.assertEqual(f.min(), x + 5)
                 self.assertEqual(f.max(), x + 35)
         self.assertEqual(
             s.sum(), s.getFilledSegmentsFromStart(x, 100).sum())
Example #7
0
 def testMergeAdjacent(self):
     ss = [(x, x + 100) for x in range(0, 1000, 100)]
     random.shuffle(ss)
     s = SegmentList(iter=ss)
     s.merge(0)
     self.assertEqual(len(s), 1)
     self.assertEqual(s.sum(), 1000)
Example #8
0
 def testNormalize5(self):
     ss = [(489, 589), (1966, 2066), (2786, 2886), (0, 0), (3889, 3972), (3998, 4098),
           (6441, 6541), (6937, 7054), (7392, 7492), (8154, 8254), (9046, 9146)]
     s = SegmentList(iter=ss)
     s.normalize()
     self.assertEqual(len(s), len([x for x in ss if x[1] - x[0] > 0]))
     self.assertEqual(s.sum(), 1000)
     s2 = SegmentList(iter=ss)
     s2.merge(-1)
     self.assertEqual(s, s2)
Example #9
0
    def testNormalize2(self):
        '''overlapping segments.'''

        ss = [(x, x + 1000) for x in range(0, 1000, 100)]
        random.shuffle(ss)
        s = SegmentList()
        for start, end in ss:
            s.add(start, end)
        s.normalize()
        self.assertEqual(len(s), 1)
        self.assertEqual(s.sum(), 1900)
Example #10
0
 def testNormalize4(self):
     # test multiple interleaved segments
     ss = [(x, x + 100) for x in range(0, 1000, 10)]
     s = SegmentList()
     for start, end in ss:
         s.add(start, end)
     s.normalize()
     self.assertEqual(len(s), 1)
     self.assertEqual(s.sum(), 1090)
     s2 = SegmentList(iter=ss)
     s2.merge(-1)
     self.assertEqual(s, s2)
Example #11
0
    def testNormalize1b(self):
        '''non-overlapping segments.'''

        ss = [(x, x + 10) for x in range(100, 1100, 100)]
        random.shuffle(ss)
        s = SegmentList()
        for start, end in ss:
            s.add(start, end)
        s.normalize()
        self.assertEqual(len(s), 10)
        self.assertEqual(s.sum(), 100)
        s2 = SegmentList(iter=ss)
        s2.merge(-1)
        self.assertEqual(s, s2)
Example #12
0
 def testPartialIntersection(self):
     b = SegmentList(iter=((x, x + 10)
                           for x in range(5, 1000, 100)), normalize = True)
     b.intersect(self.a)
     self.assertEqual(len(b), len(self.a))
     self.assertEqual(b.sum(), self.a.sum() / 2)
Example #13
0
class TestSegmentListIntersection(GatTest):

    def setUp(self):
        self.a = SegmentList(
            iter=((x, x + 10) for x in range(0, 1000, 100)), normalize=True)

    def testIntersectionFull(self):
        b = SegmentList(iter=[(0, 1000)], normalize=True)
        b.intersect(self.a)
        self.assertEqual(b.asList(), self.a.asList())

    def testIntersectionSelf(self):
        self.a.intersect(self.a)
        self.assertEqual(self.a.asList(), self.a.asList())

    def testIntersectionCopy(self):
        b = SegmentList(clone=self.a)
        b.intersect(self.a)
        self.assertEqual(b.asList(), self.a.asList())

    def testNoIntersection(self):
        b = SegmentList(iter=((x, x + 10)
                              for x in range(10, 1000, 100)), normalize = True)
        b.intersect(self.a)
        self.assertEqual(b.asList(), [])
        self.assertEqual(b.isEmpty, True)

    def testPartialIntersection(self):
        b = SegmentList(iter=((x, x + 10)
                              for x in range(5, 1000, 100)), normalize = True)
        b.intersect(self.a)
        self.assertEqual(len(b), len(self.a))
        self.assertEqual(b.sum(), self.a.sum() / 2)

    def testOverlap(self):
        '''test if number of segments intersection is correct.'''

        b = SegmentList(iter=((x, x + 10)
                              for x in range(5, 1000, 100)), normalize = True)
        self.assertEqual(self.a.intersectionWithSegments(b), len(b))
        self.assertEqual(b.intersectionWithSegments(self.a), len(b))

        # no intersection
        b = SegmentList(iter=((x, x + 10)
                              for x in range(10, 1000, 100)), normalize = True)
        self.assertEqual(self.a.intersectionWithSegments(b), 0)
        self.assertEqual(b.intersectionWithSegments(self.a), 0)

        # double the number of segments in b
        b = SegmentList(iter=[(x, x + 5) for x in range(0, 1000, 100)] +
                        [(x + 5, x + 10) for x in range(0, 1000, 100)],
                        normalize=True)
        self.assertEqual(self.a.intersectionWithSegments(b), 10)
        self.assertEqual(b.intersectionWithSegments(self.a), 20)

    def testFilter(self):

        b = SegmentList(iter=((x, x + 5)
                              for x in range(500, 2000, 100)), normalize=True)
        b.filter(self.a)
        self.assertEqual(b.asList(), [
            (500, 505), (600, 605), (700, 705), (800, 805),
            (900, 905)])

        b = SegmentList(iter=((0, 56), ))
        c = SegmentList(iter=[(0, 50), (75, 125)])
        b.filter(c)
        self.assertEqual(b.asList(), [(0, 56)])

        b = SegmentList(iter=((0, 56), ))
        c = SegmentList(iter=[(0, 10)])
        b.filter(c)
        self.assertEqual(b.asList(), [(0, 56)])
Example #14
0
 def testExpand(self):
     ss = [(x, x + 10) for x in range(0, 1000, 100)]
     s = SegmentList(iter=ss, normalize=True)
     s.expand_segments(2.0)
     self.assertEqual(s.sum(), 195)
     self.assertEqual(len(s), 10)