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

from __future__ import with_statement 

from builtins import range 

import unittest 

import os 

import numpy as np 

import tempfile 

import shutil 

import lsst.utils.tests 

 

from lsst.sims.utils.CodeUtilities import sims_clean_up 

from lsst.sims.catalogs.db import fileDBObject, CatalogDBObject 

from lsst.sims.catalogs.definitions import InstanceCatalog 

 

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

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

class ConnectionPassingTest(unittest.TestCase): 

""" 

This will test whether we can can construct InstanceCatalogs 

containing multiple classes of object using only a single 

connection to the database. 

""" 

 

@classmethod 

def write_star_txt(cls): 

np.random.seed(77) 

cls.n_stars = 20 

cls.star_ra = np.random.random_sample(cls.n_stars)*360.0 

cls.star_dec = (np.random.random_sample(cls.n_stars)-0.5)*180.0 

cls.star_umag = np.random.random_sample(cls.n_stars)*10.0 + 15.0 

cls.star_gmag = np.random.random_sample(cls.n_stars)*10.0 + 15.0 

 

cls.star_txt_name = os.path.join(cls.scratch_dir, 

'ConnectionPassingTestStars.txt') 

 

40 ↛ 41line 40 didn't jump to line 41, because the condition on line 40 was never true if os.path.exists(cls.star_txt_name): 

os.unlink(cls.star_txt_name) 

 

with open(cls.star_txt_name, 'w') as output_file: 

output_file.write("#id raJ2000 decJ2000 umag gmag\n") 

for ix in range(cls.n_stars): 

output_file.write("%d %.4f %.4f %.4f %.4f\n" 

% (ix, cls.star_ra[ix], cls.star_dec[ix], 

cls.star_umag[ix], cls.star_gmag[ix])) 

 

@classmethod 

def write_galaxy_txt(cls): 

np.random.seed(88) 

cls.n_galaxies = 100 

cls.gal_ra = np.random.random_sample(cls.n_galaxies)*360.0 

cls.gal_dec = (np.random.random_sample(cls.n_galaxies)-0.5)*180.0 

cls.gal_redshift = np.random.random_sample(cls.n_galaxies)*5.0 

cls.gal_umag = np.random.random_sample(cls.n_galaxies)*10.0+21.0 

cls.gal_gmag = np.random.random_sample(cls.n_galaxies)*10.0+21.0 

 

cls.gal_txt_name = os.path.join(cls.scratch_dir, 

'ConnectionPassingTestGal.txt') 

 

63 ↛ 64line 63 didn't jump to line 64, because the condition on line 63 was never true if os.path.exists(cls.gal_txt_name): 

os.unlink(cls.gal_txt_name) 

 

with open(cls.gal_txt_name, 'w') as output_file: 

output_file.write("#id raJ2000 decJ2000 redshift umag gmag\n") 

for ix in range(cls.n_galaxies): 

output_file.write("%d %.4f %.4f %.4f %.4f %.4f\n" 

% (ix, cls.gal_ra[ix], cls.gal_dec[ix], 

cls.gal_redshift[ix], 

cls.gal_umag[ix], cls.gal_gmag[ix])) 

 

@classmethod 

def setUpClass(cls): 

 

cls.scratch_dir = tempfile.mkdtemp(dir=ROOT, prefix="scratchSpace-") 

cls.write_star_txt() 

cls.write_galaxy_txt() 

 

cls.dbName = os.path.join(cls.scratch_dir, 'ConnectionPassingTestDB.db') 

 

83 ↛ 84line 83 didn't jump to line 84, because the condition on line 83 was never true if os.path.exists(cls.dbName): 

os.unlink(cls.dbName) 

 

galDtype = np.dtype([('id', np.int), 

('raJ2000', np.float), ('decJ2000', np.float), 

('redshift', np.float), ('umag', np.float), 

('gmag', np.float)]) 

 

starDtype = np.dtype([('id', np.int), ('raJ2000', np.float), 

('decJ2000', np.float), ('umag', np.float), 

('gmag', np.float)]) 

 

fileDBObject(cls.star_txt_name, 

database=cls.dbName, driver='sqlite', 

runtable='stars', idColKey='id', 

dtype=starDtype) 

 

fileDBObject(cls.gal_txt_name, 

database=cls.dbName, driver='sqlite', 

runtable='galaxies', idColKey='id', 

dtype=galDtype) 

 

@classmethod 

def tearDownClass(cls): 

sims_clean_up() 

108 ↛ 111line 108 didn't jump to line 111, because the condition on line 108 was never false if os.path.exists(cls.dbName): 

os.unlink(cls.dbName) 

 

111 ↛ 114line 111 didn't jump to line 114, because the condition on line 111 was never false if os.path.exists(cls.star_txt_name): 

os.unlink(cls.star_txt_name) 

 

114 ↛ 117line 114 didn't jump to line 117, because the condition on line 114 was never false if os.path.exists(cls.gal_txt_name): 

os.unlink(cls.gal_txt_name) 

 

117 ↛ exitline 117 didn't return from function 'tearDownClass', because the condition on line 117 was never false if os.path.exists(cls.scratch_dir): 

shutil.rmtree(cls.scratch_dir) 

 

def test_passing(self): 

""" 

Test that we can produce a catalog of multiple object types 

drawn from different tables of the same database by passing 

DBConnections 

""" 

 

class starDBObj(CatalogDBObject): 

database = self.dbName 

driver = 'sqlite' 

tableid = 'stars' 

idColKey = 'id' 

 

class galDBObj(CatalogDBObject): 

database = self.dbName 

driver = 'sqlite' 

tableid = 'galaxies' 

idColKey = 'id' 

 

class starCatalog(InstanceCatalog): 

column_outputs = ['id', 'raJ2000', 'decJ2000', 

'gmag', 'umag'] 

 

default_formats = {'f': '%.4f'} 

 

class galCatalog(InstanceCatalog): 

column_outputs = ['id', 'decJ2000', 'raJ2000', 

'gmag', 'umag', 'redshift'] 

 

default_formats = {'f': '%.4f'} 

 

catName = os.path.join(self.scratch_dir, 

'ConnectionPassingTestOutputCatalog.txt') 

 

154 ↛ 155line 154 didn't jump to line 155, because the condition on line 154 was never true if os.path.exists(catName): 

os.unlink(catName) 

 

stars = starDBObj() 

galaxies = galDBObj(connection=stars.connection) 

 

self.assertEqual(stars.connection, galaxies.connection) 

 

starCat = starCatalog(stars) 

galCat = galCatalog(galaxies) 

starCat.write_catalog(catName, chunk_size=5) 

galCat.write_catalog(catName, write_mode='a', chunk_size=5) 

 

with open(catName, 'r') as input_file: 

lines = input_file.readlines() 

self.assertEqual(len(lines), self.n_stars+self.n_galaxies+2) 

for ix in range(self.n_stars): 

vals = lines[ix+1].split(',') 

dex = np.int(vals[0]) 

self.assertEqual(round(self.star_ra[dex], 4), np.float(vals[1])) 

self.assertEqual(round(self.star_dec[dex], 4), np.float(vals[2])) 

self.assertEqual(round(self.star_gmag[dex], 4), np.float(vals[3])) 

self.assertEqual(round(self.star_umag[dex], 4), np.float(vals[4])) 

 

offset = 2 + self.n_stars 

for ix in range(self.n_galaxies): 

vals = lines[ix+offset].split(',') 

dex = np.int(vals[0]) 

self.assertEqual(round(self.gal_dec[dex], 4), np.float(vals[1])) 

self.assertEqual(round(self.gal_ra[dex], 4), np.float(vals[2])) 

self.assertEqual(round(self.gal_gmag[dex], 4), np.float(vals[3])) 

self.assertEqual(round(self.gal_umag[dex], 4), np.float(vals[4])) 

self.assertEqual(round(self.gal_redshift[dex], 4), np.float(vals[5])) 

 

188 ↛ exitline 188 didn't return from function 'test_passing', because the condition on line 188 was never false if os.path.exists(catName): 

os.unlink(catName) 

 

 

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

pass 

 

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

lsst.utils.tests.init() 

unittest.main()