Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

afw review for DM-2929: re-enabling tests #32

Merged
merged 8 commits into from
Jul 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/image/Wcs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -595,15 +595,16 @@ void Wcs::flipImage(int flipLR, int flipTB, afwGeom::Extent2I dimensions) const

//If naxis != 2, I'm not sure if any of what follows is correct
assert(naxis == 2);
//Origin is at (1,1). Adjust to avoid off by one.
if (flipLR) {
_wcsInfo->cd[0] = -_wcsInfo->cd[0];
_wcsInfo->cd[2] = -_wcsInfo->cd[2];
_wcsInfo->crpix[0] = -_wcsInfo->crpix[0] + dimensions.getX();
_wcsInfo->crpix[0] = -_wcsInfo->crpix[0] + dimensions.getX() + 1;
}
if (flipTB) {
_wcsInfo->cd[1] = -_wcsInfo->cd[1];
_wcsInfo->cd[3] = -_wcsInfo->cd[3];
_wcsInfo->crpix[1] = -_wcsInfo->crpix[1]+dimensions.getY();
_wcsInfo->crpix[1] = -_wcsInfo->crpix[1]+dimensions.getY() + 1;
}

// tells libwcs to invalidate cached data, since transformation has been modified
Expand All @@ -628,6 +629,9 @@ void Wcs::rotateImageBy90(int nQuarter, afwGeom::Extent2I dimensions) const {
double d = _wcsInfo->cd[3];
double crpx = _wcsInfo->crpix[0];
double crpy = _wcsInfo->crpix[1];
//Origin is at (1,1). Adjust to avoid off by one.
//E.g. CRPIX one pixel off the UR of the grid should go to
//(0,0) for nQuarter=2
switch (nQuarter%4) {
case 0:
break;
Expand All @@ -636,24 +640,24 @@ void Wcs::rotateImageBy90(int nQuarter, afwGeom::Extent2I dimensions) const {
_wcsInfo->cd[1] = a;
_wcsInfo->cd[2] = -d;
_wcsInfo->cd[3] = c;
_wcsInfo->crpix[0] = -crpy + dimensions.getY();
_wcsInfo->crpix[0] = -crpy + dimensions.getY() + 1;
_wcsInfo->crpix[1] = crpx;
break;
case 2:
_wcsInfo->cd[0] = -a;
_wcsInfo->cd[1] = -b;
_wcsInfo->cd[2] = -c;
_wcsInfo->cd[3] = -d;
_wcsInfo->crpix[0] = -crpx + dimensions.getX();
_wcsInfo->crpix[1] = -crpy + dimensions.getY();
_wcsInfo->crpix[0] = -crpx + dimensions.getX() + 1;
_wcsInfo->crpix[1] = -crpy + dimensions.getY() + 1;
break;
case 3:
_wcsInfo->cd[0] = b;
_wcsInfo->cd[1] = -a;
_wcsInfo->cd[2] = d;
_wcsInfo->cd[3] = -c;
_wcsInfo->crpix[0] = crpy;
_wcsInfo->crpix[1] = -crpx + dimensions.getX();
_wcsInfo->crpix[1] = -crpx + dimensions.getX() + 1;
break;
}

Expand Down
33 changes: 29 additions & 4 deletions tests/testSchema.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,47 @@

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


class SchemaTestCase(unittest.TestCase):

def testSchema(self):
def testKey(name, key):
col = schema.find(name)
self.assertEqual(col.key, key)
self.assertEqual(col.field.getName(), name)

schema = lsst.afw.table.Schema();
ab_k = lsst.afw.table.CoordKey.addFields(schema, "a_b", "parent coord")
abp_k = lsst.afw.table.Point2DKey.addFields(schema, "a_b_p", "point", "pixels")
abi_k = schema.addField("a_b_i", type=int, doc="int")
acf_k = schema.addField("a_c_f", type=numpy.float32, doc="float")
egd_k = schema.addField("e_g_d", type=lsst.afw.geom.Angle, doc="angle")
self.assertEqual(schema.getNames(), ("a_b_i", "a_c_f", "e_g_d"))

#Basic test for all native key types.
for name, key in (("a_b_i", abi_k), ("a_c_f", acf_k), ("e_g_d", egd_k)):
testKey(name, key)

#Extra tests for special types
self.assertEqual(ab_k.getRa(), schema["a_b_ra"].asKey());
abpx_si = schema.find("a_b_p_x")
self.assertEqual(abp_k.getX(), abpx_si.key);
self.assertEqual(abpx_si.field.getName(), "a_b_p_x")
self.assertEqual(abpx_si.field.getDoc(), "point")
self.assertEqual(abp_k.getX(), schema["a_b_p_x"].asKey());
self.assertEqual(schema.getNames(), ('a_b_dec', 'a_b_i', 'a_b_p_x', 'a_b_p_y', 'a_b_ra', 'a_c_f',
'e_g_d'))
self.assertEqual(schema.getNames(True), ("a", "e"))
self.assertEqual(schema["a"].getNames(), ("b_i", "c_f"))
self.assertEqual(schema["a"].getNames(), ('b_dec', 'b_i', 'b_p_x', 'b_p_y', 'b_ra', 'c_f'))
self.assertEqual(schema["a"].getNames(True), ("b", "c"))
schema2 = lsst.afw.table.Schema(schema)
self.assertEqual(schema, schema2)
schema2.addField("q", type=float, doc="another double")
self.assertNotEqual(schema, schema2)
schema3 = lsst.afw.table.Schema()
schema3.addField("ra", type="Angle", doc="coord_ra")
schema3.addField("dec", type="Angle", doc="coord_dec")
schema3.addField("x", type="D", doc="position_x")
schema3.addField("y", type="D", doc="position_y")
schema3.addField("i", type="I", doc="int")
schema3.addField("f", type="F", doc="float")
schema3.addField("d", type="Angle", doc="angle")
Expand Down Expand Up @@ -132,7 +157,7 @@ def testComparison(self):


class SchemaMapperTestCase(unittest.TestCase):

def testJoin(self):
inputs = [lsst.afw.table.Schema(), lsst.afw.table.Schema(), lsst.afw.table.Schema()]
inputs = lsst.afw.table.SchemaVector(inputs)
Expand Down Expand Up @@ -229,7 +254,7 @@ def testDoReplace(self):
mapper3.addMapping(ka, "c", True)
self.assertEqual(mapper3.getMapping(ka), kc)

def testJoin(self):
def testJoin2(self):
s1 = lsst.afw.table.Schema()
self.assertEqual(s1.join("a", "b"), "a_b")
self.assertEqual(s1.join("a", "b", "c"), "a_b_c")
Expand Down
82 changes: 40 additions & 42 deletions tests/wcs1.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
dataDir = lsst.utils.getPackageDir("afwdata")
if not dataDir:
raise RuntimeError("Must set up afwdata to run these tests")
InputImagePath = os.path.join(dataDir, "871034p_1_MI")
InputImagePath = os.path.join(dataDir, "data", "871034p_1_MI")
InputSmallImagePath = os.path.join(dataDir, "data", "small_img.fits")
InputCorruptMaskedImageName = "small_MI_corrupt"
currDir = os.path.abspath(os.path.dirname(__file__))
Expand All @@ -55,7 +55,7 @@
class WcsTestCase(unittest.TestCase):
def testCD_PC(self):
"""Test that we can read a FITS file with both CD and PC keys (like early Suprimecam files)"""

md = dafBase.PropertyList()
for k, v in (
("EQUINOX", 2000.0),
Expand Down Expand Up @@ -133,32 +133,34 @@ def tearDown(self):
def makeWcs(self):
crval = afwCoord.Coord(afwGeom.Point2D(1.606631, 5.090329))
crpix = afwGeom.Point2D(2036., 2000.)
wcs = afwImage.makeWcs(crval, crpix, 5.399452e-5, -1.30770e-5, 1.30770e-5, 5.399452e-5)
return afwImage.makeWcs(crval, crpix, 5.399452e-5, -1.30770e-5, 1.30770e-5, 5.399452e-5)

def testRotation(self):
# Origin for LSST pixels is (0,0). Need to subtract one when rotating to avoid off by one.
# E.g. UR (507, 1999) goes to (0,0) for nRot = 2
q1 = {0:afwGeom.Point2D(100., 1600.),
1:afwGeom.Point2D(self.size.getY() - 1600., 100.),
2:afwGeom.Point2D(self.size.getX() - 100., self.size.getY() - 1600.),
3:afwGeom.Point2D(1600., self.size.getX() - 100.)}
1:afwGeom.Point2D(self.size.getY() - 1600. - 1, 100.),
2:afwGeom.Point2D(self.size.getX() - 100. - 1, self.size.getY() - 1600. - 1),
3:afwGeom.Point2D(1600., self.size.getX() - 100. - 1)}
wcs = self.makeWcs()
pos0 = self.wcs.pixelToSky(q1[0])
pos0 = wcs.pixelToSky(q1[0])
for rot in (1,2,3):
wcs = self.makeWcs()
wcs.rotateImageBy90(rot, self.size)
self.assertEqual(pos0, self.wcs.pixelToSky(q1[rot]))
self.assertEqual(pos0, wcs.pixelToSky(q1[rot]))

def testFlip(self):
q1 = {'noFlip': afwGeom.Point2D(300., 900.),
'flipLR': afwGeom.Point2D(self.size.getX()-300., 900.),
'flipTB': afwGeom.Point2D(300., self.size.getY()-900.)}
'flipLR': afwGeom.Point2D(self.size.getX()-300.-1, 900.),
'flipTB': afwGeom.Point2D(300., self.size.getY()-900.-1)}
wcs = self.makeWcs()
pos0 = self.wcs.pixelToSky(q1['noFlip'])
pos0 = wcs.pixelToSky(q1['noFlip'])
wcs = self.makeWcs()
wcs.flipImage(True, False, self.size)
self.assertEqual(pos0, self.wcs.pixelToSky(q1['flipLR']))
self.assertEqual(pos0, wcs.pixelToSky(q1['flipLR']))
wcs = self.makeWcs()
wcs.flipImage(False, True, self.size)
self.assertEqual(pos0, self.wcs.pixelToSky(q1['flipTB']))
self.assertEqual(pos0, wcs.pixelToSky(q1['flipTB']))

class WCSTestCaseSDSS(unittest.TestCase):
"""A test case for WCS using a small (SDSS) image with a slightly weird WCS"""
Expand All @@ -169,7 +171,7 @@ def setUp(self):
self.wcs = afwImage.makeWcs(self.im.getMetadata())

if False:
ds9.mtv(im, wcs=self.wcs)
ds9.mtv(self.im, wcs=self.wcs)

def tearDown(self):
del self.wcs
Expand All @@ -179,12 +181,12 @@ def testCrpix(self):
metadata = self.im.getMetadata()
crpix0 = metadata.getAsDouble("CRPIX1")
crpix1 = metadata.getAsDouble("CRPIX2")

lsstCrpix = self.wcs.getPixelOrigin()

self.assertEqual(lsstCrpix[0], crpix0-1)
self.assertEqual(lsstCrpix[1], crpix1-1)

def testXyToRaDecArguments(self):
"""Check that conversion of xy to ra dec (and back again) works"""
xy = afwGeom.Point2D(110, 123)
Expand All @@ -195,10 +197,10 @@ def testXyToRaDecArguments(self):
self.assertAlmostEqual(xy.getY(), xy2.getY())

raDec = afwCoord.makeCoord(afwCoord.ICRS, 245.167400 * afwGeom.degrees, +19.1976583 * afwGeom.degrees)

xy = self.wcs.skyToPixel(raDec)
raDec2 = self.wcs.pixelToSky(xy)

self.assertAlmostEqual(raDec[0].asDegrees(), raDec2[0].asDegrees())
self.assertAlmostEqual(raDec[1].asDegrees(), raDec2[1].asDegrees())

Expand Down Expand Up @@ -279,8 +281,6 @@ def testStripKeywords(self):
def testAffineTransform(self):
a = self.wcs.getLinearTransform()
l = self.wcs.getCDMatrix()
#print print a[a.XX], a[a.XY], a[a.YX], a[a.YY]
#print a, l

def testXY0(self):
"""Test that XY0 values are handled correctly when building an exposure and also when
Expand Down Expand Up @@ -329,7 +329,7 @@ def makeWcs(crPixPos, crValDeg, projection):

if verbose:
print "useExposure=%s; unp pixPos=%s" % (useExposure, unpPixPos)

for i in range(2):
self.assertAlmostEqual(unpPixPos[i], 1009.5)

Expand All @@ -339,7 +339,7 @@ class WCSTestCaseCFHT(unittest.TestCase):
"""A test case for WCS"""

def setUp(self):
path = InputImagePath + "_img.fits"
path = InputImagePath + ".fits"
self.metadata = afwImage.readMetadata(path)
self.wcs = afwImage.makeWcs(self.metadata)

Expand All @@ -365,9 +365,12 @@ def testPlateScale(self):

side = 1e-3
icrs = afwCoord.ICRS
degrees = afwCoord.DEGREES
sky10 = afwCoord.makeCoord(icrs, sky00 + afwGeom.Extent2D(side/cosdec, 0), degrees)
sky01 = afwCoord.makeCoord(icrs, sky00 + afwGeom.Extent2D(0,side), degrees)
sky10 = afwCoord.makeCoord(
icrs, sky00.getPosition(afwGeom.degrees) + afwGeom.Extent2D(side/cosdec, 0), afwGeom.degrees
)
sky01 = afwCoord.makeCoord(
icrs, sky00.getPosition(afwGeom.degrees) + afwGeom.Extent2D(0,side), afwGeom.degrees
)
p10 = self.wcs.skyToPixel(sky10) - p00
p01 = self.wcs.skyToPixel(sky01) - p00

Expand All @@ -386,7 +389,7 @@ def testPlateScale(self):
def testReadWcs(self):
"""Test reading a Wcs directly from a fits file"""

meta = afwImage.readMetadata(InputImagePath + "_img.fits")
meta = afwImage.readMetadata(InputImagePath + ".fits")
wcs = afwImage.makeWcs(meta)

sky0 = wcs.pixelToSky(0.0, 0.0).getPosition()
Expand Down Expand Up @@ -419,25 +422,19 @@ def testCD(self):
self.assertAlmostEqual(cd[1,1], self.metadata.getAsDouble("CD2_2"))

def testConstructor(self):
copy = afwImage.Wcs(self.wcs.getSkyOrigin(), self.wcs.getPixelOrigin(),
copy = afwImage.Wcs(self.wcs.getSkyOrigin().getPosition(afwGeom.degrees), self.wcs.getPixelOrigin(),
self.wcs.getCDMatrix())

def testAffineTransform(self):
a = self.wcs.getLinearTransform()
l = self.wcs.getCDMatrix()
#print print a[a.XX], a[a.XY], a[a.YX], a[a.YY]

sky00g = afwGeom.Point2D(10, 10)
sky00i = afwGeom.Point2D(sky00g.getX(), sky00g.getY())
sky00c = afwCoord.makeCoord(afwCoord.ICRS, sky00i, afwCoord.DEGREES)
sky00c = afwCoord.makeCoord(afwCoord.ICRS, sky00g, afwGeom.degrees)
a = self.wcs.linearizeSkyToPixel(sky00c)
pix00i = self.wcs.skyToPixel(sky00c)
pix00g = afwGeom.Point2D(pix00i.getX(), pix00i.getY())
sky00gApprox = a(pix00g);
self.assertAlmostEqual(sky00g.getX(), sky00gApprox.getX())
self.assertAlmostEqual(sky00g.getY(), sky00gApprox.getY())
self.assertAlmostEqual(self.wcs.pixArea(sky00i), abs(a[a.XX]* a[a.YY] - a[a.XY]*a[a.YX]))
a.invert()
pix00g = self.wcs.skyToPixel(sky00c)
pix00gApprox = a(sky00g);
self.assertAlmostEqual(pix00g.getX(), pix00gApprox.getX())
self.assertAlmostEqual(pix00g.getY(), pix00gApprox.getY())
b = a.invert()
self.assertAlmostEqual(self.wcs.pixArea(sky00g), abs(b[b.XX]* b[b.YY] - b[b.XY]*b[b.YX]))

class TestWcsCompare(unittest.TestCase):

Expand Down Expand Up @@ -483,7 +480,8 @@ def suite():
suites += unittest.makeSuite(WcsTestCase)
suites += unittest.makeSuite(WCSTestCaseSDSS)
suites += unittest.makeSuite(TestWcsCompare)
# suites += unittest.makeSuite(WCSTestCaseCFHT)
suites += unittest.makeSuite(WCSTestCaseCFHT)
suites += unittest.makeSuite(WCSRotateFlip)
suites += unittest.makeSuite(utilsTests.MemoryTestCase)

return unittest.TestSuite(suites)
Expand Down