def TimepixToExposure(filename, xmin, xmax, ymin, ymax): data = np.loadtxt(filename) my_array = np.zeros((256,256), dtype = np.int32) if data.shape == (0,): my_image = makeImageFromArray(my_array) elif data.shape == (3,): x = data[0] y = data[1] t = data[2] if x >= xmin and x <= xmax and y >= ymin and y <= ymax: my_array[y,x] = t my_image = makeImageFromArray(my_array) else: x = data[:, 0] y = data[:, 1] t = data[:, 2] for pointnum in range(len(x)): if x[pointnum] >= xmin and x[pointnum] <= xmax and y[pointnum] >= ymin and y[pointnum] <= ymax: my_array[y[pointnum],x[pointnum]] = t[pointnum] my_image = makeImageFromArray(my_array) return my_image
def TimepixToExposure(filename, xmin, xmax, ymin, ymax): import numpy as np from lsst.afw.image import makeImageFromArray data = np.loadtxt(filename) my_array = np.zeros((256, 256), dtype=np.int32) if data.shape == (0, ): my_image = makeImageFromArray(my_array) elif data.shape == (3, ): x = data[0] y = data[1] t = data[2] if x >= xmin and x <= xmax and y >= ymin and y <= ymax: my_array[y, x] = t my_image = makeImageFromArray(my_array) else: x = data[:, 0] y = data[:, 1] t = data[:, 2] for pointnum in range(len(x)): if x[pointnum] >= xmin and x[pointnum] <= xmax and y[ pointnum] >= ymin and y[pointnum] <= ymax: my_array[y[pointnum], x[pointnum]] = t[pointnum] my_image = makeImageFromArray(my_array) return my_image
def testTicket873(self): """Demonstrate ticket 873: convolution of a MaskedImage with a spatially varying LinearCombinationKernel of basis kernels with low covariance gives incorrect variance. """ # create spatial model sFunc = afwMath.PolynomialFunction2D(1) # spatial parameters are a list of entries, one per kernel parameter; # each entry is a list of spatial parameters sParams = ( (1.0, -0.5/self.width, -0.5/self.height), (0.0, 1.0/self.width, 0.0/self.height), (0.0, 0.0/self.width, 1.0/self.height), ) # create three kernels with some non-overlapping pixels # (non-zero pixels in one kernel vs. zero pixels in other kernels); # note: the extreme example of this is delta function kernels, but this # is less extreme basisKernelList = [] kImArr = numpy.zeros([5, 5], dtype=float) kImArr[1:4, 1:4] = 0.5 kImArr[2, 2] = 1.0 kImage = afwImage.makeImageFromArray(kImArr) basisKernelList.append(afwMath.FixedKernel(kImage)) kImArr[:, :] = 0.0 kImArr[0:2, 0:2] = 0.125 kImArr[3:5, 3:5] = 0.125 kImage = afwImage.makeImageFromArray(kImArr) basisKernelList.append(afwMath.FixedKernel(kImage)) kImArr[:, :] = 0.0 kImArr[0:2, 3:5] = 0.125 kImArr[3:5, 0:2] = 0.125 kImage = afwImage.makeImageFromArray(kImArr) basisKernelList.append(afwMath.FixedKernel(kImage)) kernel = afwMath.LinearCombinationKernel(basisKernelList, sFunc) kernel.setSpatialParameters(sParams) for maxInterpDist, rtol, methodStr in ( (0, 1.0e-5, "brute force"), (10, 3.0e-3, "interpolation over 10 x 10 pixels"), ): self.runStdTest( kernel, kernelDescr="Spatially varying LinearCombinationKernel of basis " "kernels with low covariance, using %s" % ( methodStr,), maxInterpDist=maxInterpDist, rtol=rtol)
def testTicket873(self): """Demonstrate ticket 873: convolution of a MaskedImage with a spatially varying LinearCombinationKernel of basis kernels with low covariance gives incorrect variance. """ # create spatial model sFunc = afwMath.PolynomialFunction2D(1) # spatial parameters are a list of entries, one per kernel parameter; # each entry is a list of spatial parameters sParams = ( (1.0, -0.5/self.width, -0.5/self.height), (0.0, 1.0/self.width, 0.0/self.height), (0.0, 0.0/self.width, 1.0/self.height), ) # create three kernels with some non-overlapping pixels # (non-zero pixels in one kernel vs. zero pixels in other kernels); # note: the extreme example of this is delta function kernels, but this is less extreme basisKernelList = afwMath.KernelList() kImArr = numpy.zeros([5, 5], dtype=float) kImArr[1:4, 1:4] = 0.5 kImArr[2, 2] = 1.0 kImage = afwImage.makeImageFromArray(kImArr) basisKernelList.append(afwMath.FixedKernel(kImage)) kImArr[:, :] = 0.0 kImArr[0:2, 0:2] = 0.125 kImArr[3:5, 3:5] = 0.125 kImage = afwImage.makeImageFromArray(kImArr) basisKernelList.append(afwMath.FixedKernel(kImage)) kImArr[:, :] = 0.0 kImArr[0:2, 3:5] = 0.125 kImArr[3:5, 0:2] = 0.125 kImage = afwImage.makeImageFromArray(kImArr) basisKernelList.append(afwMath.FixedKernel(kImage)) kernel = afwMath.LinearCombinationKernel(basisKernelList, sFunc) kernel.setSpatialParameters(sParams) for maxInterpDist, rtol, methodStr in ( (0, 1.0e-5, "brute force"), (10, 3.0e-3, "interpolation over 10 x 10 pixels"), ): self.runStdTest( kernel, kernelDescr = \ "Spatially varying LinearCombinationKernel of basis kernels with low covariance, using %s" % (methodStr,), maxInterpDist = maxInterpDist, rtol = rtol)
def XYT_to_image(xyt_array, display=False): import numpy as np from lsst.afw.image import makeImageFromArray if display: import lsst.afw.display.ds9 as ds9 try: ds9.initDS9(False) except ds9.Ds9Error: print() my_array = np.zeros((256, 256), dtype=np.int32) xs = xyt_array[:, 0] ys = xyt_array[:, 1] # for x,y in zip(xs,ys): # my_array[y,x] += 1 n_counts = 0 for x, y in zip(xs, ys): if n_counts >= 1000000: break n_counts += 1 my_array[y, x] += 1 my_image = makeImageFromArray(my_array) if display: ds9.mtv(my_image) return my_image
def addNoise(self, inputExposure): mi = inputExposure.getMaskedImage() (x, y) = mi.getDimensions() noiseArr = numpy.random.poisson(self.config.noiseValue, size=x*y).reshape(y, x) noiseArr = noiseArr.astype(numpy.float32) noiseImage = afwImage.makeImageFromArray(noiseArr) mi += noiseImage
def testRandomMap(self): """Test setCoaddEdgeBits using a random depth map """ imDim = afwGeom.Extent2I(50, 55) coaddMask = afwImage.MaskU(imDim) numpy.random.seed(12345) depthMapArray = numpy.random.randint(0, 3, list((imDim[1], imDim[0]))).astype(numpy.uint16) depthMap = afwImage.makeImageFromArray(depthMapArray) refCoaddMaskArray = coaddMask.getArray() edgeMask = afwImage.MaskU.getPlaneBitMask("NO_DATA") refCoaddMaskArray |= numpy.array(numpy.where(depthMapArray > 0, 0, edgeMask), dtype=refCoaddMaskArray.dtype) coaddUtils.setCoaddEdgeBits(coaddMask, depthMap) coaddMaskArray = coaddMask.getArray() if numpy.any(refCoaddMaskArray != coaddMaskArray): errMsgList = ( "Coadd mask does not match reference:", "computed= %s" % (coaddMaskArray,), "reference= %s" % (refCoaddMaskArray,), ) errMsg = "\n".join(errMsgList) self.fail(errMsg)
def testArrays(self): for cls in (afwImage.ImageU, afwImage.ImageI, afwImage.ImageF, afwImage.ImageD): image1 = cls(lsst.geom.Extent2I(5, 6)) array1 = image1.getArray() self.assertEqual(array1.shape[0], image1.getHeight()) self.assertEqual(array1.shape[1], image1.getWidth()) image2 = cls(array1, False) self.assertEqual(array1.shape[0], image2.getHeight()) self.assertEqual(array1.shape[1], image2.getWidth()) image3 = afwImage.makeImageFromArray(array1) self.assertEqual(array1.shape[0], image2.getHeight()) self.assertEqual(array1.shape[1], image2.getWidth()) self.assertEqual(type(image3), cls) array2 = image1.array np.testing.assert_array_equal(array1, array2) array1[:, :] = np.random.uniform(low=0, high=10, size=array1.shape) for j in range(image1.getHeight()): for i in range(image1.getWidth()): self.assertEqual(image1[i, j, afwImage.LOCAL], array1[j, i]) self.assertEqual(image2[i, j, afwImage.LOCAL], array1[j, i]) array3 = np.random.uniform(low=0, high=10, size=array1.shape).astype(array1.dtype) image1.array[:] = array3 np.testing.assert_array_equal(array1, array3) image1.array[2:4, 3:] = 10 np.testing.assert_array_equal(array1[2:4, 3:], 10) array4 = image1.array.copy() array4 += 5 image1.array += 5 np.testing.assert_array_equal(image1.array, array4)
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 = list(zip(*sorted(zip(self._xList, self._scaleList)))) xvec = np.array(x, dtype=float) zvec = np.array(z, dtype=float) height = bbox.getHeight() width = bbox.getWidth() x0, y0 = bbox.getMin() interp = afwMath.makeInterpolate(xvec, zvec, self.interpStyle) interpValArr = np.zeros(width, dtype=np.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 = np.meshgrid(interpValArr, range(0, height))[0].astype(np.float32) image = afwImage.makeImageFromArray(interpGrid) image.setXY0(x0, y0) return image
def makeTestImage(xsize=200, ysize=100, nCR=15): randArr = numpy.random.poisson(1000., xsize * ysize) randArr = numpy.array(randArr.reshape(ysize, xsize), dtype=numpy.float32) # force to ImageF factory = measAlg.GaussianPsfFactory() factory.addWing = False psf = factory.apply(4) # FWHM in pixels img = afwImage.makeImageFromArray(randArr) var = afwImage.ImageF(img, True) # copy constructor mask = afwImage.Mask(xsize, ysize) xind = numpy.random.randint(0, xsize, nCR) yind = numpy.random.randint(0, ysize, nCR) # set some CRs for xi, yi in zip(xind, yind): xi, yi = int(xi), int(yi) img.set(xi, yi, 1e6) mi = afwImage.makeMaskedImage(img, mask, var) exp = afwImage.makeExposure(mi) exp.setPsf(psf) return exp
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 testSVLinearCombinationKernel(self): """Test a spatially varying LinearCombinationKernel """ kWidth = 3 kHeight = 2 # create image arrays for the basis kernels basisImArrList = [] imArr = np.zeros((kWidth, kHeight), dtype=float) imArr += 0.1 imArr[kWidth//2, :] = 0.9 basisImArrList.append(imArr) imArr = np.zeros((kWidth, kHeight), dtype=float) imArr += 0.2 imArr[:, kHeight//2] = 0.8 basisImArrList.append(imArr) # create a list of basis kernels from the images kVec = [] for basisImArr in basisImArrList: basisImage = afwImage.makeImageFromArray( basisImArr.transpose().copy()) kernel = afwMath.FixedKernel(basisImage) kVec.append(kernel) # create spatially varying linear combination kernel spFunc = afwMath.PolynomialFunction2D(1) # spatial parameters are a list of entries, one per kernel parameter; # each entry is a list of spatial parameters sParams = ( (0.0, 1.0, 0.0), (0.0, 0.0, 1.0), ) k = afwMath.LinearCombinationKernel(kVec, spFunc) k.setSpatialParameters(sParams) with lsst.utils.tests.getTempFilePath(".fits") as filename: k.writeFits(filename) k2 = afwMath.LinearCombinationKernel.readFits(filename) self.kernelCheck(k, k2) kImage = afwImage.ImageD(lsst.geom.Extent2I(kWidth, kHeight)) for colPos, rowPos, coeff0, coeff1 in [ (0.0, 0.0, 0.0, 0.0), (1.0, 0.0, 1.0, 0.0), (0.0, 1.0, 0.0, 1.0), (1.0, 1.0, 1.0, 1.0), (0.5, 0.5, 0.5, 0.5), ]: k2.computeImage(kImage, False, colPos, rowPos) kImArr = kImage.getArray().transpose() refKImArr = (basisImArrList[0] * coeff0) + \ (basisImArrList[1] * coeff1) if not np.allclose(kImArr, refKImArr): self.fail("%s = %s != %s at colPos=%s, rowPos=%s" % (k2.__class__.__name__, kImArr, refKImArr, colPos, rowPos))
def testSVLinearCombinationKernel(self): """Test a spatially varying LinearCombinationKernel """ kWidth = 3 kHeight = 2 # create image arrays for the basis kernels basisImArrList = [] imArr = np.zeros((kWidth, kHeight), dtype=float) imArr += 0.1 imArr[kWidth // 2, :] = 0.9 basisImArrList.append(imArr) imArr = np.zeros((kWidth, kHeight), dtype=float) imArr += 0.2 imArr[:, kHeight // 2] = 0.8 basisImArrList.append(imArr) # create a list of basis kernels from the images kVec = [] for basisImArr in basisImArrList: basisImage = afwImage.makeImageFromArray( basisImArr.transpose().copy()) kernel = afwMath.FixedKernel(basisImage) kVec.append(kernel) # create spatially varying linear combination kernel spFunc = afwMath.PolynomialFunction2D(1) # spatial parameters are a list of entries, one per kernel parameter; # each entry is a list of spatial parameters sParams = ( (0.0, 1.0, 0.0), (0.0, 0.0, 1.0), ) k = afwMath.LinearCombinationKernel(kVec, spFunc) k.setSpatialParameters(sParams) with lsst.utils.tests.getTempFilePath(".fits") as filename: k.writeFits(filename) k2 = afwMath.LinearCombinationKernel.readFits(filename) self.kernelCheck(k, k2) kImage = afwImage.ImageD(lsst.geom.Extent2I(kWidth, kHeight)) for colPos, rowPos, coeff0, coeff1 in [ (0.0, 0.0, 0.0, 0.0), (1.0, 0.0, 1.0, 0.0), (0.0, 1.0, 0.0, 1.0), (1.0, 1.0, 1.0, 1.0), (0.5, 0.5, 0.5, 0.5), ]: k2.computeImage(kImage, False, colPos, rowPos) kImArr = kImage.getArray().transpose() refKImArr = (basisImArrList[0] * coeff0) + \ (basisImArrList[1] * coeff1) if not np.allclose(kImArr, refKImArr): self.fail(f"{k2.__class__.__name__} = {kImArr} != {refKImArr} " f"at colPos={colPos}, rowPos={rowPos}")
def ViewIntensityArrayInDs9(intensity_array, savefile=None): import lsst.afw.display.ds9 as ds9 ds9.mtv( makeImageFromArray(100 * intensity_array / float(intensity_array.max()))) if savefile is not None: arg = 'saveimage jpeg ' + str(savefile) + ' 100' ds9.ds9Cmd(arg)
def MakeCompositeImage_PImMS(path, winow_xmin=0, winow_xmax=999, winow_ymin=0, winow_ymax=999, offset_us=0, maxfiles=None, t_min=-9999, t_max=9999, return_raw_array=False): from lsst.afw.image import makeImageFromArray import string, os import numpy as np import pylab as pl my_array = np.zeros((72, 72), dtype=np.int32) files = [] for filename in os.listdir(path): files.append(path + filename) for filenum, filename in enumerate(files): if filenum % 500 == 0: print("Compiled %s files" % filenum) xs, ys, ts = GetXYTarray_SingleFile(filename, winow_xmin, winow_xmax, winow_ymin, winow_ymax) # if len(xs) > 5000: continue # skip glitch files for i in range(len(xs)): x = xs[i] y = ys[i] t = ts[i] if x >= winow_xmin and x <= winow_xmax and y >= winow_ymin and y <= winow_ymax: if t >= t_min and t <= t_max: my_array[x, y] += 1 if maxfiles != None and filenum >= maxfiles: if return_raw_array: return my_array my_image = makeImageFromArray(my_array) return my_image my_image = makeImageFromArray(my_array) if return_raw_array: return my_array return my_image
def XYI_array_to_exposure(xs, ys, i_s): from lsst.afw.image import makeImageFromArray import numpy as np my_array = np.zeros((256, 256), dtype=np.int32) for pointnum in range(len(xs)): my_array[xs[pointnum], ys[pointnum]] = i_s[pointnum] my_image = makeImageFromArray(my_array) return my_image
def TimepixToExposure_binary(filename, xmin, xmax, ymin, ymax, mask_pixels=np.ones((1), dtype=np.float64)): from lsst.afw.image import makeImageFromArray data = np.loadtxt(filename) my_array = np.zeros((256, 256), dtype=np.int32) if data.shape == (0, ): my_image = makeImageFromArray(my_array) elif data.shape == (3, ): x = data[0] y = data[1] t = data[2] if x >= xmin and x <= xmax and y >= ymin and y <= ymax: my_array[y, x] = 1 my_image = makeImageFromArray(my_array * mask_pixels.transpose()) return_npix = ( my_array * mask_pixels.transpose()).sum() #apply the mask, *then* sum! else: x = data[:, 0] y = data[:, 1] t = data[:, 2] for pointnum in range(len(x)): if x[pointnum] >= xmin and x[pointnum] <= xmax and y[ pointnum] >= ymin and y[pointnum] <= ymax: my_array[y[pointnum], x[pointnum]] = 1 my_image = makeImageFromArray(my_array * mask_pixels.transpose()) return_npix = ( my_array * mask_pixels.transpose()).sum() #apply the mask, *then* sum! return my_image, return_npix
def MakeCompositeImage_Medipix(path, winow_xmin=0, winow_xmax=999, winow_ymin=0, winow_ymax=999, offset_us=0, maxfiles=None): from lsst.afw.image import makeImageFromArray import string, os import numpy as np import pylab as pl my_array = np.zeros((256, 256), dtype=np.int32) files = [] for filename in os.listdir(path): files.append(path + filename) num = 0 for filename in files: data = pl.loadtxt(filename, usecols=(0, 1, 2)) num += 1 if (num % 10 == 0): print('loaded %s files' % num) for i in range(len(data)): x = int(data[i, 0]) y = int(data[i, 1]) intensity = int(data[i, 2]) if x >= winow_xmin and x <= winow_xmax and y >= winow_ymin and y <= winow_ymax: my_array[x, y] += intensity if maxfiles != None and num == maxfiles: my_image = makeImageFromArray(my_array) return my_image my_image = makeImageFromArray(my_array) return my_image
def testRandomMap(self): """Test setCoaddEdgeBits using a random depth map """ imDim = afwGeom.Extent2I(50, 55) coaddMask = afwImage.Mask(imDim) np.random.seed(12345) depthMapArray = np.random.randint(0, 3, list((imDim[1], imDim[0]))).astype(np.uint16) depthMap = afwImage.makeImageFromArray(depthMapArray) refCoaddMask = afwImage.Mask(imDim) refCoaddMaskArray = refCoaddMask.getArray() edgeMask = afwImage.Mask.getPlaneBitMask("NO_DATA") refCoaddMaskArray |= np.array(np.where(depthMapArray > 0, 0, edgeMask), dtype=refCoaddMaskArray.dtype) coaddUtils.setCoaddEdgeBits(coaddMask, depthMap) self.assertMasksEqual(coaddMask, refCoaddMask)
def testArrays(self): for cls in (afwImage.ImageU, afwImage.ImageI, afwImage.ImageF, afwImage.ImageD): image1 = cls(afwGeom.Extent2I(5,6)) array1 = image1.getArray() self.assertEqual(array1.shape[0], image1.getHeight()) self.assertEqual(array1.shape[1], image1.getWidth()) image2 = cls(array1, False) self.assertEqual(array1.shape[0], image2.getHeight()) self.assertEqual(array1.shape[1], image2.getWidth()) image3 = afwImage.makeImageFromArray(array1) self.assertEqual(array1.shape[0], image2.getHeight()) self.assertEqual(array1.shape[1], image2.getWidth()) self.assertEqual(type(image3), cls) array1[:,:] = numpy.random.uniform(low=0, high=10, size=array1.shape) for j in range(image1.getHeight()): for i in range(image1.getWidth()): self.assertEqual(image1.get(i, j), array1[j, i]) self.assertEqual(image2.get(i, j), array1[j, i])
def testRandomMap(self): """Test setCoaddEdgeBits using a random depth map """ imDim = geom.Extent2I(50, 55) coaddMask = afwImage.Mask(imDim) np.random.seed(12345) depthMapArray = np.random.randint(0, 3, list( (imDim[1], imDim[0]))).astype(np.uint16) depthMap = afwImage.makeImageFromArray(depthMapArray) refCoaddMask = afwImage.Mask(imDim) refCoaddMaskArray = refCoaddMask.getArray() edgeMask = afwImage.Mask.getPlaneBitMask("NO_DATA") refCoaddMaskArray |= np.array(np.where(depthMapArray > 0, 0, edgeMask), dtype=refCoaddMaskArray.dtype) coaddUtils.setCoaddEdgeBits(coaddMask, depthMap) self.assertMasksEqual(coaddMask, refCoaddMask)
def TimepixToExposure(filename): from lsst.afw.image import makeImageFromArray import numpy as np data = np.loadtxt(filename) x = data[:, 0] y = data[:, 1] t = data[:, 2] my_array = np.zeros((256,256), dtype = np.int32) for pointnum in range(len(x)): my_array[x[pointnum],y[pointnum]] = t[pointnum] my_image = makeImageFromArray(my_array) return my_image
def makeTestImage(xsize=200, ysize=100, nCR=15): randArr = numpy.random.poisson(1000., xsize*ysize) randArr = numpy.array(randArr.reshape(ysize, xsize), dtype=numpy.float32) # force to ImageF factory = measAlg.GaussianPsfFactory() factory.addWing = False psf = factory.apply(4) # FWHM in pixels img = afwImage.makeImageFromArray(randArr) var = afwImage.ImageF(img, True) #copy constructor mask = afwImage.MaskU(xsize, ysize) xind = numpy.random.randint(0, xsize, nCR) yind = numpy.random.randint(0, ysize, nCR) # set some CRs for xi, yi in zip(xind, yind): xi, yi = int(xi), int(yi) img.set(xi, yi, 1e6) mi = afwImage.makeMaskedImage(img, mask, var) exp = afwImage.makeExposure(mi) exp.setPsf(psf) return exp
def testSVLinearCombinationKernel(self): """Test a spatially varying LinearCombinationKernel """ kWidth = 3 kHeight = 2 pol = pexPolicy.Policy() additionalData = dafBase.PropertySet() loc = dafPersist.LogicalLocation( os.path.join(testPath, "data", "kernel6.boost")) persistence = dafPersist.Persistence.getPersistence(pol) # create image arrays for the basis kernels basisImArrList = [] imArr = np.zeros((kWidth, kHeight), dtype=float) imArr += 0.1 imArr[kWidth // 2, :] = 0.9 basisImArrList.append(imArr) imArr = np.zeros((kWidth, kHeight), dtype=float) imArr += 0.2 imArr[:, kHeight // 2] = 0.8 basisImArrList.append(imArr) # create a list of basis kernels from the images kVec = [] for basisImArr in basisImArrList: basisImage = afwImage.makeImageFromArray( basisImArr.transpose().copy()) kernel = afwMath.FixedKernel(basisImage) kVec.append(kernel) # create spatially varying linear combination kernel spFunc = afwMath.PolynomialFunction2D(1) # spatial parameters are a list of entries, one per kernel parameter; # each entry is a list of spatial parameters sParams = ( (0.0, 1.0, 0.0), (0.0, 0.0, 1.0), ) k = afwMath.LinearCombinationKernel(kVec, spFunc) k.setSpatialParameters(sParams) storageList = dafPersist.StorageList() storage = persistence.getPersistStorage("XmlStorage", loc) storageList.append(storage) persistence.persist(k, storageList, additionalData) storageList2 = dafPersist.StorageList() storage2 = persistence.getRetrieveStorage("XmlStorage", loc) storageList2.append(storage2) k2 = persistence.unsafeRetrieve("LinearCombinationKernel", storageList2, additionalData) self.kernelCheck(k, k2) kImage = afwImage.ImageD(afwGeom.Extent2I(kWidth, kHeight)) for colPos, rowPos, coeff0, coeff1 in [ (0.0, 0.0, 0.0, 0.0), (1.0, 0.0, 1.0, 0.0), (0.0, 1.0, 0.0, 1.0), (1.0, 1.0, 1.0, 1.0), (0.5, 0.5, 0.5, 0.5), ]: k2.computeImage(kImage, False, colPos, rowPos) kImArr = kImage.getArray().transpose() refKImArr = (basisImArrList[0] * coeff0) + \ (basisImArrList[1] * coeff1) if not np.allclose(kImArr, refKImArr): self.fail( "%s = %s != %s at colPos=%s, rowPos=%s" % (k2.__class__.__name__, kImArr, refKImArr, colPos, rowPos))
def testSVLinearCombinationKernelFixed(self): """Test a spatially varying LinearCombinationKernel """ kWidth = 3 kHeight = 2 # create image arrays for the basis kernels basisImArrList = [] imArr = numpy.zeros((kWidth, kHeight), dtype=float) imArr += 0.1 imArr[kWidth//2, :] = 0.9 basisImArrList.append(imArr) imArr = numpy.zeros((kWidth, kHeight), dtype=float) imArr += 0.2 imArr[:, kHeight//2] = 0.8 basisImArrList.append(imArr) # create a list of basis kernels from the images basisKernelList = afwMath.KernelList() for basisImArr in basisImArrList: basisImage = afwImage.makeImageFromArray(basisImArr.transpose().copy()) kernel = afwMath.FixedKernel(basisImage) basisKernelList.append(kernel) # create spatially varying linear combination kernel spFunc = afwMath.PolynomialFunction2D(1) # spatial parameters are a list of entries, one per kernel parameter; # each entry is a list of spatial parameters sParams = ( (0.0, 1.0, 0.0), (0.0, 0.0, 1.0), ) kernel = afwMath.LinearCombinationKernel(basisKernelList, spFunc) self.assert_(not kernel.isDeltaFunctionBasis()) self.basicTests(kernel, 2, 3) kernel.setSpatialParameters(sParams) kImage = afwImage.ImageD(afwGeom.Extent2I(kWidth, kHeight)) for colPos, rowPos, coeff0, coeff1 in [ (0.0, 0.0, 0.0, 0.0), (1.0, 0.0, 1.0, 0.0), (0.0, 1.0, 0.0, 1.0), (1.0, 1.0, 1.0, 1.0), (0.5, 0.5, 0.5, 0.5), ]: kernel.computeImage(kImage, False, colPos, rowPos) kImArr = kImage.getArray().transpose() refKImArr = (basisImArrList[0] * coeff0) + (basisImArrList[1] * coeff1) if not numpy.allclose(kImArr, refKImArr): self.fail("%s = %s != %s at colPos=%s, rowPos=%s" % \ (kernel.__class__.__name__, kImArr, refKImArr, colPos, rowPos)) sParams = ( (0.1, 1.0, 0.0), (0.1, 0.0, 1.0), ) kernel.setSpatialParameters(sParams) kernelClone = kernel.clone() errStr = self.compareKernels(kernel, kernelClone) if errStr: self.fail(errStr) newSParams = ( (0.1, 0.2, 0.5), (0.1, 0.5, 0.2), ) kernel.setSpatialParameters(newSParams) errStr = self.compareKernels(kernel, kernelClone) if not errStr: self.fail("Clone was modified by changing original's spatial parameters")
def testSVLinearCombinationKernel(self): """Test a spatially varying LinearCombinationKernel """ kWidth = 3 kHeight = 2 pol = pexPolicy.Policy() additionalData = dafBase.PropertySet() loc = dafPersist.LogicalLocation("tests/data/kernel6.boost") persistence = dafPersist.Persistence.getPersistence(pol) # create image arrays for the basis kernels basisImArrList = [] imArr = numpy.zeros((kWidth, kHeight), dtype=float) imArr += 0.1 imArr[kWidth//2, :] = 0.9 basisImArrList.append(imArr) imArr = numpy.zeros((kWidth, kHeight), dtype=float) imArr += 0.2 imArr[:, kHeight//2] = 0.8 basisImArrList.append(imArr) # create a list of basis kernels from the images kVec = afwMath.KernelList() for basisImArr in basisImArrList: basisImage = afwImage.makeImageFromArray(basisImArr.transpose().copy()) kernel = afwMath.FixedKernel(basisImage) kVec.append(kernel) # create spatially varying linear combination kernel spFunc = afwMath.PolynomialFunction2D(1) # spatial parameters are a list of entries, one per kernel parameter; # each entry is a list of spatial parameters sParams = ( (0.0, 1.0, 0.0), (0.0, 0.0, 1.0), ) k = afwMath.LinearCombinationKernel(kVec, spFunc) k.setSpatialParameters(sParams) storageList = dafPersist.StorageList() storage = persistence.getPersistStorage("XmlStorage", loc) storageList.append(storage) persistence.persist(k, storageList, additionalData) storageList2 = dafPersist.StorageList() storage2 = persistence.getRetrieveStorage("XmlStorage", loc) storageList2.append(storage2) x = persistence.unsafeRetrieve("LinearCombinationKernel", storageList2, additionalData) k2 = afwMath.LinearCombinationKernel.swigConvert(x) self.kernelCheck(k, k2) kImage = afwImage.ImageD(afwGeom.Extent2I(kWidth, kHeight)) for colPos, rowPos, coeff0, coeff1 in [ (0.0, 0.0, 0.0, 0.0), (1.0, 0.0, 1.0, 0.0), (0.0, 1.0, 0.0, 1.0), (1.0, 1.0, 1.0, 1.0), (0.5, 0.5, 0.5, 0.5), ]: k2.computeImage(kImage, False, colPos, rowPos) kImArr = kImage.getArray().transpose() refKImArr = (basisImArrList[0] * coeff0) + (basisImArrList[1] * coeff1) if not numpy.allclose(kImArr, refKImArr): self.fail("%s = %s != %s at colPos=%s, rowPos=%s" % \ (k2.__class__.__name__, kImArr, refKImArr, colPos, rowPos))
if __name__ == '__main__': try: ds9.initDS9(False) except ds9.Ds9Error: print 'DS9 launch bug error thrown away (probably)' t_total = 0 t0 = time.time() data = np.loadtxt(filename, dtype=np.int32, delimiter=',')#, converters, skiprows, usecols, unpack, ndmin) dt = time.time() - t0 print "Load time = %.2f us" %(dt*1e6) t0 = time.time() t_start = time.time() image = makeImageFromArray(data) dt = time.time() - t0 print "DM image made in = %.2f us" %(dt*1e6) t_total += dt t0 = time.time() sigma = 0.75 # kWidth = (int(sigma * 7 + 0.5) // 2) * 2 + 1 # make sure it is odd kWidth = 5 ####### discrete method gaussFunc = math.GaussianFunction1D(sigma) gaussKernel = math.SeparableKernel(kWidth, kWidth, gaussFunc, gaussFunc) math.convolve(image, image, gaussKernel, math.ConvolutionControl())
if ha is None: mi = ai.MaskedImageF("QE_R%i%i_S%i%i.fits.gz"%(rx, ry, sx, sy)) elif ha == 'A': mi = ai.MaskedImageF("QE_R%i%i_S%i%i_C0.fits.gz"%(rx, ry, sx, sy)) elif ha == 'B': mi = ai.MaskedImageF("QE_R%i%i_S%i%i_C1.fits.gz"%(rx, ry, sx, sy)) else: raise ValueError("passed an invalid value for ha") im = mi.getImage() arr = im.getArray() deadidx = numpy.where(arr == 0.) hotidx = numpy.where(arr > 1.1) arr[deadidx] = 2. arr[hotidx] = 3. im2 = ai.makeImageFromArray(arr) thresh = afwDetection.Threshold(1.1) fs = afwDetection.FootprintSet(im2, thresh) x0 = [] y0 = [] width = [] height = [] for f in fs.getFootprints(): for bbox in afwDetection.footprintToBBoxList(f): bbox = cameraGeom.rotateBBoxBy90(bbox, 3, im.getBBox().getDimensions()) x0.append(bbox.getMinX()) y0.append(bbox.getMinY()) width.append(bbox.getWidth()) height.append(bbox.getHeight()) head = fits.Header()
def ViewMaskInDs9(mask_array): import lsst.afw.display.ds9 as ds9 ds9.mtv(makeImageFromArray(mask_array))
def testSVLinearCombinationKernelFixed(self): """Test a spatially varying LinearCombinationKernel whose bases are FixedKernels""" kWidth = 3 kHeight = 2 # create image arrays for the basis kernels basisImArrList = [] imArr = np.zeros((kWidth, kHeight), dtype=float) imArr += 0.1 imArr[kWidth//2, :] = 0.9 basisImArrList.append(imArr) imArr = np.zeros((kWidth, kHeight), dtype=float) imArr += 0.2 imArr[:, kHeight//2] = 0.8 basisImArrList.append(imArr) # create a list of basis kernels from the images basisKernelList = [] for basisImArr in basisImArrList: basisImage = afwImage.makeImageFromArray( basisImArr.transpose().copy()) kernel = afwMath.FixedKernel(basisImage) basisKernelList.append(kernel) # create spatially varying linear combination kernel spFunc = afwMath.PolynomialFunction2D(1) # spatial parameters are a list of entries, one per kernel parameter; # each entry is a list of spatial parameters sParams = ( (0.0, 1.0, 0.0), (0.0, 0.0, 1.0), ) kernel = afwMath.LinearCombinationKernel(basisKernelList, spFunc) self.assertFalse(kernel.isDeltaFunctionBasis()) self.basicTests(kernel, 2, nSpatialParams=3) kernel.setSpatialParameters(sParams) kImage = afwImage.ImageD(lsst.geom.Extent2I(kWidth, kHeight)) for colPos, rowPos, coeff0, coeff1 in [ (0.0, 0.0, 0.0, 0.0), (1.0, 0.0, 1.0, 0.0), (0.0, 1.0, 0.0, 1.0), (1.0, 1.0, 1.0, 1.0), (0.5, 0.5, 0.5, 0.5), ]: kernel.computeImage(kImage, False, colPos, rowPos) kImArr = kImage.getArray().transpose() refKImArr = (basisImArrList[0] * coeff0) + \ (basisImArrList[1] * coeff1) if not np.allclose(kImArr, refKImArr): self.fail(f"{kernel.__class__.__name__} = {kImArr} != " f"{refKImArr} at colPos={colPos}, rowPos={rowPos}") sParams = ( (0.1, 1.0, 0.0), (0.1, 0.0, 1.0), ) kernel.setSpatialParameters(sParams) kernelClone = kernel.clone() errStr = self.compareKernels(kernel, kernelClone) if errStr: self.fail(errStr) newSParams = ( (0.1, 0.2, 0.5), (0.1, 0.5, 0.2), ) kernel.setSpatialParameters(newSParams) errStr = self.compareKernels(kernel, kernelClone) if not errStr: self.fail( "Clone was modified by changing original's spatial parameters")
if ha is None: mi = ai.MaskedImageF("QE_R%i%i_S%i%i.fits.gz" % (rx, ry, sx, sy)) elif ha == 'A': mi = ai.MaskedImageF("QE_R%i%i_S%i%i_C0.fits.gz" % (rx, ry, sx, sy)) elif ha == 'B': mi = ai.MaskedImageF("QE_R%i%i_S%i%i_C1.fits.gz" % (rx, ry, sx, sy)) else: raise ValueError("passed an invalid value for ha") im = mi.getImage() arr = im.getArray() deadidx = numpy.where(arr == 0.) hotidx = numpy.where(arr > 1.1) arr[deadidx] = 2. arr[hotidx] = 3. im2 = ai.makeImageFromArray(arr) thresh = afwDetection.Threshold(1.1) fs = afwDetection.FootprintSet(im2, thresh) x0 = [] y0 = [] width = [] height = [] for f in fs.getFootprints(): for bbox in afwDetection.footprintToBBoxList(f): bbox = cameraGeom.rotateBBoxBy90(bbox, 3, im.getBBox().getDimensions()) x0.append(bbox.getMinX()) y0.append(bbox.getMinY()) width.append(bbox.getWidth()) height.append(bbox.getHeight()) head = pyfits.Header()