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

# 

# This file is part of ap_verify. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (http://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 <http://www.gnu.org/licenses/>. 

# 

 

import unittest 

from unittest.mock import NonCallableMock 

 

import astropy.units as u 

import numpy as np 

import os 

import sqlite3 

 

import lsst.daf.persistence as dafPersist 

import lsst.afw.geom as afwGeom 

import lsst.afw.table as afwTable 

from lsst.ap.association import \ 

make_minimal_dia_source_schema, \ 

make_minimal_dia_object_schema, \ 

AssociationTask 

import lsst.pipe.base as pipeBase 

import lsst.utils.tests 

from lsst.verify import Measurement 

from lsst.ap.verify.measurements.association import \ 

measureNumberNewDiaObjects, \ 

measureNumberUnassociatedDiaObjects, \ 

measureFractionUpdatedDiaObjects, \ 

measureNumberSciSources, \ 

measureFractionDiaSourcesToSciSources, \ 

measureTotalUnassociatedDiaObjects 

 

# Define the root of the tests relative to this file 

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

 

# Define a generic dataId 

dataIdDict = {'visit': 1111, 

'filter': 'r'} 

 

 

def createTestPoints(pointLocsDeg, 

startId=0, 

schema=None, 

scatterArcsec=1.0, 

indexerIds=None, 

associatedIds=None): 

"""Create dummy DIASources or DIAObjects for use in our tests. 

 

Parameters 

---------- 

pointLocsDeg : array-like (N, 2) of `float`s 

Positions of the test points to create in RA, DEC. 

startId : `int` 

Unique id of the first object to create. The remaining sources are 

incremented by one from the first id. 

schema : `lsst.afw.table.Schema` 

Schema of the objects to create. Defaults to the DIASource schema. 

scatterArcsec : `float` 

Scatter to add to the position of each DIASource. 

indexerIds : `list` of `ints`s 

Id numbers of pixelization indexer to store. Must be the same length 

as the first dimension of point_locs_deg. 

associatedIds : `list` of `ints`s 

Id numbers of associated DIAObjects to store. Must be the same length 

as the first dimension of point_locs_deg. 

 

Returns 

------- 

testPoints : `lsst.afw.table.SourceCatalog` 

Catalog of points to test. 

""" 

if schema is None: 

schema = make_minimal_dia_source_schema() 

sources = afwTable.SourceCatalog(schema) 

 

for src_idx, (ra, dec,) in enumerate(pointLocsDeg): 

src = sources.addNew() 

src['id'] = src_idx + startId 

coord = afwGeom.SpherePoint(ra, dec, afwGeom.degrees) 

if scatterArcsec > 0.0: 

coord = coord.offset( 

np.random.rand() * 360 * afwGeom.degrees, 

np.random.rand() * scatterArcsec * afwGeom.arcseconds) 

if indexerIds is not None: 

src['pixelId'] = indexerIds[src_idx] 

if associatedIds is not None: 

src['diaObjectId'] = associatedIds[src_idx] 

src.setCoord(coord) 

 

return sources 

 

 

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

 

def setUp(self): 

 

# Create an unrun AssociationTask. 

self.assocTask = AssociationTask() 

 

# Create a empty butler repository and put data in it. 

self.numTestSciSources = 10 

self.numTestDiaSources = 5 

testSources = createTestPoints( 

pointLocsDeg=[[idx, idx] for idx in 

range(self.numTestSciSources)]) 

testDiaSources = createTestPoints( 

pointLocsDeg=[[idx, idx] for idx in 

range(self.numTestDiaSources)]) 

 

# Fake Butler to avoid initialization and I/O overhead 

def mockGet(datasetType, dataId=None): 

"""An emulator for `lsst.daf.persistence.Butler.get` that can only handle test data. 

""" 

# Check whether dataIdDict is a subset of dataId 

if dataIdDict.items() <= dataId.items(): 

if datasetType == 'src': 

return testSources 

elif datasetType == 'deepDiff_diaSrc': 

return testDiaSources 

raise dafPersist.NoResults("Dataset not found:", datasetType, dataId) 

self.butler = NonCallableMock(spec=dafPersist.Butler, get=mockGet) 

 

self.numTestDiaObjects = 5 

self.diaObjects = createTestPoints( 

pointLocsDeg=[[idx, idx] for idx in 

range(self.numTestDiaObjects)], 

schema=make_minimal_dia_object_schema(['r'])) 

for diaObject in self.diaObjects: 

diaObject['nDiaSources'] = 1 

 

def tearDown(self): 

del self.assocTask 

 

if hasattr(self, "butler"): 

del self.butler 

 

def testValidFromMetadata(self): 

"""Verify that association information can be recovered from metadata. 

""" 

# Insert data into the task metadata. 

nUpdatedDiaObjects = 5 

nNewDiaObjects = 6 

nUnassociatedDiaObjects = 7 

testAssocResult = pipeBase.Struct( 

n_updated_dia_objects=nUpdatedDiaObjects, 

n_new_dia_objects=nNewDiaObjects, 

n_unassociated_dia_objects=nUnassociatedDiaObjects,) 

self.assocTask._add_association_meta_data(testAssocResult) 

metadata = self.assocTask.getFullMetadata() 

 

meas = measureNumberNewDiaObjects( 

metadata, 

"association", 

"association.numNewDIAObjects") 

self.assertIsInstance(meas, Measurement) 

self.assertEqual( 

meas.metric_name, 

lsst.verify.Name(metric="association.numNewDIAObjects")) 

self.assertEqual(meas.quantity, nNewDiaObjects * u.count) 

 

meas = measureNumberUnassociatedDiaObjects( 

metadata, 

'association', 

'association.fracUpdatedDIAObjects') 

self.assertIsInstance(meas, Measurement) 

self.assertEqual( 

meas.metric_name, 

lsst.verify.Name( 

metric='association.fracUpdatedDIAObjects')) 

self.assertEqual(meas.quantity, nUnassociatedDiaObjects * u.count) 

 

meas = measureFractionUpdatedDiaObjects( 

metadata, 

'association', 

'association.fracUpdatedDIAObjects') 

self.assertIsInstance(meas, Measurement) 

self.assertEqual( 

meas.metric_name, 

lsst.verify.Name(metric='association.fracUpdatedDIAObjects')) 

self.assertEqual(meas.quantity, 

nUpdatedDiaObjects / 

(nUpdatedDiaObjects + nUnassociatedDiaObjects) * 

u.dimensionless_unscaled) 

 

def testValidFromButler(self): 

""" Test the association measurements that require a butler. 

""" 

meas = measureNumberSciSources( 

self.butler, 

dataIdDict=dataIdDict, 

metricName='ip_diffim.numSciSrc') 

self.assertIsInstance(meas, Measurement) 

self.assertEqual( 

meas.metric_name, 

lsst.verify.Name(metric='ip_diffim.numSciSrc')) 

self.assertEqual(meas.quantity, self.numTestSciSources * u.count) 

 

meas = measureFractionDiaSourcesToSciSources( 

self.butler, 

dataIdDict=dataIdDict, 

metricName='ip_diffim.fracDiaSrcToSciSrc') 

self.assertIsInstance(meas, Measurement) 

self.assertEqual( 

meas.metric_name, 

lsst.verify.Name(metric='ip_diffim.fracDiaSrcToSciSrc')) 

self.assertEqual(meas.quantity, 

self.numTestDiaSources / self.numTestSciSources * u.dimensionless_unscaled) 

 

def testValidFromSqlite(self): 

# Fake DB handle to avoid DB initialization overhead 

cursor = NonCallableMock(spec=sqlite3.Cursor) 

cursor.fetchall.return_value = [(len(self.diaObjects),)] 

 

meas = measureTotalUnassociatedDiaObjects( 

cursor, 

metricName='association.numTotalUnassociatedDiaObjects') 

self.assertIsInstance(meas, Measurement) 

self.assertEqual( 

meas.metric_name, 

lsst.verify.Name( 

metric='association.numTotalUnassociatedDiaObjects')) 

self.assertEqual(meas.quantity, self.numTestDiaObjects * u.count) 

 

def testNoButlerData(self): 

""" Test attempting to create a measurement with data that the butler 

does not contain. 

""" 

 

with self.assertRaises(dafPersist.NoResults): 

measureNumberSciSources( 

self.butler, 

dataIdDict={'visit': 1000, 'filter': 'r'}, 

metricName='ip_diffim.fracDiaSrcToSciSrc') 

 

with self.assertRaises(dafPersist.NoResults): 

measureFractionDiaSourcesToSciSources( 

self.butler, 

dataIdDict={'visit': 1000, 'filter': 'r'}, 

metricName='ip_diffim.fracDiaSrcToSciSrc') 

 

with self.assertRaises(dafPersist.NoResults): 

measureNumberSciSources( 

self.butler, 

dataIdDict={'visit': 1111, 'filter': 'g'}, 

metricName='ip_diffim.fracDiaSrcToSciSrc') 

 

with self.assertRaises(dafPersist.NoResults): 

measureFractionDiaSourcesToSciSources( 

self.butler, 

dataIdDict={'visit': 1111, 'filter': 'g'}, 

metricName='ip_diffim.fracDiaSrcToSciSrc') 

 

def testMetadataNotCreated(self): 

""" Test for the correct failure when measuring from non-existent 

metadata. 

""" 

metadata = self.assocTask.getFullMetadata() 

 

meas = measureNumberNewDiaObjects( 

metadata, 

"association", 

"association.numNewDIAObjects") 

self.assertIsNone(meas) 

 

def testNoMetric(self): 

"""Verify that trying to measure a nonexistent metric fails. 

""" 

testAssocResult = pipeBase.Struct( 

n_updated_dia_objects=5, 

n_new_dia_objects=6, 

n_unassociated_dia_objects=7,) 

self.assocTask._add_association_meta_data(testAssocResult) 

metadata = self.assocTask.getFullMetadata() 

with self.assertRaises(TypeError): 

measureNumberNewDiaObjects( 

metadata, "association", "foo.bar.FooBar") 

with self.assertRaises(TypeError): 

measureNumberUnassociatedDiaObjects( 

metadata, "association", "foo.bar.FooBar") 

with self.assertRaises(TypeError): 

measureFractionUpdatedDiaObjects( 

metadata, "association", "foo.bar.FooBar") 

 

with self.assertRaises(TypeError): 

measureNumberSciSources( 

self.butler, dataId=dataIdDict, 

metricName='foo.bar.FooBar') 

with self.assertRaises(TypeError): 

measureFractionDiaSourcesToSciSources( 

self.butler, dataId=dataIdDict, 

metricName='foo.bar.FooBar') 

 

# Fake DB handle to avoid DB initialization overhead 

cursor = NonCallableMock(spec=sqlite3.Cursor) 

cursor.fetchall.return_value = [(0,)] 

with self.assertRaises(TypeError): 

measureTotalUnassociatedDiaObjects( 

cursor, metricName='foo.bar.FooBar') 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()