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

# 

# LSST Data Management System 

# Copyright 2008, 2009, 2010 LSST Corporation. 

# 

# 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 <http://www.lsstcorp.org/LegalNotices/>. 

# 

 

""" 

Tests for table.Schema 

 

Run with: 

./testSchema.py 

or 

python 

>>> import testSchema; testSchema.run() 

""" 

 

import unittest 

 

import numpy as np 

 

import lsst.utils.tests 

import lsst.pex.exceptions 

import lsst.geom 

import lsst.afw.table 

 

try: 

type(display) 

except NameError: 

display = False 

 

 

class SchemaTestCase(unittest.TestCase): 

 

def testSchema(self): 

def testKey(name, key): 

col = schema.find(name) 

self.assertEqual(col.key, key) 

self.assertEqual(col.field.getName(), name) 

 

schema = lsst.afw.table.Schema() 

ab_k = lsst.afw.table.CoordKey.addFields(schema, "a_b", "parent coord") 

abp_k = lsst.afw.table.Point2DKey.addFields( 

schema, "a_b_p", "point", "pixel") 

abi_k = schema.addField("a_b_i", type=np.int32, doc="int") 

acf_k = schema.addField("a_c_f", type=np.float32, doc="float") 

egd_k = schema.addField("e_g_d", type=lsst.geom.Angle, doc="angle") 

 

# Basic test for all native key types. 

for name, key in (("a_b_i", abi_k), ("a_c_f", acf_k), ("e_g_d", egd_k)): 

testKey(name, key) 

 

# Extra tests for special types 

self.assertEqual(ab_k.getRa(), schema["a_b_ra"].asKey()) 

abpx_si = schema.find("a_b_p_x") 

self.assertEqual(abp_k.getX(), abpx_si.key) 

self.assertEqual(abpx_si.field.getName(), "a_b_p_x") 

self.assertEqual(abpx_si.field.getDoc(), "point") 

self.assertEqual(abp_k.getX(), schema["a_b_p_x"].asKey()) 

self.assertEqual(schema.getNames(), {'a_b_dec', 'a_b_i', 'a_b_p_x', 'a_b_p_y', 'a_b_ra', 'a_c_f', 

'e_g_d'}) 

self.assertEqual(schema.getNames(True), {"a", "e"}) 

self.assertEqual(schema["a"].getNames(), { 

'b_dec', 'b_i', 'b_p_x', 'b_p_y', 'b_ra', 'c_f'}) 

self.assertEqual(schema["a"].getNames(True), {"b", "c"}) 

schema2 = lsst.afw.table.Schema(schema) 

self.assertEqual(schema, schema2) 

schema2.addField("q", type="F", doc="another double") 

self.assertNotEqual(schema, schema2) 

schema3 = lsst.afw.table.Schema() 

schema3.addField("ra", type="Angle", doc="coord_ra") 

schema3.addField("dec", type="Angle", doc="coord_dec") 

schema3.addField("x", type="D", doc="position_x") 

schema3.addField("y", type="D", doc="position_y") 

schema3.addField("i", type="I", doc="int") 

schema3.addField("f", type="F", doc="float") 

schema3.addField("d", type="Angle", doc="angle") 

self.assertEqual(schema3, schema) 

schema4 = lsst.afw.table.Schema() 

keys = [] 

keys.append(schema4.addField("a", type="Angle", doc="a")) 

keys.append(schema4.addField("b", type="Flag", doc="b")) 

keys.append(schema4.addField("c", type="I", doc="c")) 

keys.append(schema4.addField("d", type="Flag", doc="d")) 

self.assertEqual(keys[1].getBit(), 0) 

self.assertEqual(keys[3].getBit(), 1) 

for n1, n2 in zip(schema4.getOrderedNames(), "abcd"): 

self.assertEqual(n1, n2) 

keys2 = [x.key for x in schema4] 

self.assertEqual(keys, keys2) 

 

def testUnits(self): 

schema = lsst.afw.table.Schema() 

# first insert some valid units 

schema.addField("a", type="I", units="pixel") 

schema.addField("b", type="I", units="m2") 

schema.addField("c", type="I", units="electron / adu") 

schema.addField("d", type="I", units="kg m s^(-2)") 

schema.addField("e", type="I", units="GHz / Mpc") 

schema.addField("f", type="Angle", units="deg") 

schema.addField("g", type="Angle", units="rad") 

schema.checkUnits() 

# now try inserting invalid units 

with self.assertRaises(ValueError): 

schema.addField("a", type="I", units="camel") 

with self.assertRaises(ValueError): 

schema.addField("b", type="I", units="pixels^2^2") 

# add invalid units in silent mode, should work fine 

schema.addField("h", type="I", units="lala", parse_strict='silent') 

# Now this check should raise because there is an invalid unit 

with self.assertRaises(ValueError): 

schema.checkUnits() 

 

def testInspection(self): 

schema = lsst.afw.table.Schema() 

keys = [] 

keys.append(schema.addField("d", type=np.int32)) 

keys.append(schema.addField("c", type=np.float32)) 

keys.append(schema.addField("b", type="ArrayF", size=3)) 

keys.append(schema.addField("a", type="F")) 

for key, item in zip(keys, schema): 

self.assertEqual(item.key, key) 

self.assertIn(key, schema) 

for name in ("a", "b", "c", "d"): 

self.assertIn(name, schema) 

self.assertNotIn("e", schema) 

otherSchema = lsst.afw.table.Schema() 

otherKey = otherSchema.addField("d", type=np.float32) 

self.assertNotIn(otherKey, schema) 

self.assertNotEqual(keys[0], keys[1]) 

 

def testKeyAccessors(self): 

schema = lsst.afw.table.Schema() 

arrayKey = schema.addField( 

"a", type="ArrayF", doc="doc for array field", size=5) 

arrayElementKey = arrayKey[1] 

self.assertEqual(lsst.afw.table.Key["F"], type(arrayElementKey)) 

 

def testComparison(self): 

schema1 = lsst.afw.table.Schema() 

schema1.addField("a", type=np.float32, doc="doc for a", units="m") 

schema1.addField("b", type=np.int32, doc="doc for b", units="s") 

schema2 = lsst.afw.table.Schema() 

schema2.addField("a", type=np.int32, doc="doc for a", units="m") 

schema2.addField("b", type=np.float32, doc="doc for b", units="s") 

cmp1 = schema1.compare(schema2, lsst.afw.table.Schema.IDENTICAL) 

self.assertTrue(cmp1 & lsst.afw.table.Schema.EQUAL_NAMES) 

self.assertTrue(cmp1 & lsst.afw.table.Schema.EQUAL_DOCS) 

self.assertTrue(cmp1 & lsst.afw.table.Schema.EQUAL_UNITS) 

self.assertFalse(cmp1 & lsst.afw.table.Schema.EQUAL_KEYS) 

schema3 = lsst.afw.table.Schema(schema1) 

schema3.addField("c", type=str, doc="doc for c", size=4) 

self.assertFalse(schema1.compare(schema3)) 

self.assertFalse(schema1.contains(schema3)) 

self.assertTrue(schema3.contains(schema1)) 

schema1.addField("d", type=str, doc="no docs!", size=4) 

cmp2 = schema1.compare(schema3, lsst.afw.table.Schema.IDENTICAL) 

self.assertFalse(cmp2 & lsst.afw.table.Schema.EQUAL_NAMES) 

self.assertFalse(cmp2 & lsst.afw.table.Schema.EQUAL_DOCS) 

self.assertTrue(cmp2 & lsst.afw.table.Schema.EQUAL_KEYS) 

self.assertTrue(cmp2 & lsst.afw.table.Schema.EQUAL_UNITS) 

self.assertFalse(schema1.compare( 

schema3, lsst.afw.table.Schema.EQUAL_NAMES)) 

 

 

class SchemaMapperTestCase(unittest.TestCase): 

 

def testJoin(self): 

inputs = [lsst.afw.table.Schema(), lsst.afw.table.Schema(), 

lsst.afw.table.Schema()] 

prefixes = ["u", "v", "w"] 

ka = inputs[0].addField("a", type=np.float64, doc="doc for a") 

kb = inputs[0].addField("b", type=np.int32, doc="doc for b") 

kc = inputs[1].addField("c", type=np.float32, doc="doc for c") 

kd = inputs[2].addField("d", type=np.int64, doc="doc for d") 

flags1 = lsst.afw.table.Schema.IDENTICAL 

flags2 = flags1 & ~lsst.afw.table.Schema.EQUAL_NAMES 

mappers1 = lsst.afw.table.SchemaMapper.join(inputs) 

mappers2 = lsst.afw.table.SchemaMapper.join(inputs, prefixes) 

records = [lsst.afw.table.BaseTable.make(schema).makeRecord() for 

schema in inputs] 

records[0].set(ka, 3.14159) 

records[0].set(kb, 21623) 

records[1].set(kc, 1.5616) 

records[2].set(kd, 1261236) 

for mappers, flags in zip((mappers1, mappers2), (flags1, flags2)): 

output = lsst.afw.table.BaseTable.make( 

mappers[0].getOutputSchema()).makeRecord() 

for mapper, record in zip(mappers, records): 

output.assign(record, mapper) 

self.assertEqual( 

mapper.getOutputSchema().compare(output.getSchema(), 

flags), 

flags) 

self.assertEqual( 

mapper.getInputSchema().compare(record.getSchema(), 

flags), 

flags) 

names = output.getSchema().getOrderedNames() 

self.assertEqual(output.get(names[0]), records[0].get(ka)) 

self.assertEqual(output.get(names[1]), records[0].get(kb)) 

self.assertEqual(output.get(names[2]), records[1].get(kc)) 

self.assertEqual(output.get(names[3]), records[2].get(kd)) 

 

def testMinimalSchema(self): 

front = lsst.afw.table.Schema() 

ka = front.addField("a", type=np.float64, doc="doc for a") 

kb = front.addField("b", type=np.int32, doc="doc for b") 

full = lsst.afw.table.Schema(front) 

kc = full.addField("c", type=np.float32, doc="doc for c") 

kd = full.addField("d", type=np.int64, doc="doc for d") 

mapper1 = lsst.afw.table.SchemaMapper(full) 

mapper2 = lsst.afw.table.SchemaMapper(full) 

mapper3 = lsst.afw.table.SchemaMapper.removeMinimalSchema(full, front) 

mapper1.addMinimalSchema(front) 

mapper2.addMinimalSchema(front, False) 

self.assertIn(ka, mapper1.getOutputSchema()) 

self.assertIn(kb, mapper1.getOutputSchema()) 

self.assertNotIn(kc, mapper1.getOutputSchema()) 

self.assertNotIn(kd, mapper1.getOutputSchema()) 

self.assertIn(ka, mapper2.getOutputSchema()) 

self.assertIn(kb, mapper2.getOutputSchema()) 

self.assertNotIn(kc, mapper2.getOutputSchema()) 

self.assertNotIn(kd, mapper2.getOutputSchema()) 

self.assertNotIn(ka, mapper3.getOutputSchema()) 

self.assertNotIn(kb, mapper3.getOutputSchema()) 

self.assertNotIn(kc, mapper3.getOutputSchema()) 

self.assertNotIn(kd, mapper3.getOutputSchema()) 

inputRecord = lsst.afw.table.BaseTable.make(full).makeRecord() 

inputRecord.set(ka, np.pi) 

inputRecord.set(kb, 2) 

inputRecord.set(kc, np.exp(1)) 

inputRecord.set(kd, 4) 

outputRecord1 = lsst.afw.table.BaseTable.make( 

mapper1.getOutputSchema()).makeRecord() 

outputRecord1.assign(inputRecord, mapper1) 

self.assertEqual(inputRecord.get(ka), outputRecord1.get(ka)) 

self.assertEqual(inputRecord.get(kb), outputRecord1.get(kb)) 

 

def testOutputSchema(self): 

mapper = lsst.afw.table.SchemaMapper(lsst.afw.table.Schema()) 

out1 = mapper.getOutputSchema() 

out2 = mapper.editOutputSchema() 

k1 = out1.addField("a1", type=np.int32) 

self.assertNotIn(k1, mapper.getOutputSchema()) 

self.assertIn(k1, out1) 

self.assertNotIn(k1, out2) 

k2 = mapper.addOutputField( 

lsst.afw.table.Field[np.float32]("a2", "doc for a2")) 

self.assertNotIn(k2, out1) 

self.assertIn(k2, mapper.getOutputSchema()) 

self.assertIn(k2, out2) 

k3 = out2.addField("a3", type=np.float32, doc="doc for a3") 

self.assertNotIn(k3, out1) 

self.assertIn(k3, mapper.getOutputSchema()) 

self.assertIn(k3, out2) 

self.assertIn(k2, out2) 

 

def testDoReplace(self): 

inSchema = lsst.afw.table.Schema() 

ka = inSchema.addField("a", type=np.int32) 

outSchema = lsst.afw.table.Schema(inSchema) 

kb = outSchema.addField("b", type=np.int32) 

kc = outSchema.addField("c", type=np.int32) 

mapper1 = lsst.afw.table.SchemaMapper(inSchema, outSchema) 

mapper1.addMapping(ka, True) 

self.assertEqual(mapper1.getMapping(ka), ka) 

mapper2 = lsst.afw.table.SchemaMapper(inSchema, outSchema) 

mapper2.addMapping( 

ka, lsst.afw.table.Field[np.int32]("b", "doc for b"), True) 

self.assertEqual(mapper2.getMapping(ka), kb) 

mapper3 = lsst.afw.table.SchemaMapper(inSchema, outSchema) 

mapper3.addMapping(ka, "c", True) 

self.assertEqual(mapper3.getMapping(ka), kc) 

 

def testJoin2(self): 

s1 = lsst.afw.table.Schema() 

self.assertEqual(s1.join("a", "b"), "a_b") 

self.assertEqual(s1.join("a", "b", "c"), "a_b_c") 

self.assertEqual(s1.join("a", "b", "c", "d"), "a_b_c_d") 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()