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

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

 

__all__ = ["IngestIndexCatalogTestBase", "make_coord"] 

 

import math 

import os.path 

import shutil 

import string 

import tempfile 

 

import numpy as np 

import astropy 

 

import lsst.daf.persistence as dafPersist 

from lsst.meas.algorithms import IndexerRegistry 

from lsst.meas.algorithms import IngestIndexedReferenceTask 

import lsst.utils 

 

 

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 IngestIndexCatalogTestBase: 

"""Base class for tests involving IngestIndexedReferenceTask 

""" 

@classmethod 

def makeSkyCatalog(cls, outPath, size=1000, idStart=1, seed=123): 

"""Make an on-sky catalog, and save it to a text file. 

 

Parameters 

---------- 

outPath : `str` or None 

The directory to write the catalog to. 

Specify None to not write any output. 

size : `int`, (optional) 

Number of items to add to the catalog. 

idStart : `int`, (optional) 

First id number to put in the catalog. 

seed : `float`, (optional) 

Random seed for ``np.random``. 

 

Returns 

------- 

refCatPath : `str` 

Path to the created on-sky catalog. 

refCatOtherDelimiterPath : `str` 

Path to the created on-sky catalog with a different delimiter. 

refCatData : `np.ndarray` 

The data contained in the on-sky catalog files. 

""" 

np.random.seed(seed) 

ident = np.arange(idStart, size + idStart, dtype=int) 

ra = np.random.random(size)*360. 

dec = np.degrees(np.arccos(2.*np.random.random(size) - 1.)) 

dec -= 90. 

ra_err = np.ones(size)*0.1 # arcsec 

dec_err = np.ones(size)*0.1 # arcsec 

a_mag = 16. + np.random.random(size)*4. 

a_mag_err = 0.01 + np.random.random(size)*0.2 

b_mag = 17. + np.random.random(size)*5. 

b_mag_err = 0.02 + np.random.random(size)*0.3 

is_photometric = np.random.randint(2, size=size) 

is_resolved = np.random.randint(2, size=size) 

is_variable = np.random.randint(2, size=size) 

extra_col1 = np.random.normal(size=size) 

extra_col2 = np.random.normal(1000., 100., size=size) 

# compute proper motion and PM error in arcseconds/year 

# and let the ingest task scale them to radians 

pm_amt_arcsec = cls.properMotionAmt.asArcseconds() 

pm_dir_rad = cls.properMotionDir.asRadians() 

pm_ra = np.ones(size)*pm_amt_arcsec*math.cos(pm_dir_rad) 

pm_dec = np.ones(size)*pm_amt_arcsec*math.sin(pm_dir_rad) 

pm_ra_err = np.ones(size)*cls.properMotionErr.asArcseconds()*abs(math.cos(pm_dir_rad)) 

pm_dec_err = np.ones(size)*cls.properMotionErr.asArcseconds()*abs(math.sin(pm_dir_rad)) 

unixtime = np.ones(size)*cls.epoch.unix 

 

def get_word(word_len): 

return "".join(np.random.choice([s for s in string.ascii_letters], word_len)) 

extra_col3 = np.array([get_word(num) for num in np.random.randint(11, size=size)]) 

 

dtype = np.dtype([('id', float), ('ra_icrs', float), ('dec_icrs', float), 

('ra_err', float), ('dec_err', float), ('a', float), 

('a_err', float), ('b', float), ('b_err', float), ('is_phot', int), 

('is_res', int), ('is_var', int), ('val1', float), ('val2', float), 

('val3', '|S11'), ('pm_ra', float), ('pm_dec', float), ('pm_ra_err', float), 

('pm_dec_err', float), ('unixtime', float)]) 

 

arr = np.array(list(zip(ident, ra, dec, ra_err, dec_err, a_mag, a_mag_err, b_mag, b_mag_err, 

is_photometric, is_resolved, is_variable, extra_col1, extra_col2, extra_col3, 

pm_ra, pm_dec, pm_ra_err, pm_dec_err, unixtime)), dtype=dtype) 

if outPath is not None: 

# write the data with full precision; this is not realistic for 

# real catalogs, but simplifies tests based on round tripped data 

saveKwargs = dict( 

header="id,ra_icrs,dec_icrs,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", 

fmt=["%i", "%.15g", "%.15g", "%.15g", "%.15g", 

"%.15g", "%.15g", "%.15g", "%.15g", "%i", "%i", "%i", "%.15g", "%.15g", "%s", 

"%.15g", "%.15g", "%.15g", "%.15g", "%.15g"] 

) 

 

np.savetxt(outPath+"/ref.txt", arr, delimiter=",", **saveKwargs) 

np.savetxt(outPath+"/ref_test_delim.txt", arr, delimiter="|", **saveKwargs) 

return outPath+"/ref.txt", outPath+"/ref_test_delim.txt", arr 

else: 

return arr 

 

@classmethod 

def tearDownClass(cls): 

try: 

shutil.rmtree(cls.outPath) 

except Exception: 

print("WARNING: failed to remove temporary dir %r" % (cls.outPath,)) 

del cls.outPath 

del cls.skyCatalogFile 

del cls.skyCatalogFileDelim 

del cls.skyCatalog 

del cls.testRas 

del cls.testDecs 

del cls.searchRadius 

del cls.compCats 

del cls.testButler 

 

@classmethod 

def setUpClass(cls): 

cls.obs_test_dir = lsst.utils.getPackageDir('obs_test') 

cls.input_dir = os.path.join(cls.obs_test_dir, "data", "input") 

 

cls.outPath = tempfile.mkdtemp() 

cls.testCatPath = os.path.join(os.path.dirname(os.path.realpath(__file__)), "data", 

"testHtmIndex.fits") 

# arbitrary, but reasonable, amount of proper motion (angle/year) 

# and direction of proper motion 

cls.properMotionAmt = 3.0*lsst.geom.arcseconds 

cls.properMotionDir = 45*lsst.geom.degrees 

cls.properMotionErr = 1e-3*lsst.geom.arcseconds 

cls.epoch = astropy.time.Time(58206.861330339219, scale="tai", format="mjd") 

cls.skyCatalogFile, cls.skyCatalogFileDelim, cls.skyCatalog = cls.makeSkyCatalog(cls.outPath) 

cls.testRas = [210., 14.5, 93., 180., 286., 0.] 

cls.testDecs = [-90., -51., -30.1, 0., 27.3, 62., 90.] 

cls.searchRadius = 3. * lsst.geom.degrees 

cls.compCats = {} # dict of center coord: list of IDs of stars within cls.searchRadius of center 

cls.depth = 4 # gives a mean area of 20 deg^2 per pixel, roughly matching a 3 deg search radius 

 

config = IndexerRegistry['HTM'].ConfigClass() 

# Match on disk comparison file 

config.depth = cls.depth 

cls.indexer = IndexerRegistry['HTM'](config) 

for ra in cls.testRas: 

for dec in cls.testDecs: 

tupl = (ra, dec) 

cent = make_coord(*tupl) 

cls.compCats[tupl] = [] 

for rec in cls.skyCatalog: 

if make_coord(rec['ra_icrs'], rec['dec_icrs']).separation(cent) < cls.searchRadius: 

cls.compCats[tupl].append(rec['id']) 

 

cls.testRepoPath = cls.outPath+"/test_repo" 

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

# To match on disk test data 

config.dataset_config.indexer.active.depth = cls.depth 

config.id_name = 'id' 

config.pm_scale = 1000.0 # arcsec/yr --> mas/yr 

# np.savetxt prepends '# ' to the header lines, so use a reader that understands that 

config.file_reader.format = 'ascii.commented_header' 

# run the intest once to create a butler repo we can compare to 

IngestIndexedReferenceTask.parseAndRun(args=[cls.input_dir, "--output", cls.testRepoPath, 

cls.skyCatalogFile], config=config) 

cls.defaultDatasetName = config.dataset_config.ref_dataset_name 

cls.testDatasetName = 'diff_ref_name' 

cls.testButler = dafPersist.Butler(cls.testRepoPath) 

os.symlink(os.path.join(cls.testRepoPath, 'ref_cats', cls.defaultDatasetName), 

os.path.join(cls.testRepoPath, 'ref_cats', cls.testDatasetName)) 

 

@staticmethod 

def makeConfig(withMagErr=False, withRaDecErr=False, withPm=False, withPmErr=False, 

withParallax=False, withParallaxErr=False): 

"""Make a config for IngestIndexedReferenceTask 

 

This is primarily intended to simplify tests of config validation, 

so fields that are not validated are not set. 

However, it can calso be used to reduce boilerplate in other tests. 

""" 

config = IngestIndexedReferenceTask.ConfigClass() 

config.pm_scale = 1000.0 

config.ra_name = 'ra_icrs' 

config.dec_name = 'dec_icrs' 

config.mag_column_list = ['a', 'b'] 

 

if withMagErr: 

config.mag_err_column_map = {'a': 'a_err', 'b': 'b_err'} 

 

if withRaDecErr: 

config.ra_err_name = "ra_err" 

config.dec_err_name = "dec_err" 

 

if withPm: 

config.pm_ra_name = "pm_ra" 

config.pm_dec_name = "pm_dec" 

 

if withPmErr: 

config.pm_ra_err_name = "pm_ra_err" 

config.pm_dec_err_name = "pm_dec_err" 

 

if withParallax: 

config.parallax_name = "parallax" 

 

if withParallaxErr: 

config.parallax_err_name = "parallax_err" 

 

if withPm or withParallax: 

config.epoch_name = "unixtime" 

config.epoch_format = "unix" 

config.epoch_scale = "utc" 

 

return config 

 

def checkAllRowsInRefcat(self, refObjLoader, skyCatalog): 

"""Check that every item in ``skyCatalog`` is in the ingested catalog. 

 

Parameters 

---------- 

refObjLoader : `lsst.meas.algorithms.LoadIndexedReferenceObjectsTask` 

A reference object loader to use to search for rows from 

``skyCatalog``. 

skyCatalog : `np.ndarray` 

The original data to compare with. 

""" 

for row in skyCatalog: 

center = lsst.geom.SpherePoint(row['ra_icrs'], row['dec_icrs'], lsst.geom.degrees) 

cat = refObjLoader.loadSkyCircle(center, 2*lsst.geom.arcseconds, filterName='a').refCat 

self.assertGreater(len(cat), 0, "No objects found in loaded catalog.") 

msg = f"input row not found in loaded catalog:\nrow:\n{row}\n{row.dtype}\n\ncatalog:\n{cat[0]}" 

self.assertEqual(row['id'], cat[0]['id'], msg) 

# coordinates won't match perfectly due to rounding in radian/degree conversions 

self.assertFloatsAlmostEqual(row['ra_icrs'], cat[0]['coord_ra'].asDegrees(), 

rtol=1e-14, msg=msg) 

self.assertFloatsAlmostEqual(row['dec_icrs'], cat[0]['coord_dec'].asDegrees(), 

rtol=1e-14, msg=msg)