Hide keyboard shortcuts

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

# 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/>. 

 

import unittest 

import os 

 

import numpy as np 

 

import lsst.utils.tests 

import lsst.pex.exceptions as pexExcept 

import lsst.geom 

import lsst.afw.image as afwImage 

import lsst.afw.display as afwDisplay 

from lsst.afw.cameraGeom import PIXELS, FIELD_ANGLE, FOCAL_PLANE, CameraSys, CameraSysPrefix, \ 

Camera, Detector, assembleAmplifierImage, assembleAmplifierRawImage, DetectorCollection 

import lsst.afw.cameraGeom.testUtils as testUtils 

import lsst.afw.cameraGeom.utils as cameraGeomUtils 

 

try: 

type(display) 

except NameError: 

display = False 

 

testPath = os.path.abspath(os.path.dirname(__file__)) 

 

 

class CameraGeomTestCase(lsst.utils.tests.TestCase): 

"""A test case for camera geometry""" 

 

def setUp(self): 

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)] 

 

def tearDown(self): 

del self.lsstCamWrapper 

del self.scCamWrapper 

del self.cameraList 

del self.assemblyList 

 

def testConstructor(self): 

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)) 

 

def testCameraSysRepr(self): 

"""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) 

 

def testAccessor(self): 

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) 

 

def testTransformSlalib(self): 

"""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) 

 

def testTransformDet(self): 

"""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) 

 

def testFindDetectors(self): 

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) 

 

def testFpBbox(self): 

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()) 

 

def testLinearity(self): 

"""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) 

 

def testAssembly(self): 

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()) 

 

@unittest.skipIf(not display, "display variable not set; skipping cameraGeomUtils test") 

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() 

 

def testCameraRaises(self): 

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")) 

 

def testDetectorCollectionPersistence(self): 

"""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) 

 

def testCameraPersistence(self): 

"""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) 

 

 

class TestMemory(lsst.utils.tests.MemoryTestCase): 

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

357 ↛ 358line 357 didn't jump to line 358, because the condition on line 357 was never trueif __name__ == "__main__": 

lsst.utils.tests.init() 

unittest.main()