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

# This file is part of daf_butler. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (http://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 <http://www.gnu.org/licenses/>. 

 

import unittest 

import itertools 

 

from lsst.daf.butler import DimensionUniverse, Dimension, DimensionJoin 

 

 

class DimensionTestCase(unittest.TestCase): 

"""Tests for dimensions. 

 

All tests here rely on the content of ``config/dimensions.yaml``, either 

to test that the definitions there are read in properly or just as generic 

data for testing various operations. 

""" 

 

def setUp(self): 

self.universe = DimensionUniverse.fromConfig() 

 

def checkSetInvariants(self, dimensions): 

"""Run tests on DimensionSet that should pass for any instance. 

""" 

# DimensionSet should be interoperable with regular sets of 

# DimensionElements and regular sets of their names 

self.assertEqual(dimensions, set(dimensions)) 

self.assertEqual(dimensions, set(dimensions.names)) 

self.assertLessEqual(dimensions, set(dimensions)) 

self.assertLessEqual(dimensions, set(dimensions.names)) 

self.assertGreaterEqual(dimensions, set(dimensions)) 

self.assertGreaterEqual(dimensions, set(dimensions.names)) 

self.assertFalse(dimensions < set(dimensions)) 

self.assertFalse(dimensions < set(dimensions.names)) 

self.assertFalse(dimensions > set(dimensions)) 

self.assertFalse(dimensions > set(dimensions.names)) 

 

copy2 = dimensions.union(dimensions) 

copy3 = dimensions.intersection(dimensions) 

for name, copy in [("union", copy2), ("intersection", copy3)]: 

with self.subTest(copy=name): 

self.assertLessEqual(dimensions, copy) 

self.assertGreaterEqual(dimensions, copy) 

self.assertEqual(dimensions, copy) 

self.assertTrue(dimensions.issubset(copy)) 

self.assertTrue(dimensions.issuperset(copy)) 

self.assertFalse(dimensions != copy) 

self.assertFalse(dimensions < copy) 

self.assertFalse(dimensions > copy) 

self.assertTrue(not dimensions.isdisjoint(copy) or not dimensions) 

 

def checkGraphInvariants(self, graph): 

"""Run tests on DimensionGraph that should pass for any instance. 

""" 

self.checkSetInvariants(graph.toSet()) 

self.assertLessEqual(graph, self.universe) 

for dim in graph: 

self.assertIn(dim, graph) 

self.assertIn(dim.name, graph) 

self.assertIs(graph[dim.name], dim) 

self.assertIs(graph.get(dim.name), dim) 

self.assertIsInstance(dim, Dimension) 

self.assertEqual(dim.dependencies(), dim.dependencies(implied=False)) 

self.assertLessEqual(dim.dependencies(), dim.dependencies(implied=True)) 

self.assertLessEqual(dim.dependencies(), graph) 

 

self.checkSetInvariants(graph.joins(summaries=True)) 

for join in graph.joins(summaries=True): 

self.assertIsInstance(join, DimensionJoin) 

self.assertGreater(join.dependencies(), join.lhs) 

self.assertGreater(join.dependencies(), join.rhs) 

self.assertLessEqual(join.dependencies(), graph) 

self.checkSetInvariants(graph.joins(summaries=False)) 

for join in graph.joins(summaries=False): 

self.assertTrue(join.summarizes.isdisjoint(graph.joins())) 

 

copy2 = graph.union(graph) 

copy3 = graph.intersection(graph) 

for name, copy in [("union", copy2), ("intersection", copy3)]: 

with self.subTest(copy=name): 

self.assertLessEqual(graph, copy) 

self.assertGreaterEqual(graph, copy) 

self.assertEqual(graph, copy) 

self.assertTrue(graph.issubset(copy)) 

self.assertTrue(graph.issuperset(copy)) 

self.assertTrue(not graph.isdisjoint(copy) or not graph) 

self.assertFalse(graph != copy) 

self.assertFalse(graph < copy) 

self.assertFalse(graph > copy) 

 

def testInstrumentDimensions(self): 

"""Test that the Instrument dimensions and joins we expect to be 

defined in ``dimensions.yaml`` are present and related correctly. 

""" 

graph1 = self.universe.extract( 

dim for dim in self.universe if "Instrument" in dim.dependencies() 

) 

self.checkGraphInvariants(graph1) 

self.assertCountEqual(graph1.names, 

["Instrument", "Detector", "PhysicalFilter", "Visit", "Exposure", 

"CalibrationLabel"]) 

self.assertCountEqual(graph1.joins().names, ["VisitDetectorRegion", "ExposureCalibrationLabelJoin"]) 

self.assertEqual(graph1.getRegionHolder(), graph1.joins().get("VisitDetectorRegion")) 

graph2 = graph1.intersection(["Visit"]) 

self.assertCountEqual(graph2.names, ["Instrument", "Visit"]) 

self.assertEqual(graph2.getRegionHolder(), graph1["Visit"]) 

self.assertCountEqual(graph2.joins().names, []) 

self.checkGraphInvariants(graph2) 

graph3 = graph1.intersection(["Detector"]) 

self.checkGraphInvariants(graph3) 

self.assertCountEqual(graph3.names, ["Instrument", "Detector"]) 

self.assertIsNone(graph3.getRegionHolder()) 

self.assertCountEqual(graph3.joins(), []) 

visit = self.universe["Visit"] 

self.assertCountEqual(visit.dependencies(implied=True).names, 

["Instrument", "PhysicalFilter"]) 

self.assertCountEqual(visit.dependencies(implied=False).names, ["Instrument"]) 

 

def testSkyMapDimensions(self): 

"""Test that the SkyMap dimensions and joins we expect to be defined 

in ``dimensions.yaml`` are present and related correctly. 

""" 

patchGraph = self.universe.extract(["Patch"]) 

self.checkGraphInvariants(patchGraph) 

self.assertCountEqual(patchGraph.names, ["SkyMap", "Tract", "Patch"]) 

self.assertCountEqual(patchGraph.joins(), []) 

self.assertEqual(patchGraph.getRegionHolder(), patchGraph["Patch"]) 

tractGraph = patchGraph.intersection(["Tract"]) 

self.checkGraphInvariants(tractGraph) 

self.assertCountEqual(tractGraph.names, ["SkyMap", "Tract"]) 

self.assertEqual(tractGraph.getRegionHolder(), tractGraph["Tract"]) 

self.assertCountEqual(tractGraph.joins(), []) 

skyMapOnly = tractGraph.intersection(["SkyMap"]) 

self.checkGraphInvariants(skyMapOnly) 

self.assertCountEqual(skyMapOnly.names, ["SkyMap"]) 

self.assertIsNone(skyMapOnly.getRegionHolder()) 

self.assertCountEqual(skyMapOnly.joins(), []) 

 

def testMiscDimensions(self): 

"""Test that the miscelleneous dimensions and joins we expect to be 

defined in ``dimensions.yaml`` are present and related correctly. 

""" 

def predicate(dim): 

return () 

 

misc = self.universe.extract( 

dim for dim in self.universe if ( 

dim.dependencies().names.isdisjoint(["Instrument", "SkyMap"]) and 

dim.name not in ("Instrument", "SkyMap") 

) 

) 

self.checkGraphInvariants(misc) 

self.assertCountEqual(misc.names, ["SkyPix", "Label", "AbstractFilter"]) 

self.assertCountEqual(misc.joins(), []) 

self.assertEqual(misc.getRegionHolder(), misc["SkyPix"]) 

 

def checkSpatialJoin(self, lhsNames, rhsNames, joinName=None): 

"""Test the spatial join that relates the given dimensions. 

 

Parameters 

---------- 

lhsNames : `list` of `str` 

Name of the Dimensions of the left-hand side of the join. 

rhsNames : `list` of `str` 

Name of the Dimensions of the right-hand side of the join. 

joinName : `str`, implied 

Name of the DimensionJoin to be tested; if `None`, computed by 

concatenating ``lhsNames`` and ``rhsNames``. 

""" 

if joinName is None: 

joinName = "{}{}Join".format("".join(lhsNames), "".join(rhsNames)) 

lhs = self.universe.extract(lhsNames) 

rhs = self.universe.extract(rhsNames) 

both = self.universe.extract(joins=[joinName]) 

self.checkGraphInvariants(both) 

join = both.joins().get(joinName) 

self.assertIsNotNone(join) 

self.assertLess(lhs, both) 

self.assertGreater(both, rhs) 

self.assertGreaterEqual(lhs, join.lhs) # [lr]hs has implieds, join.[lr]hs does not 

self.assertGreaterEqual(rhs, join.rhs) 

allExpectedJoins = set([join]).union(lhs.joins(summaries=False), rhs.joins(summaries=False)) 

self.assertEqual(both.joins(summaries=False), allExpectedJoins) 

 

def testSpatialJoins(self): 

"""Test that the spatial joins defined in ``dimensions.yaml`` are 

present and related correctly. 

""" 

self.checkSpatialJoin(["Tract"], ["SkyPix"]) 

self.checkSpatialJoin(["Patch"], ["SkyPix"]) 

self.checkSpatialJoin(["Visit"], ["SkyPix"]) 

self.checkSpatialJoin(["Visit", "Detector"], ["SkyPix"]) 

self.checkSpatialJoin(["Visit"], ["Tract"]) 

self.checkSpatialJoin(["Visit", "Detector"], ["Tract"]) 

self.checkSpatialJoin(["Visit"], ["Patch"]) 

self.checkSpatialJoin(["Visit", "Detector"], ["Patch"]) 

 

def testGraphSetOperations(self): 

"""Test set-like operations on DimensionGraph. 

 

Also provides test coverage of DimensionSet, because that's what 

DimensionGraph delegates to. 

""" 

# characters in the keys (interpreted as sets) have same expected 

# relationships as the corresponding values 

graphs = { 

# expands to [Detector, Instrument] 

"di": self.universe.extract(["Detector"]), 

# expands to [PhysicalFilter, Instrument, AbstractFilter] 

"pia": self.universe.extract(["PhysicalFilter"], implied=True), 

# expands to [Visit, PhysicalFilter, Instrument, AbstractFilter] 

"vpia": self.universe.extract(["Visit"], implied=True), 

# expands to [Tract, SkyMap] 

"ts": self.universe.extract(["Tract"]), 

# empty 

"": self.universe.extract([]), 

} 

# A big loop to test all of the combinations we can predict 

# mechanically (many of these are trivial). 

for (lhsName, lhsGraph), (rhsName, rhsGraph) in itertools.product(graphs.items(), repeat=2): 

with self.subTest(lhs=lhsName, rhs=rhsName): 

lhsChars = frozenset(lhsName) 

rhsChars = frozenset(rhsName) 

self.assertEqual(lhsChars == rhsChars, lhsGraph == rhsGraph) 

self.assertEqual(lhsChars != rhsChars, lhsGraph != rhsGraph) 

self.assertEqual(lhsChars <= rhsChars, lhsGraph <= rhsGraph) 

self.assertEqual(lhsChars >= rhsChars, lhsGraph >= rhsGraph) 

self.assertEqual(lhsChars < rhsChars, lhsGraph < rhsGraph) 

self.assertEqual(lhsChars > rhsChars, lhsGraph > rhsGraph) 

self.assertEqual(lhsChars.issubset(rhsChars), lhsGraph.issubset(rhsGraph)) 

self.assertEqual(lhsChars.issuperset(rhsChars), lhsGraph.issuperset(rhsGraph)) 

self.assertEqual(lhsChars.isdisjoint(rhsChars), lhsGraph.isdisjoint(rhsGraph)) 

self.assertEqual(lhsGraph.intersection(rhsGraph), lhsGraph & rhsGraph) 

self.assertEqual(lhsGraph.union(rhsGraph), lhsGraph | rhsGraph) 

 

# A few more spot-checks for graph-creating operations to make sure 

# we get exactly what we expect in those cases. 

self.assertEqual(graphs["di"] | graphs["ts"], 

self.universe.extract(["Detector", "Tract"])) 

self.assertEqual(graphs["di"] & graphs["ts"], 

self.universe.extract([])) 

self.assertEqual(graphs["di"] | graphs["pia"], 

self.universe.extract(["Detector", "PhysicalFilter"], implied=True)) 

self.assertEqual(graphs["di"] & graphs["pia"], self.universe.extract(["Instrument"])) 

self.assertEqual(graphs["vpia"] | graphs["pia"], graphs["vpia"]) 

self.assertEqual(graphs["vpia"] & graphs["pia"], graphs["pia"]) 

 

def testDimensionJoinSetOperations(self): 

"""Test set-like operations on DimensionSet with joins. 

""" 

# characters in the keys (interepreted as sets) have same expected 

# relationships as the corresponding values 

joins = { 

"t": self.universe.joins().intersection(["TractSkyPixJoin"]), 

"dt": self.universe.joins().intersection(["VisitDetectorSkyPixJoin", "TractSkyPixJoin"]), 

"pt": self.universe.joins().intersection(["PatchSkyPixJoin", "TractSkyPixJoin"]), 

"v": self.universe.joins().intersection(["VisitSkyPixJoin"]), 

"": self.universe.joins().intersection([]), 

} 

# A big loop to test all of the combinations we can predict 

# mechanically. 

for (lhsName, lhs), (rhsName, rhs) in itertools.product(joins.items(), repeat=2): 

with self.subTest(lhs=lhsName, rhs=rhsName): 

# Make regular Python sets with the same contents; they'll be 

# sorted differently, but should otherwise behave the same. 

lhsSet = set(lhs) 

rhsSet = set(rhs) 

self.assertCountEqual(lhsSet, lhs) 

self.assertCountEqual(rhsSet, rhs) 

self.assertEqual(lhsSet == rhsSet, lhs == rhs) 

self.assertEqual(lhsSet != rhsSet, lhs != rhs) 

self.assertEqual(lhsSet <= rhsSet, lhs <= rhs) 

self.assertEqual(lhsSet >= rhsSet, lhs >= rhs) 

self.assertEqual(lhsSet < rhsSet, lhs < rhs) 

self.assertEqual(lhsSet > rhsSet, lhs > rhs) 

self.assertEqual(lhsSet.issubset(rhsSet), lhs.issubset(rhs)) 

self.assertEqual(lhsSet.issuperset(rhsSet), lhs.issuperset(rhs)) 

self.assertEqual(lhsSet.isdisjoint(rhsSet), lhs.isdisjoint(rhs)) 

self.assertEqual(lhs.intersection(rhs), lhs & rhs) 

self.assertEqual(lhs.union(rhs), lhs | rhs) 

 

# A few more spot-checks for set-creating operations to make sure 

# we get exactly what we expect in those cases. 

self.assertEqual(joins["t"] | joins["pt"], joins["pt"]) 

self.assertEqual(joins["t"] & joins["pt"], joins["t"]) 

self.assertEqual(joins["t"] ^ joins["pt"], self.universe.joins().intersection(["PatchSkyPixJoin"])) 

self.assertEqual(joins["pt"] - joins["t"], self.universe.joins().intersection(["PatchSkyPixJoin"])) 

self.assertEqual(joins["dt"] | joins["pt"], 

self.universe.joins().intersection(["VisitDetectorSkyPixJoin", "TractSkyPixJoin", 

"PatchSkyPixJoin"])) 

self.assertEqual(joins["dt"] & joins["pt"], joins["t"]) 

self.assertEqual(joins["dt"] ^ joins["pt"], 

self.universe.joins().intersection(["VisitDetectorSkyPixJoin", "PatchSkyPixJoin"])) 

self.assertEqual(joins["t"] | joins[""], joins["t"]) 

self.assertEqual(joins["t"] & joins[""], joins[""]) 

self.assertEqual(joins["t"] ^ joins[""], joins["t"]) 

self.assertEqual(joins["t"] - joins[""], joins["t"]) 

self.assertEqual(joins["t"] | joins["v"], 

self.universe.joins().intersection(["TractSkyPixJoin", "VisitSkyPixJoin"])) 

self.assertEqual(joins["t"] & joins["v"], joins[""]) 

self.assertEqual(joins["t"] ^ joins["v"], 

self.universe.joins().intersection(["TractSkyPixJoin", "VisitSkyPixJoin"])) 

self.assertEqual(joins["t"] - joins["v"], joins["t"]) 

 

 

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

unittest.main()