Ejemplo n.º 1
0
    def testDot(self):
        """Test HeavyFootprint::dot"""
        size = 20, 20
        for xOffset, yOffset in [(0, 0), (0, 3), (3, 0), (2, 2)]:
            mi1 = afwImage.MaskedImageF(*size)
            mi2 = afwImage.MaskedImageF(*size)
            mi1.set(0)
            mi2.set(0)

            spanList1 = []
            spanList2 = []
            for y, x0, x1 in [
                (5, 3, 7),
                (6, 3, 4),
                (6, 6, 7),
                (7, 3, 7),
            ]:
                spanList1.append(afwGeom.Span(y, x0, x1))
                spanList2.append(
                    afwGeom.Span(y + yOffset, x0 + xOffset, x1 + xOffset))
                for x in range(x0, x1 + 1):
                    value = (x + y, 0, 1.0)
                    mi1[x, y, afwImage.LOCAL] = value
                    mi2[x + xOffset, y + yOffset, afwImage.LOCAL] = value

            fp1 = afwDetect.Footprint(afwGeom.SpanSet(spanList1))
            fp2 = afwDetect.Footprint(afwGeom.SpanSet(spanList2))

            hfp1 = afwDetect.makeHeavyFootprint(fp1, mi1)
            hfp2 = afwDetect.makeHeavyFootprint(fp2, mi2)

            dot = np.vdot(mi1.getImage().getArray(), mi2.getImage().getArray())
            self.assertEqual(hfp1.dot(hfp2), dot)
            self.assertEqual(hfp2.dot(hfp1), dot)
Ejemplo n.º 2
0
    def testCopyWithinFootprintImage(self):
        W, H = 10, 10
        dims = lsst.geom.Extent2I(W, H)
        source = afwImage.ImageF(dims)
        dest = afwImage.ImageF(dims)
        sa = source.getArray()
        for i in range(H):
            for j in range(W):
                sa[i, j] = 100 * i + j

        footSpans = [s for s in self.foot.spans]
        footSpans.append(afwGeom.Span(4, 3, 6))
        footSpans.append(afwGeom.Span(5, 2, 4))
        self.foot.spans = afwGeom.SpanSet(footSpans)

        self.foot.spans.copyImage(source, dest)

        da = dest.getArray()
        self.assertEqual(da[4, 2], 0)
        self.assertEqual(da[4, 3], 403)
        self.assertEqual(da[4, 4], 404)
        self.assertEqual(da[4, 5], 405)
        self.assertEqual(da[4, 6], 406)
        self.assertEqual(da[4, 7], 0)
        self.assertEqual(da[5, 1], 0)
        self.assertEqual(da[5, 2], 502)
        self.assertEqual(da[5, 3], 503)
        self.assertEqual(da[5, 4], 504)
        self.assertEqual(da[5, 5], 0)
        self.assertTrue(np.all(da[:4, :] == 0))
        self.assertTrue(np.all(da[6:, :] == 0))
Ejemplo n.º 3
0
    def testCopyWithinFootprintMaskedImage(self):
        W, H = 10, 10
        dims = lsst.geom.Extent2I(W, H)
        source = afwImage.MaskedImageF(dims)
        dest = afwImage.MaskedImageF(dims)
        sa = source.getImage().getArray()
        sv = source.getVariance().getArray()
        sm = source.getMask().getArray()
        for i in range(H):
            for j in range(W):
                sa[i, j] = 100 * i + j
                sv[i, j] = 100 * j + i
                sm[i, j] = 1

        footSpans = [s for s in self.foot.spans]
        footSpans.append(afwGeom.Span(4, 3, 6))
        footSpans.append(afwGeom.Span(5, 2, 4))
        self.foot.spans = afwGeom.SpanSet(footSpans)

        self.foot.spans.copyMaskedImage(source, dest)

        da = dest.getImage().getArray()
        dv = dest.getVariance().getArray()
        dm = dest.getMask().getArray()

        self.assertEqual(da[4, 2], 0)
        self.assertEqual(da[4, 3], 403)
        self.assertEqual(da[4, 4], 404)
        self.assertEqual(da[4, 5], 405)
        self.assertEqual(da[4, 6], 406)
        self.assertEqual(da[4, 7], 0)
        self.assertEqual(da[5, 1], 0)
        self.assertEqual(da[5, 2], 502)
        self.assertEqual(da[5, 3], 503)
        self.assertEqual(da[5, 4], 504)
        self.assertEqual(da[5, 5], 0)
        self.assertTrue(np.all(da[:4, :] == 0))
        self.assertTrue(np.all(da[6:, :] == 0))

        self.assertEqual(dv[4, 2], 0)
        self.assertEqual(dv[4, 3], 304)
        self.assertEqual(dv[4, 4], 404)
        self.assertEqual(dv[4, 5], 504)
        self.assertEqual(dv[4, 6], 604)
        self.assertEqual(dv[4, 7], 0)
        self.assertEqual(dv[5, 1], 0)
        self.assertEqual(dv[5, 2], 205)
        self.assertEqual(dv[5, 3], 305)
        self.assertEqual(dv[5, 4], 405)
        self.assertEqual(dv[5, 5], 0)
        self.assertTrue(np.all(dv[:4, :] == 0))
        self.assertTrue(np.all(dv[6:, :] == 0))

        self.assertTrue(np.all(dm[4, 3:7] == 1))
        self.assertTrue(np.all(dm[5, 2:5] == 1))
        self.assertTrue(np.all(dm[:4, :] == 0))
        self.assertTrue(np.all(dm[6:, :] == 0))
        self.assertTrue(np.all(dm[4, :3] == 0))
        self.assertTrue(np.all(dm[4, 7:] == 0))
Ejemplo n.º 4
0
    def testIsContiguous(self):
        spanSetConList = [afwGeom.Span(0, 2, 5), afwGeom.Span(1, 5, 8)]
        spanSetCon = afwGeom.SpanSet(spanSetConList)
        self.assertTrue(spanSetCon.isContiguous())

        spanSetNotConList = [afwGeom.Span(0, 2, 5), afwGeom.Span(1, 20, 25)]
        spanSetNotCon = afwGeom.SpanSet(spanSetNotConList)
        self.assertFalse(spanSetNotCon.isContiguous())
Ejemplo n.º 5
0
    def testMergeFootprints(self):
        f1 = self.foot
        f2 = afwDetect.Footprint()

        spanList1 = [(10, 10, 20), (10, 30, 40), (10, 50, 60), (11, 30, 50),
                     (12, 30, 50), (13, 10, 20), (13, 30, 40), (13, 50, 60),
                     (15, 10, 20), (15, 31, 40), (15, 51, 60)]
        spanSet1 = afwGeom.SpanSet([afwGeom.Span(*span) for span in spanList1])
        f1.spans = spanSet1

        spanList2 = [(8, 10, 20), (9, 20, 30), (10, 0, 9), (10, 35, 65),
                     (10, 70, 80), (13, 49, 54), (14, 10, 30), (15, 21, 30),
                     (15, 41, 50), (15, 61, 70)]
        spanSet2 = afwGeom.SpanSet([afwGeom.Span(*span) for span in spanList2])
        f2.spans = spanSet2

        fA = afwDetect.mergeFootprints(f1, f2)
        fB = afwDetect.mergeFootprints(f2, f1)

        ims = []
        for i, f in enumerate([f1, f2, fA, fB]):
            im1 = afwImage.ImageU(100, 100)
            im1.set(0)
            imbb = im1.getBBox()
            f.setRegion(imbb)
            f.spans.setImage(im1, 1)
            ims.append(im1)

        for i, merged in enumerate([ims[2], ims[3]]):
            m = merged.getArray()
            a1 = ims[0].getArray()
            a2 = ims[1].getArray()
            # Slightly looser tests to start...
            # Every pixel in f1 is in f[AB]
            self.assertTrue(np.all(m.flat[np.flatnonzero(a1)] == 1))
            # Every pixel in f2 is in f[AB]
            self.assertTrue(np.all(m.flat[np.flatnonzero(a2)] == 1))
            # merged == a1 | a2.
            self.assertTrue(np.all(m == np.maximum(a1, a2)))

        if False:
            import matplotlib
            matplotlib.use('Agg')
            import pylab as plt
            plt.clf()
            for i, im1 in enumerate(ims):
                plt.subplot(4, 1, i + 1)
                plt.imshow(im1.getArray(),
                           interpolation='nearest',
                           origin='lower')
                plt.axis([0, 100, 0, 20])
            plt.savefig('merge2.png')
Ejemplo n.º 6
0
    def testIteratorConstructor(self):
        spans = [afwGeom.Span(0, 2, 4), afwGeom.Span(1, 2, 4),
                 afwGeom.Span(2, 2, 4)]
        spanSetFromList = afwGeom.SpanSet(spans)
        spanSetFromArray = afwGeom.SpanSet(np.array(spans))

        self.assertEqual(spanSetFromList.getBBox().getMinX(), 2)
        self.assertEqual(spanSetFromList.getBBox().getMaxX(), 4)
        self.assertEqual(spanSetFromList.getBBox().getMinY(), 0)

        self.assertEqual(spanSetFromArray.getBBox().getMinX(), 2)
        self.assertEqual(spanSetFromArray.getBBox().getMaxX(), 4)
        self.assertEqual(spanSetFromArray.getBBox().getMinY(), 0)
Ejemplo n.º 7
0
    def testCopyWithinFootprintOutside(self):
        """Copy a footprint that is larger than the image"""
        target = afwImage.ImageF(100, 100)
        target.set(0)
        subTarget = afwImage.ImageF(
            target,
            lsst.geom.Box2I(lsst.geom.Point2I(40, 40),
                            lsst.geom.Extent2I(20, 20)))
        source = afwImage.ImageF(10, 30)
        source.setXY0(45, 45)
        source.set(1.0)

        foot = afwDetect.Footprint()
        spanList = [
            afwGeom.Span(*s) for s in (
                (
                    50, 50, 60
                ),  # Oversized on the source image, right; only some pixels overlap
                (
                    60, 0, 100
                ),  # Oversized on the source, left and right; and on sub-target image, top
                (
                    99, 0, 1000
                ),  # Oversized on the source image, top, left and right; aiming for segfault
            )
        ]
        foot.spans = afwGeom.SpanSet(spanList)

        foot.spans.clippedTo(subTarget.getBBox()).clippedTo(source.getBBox()).\
            copyImage(source, subTarget)

        expected = np.zeros((100, 100))
        expected[50, 50:55] = 1.0

        self.assertTrue(np.all(target.getArray() == expected))
Ejemplo n.º 8
0
    def testBbox(self):
        """Add Spans and check bounding box"""
        foot = afwDetect.Footprint()
        spanLists = [afwGeom.Span(10, 100, 105), afwGeom.Span(11, 99, 104)]
        spanSet = afwGeom.SpanSet(spanLists)
        foot.spans = spanSet

        bbox = foot.getBBox()
        self.assertEqual(bbox.getWidth(), 7)
        self.assertEqual(bbox.getHeight(), 2)
        self.assertEqual(bbox.getMinX(), 99)
        self.assertEqual(bbox.getMinY(), 10)
        self.assertEqual(bbox.getMaxX(), 105)
        self.assertEqual(bbox.getMaxY(), 11)
        # clip with a bbox that doesn't overlap at all
        bbox2 = lsst.geom.Box2I(lsst.geom.Point2I(5, 90), lsst.geom.Extent2I(1, 2))
        foot.clipTo(bbox2)
        self.assertTrue(foot.getBBox().isEmpty())
        self.assertEqual(foot.getArea(), 0)
Ejemplo n.º 9
0
    def setUp(self):
        self.mi = afwImage.MaskedImageF(20, 10)
        self.objectPixelVal = (10, 0x1, 100)

        spanList = []
        for y, x0, x1 in [(2, 10, 13), (3, 11, 14)]:
            spanList.append(afwGeom.Span(y, x0, x1))

            for x in range(x0, x1 + 1):
                self.mi[x, y, afwImage.LOCAL] = self.objectPixelVal
        self.foot = afwDetect.Footprint(afwGeom.SpanSet(spanList))
Ejemplo n.º 10
0
    def testUnflatten(self):
        inputArray = np.ones(6) * 4
        inputSpanSet = afwGeom.SpanSet([afwGeom.Span(9, 2, 3),
                                        afwGeom.Span(10, 3, 4),
                                        afwGeom.Span(11, 2, 3)])
        outputArray = inputSpanSet.unflatten(inputArray)

        arrayShape = outputArray.shape
        bBox = inputSpanSet.getBBox()
        self.assertEqual(arrayShape[0], bBox.getHeight())
        self.assertEqual(arrayShape[1], bBox.getWidth())

        # Test unflattening a 2D array
        spanSetArea = afwGeom.SpanSet.fromShape(2, afwGeom.Stencil.BOX)
        spanSetArea = spanSetArea.shiftedBy(2, 2)

        testArray = np.arange(5*5*3).reshape(5*5, 3)
        unflattened3DArray = spanSetArea.unflatten(testArray)

        truthArray = np.arange(5*5*3).reshape(5, 5, 3)
        self.assertFloatsAlmostEqual(unflattened3DArray, truthArray)
Ejemplo n.º 11
0
    def testSplit(self):
        spanList = [
            afwGeom.Span(0, 2, 4),
            afwGeom.Span(1, 2, 4),
            afwGeom.Span(2, 2, 4),
            afwGeom.Span(10, 4, 7),
            afwGeom.Span(11, 4, 7),
            afwGeom.Span(12, 4, 7)
        ]

        spans = afwGeom.SpanSet(spanList)
        region = lsst.geom.Box2I(lsst.geom.PointI(-6, -6),
                                 lsst.geom.PointI(20, 20))
        multiFoot = afwDet.Footprint(spans, region)

        records = [multiFoot.addPeak(3, 1, 100), multiFoot.addPeak(5, 11, 100)]

        # Verify that the footprint is multi-component
        self.assertFalse(multiFoot.isContiguous())

        footprintList = multiFoot.split()

        self.assertEqual(len(footprintList), 2)
        for i, fp in enumerate(footprintList):
            # check that the correct Spans are populated for each
            tempSpan = afwGeom.SpanSet(spanList[i * 3:i * 3 + 3])
            self.assertEqual(fp.spans, tempSpan)

            # check that the peaks are split properly
            self.assertEqual(len(fp.peaks), 1)
            self.assertEqual(fp.peaks[0], records[i])
Ejemplo n.º 12
0
    def testMergeFootprints(self):
        f1 = self.foot
        f2 = afwDetect.Footprint()

        spanList1 = [(10, 10, 20), (10, 30, 40), (10, 50, 60), (11, 30, 50),
                     (12, 30, 50), (13, 10, 20), (13, 30, 40), (13, 50, 60),
                     (15, 10, 20), (15, 31, 40), (15, 51, 60)]
        spanSet1 = afwGeom.SpanSet([afwGeom.Span(*span) for span in spanList1])
        f1.spans = spanSet1

        spanList2 = [(8, 10, 20), (9, 20, 30), (10, 0, 9), (10, 35, 65),
                     (10, 70, 80), (13, 49, 54), (14, 10, 30), (15, 21, 30),
                     (15, 41, 50), (15, 61, 70)]
        spanSet2 = afwGeom.SpanSet([afwGeom.Span(*span) for span in spanList2])
        f2.spans = spanSet2

        fA = afwDetect.mergeFootprints(f1, f2)
        fB = afwDetect.mergeFootprints(f2, f1)

        ims = []
        for i, f in enumerate([f1, f2, fA, fB]):
            im1 = afwImage.ImageU(100, 100)
            im1.set(0)
            imbb = im1.getBBox()
            f.setRegion(imbb)
            f.spans.setImage(im1, 1)
            ims.append(im1)

        for i, merged in enumerate([ims[2], ims[3]]):
            m = merged.getArray()
            a1 = ims[0].getArray()
            a2 = ims[1].getArray()
            # Slightly looser tests to start...
            # Every pixel in f1 is in f[AB]
            self.assertTrue(np.all(m.flat[np.flatnonzero(a1)] == 1))
            # Every pixel in f2 is in f[AB]
            self.assertTrue(np.all(m.flat[np.flatnonzero(a2)] == 1))
            # merged == a1 | a2.
            self.assertTrue(np.all(m == np.maximum(a1, a2)))
Ejemplo n.º 13
0
    def testEdge(self):
        """Test for Footprint::findEdgePixels()"""
        foot = afwDetect.Footprint()
        spanList = [
            afwGeom.Span(*span) for span in ((3, 3, 9), (4, 2, 4), (4, 6, 7),
                                             (4, 9, 11), (5, 3, 9), (6, 6, 7))
        ]
        foot.spans = afwGeom.SpanSet(spanList)
        self.checkEdge(foot)

        # This footprint came from a very large Footprint in a deep HSC coadd patch
        self.checkEdge(
            afwDetect.Footprint.readFits(
                os.path.join(testPath, "testFootprintEdge.fits")))
Ejemplo n.º 14
0
    def testFlatten(self):
        # Give an initial value to an input array
        inputArray = np.ones((6, 6)) * 9
        inputArray[1, 1] = 1
        inputArray[1, 2] = 2
        inputArray[2, 1] = 3
        inputArray[2, 2] = 4

        inputSpanSet = afwGeom.SpanSet([afwGeom.Span(0, 0, 1),
                                        afwGeom.Span(1, 0, 1)])
        flatArr = inputSpanSet.flatten(inputArray, afwGeom.Point2I(-1, -1))

        self.assertEqual(flatArr.size, inputSpanSet.getArea())

        # Test flatttening a 3D array
        spanSetArea = afwGeom.SpanSet.fromShape(2, afwGeom.Stencil.BOX)
        spanSetArea = spanSetArea.shiftedBy(2, 2)

        testArray = np.arange(5*5*3).reshape(5, 5, 3)
        flattened2DArray = spanSetArea.flatten(testArray)

        truthArray = np.arange(5*5*3).reshape(5*5, 3)
        self.assertFloatsAlmostEqual(flattened2DArray, truthArray)
Ejemplo n.º 15
0
    def testFootprintToBBoxList(self):
        """Test footprintToBBoxList"""
        region = lsst.geom.Box2I(lsst.geom.Point2I(0, 0),
                                 lsst.geom.Extent2I(12, 10))
        foot = afwDetect.Footprint(afwGeom.SpanSet(), region)
        spanList = [
            afwGeom.Span(*span)
            for span in ((3, 3, 5), (3, 7, 7), (4, 2, 3), (4, 5, 7), (5, 2, 3),
                         (5, 5, 8), (6, 3, 5))
        ]
        foot.spans = afwGeom.SpanSet(spanList)

        idImage = afwImage.ImageU(region.getDimensions())
        idImage.set(0)

        foot.spans.setImage(idImage, 1)
        if display:
            disp = afwDisplay.Display(frame=1)
            disp.mtv(idImage, title=self._testMethodName + " image")

        idImageFromBBox = idImage.Factory(idImage, True)
        idImageFromBBox.set(0)
        bboxes = afwDetect.footprintToBBoxList(foot)
        for bbox in bboxes:
            x0, y0, x1, y1 = bbox.getMinX(), bbox.getMinY(), \
                bbox.getMaxX(), bbox.getMaxY()

            for y in range(y0, y1 + 1):
                for x in range(x0, x1 + 1):
                    idImageFromBBox[x, y, afwImage.LOCAL] = 1

            if display:
                x0 -= 0.5
                y0 -= 0.5
                x1 += 0.5
                y1 += 0.5

                disp.line([(x0, y0), (x1, y0), (x1, y1), (x0, y1), (x0, y0)],
                          ctype=afwDisplay.RED)

        idImageFromBBox -= idImage  # should be blank
        stats = afwMath.makeStatistics(idImageFromBBox, afwMath.MAX)

        self.assertEqual(stats.getValue(), 0)
Ejemplo n.º 16
0
    def testCircle(self):
        xc, yc = 30, 50
        radius = 10

        spanSet = afwGeom.SpanSet.fromShape(radius).shiftedBy(xc, yc)
        test = afwDet.Footprint(spanSet)

        # Here's how it used to be done using circles, before #1556
        r2 = int(radius**2 + 0.5)
        r = int(math.sqrt(r2))
        spanList = []
        for i in range(-r, r + 1):
            hlen = int(math.sqrt(r2 - i**2))
            spanList.append(afwGeom.Span(yc + i, xc - hlen, xc + hlen))
        control = afwDet.Footprint(afwGeom.SpanSet(spanList))

        self.assertEqual(len(test.getSpans()), len(control.getSpans()))
        for s0, s1 in zip(test.getSpans(), control.getSpans()):
            self.assertEqual(s0.getX0(), s1.getX0())
            self.assertEqual(s0.getX1(), s1.getX1())
            self.assertEqual(s0.getY(), s1.getY())
        self.assertEqual(test.getArea(), control.getArea())
Ejemplo n.º 17
0
    def testWriteDefect(self):
        """Write a Footprint as a set of Defects"""
        region = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Extent2I(12, 10))
        spanSet = afwGeom.SpanSet([afwGeom.Span(*span) for span in [(3, 3, 5),
                                                                    (3, 7, 7),
                                                                    (4, 2, 3),
                                                                    (4, 5, 7),
                                                                    (5, 2, 3),
                                                                    (5, 5, 8),
                                                                    (6, 3, 5)]])
        foot = afwDetect.Footprint(spanSet, region)

        openedFile = False
        if True:
            fd = open("/dev/null", "w")
            openedFile = True
        else:
            fd = sys.stdout

        afwDetectUtils.writeFootprintAsDefects(fd, foot)
        if openedFile:
            fd.close()
Ejemplo n.º 18
0
    def testLessThan(self):
        span1 = afwGeom.Span(42, 0, 100)
        span2 = afwGeom.Span(41, 0, 100)
        span3 = afwGeom.Span(43, 0, 100)
        span4 = afwGeom.Span(42, -100, 100)
        span5 = afwGeom.Span(42, 100, 200)
        span6 = afwGeom.Span(42, 0, 10)
        span7 = afwGeom.Span(42, 0, 200)
        span8 = afwGeom.Span(42, 0, 100)

        # Cannot use assertLess and friends here
        # because Span only has operator <
        def assertOrder(x1, x2):
            self.assertTrue(x1 < x2)
            self.assertFalse(x2 < x1)

        assertOrder(span2, span1)
        assertOrder(span1, span3)
        assertOrder(span4, span1)
        assertOrder(span1, span5)
        assertOrder(span6, span1)
        assertOrder(span1, span7)
        self.assertFalse(span1 < span8)
        self.assertFalse(span8 < span1)
Ejemplo n.º 19
0
    def testMergeHeavyFootprints(self):
        mi = afwImage.MaskedImageF(20, 10)
        objectPixelVal = (42, 0x9, 400)

        spanList = []
        for y, x0, x1 in [(1, 9, 12), (2, 12, 13), (3, 11, 15)]:
            spanList.append(afwGeom.Span(y, x0, x1))
            for x in range(x0, x1 + 1):
                mi[x, y, afwImage.LOCAL] = objectPixelVal

        foot = afwDetect.Footprint(afwGeom.SpanSet(spanList))

        hfoot1 = afwDetect.makeHeavyFootprint(self.foot, self.mi)
        hfoot2 = afwDetect.makeHeavyFootprint(foot, mi)

        hsum = afwDetect.mergeHeavyFootprints(hfoot1, hfoot2)

        bb = hsum.getBBox()
        self.assertEqual(bb.getMinX(), 9)
        self.assertEqual(bb.getMaxX(), 15)
        self.assertEqual(bb.getMinY(), 1)
        self.assertEqual(bb.getMaxY(), 3)

        msum = afwImage.MaskedImageF(20, 10)
        hsum.insert(msum)

        sa = msum.getImage().getArray()

        self.assertFloatsEqual(sa[1, 9:13], objectPixelVal[0])
        self.assertFloatsEqual(sa[2, 12:14],
                               objectPixelVal[0] + self.objectPixelVal[0])
        self.assertFloatsEqual(sa[2, 10:12], self.objectPixelVal[0])

        sv = msum.getVariance().getArray()

        self.assertFloatsEqual(sv[1, 9:13], objectPixelVal[2])
        self.assertFloatsEqual(sv[2, 12:14],
                               objectPixelVal[2] + self.objectPixelVal[2])
        self.assertFloatsEqual(sv[2, 10:12], self.objectPixelVal[2])

        sm = msum.getMask().getArray()

        self.assertFloatsEqual(sm[1, 9:13], objectPixelVal[1])
        self.assertFloatsEqual(sm[2, 12:14],
                               objectPixelVal[1] | self.objectPixelVal[1])
        self.assertFloatsEqual(sm[2, 10:12], self.objectPixelVal[1])

        if False:
            import matplotlib
            matplotlib.use('Agg')
            import pylab as plt
            im1 = afwImage.ImageF(bb)
            hfoot1.insert(im1)
            im2 = afwImage.ImageF(bb)
            hfoot2.insert(im2)
            im3 = afwImage.ImageF(bb)
            hsum.insert(im3)
            plt.clf()
            plt.subplot(1, 3, 1)
            plt.imshow(im1.getArray(), interpolation='nearest', origin='lower')
            plt.subplot(1, 3, 2)
            plt.imshow(im2.getArray(), interpolation='nearest', origin='lower')
            plt.subplot(1, 3, 3)
            plt.imshow(im3.getArray(), interpolation='nearest', origin='lower')
            plt.savefig('merge.png')
Ejemplo n.º 20
0
 def testSpanIteration(self):
     span = geom.Span(4, 3, 8)
     points = list(span)
     self.assertEqual(len(span), len(points))
     self.assertEqual(points, [geom.Point2I(x, 4) for x in xrange(3, 9)])
Ejemplo n.º 21
0
    def testIntersectNot(self):
        firstSpanSet, secondSpanSet = self.makeOverlapSpanSets()

        overlap = firstSpanSet.intersectNot(secondSpanSet)
        for yVal, span in enumerate(overlap):
            self.assertEqual(span.getY(), yVal+5)
            self.assertEqual(span.getMinX(), 0)
            self.assertEqual(span.getMaxX(), 4)

        mask, spanSetMaskOperation = self.makeMaskAndSpanSetForOperationTest()

        spanSetIntersectNotMask = spanSetMaskOperation.intersectNot(mask, 2)

        self.assertEqual(len(spanSetIntersectNotMask), 1)
        self.assertEqual(next(iter(spanSetIntersectNotMask)).getY(), 5)

        # More complicated intersection with disconnected SpanSets
        spanList1 = [afwGeom.Span(0, 0, 10),
                     afwGeom.Span(1, 0, 10),
                     afwGeom.Span(2, 0, 10)]

        spanList2 = [afwGeom.Span(1, 2, 4), afwGeom.Span(1, 7, 8)]

        resultList = [afwGeom.Span(0, 0, 10),
                      afwGeom.Span(1, 0, 1),
                      afwGeom.Span(1, 5, 6),
                      afwGeom.Span(1, 9, 10),
                      afwGeom.Span(2, 0, 10)]

        spanSet1 = afwGeom.SpanSet(spanList1)
        spanSet2 = afwGeom.SpanSet(spanList2)
        expectedSpanSet = afwGeom.SpanSet(resultList)

        outputSpanSet = spanSet1.intersectNot(spanSet2)

        self.assertEqual(outputSpanSet, expectedSpanSet)

        numIntersectNotTrials = 100
        spanRow = 5
        # Set a seed for random functions
        np.random.seed(400)
        for N in range(numIntersectNotTrials):
            # Create two random SpanSets, both with holes in them
            listOfRandomSpanSets = []
            for i in range(2):
                # Make two rectangles to be turned into a SpanSet
                rand1 = np.random.randint(0, 26, 2)
                rand2 = np.random.randint(rand1.max(), 51, 2)
                tempList = [afwGeom.Span(spanRow, rand1.min(), rand1.max()),
                            afwGeom.Span(spanRow, rand2.min(), rand2.max())]
                listOfRandomSpanSets.append(afwGeom.SpanSet(tempList))

            # IntersectNot the SpanSets, randomly choosing which one is the one
            # to be the negated SpanSet
            randChoice = np.random.randint(0, 2)
            negatedRandChoice = int(not randChoice)
            sourceSpanSet = listOfRandomSpanSets[randChoice]
            targetSpanSet = listOfRandomSpanSets[negatedRandChoice]
            resultSpanSet = sourceSpanSet.intersectNot(targetSpanSet)
            for span in resultSpanSet:
                for point in span:
                    self.assertTrue(sourceSpanSet.contains(point))
                    self.assertFalse(targetSpanSet.contains(point))

            for x in range(51):
                point = afwGeom.Point2I(x, spanRow)
                if sourceSpanSet.contains(point) and not\
                        targetSpanSet.contains(point):
                    self.assertTrue(resultSpanSet.contains(point))
Ejemplo n.º 22
0
    def testMaskToSpanSet(self):
        mask, _ = self.makeMaskAndSpanSetForOperationTest()
        spanSetFromMask = afwGeom.SpanSet.fromMask(mask)

        for yCoord, span in enumerate(spanSetFromMask):
            self.assertEqual(span, afwGeom.Span(yCoord, 1, 5))
Ejemplo n.º 23
0
    def testFindEdgePixels(self):
        spanSet = afwGeom.SpanSet.fromShape(6, afwGeom.Stencil.CIRCLE)
        spanSetEdge = spanSet.findEdgePixels()

        truthSpans = [afwGeom.Span(-6, 0, 0),
                      afwGeom.Span(-5, -3, -1),
                      afwGeom.Span(-5, 1, 3),
                      afwGeom.Span(-4, -4, -4),
                      afwGeom.Span(-4, 4, 4),
                      afwGeom.Span(-3, -5, -5),
                      afwGeom.Span(-3, 5, 5),
                      afwGeom.Span(-2, -5, -5),
                      afwGeom.Span(-2, 5, 5),
                      afwGeom.Span(-1, -5, -5),
                      afwGeom.Span(-1, 5, 5),
                      afwGeom.Span(0, -6, -6),
                      afwGeom.Span(0, 6, 6),
                      afwGeom.Span(1, -5, -5),
                      afwGeom.Span(1, 5, 5),
                      afwGeom.Span(2, -5, -5),
                      afwGeom.Span(2, 5, 5),
                      afwGeom.Span(3, -5, -5),
                      afwGeom.Span(3, 5, 5),
                      afwGeom.Span(4, -4, -4),
                      afwGeom.Span(4, 4, 4),
                      afwGeom.Span(5, -3, -1),
                      afwGeom.Span(5, 1, 3),
                      afwGeom.Span(6, 0, 0)]
        truthSpanSet = afwGeom.SpanSet(truthSpans)
        self.assertEqual(spanSetEdge, truthSpanSet)
Ejemplo n.º 24
0
 def testToString(self):
     y, x0, x1 = 10, 100, 101
     s = afwGeom.Span(y, x0, x1)
     self.assertEqual(s.toString(), toString(y, x0, x1))