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

# 

# LSST Data Management System 

# 

# Copyright 2008-2017 AURA/LSST. 

# 

# 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 <https://www.lsstcorp.org/LegalNotices/>. 

# 

from __future__ import absolute_import, division, print_function 

 

import os 

import unittest 

import numpy as np 

 

from lsst.daf.persistence import NoResults 

import lsst.afw.geom 

import lsst.afw.image 

from lsst.afw.fits import readMetadata 

import lsst.meas.mosaic 

import lsst.daf.base 

import lsst.utils.tests 

 

DATA_DIR = os.path.join(os.path.split(__file__)[0], "data") 

 

# Note: mosaic's internal ffp object is magnitude-based. 

# Instead, for these tests, we just divide this out when comparing. 

referenceFlux = 1e23 * 10**(48.6 / -2.5) * 1e9 

 

 

def displayImageDifferences(image1, image2, rtol=1E-8, atol=1E-8, pause=False): 

import lsst.afw.display 

diff = type(image1)(image1, deep=True) 

diff -= image2 

relTo = np.maximum(np.abs(image1.array), np.abs(image2.array)) 

mask = lsst.afw.image.Mask(image1.getBBox()) 

mask.array[:, :] = (mask.getPlaneBitMask("DETECTED") * 

np.logical_and(np.abs(diff.array[:, :]) > atol, 

np.abs(diff.array[:, :]) > rtol*relTo)) 

d1 = lsst.afw.display.Display(frame=0) 

d2 = lsst.afw.display.Display(frame=1) 

d3 = lsst.afw.display.Display(frame=3) 

d1.setMaskTransparency(50) 

d2.setMaskTransparency(50) 

d3.setMaskTransparency(50) 

d1.scale("linear", "minmax") 

d2.scale("linear", "minmax") 

d3.scale("linear", "minmax") 

d1.mtv(lsst.afw.image.makeMaskedImage(image1, mask, None), title="PhotoCalib image") 

d2.mtv(lsst.afw.image.makeMaskedImage(image2, mask, None), title="fcr image") 

d3.mtv(lsst.afw.image.makeMaskedImage(diff, mask, None), title="diff(PhotoCalib-fcr)") 

 

if pause: 

print("Dropping into debugger to allow inspection of display") 

print("Note that any pixles not satisfying atol and rtol requirements will be masked BLUE") 

print("Type 'continue' when done.") 

import pdb 

pdb.set_trace() 

 

 

class MockDetector(object): 

 

def __init__(self, orientation): 

self.orientation = orientation 

 

def getOrientation(self): 

return self.orientation 

 

 

class MockOrientation(object): 

 

def __init__(self, nQuarter): 

self.nQuarter = nQuarter 

 

def getNQuarter(self): 

return self.nQuarter 

 

 

class MockExposure(object): 

 

def __init__(self, exposure, detector): 

self.exposure = exposure 

self.detector = detector 

 

def getDetector(self): 

return self.detector 

 

def __getattr__(self, name): 

return getattr(self.exposure, name) 

 

 

class MockDataRef(object): 

 

def __init__(self, **dataId): 

self.dataId = dataId 

self.datasets = {} 

 

def get(self, name, immediate=True): 

try: 

return self.datasets[name] 

except KeyError as err: 

raise NoResults(str(err), name, self.dataId) 

 

def put(self, dataset, name): 

self.datasets[name] = dataset 

 

 

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

 

def setUp(self): 

np.random.seed(100) 

self.tract = 8766 

self.visit = 11506 

self.ccds = {0: 49, 2: 50, 3: 101, 1: 102} 

# Box is the same for all CCDs, since it's defined in CCD coordinates, 

# which are rotated w.r.t. focal plane coordinates. 

self.bbox = lsst.afw.geom.Box2I( 

lsst.afw.geom.Point2I(0, 0), 

lsst.afw.geom.Point2I(2047, 4175) 

) 

self.ffp = {} 

self.wcs = {} 

self.photoCalib = {} 

self.dataRefs = {} 

camera = {} # all we need from our mock camera is dict-like access to (Mock)Detectors. 

calexpMetadata = lsst.daf.base.PropertyList() 

for nQuarter, ccd in self.ccds.items(): 

fcrFilename = os.path.join( 

DATA_DIR, 

"%d/fcr-%07d-%03d.fits" % (self.tract, self.visit, ccd) 

) 

fcrMetadata = readMetadata(fcrFilename) 

fcrPhotoCalib = lsst.afw.image.ExposureF(fcrFilename).getPhotoCalib() 

self.ffp[ccd] = lsst.meas.mosaic.FluxFitParams(fcrMetadata) 

wcsFilename = os.path.join( 

DATA_DIR, 

"%d/jointcal_wcs-%07d-%03d.fits" % (self.tract, self.visit, ccd) 

) 

self.wcs[ccd] = lsst.afw.geom.SkyWcs.readFits(wcsFilename) 

photoCalibFilename = os.path.join( 

DATA_DIR, 

"%d/jointcal_photoCalib-%07d-%03d.fits" % (self.tract, self.visit, ccd) 

) 

self.photoCalib[ccd] = lsst.afw.image.PhotoCalib.readFits(photoCalibFilename) 

camera[ccd] = MockDetector(MockOrientation(nQuarter)) 

self.dataRefs[ccd] = MockDataRef(visit=self.visit, tract=self.tract, ccd=ccd) 

self.dataRefs[ccd].put(fcrMetadata, "fcr_md", ) 

self.dataRefs[ccd].put(fcrPhotoCalib, "fcr_photoCalib") 

self.dataRefs[ccd].put(self.wcs[ccd], "jointcal_wcs") 

self.dataRefs[ccd].put(calexpMetadata, "calexp_md") 

self.dataRefs[ccd].put(camera, "camera") 

self.dataRefs[ccd].put(self.bbox, "calexp_bbox") 

 

def tearDown(self): 

del self.ffp 

del self.wcs 

del self.photoCalib 

del self.dataRefs 

 

def makeBoundedField(self, nQuarter, ffp=True, wcs=True): 

ccd = self.ccds[nQuarter] 

if ffp: 

ffp = self.ffp[ccd] 

else: 

ffp = None 

if wcs: 

wcs = self.wcs[ccd] 

else: 

wcs = None 

bf = lsst.meas.mosaic.FluxFitBoundedField(self.bbox, ffp=ffp, wcs=wcs, 

nQuarter=nQuarter) 

return bf, ffp, wcs 

 

def checkFillImage(self, nQuarter, ffp=True, wcs=True, display=False): 

"""Test that making an image from a FluxFitBoundedField 

is equivalent to using getFCorImg on the FluxFitParams 

it was constructed from, with correct rotation. 

""" 

bf, ffp, wcs = self.makeBoundedField(nQuarter, ffp, wcs) 

image1 = lsst.afw.image.ImageF(self.bbox) 

bf.fillImage(image1, xStep=100, yStep=16) 

image1 /= referenceFlux # turn nJy into Maggy: ffp outputs Maggy 

if nQuarter%2: 

width, height = self.bbox.getHeight(), self.bbox.getWidth() 

else: 

width, height = self.bbox.getWidth(), self.bbox.getHeight() 

image2 = lsst.afw.image.ImageF(self.bbox) 

image2.array[:, :] = 1.0 

if ffp: 

image2a = lsst.meas.mosaic.getFCorImg(ffp, width, height) 

image2 *= lsst.afw.math.rotateImageBy90(image2a, 4 - nQuarter) 

if wcs: 

image2b = lsst.meas.mosaic.getJImg(wcs, self.bbox.getWidth(), 

self.bbox.getHeight()) 

image2 *= image2b 

# more round-off error in calculations for nQuarter != 0 

rtol = 1E-6 if nQuarter == 0 else 1E-4 

if display: 

print("nQuarter = {} ffp = {} wcs = {} ".format(nQuarter, ffp, wcs)) 

displayImageDifferences(image1, image2, rtol=rtol, pause=True) 

self.assertImagesAlmostEqual(image1, image2, rtol=rtol) 

 

def checkMultiply(self, nQuarter): 

bf1, ffp, wcs = self.makeBoundedField(nQuarter) 

bf2 = bf1*2.4 

bf3 = 7.2*bf1 

bf4 = bf1/0.5 

N_POINTS = 50 

px = np.random.uniform(low=self.bbox.getMinX(), 

high=self.bbox.getMaxX(), 

size=N_POINTS) 

py = np.random.uniform(low=self.bbox.getMinY(), 

high=self.bbox.getMaxY(), 

size=N_POINTS) 

z1 = bf1.evaluate(px, py) 

z2 = bf2.evaluate(px, py) 

z3 = bf3.evaluate(px, py) 

z4 = bf4.evaluate(px, py) 

self.assertFloatsAlmostEqual(z2, z1*2.4, rtol=1E-15) 

self.assertFloatsAlmostEqual(z3, 7.2*z1, rtol=1E-15) 

self.assertFloatsAlmostEqual(z4, z1/0.5, rtol=1E-15) 

 

def checkPersistence(self, nQuarter): 

bf1, ffp, wcs = self.makeBoundedField(nQuarter, ffp=True, wcs=False) 

with lsst.utils.tests.getTempFilePath(".fits") as tempFile: 

bf1.writeFits(tempFile) 

bf2 = lsst.afw.math.BoundedField.readFits(tempFile) 

self.assertEqual(bf1, bf2) 

 

def checkPhotoCalibCatalog(self, nQuarter): 

ccd = self.ccds[nQuarter] 

schema = lsst.afw.table.SourceTable.makeMinimalSchema() 

xKey = schema.addField("position_x", type=np.float64, doc="column position", units="pixel") 

yKey = schema.addField("position_y", type=np.float64, doc="row position", units="pixel") 

fluxKey = schema.addField("example_instFlux", type=np.float64, doc="flux", units="count") 

fluxErrKey = schema.addField("example_instFluxErr", type=np.float64, doc="flux uncertainty", 

units="count") 

schema.getAliasMap().set("slot_Centroid", "position") 

nRecords = 5 

catalog = lsst.afw.table.SourceCatalog(schema) 

catalog.reserve(nRecords) 

for n in range(nRecords): 

record = catalog.addNew() 

record.set(xKey, np.random.uniform(low=self.bbox.getMinX(), high=self.bbox.getMaxX())) 

record.set(yKey, np.random.uniform(low=self.bbox.getMinY(), high=self.bbox.getMaxY())) 

record.set(fluxKey, np.random.randn()*1E3 + 1E4) 

record.set(fluxErrKey, np.sqrt(record.get(fluxKey))) 

# Compute fully-calibrated magnitudes using new PhotoCalib + BoundedField object 

photoCalib = self.photoCalib[ccd] 

mag1, magErr1 = photoCalib.instFluxToMagnitude(catalog, "example").transpose() 

# Compute fully-calibrated magnitudes using the old approach 

catalog2 = catalog.copy(deep=True) 

results2 = lsst.meas.mosaic.applyMosaicResultsCatalog(self.dataRefs[ccd], catalog2) 

catalog2 = results2.catalog 

catalog2 = lsst.meas.mosaic.applyCalib(catalog2, results2.ffp.calib) 

mag2 = catalog2["example_mag"] 

# Check that the non-spatially varying part of the correction is the same. 

self.assertFloatsAlmostEqual(photoCalib.getCalibrationMean(), 

results2.ffp.calib.getCalibrationMean(), 

rtol=1E-14) 

self.assertFloatsAlmostEqual(photoCalib.getCalibrationErr(), 

results2.ffp.calib.getCalibrationErr(), 

rtol=1E-14) 

# Compute partially-calibrated magnitudes that don't account for the spatially-varying part. 

mag0 = results2.ffp.calib.instFluxToMagnitude(catalog, "example") 

# Check that both approaches yield similar results overall... 

rtol = 1E-10 if nQuarter == 0 else 1E-6 # rotating SIP Wcses involves a big loss of precision 

self.assertFloatsAlmostEqual(mag1, mag2, rtol=rtol) 

# ...and in just the spatially-varying part (but with less precision, partially because of 

# round-off error). 

magDiff2 = mag2 - mag0[:, 0] 

magDiff1 = mag1 - mag0[:, 0] 

self.assertFloatsAlmostEqual(magDiff1, magDiff2, rtol=rtol*3E3) 

 

def checkPhotoCalibExposure(self, nQuarter): 

ccd = self.ccds[nQuarter] 

photoCalib = self.photoCalib[ccd] 

original = lsst.afw.image.ExposureF(self.bbox) 

original.image.array[:, :] = 1.0 

image1 = lsst.afw.image.ImageF(original.image, deep=True) 

photoCalib.computeScaledCalibration().multiplyImage(image1, xStep=100, yStep=16) 

camera = self.dataRefs[ccd].get("camera") 

calexp = MockExposure(original, detector=camera[ccd]) 

self.dataRefs[ccd].put(calexp, "calexp") 

results2 = lsst.meas.mosaic.applyMosaicResultsExposure(self.dataRefs[ccd]) 

rtol = 1E-6 if nQuarter == 0 else 1E-4 

self.assertImagesAlmostEqual(image1, results2.exposure.image, rtol=rtol) 

 

def testNQuarter0(self): 

self.checkFillImage(0, ffp=True, wcs=False) 

self.checkFillImage(0, ffp=False, wcs=True) 

self.checkFillImage(0, ffp=True, wcs=True) 

self.checkMultiply(0) 

self.checkPersistence(0) 

self.checkPhotoCalibCatalog(0) 

self.checkPhotoCalibExposure(0) 

 

def testNQuarter1(self): 

self.checkFillImage(1, ffp=True, wcs=False) 

self.checkFillImage(1, ffp=False, wcs=True) 

self.checkFillImage(1, ffp=True, wcs=True) 

self.checkMultiply(1) 

self.checkPersistence(1) 

self.checkPhotoCalibCatalog(1) 

self.checkPhotoCalibExposure(1) 

 

def testNQuarter2(self): 

self.checkFillImage(2, ffp=True, wcs=False) 

self.checkFillImage(2, ffp=False, wcs=True) 

self.checkFillImage(2, ffp=True, wcs=True) 

self.checkMultiply(2) 

self.checkPersistence(2) 

self.checkPhotoCalibCatalog(2) 

self.checkPhotoCalibExposure(2) 

 

def testNQuarter3(self): 

self.checkFillImage(3, ffp=True, wcs=False) 

self.checkFillImage(3, ffp=False, wcs=True) 

self.checkFillImage(3, ffp=True, wcs=True) 

self.checkMultiply(3) 

self.checkPersistence(3) 

self.checkPhotoCalibCatalog(3) 

self.checkPhotoCalibExposure(3) 

 

 

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

"""Run the tests""" 

unittest.main()