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

# This file is part of meas_algorithms. 

# 

# 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 os 

import unittest 

import math 

import numpy as np 

 

import lsst.geom 

import lsst.afw.image as afwImage 

import lsst.meas.algorithms as algorithms 

import lsst.meas.algorithms.defects as defects 

import lsst.utils.tests 

from lsst.daf.base import PropertyList 

 

try: 

type(display) 

except NameError: 

display = False 

else: 

import lsst.afw.display as afwDisplay 

afwDisplay.setDefaultMaskTransparency(75) 

 

# Determine if we have afwdata 

try: 

afwdataDir = lsst.utils.getPackageDir('afwdata') 

except Exception: 

afwdataDir = None 

 

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

 

 

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

"""Tests for collections of Defect.""" 

 

def test_defects(self): 

defects = algorithms.Defects() 

 

defects.append(algorithms.Defect(lsst.geom.Box2I(lsst.geom.Point2I(5, 6), 

lsst.geom.Point2I(41, 50)))) 

 

defects.append(lsst.geom.Box2I(lsst.geom.Point2I(0, 0), 

lsst.geom.Point2I(4, 5))) 

defects.append(lsst.geom.Point2I(10, 12)) 

defects.append(afwImage.DefectBase(lsst.geom.Box2I(lsst.geom.Point2I(100, 200), 

lsst.geom.Extent2I(5, 5)))) 

self.assertEqual(len(defects), 4) 

 

for d in defects: 

self.assertIsInstance(d, algorithms.Defect) 

 

# Transposition 

transposed = defects.transpose() 

self.assertEqual(len(transposed), len(defects)) 

self.assertEqual(transposed[0].getBBox(), 

lsst.geom.Box2I(lsst.geom.Point2I(6, 5), 

lsst.geom.Extent2I(45, 37))) 

 

# Serialization round trip 

meta = PropertyList() 

meta["TESTHDR"] = "testing" 

defects.setMetadata(meta) 

 

table = defects.toTable() 

defects2 = algorithms.Defects.fromTable(table) 

self.assertEqual(defects2, defects) 

 

# via FITS 

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

defects.writeFits(tmpFile) 

defects2 = algorithms.Defects.readFits(tmpFile) 

 

# This tests the bounding boxes so metadata is tested separately. 

self.assertEqual(defects2, defects) 

 

# Must strip out DATE metadata before comparison 

meta2 = defects2.getMetadata() 

for k in ("DATE", "CALIB_CREATION_DATE", "CALIB_CREATION_TIME"): 

del meta2[k] 

 

self.assertEqual(defects2.getMetadata(), defects.getMetadata()) 

meta2["NEW"] = "additional header" 

self.assertNotEqual(defects2.getMetadata(), defects.getMetadata()) 

 

# Check bad values 

with self.assertRaises(ValueError): 

defects.append(lsst.geom.Box2D(lsst.geom.Point2D(0., 0.), 

lsst.geom.Point2D(3.1, 3.1))) 

with self.assertRaises(ValueError): 

defects.append("defect") 

 

def testAstropyRegion(self): 

"""Read a FITS region file created by Astropy regions.""" 

 

with self.assertLogs(): 

defects = algorithms.Defects.readFits(os.path.join(TESTDIR, "data", "fits_region.fits")) 

 

# Should be able to read 3 regions from the file 

self.assertEqual(len(defects), 3) 

 

def testLsstTextfile(self): 

with lsst.utils.tests.getTempFilePath(".txt") as tmpFile: 

with open(tmpFile, "w") as fh: 

print("""# X0 Y0 width height 

996 0 56 24 

0 4156 2048 20 

0 0 17 4176 

1998 4035 50 141 

1023 0 2 4176 

2027 0 21 4176 

0 4047 37 129 

# Some rows without fixed column widths 

14 20 2000 50 

10 10 10 10 

""", file=fh) 

 

defects = algorithms.Defects.readLsstDefectsFile(tmpFile) 

 

self.assertEqual(len(defects), 9) 

self.assertEqual(defects[3].getBBox(), lsst.geom.Box2I(lsst.geom.Point2I(1998, 4035), 

lsst.geom.Extent2I(50, 141))) 

 

 

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

"""A test case for interpolation.""" 

 

def setUp(self): 

self.FWHM = 5 

self.psf = algorithms.DoubleGaussianPsf(15, 15, self.FWHM/(2*math.sqrt(2*math.log(2)))) 

maskedImageFile = os.path.join(afwdataDir, "CFHT", "D4", "cal-53535-i-797722_1.fits") 

 

self.mi = afwImage.MaskedImageF(maskedImageFile) 

if False: # use sub-image? 

self.mi = self.mi.Factory(self.mi, afwImage.BBox(afwImage.PointI(760, 20), 256, 256)) 

self.mi.getMask().addMaskPlane("INTERP") 

 

measAlgorithmsDir = lsst.utils.getPackageDir('meas_algorithms') 

self.badPixels = defects.policyToBadRegionList( 

os.path.join(measAlgorithmsDir, "policy", "BadPixels.paf")) 

 

def tearDown(self): 

del self.mi 

del self.psf 

del self.badPixels 

 

@unittest.skipUnless(afwdataDir, "afwdata not available") 

def testDetection(self): 

"""Test Interp algorithms.""" 

 

if display: 

frame = 0 

afwDisplay.Display(frame=frame).mtv(self.mi, title=self._testMethodName + ": Original") 

 

algorithms.interpolateOverDefects(self.mi, self.psf, self.badPixels) 

 

if display: 

frame += 1 

afwDisplay.Display(frame=frame).mtv(self.mi, title=self._testMethodName + ": Interpolated") 

frame += 1 

afwDisplay.Display(frame=frame).mtv(self.mi.getVariance(), 

title=self._testMethodName + ": Variance") 

 

@unittest.skipUnless(afwdataDir, "afwdata not available") 

def test818(self): 

"""A test case for #818; the full test is in /lsst/DC3root/ticketFiles/818""" 

 

badPixels = [] 

defects = [((82, 663), 6, 8), 

((83, 659), 9, 6), 

((85, 660), 10, 11), 

((87, 669), 3, 3), 

] 

 

for xy0, width, height in defects: 

x0, y0 = xy0 

bbox = lsst.geom.BoxI(lsst.geom.PointI(x0, y0), lsst.geom.ExtentI(width, height)) 

badPixels.append(algorithms.Defect(bbox)) 

 

mi = afwImage.MaskedImageF(517, 800) 

 

algorithms.interpolateOverDefects(mi, self.psf, badPixels) 

 

@unittest.skipUnless(afwdataDir, "afwdata not available") 

def test1295(self): 

"""A test case for #1295 (failure to interpolate over groups of defects.""" 

im = afwImage.ImageF(lsst.geom.ExtentI(100, 100)) 

mi = afwImage.makeMaskedImage(im) 

mi.set(100) 

flat = afwImage.ImageF(im.getDimensions()) 

flat.set(1) 

flat[50:51, :, afwImage.LOCAL] = 0.0 

flat[55:56, :, afwImage.LOCAL] = 0.0 

flat[58:59, :, afwImage.LOCAL] = 0.0 

flat[51:60, 51:, afwImage.LOCAL] = 0.0 

 

mi /= flat 

 

if display: 

afwDisplay.Display(frame=0).mtv(mi, title=self._testMethodName + ": Raw") 

 

defectList = algorithms.Defects() 

bbox = lsst.geom.BoxI(lsst.geom.PointI(50, 0), lsst.geom.ExtentI(1, 100)) 

defectList.append(algorithms.Defect(bbox)) 

bbox = lsst.geom.BoxI(lsst.geom.PointI(55, 0), lsst.geom.ExtentI(1, 100)) 

defectList.append(algorithms.Defect(bbox)) 

bbox = lsst.geom.BoxI(lsst.geom.PointI(58, 0), lsst.geom.ExtentI(1, 100)) 

defectList.append(algorithms.Defect(bbox)) 

bbox = lsst.geom.BoxI(lsst.geom.PointI(51, 51), lsst.geom.ExtentI(9, 49)) 

defectList.append(algorithms.Defect(bbox)) 

 

psf = algorithms.DoubleGaussianPsf(15, 15, 1./(2*math.sqrt(2*math.log(2)))) 

algorithms.interpolateOverDefects(mi, psf, defectList, 50.) 

 

if display: 

afwDisplay.Display(frame=1).mtv(mi, title=self._testMethodName + ": Interpolated") 

 

self.assertTrue(np.isfinite(mi.image[56, 51, afwImage.LOCAL])) 

 

@unittest.skipUnless(afwdataDir, "afwdata not available") 

def testEdge(self): 

"""Test that we can interpolate to the edge""" 

mi = afwImage.MaskedImageF(80, 30) 

 

ima = mi.getImage().getArray() 

# 

# Loop over number of bad columns at left or right edge of image 

# 

for nBadCol in range(0, 20): 

mi.set((0, 0x0, 0)) 

 

np.random.seed(666) 

ima[:] = np.random.uniform(-1, 1, ima.shape) 

 

defects = [] 

 

if nBadCol > 0: 

# 

# Bad left edge 

# 

ima[:, 0:nBadCol] = 10 

defects.append(lsst.geom.BoxI(lsst.geom.PointI(0, 0), 

lsst.geom.ExtentI(nBadCol, mi.getHeight()))) 

# 

# With another bad set of columns next to bad left edge 

# 

ima[:, -nBadCol:] = 10 

defects.append(lsst.geom.BoxI(lsst.geom.PointI(mi.getWidth() - nBadCol, 0), 

lsst.geom.ExtentI(nBadCol, mi.getHeight()))) 

# 

# Bad right edge 

# 

ima[0:10, nBadCol+1:nBadCol+4] = 100 

defects.append(lsst.geom.BoxI(lsst.geom.PointI(nBadCol+1, 0), 

lsst.geom.ExtentI(3, 10))) 

# 

# With another bad set of columns next to bad right edge 

# 

ima[0:10, -nBadCol-4:-nBadCol-1] = 100 

defects.append((lsst.geom.BoxI(lsst.geom.PointI(mi.getWidth() - nBadCol - 4, 0), 

lsst.geom.ExtentI(3, 10)))) 

# 

# Test cases that left and right bad patches nearly (or do) coalesce 

# 

ima[-3:, 0:mi.getWidth()//2-1] = 100 

defects.append(lsst.geom.BoxI(lsst.geom.PointI(0, mi.getHeight() - 3), 

lsst.geom.ExtentI(mi.getWidth()//2-1, 1))) 

 

ima[-3:, mi.getWidth()//2+1:] = 100 

defects.append(lsst.geom.BoxI(lsst.geom.PointI(mi.getWidth()//2 + 1, mi.getHeight() - 3), 

lsst.geom.ExtentI(mi.getWidth()//2 - 1, 1))) 

 

ima[-2:, 0:mi.getWidth()//2] = 100 

defects.append(lsst.geom.BoxI(lsst.geom.PointI(0, mi.getHeight() - 2), 

lsst.geom.ExtentI(mi.getWidth()//2, 1))) 

 

ima[-2:, mi.getWidth()//2+1:] = 100 

defects.append(lsst.geom.BoxI(lsst.geom.PointI(mi.getWidth()//2 + 1, mi.getHeight() - 2), 

lsst.geom.ExtentI(mi.getWidth()//2 - 1, 1))) 

 

ima[-1:, :] = 100 

defects.append(lsst.geom.BoxI(lsst.geom.PointI(0, mi.getHeight() - 1), 

lsst.geom.ExtentI(mi.getWidth(), 1))) 

 

# Test fix for HSC-978: long defect stops one pixel shy of the edge (when nBadCol == 0) 

ima[13, :-1] = 100 

defects.append(lsst.geom.BoxI(lsst.geom.PointI(0, 13), lsst.geom.ExtentI(mi.getWidth() - 1, 1))) 

ima[14, 1:] = 100 

defects.append(lsst.geom.BoxI(lsst.geom.PointI(1, 14), lsst.geom.ExtentI(mi.getWidth() - 1, 1))) 

 

# 

# Build list of defects to interpolate over 

# 

defectList = algorithms.Defects() 

 

for bbox in defects: 

defectList.append(algorithms.Defect(bbox)) 

# 

# Guess a PSF and do the work 

# 

if display: 

afwDisplay.Display(frame=2).mtv(mi, title=self._testMethodName + ": image") 

 

psf = algorithms.DoubleGaussianPsf(15, 15, 1./(2*math.sqrt(2*math.log(2)))) 

algorithms.interpolateOverDefects(mi, psf, defectList, 0, True) 

 

if display: 

afwDisplay.Display(frame=3).mtv(mi, title=self._testMethodName + ": image") 

 

self.assertGreater(np.min(ima), -2) 

self.assertGreater(2, np.max(ima)) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()