def fits_mean_file(files, outfile, overwrite=True, bitpix=32): output = fits.HDUList() output.append(fits.PrimaryHDU()) all_amps = allAmps() for amp in all_amps: images = [afwImage.ImageF(item, dm_hdu(amp)) for item in files] if lsst.afw.__version__.startswith('12.0'): images = afwImage.vectorImageF(images) mean_image = afwMath.statisticsStack(images, afwMath.MEAN) if bitpix < 0: output.append(fits.ImageHDU(data=mean_image.getArray())) else: output.append( fits.CompImageHDU(data=mean_image.getArray(), compression_type='RICE_1')) with warnings.catch_warnings(): warnings.filterwarnings('ignore', category=UserWarning, append=True) warnings.filterwarnings('ignore', category=AstropyWarning, append=True) warnings.filterwarnings('ignore', category=AstropyUserWarning, append=True) with fits.open(files[0]) as template: output[0].header.update(template[0].header) output[0].header['FILENAME'] = os.path.basename(outfile) for amp in all_amps: output[amp].header.update(template[amp].header) set_bitpix(output[amp], bitpix) for i in (-3, -2, -1): output.append(template[i]) fitsWriteto(output, outfile, overwrite=overwrite)
def stack(ims, statistic=afwMath.MEDIAN): """Stacks a list of images based on a statistic.""" images = [] for image in ims: images.append(image) if lsst.afw.__version__.startswith('12.0'): images = afwImage.vectorImageF(images) summary = afwMath.statisticsStack(images, statistic) return summary
def main(): nImg = 10 nX, nY = 64, 64 imgList = afwImage.vectorImageF() for iImg in range(nImg): imgList.push_back(afwImage.ImageF(afwGeom.Extent2I(nX, nY), iImg)) imgStack = afwMath.statisticsStack(imgList, afwMath.MEAN) print imgStack.get(nX / 2, nY / 2)
def main(): nImg = 10 nX, nY = 64, 64 imgList = afwImage.vectorImageF() for iImg in range(nImg): imgList.push_back(afwImage.ImageF(afwGeom.Extent2I(nX, nY), iImg)) imgStack = afwMath.statisticsStack(imgList, afwMath.MEAN) print imgStack.get(nX/2, nY/2)
def testRequestMoreThanOneStat(self): """ Make sure we throw an exception if someone requests more than one type of statistics. """ sctrl = afwMath.StatisticsControl() imgList = afwImage.vectorImageF() for val in self.values: img = afwImage.ImageF(afwGeom.Extent2I(self.nX, self.nY), val) imgList.push_back(img) def tst(): imgStackBad = afwMath.statisticsStack(imgList, afwMath.MEAN | afwMath.MEANCLIP, sctrl) self.assertRaises(pexEx.InvalidParameterError, tst)
def testMean(self): """ Test the statisticsStack() function for a MEAN""" knownMean = 0.0 imgList = afwImage.vectorImageF() for iImg in range(self.nImg): imgList.push_back(afwImage.ImageF(afwGeom.Extent2I(self.nX, self.nY), iImg)) knownMean += iImg imgStack = afwMath.statisticsStack(imgList, afwMath.MEAN) knownMean /= self.nImg self.assertEqual(imgStack.get(self.nX/2, self.nY/2), knownMean)
def testRequestMoreThanOneStat(self): """ Make sure we throw an exception if someone requests more than one type of statistics. """ sctrl = afwMath.StatisticsControl() imgList = afwImage.vectorImageF() for val in self.values: img = afwImage.ImageF(afwGeom.Extent2I(self.nX, self.nY), val) imgList.push_back(img) def tst(): imgStackBad = afwMath.statisticsStack(imgList, afwMath.MEAN | afwMath.MEANCLIP, sctrl) utilsTests.assertRaisesLsstCpp(self, pexEx.InvalidParameterException, tst)
def testStatistics(self): """ Test the statisticsStack() function """ imgList = afwImage.vectorImageF() for val in self.values: imgList.push_back(afwImage.ImageF(afwGeom.Extent2I(self.nX, self.nY), val)) imgStack = afwMath.statisticsStack(imgList, afwMath.MEAN) mean = reduce(lambda x, y: x+y, self.values)/float(len(self.values)) self.assertAlmostEqual(imgStack.get(self.nX//2, self.nY//2), mean) imgStack = afwMath.statisticsStack(imgList, afwMath.MEDIAN) median = sorted(self.values)[len(self.values)//2] self.assertEqual(imgStack.get(self.nX//2, self.nY//2), median)
def testStatistics(self): """ Test the statisticsStack() function """ imgList = afwImage.vectorImageF() for val in self.values: imgList.push_back(afwImage.ImageF(afwGeom.Extent2I(self.nX, self.nY), val)) imgStack = afwMath.statisticsStack(imgList, afwMath.MEAN) mean = reduce(lambda x, y: x+y, self.values)/float(len(self.values)) self.assertAlmostEqual(imgStack.get(self.nX/2, self.nY/2), mean) imgStack = afwMath.statisticsStack(imgList, afwMath.MEDIAN) median = sorted(self.values)[len(self.values)//2] self.assertEqual(imgStack.get(self.nX/2, self.nY/2), median)
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 testMean(self): """ Test the statisticsStack() function for a MEAN""" knownMean = 0.0 imgList = afwImage.vectorImageF() for iImg in range(self.nImg): imgList.push_back(afwImage.ImageF(afwGeom.Extent2I(self.nX, self.nY), iImg)) knownMean += iImg imgStack = afwMath.statisticsStack(imgList, afwMath.MEAN) knownMean /= self.nImg self.assertEqual(imgStack.get(self.nX/2, self.nY/2), knownMean) # Test in-place stacking afwMath.statisticsStack(imgStack, imgList, afwMath.MEAN) self.assertEqual(imgStack.get(self.nX/2, self.nY/2), knownMean)
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 fits_median(files, hdu=2, fix=True): """Compute the median image from a set of image FITS files.""" ims = [afwImage.ImageF(f, hdu) for f in files] exptimes = [Metadata(f).get('EXPTIME') for f in files] if min(exptimes) != max(exptimes): raise RuntimeError("Error: unequal exposure times") if fix: medians = np.array([median(im) for im in ims]) med = sum(medians) / len(medians) errs = medians - med for im, err in zip(ims, errs): im -= err if lsst.afw.__version__.startswith('12.0'): ims = afwImage.vectorImageF(ims) median_image = afwMath.statisticsStack(ims, afwMath.MEDIAN) return median_image
def stack(self, sensor_id, infiles, mask_files, gains, binsize=1, bias_frame=None, exps=['0.27', '0.54', '1.60', '2.40']): for exp in exps: all_amps = imutils.allAmps(infiles[0]) #ptc_stats = dict([amp, ([],[])) for amp in all_amps]) exposure = [] bitpix = None overscan = makeAmplifierGeometry(infiles[0]).serial_overscan mean_stack = fits.open(infiles[0]) var_stack = fits.open(infiles[0]) sum_stack = fits.open(infiles[0]) median_stack = fits.open(infiles[0]) for amp in imutils.allAmps(infiles[0]): print 'on amp number', amp images = afwImage.vectorImageF() for idx, infile in enumerate(infiles): print infile ccd = MaskedCCD(infile, mask_files=(), bias_frame=bias_frame) image = ccd.unbiased_and_trimmed_image(amp) if idx == 0: fratio_im = afwImage.MaskedImageF(image) fratio_im = fratio_im.getImage().getArray() image_array = image.getImage().getArray() fratio = np.mean( fratio_im[50:fratio_im.shape[0] - 50, 50:fratio_im.shape[1] - 50]) / np.mean( image_array[50:image_array.shape[0] - 50, 50:image_array.shape[1] - 50]) image = afwImage.MaskedImageF(image).getImage() image *= fratio images.push_back(image) meanstack_image = afwMath.statisticsStack( images, afwMath.MEAN) varstack_image = afwMath.statisticsStack( images, afwMath.VARIANCE) sumstack_image = afwMath.statisticsStack( images, afwMath.SUM) medianstack_image = afwMath.statisticsStack( images, afwMath.MEDIAN) mean_stack[amp].data = meanstack_image.getArray() var_stack[amp].data = varstack_image.getArray() sum_stack[amp].data = sumstack_image.getArray() median_stack[amp].data = medianstack_image.getArray() if bitpix is not None: imutils.set_bitpix(output[amp], bitpix) fitsWriteto(mean_stack, 'mean_image_' + exp + '.fits', clobber=True) fitsWriteto(var_stack, 'var_image_' + exp + '.fits', clobber=True) fitsWriteto(sum_stack, 'sum_image_' + exp + '.fits', clobber=True) fitsWriteto(median_stack, 'median_image_' + exp + '.fits', clobber=True)