Esempio n. 1
0
    def test(self, data, dataId):
        self.matchListDictSrc = data.getMatchListBySensor(dataId, useRef='src')
        self.detector = data.getDetectorBySensor(dataId)
        self.filter = data.getFilterBySensor(dataId)

        if len(self.visits) == 0:
            visitList = [
                dataId['visit'],
            ]
        else:
            visitList = self.visits

        # Implement for 1 visit comparison
        visit = visitList[0]
        self.visitMatches = data.getVisitMatchesBySensor(
            self.database, visit, dataId)

        # create containers for data we're interested in
        self.dRa = raftCcdData.RaftCcdVector(self.detector)
        self.dDec = raftCcdData.RaftCcdVector(self.detector)
        self.x = raftCcdData.RaftCcdVector(self.detector)
        self.y = raftCcdData.RaftCcdVector(self.detector)

        if data.cameraInfo.name == "coadd":
            badFlags = measAlg.Flags.SATUR_CENTER | measAlg.Flags.EDGE  # coadds have excessive area covered by INTERP_CENTER flags
        else:
            badFlags = measAlg.Flags.INTERP_CENTER | measAlg.Flags.SATUR_CENTER | measAlg.Flags.EDGE

        for key in self.matchListDictSrc.keys():
            raft = self.detector[key].getParent().getId().getName()
            ccd = self.detector[key].getId().getName()

            # All detections matched to ref objects
            srcMatchList = self.matchListDictSrc[key]['matched']
            # All visit dets in footprint of original image
            visMatchList = self.visitMatches[key]

            # List of reference object ids
            srcObjIds = num.array([x[0].getId() for x in srcMatchList])
            visObjIds = num.array([x[0].getId() for x in visMatchList])
            common = list(set(srcObjIds) & set(visObjIds))

            self.log.log(self.log.INFO, "%s : " % (key))
            if len(common) == 0:
                self.log.log(
                    self.log.WARN,
                    "  No overlap, Using pre-to-post PT1.2 objectID mapping..."
                )

                # Try objectID hack from PT1.2 to post-PT1.2:
                visObjIds *= 2
                isStar = (num.array(
                    [x[0].getFlagForDetection()
                     for x in visMatchList]) & measAlg.Flags.STAR) > 0
                visObjIds += isStar
                common = list(set(srcObjIds) & set(visObjIds))
            self.log.log(self.log.INFO, "  Found %d matches" % (len(common)))

            # Iterate over all object ids
            for i in range(len(common)):
                srcObjId = common[i]
                idxS = num.where(srcObjIds == srcObjId)[0]
                idxV = num.where(visObjIds == srcObjId)[0]

                # only take 1-to-1 matches
                if len(idxS) != 1 or len(idxV) != 1:
                    continue

                sref1 = srcMatchList[idxS[0]][0]
                srcv1 = srcMatchList[idxS[0]][1]

                sref2 = visMatchList[idxV[0]][0]
                srcv2 = visMatchList[idxV[0]][1]

                #coord1 = afwCoord.Coord(afwGeom.Point2D(srcv1.getRa(), srcv1.getDec()))  # coords originally in degrees
                #coord2 = afwCoord.Coord(afwGeom.Point2D(srcv2.getRa(), srcv2.getDec()))  # coords originally in degrees
                #angle  = coord1.angularSeparation(coord2)

                # Measurment flags; note no star/gal separation yet
                flags1 = srcv1.getFlagForDetection()
                flags2 = srcv2.getFlagForDetection()
                # Get star/gal info here
                isStar = sref1.getFlagForDetection() & measAlg.Flags.STAR

                if (not flags1 & badFlags) and (
                        not flags2 & badFlags) and isStar:
                    ra1, dec1, ra2, dec2 = [
                        x * num.pi / 180.0
                        for x in (srcv1.getRa(), srcv1.getDec(), srcv2.getRa(),
                                  srcv2.getDec())
                    ]
                    dRa = (ra1 - ra2) * abs(num.cos(dec1))
                    dDec = (dec1 - dec2)

                    self.dRa.append(raft, ccd, dRa)
                    self.dDec.append(raft, ccd, dDec)
                    self.x.append(raft, ccd, srcv1.getXAstrom())
                    self.y.append(raft, ccd, srcv1.getYAstrom())

        testSet = self.getTestSet(data, dataId)
        testSet.addMetadata({"Description": self.description})

        self.medErrArcsec = raftCcdData.RaftCcdData(self.detector)
        self.medThetaRad = raftCcdData.RaftCcdData(self.detector)

        for raft, ccd in self.dRa.raftCcdKeys():
            dRa = self.dRa.get(raft, ccd)
            dDec = self.dDec.get(raft, ccd)

            errArcsec = 206265.0 * num.sqrt(dRa**2 + dDec**2)
            thetaRad = num.arctan2(dDec, dRa)

            if len(errArcsec) > 0:
                stat = afwMath.makeStatistics(errArcsec,
                                              afwMath.NPOINT | afwMath.MEDIAN)
                medErrArcsec = stat.getValue(afwMath.MEDIAN)
                stat = afwMath.makeStatistics(thetaRad,
                                              afwMath.NPOINT | afwMath.MEDIAN)
                medThetaRad = stat.getValue(afwMath.MEDIAN)
                n = stat.getValue(afwMath.NPOINT)
            else:
                medErrArcsec = -1.0
                medThetaRad = 0.0
                n = 0

            self.medErrArcsec.set(raft, ccd, medErrArcsec)
            self.medThetaRad.set(raft, ccd, medThetaRad)

            areaLabel = data.cameraInfo.getDetectorName(raft, ccd)
            label = "median astrometry error "
            comment = "median sqrt(dRa^2+dDec^2) (arcsec, nstar=%d)" % (n)
            test = testCode.Test(label,
                                 medErrArcsec,
                                 self.limits,
                                 comment,
                                 areaLabel=areaLabel)
            testSet.addTest(test)
Esempio n. 2
0
    def test(self, data, dataId):

        # get data
        self.ssDict = data.getSourceSetBySensor(dataId)

        self.matchListDictSrc = data.getMatchListBySensor(dataId, useRef='src')
        self.detector = data.getDetectorBySensor(dataId)
        self.filter = data.getFilterBySensor(dataId)

        # create containers for data we're interested in
        self.x = raftCcdData.RaftCcdVector(self.detector)
        self.y = raftCcdData.RaftCcdVector(self.detector)
        self.xmat = raftCcdData.RaftCcdVector(self.detector)
        self.ymat = raftCcdData.RaftCcdVector(self.detector)

        # fill containers with values we need for our test
        filter = None
        self.size = raftCcdData.RaftCcdData(self.detector,
                                            initValue=[1.0, 1.0])
        for key, ss in self.ssDict.items():
            xKey = self.sCatDummy.XAstromKey
            yKey = self.sCatDummy.YAstromKey

            raft = self.detector[key].getParent().getId().getName()
            ccd = self.detector[key].getId().getName()
            bbox = self.detector[key].getAllPixels(True)
            size = [
                bbox.getMaxX() - bbox.getMinX(),
                bbox.getMaxY() - bbox.getMinY()
            ]
            self.size.set(raft, ccd, size)
            filter = self.filter[key].getName()

            for s in ss:
                self.x.append(raft, ccd, s.getD(xKey))
                self.y.append(raft, ccd, s.getD(yKey))

            if self.matchListDictSrc.has_key(key):
                for m in self.matchListDictSrc[key]['matched']:
                    sref, s, dist = m
                    self.xmat.append(raft, ccd, s.getD(xKey))
                    self.ymat.append(raft, ccd, s.getD(yKey))

        # create a testset
        testSet = self.getTestSet(data, dataId)

        # this normally gets set in the plot as that's where the caching happens,
        # here we're stashing the nDetection and nCcd values, so we need to set it early.
        testSet.setUseCache(self.useCache)
        testSet.addMetadata({"Description": self.description})

        # analyse each sensor and put the values in a raftccd container
        self.emptySectors = raftCcdData.RaftCcdData(self.detector,
                                                    initValue=self.nx *
                                                    self.ny)
        self.emptySectorsMat = raftCcdData.RaftCcdData(self.detector,
                                                       initValue=self.nx *
                                                       self.ny)

        countBase = "countShelf"
        nShelf = testSet.unshelve(countBase)

        for raft, ccd in self.emptySectors.raftCcdKeys():
            x, y = self.x.get(raft, ccd), self.y.get(raft, ccd)
            xmat, ymat = self.xmat.get(raft, ccd), self.ymat.get(raft, ccd)
            xwid, ywid = self.size.get(raft, ccd)

            xlo, ylo = 0.0, 0.0
            if data.cameraInfo.name == 'coadd':
                xlo, ylo, xhi, yhi = x.min(), y.min(), x.max(), y.max()
                xwid, ywid = xhi - xlo, yhi - ylo

            def countEmptySectors(x, y):
                counts = numpy.zeros([self.nx, self.ny])
                for i in range(len(x)):
                    xi, yi = int(self.nx * x[i] / xwid), int(self.ny * y[i] /
                                                             ywid)
                    if xi >= 0 and xi < self.nx and yi >= 0 and yi < self.ny:
                        counts[xi, yi] += 1
                whereEmpty = numpy.where(counts.flatten() == 0)[0]
                nEmpty = len(whereEmpty)
                return nEmpty

            nEmpty = countEmptySectors(x - xlo, y - ylo)
            nEmptyMat = countEmptySectors(xmat - xlo, ymat - ylo)
            self.emptySectors.set(raft, ccd, nEmpty)
            self.emptySectorsMat.set(raft, ccd, nEmptyMat)

            # add tests for acceptible numpy of empty sectors
            areaLabel = data.cameraInfo.getDetectorName(raft, ccd)
            label = "empty ccd regions"
            comment = "%dx%d (nstar=%d)" % (self.nx, self.ny, len(x))

            test = testCode.Test(label,
                                 nEmpty,
                                 self.limits,
                                 comment,
                                 areaLabel=areaLabel)
            testSet.addTest(test)
            test = testCode.Test(label + " (matched)",
                                 nEmptyMat,
                                 self.limits,
                                 comment,
                                 areaLabel=areaLabel)
            testSet.addTest(test)

            nShelf[ccd] = len(x)

        testSet.shelve(countBase, nShelf)

        nCcd, nDet = 0, 0
        for k, v in nShelf.items():
            if v > 0:
                nCcd += 1
                nDet += v

        # a bit sketchy adding tests in the plot section, but these are dummies
        # they pass useful numbers through to the display, but don't actually test
        # anything useful
        test = testCode.Test("nDetections",
                             nDet, [1, None],
                             "number of detected sources",
                             areaLabel="all")
        testSet.addTest(test)
        test = testCode.Test("nCcd",
                             nCcd, [1, None],
                             "number of ccds processed",
                             areaLabel="all")
        testSet.addTest(test)
    def test(self, data, dataId, fluxType="psf"):

        testSet = self.getTestSet(data, dataId)
        testSet.addMetadata({"Description": self.description})

        self.fluxType = fluxType
        self.detector = data.getDetectorBySensor(dataId)
        self.filter = data.getFilterBySensor(dataId)
        self.matchListDictSrc = data.getMatchListBySensor(dataId, useRef='src')

        self.orphan = raftCcdData.RaftCcdVector(self.detector)
        self.matchedStar = raftCcdData.RaftCcdVector(self.detector)
        self.blendedStar = raftCcdData.RaftCcdVector(self.detector)
        self.undetectedStar = raftCcdData.RaftCcdVector(self.detector)
        self.matchedGalaxy = raftCcdData.RaftCcdVector(self.detector)
        self.blendedGalaxy = raftCcdData.RaftCcdVector(self.detector)
        self.undetectedGalaxy = raftCcdData.RaftCcdVector(self.detector)
        self.depth = raftCcdData.RaftCcdData(self.detector)

        if hasMinuit:
            self.fit = raftCcdData.RaftCcdData(self.detector,
                                               initValue=[0.0, 0.0])

        sCatDummy = pqaSource.Catalog().catalog
        sCatSchema = sCatDummy.getSchema()
        srefCatDummy = pqaSource.RefCatalog().catalog
        srefCatSchema = srefCatDummy.getSchema()

        psfKey = sCatSchema.find('PsfFlux').key
        psfErrKey = sCatSchema.find('PsfFluxErr').key
        apKey = sCatSchema.find('ApFlux').key
        apErrKey = sCatSchema.find('ApFluxErr').key
        extKey = sCatSchema.find('Extendedness').key

        refPsfKey = srefCatSchema.find('PsfFlux').key

        self.faintest = 0.0
        for key in self.detector.keys():
            raftId = self.detector[key].getParent().getId().getName()
            ccdId = self.detector[key].getId().getName()
            filterName = self.filter[key].getName()

            if self.matchListDictSrc.has_key(key):
                # Detections
                matchSet = [[
                    self.matchListDictSrc[key]['matched'], self.matchedStar,
                    self.matchedGalaxy
                ],
                            [
                                self.matchListDictSrc[key]['blended'],
                                self.blendedStar, self.blendedGalaxy
                            ]]
                for mset in matchSet:
                    mdict, starvec, galvec = mset

                    stars = []
                    galaxies = []
                    for m in mdict:
                        sref, s, dist = m
                        if fluxType == "psf":
                            fref = sref.getD(refPsfKey)
                            f = s.getD(psfKey)
                            ferr = s.getD(psfErrKey)
                        else:
                            fref = sref.getD(refPsfKey)
                            f = s.getD(apKey)
                            ferr = s.getD(apErrKey)

                        if (fref > 0.0 and f > 0.0):
                            # Use known catalog mag
                            mrefmag = -2.5 * num.log10(fref)
                            if num.isfinite(mrefmag):
                                if mrefmag > self.faintest:
                                    self.faintest == mrefmag

                                if s.getD(extKey) > 0.0:
                                    galaxies.append(mrefmag)
                                else:
                                    stars.append(mrefmag)

                    starvec.set(raftId, ccdId, num.array(stars))
                    galvec.set(raftId, ccdId, num.array(galaxies))

                # Non-detections
                undetectedStars = []
                undetectedGalaxies = []

                for nondet in self.matchListDictSrc[key]['undetected']:
                    mag = nondet.getMag(filterName)
                    if mag > self.faintest:
                        self.faintest = mag
                    if nondet.getIsStar():
                        undetectedStars.append(mag)
                    else:
                        undetectedGalaxies.append(mag)
                self.undetectedStar.set(raftId, ccdId,
                                        num.array(undetectedStars))
                self.undetectedGalaxy.set(raftId, ccdId,
                                          num.array(undetectedGalaxies))

                # Orphans
                orphans = []
                for orphan in self.matchListDictSrc[key]['orphan']:
                    if self.fluxType == "psf":
                        f = orphan.getD(psfKey)
                    else:
                        f = orphan.getD(apKey)
                    if f > 0.0:
                        orphmag = -2.5 * num.log10(f)
                        orphans.append(orphmag)
                        if orphmag > self.faintest:
                            self.faintest = orphmag
                self.orphan.set(raftId, ccdId, num.array(orphans))

                ############ Calculate limiting mag

                maxDepth = self.limitingMag(raftId, ccdId)
                self.depth.set(raftId, ccdId, maxDepth)

                areaLabel = data.cameraInfo.getDetectorName(raftId, ccdId)
                label = "photometric depth "
                comment = "magnitude where star completeness drops below 0.5"
                test = testCode.Test(label,
                                     maxDepth,
                                     self.limits,
                                     comment,
                                     areaLabel=areaLabel)
                testSet.addTest(test)
    def test(self, data, dataId):

        # get data
        self.matchListDictSrc = data.getMatchListBySensor(dataId, useRef='src')
        self.detector         = data.getDetectorBySensor(dataId)
        self.filter           = data.getFilterBySensor(dataId)
        
        #self.clusters = data.getSourceClusters(dataId)

        # compute the mean ra, dec for each source cluster

        self.dRa  = raftCcdData.RaftCcdVector(self.detector)
        self.dDec = raftCcdData.RaftCcdVector(self.detector)
        self.x    = raftCcdData.RaftCcdVector(self.detector)
        self.y    = raftCcdData.RaftCcdVector(self.detector)

        self.sCatDummy = pqaSource.Catalog()
        sCatDummy = self.sCatDummy.catalog
        sCatSchema = sCatDummy.getSchema()
        self.srefCatDummy  = pqaSource.RefCatalog()
        srefCatDummy = self.srefCatDummy.catalog
        srefCatSchema = srefCatDummy.getSchema()
        
        xKey      = sCatSchema.find('XAstrom').key
        yKey      = sCatSchema.find('YAstrom').key
        raKey     = sCatSchema.find('Ra').key
        decKey    = sCatSchema.find('Dec').key
        refRaKey  = srefCatSchema.find('Ra').key
        refDecKey = srefCatSchema.find('Dec').key

        filter = None
        key = None
        for key in self.matchListDictSrc.keys():
            raft = self.detector[key].getParent().getId().getName()
            ccd  = self.detector[key].getId().getName()
            filter = self.filter[key].getName()

            matchList = self.matchListDictSrc[key]['matched']
            for m in matchList:
                sref, s, dist = m
                ra, dec, raRef, decRef = \
                    [numpy.radians(x) for x in [s.getD(raKey), s.getD(decKey), \
                                                    sref.getD(refRaKey), sref.getD(refDecKey)]]
                
                dDec = decRef - dec
                dRa  = (raRef - ra)*abs(numpy.cos(decRef))
                
                intcen = s.getD(self.sCatDummy.FlagPixInterpCenKey)
                satcen = s.getD(self.sCatDummy.FlagPixSaturCenKey)
                edge   = s.getD(self.sCatDummy.FlagPixEdgeKey)

                if data.cameraInfo.name == 'coadd':
                    flagit = (satcen or edge) # coadds have excessive area covered by InterpCen flags
                else:
                    flagit = (intcen or satcen or edge)

                if not (flagit):
                    self.dRa.append(raft, ccd, dRa)
                    self.dDec.append(raft, ccd, dDec)
                    self.x.append(raft, ccd, s.getD(xKey))
                    self.y.append(raft, ccd, s.getD(yKey))
                    
                    
        testSet = self.getTestSet(data, dataId)
        testSet.addMetadata({"Description": self.description})
        
        self.medErrArcsec = raftCcdData.RaftCcdData(self.detector)
        self.medThetaRad  = raftCcdData.RaftCcdData(self.detector)

        for raft,  ccd in self.dRa.raftCcdKeys():
            dRa  = self.dRa.get(raft, ccd).copy()
            dDec = self.dDec.get(raft, ccd).copy()

            if len(dRa) > 0:
                dRaMed = numpy.median(dRa)
                dDecMed = numpy.median(dDec)
            else:
                dRaMed = 0.0
                dDecMed = 0.0

            sysErr = numpy.sqrt(dRaMed**2 + dDecMed**2)*afwGeom.radians
            sysErrArcsec = sysErr.asArcseconds()
            sysThetaRad  = numpy.arctan2(dDecMed, dRaMed)
            
            dRa  -= dRaMed
            dDec -= dDecMed

            rmsErr = numpy.sqrt(dRa**2 + dDec**2)
            rmsThetaRad  = numpy.arctan2(dDec, dRa)

            if len(rmsErr) > 0:
                stat  = afwMath.makeStatistics(rmsErr, afwMath.NPOINT | afwMath.MEDIAN)
                medRmsErr = stat.getValue(afwMath.MEDIAN)
                stat  = afwMath.makeStatistics(rmsThetaRad, afwMath.NPOINT | afwMath.MEDIAN)
                medRmsThetaRad = stat.getValue(afwMath.MEDIAN)
                n = stat.getValue(afwMath.NPOINT)
            else:
                medRmsErr = -1.0
                medRmsThetaRad = 0.0
                n = 0
                
            medRmsErr = medRmsErr*afwGeom.radians
            
            self.medErrArcsec.set(raft, ccd, sysErrArcsec)
            self.medThetaRad.set(raft, ccd, sysThetaRad)
            
            areaLabel = data.cameraInfo.getDetectorName(raft, ccd)
            label = "median systematic astrometry error "
            comment = "median sqrt(dRa^2+dDec^2) (arcsec, nstar=%d)" % (n)
            test = testCode.Test(label, sysErrArcsec, self.limits, comment, areaLabel=areaLabel)
            testSet.addTest(test)

            label = "median random astrometry error "
            comment = "median sqrt((dRa-dRaMed)^2+(dDec-dDecMed)^2) (arcsec, nstar=%d)" % (n)
            test = testCode.Test(label, medRmsErr.asArcseconds(), self.limits, comment, areaLabel=areaLabel)
            testSet.addTest(test)
Esempio n. 5
0
    def test(self, data, dataId):

        # get data
        self.detector = data.getDetectorBySensor(dataId)
        self.filter = data.getFilterBySensor(dataId)

        self.derr = raftCcdData.RaftCcdVector(self.detector)
        self.diff = raftCcdData.RaftCcdVector(self.detector)
        self.mag = raftCcdData.RaftCcdVector(self.detector)
        self.x = raftCcdData.RaftCcdVector(self.detector)
        self.y = raftCcdData.RaftCcdVector(self.detector)
        self.star = raftCcdData.RaftCcdVector(self.detector)

        filter = None

        self.matchListDictSrc = None
        self.ssDict = None

        # if we're asked to compare catalog fluxes ... we need a matchlist
        if self.magType1 == "cat" or self.magType2 == "cat":
            self.matchListDictSrc = data.getMatchListBySensor(dataId,
                                                              useRef='src')
            for key in self.matchListDictSrc.keys():
                raft = self.detector[key].getParent().getId().getName()
                ccd = self.detector[key].getId().getName()
                filter = self.filter[key].getName()

                matchList = self.matchListDictSrc[key]['matched']
                #qaAnaUtil.isStar(matchList)

                for m in matchList:
                    sref, s, dist = m

                    f1 = self._getFlux(self.magType1, s, sref)
                    f2 = self._getFlux(self.magType2, s, sref)
                    df1 = self._getFluxErr(self.magType1, s, sref)
                    df2 = self._getFluxErr(self.magType2, s, sref)

                    #badFlags = pqaSource.INTERP_CENTER | pqaSource.SATUR_CENTER | pqaSource.EDGE
                    intcen = s.getD(self.sCatDummy.FlagPixInterpCenKey)
                    satcen = s.getD(self.sCatDummy.FlagPixSaturCenKey)
                    edge = s.getD(self.sCatDummy.FlagPixEdgeKey)

                    if data.cameraInfo.name == 'coadd':
                        flagit = (
                            satcen or edge
                        )  # coadds have excessive area covered by InterpCen flags
                    else:
                        flagit = (intcen or satcen or edge)

                    if (f1 > 0.0 and f2 > 0.0 and not flagit):
                        m1 = -2.5 * numpy.log10(f1)
                        m2 = -2.5 * numpy.log10(f2)
                        dm1 = 2.5 / numpy.log(10.0) * df1 / f1
                        dm2 = 2.5 / numpy.log(10.0) * df2 / f2

                        star = 0 if s.getD(
                            self.sCatDummy.ExtendednessKey) else 1

                        if numpy.isfinite(m1) and numpy.isfinite(m2):
                            self.derr.append(raft, ccd,
                                             numpy.sqrt(dm1**2 + dm2**2))
                            self.diff.append(raft, ccd, m1 - m2)
                            self.mag.append(raft, ccd, m1)
                            self.x.append(raft, ccd,
                                          s.getD(self.sCatDummy.XAstromKey))
                            self.y.append(raft, ccd,
                                          s.getD(self.sCatDummy.YAstromKey))
                            self.star.append(raft, ccd, star)

        # if we're not asked for catalog fluxes, we can just use a sourceSet
        else:
            self.ssDict = data.getSourceSetBySensor(dataId)
            for key, ss in self.ssDict.items():
                raft = self.detector[key].getParent().getId().getName()
                ccd = self.detector[key].getId().getName()

                filter = self.filter[key].getName()

                #qaAnaUtil.isStar(ss)  # sets the 'STAR' flag
                for s in ss:
                    f1 = self._getFlux(self.magType1, s, s)
                    f2 = self._getFlux(self.magType2, s, s)
                    df1 = self._getFluxErr(self.magType1, s, s)
                    df2 = self._getFluxErr(self.magType2, s, s)
                    intcen = s.getD(self.sCatDummy.FlagPixInterpCenKey)
                    satcen = s.getD(self.sCatDummy.FlagPixSaturCenKey)
                    edge = s.getD(self.sCatDummy.FlagPixEdgeKey)

                    if data.cameraInfo.name == 'coadd':
                        flagit = (
                            satcen or edge
                        )  # coadds have excessive area covered by InterpCen flags
                    else:
                        flagit = (intcen or satcen or edge)

                    if ((f1 > 0.0 and f2 > 0.0) and not flagit):

                        m1 = -2.5 * numpy.log10(
                            f1)  #self.calib[key].getMagnitude(f1)
                        m2 = -2.5 * numpy.log10(
                            f2)  #self.calib[key].getMagnitude(f2)
                        dm1 = 2.5 / numpy.log(10.0) * df1 / f1
                        dm2 = 2.5 / numpy.log(10.0) * df2 / f2

                        star = 0 if s.getD(
                            self.sCatDummy.ExtendednessKey) else 1

                        #if star:
                        #    print "isStar: ", star
                        if numpy.isfinite(m1) and numpy.isfinite(m2):
                            self.derr.append(raft, ccd,
                                             numpy.sqrt(dm1**2 + dm2**2))
                            self.diff.append(raft, ccd, m1 - m2)
                            self.mag.append(raft, ccd, m1)
                            self.x.append(raft, ccd,
                                          s.getD(self.sCatDummy.XAstromKey))
                            self.y.append(raft, ccd,
                                          s.getD(self.sCatDummy.YAstromKey))
                            self.star.append(raft, ccd, star)

        testSet = self.getTestSet(data,
                                  dataId,
                                  label=self.magType1 + "-" + self.magType2)

        testSet.addMetadata('magType1', self.magType1)
        testSet.addMetadata('magType2', self.magType2)
        testSet.addMetadata({"Description": self.description})

        self.means = raftCcdData.RaftCcdData(self.detector)
        self.medians = raftCcdData.RaftCcdData(self.detector)
        self.stds = raftCcdData.RaftCcdData(self.detector)
        self.derrs = raftCcdData.RaftCcdData(self.detector)
        self.trend = raftCcdData.RaftCcdData(self.detector,
                                             initValue=[0.0, 0.0])

        self.dmagMax = 0.4
        allMags = numpy.array([])
        allDiffs = numpy.array([])

        for raft, ccd in self.mag.raftCcdKeys():
            dmag0 = self.diff.get(raft, ccd)
            mag0 = self.mag.get(raft, ccd)
            derr0 = self.derr.get(raft, ccd)
            star = self.star.get(raft, ccd)

            wGxy = numpy.where((mag0 > 10) & (mag0 < self.magCut) & (star == 0)
                               & (numpy.abs(dmag0) < 1.0))[0]
            w = numpy.where((mag0 > 10) & (mag0 < self.magCut) & (star > 0))[0]

            mag = mag0[w]
            dmag = dmag0[w]
            derr = derr0[w]

            allMags = numpy.append(allMags, mag)
            allDiffs = numpy.append(allDiffs, dmag)

            # already using NaN for 'no-data' for this ccd
            #  (because we can't test for 'None' in a numpy masked_array)
            # unfortunately, these failures will have to do
            mean = 99.0
            median = 99.0
            std = 99.0
            derrmed = 99.0
            n = 0
            lineFit = [[99.0, 0.0, 0.0, 0.0]] * 3
            lineCoeffs = [[99.0, 0.0]] * 3
            if len(dmag) > 0:
                stat = afwMath.makeStatistics(
                    dmag, afwMath.NPOINT | afwMath.MEANCLIP | afwMath.STDEVCLIP
                    | afwMath.MEDIAN)
                mean = stat.getValue(afwMath.MEANCLIP)
                median = stat.getValue(afwMath.MEDIAN)
                std = stat.getValue(afwMath.STDEVCLIP)
                n = stat.getValue(afwMath.NPOINT)

                derrmed = afwMath.makeStatistics(
                    derr, afwMath.MEDIAN).getValue(afwMath.MEDIAN)

                # get trendlines for stars/galaxies
                # for alldata, use trendline for stars
                if len(dmag) > 1:
                    lineFit[0] = qaAnaUtil.robustPolyFit(mag, dmag, 1)
                    lineCoeffs[0] = lineFit[0][0], lineFit[0][2]
                if len(wGxy) > 1:
                    lineFit[1] = qaAnaUtil.robustPolyFit(
                        mag0[wGxy], dmag0[wGxy], 1)
                    lineCoeffs[1] = lineFit[1][0], lineFit[1][2]
                lineFit[2] = lineFit[0]
                lineCoeffs[2] = lineCoeffs[0]

            tag = self.magType1 + "_vs_" + self.magType2
            dtag = self.magType1 + "-" + self.magType2
            self.means.set(raft, ccd, mean)
            areaLabel = data.cameraInfo.getDetectorName(raft, ccd)
            label = "mean " + tag  # +" " + areaLabel
            comment = "mean " + dtag + " (mag lt %.1f, nstar/clip=%d/%d)" % (
                self.magCut, len(dmag), n)
            testSet.addTest(
                testCode.Test(label,
                              mean,
                              self.deltaLimits,
                              comment,
                              areaLabel=areaLabel))

            self.medians.set(raft, ccd, median)
            label = "median " + tag  #+" "+areaLabel
            comment = "median " + dtag + " (mag lt %.1f, nstar/clip=%d/%d)" % (
                self.magCut, len(dmag), n)
            testSet.addTest(
                testCode.Test(label,
                              median,
                              self.deltaLimits,
                              comment,
                              areaLabel=areaLabel))

            self.stds.set(raft, ccd, std)
            label = "stdev " + tag  #+" " + areaLabel
            comment = "stdev of " + dtag + " (mag lt %.1f, nstar/clip=%d/%d)" % (
                self.magCut, len(dmag), n)
            testSet.addTest(
                testCode.Test(label,
                              std,
                              self.rmsLimits,
                              comment,
                              areaLabel=areaLabel))

            self.derrs.set(raft, ccd, derrmed)
            label = "derr " + tag
            comment = "add phot err in quad for " + dtag + " (mag lt %.1f, nstar/clip=%d/%d)" % (
                self.magCut, len(dmag), n)
            testSet.addTest(
                testCode.Test(label,
                              derrmed,
                              self.derrLimits,
                              comment,
                              areaLabel=areaLabel))

            self.trend.set(raft, ccd, lineFit)
            label = "slope " + tag  #+" " + areaLabel
            slopeLimits = self.slopeLimits[0] * lineFit[0][
                1], self.slopeLimits[1] * lineFit[0][1]
            comment = "slope of "+dtag+" (mag lt %.1f, nstar/clip=%d/%d) limits=(%.1f,%.1f)sigma" % \
                      (self.magCut, len(dmag), n, self.slopeLimits[0], self.slopeLimits[1])
            testSet.addTest(
                testCode.Test(label,
                              lineCoeffs[0][0],
                              slopeLimits,
                              comment,
                              areaLabel=areaLabel))

        # do a test of all CCDs for the slope ... suffering small number problems
        #  on indiv ccds and could miss a problem

        lineFit = [99.0, 0.0, 0.0, 0.0]
        lineCoeffs = [99.0, 0.0]
        if len(allDiffs) > 1:
            lineFit = qaAnaUtil.robustPolyFit(allMags, allDiffs, 1)
            lineCoeffs = lineFit[0], lineFit[2]
        label = "slope"
        slopeLimits = self.slopeLimits[0] * lineFit[1], self.slopeLimits[
            1] * lineFit[1]
        comment = "slope for all ccds (mag lt %.1f, nstar=%d) limits=(%.1f,%.1f sigma)" % (
            self.magCut, len(allDiffs), self.slopeLimits[0],
            self.slopeLimits[1])
        testSet.addTest(
            testCode.Test(label,
                          lineCoeffs[0],
                          slopeLimits,
                          comment,
                          areaLabel="all"))
    def test(self, data, dataId):
        self.matchListDictSrc = data.getMatchListBySensor(dataId, useRef='src')
        self.detector = data.getDetectorBySensor(dataId)

        if type(self.ownFilt) == type(None):
            filt = data.getFilterBySensor(dataId)
            self.ownFilt = filt.values()[0]

        if len(self.visits) == 0:
            visitList = [
                dataId['visit'],
            ]
        else:
            visitList = self.visits

        for visit in visitList:
            self.visitMatches[visit] = data.getVisitMatchesBySensor(
                self.database, visit, dataId)
            kvs = self.visitMatches[visit].keys()
            self.visitFilters[visit] = None
            if len(kvs) > 0:
                for kv in kvs:
                    if len(self.visitMatches[visit][kv]) > 0:
                        self.visitFilters[visit] = self.visitMatches[visit][
                            kv][0][2]
                        break

            # create containers for data we're interested in
            self.mag[visit] = raftCcdData.RaftCcdVector(self.detector)
            self.magErr[visit] = raftCcdData.RaftCcdVector(self.detector)
            self.refMag[visit] = raftCcdData.RaftCcdVector(self.detector)
            self.visitMag[visit] = raftCcdData.RaftCcdVector(self.detector)
            self.visitMagErr[visit] = raftCcdData.RaftCcdVector(self.detector)
            self.visitRefMag[visit] = raftCcdData.RaftCcdVector(self.detector)
            self.refId[visit] = raftCcdData.RaftCcdVector(self.detector)
            self.star[visit] = raftCcdData.RaftCcdVector(self.detector)

            if data.cameraInfo.name == "coadd":
                badFlags = measAlg.Flags.SATUR_CENTER | measAlg.Flags.EDGE  # coadds have excessive area covered by INTERP_CENTER flags
            else:
                badFlags = measAlg.Flags.INTERP_CENTER | measAlg.Flags.SATUR_CENTER | measAlg.Flags.EDGE

            for key in self.matchListDictSrc.keys():
                raft = self.detector[key].getParent().getId().getName()
                ccd = self.detector[key].getId().getName()

                # All detections matched to ref objects
                srcMatchList = self.matchListDictSrc[key]['matched']
                # All visit dets in footprint of original image
                visMatchList = self.visitMatches[visit][key]

                # List of reference object ids
                srcObjIds = num.array([x[0].getId() for x in srcMatchList])
                visObjIds = num.array([x[0].getId() for x in visMatchList])
                common = list(set(srcObjIds) & set(visObjIds))

                self.log.log(self.log.INFO, "%s :" % (key))
                if len(common) == 0:
                    self.log.log(
                        self.log.INFO,
                        "  No overlap, Using pre-to-post PT1.2 objectID mapping..."
                    )

                    # Try objectID hack from PT1.2 to post-PT1.2:
                    visObjIds *= 2
                    isStar = (num.array(
                        [x[0].getFlagForDetection()
                         for x in visMatchList]) & measAlg.Flags.STAR) > 0
                    visObjIds += isStar
                    common = list(set(srcObjIds) & set(visObjIds))
                self.log.log(self.log.INFO, "Found %d matches" % (len(common)))

                # Iterate over all object ids
                for i in range(len(common)):
                    srcObjId = common[i]
                    idxS = num.where(srcObjIds == srcObjId)[0]
                    idxV = num.where(visObjIds == srcObjId)[0]

                    # only take 1-to-1 matches
                    if len(idxS) != 1 or len(idxV) != 1:
                        continue

                    sref1 = srcMatchList[idxS[0]][0]
                    srcv1 = srcMatchList[idxS[0]][1]

                    sref2 = visMatchList[idxV[0]][0]
                    srcv2 = visMatchList[idxV[0]][1]

                    f1 = self._getFlux(self.magType, srcv1, sref1)
                    f2 = self._getFlux(self.magType, srcv2, sref2)
                    df1 = self._getFluxErr(self.magType, srcv1, sref1)
                    df2 = self._getFluxErr(self.magType, srcv2, sref2)

                    fref1 = self._getFlux("cat", srcv1, sref1)
                    fref2 = self._getFlux("cat", srcv2, sref2)

                    # Measurment flags; note no star/gal separation yet
                    flags1 = srcv1.getFlagForDetection()
                    flags2 = srcv2.getFlagForDetection()
                    # Get star/gal info here
                    isStar = sref1.getFlagForDetection() & measAlg.Flags.STAR

                    if (f1 > 0.0 and f2 > 0.0) and (
                            not flags1 & badFlags) and (not flags2 & badFlags):
                        m1 = -2.5 * num.log10(f1)
                        m2 = -2.5 * num.log10(f2)
                        dm1 = 2.5 / num.log(10.0) * df1 / f1
                        dm2 = 2.5 / num.log(10.0) * df2 / f2

                        M1 = -2.5 * num.log10(fref1)
                        M2 = -2.5 * num.log10(fref2)

                        if num.isfinite(m1) and num.isfinite(
                                m2) and num.isfinite(M1) and num.isfinite(M2):
                            self.mag[visit].append(raft, ccd, m1)
                            self.magErr[visit].append(raft, ccd, dm1)

                            self.visitMag[visit].append(raft, ccd, m2)
                            self.visitMagErr[visit].append(raft, ccd, dm2)

                            self.refMag[visit].append(raft, ccd, M1)
                            self.visitRefMag[visit].append(raft, ccd, M2)

                            self.refId[visit].append(raft, ccd, srcObjId)
                            self.star[visit].append(raft, ccd, isStar)

            # TMI
            #testLabel = "%s_%s_%s" % (self.database, visit, self.magType)

            #testLabel = "%s-%s" % (visit, self.magType)
            testLabel = "%s" % (self.magType)
            testSet = self.getTestSet(data, dataId, label=testLabel)
            testSet.addMetadata(
                "magType", "%s mags from %s" % (self.magType, self.database))
            testSet.addMetadata({"Description": self.description})

            self.meanDmags[visit] = raftCcdData.RaftCcdData(self.detector)
            self.medianDmags[visit] = raftCcdData.RaftCcdData(self.detector)
            self.stdDmags[visit] = raftCcdData.RaftCcdData(self.detector)

            for raft, ccd in self.mag[visit].raftCcdKeys():
                m1 = self.mag[visit].get(raft, ccd)
                m2 = self.visitMag[visit].get(raft, ccd)
                dm1 = self.magErr[visit].get(raft, ccd)
                dm2 = self.visitMagErr[visit].get(raft, ccd)
                M1 = self.refMag[visit].get(raft, ccd)
                M2 = self.visitRefMag[visit].get(raft, ccd)
                star = self.star[visit].get(raft, ccd)

                idxS = num.where((M1 > 10) & (M1 < self.magCut)
                                 & (star > 0))[0]

                dmS = m1[idxS] - m2[idxS] - (M1[idxS] - M2[idxS])
                ddmS = num.sqrt(dm1[idxS]**2 + dm2[idxS]**2)

                if len(dmS) > 0:
                    stat = afwMath.makeStatistics(
                        dmS, afwMath.NPOINT | afwMath.MEANCLIP
                        | afwMath.STDEVCLIP | afwMath.MEDIAN)
                    mean = stat.getValue(afwMath.MEANCLIP)
                    median = stat.getValue(afwMath.MEDIAN)
                    std = stat.getValue(afwMath.STDEVCLIP)
                    npts = stat.getValue(afwMath.NPOINT)

                    # Common
                    tag = self.magType
                    areaLabel = data.cameraInfo.getDetectorName(raft, ccd)

                    # MEAN
                    self.meanDmags[visit].set(raft, ccd, mean)
                    label = "mean " + tag
                    comment = "mean " + tag + " (mag lt %.1f, nstar/clip=%d/%d)" % (
                        self.magCut, len(dmS), npts)
                    testSet.addTest(
                        testCode.Test(label,
                                      mean,
                                      self.deltaLimits,
                                      comment,
                                      areaLabel=areaLabel))

                    # MEDIAN
                    self.medianDmags[visit].set(raft, ccd, median)
                    label = "median " + tag
                    comment = "median " + tag + " (mag lt %.1f, nstar/clip=%d/%d)" % (
                        self.magCut, len(dmS), npts)
                    testSet.addTest(
                        testCode.Test(label,
                                      median,
                                      self.deltaLimits,
                                      comment,
                                      areaLabel=areaLabel))

                    # STD
                    self.stdDmags[visit].set(raft, ccd, std)
                    label = "stdev " + tag
                    comment = "stdev " + tag + " (mag lt %.1f, nstar/clip=%d/%d)" % (
                        self.magCut, len(dmS), npts)
                    testSet.addTest(
                        testCode.Test(label,
                                      std,
                                      self.rmsLimits,
                                      comment,
                                      areaLabel=areaLabel))
Esempio n. 7
0
    def test(self, data, dataId):

        self.detector = data.getDetectorBySensor(dataId)
        self.filter = data.getFilterBySensor(dataId)

        # create containers for data we're interested in
        self.mem = raftCcdData.RaftCcdData(self.detector)
        self.testRuntime = raftCcdData.RaftCcdData(self.detector)
        self.plotRuntime = raftCcdData.RaftCcdData(self.detector)

        # create a testset
        testSet = self.getTestSet(data, dataId)

        # this normally gets set in the plot as that's where the caching happens,
        # here we're stashing the nDetection and nCcd values, so we need to set it early.
        testSet.setUseCache(self.useCache)
        testSet.addMetadata({"Description": self.description})

        performBase = "performShelf"
        mb = testSet.unshelve(performBase)

        for raft, ccd in self.mem.raftCcdKeys():

            # add tests for acceptible numpy of empty sectors
            areaLabel = data.cameraInfo.getDetectorName(raft, ccd)

            label = "memory usage"
            mem = getMemUsageThisPid() / 1024  # MB
            comment = "memory usage in MB (%.2f%% of %.0fMB sys.)" % (
                100.0 * mem / self.memtotal, self.memtotal)
            test = testCode.Test(label,
                                 mem,
                                 self.limits,
                                 comment,
                                 areaLabel=areaLabel)
            testSet.addTest(test)
            mb[ccd] = mem
            self.mem.set(raft, ccd, mem)

            testRuntime = data.getPerformance(dataId, 'total', 'test-runtime')
            if testRuntime is None:
                testRuntime = 0.0
            self.testRuntime.set(raft, ccd, testRuntime)

            plotRuntime = data.getPerformance(dataId, 'total', 'plot-runtime')
            # if running with plot() forked, plot times are not defined.
            if plotRuntime is None:
                plotRuntime = 0.0
            self.plotRuntime.set(raft, ccd, plotRuntime)

            tTest = testCode.Test("test-runtime",
                                  testRuntime, [0.0, 3600],
                                  "Runtime for test()[s]",
                                  areaLabel=areaLabel)
            testSet.addTest(tTest)

            pTest = testCode.Test("plot-runtime",
                                  plotRuntime, [0.0, 3600],
                                  "Runtime for plot()[s] (%d plots)" %
                                  (qaFig.QaFigure.count),
                                  areaLabel=areaLabel)
            testSet.addTest(pTest)

            info = self.node + " " + " ".join(self.dist)
            infoTest = testCode.Test("generalInfo",
                                     0.5, [0.0, 1.0],
                                     info,
                                     areaLabel=areaLabel)
            testSet.addTest(infoTest)

        testSet.shelve(performBase, mb)
Esempio n. 8
0
    def test(self, data, dataId, fluxType="psf"):

        testSet = self.getTestSet(data, dataId)
        testSet.addMetadata({"Description": self.description})

        self.fluxType = fluxType
        self.detector = data.getDetectorBySensor(dataId)
        self.filter = data.getFilterBySensor(dataId)
        self.calib = data.getCalibBySensor(dataId)
        self.matchListDictSrc = data.getMatchListBySensor(dataId, useRef='src')

        # Ignore blends in this analysis
        self.orphan = raftCcdData.RaftCcdVector(self.detector)
        self.matchedStar = raftCcdData.RaftCcdData(self.detector)
        self.undetectedStar = raftCcdData.RaftCcdVector(self.detector)
        self.matchedGalaxy = raftCcdData.RaftCcdData(self.detector)
        self.undetectedGalaxy = raftCcdData.RaftCcdVector(self.detector)

        # Results
        self.zeroPoint = raftCcdData.RaftCcdData(self.detector)
        self.medOffset = raftCcdData.RaftCcdData(self.detector)

        if data.cameraInfo.name == "coadd":
            badFlags = pqaSource.SATUR_CENTER | pqaSource.EDGE  # coadds have excessive area covered by INTERP_CENTER flags
        else:
            badFlags = pqaSource.INTERP_CENTER | pqaSource.SATUR_CENTER | pqaSource.EDGE

        for key in self.detector.keys():
            raftId = self.detector[key].getParent().getId().getName()
            ccdId = self.detector[key].getId().getName()
            filterName = self.filter[key].getName()

            self.matchedStar.set(
                raftId, ccdId, {
                    "Refmag": num.array([]),
                    "Imgmag": num.array([]),
                    "Imgerr": num.array([]),
                })
            self.matchedGalaxy.set(
                raftId, ccdId, {
                    "Refmag": num.array([]),
                    "Imgmag": num.array([]),
                    "Imgerr": num.array([])
                })
            self.undetectedStar.set(raftId, ccdId, num.array([]))
            self.undetectedGalaxy.set(raftId, ccdId, num.array([]))
            self.orphan.set(raftId, ccdId, num.array([]))

            fmag0 = self.calib[key].getFluxMag0()[0]
            if fmag0 <= 0.0:
                self.zeroPoint.set(raftId, ccdId, NaN)
                continue
            zpt = -2.5 * num.log10(fmag0)
            self.zeroPoint.set(raftId, ccdId, zpt)

            if self.matchListDictSrc.has_key(key):
                # Matched
                mdict = self.matchListDictSrc[key]['matched']
                stars = []
                galaxies = []

                for m in mdict:
                    sref, s, dist = m
                    if fluxType == "psf":
                        fref = sref.getD(self.srefCatDummy.PsfFluxKey)
                        f = s.getD(self.sCatDummy.PsfFluxKey)
                        ferr = s.getD(self.sCatDummy.PsfFluxErrKey)
                    else:
                        fref = sref.getD(self.srefCatDummy.PsfFluxKey)
                        f = s.getD(self.sCatDummy.ApFluxKey)
                        ferr = s.getD(self.sCatDummy.ApFluxErrKey)

                    # un-calibrate the magnitudes
                    f *= fmag0

                    intcen = s.getD(self.sCatDummy.FlagPixInterpCenKey)
                    satcen = s.getD(self.sCatDummy.FlagPixSaturCenKey)
                    edge = s.getD(self.sCatDummy.FlagPixEdgeKey)

                    if data.cameraInfo.name == 'coadd':
                        flagit = (
                            satcen or edge
                        )  # coadds have excessive area covered by InterpCen flags
                    else:
                        flagit = (intcen or satcen or edge)

                    if (fref > 0.0 and f > 0.0 and not flagit):
                        mrefmag = -2.5 * num.log10(fref)
                        mimgmag = -2.5 * num.log10(f)
                        mimgmerr = 2.5 / num.log(10.0) * ferr / f

                        star = 0 if s.getD(
                            self.sCatDummy.ExtendednessKey) else 1

                        if num.isfinite(mrefmag) and num.isfinite(mimgmag):
                            if star:
                                stars.append((mrefmag, mimgmag, mimgmerr))
                            else:
                                galaxies.append((mrefmag, mimgmag, mimgmerr))
                self.matchedStar.set(
                    raftId, ccdId, {
                        "Refmag": num.array([x[0] for x in stars]),
                        "Imgmag": num.array([x[1] for x in stars]),
                        "Imgerr": num.array([x[2] for x in stars])
                    })
                self.matchedGalaxy.set(
                    raftId, ccdId, {
                        "Refmag": num.array([x[0] for x in galaxies]),
                        "Imgmag": num.array([x[1] for x in galaxies]),
                        "Imgerr": num.array([x[2] for x in galaxies])
                    })

                # Non-detections
                undetectedStars = []
                undetectedGalaxies = []
                for nondet in self.matchListDictSrc[key]['undetected']:
                    mag = nondet.getMag(filterName)
                    if nondet.getIsStar():
                        undetectedStars.append(mag)
                    else:
                        undetectedGalaxies.append(mag)
                self.undetectedStar.set(raftId, ccdId,
                                        num.array(undetectedStars))
                self.undetectedGalaxy.set(raftId, ccdId,
                                          num.array(undetectedGalaxies))

                # Orphans
                orphans = []
                for orphan in self.matchListDictSrc[key]['orphan']:
                    if self.fluxType == "psf":
                        f = orphan.getD(self.sCatDummy.PsfFluxKey)
                    else:
                        f = orphan.getD(self.sCatDummy.ApFluxKey)
                    if f > 0.0:
                        # un-calibrate the magnitudes
                        f *= fmag0

                        orphans.append(-2.5 * num.log10(f))

                self.orphan.set(raftId, ccdId, num.array(orphans))

                # Metrics
                offset = num.array(
                    self.matchedStar.get(raftId,
                                         ccdId)["Imgmag"])  # make a copy
                offset -= self.zeroPoint.get(raftId, ccdId)
                offset -= self.matchedStar.get(raftId, ccdId)["Refmag"]
                med = num.median(offset)
                self.medOffset.set(raftId, ccdId, med)

                areaLabel = data.cameraInfo.getDetectorName(raftId, ccdId)
                label = "median offset from zeropoint"
                comment = "Median offset of calibrated stellar magnitude to zeropoint fit"
                test = testCode.Test(label,
                                     med,
                                     self.limits,
                                     comment,
                                     areaLabel=areaLabel)
                testSet.addTest(test)

                label = "median zeropoint"
                comment = "Median zeropoint measured for sensor"
                test = testCode.Test(label,
                                     zpt, [None, 0],
                                     comment,
                                     areaLabel=areaLabel)
                testSet.addTest(test)
    def test(self, data, dataId):

        # get data
        self.ssDict = data.getSourceSetBySensor(dataId)
        self.detector = data.getDetectorBySensor(dataId)
        self.filter = data.getFilterBySensor(dataId)
        self.calexpDict = data.getCalexpBySensor(dataId)
        self.wcs = data.getWcsBySensor(dataId)

        # create containers for data in the focal plane
        self.x = raftCcdData.RaftCcdVector(self.detector)
        self.y = raftCcdData.RaftCcdVector(self.detector)
        self.ra = raftCcdData.RaftCcdVector(self.detector)
        self.dec = raftCcdData.RaftCcdVector(self.detector)
        self.ellip = raftCcdData.RaftCcdVector(self.detector)
        self.theta = raftCcdData.RaftCcdVector(self.detector)

        # compute values of interest
        filter = None
        sigmaToFwhm = 2.0 * numpy.sqrt(2.0 * numpy.log(2.0))

        fwhmByKey = {}
        for key, ss in self.ssDict.items():

            if self.detector.has_key(key):
                raft = self.detector[key].getParent().getId().getName()
                ccd = self.detector[key].getId().getName()
            else:
                continue

            #qaAnaUtil.isStar(ss)

            fwhmByKey[key] = 0.0

            fwhmTmp = 0.0
            for s in ss:
                ixx = s.getD(self.sCatDummy.IxxKey)
                iyy = s.getD(self.sCatDummy.IyyKey)
                ixy = s.getD(self.sCatDummy.IxyKey)

                tmp = 0.25 * (ixx - iyy)**2 + ixy**2
                if tmp < 0:
                    continue

                a2 = 0.5 * (ixx + iyy) + numpy.sqrt(tmp)
                b2 = 0.5 * (ixx + iyy) - numpy.sqrt(tmp)

                if a2 == 0 or b2 / a2 < 0:
                    continue

                ellip = 1.0 - numpy.sqrt(b2 / a2)
                theta = 0.5 * numpy.arctan2(2.0 * ixy, ixx - iyy)

                # vectors have no direction, so default to pointing in +ve 'y'
                # - failing to do this caused a stats bug when alignment is near pi/2
                #   both +/- pi/2 arise but are essentially the same, ... and the mean is near zero
                if theta < 0.0:
                    theta += numpy.pi

                #print ixx, iyy, ixy, a2, b2, ellip, theta
                isStar = 0 if s.getD(self.sCatDummy.ExtendednessKey) else 1

                flux = s.getD(self.sCatDummy.PsfFluxKey)
                mag = 99.0
                if flux > 0:
                    mag = -2.5 * numpy.log10(s.getD(self.sCatDummy.PsfFluxKey))
                if numpy.isfinite(ellip) and numpy.isfinite(
                        theta) and isStar and mag < 20:
                    self.ellip.append(raft, ccd, ellip)
                    self.theta.append(raft, ccd, theta)
                    self.x.append(raft, ccd, s.getD(self.sCatDummy.XAstromKey))
                    self.y.append(raft, ccd, s.getD(self.sCatDummy.YAstromKey))
                    self.ra.append(raft, ccd, s.getD(self.sCatDummy.RaKey))
                    self.dec.append(raft, ccd, s.getD(self.sCatDummy.DecKey))
                    fwhmTmp += sigmaToFwhm * numpy.sqrt(0.5 * (a2 + b2))

            nFwhm = len(self.x.get(raft, ccd))
            if nFwhm:
                fwhmByKey[key] = fwhmTmp / nFwhm
            else:
                fwhmByKey[key] = 0.0

        # create a testset and add values
        testSet = self.getTestSet(data, dataId)
        testSet.addMetadata({"Description": self.description})

        # gets the stats for each sensor and put the values in the raftccd container
        self.ellipMedians = raftCcdData.RaftCcdData(self.detector)
        self.thetaMedians = raftCcdData.RaftCcdData(self.detector)
        for raft, ccd in self.ellip.raftCcdKeys():
            ellip = self.ellip.get(raft, ccd)
            theta = self.theta.get(raft, ccd)

            if len(ellip) > 0:
                stat = afwMath.makeStatistics(ellip,
                                              afwMath.NPOINT | afwMath.MEDIAN)
                ellipMed = stat.getValue(afwMath.MEDIAN)
                stat = afwMath.makeStatistics(theta,
                                              afwMath.NPOINT | afwMath.MEDIAN)
                thetaMed = stat.getValue(afwMath.MEDIAN)
                n = stat.getValue(afwMath.NPOINT)
            else:
                ellipMed = -1.0
                thetaMed = 0.0
                n = 0

            # add a test for acceptible psf ellipticity
            self.ellipMedians.set(raft, ccd, ellipMed)
            areaLabel = data.cameraInfo.getDetectorName(raft, ccd)
            label = "median psf ellipticity "
            comment = "median psf ellipticity (nstar=%d)" % (n)
            testSet.addTest(
                testCode.Test(label,
                              ellipMed,
                              self.limitsEllip,
                              comment,
                              areaLabel=areaLabel))

            # stash the angles.  We'll use them to make figures in plot()
            self.thetaMedians.set(raft, ccd, thetaMed)

        # And the Fwhm
        self.fwhm = raftCcdData.RaftCcdData(self.detector)
        for key, item in self.calexpDict.items():
            if (self.detector.has_key(key)
                    and hasattr(self.detector[key], 'getParent')
                    and hasattr(self.detector[key], 'getId')):
                raft = self.detector[key].getParent().getId().getName()
                ccd = self.detector[key].getId().getName()
            else:
                continue

            wcs = self.wcs[key]
            fwhmTmp = float(fwhmByKey[key] *
                            wcs.pixelScale().asArcseconds())  #item['fwhm']
            #print fwhmTmp, item['fwhm'], type(fwhmTmp), type(item['fwhm'])
            self.fwhm.set(raft, ccd, fwhmTmp)
            areaLabel = data.cameraInfo.getDetectorName(raft, ccd)
            label = "psf fwhm (arcsec) "
            comment = "psf fwhm (arcsec)"
            testSet.addTest(
                testCode.Test(label,
                              item['fwhm'],
                              self.limitsFwhm,
                              comment,
                              areaLabel=areaLabel))
Esempio n. 10
0
    def test(self, data, dataId):

        testSet = self.getTestSet(data, dataId)
        testSet.addMetadata({"Description": self.description})

        self.detector = data.getDetectorBySensor(dataId)
        self.filter = data.getFilterBySensor(dataId)
        self.matchListDictSrc = data.getMatchListBySensor(dataId, useRef='src')

        self.dmag = raftCcdData.RaftCcdVector(self.detector)
        self.ids = raftCcdData.RaftCcdVector(self.detector)
        self.radius = raftCcdData.RaftCcdVector(self.detector)

        self.medianOffset = raftCcdData.RaftCcdData(self.detector)
        self.rmsOffset = raftCcdData.RaftCcdData(self.detector)

        #badFlags = pqaSource.INTERP_CENTER | pqaSource.SATUR_CENTER | pqaSource.EDGE

        for key in self.detector.keys():

            if self.detector[key] is None:
                continue

            raftId = self.detector[key].getParent().getId().getName()
            ccdId = self.detector[key].getId().getName()

            # We have a trimmed detector
            self.detector[key].setTrimmed(True)
            pixelSize = self.detector[key].getPixelSize()  # mm
            centerXm, centerYm = self.detector[key].getCenter().getMm(
            )  # focal plane mm

            bbox = self.detector[key].getAllPixels()
            startX = bbox.getBeginX()
            startY = bbox.getBeginY()
            endX = bbox.getEndX()
            endY = bbox.getEndY()
            centerXp = 0.5 * (endX - startX)
            centerYp = 0.5 * (endY - startY)

            if self.matchListDictSrc.has_key(key):
                mdict = self.matchListDictSrc[key]['matched']
                for m in mdict:
                    sref, s, dist = m

                    if s.getD(
                            self.sCatDummy.ExtendednessKey):  # if non-stellar
                        continue

                    f1 = self._getFlux(self.magType1, s, sref)
                    f2 = self._getFlux(self.magType2, s, sref)

                    intcen = s.getD(self.sCatDummy.FlagPixInterpCenKey)
                    satcen = s.getD(self.sCatDummy.FlagPixSaturCenKey)
                    edge = s.getD(self.sCatDummy.FlagPixEdgeKey)

                    if data.cameraInfo.name == 'coadd':
                        flagit = (
                            satcen or edge
                        )  # coadds have excessive area covered by InterpCen flags
                    else:
                        flagit = (intcen or satcen or edge)
                    if (f1 > 0.0 and f2 > 0.0 and not flagit):
                        m1 = -2.5 * num.log10(f1)
                        m2 = -2.5 * num.log10(f2)

                        if m2 > self.maxMag:
                            continue

                        if num.isfinite(m1) and num.isfinite(m2):
                            self.dmag.append(raftId, ccdId, m1 - m2)
                            self.ids.append(raftId, ccdId, str(s.getId()))

                            if data.cameraInfo.name == 'lsstSim':
                                # XY switched
                                xmm = centerXm + (
                                    s.getD(self.sCatDummy.YAstromKey) -
                                    centerXp) * pixelSize
                                ymm = centerYm + (
                                    s.getD(self.sCatDummy.XAstromKey) -
                                    centerYp) * pixelSize
                                radiusp = num.sqrt(xmm**2 + ymm**2) / pixelSize
                            else:
                                # XY not switch, and pixel centers not in mm
                                xmm = centerXm + (s.getD(
                                    self.sCatDummy.XAstromKey) - centerXp)
                                ymm = centerYm + (s.getD(
                                    self.sCatDummy.YAstromKey) - centerYp)
                                radiusp = num.sqrt(xmm**2 + ymm**2)
                            self.radius.append(raftId, ccdId, radiusp)

                # Calculate stats
                dmags = self.dmag.get(raftId, ccdId)
                if len(dmags) > 0:
                    med = num.median(dmags)
                else:
                    med = 0.0

                std = 0.0
                if len(dmags) > 1:
                    stat = afwMath.makeStatistics(dmags, afwMath.IQRANGE)
                    std = 0.741 * stat.getValue(afwMath.IQRANGE)
                self.medianOffset.set(raftId, ccdId, med)
                self.rmsOffset.set(raftId, ccdId, std)

                areaLabel = data.cameraInfo.getDetectorName(raftId, ccdId)

                label = "median offset "
                comment = "median offset from cat mag"
                test = testCode.Test(label,
                                     med,
                                     self.medLimits,
                                     comment,
                                     areaLabel=areaLabel)
                testSet.addTest(test)

                label = "stddev offset "
                comment = "stddev of offset from cat mag"
                test = testCode.Test(label,
                                     std,
                                     self.rmsLimits,
                                     comment,
                                     areaLabel=areaLabel)
                testSet.addTest(test)