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

# This file is part of afw. 

# 

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

 

""" 

Tests for SpatialCell 

 

Run with: 

python test_spatialCell.py 

or 

pytest test_spatialCell.py 

""" 

import unittest 

 

import lsst.utils.tests 

import lsst.pex.exceptions as pexExcept 

import lsst.geom 

import lsst.afw.math as afwMath 

from lsst.afw.image import LOCAL 

 

 

def getFlux(x): 

return 1000 - 10*x 

 

 

class SpatialCellTestCase(unittest.TestCase): 

"""A test case for SpatialCell""" 

 

def setUp(self): 

candidateList = [] 

self.nCandidate = 5 

for i in (0, 1, 4, 3, 2): # must be all numbers in range(self.nCandidate) 

x, y = i, 5*i 

candidateList.append(afwMath.TestCandidate(x, y, getFlux(x))) 

 

self.cell = afwMath.SpatialCell("Test", lsst.geom.Box2I(), candidateList) 

self.assertEqual(self.cell.getLabel(), "Test") 

 

def tearDown(self): 

del self.cell 

 

def testCandidateList(self): 

"""Check that we can retrieve candidates, and that they are sorted by ranking""" 

self.assertEqual(self.cell[0].getXCenter(), 0) 

self.assertEqual(self.cell[1].getXCenter(), 1) 

self.assertEqual(self.cell[1].getYCenter(), 5) 

 

def testBuildCandidateListByInsertion(self): 

"""Build a candidate list by inserting candidates""" 

 

self.cell = afwMath.SpatialCell("Test", lsst.geom.Box2I()) 

 

for x, y in ([5, 0], [1, 1], [2, 2], [0, 0], [4, 4], [3, 4]): 

self.cell.insertCandidate(afwMath.TestCandidate(x, y, getFlux(x))) 

 

self.assertEqual(self.cell[0].getXCenter(), 0) 

 

def testIterators(self): 

"""Test the SpatialCell iterators""" 

 

# 

# Count the candidates 

# 

self.assertEqual(self.cell.size(), self.nCandidate) 

self.assertEqual(self.cell.end() - self.cell.begin(), self.nCandidate) 

 

ptr = self.cell.begin() 

ptr.__incr__() 

self.assertEqual(self.cell.end() - ptr, self.nCandidate - 1) 

 

self.assertEqual(ptr - self.cell.begin(), 1) 

# 

# Now label one candidate as bad 

# 

self.cell[2].setStatus(afwMath.SpatialCellCandidate.BAD) 

 

self.assertEqual(self.cell.size(), self.nCandidate - 1) 

self.assertEqual(self.cell.end() - self.cell.begin(), 

self.nCandidate - 1) 

 

self.cell.setIgnoreBad(False) 

self.assertEqual(self.cell.size(), self.nCandidate) 

self.assertEqual(self.cell.end() - self.cell.begin(), self.nCandidate) 

 

def testGetCandidateById(self): 

"""Check that we can lookup candidates by ID""" 

id = self.cell[1].getId() 

self.assertEqual(self.cell.getCandidateById(id).getId(), id) 

 

self.assertEqual(self.cell.getCandidateById(-1, True), None) 

with self.assertRaises(pexExcept.NotFoundError): 

self.cell.getCandidateById(-1) 

 

def testSetIteratorBad(self): 

"""Setting a candidate BAD shouldn't stop us seeing the rest of the candidates""" 

i = 0 

for cand in self.cell: 

if i == 1: 

cand.setStatus(afwMath.SpatialCellCandidate.BAD) 

i += 1 

 

self.assertEqual(i, self.nCandidate) 

 

def testSortCandidates(self): 

"""Check that we can update ratings and maintain order""" 

ratings0 = [cand.getCandidateRating() for cand in self.cell] 

# 

# Change a rating 

# 

i, flux = 1, 9999 

self.cell[i].setCandidateRating(flux) 

ratings0[i] = flux 

 

self.assertEqual(ratings0, [cand.getCandidateRating() 

for cand in self.cell]) 

 

self.cell.sortCandidates() 

self.assertNotEqual( 

ratings0, [cand.getCandidateRating() for cand in self.cell]) 

 

def sortKey(a): 

return -a 

self.assertEqual(sorted(ratings0, key=sortKey), 

[cand.getCandidateRating() for cand in self.cell]) 

 

 

class SpatialCellSetTestCase(unittest.TestCase): 

"""A test case for SpatialCellSet""" 

 

def setUp(self): 

self.cellSet = afwMath.SpatialCellSet(lsst.geom.Box2I( 

lsst.geom.Point2I(0, 0), lsst.geom.Extent2I(501, 501)), 260, 200) 

 

def makeTestCandidateCellSet(self): 

"""Populate a SpatialCellSet""" 

 

if False: # Print the bboxes for the cells 

print() 

for i in range(len(self.cellSet.getCellList())): 

cell = self.cellSet.getCellList()[i] 

print(i, "%3d,%3d -- %3d,%3d" % (cell.getBBox().getMinX(), cell.getBBox().getMinY(), 

cell.getBBox().getMaxX(), cell.getBBox().getMaxY()), 

cell.getLabel()) 

self.assertEqual(len(self.cellSet.getCellList()), 6) 

 

# number of candidates 

self.NTestCandidates = 0 

for x, y in ([5, 0], [1, 1], [2, 2], [0, 0], [4, 4], [3, 4]): # all in cell0 

self.cellSet.insertCandidate(afwMath.TestCandidate(x, y, -x)) 

self.NTestCandidates += 1 

 

# in cell1 

self.cellSet.insertCandidate(afwMath.TestCandidate(305, 0, 100)) 

self.NTestCandidates += 1 

# the top right corner of cell5 

self.cellSet.insertCandidate(afwMath.TestCandidate(500, 500, 100)) 

self.NTestCandidates += 1 

 

def tearDown(self): 

del self.cellSet 

 

def testNoCells(self): 

"""Test that we check for a request to make a SpatialCellSet with no cells""" 

def tst(): 

afwMath.SpatialCellSet(lsst.geom.Box2I( 

lsst.geom.Point2I(0, 0), lsst.geom.Extent2I(500, 500)), 0, 3) 

 

self.assertRaises(pexExcept.LengthError, tst) 

 

def testInsertCandidate(self): 

"""Insert candidates into the SpatialCellSet""" 

 

self.makeTestCandidateCellSet() 

 

def tst(): 

# Doesn't fit 

self.cellSet.insertCandidate(afwMath.TestCandidate(501, 501, 100)) 

self.assertRaises(pexExcept.OutOfRangeError, tst) 

# 

# OK, the SpatialCellList is populated 

# 

cell0 = self.cellSet.getCellList()[0] 

self.assertFalse(cell0.empty()) 

self.assertEqual(cell0[0].getXCenter(), 0.0) 

 

self.assertEqual(self.cellSet.getCellList()[1][0].getXCenter(), 305.0) 

 

self.assertTrue(self.cellSet.getCellList()[2].empty()) 

 

def tst1(): 

self.cellSet.getCellList()[2][0] 

self.assertRaises(IndexError, tst1) 

 

def tst2(): 

self.cellSet.getCellList()[2].begin().__deref__() 

self.assertRaises(pexExcept.NotFoundError, tst2) 

 

self.assertFalse(self.cellSet.getCellList()[5].empty()) 

 

def testVisitor(self): 

"""Test the candidate visitors""" 

 

self.makeTestCandidateCellSet() 

 

visitor = afwMath.TestCandidateVisitor() 

 

self.cellSet.visitCandidates(visitor) 

self.assertEqual(visitor.getN(), self.NTestCandidates) 

 

self.cellSet.visitCandidates(visitor, 1) 

self.assertEqual(visitor.getN(), 3) 

 

def testGetCandidateById(self): 

"""Check that we can lookup candidates by ID""" 

 

self.makeTestCandidateCellSet() 

# 

# OK, the SpatialCellList is populated 

# 

id = self.cellSet.getCellList()[0][1].getId() 

self.assertEqual(self.cellSet.getCandidateById(id).getId(), id) 

 

def tst(): 

self.cellSet.getCandidateById(-1) # non-existent ID 

 

self.assertEqual(self.cellSet.getCandidateById(-1, True), None) 

self.assertRaises(pexExcept.NotFoundError, tst) 

 

def testSpatialCell(self): 

dx, dy, sx, sy = 100, 100, 50, 50 

for x0, y0 in [(0, 0), (100, 100)]: 

# only works for tests where dx,dx is some multiple of sx,sy 

assert(dx//sx == float(dx)/float(sx)) 

assert(dy//sy == float(dy)/float(sy)) 

 

bbox = lsst.geom.Box2I(lsst.geom.Point2I(x0, y0), 

lsst.geom.Extent2I(dx, dy)) 

cset = afwMath.SpatialCellSet(bbox, sx, sy) 

for cell in cset.getCellList(): 

label = cell.getLabel() 

nx, ny = [int(z) for z in label.split()[1].split('x')] 

 

cbbox = cell.getBBox() 

 

self.assertEqual(cbbox.getMinX(), nx*sx + x0) 

self.assertEqual(cbbox.getMinY(), ny*sy + y0) 

self.assertEqual(cbbox.getMaxX(), (nx+1)*sx + x0 - 1) 

self.assertEqual(cbbox.getMaxY(), (ny+1)*sy + y0 - 1) 

 

def testSortCandidates(self): 

"""Check that we can update ratings and maintain order""" 

 

self.makeTestCandidateCellSet() 

 

cell1 = self.cellSet.getCellList()[0] 

self.assertFalse(cell1.empty()) 

 

ratings0 = [cand.getCandidateRating() for cand in cell1] 

# 

# Change a rating 

# 

i, flux = 1, 9999 

cell1[i].setCandidateRating(flux) 

ratings0[i] = flux 

 

self.assertEqual( 

ratings0, [cand.getCandidateRating() for cand in cell1]) 

 

self.cellSet.sortCandidates() 

self.assertNotEqual( 

ratings0, [cand.getCandidateRating() for cand in cell1]) 

 

def sortKey(a): 

return -a 

self.assertEqual(sorted(ratings0, key=sortKey), 

[cand.getCandidateRating() for cand in cell1]) 

 

 

class TestImageCandidateCase(unittest.TestCase): 

"""A test case for TestImageCandidate""" 

 

def setUp(self): 

self.cellSet = afwMath.SpatialCellSet(lsst.geom.Box2I( 

lsst.geom.Point2I(0, 0), lsst.geom.Extent2I(501, 501)), 2, 3) 

 

def tearDown(self): 

del self.cellSet 

 

def testInsertCandidate(self): 

"""Test that we can use SpatialCellMaskedImageCandidate""" 

 

flux = 10 

self.cellSet.insertCandidate(afwMath.TestImageCandidate(0, 0, flux)) 

 

cand = self.cellSet.getCellList()[0][0] 

 

width, height = 15, 21 

cand.setWidth(width) 

cand.setHeight(height) 

 

im = cand.getMaskedImage().getImage() 

# This is how TestMaskedImageCandidate sets its pixels 

self.assertEqual(im[0, 0, LOCAL], flux) 

self.assertEqual(im.getWidth(), width) 

self.assertEqual(im.getHeight(), height) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()