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

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

# See COPYRIGHT file at the top of the source tree. 

# 

# This file is part of fgcmcal. 

# 

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

"""General fgcmcal testing class. 

 

This class is used as the basis for individual obs package tests using 

data from testdata_jointcal. 

""" 

 

import os 

import shutil 

import numpy as np 

import glob 

 

import lsst.daf.persistence as dafPersistence 

import lsst.afw.geom as afwGeom 

import lsst.log 

from lsst.meas.algorithms import LoadIndexedReferenceObjectsTask, LoadIndexedReferenceObjectsConfig 

import lsst.afw.image as afwImage 

 

import lsst.fgcmcal as fgcmcal 

 

 

class FgcmcalTestBase(object): 

""" 

Base class for fgcmcal tests, to genericize some test running and setup. 

 

Derive from this first, then from TestCase. 

""" 

 

def setUp_base(self, inputDir=None, testDir=None, logLevel=None, otherArgs=[]): 

""" 

Call from your child class's setUp() to get variables built. 

 

Parameters 

---------- 

inputDir: `str`, optional 

Input directory 

testDir: `str`, optional 

Test directory 

logLevel: `str`, optional 

Override loglevel for command-line tasks 

otherArgs: `list`, default=[] 

List of additional arguments to send to command-line tasks 

""" 

 

self.config = None 

self.inputDir = inputDir 

self.testDir = testDir 

self.logLevel = logLevel 

self.otherArgs = otherArgs 

 

lsst.log.setLevel("daf.persistence.butler", lsst.log.FATAL) 

lsst.log.setLevel("CameraMapper", lsst.log.FATAL) 

 

if self.logLevel is not None: 

self.otherArgs.extend(['--loglevel', 'fgcmcal=%s'%self.logLevel]) 

 

def _testFgcmMakeLut(self, nBand, i0Std, i0Recon, i10Std, i10Recon): 

""" 

Test running of FgcmMakeLutTask 

 

Parameters 

---------- 

nBand: `int` 

Number of bands tested 

i0Std: `np.array', size nBand 

Values of i0Std to compare to 

i10Std: `np.array`, size nBand 

Values of i10Std to compare to 

i0Recon: `np.array`, size nBand 

Values of reconstructed i0 to compare to 

i10Recon: `np.array`, size nBand 

Values of reconsntructed i10 to compare to 

 

Raises 

------ 

Exceptions on test failures 

""" 

 

args = [self.inputDir, '--output', self.testDir, 

'--doraise'] 

args.extend(self.otherArgs) 

 

result = fgcmcal.FgcmMakeLutTask.parseAndRun(args=args, config=self.config) 

self._checkResult(result) 

 

butler = dafPersistence.butler.Butler(self.testDir) 

tempTask = fgcmcal.FgcmFitCycleTask() 

fgcmLut, lutIndexVals, lutStd = tempTask._loadFgcmLut(butler) 

 

# Check that we got the requested number of bands... 

self.assertEqual(nBand, len(lutIndexVals[0]['FILTERNAMES'])) 

 

self.assertFloatsAlmostEqual(i0Std, lutStd[0]['I0STD'], msg='I0Std', rtol=1e-5) 

self.assertFloatsAlmostEqual(i10Std, lutStd[0]['I10STD'], msg='I10Std', rtol=1e-5) 

 

indices = fgcmLut.getIndices(np.arange(nBand, dtype=np.int32), 

np.zeros(nBand) + np.log(lutStd[0]['PWVSTD']), 

np.zeros(nBand) + lutStd[0]['O3STD'], 

np.zeros(nBand) + np.log(lutStd[0]['TAUSTD']), 

np.zeros(nBand) + lutStd[0]['ALPHASTD'], 

np.zeros(nBand) + 1. / np.cos(np.radians(lutStd[0]['ZENITHSTD'])), 

np.zeros(nBand, dtype=np.int32), 

np.zeros(nBand) + lutStd[0]['PMBSTD']) 

i0 = fgcmLut.computeI0(np.zeros(nBand) + np.log(lutStd[0]['PWVSTD']), 

np.zeros(nBand) + lutStd[0]['O3STD'], 

np.zeros(nBand) + np.log(lutStd[0]['TAUSTD']), 

np.zeros(nBand) + lutStd[0]['ALPHASTD'], 

np.zeros(nBand) + 1. / np.cos(np.radians(lutStd[0]['ZENITHSTD'])), 

np.zeros(nBand) + lutStd[0]['PMBSTD'], 

indices) 

 

self.assertFloatsAlmostEqual(i0Recon, i0, msg='i0Recon', rtol=1e-5) 

 

i1 = fgcmLut.computeI1(np.zeros(nBand) + np.log(lutStd[0]['PWVSTD']), 

np.zeros(nBand) + lutStd[0]['O3STD'], 

np.zeros(nBand) + np.log(lutStd[0]['TAUSTD']), 

np.zeros(nBand) + lutStd[0]['ALPHASTD'], 

np.zeros(nBand) + 1. / np.cos(np.radians(lutStd[0]['ZENITHSTD'])), 

np.zeros(nBand) + lutStd[0]['PMBSTD'], 

indices) 

 

self.assertFloatsAlmostEqual(i10Recon, i1 / i0, msg='i10Recon', rtol=1e-5) 

 

def _testFgcmBuildStars(self, nVisit, nStar, nObs): 

""" 

Test running of FgcmBuildStarsTask 

 

Parameters 

---------- 

nVisit: `int` 

Number of visits expected 

nStar: `int` 

Number of stars expected 

nObs: `int` 

Number of observations of stars expected 

 

Raises 

------ 

Exceptions on test failures 

""" 

 

args = [self.inputDir, '--output', self.testDir, 

'--doraise'] 

args.extend(self.otherArgs) 

 

result = fgcmcal.FgcmBuildStarsTask.parseAndRun(args=args, config=self.config) 

self._checkResult(result) 

 

butler = dafPersistence.butler.Butler(self.testDir) 

 

visitCat = butler.get('fgcmVisitCatalog') 

self.assertEqual(nVisit, len(visitCat)) 

 

starIds = butler.get('fgcmStarIds') 

self.assertEqual(nStar, len(starIds)) 

 

starObs = butler.get('fgcmStarObservations') 

self.assertEqual(nObs, len(starObs)) 

 

def _testFgcmFitCycle(self, nZp, nGoodZp, nOkZp, nBadZp, nStdStars, nPlots, skipChecks=False): 

""" 

Test running of FgcmFitCycleTask 

 

Parameters 

---------- 

nZp: `int` 

Number of zeropoints created by the task 

nGoodZp: `int` 

Number of good (photometric) zeropoints created 

nOkZp: `int` 

Number of constrained zeropoints (photometric or not) 

nBadZp: `int` 

Number of unconstrained (bad) zeropoints 

nStdStars: `int` 

Number of standard stars produced 

nPlots: `int` 

Number of plots produced 

skipChecks: `bool`, optional 

Skip number checks, when running less-than-final cycle. 

Default is False. 

""" 

 

args = [self.inputDir, '--output', self.testDir, 

'--doraise'] 

args.extend(self.otherArgs) 

 

# Move into the test directory so the plots will get cleaned in tearDown 

# In the future, with Gen3, we will probably have a better way of managing 

# non-data output such as plots. 

cwd = os.getcwd() 

os.chdir(self.testDir) 

 

result = fgcmcal.FgcmFitCycleTask.parseAndRun(args=args, config=self.config) 

self._checkResult(result) 

 

# Move back to the previous directory 

os.chdir(cwd) 

 

if skipChecks: 

return 

 

# Check that the expected number of plots are there. 

plots = glob.glob(os.path.join(self.testDir, self.config.outfileBase + 

'_cycle%02d_plots/' % (self.config.cycleNumber) + 

'*.png')) 

self.assertEqual(nPlots, len(plots)) 

 

butler = dafPersistence.butler.Butler(self.testDir) 

 

zps = butler.get('fgcmZeropoints', fgcmcycle=self.config.cycleNumber) 

 

# Check the numbers of zeropoints in all, good, okay, and bad 

self.assertEqual(nZp, len(zps)) 

 

gd, = np.where(zps['fgcmFlag'] == 1) 

self.assertEqual(nGoodZp, len(gd)) 

 

ok, = np.where(zps['fgcmFlag'] < 16) 

self.assertEqual(nOkZp, len(ok)) 

 

bd, = np.where(zps['fgcmFlag'] >= 16) 

self.assertEqual(nBadZp, len(bd)) 

 

# Check that there are no illegal values with the ok zeropoints 

test, = np.where(zps['fgcmZpt'][gd] < -9000.0) 

self.assertEqual(0, len(test)) 

 

stds = butler.get('fgcmStandardStars', fgcmcycle=self.config.cycleNumber) 

 

self.assertEqual(nStdStars, len(stds)) 

 

def _testFgcmOutputProducts(self, visitDataRefName, ccdDataRefName, filterMapping, 

zpOffsets, testVisit, testCcd, testFilter, testBandIndex): 

""" 

Test running of FgcmOutputProductsTask 

 

Parameters 

---------- 

visitDataRefName: `str` 

Name of column in dataRef to get the visit 

ccdDataRefName: `str` 

Name of column in dataRef to get the ccd 

filterMapping: `dict` 

Mapping of filterName to dataRef filter names 

zpOffsets: `np.array` 

Zeropoint offsets expected 

testVisit: `int` 

Visit id to check for round-trip computations 

testCcd: `int` 

Ccd id to check for round-trip computations 

testFilter: `str` 

Filtername for testVisit/testCcd 

testBandIndex: `int` 

Band index for testVisit/testCcd 

""" 

 

args = [self.inputDir, '--output', self.testDir, 

'--doraise'] 

args.extend(self.otherArgs) 

 

result = fgcmcal.FgcmOutputProductsTask.parseAndRun(args=args, config=self.config, 

doReturnResults=True) 

self._checkResult(result) 

 

# Extract the offsets from the results 

offsets = result.resultList[0].results.offsets 

 

self.assertFloatsAlmostEqual(offsets[0], zpOffsets[0], atol=1e-6) 

self.assertFloatsAlmostEqual(offsets[1], zpOffsets[1], atol=1e-6) 

 

butler = dafPersistence.butler.Butler(self.testDir) 

 

# Test the reference catalog stars 

 

# Read in the raw stars... 

rawStars = butler.get('fgcmStandardStars', fgcmcycle=self.config.cycleNumber) 

 

# Read in the new reference catalog... 

config = LoadIndexedReferenceObjectsConfig() 

config.ref_dataset_name = 'fgcm_stars' 

task = LoadIndexedReferenceObjectsTask(butler, config=config) 

# Read in a giant radius to get them all 

refStruct = task.loadSkyCircle(rawStars[0].getCoord(), 5.0 * lsst.geom.degrees, 

filterName='r') 

 

# Make sure all the stars are there 

self.assertEqual(len(rawStars), len(refStruct.refCat)) 

 

# And make sure the numbers are consistent 

test, = np.where(rawStars['id'][0] == refStruct.refCat['id']) 

 

mag = rawStars['mag_std_noabs'][0, 0] + offsets[0] 

flux = afwImage.fluxFromABMag(mag) 

fluxErr = afwImage.fluxErrFromABMagErr(rawStars['magErr_std'][0, 0], mag) 

self.assertFloatsAlmostEqual(flux, refStruct.refCat['r_flux'][test[0]], rtol=1e-6) 

self.assertFloatsAlmostEqual(fluxErr, refStruct.refCat['r_fluxErr'][test[0]], rtol=1e-6) 

 

# Test the joincal_photoCalib output 

 

zptCat = butler.get('fgcmZeropoints', fgcmcycle=self.config.cycleNumber) 

selected = (zptCat['fgcmFlag'] < 16) 

 

# Read in all the calibrations, these should all be there 

# This test is simply to ensure that all the photoCalib files exist 

for rec in zptCat[selected]: 

testCal = butler.get('fgcm_photoCalib', 

dataId={visitDataRefName: int(rec['visit']), 

ccdDataRefName: int(rec['ccd']), 

'filter': filterMapping[rec['filtername']]}) 

self.assertIsNotNone(testCal) 

 

# We do round-trip value checking on just the final one (chosen arbitrarily) 

testCal = butler.get('fgcm_photoCalib', 

dataId={visitDataRefName: int(testVisit), 

ccdDataRefName: int(testCcd), 

'filter': filterMapping[testFilter]}) 

self.assertIsNotNone(testCal) 

 

src = butler.get('src', dataId={visitDataRefName: int(testVisit), 

ccdDataRefName: int(testCcd)}) 

 

# Only test sources with positive flux 

gdSrc = (src['slot_CalibFlux_flux'] > 0.0) 

 

# We need to apply the calibration offset to the fgcmzpt (which is internal 

# and doesn't know about that yet) 

testZpInd, = np.where((zptCat['visit'] == testVisit) & 

(zptCat['ccd'] == testCcd)) 

fgcmZpt = zptCat['fgcmZpt'][testZpInd] + offsets[testBandIndex] 

 

# This is the magnitude through the mean calibration 

photoCalMeanCalMags = np.zeros(gdSrc.sum()) 

# This is the magnitude through the full focal-plane variable mags 

photoCalMags = np.zeros_like(photoCalMeanCalMags) 

# This is the magnitude with the FGCM (central-ccd) zeropoint 

zptMeanCalMags = np.zeros_like(photoCalMeanCalMags) 

 

for i, rec in enumerate(src[gdSrc]): 

photoCalMeanCalMags[i] = testCal.instFluxToMagnitude(rec['slot_CalibFlux_flux']) 

photoCalMags[i] = testCal.instFluxToMagnitude(rec['slot_CalibFlux_flux'], 

rec.getCentroid()) 

zptMeanCalMags[i] = fgcmZpt - 2.5*np.log10(rec['slot_CalibFlux_flux']) 

 

# These should be very close but some tiny differences because the fgcm value 

# is defined at the center of the bbox, and the photoCal is the mean over the box 

self.assertFloatsAlmostEqual(photoCalMeanCalMags, 

zptMeanCalMags, rtol=1e-6) 

# These should be roughly equal, but not precisely because of the focal-plane 

# variation. However, this is a useful sanity check for something going totally 

# wrong. 

self.assertFloatsAlmostEqual(photoCalMeanCalMags, 

photoCalMags, rtol=1e-2) 

 

# Test the transmission output 

 

visitCatalog = butler.get('fgcmVisitCatalog') 

lutCat = butler.get('fgcmLookUpTable') 

 

testTrans = butler.get('transmission_atmosphere_fgcm', 

dataId={visitDataRefName: visitCatalog[0]['visit']}) 

testResp = testTrans.sampleAt(position=afwGeom.Point2D(0, 0), 

wavelengths=lutCat[0]['atmLambda']) 

 

# The test fit is performed with the atmosphere parameters frozen 

# (freezeStdAtmosphere = True). Thus the only difference between 

# these output atmospheres and the standard is the different 

# airmass. Furthermore, this is a very rough comparison because 

# the look-up table is computed with very coarse sampling for faster 

# testing. 

# Therefore, this rough comparison can only be seen as a sanity check 

# and is not high precision. 

self.assertFloatsAlmostEqual(testResp, lutCat[0]['atmStdTrans'], atol=0.06) 

 

# The second should be close to the first, but there is the airmass 

# difference so they aren't identical 

testTrans2 = butler.get('transmission_atmosphere_fgcm', 

dataId={visitDataRefName: visitCatalog[1]['visit']}) 

testResp2 = testTrans2.sampleAt(position=afwGeom.Point2D(0, 0), 

wavelengths=lutCat[0]['atmLambda']) 

self.assertFloatsAlmostEqual(testResp, testResp2, atol=1e-4) 

 

def _checkResult(self, result): 

""" 

Check the result output from the task 

 

Parameters 

---------- 

result: `pipeBase.struct` 

Result structure output from a task 

 

Raises 

------ 

Exceptions on test failures 

""" 

 

self.assertNotEqual(result.resultList, [], 'resultList should not be empty') 

self.assertEqual(result.resultList[0].exitStatus, 0) 

 

def tearDown(self): 

""" 

Tear down and clear directories 

""" 

 

if getattr(self, 'config', None) is not None: 

del self.config 

 

if os.path.exists(self.testDir): 

shutil.rmtree(self.testDir, True)