|
#
# LSST Data Management System
# Copyright 2008-2017 LSST Corporation.
#
# This product includes software developed by the
# LSST Project (http://www.lsst.org/).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the LSST License Statement and
# the GNU General Public License along with this program. If not,
# see <http://www.lsstcorp.org/LegalNotices/>.
#
# -*- python -*-
"""
Tests for Stack
Run with:
./Stacker.py
or
python
>>> import Stacker; Stacker.run()
"""
import unittest
from functools import reduce
import numpy as np
import lsst.geom
import lsst.afw.image as afwImage
import lsst.afw.math as afwMath
import lsst.utils.tests
import lsst.pex.exceptions as pexEx
import lsst.afw.display.ds9 as ds9
display = False
######################################
# main body of code
######################################
class StackTestCase(lsst.utils.tests.TestCase):
def setUp(self):
np.random.seed(1)
self.nImg = 10
self.nX, self.nY = 64, 64
self.values = [1.0, 2.0, 2.0, 3.0, 8.0]
def testMean(self):
""" Test the statisticsStack() function for a MEAN"""
knownMean = 0.0
imgList = []
for iImg in range(self.nImg):
imgList.append(afwImage.ImageF(
lsst.geom.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 testStatistics(self):
""" Test the statisticsStack() function """
imgList = []
for val in self.values:
imgList.append(afwImage.ImageF(
lsst.geom.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 testWeightedStack(self):
""" Test statisticsStack() function when weighting by a variance plane"""
sctrl = afwMath.StatisticsControl()
sctrl.setWeighted(True)
mimgList = []
for val in self.values:
mimg = afwImage.MaskedImageF(lsst.geom.Extent2I(self.nX, self.nY))
mimg.set(val, 0x0, val)
mimgList.append(mimg)
mimgStack = afwMath.statisticsStack(mimgList, afwMath.MEAN, sctrl)
wvalues = [1.0/q for q in self.values]
wmean = float(len(self.values)) / reduce(lambda x, y: x + y, wvalues)
self.assertAlmostEqual(
mimgStack.getImage().get(self.nX//2, self.nY//2),
wmean)
# Test in-place stacking
afwMath.statisticsStack(mimgStack, mimgList, afwMath.MEAN, sctrl)
self.assertAlmostEqual(
mimgStack.getImage().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 = []
weights = []
for val in self.values:
img = afwImage.ImageF(lsst.geom.Extent2I(self.nX, self.nY), val)
imgList.append(img)
weights.append(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 testRequestMoreThanOneStat(self):
""" Make sure we throw an exception if someone requests more than one type of statistics. """
sctrl = afwMath.StatisticsControl()
imgList = []
for val in self.values:
img = afwImage.ImageF(lsst.geom.Extent2I(self.nX, self.nY), val)
imgList.append(img)
def tst():
afwMath.statisticsStack(
imgList,
afwMath.Property(afwMath.MEAN | afwMath.MEANCLIP),
sctrl)
self.assertRaises(pexEx.InvalidParameterError, tst)
def testReturnInputs(self):
""" Make sure that a single file put into the stacker is returned unscathed"""
imgList = []
img = afwImage.MaskedImageF(lsst.geom.Extent2I(10, 20))
for y in range(img.getHeight()):
simg = img.Factory(
img,
lsst.geom.Box2I(lsst.geom.Point2I(0, y),
lsst.geom.Extent2I(img.getWidth(), 1)),
afwImage.LOCAL)
simg.set(y)
imgList.append(img)
imgStack = afwMath.statisticsStack(imgList, afwMath.MEAN)
171 ↛ 172line 171 didn't jump to line 172, because the condition on line 171 was never true if display:
ds9.mtv(img, frame=1, title="input")
ds9.mtv(imgStack, frame=2, title="stack")
self.assertEqual(img.get(0, 0)[0], imgStack.get(0, 0)[0])
def testStackBadPixels(self):
"""Check that we properly ignore masked pixels, and set noGoodPixelsMask where there are
no good pixels"""
mimgVec = []
DETECTED = afwImage.Mask.getPlaneBitMask("DETECTED")
EDGE = afwImage.Mask.getPlaneBitMask("EDGE")
INTRP = afwImage.Mask.getPlaneBitMask("INTRP")
SAT = afwImage.Mask.getPlaneBitMask("SAT")
sctrl = afwMath.StatisticsControl()
sctrl.setNanSafe(False)
sctrl.setAndMask(INTRP | SAT)
sctrl.setNoGoodPixelsMask(EDGE)
# set these pixels to EDGE
edgeBBox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0),
lsst.geom.Extent2I(20, 20))
width, height = 512, 512
dim = lsst.geom.Extent2I(width, height)
val, maskVal = 10, DETECTED
for i in range(4):
mimg = afwImage.MaskedImageF(dim)
mimg.set(val, maskVal, 1)
#
# Set part of the image to NaN (with the INTRP bit set)
#
llc = lsst.geom.Point2I(width//2*(i//2), height//2*(i % 2))
bbox = lsst.geom.Box2I(llc, dim//2)
smimg = mimg.Factory(mimg, bbox, afwImage.LOCAL)
del smimg
#
# And the bottom corner to SAT
#
smask = mimg.getMask().Factory(mimg.getMask(), edgeBBox, afwImage.LOCAL)
smask |= SAT
del smask
mimgVec.append(mimg)
218 ↛ 219line 218 didn't jump to line 219, because the condition on line 218 was never true if display > 1:
ds9.mtv(mimg, frame=i, title=str(i))
mimgStack = afwMath.statisticsStack(mimgVec, afwMath.MEAN, sctrl)
223 ↛ 224line 223 didn't jump to line 224, because the condition on line 223 was never true if display:
i += 1
ds9.mtv(mimgStack, frame=i, title="Stack")
i += 1
ds9.mtv(mimgStack.getVariance(), frame=i, title="var(Stack)")
#
# Check the output, ignoring EDGE pixels
#
sctrl = afwMath.StatisticsControl()
sctrl.setAndMask(afwImage.Mask.getPlaneBitMask("EDGE"))
stats = afwMath.makeStatistics(
mimgStack, afwMath.MIN | afwMath.MAX, sctrl)
self.assertEqual(stats.getValue(afwMath.MIN), val)
self.assertEqual(stats.getValue(afwMath.MAX), val)
#
# We have to clear EDGE in the known bad corner to check the mask
#
smask = mimgStack.getMask().Factory(mimgStack.getMask(), edgeBBox, afwImage.LOCAL)
self.assertEqual(
smask.get(edgeBBox.getMinX(), edgeBBox.getMinY()), EDGE)
smask &= ~EDGE
del smask
self.assertEqual(
afwMath.makeStatistics(mimgStack.getMask(),
afwMath.SUM, sctrl).getValue(),
maskVal)
def testTicket1412(self):
"""Ticket 1412: ignored mask bits are propegated to output stack."""
mimg1 = afwImage.MaskedImageF(lsst.geom.Extent2I(1, 1))
mimg1.set(0, 0, (1, 0x4, 1)) # set 0100
mimg2 = afwImage.MaskedImageF(lsst.geom.Extent2I(1, 1))
mimg2.set(0, 0, (2, 0x3, 1)) # set 0010 and 0001
imgList = []
imgList.append(mimg1)
imgList.append(mimg2)
sctrl = afwMath.StatisticsControl()
sctrl.setAndMask(0x1) # andmask only 0001
# try first with no sctrl (no andmask set), should see 0x0111 for all
# output mask pixels
imgStack = afwMath.statisticsStack(imgList, afwMath.MEAN)
self.assertEqual(imgStack.get(0, 0)[1], 0x7)
# now try with sctrl (andmask = 0x0001), should see 0x0100 for all
# output mask pixels
imgStack = afwMath.statisticsStack(imgList, afwMath.MEAN, sctrl)
self.assertEqual(imgStack.get(0, 0)[1], 0x4)
def test2145(self):
"""The how-to-repeat from #2145"""
Size = 5
statsCtrl = afwMath.StatisticsControl()
statsCtrl.setCalcErrorFromInputVariance(True)
maskedImageList = []
weightList = []
for i in range(3):
mi = afwImage.MaskedImageF(Size, Size)
imArr, maskArr, varArr = mi.getArrays()
imArr[:] = np.random.normal(10, 0.1, (Size, Size))
varArr[:] = np.random.normal(10, 0.1, (Size, Size))
maskedImageList.append(mi)
weightList.append(1.0)
stack = afwMath.statisticsStack(
maskedImageList, afwMath.MEAN, statsCtrl, weightList)
if False:
print("image=", stack.getImage().getArray())
print("variance=", stack.getVariance().getArray())
self.assertNotEqual(np.sum(stack.getVariance().getArray()), 0.0)
def testRejectedMaskPropagation(self):
"""Test that we can propagate mask bits from rejected pixels, when the amount
of rejection crosses a threshold."""
rejectedBit = 1 # use this bit to determine whether to reject a pixel
propagatedBit = 2 # propagate this bit if a pixel with it set is rejected
statsCtrl = afwMath.StatisticsControl()
statsCtrl.setMaskPropagationThreshold(propagatedBit, 0.3)
statsCtrl.setAndMask(1 << rejectedBit)
statsCtrl.setWeighted(True)
maskedImageList = []
# start with 4 images with no mask bits set
partialSum = np.zeros((1, 4), dtype=np.float32)
finalImage = np.array([12.0, 12.0, 12.0, 12.0], dtype=np.float32)
for i in range(4):
mi = afwImage.MaskedImageF(4, 1)
imArr, maskArr, varArr = mi.getArrays()
imArr[:, :] = np.ones((1, 4), dtype=np.float32)
maskedImageList.append(mi)
partialSum += imArr
# add one more image with all permutations of the first two bits set in
# different pixels
mi = afwImage.MaskedImageF(4, 1)
imArr, maskArr, varArr = mi.getArrays()
imArr[0, :] = finalImage
maskArr[0, 1] |= (1 << rejectedBit)
maskArr[0, 2] |= (1 << propagatedBit)
maskArr[0, 3] |= (1 << rejectedBit)
maskArr[0, 3] |= (1 << propagatedBit)
maskedImageList.append(mi)
# these will always be rejected
finalImage[1] = 0.0
finalImage[3] = 0.0
# Uniform weights: we should only see pixel 2 set with propagatedBit, because it's not rejected;
# pixel 3 is rejected, but its weight (0.2) below the propagation
# threshold (0.3)
stack1 = afwMath.statisticsStack(maskedImageList, afwMath.MEAN, statsCtrl, [
1.0, 1.0, 1.0, 1.0, 1.0])
self.assertEqual(stack1.get(0, 0)[1], 0x0)
self.assertEqual(stack1.get(1, 0)[1], 0x0)
self.assertEqual(stack1.get(2, 0)[1], 1 << propagatedBit)
self.assertEqual(stack1.get(3, 0)[1], 0x0)
self.assertFloatsAlmostEqual(stack1.getImage().getArray(),
(partialSum + finalImage) / np.array([5.0, 4.0, 5.0, 4.0]), rtol=1E-7)
# Give the masked image more weight: we should see pixel 2 and pixel 3 set with propagatedBit,
# pixel 2 because it's not rejected, and pixel 3 because the weight of the rejection (0.3333)
# is above the threshold (0.3)
# Note that rejectedBit is never propagated, because we didn't include it in statsCtrl (of course,
# normally the bits we'd propagate and the bits we'd reject would be
# the same)
stack2 = afwMath.statisticsStack(maskedImageList, afwMath.MEAN, statsCtrl, [
1.0, 1.0, 1.0, 1.0, 2.0])
self.assertEqual(stack2.get(0, 0)[1], 0x0)
self.assertEqual(stack2.get(1, 0)[1], 0x0)
self.assertEqual(stack2.get(2, 0)[1], 1 << propagatedBit)
self.assertEqual(stack2.get(3, 0)[1], 1 << propagatedBit)
self.assertFloatsAlmostEqual(stack2.getImage().getArray(),
(partialSum + 2*finalImage) / np.array([6.0, 4.0, 6.0, 4.0]), rtol=1E-7)
def testClipped(self):
"""Test that we set mask bits when pixels are clipped"""
box = lsst.geom.Box2I(lsst.geom.Point2I(12345, 67890), lsst.geom.Extent2I(3, 3))
num = 10
maskVal = 0xAD
value = 0.0
images = [afwImage.MaskedImageF(box) for _ in range(num)]
statsCtrl = afwMath.StatisticsControl()
statsCtrl.setAndMask(maskVal)
clipped = 1 << afwImage.Mask().addMaskPlane("CLIPPED")
# No clipping: check that vanilla is working
for img in images:
img.getImage().set(value)
img.getMask().set(0)
stack = afwMath.statisticsStack(images, afwMath.MEANCLIP, clipped=clipped)
self.assertFloatsAlmostEqual(stack.getImage().getArray(), 0.0, atol=0.0)
self.assertFloatsAlmostEqual(stack.getMask().getArray(), 0, atol=0.0) # Not floats, but that's OK
# Clip a pixel; the CLIPPED bit should be set
images[0].getImage().set(1, 1, value + 1.0)
stack = afwMath.statisticsStack(images, afwMath.MEANCLIP, clipped=clipped)
self.assertFloatsAlmostEqual(stack.getImage().getArray(), 0.0, atol=0.0)
self.assertEqual(stack.getMask().get(1, 1), clipped)
# Mask a pixel; the CLIPPED bit should be set
images[0].getMask().set(1, 1, maskVal)
stack = afwMath.statisticsStack(images, afwMath.MEAN, statsCtrl, clipped=clipped)
self.assertFloatsAlmostEqual(stack.getImage().getArray(), 0.0, atol=0.0)
self.assertEqual(stack.getMask().get(1, 1), clipped)
# Excuse that mask; the CLIPPED bit should not be set
stack = afwMath.statisticsStack(images, afwMath.MEAN, statsCtrl, clipped=clipped, excuse=maskVal)
self.assertFloatsAlmostEqual(stack.getImage().getArray(), 0.0, atol=0.0)
self.assertEqual(stack.getMask().get(1, 1), 0)
# Map that mask value to a different one.
rejected = 1 << afwImage.Mask().addMaskPlane("REJECTED")
maskMap = [(maskVal, rejected)]
images[0].getMask().set(1, 1, 0) # only want to clip, not mask, this one
images[1].getMask().set(1, 2, maskVal) # only want to mask, not clip, this one
stack = afwMath.statisticsStack(images, afwMath.MEANCLIP, statsCtrl, wvector=[], clipped=clipped,
maskMap=maskMap)
self.assertFloatsAlmostEqual(stack.getImage().getArray(), 0.0, atol=0.0)
self.assertEqual(stack.getMask().get(1, 1), clipped)
self.assertEqual(stack.getMask().get(1, 2), rejected)
#################################################################
# Test suite boiler plate
#################################################################
class TestMemory(lsst.utils.tests.MemoryTestCase):
pass
def setup_module(module):
lsst.utils.tests.init()
422 ↛ 423line 422 didn't jump to line 423, because the condition on line 422 was never trueif __name__ == "__main__":
lsst.utils.tests.init()
unittest.main()
|