def setUp(self): self.val = 10 self.nRow, self.nCol = 100, 200 self.sctrl = afwMath.StatisticsControl() # Integers self.mimgI = afwImage.MaskedImageI(afwGeom.Extent2I(self.nRow, self.nCol)) self.mimgI.set(self.val, 0x0, self.val) self.imgI = afwImage.ImageI(afwGeom.Extent2I(self.nRow, self.nCol), self.val) self.vecI = afwMath.vectorI(self.nRow*self.nCol, self.val) # floats self.mimgF = afwImage.MaskedImageF(afwGeom.Extent2I(self.nRow, self.nCol)) self.mimgF.set(self.val, 0x0, self.val) self.imgF = afwImage.ImageF(afwGeom.Extent2I(self.nRow, self.nCol), self.val) self.vecF = afwMath.vectorF(self.nRow*self.nCol, self.val) # doubles self.mimgD = afwImage.MaskedImageD(afwGeom.Extent2I(self.nRow, self.nCol)) self.mimgD.set(self.val, 0x0, self.val) self.imgD = afwImage.ImageD(afwGeom.Extent2I(self.nRow, self.nCol), self.val) self.vecD = afwMath.vectorD(self.nRow*self.nCol, self.val) self.imgList = [self.imgI, self.imgF, self.imgD] self.mimgList = [self.mimgI, self.mimgF, self.mimgD] self.vecList = [self.vecI, self.vecF, self.vecD]
def histogram(self, data, bounds, bins=51, gaussFit=True, iterations=3, clip=3.0, title=None): plot.figure() n, bins, patches = plot.hist(data, bins=bins, range=bounds, normed=False, histtype='bar', align='mid') if gaussFit: calc = afwMath.vectorF() for value in data: if value > bounds[0] and value < bounds[1]: calc.push_back(value) stats = afwMath.makeStatistics(calc, afwMath.MEANCLIP | afwMath.STDEVCLIP) mean = stats.getValue(afwMath.MEANCLIP) stdev = stats.getValue(afwMath.STDEVCLIP) num = len(calc) print "Histogram statistics:", num, mean, stdev norm = num * (bins[1:] - bins[:-1]).mean() / math.sqrt(2.0 * math.pi) / stdev middle = (bins[:-1] + bins[1:]) / 2.0 gauss = gaussian([norm, mean, stdev], middle) plot.plot(middle, gauss, 'r-', label='%d*N(%.3f, %.3f^2)' % (num, mean, stdev)) leg = plot.legend(loc='upper right') for t in leg.get_texts(): t.set_fontsize('small') if title is not None: plot.title(title) self.pdf.savefig() plot.close()
def correctReferences(self, dataRef, references): self.log.info("Correcting reference positions...") sources = dataRef.get("src") matches = afwTable.matchRaDec(sources, references, self.config.radius * afwGeom.arcseconds) num = len(matches) self.log.info("%d matches between source and reference catalogs" % num) stats = afwMath.StatisticsControl() # XXX statistics parameters? dra, ddec = afwMath.vectorF(), afwMath.vectorF() dra.reserve(num) ddec.reserve(num) units = afwGeom.arcseconds # XXX errors in positions? for m in matches: src = m.first if src.getPsfFlux() < self.config.minFlux: continue ref = m.second offset = ref.getCoord().getTangentPlaneOffset(src.getCoord()) dra.push_back(offset[0].asAngularUnits(units)) ddec.push_back(offset[1].asAngularUnits(units)) num = len(dra) draStats = afwMath.makeStatistics(dra, afwMath.MEANCLIP | afwMath.STDEVCLIP, stats) ddecStats = afwMath.makeStatistics(ddec, afwMath.MEANCLIP | afwMath.STDEVCLIP, stats) draMean = draStats.getValue(afwMath.MEANCLIP) ddecMean = ddecStats.getValue(afwMath.MEANCLIP) self.log.info("Offset from %d sources is dRA = %f +/- %f arcsec, dDec = %f +/- %f arcsec" % (num, draMean, draStats.getValue(afwMath.STDEVCLIP), dDecMean, ddecStats.getValue(afwMath.STDEVCLIP))) angle = math.atan2(ddecMean, draMean)*afwGeom.radians distance = math.hypot(draMean, ddecMean)*units for ref in references: coord = ref.getCoord() coord.offset(angle, distance) ref.setCoord(coord) return references
def testConstantWeightedStack(self): """ Test statisticsStack() function when weighting by a vector of weights""" sctrl = afwMath.StatisticsControl() imgList = afwImage.vectorImageF() weights = afwMath.vectorF() for val in self.values: img = afwImage.ImageF(afwGeom.Extent2I(self.nX, self.nY), val) imgList.push_back(img) weights.push_back(val) imgStack = afwMath.statisticsStack(imgList, afwMath.MEAN, sctrl, weights) wsum = reduce(lambda x, y: x + y, self.values) wvalues = [x*x for x in self.values] wmean = reduce(lambda x, y: x + y, wvalues)/float(wsum) self.assertAlmostEqual(imgStack.get(self.nX//2, self.nY//2), wmean)
def testConstantWeightedStack(self): """ Test statisticsStack() function when weighting by a vector of weights""" sctrl = afwMath.StatisticsControl() imgList = afwImage.vectorImageF() weights = afwMath.vectorF() for val in self.values: img = afwImage.ImageF(afwGeom.Extent2I(self.nX, self.nY), val) imgList.push_back(img) weights.push_back(val) imgStack = afwMath.statisticsStack(imgList, afwMath.MEAN, sctrl, weights) wsum = reduce(lambda x, y: x + y, self.values) wvalues = [x*x for x in self.values] wmean = reduce(lambda x, y: x + y, wvalues)/float(wsum) self.assertAlmostEqual(imgStack.get(self.nX/2, self.nY/2), wmean)