Coverage for tests/test_cameraGeom.py : 12%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
|
# This file is part of afw. # # Developed for the LSST Data Management System. # This product includes software developed by the LSST Project # (https://www.lsst.org). # See the COPYRIGHT file at the top-level directory of this distribution # for details of code ownership. # # 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 GNU General Public License # along with this program. If not, see <https://www.gnu.org/licenses/>.
Camera, Detector, assembleAmplifierImage, assembleAmplifierRawImage, DetectorCollection
"""A test case for camera geometry"""
self.lsstCamWrapper = testUtils.CameraWrapper(isLsstLike=True) self.scCamWrapper = testUtils.CameraWrapper(isLsstLike=False) self.cameraList = (self.lsstCamWrapper, self.scCamWrapper) self.assemblyList = {} self.assemblyList[self.lsstCamWrapper.camera.getName()] =\ [afwImage.ImageU(os.path.join(testPath, 'test_amp.fits.gz')) for i in range(8)] self.assemblyList[self.scCamWrapper.camera.getName()] =\ [afwImage.ImageU(os.path.join(testPath, 'test.fits.gz'), allowUnsafe=True)]
del self.lsstCamWrapper del self.scCamWrapper del self.cameraList del self.assemblyList
for cw in self.cameraList: self.assertIsInstance(cw.camera, Camera) self.assertEqual(cw.nDetectors, len(cw.camera)) self.assertEqual(cw.nDetectors, len(cw.ampInfoDict)) self.assertEqual(sorted(cw.detectorNameList), sorted(cw.camera.getNameIter())) self.assertEqual(sorted(cw.detectorIdList), sorted(cw.camera.getIdIter())) for det in cw.camera: self.assertIsInstance(det, Detector) self.assertEqual( cw.ampInfoDict[det.getName()]['namps'], len(det)) idList = [det.getId() for det in cw.camera] self.assertEqual(idList, sorted(idList))
"""Test CameraSys.__repr__ and CameraSysPrefix.__repr__ """ for sysName in ("FocalPlane", "FieldAngle", "Pixels", "foo"): cameraSys = CameraSys(sysName) predRepr = "CameraSys(%s)" % (sysName) self.assertEqual(repr(cameraSys), predRepr)
cameraSysPrefix = CameraSysPrefix(sysName) predCSPRepr = "CameraSysPrefix(%s)" % (sysName) self.assertEqual(repr(cameraSysPrefix), predCSPRepr) for detectorName in ("Detector 1", "bar"): cameraSys2 = CameraSys(sysName, detectorName) predRepr2 = "CameraSys(%s, %s)" % (sysName, detectorName) self.assertEqual(repr(cameraSys2), predRepr2)
for cw in self.cameraList: camera = cw.camera for name in cw.detectorNameList: self.assertIsInstance(camera[name], Detector) for detId in cw.detectorIdList: self.assertIsInstance(camera[detId], Detector)
"""Test Camera.transform against data computed using SLALIB
These test data come from SLALIB using SLA_PCD with 0.925 and a plate scale of 20 arcsec/mm """ testData = [(-1.84000000, 1.04000000, -331.61689069, 187.43563387), (-1.64000000, 0.12000000, -295.42491556, 21.61645724), (-1.44000000, -0.80000000, -259.39818797, -144.11010443), (-1.24000000, -1.72000000, -223.48275934, -309.99221457), (-1.08000000, 1.36000000, -194.56520533, 245.00803635), (-0.88000000, 0.44000000, -158.44320430, 79.22160215), (-0.68000000, -0.48000000, -122.42389383, -86.41686623), (-0.48000000, -1.40000000, -86.45332534, -252.15553224), (-0.32000000, 1.68000000, -57.64746955, 302.64921514), (-0.12000000, 0.76000000, -21.60360306, 136.82281940), (0.08000000, -0.16000000, 14.40012984, -28.80025968), (0.28000000, -1.08000000, 50.41767773, -194.46818554), (0.48000000, -2.00000000, 86.50298919, -360.42912163), (0.64000000, 1.08000000, 115.25115701, 194.48632746), (0.84000000, 0.16000000, 151.23115189, 28.80593369), (1.04000000, -0.76000000, 187.28751874, -136.86395600), (1.24000000, -1.68000000, 223.47420612, -302.77150507), (1.40000000, 1.40000000, 252.27834478, 252.27834478), (1.60000000, 0.48000000, 288.22644118, 86.46793236), (1.80000000, -0.44000000, 324.31346653, -79.27662515), ]
for cw in self.cameraList: camera = cw.camera for point in testData: fpGivenPos = lsst.geom.Point2D(point[2], point[3]) fieldGivenPos = lsst.geom.Point2D( lsst.geom.degToRad(point[0]), lsst.geom.degToRad(point[1]))
fieldAngleToFocalPlane = camera.getTransform(FIELD_ANGLE, FOCAL_PLANE) fpComputedPos = fieldAngleToFocalPlane.applyForward(fieldGivenPos) self.assertPairsAlmostEqual(fpComputedPos, fpGivenPos)
focalPlaneToFieldAngle = camera.getTransform(FOCAL_PLANE, FIELD_ANGLE) fieldComputedPos = focalPlaneToFieldAngle.applyForward(fpGivenPos) self.assertPairsAlmostEqual(fieldComputedPos, fieldGivenPos)
"""Test Camera.getTransform with detector-based coordinate systems (PIXELS) """ for cw in self.cameraList: numOffUsable = 0 # number of points off one detector but on another camera = cw.camera detNameList = list(camera.getNameIter()) for detName in detNameList: det = camera[detName]
# test transforms using an arbitrary point on the detector posPixels = lsst.geom.Point2D(10, 10) pixSys = det.makeCameraSys(PIXELS) pixelsToFocalPlane = camera.getTransform(pixSys, FOCAL_PLANE) pixelsToFieldAngle = camera.getTransform(pixSys, FIELD_ANGLE) focalPlaneToFieldAngle = camera.getTransform(FOCAL_PLANE, FIELD_ANGLE) posFocalPlane = pixelsToFocalPlane.applyForward(posPixels) posFieldAngle = pixelsToFieldAngle.applyForward(posPixels) posFieldAngle2 = focalPlaneToFieldAngle.applyForward(posFocalPlane) self.assertPairsAlmostEqual(posFieldAngle, posFieldAngle2)
posFieldAngle3 = camera.transform(posPixels, pixSys, FIELD_ANGLE) self.assertPairsAlmostEqual(posFieldAngle, posFieldAngle3)
for intermedPos, intermedSys in ( (posPixels, pixSys), (posFocalPlane, FOCAL_PLANE), (posFieldAngle, FIELD_ANGLE), ): pixelSys = det.makeCameraSys(PIXELS) intermedSysToPixels = camera.getTransform(intermedSys, pixelSys) posPixelsRoundTrip = intermedSysToPixels.applyForward(intermedPos) self.assertPairsAlmostEqual(posPixels, posPixelsRoundTrip)
posPixelsRoundTrip2 = camera.transform(intermedPos, intermedSys, pixelSys) self.assertPairsAlmostEqual(posPixels, posPixelsRoundTrip2)
# Test finding detectors for a point off this detector. # The point off the detector may be on one other detector, # depending if the detector has neighbor on the correct edge. pixOffDet = lsst.geom.Point2D(0, -10) pixCoordSys = det.makeCameraSys(PIXELS) detList = camera.findDetectors(pixOffDet, pixCoordSys) self.assertIn(len(detList), (0, 1)) if len(detList) == 1: numOffUsable += 1
otherDet = detList[0] self.assertNotEqual(otherDet, det) otherCoordPixSys = otherDet.makeCameraSys(PIXELS)
pixelsToOtherPixels = camera.getTransform(pixCoordSys, otherCoordPixSys) otherPixels = pixelsToOtherPixels.applyForward(pixOffDet) with self.assertRaises(AssertionError): self.assertPairsAlmostEqual(otherPixels, pixOffDet)
# convert back otherPixelsToPixels = camera.getTransform(otherCoordPixSys, pixCoordSys) pixOffDetRoundTrip = otherPixelsToPixels.applyForward(otherPixels) self.assertPairsAlmostEqual(pixOffDet, pixOffDetRoundTrip) self.assertEqual(numOffUsable, 5)
for cw in self.cameraList: detCtrFocalPlaneList = [] for det in cw.camera: # This currently assumes there is only one detector at the center # position of any detector. That is not enforced and multiple detectors # at a given FIELD_ANGLE position is supported. Change this if the default # camera changes. detCtrFocalPlane = det.getCenter(FOCAL_PLANE) detCtrFocalPlaneList.append(detCtrFocalPlane) detList = cw.camera.findDetectors(detCtrFocalPlane, FOCAL_PLANE) self.assertEqual(len(detList), 1) self.assertEqual(det.getName(), detList[0].getName()) detList = cw.camera.findDetectorsList(detCtrFocalPlaneList, FOCAL_PLANE) self.assertEqual(len(cw.camera), len(detList)) for dets in detList: self.assertEqual(len(dets), 1)
for cw in self.cameraList: camera = cw.camera bbox = lsst.geom.Box2D() for name in cw.detectorNameList: for corner in camera[name].getCorners(FOCAL_PLANE): bbox.include(corner) self.assertEqual(bbox.getMin(), camera.getFpBBox().getMin()) self.assertEqual(bbox.getMax(), camera.getFpBBox().getMax())
"""Test if we can set/get Linearity parameters""" for cw in self.cameraList: camera = cw.camera for det in camera: for amp in det: self.assertEqual(cw.ampInfoDict[det.getName()]['linInfo'][amp.getName()]['linthresh'], amp.get('linearityThreshold')) self.assertEqual(cw.ampInfoDict[det.getName()]['linInfo'][amp.getName()]['linmax'], amp.get('linearityMaximum')) self.assertEqual(cw.ampInfoDict[det.getName()]['linInfo'][amp.getName()]['linunits'], amp.get('linearityUnits')) self.assertEqual(cw.ampInfoDict[det.getName()]['linInfo'][amp.getName()]['lintype'], amp.getLinearityType()) for c1, c2 in zip(cw.ampInfoDict[det.getName()]['linInfo'][amp.getName()]['lincoeffs'], amp.getLinearityCoeffs()): if np.isfinite(c1) and np.isfinite(c2): self.assertEqual(c1, c2)
ccdNames = ('R:0,0 S:1,0', 'R:0,0 S:0,1') compMap = {True: afwImage.ImageU(os.path.join(testPath, 'test_comp_trimmed.fits.gz'), allowUnsafe=True), False: afwImage.ImageU(os.path.join(testPath, 'test_comp.fits.gz'), allowUnsafe=True)} for cw in self.cameraList: camera = cw.camera imList = self.assemblyList[camera.getName()] for ccdName in ccdNames: for trim, assemble in ((False, assembleAmplifierRawImage), (True, assembleAmplifierImage)): det = camera[ccdName] if not trim: outBbox = cameraGeomUtils.calcRawCcdBBox(det) else: outBbox = det.getBBox() outImage = afwImage.ImageU(outBbox) if len(imList) == 1: for amp in det: assemble(outImage, imList[0], amp) else: for amp, im in zip(det, imList): assemble(outImage, im, amp) self.assertListEqual(outImage.getArray().flatten().tolist(), compMap[trim].getArray().flatten().tolist())
def testCameraGeomUtils(self): for cw in self.cameraList: camera = cw.camera disp = afwDisplay.Display() cameraGeomUtils.showCamera(camera, display=disp) disp.incrDefaultFrame() for det in (camera[10], camera[20]): cameraGeomUtils.showCcd(det, inCameraCoords=False) disp.incrDefaultFrame() cameraGeomUtils.showCcd(det, inCameraCoords=True) disp.incrDefaultFrame() cameraGeomUtils.showCcd(det, inCameraCoords=False) disp.incrDefaultFrame() cameraGeomUtils.showCcd(det, inCameraCoords=True) disp.incrDefaultFrame() for amp in det: cameraGeomUtils.showAmp(amp, display=disp, imageFactory=afwImage.ImageF) disp.incrDefaultFrame()
for cw in self.cameraList: camera = cw.camera point = lsst.geom.Point2D(0, 0) # non-existant source camera system with self.assertRaises(pexExcept.InvalidParameterError): camera.getTransform(CameraSys("badSystem"), FOCAL_PLANE) with self.assertRaises(pexExcept.InvalidParameterError): camera.transform(point, CameraSys("badSystem"), FOCAL_PLANE) # non-existant destination camera system with self.assertRaises(pexExcept.InvalidParameterError): camera.getTransform(FOCAL_PLANE, CameraSys("badSystem")) with self.assertRaises(pexExcept.InvalidParameterError): camera.transform(point, FOCAL_PLANE, CameraSys("badSystem")) # non-existent source detector with self.assertRaises(KeyError): camera.getTransform(CameraSys("pixels", "invalid"), FOCAL_PLANE) with self.assertRaises(KeyError): camera.transform(point, CameraSys("pixels", "invalid"), FOCAL_PLANE) # non-existent destination detector with self.assertRaises(KeyError): camera.getTransform(FOCAL_PLANE, CameraSys("pixels", "invalid")) with self.assertRaises(KeyError): camera.transform(point, FOCAL_PLANE, CameraSys("pixels", "invalid"))
"""Test that we can round-trip a DetectorCollection through FITS I/O. """ for wrapper in self.cameraList: camera = wrapper.camera detectors = list(camera) collectionIn = DetectorCollection(detectors) with lsst.utils.tests.getTempFilePath(".fits") as filename: collectionIn.writeFits(filename) collectionOut = DetectorCollection.readFits(filename) self.assertDetectorCollectionsEqual(collectionIn, collectionOut)
"""Test that we can round-trip a Camera through FITS I/O. """ for wrapper in self.cameraList: cameraIn = wrapper.camera with lsst.utils.tests.getTempFilePath(".fits") as filename: cameraIn.writeFits(filename) cameraOut = Camera.readFits(filename) self.assertCamerasEqual(cameraIn, cameraOut)
lsst.utils.tests.init()
lsst.utils.tests.init() unittest.main() |