def getInterpImage(self, bbox): """Return an image interpolated in R.A direction covering supplied bounding box @param[in] bbox: integer bounding box for image (afwGeom.Box2I) """ npoints = len(self._xList) #sort by X coordinate if npoints < 1: raise RuntimeError("Cannot create scaling image. Found no fluxMag0s to interpolate") x, z = zip(*sorted(zip(self._xList, self._scaleList))) xvec = afwMath.vectorD(x) zvec = afwMath.vectorD(z) height = bbox.getHeight() width = bbox.getWidth() x0, y0 = bbox.getMin() interp = afwMath.makeInterpolate(xvec, zvec, self.interpStyle) interpValArr = numpy.zeros(width, dtype=numpy.float32) for i, xInd in enumerate(range(x0, x0 + width)): xPos = afwImage.indexToPosition(xInd) interpValArr[i] = interp.interpolate(xPos) # assume the maskedImage being scaled is MaskedImageF (which is usually true); see ticket #3070 interpGrid = numpy.meshgrid(interpValArr, range(0, height))[0].astype(numpy.float32) image = afwImage.makeImageFromArray(interpGrid) image.setXY0(x0, y0) return image
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 setUp(self): self.n = 10 self.x = afwMath.vectorD(self.n) self.y1 = afwMath.vectorD(self.n) self.y2 = afwMath.vectorD(self.n) self.y0 = 1.0 self.dydx = 1.0 self.d2ydx2 = 0.5 for i in range(0, self.n, 1): self.x[i] = i self.y1[i] = self.dydx*self.x[i] + self.y0 self.y2[i] = self.d2ydx2*self.x[i]*self.x[i] + self.dydx*self.x[i] + self.y0 self.xtest = 4.5 self.y1test = self.dydx*self.xtest + self.y0 self.y2test = self.d2ydx2*self.xtest*self.xtest + self.dydx*self.xtest + self.y0
def testNaturalSplineDerivative1(self): """Test fitting a natural spline to a smooth function and finding its derivative""" sp = afwMath.TautSpline(self.x, self.ySin) y2 = afwMath.vectorD() sp.derivative(self.x2, y2) for x, y in zip(self.x2, y2): self.assertTrue(abs(y - self.smooth(x, True)) < 1.5e-3)
def makeRatingVector(kernelCellSet, spatialKernel, spatialBg): imstats = diffimLib.ImageStatisticsF() #sdqaVector = sdqa.SdqaRatingSet() width, height = spatialKernel.getDimensions() kImage = afwImage.ImageD(width, height) # find the kernel sum and its Rms by looking at the 4 corners of the image kSums = afwMath.vectorD() for x in (0, width): for y in (0, height): kSum = spatialKernel.computeImage(kImage, False, x, y) kSums.push_back(kSum) #afwStat = afwMath.makeStatistics(kSums, afwMath.MEAN | afwMath.STDEV) #kSumRating = sdqa.SdqaRating("lsst.ip.diffim.kernel_sum", # afwStat.getValue(afwMath.MEAN), # afwStat.getValue(afwMath.STDEV), # scope) #sdqaVector.append(kSumRating) nGood = 0 nBad = 0 for cell in kernelCellSet.getCellList(): for cand in cell.begin(False): # False = include bad candidates cand = diffimLib.cast_KernelCandidateF(cand) if cand.getStatus() == afwMath.SpatialCellCandidate.GOOD: # this has been used for processing nGood += 1 xCand = int(cand.getXCenter()) yCand = int(cand.getYCenter()) # evaluate kernel and background at position of candidate kSum = spatialKernel.computeImage(kImage, False, xCand, yCand) kernel = afwMath.FixedKernel(kImage) background = spatialBg(xCand, yCand) diffIm = cand.getDifferenceImage(kernel, background) imstats.apply(diffIm) #candMean = imstats.getMean() #candRms = imstats.getRms() #candRating = sdqa.SdqaRating("lsst.ip.diffim.residuals_%d_%d" % (xCand, yCand), # candMean, candRms, scope) #sdqaVector.append(candRating) elif cand.getStatus() == afwMath.SpatialCellCandidate.BAD: nBad += 1 #nGoodRating = sdqa.SdqaRating("lsst.ip.diffim.nCandGood", nGood, 0, scope) #sdqaVector.append(nGoodRating) #nBadRating = sdqa.SdqaRating("lsst.ip.diffim.nCandBad", nBad, 0, scope) #sdqaVector.append(nBadRating) nKernelTerms = spatialKernel.getNSpatialParameters() if nKernelTerms == 0: # order 0 nKernelTerms = 1
def testTautSpline2(self): """Test fitting a taut spline to a non-differentiable function""" gamma = 2.5 sp = afwMath.TautSpline(self.x, self.yND, gamma) y2 = afwMath.vectorD() sp.interpolate(self.x2, y2) for x, y in zip(self.x2, y2): self.assertAlmostEqual(y, self.noDerivative(x))
def testNaturalSpline1(self): """Test fitting a natural spline to a smooth function""" gamma = 0 sp = afwMath.TautSpline(self.x, self.ySin, gamma) y2 = afwMath.vectorD() sp.interpolate(self.x2, y2) for x, y in zip(self.x2, y2): self.assertAlmostEqual(y, self.smooth(x), 1) # fails at 2 places!
def testTautSpline1(self): """Test fitting a taut spline to a smooth function""" gamma = 2.5 sp = afwMath.TautSpline(self.x, self.ySin, gamma) y2 = afwMath.vectorD() sp.interpolate(self.x2, y2) for x, y in zip(self.x2, y2): self.assertAlmostEqual(y, self.smooth(x), 4)
def testNaturalSpline2(self): """Test fitting a natural spline to a non-differentiable function (we basically fail)""" gamma = 0 sp = afwMath.TautSpline(self.x, self.yND, gamma) y2 = afwMath.vectorD() sp.interpolate(self.x2, y2) for x, y in zip(self.x2, y2): self.assertAlmostEqual(y, self.noDerivative(x), 1) # fails at 2 places!
def testRootFinding(self): """Test finding roots of Spline = value""" gamma = 2.5 sp = afwMath.TautSpline(self.x, self.yND, gamma) for value in (0.1, 0.5): self.assertEqual(sp.roots(value, self.x[0], self.x[-1])[0], 1 - value) if False: y = afwMath.vectorD() sp.interpolate(self.x, y) for x, y in zip(self.x, y): print x, y # # Solve sin(x) = 0.5 # sp = afwMath.TautSpline(self.x, self.ySin) roots = [math.degrees(x) for x in sp.roots(0.5, self.x[0], self.x[-1])] self.assertAlmostEqual(roots[0], 30, 5) self.assertAlmostEqual(roots[1], 150, 5)
def testRootFinding(self): """Test finding roots of Spline = value""" gamma = 2.5 sp = afwMath.TautSpline(self.x, self.yND, gamma) for value in (0.1, 0.5): self.assertEqual(sp.roots(value, self.x[0], self.x[-1])[0], 1 - value) if False: y = afwMath.vectorD() sp.interpolate(self.x, y) for x, y in zip(self.x, y): print(x, y) # # Solve sin(x) = 0.5 # sp = afwMath.TautSpline(self.x, self.ySin) roots = [math.degrees(x) for x in sp.roots(0.5, self.x[0], self.x[-1])] self.assertAlmostEqual(roots[0], 30, 5) self.assertAlmostEqual(roots[1], 150, 5)