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

import matplotlib 

matplotlib.use("Agg") 

import os 

import unittest 

import numpy as np 

import lsst.sims.maf.db as db 

import lsst.sims.maf.utils.outputUtils as out 

import lsst.utils.tests 

from lsst.utils import getPackageDir 

from lsst.sims.utils.CodeUtilities import sims_clean_up 

from builtins import str 

 

 

class TestOpsimDb(unittest.TestCase): 

"""Test opsim specific database class.""" 

 

@classmethod 

def tearDownClass(cls): 

sims_clean_up() 

 

def setUp(self): 

self.database = os.path.join(getPackageDir('sims_data'), 'OpSimData', 

'astro-lsst-01_2014.db') 

self.oo = db.OpsimDatabaseV4(database=self.database) 

 

def tearDown(self): 

del self.oo 

del self.database 

self.oo = None 

 

def testOpsimDbSetup(self): 

"""Test opsim specific database class setup/instantiation.""" 

# Test tables were connected to. 

self.assertIn('SummaryAllProps', self.oo.tableNames) 

self.assertEqual(self.oo.defaultTable, 'SummaryAllProps') 

 

def testOpsimDbMetricData(self): 

"""Test queries for sim data. """ 

data = self.oo.fetchMetricData(['seeingFwhmEff', ], 'filter="r" and seeingFwhmEff<1.0') 

self.assertEqual(data.dtype.names, ('seeingFwhmEff',)) 

self.assertLessEqual(data['seeingFwhmEff'].max(), 1.0) 

 

def testOpsimDbPropID(self): 

"""Test queries for prop ID""" 

propids, propTags = self.oo.fetchPropInfo() 

self.assertGreater(len(list(propids.keys())), 0) 

self.assertGreater(len(propTags['WFD']), 0) 

self.assertGreaterEqual(len(propTags['DD']), 0) 

for w in propTags['WFD']: 

self.assertIn(w, propids) 

for d in propTags['DD']: 

self.assertIn(d, propids) 

 

def testOpsimDbFields(self): 

"""Test queries for field data.""" 

# Fetch field data for all fields. 

dataAll = self.oo.fetchFieldsFromFieldTable() 

self.assertEqual(dataAll.dtype.names, ('fieldId', 'fieldRA', 'fieldDec')) 

# Fetch field data for all fields requested by a particular propid. 

# Need to reinstate this capability. 

 

def testOpsimDbRunLength(self): 

"""Test query for length of opsim run.""" 

nrun = self.oo.fetchRunLength() 

self.assertEqual(nrun, 0.04) 

 

def testOpsimDbSimName(self): 

"""Test query for opsim name.""" 

simname = self.oo.fetchOpsimRunName() 

self.assertIsInstance(simname, str) 

self.assertEqual(simname, 'astro-lsst-01_2014') 

 

def testOpsimDbConfig(self): 

"""Test generation of config data. """ 

configsummary, configdetails = self.oo.fetchConfig() 

self.assertIsInstance(configsummary, dict) 

self.assertIsInstance(configdetails, dict) 

# self.assertEqual(set(configsummary.keys()), set(['Version', 'RunInfo', 'Proposals', 'keyorder'])) 

propids, proptags = self.oo.fetchPropInfo() 

propidsSummary = [] 

for propname in configsummary['Proposals']: 

82 ↛ 81line 82 didn't jump to line 81, because the condition on line 82 was never false if propname != 'keyorder': 

propidsSummary.append(configsummary['Proposals'][propname]['PropId']) 

self.assertEqual(set(propidsSummary), set(propids)) 

#out.printDict(configsummary, 'Summary') 

 

 

def testCreateSqlWhere(self): 

""" 

Test that the createSQLWhere method handles expected cases. 

""" 

# propTags is a dictionary of lists returned by OpsimDatabase 

propTags = {'WFD': [1, 2, 3], 'DD': [4], 'Rolling': [2]} 

# If tag is in dictionary with one value, returned sql where clause 

# is simply 'propId = 4' 

tag = 'DD' 

sqlWhere = self.oo.createSQLWhere(tag, propTags) 

self.assertEqual(sqlWhere, 'proposalId = 4') 

# if multiple proposals with the same tag, all should be in list. 

tag = 'WFD' 

sqlWhere = self.oo.createSQLWhere(tag, propTags) 

self.assertEqual(sqlWhere.split()[0], '(proposalId') 

for id_val in propTags['WFD']: 

self.assertIn('%s' % (id_val), sqlWhere) 

# And the same id can be in multiple proposals. 

tag = 'Rolling' 

sqlWhere = self.oo.createSQLWhere(tag, propTags) 

self.assertEqual(sqlWhere, 'proposalId = 2') 

# And tags not in propTags are handled. 

badprop = 'proposalId like "NO PROP"' 

tag = 'nogo' 

sqlWhere = self.oo.createSQLWhere(tag, propTags) 

self.assertEqual(sqlWhere, badprop) 

# And tags which identify no proposal ID are handled. 

propTags['Rolling'] = [] 

sqlWhere = self.oo.createSQLWhere(tag, propTags) 

self.assertEqual(sqlWhere, badprop) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()