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

# 

# LSST Data Management System 

# Copyright 2008-2017 LSST Corporation. 

# 

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

# 

 

# -*- python -*- 

""" 

Tests for Stack 

 

Run with: 

./Stacker.py 

or 

python 

>>> import Stacker; Stacker.run() 

""" 

import unittest 

from functools import reduce 

 

import numpy as np 

 

import lsst.geom 

import lsst.afw.image as afwImage 

import lsst.afw.math as afwMath 

import lsst.utils.tests 

import lsst.pex.exceptions as pexEx 

import lsst.afw.display.ds9 as ds9 

 

display = False 

 

###################################### 

# main body of code 

###################################### 

 

 

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

 

def setUp(self): 

np.random.seed(1) 

self.nImg = 10 

self.nX, self.nY = 64, 64 

self.values = [1.0, 2.0, 2.0, 3.0, 8.0] 

 

def testMean(self): 

""" Test the statisticsStack() function for a MEAN""" 

 

knownMean = 0.0 

imgList = [] 

for iImg in range(self.nImg): 

imgList.append(afwImage.ImageF( 

lsst.geom.Extent2I(self.nX, self.nY), iImg)) 

knownMean += iImg 

 

imgStack = afwMath.statisticsStack(imgList, afwMath.MEAN) 

knownMean /= self.nImg 

self.assertEqual(imgStack.get(self.nX//2, self.nY//2), knownMean) 

 

# Test in-place stacking 

afwMath.statisticsStack(imgStack, imgList, afwMath.MEAN) 

self.assertEqual(imgStack.get(self.nX//2, self.nY//2), knownMean) 

 

def testStatistics(self): 

""" Test the statisticsStack() function """ 

 

imgList = [] 

for val in self.values: 

imgList.append(afwImage.ImageF( 

lsst.geom.Extent2I(self.nX, self.nY), val)) 

 

imgStack = afwMath.statisticsStack(imgList, afwMath.MEAN) 

mean = reduce(lambda x, y: x+y, self.values)/float(len(self.values)) 

self.assertAlmostEqual(imgStack.get(self.nX//2, self.nY//2), mean) 

 

imgStack = afwMath.statisticsStack(imgList, afwMath.MEDIAN) 

median = sorted(self.values)[len(self.values)//2] 

self.assertEqual(imgStack.get(self.nX//2, self.nY//2), median) 

 

def testWeightedStack(self): 

""" Test statisticsStack() function when weighting by a variance plane""" 

 

sctrl = afwMath.StatisticsControl() 

sctrl.setWeighted(True) 

mimgList = [] 

for val in self.values: 

mimg = afwImage.MaskedImageF(lsst.geom.Extent2I(self.nX, self.nY)) 

mimg.set(val, 0x0, val) 

mimgList.append(mimg) 

mimgStack = afwMath.statisticsStack(mimgList, afwMath.MEAN, sctrl) 

 

wvalues = [1.0/q for q in self.values] 

wmean = float(len(self.values)) / reduce(lambda x, y: x + y, wvalues) 

self.assertAlmostEqual( 

mimgStack.getImage().get(self.nX//2, self.nY//2), 

wmean) 

 

# Test in-place stacking 

afwMath.statisticsStack(mimgStack, mimgList, afwMath.MEAN, sctrl) 

self.assertAlmostEqual( 

mimgStack.getImage().get(self.nX//2, self.nY//2), 

wmean) 

 

def testConstantWeightedStack(self): 

""" Test statisticsStack() function when weighting by a vector of weights""" 

 

sctrl = afwMath.StatisticsControl() 

imgList = [] 

weights = [] 

for val in self.values: 

img = afwImage.ImageF(lsst.geom.Extent2I(self.nX, self.nY), val) 

imgList.append(img) 

weights.append(val) 

imgStack = afwMath.statisticsStack( 

imgList, afwMath.MEAN, sctrl, weights) 

 

wsum = reduce(lambda x, y: x + y, self.values) 

wvalues = [x*x for x in self.values] 

wmean = reduce(lambda x, y: x + y, wvalues)/float(wsum) 

self.assertAlmostEqual(imgStack.get(self.nX//2, self.nY//2), wmean) 

 

def testRequestMoreThanOneStat(self): 

""" Make sure we throw an exception if someone requests more than one type of statistics. """ 

 

sctrl = afwMath.StatisticsControl() 

imgList = [] 

for val in self.values: 

img = afwImage.ImageF(lsst.geom.Extent2I(self.nX, self.nY), val) 

imgList.append(img) 

 

def tst(): 

afwMath.statisticsStack( 

imgList, 

afwMath.Property(afwMath.MEAN | afwMath.MEANCLIP), 

sctrl) 

 

self.assertRaises(pexEx.InvalidParameterError, tst) 

 

def testReturnInputs(self): 

""" Make sure that a single file put into the stacker is returned unscathed""" 

 

imgList = [] 

 

img = afwImage.MaskedImageF(lsst.geom.Extent2I(10, 20)) 

for y in range(img.getHeight()): 

simg = img.Factory( 

img, 

lsst.geom.Box2I(lsst.geom.Point2I(0, y), 

lsst.geom.Extent2I(img.getWidth(), 1)), 

afwImage.LOCAL) 

simg.set(y) 

 

imgList.append(img) 

 

imgStack = afwMath.statisticsStack(imgList, afwMath.MEAN) 

 

171 ↛ 172line 171 didn't jump to line 172, because the condition on line 171 was never true if display: 

ds9.mtv(img, frame=1, title="input") 

ds9.mtv(imgStack, frame=2, title="stack") 

 

self.assertEqual(img.get(0, 0)[0], imgStack.get(0, 0)[0]) 

 

def testStackBadPixels(self): 

"""Check that we properly ignore masked pixels, and set noGoodPixelsMask where there are 

no good pixels""" 

mimgVec = [] 

 

DETECTED = afwImage.Mask.getPlaneBitMask("DETECTED") 

EDGE = afwImage.Mask.getPlaneBitMask("EDGE") 

INTRP = afwImage.Mask.getPlaneBitMask("INTRP") 

SAT = afwImage.Mask.getPlaneBitMask("SAT") 

 

sctrl = afwMath.StatisticsControl() 

sctrl.setNanSafe(False) 

sctrl.setAndMask(INTRP | SAT) 

sctrl.setNoGoodPixelsMask(EDGE) 

 

# set these pixels to EDGE 

edgeBBox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), 

lsst.geom.Extent2I(20, 20)) 

width, height = 512, 512 

dim = lsst.geom.Extent2I(width, height) 

val, maskVal = 10, DETECTED 

for i in range(4): 

mimg = afwImage.MaskedImageF(dim) 

mimg.set(val, maskVal, 1) 

# 

# Set part of the image to NaN (with the INTRP bit set) 

# 

llc = lsst.geom.Point2I(width//2*(i//2), height//2*(i % 2)) 

bbox = lsst.geom.Box2I(llc, dim//2) 

 

smimg = mimg.Factory(mimg, bbox, afwImage.LOCAL) 

del smimg 

# 

# And the bottom corner to SAT 

# 

smask = mimg.getMask().Factory(mimg.getMask(), edgeBBox, afwImage.LOCAL) 

smask |= SAT 

del smask 

 

mimgVec.append(mimg) 

 

218 ↛ 219line 218 didn't jump to line 219, because the condition on line 218 was never true if display > 1: 

ds9.mtv(mimg, frame=i, title=str(i)) 

 

mimgStack = afwMath.statisticsStack(mimgVec, afwMath.MEAN, sctrl) 

 

223 ↛ 224line 223 didn't jump to line 224, because the condition on line 223 was never true if display: 

i += 1 

ds9.mtv(mimgStack, frame=i, title="Stack") 

i += 1 

ds9.mtv(mimgStack.getVariance(), frame=i, title="var(Stack)") 

# 

# Check the output, ignoring EDGE pixels 

# 

sctrl = afwMath.StatisticsControl() 

sctrl.setAndMask(afwImage.Mask.getPlaneBitMask("EDGE")) 

 

stats = afwMath.makeStatistics( 

mimgStack, afwMath.MIN | afwMath.MAX, sctrl) 

self.assertEqual(stats.getValue(afwMath.MIN), val) 

self.assertEqual(stats.getValue(afwMath.MAX), val) 

# 

# We have to clear EDGE in the known bad corner to check the mask 

# 

smask = mimgStack.getMask().Factory(mimgStack.getMask(), edgeBBox, afwImage.LOCAL) 

self.assertEqual( 

smask.get(edgeBBox.getMinX(), edgeBBox.getMinY()), EDGE) 

smask &= ~EDGE 

del smask 

 

self.assertEqual( 

afwMath.makeStatistics(mimgStack.getMask(), 

afwMath.SUM, sctrl).getValue(), 

maskVal) 

 

def testTicket1412(self): 

"""Ticket 1412: ignored mask bits are propegated to output stack.""" 

 

mimg1 = afwImage.MaskedImageF(lsst.geom.Extent2I(1, 1)) 

mimg1.set(0, 0, (1, 0x4, 1)) # set 0100 

mimg2 = afwImage.MaskedImageF(lsst.geom.Extent2I(1, 1)) 

mimg2.set(0, 0, (2, 0x3, 1)) # set 0010 and 0001 

 

imgList = [] 

imgList.append(mimg1) 

imgList.append(mimg2) 

 

sctrl = afwMath.StatisticsControl() 

sctrl.setAndMask(0x1) # andmask only 0001 

 

# try first with no sctrl (no andmask set), should see 0x0111 for all 

# output mask pixels 

imgStack = afwMath.statisticsStack(imgList, afwMath.MEAN) 

self.assertEqual(imgStack.get(0, 0)[1], 0x7) 

 

# now try with sctrl (andmask = 0x0001), should see 0x0100 for all 

# output mask pixels 

imgStack = afwMath.statisticsStack(imgList, afwMath.MEAN, sctrl) 

self.assertEqual(imgStack.get(0, 0)[1], 0x4) 

 

def test2145(self): 

"""The how-to-repeat from #2145""" 

Size = 5 

statsCtrl = afwMath.StatisticsControl() 

statsCtrl.setCalcErrorFromInputVariance(True) 

maskedImageList = [] 

weightList = [] 

for i in range(3): 

mi = afwImage.MaskedImageF(Size, Size) 

imArr, maskArr, varArr = mi.getArrays() 

imArr[:] = np.random.normal(10, 0.1, (Size, Size)) 

varArr[:] = np.random.normal(10, 0.1, (Size, Size)) 

maskedImageList.append(mi) 

weightList.append(1.0) 

 

stack = afwMath.statisticsStack( 

maskedImageList, afwMath.MEAN, statsCtrl, weightList) 

if False: 

print("image=", stack.getImage().getArray()) 

print("variance=", stack.getVariance().getArray()) 

self.assertNotEqual(np.sum(stack.getVariance().getArray()), 0.0) 

 

def testRejectedMaskPropagation(self): 

"""Test that we can propagate mask bits from rejected pixels, when the amount 

of rejection crosses a threshold.""" 

rejectedBit = 1 # use this bit to determine whether to reject a pixel 

propagatedBit = 2 # propagate this bit if a pixel with it set is rejected 

statsCtrl = afwMath.StatisticsControl() 

statsCtrl.setMaskPropagationThreshold(propagatedBit, 0.3) 

statsCtrl.setAndMask(1 << rejectedBit) 

statsCtrl.setWeighted(True) 

maskedImageList = [] 

 

# start with 4 images with no mask bits set 

partialSum = np.zeros((1, 4), dtype=np.float32) 

finalImage = np.array([12.0, 12.0, 12.0, 12.0], dtype=np.float32) 

for i in range(4): 

mi = afwImage.MaskedImageF(4, 1) 

imArr, maskArr, varArr = mi.getArrays() 

imArr[:, :] = np.ones((1, 4), dtype=np.float32) 

maskedImageList.append(mi) 

partialSum += imArr 

# add one more image with all permutations of the first two bits set in 

# different pixels 

mi = afwImage.MaskedImageF(4, 1) 

imArr, maskArr, varArr = mi.getArrays() 

imArr[0, :] = finalImage 

maskArr[0, 1] |= (1 << rejectedBit) 

maskArr[0, 2] |= (1 << propagatedBit) 

maskArr[0, 3] |= (1 << rejectedBit) 

maskArr[0, 3] |= (1 << propagatedBit) 

maskedImageList.append(mi) 

 

# these will always be rejected 

finalImage[1] = 0.0 

finalImage[3] = 0.0 

 

# Uniform weights: we should only see pixel 2 set with propagatedBit, because it's not rejected; 

# pixel 3 is rejected, but its weight (0.2) below the propagation 

# threshold (0.3) 

stack1 = afwMath.statisticsStack(maskedImageList, afwMath.MEAN, statsCtrl, [ 

1.0, 1.0, 1.0, 1.0, 1.0]) 

self.assertEqual(stack1.get(0, 0)[1], 0x0) 

self.assertEqual(stack1.get(1, 0)[1], 0x0) 

self.assertEqual(stack1.get(2, 0)[1], 1 << propagatedBit) 

self.assertEqual(stack1.get(3, 0)[1], 0x0) 

self.assertFloatsAlmostEqual(stack1.getImage().getArray(), 

(partialSum + finalImage) / np.array([5.0, 4.0, 5.0, 4.0]), rtol=1E-7) 

 

# Give the masked image more weight: we should see pixel 2 and pixel 3 set with propagatedBit, 

# pixel 2 because it's not rejected, and pixel 3 because the weight of the rejection (0.3333) 

# is above the threshold (0.3) 

# Note that rejectedBit is never propagated, because we didn't include it in statsCtrl (of course, 

# normally the bits we'd propagate and the bits we'd reject would be 

# the same) 

stack2 = afwMath.statisticsStack(maskedImageList, afwMath.MEAN, statsCtrl, [ 

1.0, 1.0, 1.0, 1.0, 2.0]) 

self.assertEqual(stack2.get(0, 0)[1], 0x0) 

self.assertEqual(stack2.get(1, 0)[1], 0x0) 

self.assertEqual(stack2.get(2, 0)[1], 1 << propagatedBit) 

self.assertEqual(stack2.get(3, 0)[1], 1 << propagatedBit) 

self.assertFloatsAlmostEqual(stack2.getImage().getArray(), 

(partialSum + 2*finalImage) / np.array([6.0, 4.0, 6.0, 4.0]), rtol=1E-7) 

 

def testClipped(self): 

"""Test that we set mask bits when pixels are clipped""" 

box = lsst.geom.Box2I(lsst.geom.Point2I(12345, 67890), lsst.geom.Extent2I(3, 3)) 

num = 10 

maskVal = 0xAD 

value = 0.0 

 

images = [afwImage.MaskedImageF(box) for _ in range(num)] 

statsCtrl = afwMath.StatisticsControl() 

statsCtrl.setAndMask(maskVal) 

clipped = 1 << afwImage.Mask().addMaskPlane("CLIPPED") 

 

# No clipping: check that vanilla is working 

for img in images: 

img.getImage().set(value) 

img.getMask().set(0) 

stack = afwMath.statisticsStack(images, afwMath.MEANCLIP, clipped=clipped) 

self.assertFloatsAlmostEqual(stack.getImage().getArray(), 0.0, atol=0.0) 

self.assertFloatsAlmostEqual(stack.getMask().getArray(), 0, atol=0.0) # Not floats, but that's OK 

 

# Clip a pixel; the CLIPPED bit should be set 

images[0].getImage().set(1, 1, value + 1.0) 

stack = afwMath.statisticsStack(images, afwMath.MEANCLIP, clipped=clipped) 

self.assertFloatsAlmostEqual(stack.getImage().getArray(), 0.0, atol=0.0) 

self.assertEqual(stack.getMask().get(1, 1), clipped) 

 

# Mask a pixel; the CLIPPED bit should be set 

images[0].getMask().set(1, 1, maskVal) 

stack = afwMath.statisticsStack(images, afwMath.MEAN, statsCtrl, clipped=clipped) 

self.assertFloatsAlmostEqual(stack.getImage().getArray(), 0.0, atol=0.0) 

self.assertEqual(stack.getMask().get(1, 1), clipped) 

 

# Excuse that mask; the CLIPPED bit should not be set 

stack = afwMath.statisticsStack(images, afwMath.MEAN, statsCtrl, clipped=clipped, excuse=maskVal) 

self.assertFloatsAlmostEqual(stack.getImage().getArray(), 0.0, atol=0.0) 

self.assertEqual(stack.getMask().get(1, 1), 0) 

 

# Map that mask value to a different one. 

rejected = 1 << afwImage.Mask().addMaskPlane("REJECTED") 

maskMap = [(maskVal, rejected)] 

images[0].getMask().set(1, 1, 0) # only want to clip, not mask, this one 

images[1].getMask().set(1, 2, maskVal) # only want to mask, not clip, this one 

stack = afwMath.statisticsStack(images, afwMath.MEANCLIP, statsCtrl, wvector=[], clipped=clipped, 

maskMap=maskMap) 

self.assertFloatsAlmostEqual(stack.getImage().getArray(), 0.0, atol=0.0) 

self.assertEqual(stack.getMask().get(1, 1), clipped) 

self.assertEqual(stack.getMask().get(1, 2), rejected) 

 

################################################################# 

# Test suite boiler plate 

################################################################# 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()