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

from __future__ import print_function 

from builtins import zip 

from builtins import next 

import os 

import inspect 

import numpy as np 

import sys 

import traceback 

import unittest 

import tempfile 

import shutil 

import lsst.utils.tests 

 

from lsst.utils import getPackageDir 

from lsst.sims.catalogs.db import CatalogDBObject 

from lsst.sims.catalogs.definitions import InstanceCatalog 

from lsst.sims.catUtils.exampleCatalogDefinitions import ObsStarCatalogBase 

from lsst.sims.catUtils.utils import failedOnFatboy 

# The following is to get the object ids in the registry 

import lsst.sims.catUtils.baseCatalogModels as bcm 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

class DummyCat(InstanceCatalog): 

catalog_type = __file__ + 'unit_test_catalog' 

column_outputs = ['raJ2000', 'decJ2000'] 

 

 

class basicAccessTest(unittest.TestCase): 

 

longMessage = True 

 

def testObjects(self): 

catDir = tempfile.mkdtemp('basicAccessTest_testObjects') 

if not os.path.exists(catDir): 

os.mkdir(catDir) 

catName = tempfile.mktemp(prefix='basicAccessTest_testObjects', 

dir=catDir, suffix='.txt') 

ct_connected = 0 

ct_failed_connection = 0 

list_of_failures = [] 

 

for objname, objcls in CatalogDBObject.registry.items(): 

if not objcls.doRunTest or (objcls.testObservationMetaData is None): 

continue 

 

print("Running tests for", objname) 

try: 

dbobj = objcls(verbose=False) 

except: 

trace = traceback.extract_tb(sys.exc_info()[2], limit=20) 

msg = sys.exc_info()[1].args[0] 

if 'Failed to connect' in str(msg) or failedOnFatboy(trace): 

 

# if the exception was due to a failed connection 

# to fatboy, ignore it 

 

ct_failed_connection += 1 

list_of_failures.append(objname) 

continue 

else: 

raise 

 

obs_metadata = dbobj.testObservationMetaData 

 

# Get results all at once 

try: 

result = dbobj.query_columns(obs_metadata=obs_metadata) 

except: 

 

# This is because the solar system object 'tables' 

# don't actually connect to tables on fatboy; they just 

# call methods stored on fatboy. Therefore, the connection 

# failure will not be noticed until this part of the test 

 

ct_failed_connection += 1 

list_of_failures.append(objname) 

msg = sys.exc_info()[1].args[0] 

if 'DB-Lib error' in msg: 

continue 

else: 

raise 

 

ct_connected += 1 

 

# Since there is only one chunk, 

try: 

result = next(result) 

except StopIteration: 

raise RuntimeError("No results for %s defined in %s"%(objname, 

inspect.getsourcefile(dbobj.__class__))) 

if objname.startswith('galaxy'): 

DummyCat.column_outputs = ['galid', 'raJ2000', 'decJ2000'] 

else: 

DummyCat.column_outputs = ['raJ2000', 'decJ2000'] 

cat = dbobj.getCatalog(__file__+'unit_test_catalog', obs_metadata) 

if os.path.exists(catName): 

os.unlink(catName) 

try: 

cat.write_catalog(catName) 

dtypeList = [(name, np.float) for name in cat._column_outputs] 

testData = np.genfromtxt(catName, delimiter = ', ', 

dtype=np.dtype(dtypeList)) 

self.assertGreater(len(testData), 0) 

finally: 

if os.path.exists(catName): 

os.unlink(catName) 

 

if os.path.exists(catDir): 

shutil.rmtree(catDir) 

 

self.assertEqual(len(list_of_failures), ct_failed_connection) 

 

print('\n================') 

print('Do not worry about this message') 

print('sometimes, connections to the UW database fail.') 

print('It is expected.') 

print('This is just a tally so that you know how often that happened.') 

print('successful connections: ', ct_connected) 

print('failed connections: ', ct_failed_connection) 

if len(list_of_failures) > 0: 

print('objects that failed to connect: ', list_of_failures) 

 

def testObsCat(self): 

objname = 'wdstars' 

catDir = tempfile.mkdtemp('basicAccessTest_testObsCat') 

if not os.path.exists(catDir): 

os.mkdir(catDir) 

catName = tempfile.mktemp(prefix='basicAccessTest_testObsCat', 

dir=catDir, suffix='.txt') 

 

try: 

dbobj = CatalogDBObject.from_objid(objname) 

obs_metadata = dbobj.testObservationMetaData 

# To cover the central ~raft 

obs_metadata.boundLength = 0.4 

obs_metadata.rotSkyPos = 0.0 

cat = dbobj.getCatalog('obs_star_cat', obs_metadata) 

if os.path.exists(catName): 

os.unlink(catName) 

try: 

cat.write_catalog(catName) 

dtypeList = [(name, np.float) for name in cat._column_outputs] 

testData = np.genfromtxt(catName, delimiter = ', ', 

dtype=np.dtype(dtypeList)) 

self.assertGreater(len(testData), 0) 

finally: 

if os.path.exists(catName): 

os.unlink(catName) 

if os.path.exists(catDir): 

shutil.rmtree(catDir) 

 

print('\ntestObsCat successfully connected to fatboy') 

 

except: 

trace = traceback.extract_tb(sys.exc_info()[2], limit=20) 

msg = sys.exc_info()[1].args[0] 

if 'Failed to connect' in str(msg) or failedOnFatboy(trace): 

 

# if the exception was because of a failed connection 

# to fatboy, ignore it. 

 

print('\ntestObsCat failed to connect to fatboy') 

print('Sometimes that happens. Do not worry.') 

 

if os.path.exists(catDir): 

shutil.rmtree(catDir) 

 

pass 

else: 

raise 

 

def test_limit(self): 

""" 

Test that the limit kwarg in query_columns behaves correctly. 

 

Will test on one star table and one galaxy table. 

""" 

list_of_failures = [] 

for objcls, clsname in zip((bcm.StarObj, bcm.GalaxyObj), ('StarObj', 'GalaxyObj')): 

msg = "failed the limit test\noffending class is %s" % clsname 

try: 

dbobj = objcls(verbose=False) 

except: 

trace = traceback.extract_tb(sys.exc_info()[2], limit=20) 

msg = sys.exc_info()[1].args[0] 

if 'Failed to connect' in str(msg) or failedOnFatboy(trace): 

 

# if the exception was due to a failed connection 

# to fatboy, ignore it 

list_of_failures.append(clsname) 

continue 

else: 

raise 

 

obs_metadata = dbobj.testObservationMetaData 

 

results = dbobj.query_columns(obs_metadata=obs_metadata) 

 

ct_res = 0 

for chunk in results: 

for line in chunk: 

ct_res += 1 

 

self.assertGreater(ct_res, 10, msg=msg) 

 

limited_results = dbobj.query_columns(obs_metadata=obs_metadata, limit=10) 

 

ct_limit = 0 

for chunk in limited_results: 

for line in chunk: 

ct_limit += 1 

 

self.assertEqual(ct_limit, 10, msg=msg) 

 

if len(list_of_failures) > 0: 

print("\nList of DBObjects that could not connect to fatboy " \ 

"for the test on the limit kwarg") 

for nn in list_of_failures: 

print(nn) 

 

def test_constraint(self): 

""" 

Test that passing a constraint into query_columns works (i.e. if I only want 

to select galaxies whose varParamStr is not NULL). 

""" 

list_of_failures = [] 

constraint = "varParamStr IS NOT NULL" 

for objcls, clsname in zip((bcm.GalaxyObj, bcm.GalaxyTileObj), ('GalaxyObj', 'GalaxyTileObj')): 

msg = "failed the constraint test\noffending class is %s" % clsname 

try: 

dbobj = objcls(verbose=False) 

except: 

trace = traceback.extract_tb(sys.exc_info()[2], limit=20) 

msg = sys.exc_info()[1].args[0] 

if 'Failed to connect' in str(msg) or failedOnFatboy(trace): 

 

# if the exception was due to a failed connection 

# to fatboy, ignore it 

list_of_failures.append(clsname) 

continue 

else: 

raise 

 

obs_metadata = dbobj.testObservationMetaData 

 

# query witout a constraint on varParamStr 

results = dbobj.query_columns(colnames=['raJ2000', 'decJ2000', 'varParamStr'], 

obs_metadata=obs_metadata) 

 

# count total number of rows (ct_res) and number of rows with a null 

# varParamStr (ct_no_varparamstr). Note that varParamStr will be the 

# index=3 entry in result rows because the id of the galaxy gets 

# automatically added to query results. 

ct_res = 0 

ct_no_varparamstr = 0 

for chunk in results: 

for line in chunk: 

if line[3] == 'None': 

ct_no_varparamstr += 1 

ct_res += 1 

 

# run the same query, but demanding that varParamStr is not NULL 

constrained_results = dbobj.query_columns(colnames=['raJ2000', 'decJ2000', 'varParamStr'], 

obs_metadata=obs_metadata, 

constraint=constraint) 

 

# count the number of rows with non-NULL varParamStr 

ct_con = 0 

for chunk in constrained_results: 

for line in chunk: 

ct_con += 1 

self.assertNotEqual(line[3], 'None') 

 

# check that the number of non-NULL varParamStr and NULL varParamStr rows 

# compare the way that they should 

self.assertGreater(ct_res, ct_con) 

self.assertGreater(ct_no_varparamstr, 0) 

self.assertEqual(ct_res-ct_con, ct_no_varparamstr) 

 

if len(list_of_failures) > 0: 

print("\nList of DBObjects that could not connect to fatboy " \ 

"for the test on the constraint kwarg") 

for nn in list_of_failures: 

print(nn) 

 

def test_limit_and_constraint(self): 

""" 

Test that limit and constraint work together 

""" 

list_of_failures = [] 

constraint = "varParamStr IS NOT NULL" 

could_connect = True 

try: 

dbobj = bcm.GalaxyObj(verbose=False) 

except: 

trace = traceback.extract_tb(sys.exc_info()[2], limit=20) 

msg = sys.exc_info()[1].args[0] 

if 'Failed to connect' in str(msg) or failedOnFatboy(trace): 

 

# if the exception was due to a failed connection 

# to fatboy, ignore it 

list_of_failures.append('GalaxyObj') 

could_connect = False 

else: 

raise 

 

if could_connect: 

obs_metadata = dbobj.testObservationMetaData 

 

# query with a constraint on varParamStr but no limit 

results_no_limit = dbobj.query_columns(colnames=['raJ2000', 'decJ2000', 'varParamStr'], 

obs_metadata=obs_metadata, 

constraint=constraint) 

 

ct_res = 0 

for chunk in results_no_limit: 

for line in chunk: 

self.assertNotEqual(line[3], 'None') 

ct_res += 1 

 

self.assertGreater(ct_res, 1) 

 

# run the same query, but limiting the results 

limited_results = dbobj.query_columns(colnames=['raJ2000', 'decJ2000', 'varParamStr'], 

obs_metadata=obs_metadata, 

constraint=constraint, 

limit=ct_res-1) 

ct_lim = 0 

for chunk in limited_results: 

for line in chunk: 

ct_lim += 1 

self.assertNotEqual(line[3], 'None') 

 

self.assertEqual(ct_lim, ct_res-1) 

 

if len(list_of_failures) > 0: 

print("\nList of DBObjects that could not connect to fatboy " \ 

"for the test on the constraint and limit kwargs") 

for nn in list_of_failures: 

print(nn) 

 

 

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

pass 

 

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

lsst.utils.tests.init() 

unittest.main()