def testBackgroundTestImages(self): imginfolist = [] #imginfolist.append( ["v1_i1_g_m400_s20_f.fits", 400.05551471441612] ) # cooked to known value #imginfolist.append( ["v1_i1_g_m400_s20_f.fits", 400.00295902395123] ) # cooked to known value #imginfolist.append( ["v1_i1_g_m400_s20_f.fits", 400.08468385712251] ) # cooked to known value #imginfolist.append( ["v1_i1_g_m400_s20_f.fits", 400.00305806663295] ) # cooked to known value #imginfolist.append( ["v1_i1_g_m400_s20_f.fits", 400.0035102188698] ) # cooked to known value imginfolist.append( ["v1_i1_g_m400_s20_f.fits", 399.9912966583894] ) # cooked to known value #imgfiles.append("v1_i1_g_m400_s20_u16.fits") #imgfiles.append("v1_i2_g_m400_s20_f.fits" #imgfiles.append("v1_i2_g_m400_s20_u16.fits") #imgfiles.append("v2_i1_p_m9_f.fits") #imgfiles.append("v2_i1_p_m9_u16.fits") #imgfiles.append("v2_i2_p_m9_f.fits") #imgfiles.append("v2_i2_p_m9_u16.fits") for imginfo in imginfolist: imgfile, centerValue = imginfo imgPath = os.path.join(AfwdataDir, "Statistics", imgfile) # get the image and header dimg = afwImage.DecoratedImageF(imgPath) img = dimg.getImage() fitsHdr = dimg.getMetadata() # the FITS header # get the True values of the mean and stdev reqMean = fitsHdr.getAsDouble("MEANREQ") reqStdev = fitsHdr.getAsDouble("SIGREQ") naxis1 = img.getWidth() naxis2 = img.getHeight() # create a background control object bctrl = afwMath.BackgroundControl(afwMath.Interpolate.AKIMA_SPLINE) bctrl.setNxSample(5) bctrl.setNySample(5) # run the background constructor and call the getPixel() and getImage() functions. backobj = afwMath.makeBackground(img, bctrl) pixPerSubimage = img.getWidth()*img.getHeight()/(bctrl.getNxSample()*bctrl.getNySample()) stdevInterp = reqStdev/math.sqrt(pixPerSubimage) # test getPixel() testval = afwMath.cast_BackgroundMI(backobj).getPixel(naxis1//2, naxis2//2) self.assertAlmostEqual(testval/centerValue, 1, places=7) self.assertTrue( abs(testval - reqMean) < 2*stdevInterp ) # test getImage() by checking the center pixel bimg = backobj.getImageF() testImgval = bimg.get(naxis1//2, naxis2//2) self.assertTrue( abs(testImgval - reqMean) < 2*stdevInterp )
def testSampleImageStats(self): """ Compare our results to known values in test data """ imgfiles = [] imgfiles.append("v1_i1_g_m400_s20_f.fits") imgfiles.append("v1_i1_g_m400_s20_u16.fits") imgfiles.append("v1_i2_g_m400_s20_f.fits") imgfiles.append("v1_i2_g_m400_s20_u16.fits") imgfiles.append("v2_i1_p_m9_f.fits") imgfiles.append("v2_i1_p_m9_u16.fits") imgfiles.append("v2_i2_p_m9_f.fits") imgfiles.append("v2_i2_p_m9_u16.fits") afwdataDir = os.getenv("AFWDATA_DIR") if not afwdataDir: print >> sys.stderr, "Skipping tests as afwdata is not setup" return for imgfile in imgfiles: imgPath = os.path.join(afwdataDir, "Statistics", imgfile) # get the image and header dimg = afwImage.DecoratedImageF(imgPath) fitsHdr = dimg.getMetadata() # get the true values of the mean and stdev trueMean = fitsHdr.getAsDouble("MEANCOMP") trueStdev = fitsHdr.getAsDouble("SIGCOMP") # measure the mean and stdev with the Statistics class img = dimg.getImage() statobj = afwMath.makeStatistics(img, afwMath.MEAN | afwMath.STDEV) mean = statobj.getValue(afwMath.MEAN) stdev = statobj.getValue(afwMath.STDEV) # print trueMean, mean, trueStdev, stdev self.assertAlmostEqual(mean, trueMean, 8) self.assertAlmostEqual(stdev, trueStdev, 8)