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

# 

# 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.utils.tests 

import lsst.ip.isr as ipIsr 

import lsst.pex.exceptions as pexExcept 

import lsst.ip.isr.isrMock as isrMock 

import lsst.pipe.base as pipeBase 

 

 

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 IsrFunctionsCases(lsst.utils.tests.TestCase): 

"""Test that functions for ISR produce expected outputs. 

""" 

def setUp(self): 

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

self.mi = self.inputExp.getMaskedImage() 

 

def test_transposeMaskedImage(self): 

"""Expect height and width to be exchanged. 

""" 

transposed = ipIsr.transposeMaskedImage(self.mi) 

self.assertEqual(transposed.getImage().getBBox().getHeight(), 

self.mi.getImage().getBBox().getWidth()) 

self.assertEqual(transposed.getImage().getBBox().getWidth(), 

self.mi.getImage().getBBox().getHeight()) 

 

def test_interpolateDefectList(self): 

"""Expect number of interpolated pixels to be non-zero. 

""" 

defectList = isrMock.DefectMock().run() 

self.assertEqual(len(defectList), 1) 

 

for fallbackValue in (None, -999.0): 

for haveMask in (True, False): 

with self.subTest(fallbackValue=fallbackValue, haveMask=haveMask): 

if haveMask is False: 

if 'INTRP' in self.mi.getMask().getMaskPlaneDict(): 

self.mi.getMask().removeAndClearMaskPlane('INTRP') 

else: 

if 'INTRP' not in self.mi.getMask().getMaskPlaneDict(): 

self.mi.getMask().addMaskPlane('INTRP') 

numBit = countMaskedPixels(self.mi, "INTRP") 

self.assertEqual(numBit, 0) 

 

def test_transposeDefectList(self): 

"""Expect bbox dimension values to flip. 

""" 

defectList = isrMock.DefectMock().run() 

transposed = ipIsr.transposeDefectList(defectList) 

 

for d, t in zip(defectList, transposed): 

self.assertEqual(d.getBBox().getDimensions().getX(), t.getBBox().getDimensions().getY()) 

self.assertEqual(d.getBBox().getDimensions().getY(), t.getBBox().getDimensions().getX()) 

 

def test_makeThresholdMask(self): 

"""Expect list of defects to have elements. 

""" 

defectList = ipIsr.makeThresholdMask(self.mi, 200, growFootprints=2, maskName='SAT') 

 

self.assertEqual(len(defectList), 1) 

 

def test_interpolateFromMask(self): 

"""Expect number of interpolated pixels to be non-zero. 

""" 

ipIsr.makeThresholdMask(self.mi, 200, growFootprints=2, maskName='SAT') 

for growFootprints in range(0, 3): 

interpMaskedImage = ipIsr.interpolateFromMask(self.mi, 2.0, 

growFootprints=growFootprints, maskName='SAT') 

numBit = countMaskedPixels(interpMaskedImage, "INTRP") 

self.assertEqual(numBit, 40800, msg=f"interpolateFromMask with growFootprints={growFootprints}") 

 

def test_saturationCorrectionInterpolate(self): 

"""Expect number of mask pixels with SAT marked to be non-zero. 

""" 

corrMaskedImage = ipIsr.saturationCorrection(self.mi, 200, 2.0, 

growFootprints=2, interpolate=True, 

maskName='SAT') 

numBit = countMaskedPixels(corrMaskedImage, "SAT") 

self.assertEqual(numBit, 40800) 

 

def test_saturationCorrectionNoInterpolate(self): 

"""Expect number of mask pixels with SAT marked to be non-zero. 

""" 

corrMaskedImage = ipIsr.saturationCorrection(self.mi, 200, 2.0, 

growFootprints=2, interpolate=False, 

maskName='SAT') 

numBit = countMaskedPixels(corrMaskedImage, "SAT") 

self.assertEqual(numBit, 40800) 

 

def test_trimToMatchCalibBBox(self): 

"""Expect bounding boxes to match. 

""" 

darkExp = isrMock.DarkMock().run() 

darkMi = darkExp.getMaskedImage() 

 

nEdge = 2 

darkMi = darkMi[nEdge:-nEdge, nEdge:-nEdge, afwImage.LOCAL] 

newInput = ipIsr.trimToMatchCalibBBox(self.mi, darkMi) 

 

self.assertEqual(newInput.getImage().getBBox(), darkMi.getImage().getBBox()) 

 

def test_darkCorrection(self): 

"""Expect round-trip application to be equal. 

Expect RuntimeError if sizes are different. 

""" 

darkExp = isrMock.DarkMock().run() 

darkMi = darkExp.getMaskedImage() 

 

mi = self.mi.clone() 

 

# The `invert` parameter controls the direction of the 

# application. This will apply, and un-apply the dark. 

ipIsr.darkCorrection(self.mi, darkMi, 1.0, 1.0, trimToFit=True) 

ipIsr.darkCorrection(self.mi, darkMi, 1.0, 1.0, trimToFit=True, invert=True) 

 

self.assertMaskedImagesAlmostEqual(self.mi, mi, atol=1e-3) 

 

darkMi = darkMi[1:-1, 1:-1, afwImage.LOCAL] 

with self.assertRaises(RuntimeError): 

ipIsr.darkCorrection(self.mi, darkMi, 1.0, 1.0, trimToFit=False) 

 

def test_biasCorrection(self): 

"""Expect smaller median image value after. 

Expect RuntimeError if sizes are different. 

""" 

biasExp = isrMock.BiasMock().run() 

biasMi = biasExp.getMaskedImage() 

 

mi = self.mi.clone() 

ipIsr.biasCorrection(self.mi, biasMi, trimToFit=True) 

self.assertLess(computeImageMedianAndStd(self.mi.getImage())[0], 

computeImageMedianAndStd(mi.getImage())[0]) 

 

biasMi = biasMi[1:-1, 1:-1, afwImage.LOCAL] 

with self.assertRaises(RuntimeError): 

ipIsr.biasCorrection(self.mi, biasMi, trimToFit=False) 

 

def test_flatCorrection(self): 

"""Expect round-trip application to be equal. 

Expect RuntimeError if sizes are different. 

""" 

flatExp = isrMock.FlatMock().run() 

flatMi = flatExp.getMaskedImage() 

 

mi = self.mi.clone() 

for scaling in ('USER', 'MEAN', 'MEDIAN'): 

# The `invert` parameter controls the direction of the 

# application. This will apply, and un-apply the flat. 

ipIsr.flatCorrection(self.mi, flatMi, scaling, userScale=1.0, trimToFit=True) 

ipIsr.flatCorrection(self.mi, flatMi, scaling, userScale=1.0, 

trimToFit=True, invert=True) 

 

self.assertMaskedImagesAlmostEqual(self.mi, mi, atol=1e-3, 

msg=f"flatCorrection with scaling {scaling}") 

 

flatMi = flatMi[1:-1, 1:-1, afwImage.LOCAL] 

with self.assertRaises(RuntimeError): 

ipIsr.flatCorrection(self.mi, flatMi, 'USER', userScale=1.0, trimToFit=False) 

 

def test_flatCorrectionUnknown(self): 

"""Raise if an unknown scaling is used. 

 

The `scaling` parameter must be a known type. If not, the 

flat correction will raise a RuntimeError. 

""" 

flatExp = isrMock.FlatMock().run() 

flatMi = flatExp.getMaskedImage() 

 

with self.assertRaises(RuntimeError): 

ipIsr.flatCorrection(self.mi, flatMi, "UNKNOWN", userScale=1.0, trimToFit=True) 

 

def test_illumCorrection(self): 

"""Expect larger median value after. 

Expect RuntimeError if sizes are different. 

""" 

flatExp = isrMock.FlatMock().run() 

flatMi = flatExp.getMaskedImage() 

 

mi = self.mi.clone() 

ipIsr.illuminationCorrection(self.mi, flatMi, 1.0) 

self.assertGreater(computeImageMedianAndStd(self.mi.getImage())[0], 

computeImageMedianAndStd(mi.getImage())[0]) 

 

flatMi = flatMi[1:-1, 1:-1, afwImage.LOCAL] 

with self.assertRaises(RuntimeError): 

ipIsr.illuminationCorrection(self.mi, flatMi, 1.0) 

 

def test_overscanCorrection_isInt(self): 

"""Expect smaller median/smaller std after. 

Expect exception if overscan fit type isn't known. 

""" 

inputExp = isrMock.RawMock().run() 

 

amp = inputExp.getDetector()[0] 

ampI = inputExp.maskedImage[amp.getRawDataBBox()] 

overscanI = inputExp.maskedImage[amp.getRawHorizontalOverscanBBox()] 

 

for fitType in ('MEAN', 'MEDIAN', 'MEANCLIP', 'POLY', 'CHEB', 

'NATURAL_SPLINE', 'CUBIC_SPLINE', 'UNKNOWN'): 

if fitType in ('NATURAL_SPLINE', 'CUBIC_SPLINE'): 

order = 3 

else: 

order = 1 

 

if fitType == 'UNKNOWN': 

with self.assertRaises(pexExcept.Exception, 

msg=f"overscanCorrection overscanIsInt fitType: {fitType}"): 

ipIsr.overscanCorrection(ampI, overscanI, fitType=fitType, 

order=order, collapseRej=3.0, 

statControl=None, overscanIsInt=True) 

else: 

response = ipIsr.overscanCorrection(ampI, overscanI, fitType=fitType, 

order=order, collapseRej=3.0, 

statControl=None, overscanIsInt=True) 

self.assertIsInstance(response, pipeBase.Struct, 

msg=f"overscanCorrection overscanIsInt Bad response: {fitType}") 

self.assertIsNotNone(response.imageFit, 

msg=f"overscanCorrection overscanIsInt Bad imageFit: {fitType}") 

self.assertIsNotNone(response.overscanFit, 

msg=f"overscanCorrection overscanIsInt Bad overscanFit: {fitType}") 

self.assertIsInstance(response.overscanImage, afwImage.MaskedImageF, 

msg=f"overscanCorrection overscanIsInt Bad overscanImage: {fitType}") 

 

def test_overscanCorrection_isNotInt(self): 

"""Expect smaller median/smaller std after. 

Expect exception if overscan fit type isn't known. 

""" 

inputExp = isrMock.RawMock().run() 

 

amp = inputExp.getDetector()[0] 

ampI = inputExp.maskedImage[amp.getRawDataBBox()] 

overscanI = inputExp.maskedImage[amp.getRawHorizontalOverscanBBox()] 

 

for fitType in ('MEAN', 'MEDIAN', 'MEANCLIP', 'POLY', 'CHEB', 

'NATURAL_SPLINE', 'CUBIC_SPLINE', 'UNKNOWN'): 

if fitType in ('NATURAL_SPLINE', 'CUBIC_SPLINE'): 

order = 3 

else: 

order = 1 

 

if fitType == 'UNKNOWN': 

with self.assertRaises(pexExcept.Exception, 

msg=f"overscanCorrection overscanIsNotInt fitType: {fitType}"): 

ipIsr.overscanCorrection(ampI, overscanI, fitType=fitType, 

order=order, collapseRej=3.0, 

statControl=None, overscanIsInt=False) 

else: 

response = ipIsr.overscanCorrection(ampI, overscanI, fitType=fitType, 

order=order, collapseRej=3.0, 

statControl=None, overscanIsInt=False) 

self.assertIsInstance(response, pipeBase.Struct, 

msg=f"overscanCorrection overscanIsNotInt Bad response: {fitType}") 

self.assertIsNotNone(response.imageFit, 

msg=f"overscanCorrection overscanIsNotInt Bad imageFit: {fitType}") 

self.assertIsNotNone(response.overscanFit, 

msg=f"overscanCorrection overscanIsNotInt Bad overscanFit: {fitType}") 

self.assertIsInstance(response.overscanImage, afwImage.MaskedImageF, 

msg=f"overscanCorrection overscanIsNotInt Bad overscanImage: {fitType}") 

 

def test_brighterFatterCorrection(self): 

"""Expect smoother image/smaller std before. 

""" 

bfKern = isrMock.BfKernelMock().run() 

 

before = computeImageMedianAndStd(self.inputExp.getImage()) 

ipIsr.brighterFatterCorrection(self.inputExp, bfKern, 10, 1e-2, False) 

after = computeImageMedianAndStd(self.inputExp.getImage()) 

 

self.assertLess(before[1], after[1]) 

 

def test_gainContext(self): 

"""Expect image to be unmodified before and after 

""" 

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

with ipIsr.gainContext(self.inputExp, self.inputExp.getImage(), apply=True): 

pass 

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

 

def test_addDistortionModel(self): 

"""Expect RuntimeError if no model supplied, or incomplete exposure information. 

""" 

camera = isrMock.IsrMock().getCamera() 

ipIsr.addDistortionModel(self.inputExp, camera) 

 

with self.assertRaises(RuntimeError): 

ipIsr.addDistortionModel(self.inputExp, None) 

 

self.inputExp.setDetector(None) 

ipIsr.addDistortionModel(self.inputExp, camera) 

 

self.inputExp.setWcs(None) 

ipIsr.addDistortionModel(self.inputExp, camera) 

 

def test_widenSaturationTrails(self): 

"""Expect more mask pixels with SAT set after. 

""" 

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

 

ipIsr.widenSaturationTrails(self.mi.getMask()) 

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

 

self.assertGreaterEqual(numBitAfter, numBitBefore) 

 

def test_setBadRegions(self): 

"""Expect RuntimeError if improper statistic given. 

Expect a float value otherwise. 

""" 

for badStatistic in ('MEDIAN', 'MEANCLIP', 'UNKNOWN'): 

if badStatistic == 'UNKNOWN': 

with self.assertRaises(RuntimeError, 

msg=f"setBadRegions did not fail for stat {badStatistic}"): 

nBad, value = ipIsr.setBadRegions(self.inputExp, badStatistic=badStatistic) 

else: 

nBad, value = ipIsr.setBadRegions(self.inputExp, badStatistic=badStatistic) 

self.assertGreaterEqual(abs(value), 0.0, 

msg=f"setBadRegions did not find valid value for stat {badStatistic}") 

 

def test_attachTransmissionCurve(self): 

"""Expect no failure and non-None output from attachTransmissionCurve. 

""" 

curve = isrMock.TransmissionMock().run() 

combined = ipIsr.attachTransmissionCurve(self.inputExp, 

opticsTransmission=curve, 

filterTransmission=curve, 

sensorTransmission=curve, 

atmosphereTransmission=curve) 

# DM-19707: ip_isr functionality not fully tested by unit tests 

self.assertIsNotNone(combined) 

 

def test_attachTransmissionCurve_None(self): 

"""Expect no failure and non-None output from attachTransmissionCurve. 

""" 

curve = None 

combined = ipIsr.attachTransmissionCurve(self.inputExp, 

opticsTransmission=curve, 

filterTransmission=curve, 

sensorTransmission=curve, 

atmosphereTransmission=curve) 

# DM-19707: ip_isr functionality not fully tested by unit tests 

self.assertIsNotNone(combined) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()