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

# 

# LSST Data Management System 

# 

# Copyright 2008-2016 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 os 

import unittest 

from collections import Counter 

 

import astropy.time 

import numpy as np 

 

import lsst.geom 

import lsst.afw.table as afwTable 

import lsst.afw.geom as afwGeom 

import lsst.daf.persistence as dafPersist 

from lsst.meas.algorithms import (IngestIndexedReferenceTask, LoadIndexedReferenceObjectsTask, 

LoadIndexedReferenceObjectsConfig, getRefFluxField) 

from lsst.meas.algorithms.loadReferenceObjects import hasNanojanskyFluxUnits 

import lsst.utils 

 

import ingestIndexTestBase 

 

REGENERATE_COMPARISON = False # Regenerate comparison data? 

 

 

def make_coord(ra, dec): 

"""Make an ICRS coord given its RA, Dec in degrees.""" 

return lsst.geom.SpherePoint(ra, dec, lsst.geom.degrees) 

 

 

class HtmIndexTestCase(ingestIndexTestBase.IngestIndexCatalogTestBase, lsst.utils.tests.TestCase): 

"""Tests of ingesting and validating an HTM Indexed Reference Catalog. 

""" 

def testSanity(self): 

"""Sanity-check that compCats contains some entries with sources.""" 

numWithSources = 0 

for idList in self.compCats.values(): 

if len(idList) > 0: 

numWithSources += 1 

self.assertGreater(numWithSources, 0) 

 

def testAgainstPersisted(self): 

shardId = 2222 

dataset_name = IngestIndexedReferenceTask.ConfigClass().dataset_config.ref_dataset_name 

dataId = self.indexer.makeDataId(shardId, dataset_name) 

self.assertTrue(self.testButler.datasetExists('ref_cat', dataId)) 

refCat = self.testButler.get('ref_cat', dataId) 

if REGENERATE_COMPARISON: 

if os.path.exists(self.testCatPath): 

os.unlink(self.testCatPath) 

refCat.writeFits(self.testCatPath) 

self.fail("New comparison data written; unset REGENERATE_COMPARISON in order to proceed") 

 

ex1 = refCat.extract('*') 

testCat = afwTable.SimpleCatalog.readFits(self.testCatPath) 

 

ex2 = testCat.extract('*') 

self.assertEqual(set(ex1.keys()), set(ex2.keys())) 

for kk in ex1: 

np.testing.assert_array_almost_equal(ex1[kk], ex2[kk], ) 

 

def testValidateRaDecMag(self): 

config = self.makeConfig() 

config.validate() 

 

for name in ("ra_name", "dec_name", "mag_column_list"): 

with self.subTest(name=name): 

config = self.makeConfig() 

setattr(config, name, None) 

with self.assertRaises(ValueError): 

config.validate() 

 

def testValidateRaDecErr(self): 

config = self.makeConfig(withRaDecErr=True) 

config.validate() 

 

for name in ("ra_err_name", "dec_err_name"): 

with self.subTest(name=name): 

config = self.makeConfig(withRaDecErr=True) 

setattr(config, name, None) 

with self.assertRaises(ValueError): 

config.validate() 

 

def testValidateMagErr(self): 

config = self.makeConfig(withMagErr=True) 

config.validate() 

 

# test for missing names 

for name in config.mag_column_list: 

with self.subTest(name=name): 

config = self.makeConfig(withMagErr=True) 

del config.mag_err_column_map[name] 

with self.assertRaises(ValueError): 

config.validate() 

 

# test for incorrect names 

for name in config.mag_column_list: 

with self.subTest(name=name): 

config = self.makeConfig(withMagErr=True) 

config.mag_err_column_map["badName"] = config.mag_err_column_map[name] 

del config.mag_err_column_map[name] 

with self.assertRaises(ValueError): 

config.validate() 

 

def testValidatePm(self): 

basicNames = ["pm_ra_name", "pm_dec_name", "epoch_name", "epoch_format", "epoch_scale"] 

 

for withPmErr in (False, True): 

config = self.makeConfig(withPm=True, withPmErr=withPmErr) 

config.validate() 

del config 

 

if withPmErr: 

names = basicNames + ["pm_ra_err_name", "pm_dec_err_name"] 

else: 

names = basicNames 

for name in names: 

with self.subTest(name=name, withPmErr=withPmErr): 

config = self.makeConfig(withPm=True, withPmErr=withPmErr) 

setattr(config, name, None) 

with self.assertRaises(ValueError): 

config.validate() 

 

def testValidateParallax(self): 

basicNames = ["parallax_name", "epoch_name", "epoch_format", "epoch_scale"] 

 

for withParallaxErr in (False, True): 

config = self.makeConfig(withParallax=True, withParallaxErr=withParallaxErr) 

config.validate() 

del config 

 

if withParallaxErr: 

names = basicNames + ["parallax_err_name"] 

else: 

names = basicNames 

for name in names: 

with self.subTest(name=name, withParallaxErr=withParallaxErr): 

config = self.makeConfig(withParallax=True, withParallaxErr=withParallaxErr) 

setattr(config, name, None) 

if name == "parallax_name" and not withParallaxErr: 

# it is OK to omit parallax_name if no parallax_err_name 

config.validate() 

else: 

with self.assertRaises(ValueError): 

config.validate() 

 

def testIngest(self): 

"""Test IngestIndexedReferenceTask with different configs.""" 

# Test with multiple files and standard config 

config = self.makeConfig(withRaDecErr=True, withMagErr=True, withPm=True, withPmErr=True) 

# don't use the default depth, to avoid taking the time to create thousands of file locks 

config.dataset_config.indexer.active.depth = self.depth 

IngestIndexedReferenceTask.parseAndRun( 

args=[self.input_dir, "--output", self.outPath+"/output_multifile", 

self.skyCatalogFile, self.skyCatalogFile], 

config=config) 

# A newly-ingested refcat should be marked format_version=1. 

loader = LoadIndexedReferenceObjectsTask(butler=dafPersist.Butler(self.outPath+"/output_multifile")) 

self.assertEqual(loader.dataset_config.format_version, 1) 

 

# Test with config overrides 

config2 = self.makeConfig(withRaDecErr=True, withMagErr=True, withPm=True, withPmErr=True) 

config2.ra_name = "ra" 

config2.dec_name = "dec" 

config2.dataset_config.ref_dataset_name = 'myrefcat' 

# Change the indexing depth to prove we can. 

# Smaller is better than larger because it makes fewer files. 

config2.dataset_config.indexer.active.depth = self.depth - 1 

config2.is_photometric_name = 'is_phot' 

config2.is_resolved_name = 'is_res' 

config2.is_variable_name = 'is_var' 

config2.id_name = 'id' 

config2.extra_col_names = ['val1', 'val2', 'val3'] 

config2.file_reader.header_lines = 1 

config2.file_reader.colnames = [ 

'id', 'ra', 'dec', 'ra_err', 'dec_err', 'a', 'a_err', 'b', 'b_err', 'is_phot', 

'is_res', 'is_var', 'val1', 'val2', 'val3', 'pm_ra', 'pm_dec', 'pm_ra_err', 

'pm_dec_err', 'unixtime', 

] 

config2.file_reader.delimiter = '|' 

# this also tests changing the delimiter 

IngestIndexedReferenceTask.parseAndRun( 

args=[self.input_dir, "--output", self.outPath+"/output_override", 

self.skyCatalogFileDelim], config=config2) 

 

# Test if we can get back the catalog with a non-standard dataset name 

butler = dafPersist.Butler(self.outPath+"/output_override") 

loaderConfig = LoadIndexedReferenceObjectsConfig() 

loaderConfig.ref_dataset_name = "myrefcat" 

loader = LoadIndexedReferenceObjectsTask(butler=butler, config=loaderConfig) 

self.checkAllRowsInRefcat(loader, self.skyCatalog) 

 

# test that a catalog can be loaded even with a name not used for ingestion 

butler = dafPersist.Butler(self.testRepoPath) 

loaderConfig2 = LoadIndexedReferenceObjectsConfig() 

loaderConfig2.ref_dataset_name = self.testDatasetName 

loader = LoadIndexedReferenceObjectsTask(butler=butler, config=loaderConfig2) 

self.checkAllRowsInRefcat(loader, self.skyCatalog) 

 

def testLoadIndexedReferenceConfig(self): 

"""Make sure LoadIndexedReferenceConfig has needed fields.""" 

""" 

Including at least one from the base class LoadReferenceObjectsConfig 

""" 

config = LoadIndexedReferenceObjectsConfig() 

self.assertEqual(config.ref_dataset_name, "cal_ref_cat") 

self.assertEqual(config.defaultFilter, "") 

 

def testLoadSkyCircle(self): 

"""Test LoadIndexedReferenceObjectsTask.loadSkyCircle with default config.""" 

loader = LoadIndexedReferenceObjectsTask(butler=self.testButler) 

for tupl, idList in self.compCats.items(): 

cent = ingestIndexTestBase.make_coord(*tupl) 

lcat = loader.loadSkyCircle(cent, self.searchRadius, filterName='a') 

self.assertTrue(lcat.refCat.isContiguous()) 

self.assertFalse("camFlux" in lcat.refCat.schema) 

self.assertEqual(Counter(lcat.refCat['id']), Counter(idList)) 

if len(lcat.refCat) > 0: 

# make sure there are no duplicate ids 

self.assertEqual(len(set(Counter(lcat.refCat['id']).values())), 1) 

self.assertEqual(len(set(Counter(idList).values())), 1) 

# A default-loaded sky circle should not have centroids 

self.assertNotIn("centroid_x", lcat.refCat.schema) 

self.assertNotIn("centroid_y", lcat.refCat.schema) 

self.assertNotIn("hasCentroid", lcat.refCat.schema) 

else: 

self.assertEqual(len(idList), 0) 

 

def testLoadPixelBox(self): 

"""Test LoadIndexedReferenceObjectsTask.loadPixelBox with default config.""" 

loader = LoadIndexedReferenceObjectsTask(butler=self.testButler) 

numFound = 0 

for tupl, idList in self.compCats.items(): 

cent = ingestIndexTestBase.make_coord(*tupl) 

bbox = lsst.geom.Box2I(lsst.geom.Point2I(30, -5), lsst.geom.Extent2I(1000, 1004)) # arbitrary 

ctr_pix = bbox.getCenter() 

# catalog is sparse, so set pixel scale such that bbox encloses region 

# used to generate compCats 

pixel_scale = 2*self.searchRadius/max(bbox.getHeight(), bbox.getWidth()) 

cdMatrix = afwGeom.makeCdMatrix(scale=pixel_scale) 

wcs = afwGeom.makeSkyWcs(crval=cent, crpix=ctr_pix, cdMatrix=cdMatrix) 

result = loader.loadPixelBox(bbox=bbox, wcs=wcs, filterName="a") 

self.assertFalse("camFlux" in result.refCat.schema) 

self.assertGreaterEqual(len(result.refCat), len(idList)) 

numFound += len(result.refCat) 

self.assertGreater(numFound, 0) 

 

def testDefaultFilterAndFilterMap(self): 

"""Test defaultFilter and filterMap parameters of LoadIndexedReferenceObjectsConfig.""" 

config = LoadIndexedReferenceObjectsConfig() 

config.defaultFilter = "b" 

config.filterMap = {"aprime": "a"} 

loader = LoadIndexedReferenceObjectsTask(butler=self.testButler, config=config) 

for tupl, idList in self.compCats.items(): 

cent = ingestIndexTestBase.make_coord(*tupl) 

lcat = loader.loadSkyCircle(cent, self.searchRadius) 

self.assertEqual(lcat.fluxField, "camFlux") 

if len(idList) > 0: 

defFluxFieldName = getRefFluxField(lcat.refCat.schema, None) 

self.assertTrue(defFluxFieldName in lcat.refCat.schema) 

aprimeFluxFieldName = getRefFluxField(lcat.refCat.schema, "aprime") 

self.assertTrue(aprimeFluxFieldName in lcat.refCat.schema) 

break # just need one test 

 

def testProperMotion(self): 

"""Test proper motion correction""" 

center = ingestIndexTestBase.make_coord(93.0, -90.0) 

loader = LoadIndexedReferenceObjectsTask(butler=self.testButler) 

references = loader.loadSkyCircle(center, self.searchRadius, filterName='a').refCat 

original = references.copy(True) 

 

# Zero epoch change --> no proper motion correction (except minor numerical effects) 

loader.applyProperMotions(references, self.epoch) 

self.assertFloatsAlmostEqual(references["coord_ra"], original["coord_ra"], rtol=1.0e-14) 

self.assertFloatsAlmostEqual(references["coord_dec"], original["coord_dec"], rtol=1.0e-14) 

self.assertFloatsEqual(references["coord_raErr"], original["coord_raErr"]) 

self.assertFloatsEqual(references["coord_decErr"], original["coord_decErr"]) 

 

# One year difference 

loader.applyProperMotions(references, self.epoch + 1.0*astropy.units.yr) 

self.assertFloatsEqual(references["pm_raErr"], original["pm_raErr"]) 

self.assertFloatsEqual(references["pm_decErr"], original["pm_decErr"]) 

for orig, ref in zip(original, references): 

self.assertAnglesAlmostEqual(orig.getCoord().separation(ref.getCoord()), 

self.properMotionAmt, maxDiff=1.0e-6*lsst.geom.arcseconds) 

self.assertAnglesAlmostEqual(orig.getCoord().bearingTo(ref.getCoord()), 

self.properMotionDir, maxDiff=1.0e-4*lsst.geom.arcseconds) 

predictedRaErr = np.hypot(original["coord_raErr"], original["pm_raErr"]) 

predictedDecErr = np.hypot(original["coord_decErr"], original["pm_decErr"]) 

self.assertFloatsAlmostEqual(references["coord_raErr"], predictedRaErr) 

self.assertFloatsAlmostEqual(references["coord_decErr"], predictedDecErr) 

 

def testLoadVersion0(self): 

"""Test reading a pre-written format_version=0 (Jy flux) catalog. 

It should be converted to have nJy fluxes. 

""" 

path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data/version0') 

loader = LoadIndexedReferenceObjectsTask(butler=dafPersist.Butler(path)) 

self.assertEqual(loader.dataset_config.format_version, 0) 

result = loader.loadSkyCircle(ingestIndexTestBase.make_coord(10, 20), 

5*lsst.geom.degrees, filterName='a') 

self.assertTrue(hasNanojanskyFluxUnits(result.refCat.schema)) 

catalog = afwTable.SimpleCatalog.readFits(os.path.join(path, 'ref_cats/cal_ref_cat/4022.fits')) 

self.assertFloatsEqual(catalog['a_flux']*1e9, result.refCat['a_flux']) 

self.assertFloatsEqual(catalog['a_fluxSigma']*1e9, result.refCat['a_fluxErr']) 

self.assertFloatsEqual(catalog['b_flux']*1e9, result.refCat['b_flux']) 

self.assertFloatsEqual(catalog['b_fluxSigma']*1e9, result.refCat['b_fluxErr']) 

 

def testLoadVersion1(self): 

"""Test reading a format_version=1 catalog (fluxes unchanged).""" 

path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data/version1') 

loader = LoadIndexedReferenceObjectsTask(butler=dafPersist.Butler(path)) 

self.assertEqual(loader.dataset_config.format_version, 1) 

result = loader.loadSkyCircle(ingestIndexTestBase.make_coord(10, 20), 

5*lsst.geom.degrees, filterName='a') 

self.assertTrue(hasNanojanskyFluxUnits(result.refCat.schema)) 

catalog = afwTable.SimpleCatalog.readFits(os.path.join(path, 'ref_cats/cal_ref_cat/4022.fits')) 

self.assertFloatsEqual(catalog['a_flux'], result.refCat['a_flux']) 

self.assertFloatsEqual(catalog['a_fluxErr'], result.refCat['a_fluxErr']) 

self.assertFloatsEqual(catalog['b_flux'], result.refCat['b_flux']) 

self.assertFloatsEqual(catalog['b_fluxErr'], result.refCat['b_fluxErr']) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()