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

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

# 

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

# 

 

import unittest 

import numpy as np 

 

import lsst.afw.image as afwImage 

import lsst.ip.isr.isrMock as isrMock 

import lsst.utils.tests 

from lsst.ip.isr.isrTask import (IsrTask, IsrTaskConfig) 

from lsst.ip.isr.isrQa import IsrQaConfig 

from lsst.pipe.base import Struct 

 

 

def countMaskedPixels(maskedImage, maskPlane): 

"""Function to count the number of masked pixels of a given type. 

 

Parameters 

---------- 

maskedImage : `lsst.afw.image.MaskedImage` 

Image to measure the mask on. 

maskPlane : `str` 

Name of the mask plane to count 

 

Returns 

------- 

nMask : `int` 

Number of masked pixels. 

""" 

bitMask = maskedImage.getMask().getPlaneBitMask(maskPlane) 

isBit = maskedImage.getMask().getArray() & bitMask > 0 

numBit = np.sum(isBit) 

return numBit 

 

 

def computeImageMedianAndStd(image): 

"""Function to calculate median and std of image data. 

 

Parameters 

---------- 

image : `lsst.afw.image.Image` 

Image to measure statistics on. 

 

Returns 

------- 

median : `float` 

Image median. 

std : `float` 

Image stddev. 

""" 

median = np.nanmedian(image.getArray()) 

std = np.nanstd(image.getArray()) 

return (median, std) 

 

 

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

"""Test IsrTask methods with trimmed raw data. 

""" 

def setUp(self): 

self.config = IsrTaskConfig() 

self.config.qa = IsrQaConfig() 

self.task = IsrTask(config=self.config) 

self.dataRef = isrMock.DataRefMock() 

self.camera = isrMock.IsrMock().getCamera() 

 

self.inputExp = isrMock.TrimmedRawMock().run() 

self.amp = self.inputExp.getDetector()[0] 

self.mi = self.inputExp.getMaskedImage() 

 

def validateIsrData(self, results): 

"""results should be a struct with components that are 

not None if included in the configuration file. 

""" 

self.assertIsInstance(results, Struct) 

if self.config.doBias is True: 

self.assertIsNotNone(results.bias) 

if self.config.doDark is True: 

self.assertIsNotNone(results.dark) 

if self.config.doFlat is True: 

self.assertIsNotNone(results.flat) 

if self.config.doFringe is True: 

self.assertIsNotNone(results.fringes) 

if self.config.doDefect is True: 

self.assertIsNotNone(results.defects) 

if self.config.doBrighterFatter is True: 

self.assertIsNotNone(results.bfKernel) 

if self.config.doAttachTransmissionCurve is True: 

self.assertIsNotNone(results.opticsTransmission) 

self.assertIsNotNone(results.filterTransmission) 

self.assertIsNotNone(results.sensorTransmission) 

self.assertIsNotNone(results.atmosphereTransmission) 

 

def test_readIsrData_noTrans(self): 

"""Test that all necessary calibration frames are retrieved. 

""" 

self.config.doAttachTransmissionCurve = False 

self.task = IsrTask(config=self.config) 

results = self.task.readIsrData(self.dataRef, self.inputExp) 

self.validateIsrData(results) 

 

def test_readIsrData_withTrans(self): 

"""Test that all necessary calibration frames are retrieved. 

""" 

self.config.doAttachTransmissionCurve = True 

self.task = IsrTask(config=self.config) 

results = self.task.readIsrData(self.dataRef, self.inputExp) 

self.validateIsrData(results) 

 

def test_ensureExposure(self): 

"""Test that an exposure has a usable instance class. 

""" 

self.assertIsInstance(self.task.ensureExposure(self.inputExp, self.camera, 0), 

afwImage.Exposure) 

 

def test_convertItoF(self): 

"""Test conversion from integer to floating point pixels. 

""" 

result = self.task.convertIntToFloat(self.inputExp) 

self.assertEqual(result.getImage().getArray().dtype, np.dtype("float32")) 

self.assertEqual(result, self.inputExp) 

 

def test_updateVariance(self): 

"""Expect The variance image should have a larger median value after 

this operation. 

""" 

statBefore = computeImageMedianAndStd(self.inputExp.variance[self.amp.getBBox()]) 

self.task.updateVariance(self.inputExp, self.amp) 

statAfter = computeImageMedianAndStd(self.inputExp.variance[self.amp.getBBox()]) 

self.assertGreater(statAfter[0], statBefore[0]) 

self.assertFloatsAlmostEqual(statBefore[0], 0.0, atol=1e-2) 

self.assertFloatsAlmostEqual(statAfter[0], 8175.6885, atol=1e-2) 

 

def test_darkCorrection(self): 

"""Expect the median image value should decrease after this operation. 

""" 

darkIm = isrMock.DarkMock().run() 

 

statBefore = computeImageMedianAndStd(self.inputExp.image[self.amp.getBBox()]) 

self.task.darkCorrection(self.inputExp, darkIm) 

statAfter = computeImageMedianAndStd(self.inputExp.image[self.amp.getBBox()]) 

self.assertLess(statAfter[0], statBefore[0]) 

self.assertFloatsAlmostEqual(statBefore[0], 8075.6885, atol=1e-2) 

self.assertFloatsAlmostEqual(statAfter[0], 8051.6206, atol=1e-2) 

 

def test_darkCorrection_noVisitInfo(self): 

"""Expect the median image value should decrease after this operation. 

""" 

darkIm = isrMock.DarkMock().run() 

darkIm.getInfo().setVisitInfo(None) 

 

statBefore = computeImageMedianAndStd(self.inputExp.image[self.amp.getBBox()]) 

self.task.darkCorrection(self.inputExp, darkIm) 

statAfter = computeImageMedianAndStd(self.inputExp.image[self.amp.getBBox()]) 

self.assertLess(statAfter[0], statBefore[0]) 

self.assertFloatsAlmostEqual(statBefore[0], 8075.6885, atol=1e-2) 

self.assertFloatsAlmostEqual(statAfter[0], 8051.6206, atol=1e-2) 

 

def test_flatCorrection(self): 

"""Expect the image median should increase (divide by < 1). 

""" 

flatIm = isrMock.FlatMock().run() 

 

statBefore = computeImageMedianAndStd(self.inputExp.image[self.amp.getBBox()]) 

self.task.flatCorrection(self.inputExp, flatIm) 

statAfter = computeImageMedianAndStd(self.inputExp.image[self.amp.getBBox()]) 

self.assertGreater(statAfter[1], statBefore[1]) 

self.assertFloatsAlmostEqual(statAfter[1], 147417.36, atol=1e-2) 

self.assertFloatsAlmostEqual(statBefore[1], 148.28436, atol=1e-2) 

 

def test_saturationDetection(self): 

"""Expect the saturation level detection/masking to scale with 

threshold. 

""" 

self.amp.setSaturation(9000.0) 

self.task.saturationDetection(self.inputExp, self.amp) 

countBefore = countMaskedPixels(self.mi, "SAT") 

 

self.amp.setSaturation(8250.0) 

self.task.saturationDetection(self.inputExp, self.amp) 

countAfter = countMaskedPixels(self.mi, "SAT") 

 

self.assertLessEqual(countBefore, countAfter) 

self.assertEqual(countBefore, 43) 

self.assertEqual(countAfter, 141) 

 

def test_measureBackground(self): 

"""Expect the background measurement runs successfully and to save 

metadata values. 

""" 

self.config.qa.flatness.meshX = 20 

self.config.qa.flatness.meshY = 20 

self.task.measureBackground(self.inputExp, self.config.qa) 

self.assertIsNotNone(self.inputExp.getMetadata().getScalar('SKYLEVEL')) 

 

def test_flatContext(self): 

"""Expect the flat context manager runs successfully (applying both 

flat and dark within the context), and results in the same 

image data after completion. 

""" 

darkExp = isrMock.DarkMock().run() 

flatExp = isrMock.FlatMock().run() 

 

mi = self.inputExp.getMaskedImage().clone() 

self.inputExp.writeFits("/tmp/czwBefore.fits") 

with self.task.flatContext(self.inputExp, flatExp, darkExp): 

contextStat = computeImageMedianAndStd(self.inputExp.getMaskedImage().getImage()) 

self.assertFloatsAlmostEqual(contextStat[0], 37269.914, atol=1e-2) 

 

self.assertMaskedImagesAlmostEqual(mi, self.inputExp.getMaskedImage()) 

 

 

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

"""Test IsrTask methods using untrimmed raw data. 

""" 

def setUp(self): 

self.config = IsrTaskConfig() 

self.config.qa = IsrQaConfig() 

self.task = IsrTask(config=self.config) 

 

self.mockConfig = isrMock.IsrMockConfig() 

self.mockConfig.isTrimmed = False 

self.doGenerateImage = True 

self.dataRef = isrMock.DataRefMock(config=self.mockConfig) 

self.camera = isrMock.IsrMock(config=self.mockConfig).getCamera() 

 

self.inputExp = isrMock.RawMock(config=self.mockConfig).run() 

self.amp = self.inputExp.getDetector()[0] 

self.mi = self.inputExp.getMaskedImage() 

 

def batchSetConfiguration(self, value): 

"""Set the configuration state to a consistent value. 

 

Disable options we do not need as well. 

 

Parameters 

---------- 

value : `bool` 

Value to switch common ISR configuration options to. 

""" 

self.config.qa.flatness.meshX = 20 

self.config.qa.flatness.meshY = 20 

self.config.doWrite = False 

self.config.doLinearize = False 

self.config.doCrosstalk = False 

 

self.config.doConvertIntToFloat = value 

self.config.doSaturation = value 

self.config.doSuspect = value 

self.config.doSetBadRegions = value 

self.config.doOverscan = value 

self.config.doBias = value 

self.config.doVariance = value 

self.config.doWidenSaturationTrails = value 

self.config.doBrighterFatter = value 

self.config.doDefect = value 

self.config.doSaturationInterpolation = value 

self.config.doDark = value 

self.config.doStrayLight = value 

self.config.doFlat = value 

self.config.doFringe = value 

self.config.doAddDistortionModel = value 

self.config.doMeasureBackground = value 

self.config.doVignette = value 

self.config.doAttachTransmissionCurve = value 

self.config.doUseOpticsTransmission = value 

self.config.doUseFilterTransmission = value 

self.config.doUseSensorTransmission = value 

self.config.doUseAtmosphereTransmission = value 

self.config.qa.saveStats = value 

self.config.qa.doThumbnailOss = value 

self.config.qa.doThumbnailFlattened = value 

 

self.config.doApplyGains = not value 

self.config.doCameraSpecificMasking = value 

self.config.vignette.doWriteVignettePolygon = value 

 

def validateIsrResults(self): 

"""results should be a struct with components that are 

not None if included in the configuration file. 

 

Returns 

------- 

results : `pipeBase.Struct` 

Results struct generated from the current ISR configuration. 

""" 

self.task = IsrTask(config=self.config) 

results = self.task.run(self.inputExp, 

camera=self.camera, 

bias=self.dataRef.get("bias"), 

dark=self.dataRef.get("dark"), 

flat=self.dataRef.get("flat"), 

bfKernel=self.dataRef.get("bfKernel"), 

defects=self.dataRef.get("defects"), 

fringes=Struct(fringes=self.dataRef.get("fringe"), seed=1234), 

opticsTransmission=self.dataRef.get("transmission_"), 

filterTransmission=self.dataRef.get("transmission_"), 

sensorTransmission=self.dataRef.get("transmission_"), 

atmosphereTransmission=self.dataRef.get("transmission_") 

) 

 

self.assertIsInstance(results, Struct) 

self.assertIsInstance(results.exposure, afwImage.Exposure) 

return results 

 

def test_overscanCorrection(self): 

"""Expect that this should reduce the image variance with a full fit. 

The default fitType of MEDIAN will reduce the median value. 

 

This needs to operate on a RawMock() to have overscan data to use. 

 

The output types may be different when fitType != MEDIAN. 

""" 

statBefore = computeImageMedianAndStd(self.inputExp.image[self.amp.getRawDataBBox()]) 

 

oscanResults = self.task.overscanCorrection(self.inputExp, self.amp) 

self.assertIsInstance(oscanResults, Struct) 

self.assertIsInstance(oscanResults.imageFit, float) 

self.assertIsInstance(oscanResults.overscanFit, float) 

self.assertIsInstance(oscanResults.overscanImage, afwImage.MaskedImageF) 

 

statAfter = computeImageMedianAndStd(self.inputExp.image[self.amp.getRawDataBBox()]) 

self.assertLess(statAfter[0], statBefore[0]) 

 

def test_runDataRef(self): 

"""Expect a dataRef to be handled correctly. 

""" 

self.config.doLinearize = False 

self.config.doWrite = False 

self.task = IsrTask(config=self.config) 

results = self.task.runDataRef(self.dataRef) 

 

self.assertIsInstance(results, Struct) 

self.assertIsInstance(results.exposure, afwImage.Exposure) 

 

def test_run_allTrue(self): 

"""Expect successful run with expected outputs when all non-exclusive 

configuration options are on. 

 

Output results should be tested more precisely by the 

individual function tests. 

 

""" 

self.batchSetConfiguration(True) 

self.validateIsrResults() 

 

def test_run_allFalse(self): 

"""Expect successful run with expected outputs when all non-exclusive 

configuration options are off. 

 

Output results should be tested more precisely by the 

individual function tests. 

 

""" 

self.batchSetConfiguration(False) 

self.validateIsrResults() 

 

def test_failCases(self): 

"""Expect failure with crosstalk enabled. 

 

Output results should be tested more precisely by the 

individual function tests. 

""" 

self.batchSetConfiguration(True) 

 

# This breaks it 

self.config.doCrosstalk = True 

 

with self.assertRaises(RuntimeError): 

self.validateIsrResults() 

 

def test_maskingCase_noMasking(self): 

"""Test masking cases of configuration parameters. 

""" 

self.batchSetConfiguration(True) 

self.config.overscanFitType = "POLY" 

self.config.overscanOrder = 1 

 

self.config.doSaturation = False 

self.config.doWidenSaturationTrails = False 

self.config.doSaturationInterpolation = False 

self.config.doSuspect = False 

self.config.doSetBadRegions = False 

self.config.doDefect = False 

self.config.doBrighterFatter = False 

 

results = self.validateIsrResults() 

 

self.assertEqual(countMaskedPixels(results.exposure, "SAT"), 0) 

self.assertEqual(countMaskedPixels(results.exposure, "INTRP"), 0) 

self.assertEqual(countMaskedPixels(results.exposure, "SUSPECT"), 0) 

self.assertEqual(countMaskedPixels(results.exposure, "BAD"), 0) 

 

def test_maskingCase_satMasking(self): 

"""Test masking cases of configuration parameters. 

""" 

self.batchSetConfiguration(True) 

self.config.overscanFitType = "POLY" 

self.config.overscanOrder = 1 

 

self.config.saturation = 20000.0 

self.config.doSaturation = True 

self.config.doWidenSaturationTrails = True 

 

self.config.doSaturationInterpolation = False 

self.config.doSuspect = False 

self.config.doSetBadRegions = False 

self.config.doDefect = False 

self.config.doBrighterFatter = False 

 

results = self.validateIsrResults() 

 

self.assertEqual(countMaskedPixels(results.exposure, "SAT"), 0) 

self.assertEqual(countMaskedPixels(results.exposure, "INTRP"), 0) 

self.assertEqual(countMaskedPixels(results.exposure, "SUSPECT"), 0) 

self.assertEqual(countMaskedPixels(results.exposure, "BAD"), 0) 

 

def test_maskingCase_satMaskingAndInterp(self): 

"""Test masking cases of configuration parameters. 

""" 

self.batchSetConfiguration(True) 

self.config.overscanFitType = "POLY" 

self.config.overscanOrder = 1 

 

self.config.saturation = 20000.0 

self.config.doSaturation = True 

self.config.doWidenSaturationTrails = True 

self.config.doSaturationInterpolation = True 

 

self.config.doSuspect = False 

self.config.doSetBadRegions = False 

self.config.doDefect = False 

self.config.doBrighterFatter = False 

 

results = self.validateIsrResults() 

 

self.assertEqual(countMaskedPixels(results.exposure, "SAT"), 0) 

self.assertEqual(countMaskedPixels(results.exposure, "INTRP"), 0) 

self.assertEqual(countMaskedPixels(results.exposure, "SUSPECT"), 0) 

self.assertEqual(countMaskedPixels(results.exposure, "BAD"), 0) 

 

def test_maskingCase_throughEdge(self): 

"""Test masking cases of configuration parameters. 

""" 

self.batchSetConfiguration(True) 

self.config.overscanFitType = "POLY" 

self.config.overscanOrder = 1 

 

self.config.saturation = 20000.0 

self.config.doSaturation = True 

self.config.doWidenSaturationTrails = True 

self.config.doSaturationInterpolation = True 

self.config.numEdgeSuspect = 5 

self.config.doSuspect = True 

 

self.config.doSetBadRegions = False 

self.config.doDefect = False 

self.config.doBrighterFatter = False 

 

results = self.validateIsrResults() 

 

self.assertEqual(countMaskedPixels(results.exposure, "SAT"), 0) 

self.assertEqual(countMaskedPixels(results.exposure, "INTRP"), 0) 

self.assertEqual(countMaskedPixels(results.exposure, "SUSPECT"), 0) 

self.assertEqual(countMaskedPixels(results.exposure, "BAD"), 0) 

 

def test_maskingCase_throughDefects(self): 

"""Test masking cases of configuration parameters. 

""" 

self.batchSetConfiguration(True) 

self.config.overscanFitType = "POLY" 

self.config.overscanOrder = 1 

 

self.config.saturation = 20000.0 

self.config.doSaturation = True 

self.config.doWidenSaturationTrails = True 

self.config.doSaturationInterpolation = True 

self.config.numEdgeSuspect = 5 

self.config.doSuspect = True 

self.config.doDefect = True 

 

self.config.doSetBadRegions = False 

self.config.doBrighterFatter = False 

 

results = self.validateIsrResults() 

 

self.assertEqual(countMaskedPixels(results.exposure, "SAT"), 0) 

self.assertEqual(countMaskedPixels(results.exposure, "INTRP"), 2000) 

self.assertEqual(countMaskedPixels(results.exposure, "SUSPECT"), 3940) 

self.assertEqual(countMaskedPixels(results.exposure, "BAD"), 2000) 

 

def test_maskingCase_throughBad(self): 

"""Test masking cases of configuration parameters. 

""" 

self.batchSetConfiguration(True) 

self.config.overscanFitType = "POLY" 

self.config.overscanOrder = 1 

 

self.config.saturation = 20000.0 

self.config.doSaturation = True 

self.config.doWidenSaturationTrails = True 

self.config.doSaturationInterpolation = True 

 

self.config.doSuspect = True 

self.config.doDefect = True 

self.config.doSetBadRegions = True 

self.config.doBrighterFatter = False 

 

results = self.validateIsrResults() 

 

self.assertEqual(countMaskedPixels(results.exposure, "SAT"), 0) 

self.assertEqual(countMaskedPixels(results.exposure, "INTRP"), 2000) 

self.assertEqual(countMaskedPixels(results.exposure, "SUSPECT"), 0) 

self.assertEqual(countMaskedPixels(results.exposure, "BAD"), 2000) 

 

 

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

pass 

 

 

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

lsst.utils.tests.init() 

unittest.main()