Esempio n. 1
0
def _testImageSlicing(testCase, mImage, gVal, rVal, iVal):
    """Test slicing in the spatial dimensions for image-like objects"""
    testCase.assertIsInstance(mImage[:, -1, -1, LOCAL], MultibandPixel)
    testCase.assertEqual(mImage["G", -1, -1, LOCAL], gVal)

    testCase.assertEqual(mImage[:, 1100:, 2025:].getBBox(), Box2I(Point2I(1100, 2025), Extent2I(100, 75)))
    testCase.assertEqual(mImage[:, -20:-10, -10:-5, LOCAL].getBBox(),
                         Box2I(Point2I(1180, 2090), Extent2I(10, 5)))
    testCase.assertEqual(mImage[:, :1100, :2050].getBBox(), Box2I(Point2I(1000, 2000), Extent2I(100, 50)))
    coord = Point2I(1075, 2015)
    bbox = Box2I(Point2I(1050, 2010), coord)
    testCase.assertEqual(mImage[:, bbox].getBBox(), bbox)
    testCase.assertEqual(mImage[:, 1010, 2010].getBBox().getMin(), Point2I(1010, 2010))
    testCase.assertEqual(mImage[:, Point2I(1075, 2015)].getBBox().getMin(), coord)

    testCase.assertEqual(mImage["G", 1100:, 2025:].getBBox(), Box2I(Point2I(1100, 2025), Extent2I(100, 75)))
    testCase.assertEqual(mImage["R", -20:-10, -10:-5, LOCAL].getBBox(),
                         Box2I(Point2I(1180, 2090), Extent2I(10, 5)))
    testCase.assertEqual(mImage["I", :1100, :2050].getBBox(), Box2I(Point2I(1000, 2000), Extent2I(100, 50)))
    testCase.assertEqual(mImage["R", bbox].getBBox(), bbox)
    testCase.assertEqual(mImage["I", 1010, 2010], iVal)
    testCase.assertEqual(mImage["R", Point2I(1075, 2015)], rVal)

    with testCase.assertRaises(TypeError):
        mImage[:, 0]
    with testCase.assertRaises(TypeError):
        mImage[:, 10:]
    with testCase.assertRaises(TypeError):
        mImage[:, :10]
    with testCase.assertRaises(TypeError):
        mImage[:, :, 0]
Esempio n. 2
0
def _testMaskedImageCopy(testCase, maskedImage1):
    maskedImage2 = maskedImage1.clone()

    maskedImage2.setXY0(Point2I(11, 23))
    testCase.assertEqual(maskedImage2.getBBox(),
                         Box2I(Point2I(11, 23), Extent2I(200, 100)))
    testCase.assertEqual(maskedImage1.getBBox(),
                         Box2I(Point2I(1000, 2000), Extent2I(200, 100)))
    testCase.assertTrue(
        np.all([
            img.getBBox() == maskedImage1.getBBox()
            for img in maskedImage1.image
        ]))
    testCase.assertTrue(
        np.all([
            img.getBBox() == maskedImage2.getBBox()
            for img in maskedImage2.image
        ]))

    maskedImage2.image.array = 1
    testCase.assertFloatsEqual(maskedImage1.image.array, testCase.imgValue)
    testCase.assertFloatsEqual(maskedImage2.image.array, 1)
    testCase.assertFloatsEqual(maskedImage1["G"].image.array,
                               testCase.imgValue)
    testCase.assertFloatsEqual(maskedImage2["G"].image.array, 1)

    maskedImage2 = maskedImage1.clone(False)
    maskedImage2.image.array = 1
    testCase.assertFloatsEqual(maskedImage1.image.array, 1)
    testCase.assertFloatsEqual(maskedImage2.image.array, 1)
    testCase.assertFloatsEqual(maskedImage1["G"].image.array, 1)
    testCase.assertFloatsEqual(maskedImage2["G"].image.array, 1)
Esempio n. 3
0
    def test_getNumGoodPixels(self):
        """Test the the number of pixels in the image not masked is as expected."""
        testImage = self.flatExp.clone()
        mi = testImage.maskedImage

        imageSize = testImage.getBBox().getArea()
        nGood = self.defaultTask.measure._getNumGoodPixels(mi)

        self.assertEqual(imageSize, nGood)

        NODATABIT = mi.mask.getPlaneBitMask("NO_DATA")

        noDataBox = Box2I(Point2I(31, 49), Extent2I(3, 6))
        testImage.mask[noDataBox] |= NODATABIT

        self.assertEqual(imageSize - noDataBox.getArea(), self.defaultTask.measure._getNumGoodPixels(mi))
        # check for misfire; we're setting NO_DATA here, not BAD
        self.assertEqual(imageSize, self.defaultTask.measure._getNumGoodPixels(mi, 'BAD'))

        testImage.mask[noDataBox] ^= NODATABIT  # XOR to reset what we did
        self.assertEqual(imageSize, nGood)

        BADBIT = mi.mask.getPlaneBitMask("BAD")
        badBox = Box2I(Point2I(85, 98), Extent2I(4, 7))
        testImage.mask[badBox] |= BADBIT

        self.assertEqual(imageSize - badBox.getArea(), self.defaultTask.measure._getNumGoodPixels(mi, 'BAD'))
Esempio n. 4
0
def _testImageCopy(testCase, mImage1, value1, value2):
    """Test copy and deep copy in image-like objects"""
    mImage2 = mImage1.clone()
    mImage2.setXY0(Point2I(11, 23))
    testCase.assertEqual(mImage2.getBBox(),
                         Box2I(Point2I(11, 23), Extent2I(200, 100)))
    testCase.assertEqual(mImage1.getBBox(),
                         Box2I(Point2I(1000, 2000), Extent2I(200, 100)))
    testCase.assertTrue(
        np.all([s.getBBox() == mImage1.getBBox() for s in mImage1.singles]))
    testCase.assertTrue(
        np.all([s.getBBox() == mImage2.getBBox() for s in mImage2.singles]))
    mImage2.array[:] = 17
    testCase.assertNotEqual(mImage1.array[0, 0, 0], 17)

    mImage2.array[:] = value2
    testCase.assertFloatsEqual(mImage1.array, value1)
    testCase.assertFloatsEqual(mImage2.array, value2)
    testCase.assertFloatsEqual(mImage1["G"].array, value1)
    testCase.assertFloatsEqual(mImage2["G"].array, value2)

    mImage2 = mImage1.clone(False)
    mImage2.setXY0(Point2I(11, 23))
    mImage2.array[:] = 17
    testCase.assertFloatsEqual(mImage2.array, mImage1.array)

    mImage2.array[:] = value2
    testCase.assertFloatsEqual(mImage1.array, value2)
    testCase.assertFloatsEqual(mImage2.array, value2)
    testCase.assertFloatsEqual(mImage1["G"].array, value2)
    testCase.assertFloatsEqual(mImage2["G"].array, value2)
Esempio n. 5
0
 def setUp(self):
     np.random.seed(1)
     self.bbox1 = Box2I(Point2I(1000, 2000), Extent2I(200, 100))
     self.filters = ["G", "R", "I", "Z", "Y"]
     self.value1, self.value2 = 10, 100
     images = [ImageF(self.bbox1, self.value1) for f in self.filters]
     self.mImage1 = MultibandImage.fromImages(self.filters, images)
     self.bbox2 = Box2I(Point2I(1100, 2025), Extent2I(30, 50))
     images = [ImageF(self.bbox2, self.value2) for f in self.filters]
     self.mImage2 = MultibandImage.fromImages(self.filters, images)
Esempio n. 6
0
 def test_amp_curve(self):
     # Future versions of astropy will pass unit through concatenation
     amp_wavelength = np.concatenate([self.wavelength.value, self.wavelength.value])*u.angstrom  # Two amps
     amp_efficiency = np.concatenate([self.efficiency.value,
                                      self.efficiency.value*0.8])*u.percent  # Two amps
     amp_name = np.concatenate([['A' for el in self.wavelength], ['B' for el in self.wavelength]])
     amplist = [MockAmp('A', Box2I(Point2I(0, 0), Extent2I(512, 1025))),
                MockAmp('B', Box2I(Point2I(512, 10), Extent2I(512, 1024)))]
     args = (amp_name, amp_wavelength, amp_efficiency, self.metadata)
     self.curve_tester(algorithms.AmpCurve, args)
     self.interp_tester(algorithms.AmpCurve, args, amplist)
Esempio n. 7
0
 def setUp(self):
     self.dtypes = [
         np.dtype(t) for t in (np.uint16, np.int32, np.float32, np.float64)
     ]
     self.bbox = Box2I(Point2I(2, 1), Extent2I(5, 7))
     self.args = [
         (),
         (Box2I(Point2I(3, 4), Extent2I(2, 1)), ),
         (Box2I(Point2I(3, 4), Extent2I(2, 1)), PARENT),
         (Box2I(Point2I(1, 0), Extent2I(3, 2)), LOCAL),
     ]
Esempio n. 8
0
    def testPixelBBoxModification(self):
        pixel = self.pixel.clone()
        otherPixel = pixel.clone()
        pixel.getBBox().shift(Extent2I(9, -2))
        self.assertEqual(pixel.getBBox().getMin(), Point2I(110, 500))
        self.assertEqual(otherPixel.getBBox().getMin(), Point2I(101, 502))

        pixel = self.pixel.clone()
        otherPixel = pixel.clone(False)
        pixel.getBBox().shift(Extent2I(9, -2))
        self.assertEqual(pixel.getBBox().getMin(), Point2I(110, 500))
        self.assertEqual(otherPixel.getBBox().getMin(), Point2I(110, 500))
Esempio n. 9
0
    def testConstructor(self):
        def projectSpans(radius, value, bbox, asArray):
            ss = SpanSet.fromShape(radius, Stencil.CIRCLE, offset=(10, 10))
            image = ImageF(bbox)
            ss.setImage(image, value)
            if asArray:
                return image.array
            else:
                return image

        def runTest(images, mFoot, peaks=self.peaks, footprintBBox=Box2I(Point2I(6, 6), Extent2I(9, 9))):
            self.assertEqual(mFoot.getBBox(), footprintBBox)
            try:
                fpImage = np.array(images)[:, 1:-1, 1:-1]
            except IndexError:
                fpImage = np.array([img.array for img in images])[:, 1:-1, 1:-1]
            # result = mFoot.getImage(fill=0).image.array
            self.assertFloatsAlmostEqual(mFoot.getImage(fill=0).image.array, fpImage)
            if peaks is not None:
                self.verifyPeaks(mFoot.getPeaks(), peaks)

        bbox = Box2I(Point2I(5, 5), Extent2I(11, 11))
        xy0 = Point2I(5, 5)

        images = np.array([projectSpans(n, 5-n, bbox, True) for n in range(2, 5)])
        mFoot = MultibandFootprint.fromArrays(self.filters, images, xy0=xy0, peaks=self.peaks)
        runTest(images, mFoot)

        mFoot = MultibandFootprint.fromArrays(self.filters, images)
        runTest(images, mFoot, None, Box2I(Point2I(1, 1), Extent2I(9, 9)))

        images = [projectSpans(n, 5-n, bbox, False) for n in range(2, 5)]
        mFoot = MultibandFootprint.fromImages(self.filters, images, peaks=self.peaks)
        runTest(images, mFoot)

        images = np.array([projectSpans(n, n, bbox, True) for n in range(2, 5)])
        mFoot = MultibandFootprint.fromArrays(self.filters, images, peaks=self.peaks, xy0=bbox.getMin())
        runTest(images, mFoot)

        images = np.array([projectSpans(n, 5-n, bbox, True) for n in range(2, 5)])
        thresh = [1, 2, 2.5]
        mFoot = MultibandFootprint.fromArrays(self.filters, images, xy0=bbox.getMin(), thresh=thresh)
        footprintBBox = Box2I(Point2I(8, 8), Extent2I(5, 5))
        self.assertEqual(mFoot.getBBox(), footprintBBox)

        fpImage = np.array(images)[:, 3:-3, 3:-3]
        mask = np.all(fpImage <= np.array(thresh)[:, None, None], axis=0)
        fpImage[:, mask] = 0
        self.assertFloatsAlmostEqual(mFoot.getImage(fill=0).image.array, fpImage)
        img = mFoot.getImage().image.array
        img[~np.isfinite(img)] = 1.1
        self.assertFloatsAlmostEqual(mFoot.getImage(fill=1.1).image.array, img)
Esempio n. 10
0
    def test_maskBlocks_short_column(self):
        """A test for maskBlocksIfIntermitentBadPixelsInColumn.
        Tests that a contigous bad column Npix < badOnAndOffPixelColumnThreshold (10)
        does not get split by the code.

        Plots can be found in DM-19903 on Jira.
        """

        expectedDefects = [Box2I(corner=Point2I(25, 1), dimensions=Extent2I(1, 8))]
        defects = self.allDefectsList
        defects.append(Box2I(corner=Point2I(25, 1), dimensions=Extent2I(1, 8)))

        self.check_maskBlocks(defects, expectedDefects)
Esempio n. 11
0
    def __init__(self, filters, singles, position):
        if any([arg is None for arg in [singles, filters, position]]):
            err = "Expected an array of `singles`, a list of `filters, and a `bbox`"
            raise NotImplementedError(err)

        # Make sure that singles is an array
        singles = np.array(singles, copy=False)

        super().__init__(filters, singles, bbox=Box2I(position, Extent2I(1, 1)))
        # In this case we want self.singles to be an array
        self._singles = singles

        # Make sure that the bounding box has been setup properly
        assert self.getBBox().getDimensions() == Extent2I(1, 1)
Esempio n. 12
0
    def setUp(self):
        xy0 = Point2I(12345, 67890)  # xy0 for image
        dims = Extent2I(2345, 2345)  # Dimensions of image
        box = Box2I(xy0, dims)  # Bounding box of image
        sigma = 3.21  # PSF sigma
        buffer = 4.0  # Buffer for star centers around edge
        nSigmaForKernel = 5.0  # Number of PSF sigmas for kernel
        sky = 12345.6  # Sky level
        numStars = 100  # Number of stars
        noise = np.sqrt(sky) * np.pi * sigma**2  # Poisson noise per PSF
        faint = 1.0 * noise  # Faintest level for star fluxes
        bright = 100.0 * noise  # Brightest level for star fluxes
        starBox = Box2I(box)  # Area on image in which we can put star centers
        starBox.grow(-int(buffer * sigma))
        scale = 1.0e-5 * degrees  # Pixel scale

        np.random.seed(12345)
        stars = [(xx, yy, ff, sigma) for xx, yy, ff in zip(
            np.random.uniform(starBox.getMinX(), starBox.getMaxX(), numStars),
            np.random.uniform(starBox.getMinY(), starBox.getMaxY(), numStars),
            np.linspace(faint, bright, numStars))]
        self.exposure = plantSources(box, 2 * int(nSigmaForKernel * sigma) + 1,
                                     sky, stars, True)
        self.exposure.setWcs(
            makeSkyWcs(crpix=Point2D(0, 0),
                       crval=SpherePoint(0, 0, degrees),
                       cdMatrix=makeCdMatrix(scale=scale)))

        # Make a large area of extra background; we should be robust against it
        # Unfortunately, some tuning is required here to get something challenging but not impossible:
        # * A very large box will cause failures because the "extra" and the "normal" are reversed.
        # * A small box will not be challenging because it's simple to clip out.
        # * A large value will cause failures because it produces large edges in background-subtrction that
        #     broaden flux distributions.
        # * A small value will not be challenging because it has little effect.
        extraBox = Box2I(xy0 + Extent2I(345, 456),
                         Extent2I(1234, 1234))  # Box for extra background
        extraValue = 0.5 * noise  # Extra background value to add in
        self.exposure.image[extraBox, PARENT] += extraValue

        self.config = DynamicDetectionTask.ConfigClass()
        self.config.skyObjects.nSources = 300
        self.config.reEstimateBackground = False
        self.config.doTempWideBackground = True
        self.config.thresholdType = "pixel_stdev"

        # Relative tolerance for tweak factor
        # Not sure why this isn't smaller; maybe due to use of Poisson instead of Gaussian noise?
        self.rtol = 0.1
Esempio n. 13
0
    def test_maskBlocks_full_column(self):
        """A test for maskBlocksIfIntermitentBadPixelsInColumn.
        Tests that a contigous bad column does not get split by the code.

        The mock flat has a size of 200X204 pixels. This column has a maximum length of 50
        pixels, otherwise there would be a split along the mock amp boundary.

        Plots can be found in DM-19903 on Jira.
        """

        defects = self.allDefectsList
        defects.append(Box2I(corner=Point2I(15, 1), dimensions=Extent2I(1, 50)))
        expectedDefects = [Box2I(corner=Point2I(15, 1), dimensions=Extent2I(1, 50))]

        self.check_maskBlocks(defects, expectedDefects)
Esempio n. 14
0
    def run(self, exposure, catalog, flux_key):

        subtracted_exposure = afwImage.ExposureF(exposure, deep=True)
        model = self.modelImage.run(subtracted_exposure, catalog, flux_key)

        for source in catalog:
            with self.modelImage.replaced_source(subtracted_exposure, source,
                                                 flux_key):

                # This parameter is the radius of the spanset
                # Changing the radius doesn't seem to affect SDSS Centroid?
                spanSet = afwGeom.SpanSet.fromShape(3)
                spanSet = spanSet.shiftedBy(Extent2I(source.getCentroid()))
                footprint = afwDetection.Footprint(spanSet)
                peak = footprint.peaks.addNew()
                peak.setFx(source.getCentroid().getX())
                peak.setFy(source.getCentroid().getY())

                # Check for NaNs
                if (source.getCentroid().getX() == source.getCentroid().getX()
                    ):
                    peak.setIx(int(source.getCentroid().getX()))
                    peak.setIy(int(source.getCentroid().getY()))
                source.setFootprint(footprint)

                try:
                    self.sdssCentroid.measure(source, subtracted_exposure)
                except (MeasurementError):
                    pass
Esempio n. 15
0
def _testImageModification(testCase, mImage1, mImage2, bbox1, bbox2, value1,
                           value2):
    """Test the image-like objects can be modified"""
    mImage1[:"R", bbox2].array = value2
    testCase.assertFloatsEqual(mImage1["G", bbox2].array, mImage2["G"].array)
    testCase.assertFloatsEqual(mImage1["R"].array, value1)
    mImage1.setXY0(Point2I(500, 150))
    testCase.assertEqual(
        mImage1.getBBox(),
        Box2I(Point2I(500, 150), Extent2I(bbox1.getDimensions())))

    mImage1["G"].array[:] = value2
    testCase.assertFloatsEqual(mImage1["G"].array, value2)
    testCase.assertFloatsEqual(mImage1.array[0], value2)

    if "Z" in mImage1.filters:
        filterSlice = slice("R", "Z")
    else:
        filterSlice = slice("R", None)
    mImage1[filterSlice].array[:] = 7
    testCase.assertFloatsEqual(mImage1["I"].array, 7)
    newBBox = Box2I(Point2I(10000, 20000), mImage1.getBBox().getDimensions())
    mImage1.setXY0(newBBox.getMin())
    testCase.assertEqual(mImage1.getBBox(), newBBox)
    for image in mImage1:
        testCase.assertEqual(image.getBBox(), newBBox)
Esempio n. 16
0
    def setUp(self):
        np.random.seed(1)
        self.bbox = Box2I(Point2I(1000, 2000), Extent2I(200, 100))
        self.filters = ["G", "R", "I"]

        self.imgValue = 10
        images = [ImageF(self.bbox, self.imgValue) for f in self.filters]
        mImage = MultibandImage.fromImages(self.filters, images)

        self.Mask = Mask[MaskPixel]
        # Store the default mask planes for later use
        maskPlaneDict = self.Mask().getMaskPlaneDict()
        self.defaultMaskPlanes = sorted(maskPlaneDict,
                                        key=maskPlaneDict.__getitem__)

        # reset so tests will be deterministic
        self.Mask.clearMaskPlaneDict()
        for p in ("BAD", "SAT", "INTRP", "CR", "EDGE"):
            self.Mask.addMaskPlane(p)

        self.maskValue = self.Mask.getPlaneBitMask("BAD")
        singles = [self.Mask(self.bbox) for f in range(len(self.filters))]
        for n in range(len(singles)):
            singles[n].set(self.maskValue)
        mMask = MultibandMask.fromMasks(self.filters, singles)

        self.varValue = 1e-2
        images = [ImageF(self.bbox, self.varValue) for f in self.filters]
        mVariance = MultibandImage.fromImages(self.filters, images)

        self.maskedImage = MultibandMaskedImage(self.filters,
                                                image=mImage,
                                                mask=mMask,
                                                variance=mVariance)
Esempio n. 17
0
def make_coadd_dm_wcs_simple(coadd_dim):
    """
    make a coadd wcs, using the default world origin.

    Parameters
    ----------
    coadd_origin: int
        Origin in pixels of the coadd, can be within a larger
        pixel grid e.g. tract surrounding the patch

    Returns
    --------
    A galsim wcs, see make_wcs for return type
    """

    coadd_bbox = Box2I(Point2I(0), Extent2I(coadd_dim))
    coadd_origin = coadd_bbox.getCenter()

    gs_coadd_origin = galsim.PositionD(
        x=coadd_origin.x,
        y=coadd_origin.y,
    )
    coadd_wcs = make_dm_wcs(
        make_wcs(
            scale=SCALE,
            image_origin=gs_coadd_origin,
            world_origin=WORLD_ORIGIN,
        ))
    return coadd_wcs, coadd_bbox
Esempio n. 18
0
    def run(self, bss_ref_list, region_name=None):
        """Read input bright star stamps and stack them together.

        The stacking is done iteratively over smaller areas of the final model
        image to allow for a great number of bright star stamps to be used.

        Parameters
        ----------
        bss_ref_list : `list` of
                `lsst.daf.butler._deferredDatasetHandle.DeferredDatasetHandle`
            List of available bright star stamps data references.
        region_name : `str`, optional
            Name of the focal plane region, if applicable. Only used for
            logging purposes, when running over multiple such regions
            (typically from `MeasureExtendedPsfTask`)
        """
        if region_name:
            region_message = f' for region "{region_name}".'
        else:
            region_message = ''
        self.log.info(
            'Building extended PSF from stamps extracted from %d detector images%s',
            len(bss_ref_list), region_message)
        # read in example set of full stamps
        example_bss = bss_ref_list[0].get(datasetType="brightStarStamps",
                                          immediate=True)
        example_stamp = example_bss[0].stamp_im
        # create model image
        ext_psf = afwImage.MaskedImageF(example_stamp.getBBox())
        # divide model image into smaller subregions
        subregion_size = Extent2I(*self.config.subregion_size)
        sub_bboxes = AssembleCoaddTask._subBBoxIter(ext_psf.getBBox(),
                                                    subregion_size)
        # compute approximate number of subregions
        n_subregions = int(ext_psf.getDimensions()[0] / subregion_size[0] +
                           1) * int(ext_psf.getDimensions()[1] /
                                    subregion_size[1] + 1)
        self.log.info(
            "Stacking will performed iteratively over approximately %d "
            "smaller areas of the final model image.", n_subregions)
        # set up stacking statistic
        stats_control, stats_flags = self._set_up_stacking(example_stamp)
        # perform stacking
        for jbbox, bbox in enumerate(sub_bboxes):
            all_stars = None
            for bss_ref in bss_ref_list:
                read_stars = bss_ref.get(datasetType="brightStarStamps",
                                         parameters={'bbox': bbox})
                if self.config.do_mag_cut:
                    read_stars = read_stars.selectByMag(
                        magMax=self.config.mag_limit)
                if all_stars:
                    all_stars.extend(read_stars)
                else:
                    all_stars = read_stars
            # TODO: DM-27371 add weights to bright stars for stacking
            coadd_sub_bbox = afwMath.statisticsStack(
                all_stars.getMaskedImages(), stats_flags, stats_control)
            ext_psf.assign(coadd_sub_bbox, bbox)
        return ext_psf
Esempio n. 19
0
 def __init__(self, width, height, piffResult):
     assert width == height
     ImagePsf.__init__(self)
     self.width = width
     self.height = height
     self.dimensions = Extent2I(width, height)
     self._piffResult = piffResult
Esempio n. 20
0
def tripleFromArrays(cls, filters, image, mask, variance, bbox=None):
    """Construct a MultibandTriple from a set of arrays

    Parameters
    ----------
    filters: `list`
        List of filter names.
    image: array
        Array of image values
    mask: array
        Array of mask values
    variance: array
        Array of variance values
    bbox: `Box2I`
        Location of the array in a larger single band image.
        This argument is ignored if `singles` is not `None`.
    """
    if bbox is None:
        bbox = Box2I(Point2I(0, 0), Extent2I(image.shape[1], image.shape[0]))
    mImage = MultibandImage(filters, image, bbox)
    if mask is not None:
        mMask = MultibandMask(filters, mask, bbox)
    else:
        mMask = None
    if variance is not None:
        mVariance = MultibandImage(filters, variance, bbox)
    else:
        mVariance = None
    return cls(filters, mImage, mMask, mVariance)
Esempio n. 21
0
    def setUp(self):
        np.random.seed(1)
        self.filters = ["G", "R", "I"]
        self.Mask = Mask[MaskPixel]

        # Store the default mask planes for later use
        maskPlaneDict = self.Mask().getMaskPlaneDict()
        self.defaultMaskPlanes = sorted(maskPlaneDict,
                                        key=maskPlaneDict.__getitem__)

        # reset so tests will be deterministic
        self.Mask.clearMaskPlaneDict()
        for p in ("BAD", "SAT", "INTRP", "CR", "EDGE"):
            self.Mask.addMaskPlane(p)

        self.BAD = self.Mask.getPlaneBitMask("BAD")
        self.CR = self.Mask.getPlaneBitMask("CR")
        self.EDGE = self.Mask.getPlaneBitMask("EDGE")

        self.values1 = [
            self.BAD | self.CR, self.BAD | self.EDGE,
            self.BAD | self.CR | self.EDGE
        ]
        self.bbox = Box2I(Point2I(1000, 2000), Extent2I(200, 100))
        singles = [self.Mask(self.bbox) for f in range(len(self.filters))]
        for n in range(len(singles)):
            singles[n].set(self.values1[n])
        self.mMask1 = MultibandMask.fromMasks(self.filters, singles)

        self.values2 = [self.EDGE, self.BAD, self.CR | self.EDGE]
        singles = [self.Mask(self.bbox) for f in range(len(self.filters))]
        for n in range(len(singles)):
            singles[n].set(self.values2[n])
        self.mMask2 = MultibandMask.fromMasks(self.filters, singles)
Esempio n. 22
0
 def run(md, **kwds2):
     kwds = extractCtorArgs(md)
     kwds.update(kwds2)
     gridShape = Extent2I(20, 20)
     approx = SipApproximation(gridShape=gridShape, **kwds)
     diffs = approx.computeMaxDeviation()
     self.assertLess(diffs[0], 0.1)
     self.assertLess(diffs[1], 0.1)
Esempio n. 23
0
    def fromTable(cls, tableList):
        """Read linearity from a FITS file.

        This method uses the `fromDict` method to create the
        calibration, after constructing an appropriate dictionary from
        the input tables.

        Parameters
        ----------
        tableList : `list` [`astropy.table.Table`]
            afwTable read from input file name.

        Returns
        -------
        linearity : `~lsst.ip.isr.linearize.Linearizer``
            Linearity parameters.

        Notes
        -----
        The method reads a FITS file with 1 or 2 extensions. The metadata is read from the header of
        extension 1, which must exist.  Then the table is loaded, and  the ['AMPLIFIER_NAME', 'TYPE',
        'COEFFS', 'BBOX_X0', 'BBOX_Y0', 'BBOX_DX', 'BBOX_DY'] columns are read and used to
        set each dictionary by looping over rows.
        Eextension 2 is then attempted to read in the try block (which only exists for lookup tables).
        It has a column named 'LOOKUP_VALUES' that contains a vector of the lookup entries in each row.

        """
        coeffTable = tableList[0]

        metadata = coeffTable.meta
        inDict = dict()
        inDict['metadata'] = metadata
        inDict['hasLinearity'] = metadata.get('HAS_LINEARITY', False)
        inDict['amplifiers'] = dict()

        for record in coeffTable:
            ampName = record['AMPLIFIER_NAME']

            fitParams = record['FIT_PARAMS'] if 'FIT_PARAMS' in record.columns else np.array([0.0])
            fitParamsErr = record['FIT_PARAMS_ERR'] if 'FIT_PARAMS_ERR' in record.columns else np.array([0.0])
            fitChiSq = record['RED_CHI_SQ'] if 'RED_CHI_SQ' in record.columns else np.nan

            inDict['amplifiers'][ampName] = {
                'linearityType': record['TYPE'],
                'linearityCoeffs': record['COEFFS'],
                'linearityBBox': Box2I(Point2I(record['BBOX_X0'], record['BBOX_Y0']),
                                       Extent2I(record['BBOX_DX'], record['BBOX_DY'])),
                'fitParams': fitParams,
                'fitParamsErr': fitParamsErr,
                'fitChiSq': fitChiSq,
            }

        if len(tableList) > 1:
            tableData = tableList[1]
            inDict['tableData'] = [record['LOOKUP_VALUES'] for record in tableData]

        return cls().fromDict(inDict)
 def setUp(self):
     dimensions = Extent2I(7, 7)
     self.bbox = Box2I(Point2I(-dimensions/2), dimensions)
     self.img = Image(self.bbox, dtype=np.float64)
     x, y = np.ogrid[-3:4, -3:4]
     rsqr = x**2 + y**2
     # Some arbitrary circular double Gaussian
     self.img.array[:] = np.exp(-0.5*rsqr**2) + np.exp(-0.5*rsqr**2/4)
     self.img.array /= np.sum(self.img.array)
     self.psf = MyTestImagePsf(self.img)
Esempio n. 25
0
 def _doComputeKernelImage(self, position=None, color=None):
     bbox = Box2I(Point2I(-3, -3), Extent2I(7, 7))
     img = Image(bbox, dtype=np.float64)
     x, y = np.ogrid[bbox.minY:bbox.maxY + 1, bbox.minX:bbox.maxX + 1]
     rsqr = x**2 + y**2
     if position.x >= 0.0:
         img.array[:] = np.exp(-0.5 * rsqr)
     else:
         img.array[:] = np.exp(-0.5 * rsqr / 4)
     img.array /= np.sum(img.array)
     return img
Esempio n. 26
0
    def test_maskBlocks_not_enough_bad_pixels_in_column(self):
        """A test for maskBlocksIfIntermitentBadPixelsInColumn.
        Npix discontiguous bad pixels in a column where Npix < badOnAndOffPixelColumnThreshold (10) and
        and gaps of good pixels > goodPixelColumnGapThreshold (5). Since  Npix <
        badOnAndOffPixelColumnThreshold, then it doesn't matter that the number of good pixels in gap >
        goodPixelColumnGapThreshold. 5<10 bad pixels total, 1 "good" gap big enough
        (29>=5 good pixels, from y =12 (10+2) to y=30)

        Plots can be found in DM-19903 on Jira.
        """

        expectedDefects = [Box2I(corner=Point2I(45, 10), dimensions=Extent2I(1, 2)),
                           Box2I(corner=Point2I(45, 30), dimensions=Extent2I(1, 3))]
        defects = self.allDefectsList
        badPixels = [Box2I(corner=Point2I(45, 10), dimensions=Extent2I(1, 2)),
                     Box2I(corner=Point2I(45, 30), dimensions=Extent2I(1, 3))]
        for badBox in badPixels:
            defects.append(badBox)

        self.check_maskBlocks(defects, expectedDefects)
Esempio n. 27
0
 def run(md, **kwds2):
     kwds = extractCtorArgs(md)
     kwds['order'] = max(md["A_ORDER"], md["B_ORDER"], md["AP_ORDER"],
                         md["BP_ORDER"])
     kwds.update(kwds2)
     gridShape = Extent2I(10, 10)
     approx = SipApproximation(gridShape=gridShape, **kwds)
     diffs = approx.computeMaxDeviation()
     self.compareSolution(md, approx)
     self.assertLess(diffs[0], 1E-10)
     self.assertLess(diffs[1], 1E-10)
def numpyToStack(images, center, offset):
    """Convert numpy and python objects to stack objects
    """
    cy, cx = center
    bands, height, width = images.shape
    x0, y0 = offset
    bbox = Box2I(Point2I(x0, y0), Extent2I(width, height))
    spanset = SpanSet(bbox)
    foot = Footprint(spanset)
    foot.addPeak(cx + x0, cy + y0, images[:, cy, cx].max())
    peak = foot.getPeaks()[0]
    return foot, peak, bbox
Esempio n. 29
0
    def runExposureCompositePutGetTest(self, storageClass, datasetTypeName):
        example = os.path.join(TESTDIR, "data", "basic", "small.fits")
        exposure = lsst.afw.image.ExposureF(example)
        butler = Butler(self.tmpConfigFile)
        dimensions = butler.registry.dimensions.extract(
            ["instrument", "visit"])
        self.registerDatasetTypes(datasetTypeName, dimensions, storageClass,
                                  butler.registry)
        dataId = {
            "visit": 42,
            "instrument": "DummyCam",
            "physical_filter": "d-r"
        }
        # Add needed Dimensions
        butler.registry.addDimensionEntry("instrument",
                                          {"instrument": "DummyCam"})
        butler.registry.addDimensionEntry("physical_filter", {
            "instrument": "DummyCam",
            "physical_filter": "d-r"
        })
        butler.registry.addDimensionEntry("visit", {
            "instrument": "DummyCam",
            "visit": 42,
            "physical_filter": "d-r"
        })
        butler.put(exposure, datasetTypeName, dataId)
        # Get the full thing
        butler.get(datasetTypeName, dataId)
        # TODO enable check for equality (fix for Exposure type)
        # self.assertEqual(full, exposure)
        # Get a component
        compsRead = {}
        for compName in ("wcs", "image", "mask", "coaddInputs", "psf"):
            compTypeName = DatasetType.nameWithComponent(
                datasetTypeName, compName)
            component = butler.get(compTypeName, dataId)
            # TODO enable check for component instance types
            # compRef = butler.registry.find(butler.run.collection,
            #                                f"calexp.{compName}", dataId)
            # self.assertIsInstance(component,
            #                       compRef.datasetType.storageClass.pytype)
            compsRead[compName] = component
        # Simple check of WCS
        bbox = Box2I(Point2I(0, 0), Extent2I(9, 9))
        self.assertWcsAlmostEqualOverBBox(compsRead["wcs"], exposure.getWcs(),
                                          bbox)

        # With parameters
        inBBox = Box2I(minimum=Point2I(0, 0), maximum=Point2I(3, 3))
        parameters = dict(bbox=inBBox, origin=LOCAL)
        subset = butler.get(datasetTypeName, dataId, parameters=parameters)
        outBBox = subset.getBBox()
        self.assertEqual(inBBox, outBBox)
Esempio n. 30
0
def extractCtorArgs(md):
    wcs = makeSkyWcs(makePropertyListFromDict(md))
    kwds = {
        "pixelToIwc": getPixelToIntermediateWorldCoords(wcs),
        "bbox":
        Box2D(Box2I(Point2I(0, 0), Extent2I(md["NAXES1"], md["NAXES2"]))),
        "crpix":
        Point2D(md["CRPIX1"] - 1.0,
                md["CRPIX2"] - 1.0),  # -1 for LSST vs. FITS conventions
        "cd": np.array([[md["CD1_1"], md["CD1_2"]], [md["CD2_1"],
                                                     md["CD2_2"]]]),
    }
    return kwds