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

# 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 os 

import unittest 

import tempfile 

import shutil 

 

import lsst.utils.tests 

from lsst.utils import doImport 

 

from lsst.daf.butler import StorageClassFactory 

from lsst.daf.butler.datastores.posixDatastore import DatastoreConfig 

 

from datasetsHelper import FitsCatalogDatasetsHelper, DatasetTestHelper, DatastoreTestHelper 

 

from dummyRegistry import DummyRegistry 

 

try: 

import lsst.afw.table 

import lsst.afw.image 

import lsst.geom 

except ImportError: 

lsst.afw = None 

 

TESTDIR = os.path.dirname(__file__) 

 

 

class DatastoreFitsTests(FitsCatalogDatasetsHelper, DatasetTestHelper, DatastoreTestHelper): 

root = None 

 

@classmethod 

def setUpClass(cls): 

if lsst.afw is None: 

raise unittest.SkipTest("afw not available.") 

 

# Base classes need to know where the test directory is 

cls.testDir = TESTDIR 

 

# Storage Classes are fixed for all datastores in these tests 

scConfigFile = os.path.join(TESTDIR, "config/basic/storageClasses.yaml") 

cls.storageClassFactory = StorageClassFactory() 

cls.storageClassFactory.addFromConfig(scConfigFile) 

 

# Read the Datastore config so we can get the class 

# information (since we should not assume the constructor 

# name here, but rely on the configuration file itself) 

datastoreConfig = DatastoreConfig(cls.configFile) 

cls.datastoreType = doImport(datastoreConfig["cls"]) 

 

def setUp(self): 

self.setUpDatastoreTests(DummyRegistry, DatastoreConfig) 

 

def tearDown(self): 

if self.root is not None and os.path.exists(self.root): 

shutil.rmtree(self.root, ignore_errors=True) 

 

def testConstructor(self): 

datastore = self.makeDatastore() 

self.assertIsNotNone(datastore) 

 

def testBasicPutGet(self): 

catalog = self.makeExampleCatalog() 

datastore = self.makeDatastore() 

 

# Put 

dimensions = frozenset(("visit", "filter")) 

dataId = {"visit": 123456, "filter": "blue"} 

storageClass = self.storageClassFactory.getStorageClass("SourceCatalog") 

 

ref = self.makeDatasetRef("calexp", dimensions, storageClass, dataId) 

 

datastore.put(catalog, ref) 

 

# Does it exist? 

self.assertTrue(datastore.exists(ref)) 

 

uri = datastore.getUri(ref) 

if self.fileExt is not None: 

self.assertTrue(uri.endswith(self.fileExt)) 

self.assertTrue(uri.startswith(self.uriScheme)) 

 

# Get 

catalogOut = datastore.get(ref, parameters=None) 

self.assertCatalogEqual(catalog, catalogOut) 

 

# These should raise 

ref = self.makeDatasetRef("calexp2", dimensions, storageClass, dataId) 

with self.assertRaises(FileNotFoundError): 

# non-existing file 

datastore.get(ref, parameters=None) 

 

def testRemove(self): 

catalog = self.makeExampleCatalog() 

datastore = self.makeDatastore() 

 

# Put 

storageClass = self.storageClassFactory.getStorageClass("SourceCatalog") 

dimensions = frozenset(("visit", "filter")) 

dataId = {"visit": 1234567, "filter": "blue"} 

 

ref = self.makeDatasetRef("calexp", dimensions, storageClass, dataId) 

datastore.put(catalog, ref) 

 

# Does it exist? 

self.assertTrue(datastore.exists(ref)) 

 

# Get 

catalogOut = datastore.get(ref) 

self.assertCatalogEqual(catalog, catalogOut) 

 

# Remove 

datastore.remove(ref) 

 

# Does it exist? 

self.assertFalse(datastore.exists(ref)) 

 

# Get should now fail 

with self.assertRaises(FileNotFoundError): 

datastore.get(ref) 

# Can only delete once 

with self.assertRaises(FileNotFoundError): 

datastore.remove(ref) 

 

def testTransfer(self): 

catalog = self.makeExampleCatalog() 

dimensions = frozenset(("visit", "filter")) 

dataId = {"visit": 12345, "filter": "red"} 

 

storageClass = self.storageClassFactory.getStorageClass("SourceCatalog") 

ref = self.makeDatasetRef("calexp", dimensions, storageClass, dataId) 

 

inputDatastore = self.makeDatastore("test_input_datastore") 

outputDatastore = self.makeDatastore("test_output_datastore") 

 

inputDatastore.put(catalog, ref) 

outputDatastore.transfer(inputDatastore, ref) 

 

catalogOut = outputDatastore.get(ref) 

self.assertCatalogEqual(catalog, catalogOut) 

 

def testExposurePutGet(self): 

example = os.path.join(self.testDir, "data", "basic", "small.fits") 

exposure = lsst.afw.image.ExposureF(example) 

datastore = self.makeDatastore() 

# Put 

dimensions = frozenset(("visit", "filter")) 

dataId = {"visit": 231, "filter": "Fc"} 

storageClass = datastore.storageClassFactory.getStorageClass("ExposureF") 

ref = self.makeDatasetRef("calexp", dimensions, storageClass, dataId) 

 

datastore.put(exposure, ref) 

 

# Does it exist? 

self.assertTrue(datastore.exists(ref)) 

 

# Get 

exposureOut = datastore.get(ref) 

self.assertEqual(type(exposure), type(exposureOut)) 

 

# Get some components 

for compName in ("wcs", "image", "mask", "coaddInputs", "psf"): 

compRef = self.makeDatasetRef(ref.datasetType.componentTypeName(compName), dimensions, 

storageClass.components[compName], dataId, id=ref.id) 

component = datastore.get(compRef) 

self.assertIsInstance(component, compRef.datasetType.storageClass.pytype) 

 

# Get the WCS component to check it 

wcsRef = self.makeDatasetRef(ref.datasetType.componentTypeName("wcs"), dimensions, 

storageClass.components["wcs"], dataId, id=ref.id) 

wcs = datastore.get(wcsRef) 

 

# Simple check of WCS 

bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), 

lsst.geom.Extent2I(9, 9)) 

self.assertWcsAlmostEqualOverBBox(wcs, exposure.getWcs(), bbox) 

 

def testExposureCompositePutGet(self): 

example = os.path.join(self.testDir, "data", "basic", "small.fits") 

exposure = lsst.afw.image.ExposureF(example) 

datastore = self.makeDatastore() 

# Put 

dimensions = frozenset(("visit", "filter")) 

dataId = {"visit": 23, "filter": "F"} 

storageClass = datastore.storageClassFactory.getStorageClass("ExposureCompositeF") 

ref = self.makeDatasetRef("calexp", dimensions, storageClass, dataId) 

 

# Get the predicted URI 

self.assertFalse(datastore.exists(ref)) 

uri = datastore.getUri(ref, predict=True) 

self.assertTrue(uri.endswith("#predicted")) 

 

components = storageClass.assembler().disassemble(exposure) 

self.assertTrue(components) 

 

# Get a component 

compsRead = {} 

for compName in ("wcs", "image", "mask", "coaddInputs", "psf"): 

compRef = self.makeDatasetRef(ref.datasetType.componentTypeName(compName), dimensions, 

components[compName].storageClass, dataId) 

 

datastore.put(components[compName].component, compRef) 

 

# Does it exist? 

self.assertTrue(datastore.exists(compRef)) 

 

component = datastore.get(compRef) 

self.assertIsInstance(component, compRef.datasetType.storageClass.pytype) 

compsRead[compName] = component 

 

# Simple check of WCS 

bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), 

lsst.geom.Extent2I(9, 9)) 

self.assertWcsAlmostEqualOverBBox(compsRead["wcs"], exposure.getWcs(), bbox) 

 

# Try to reassemble the exposure 

retrievedExposure = storageClass.assembler().assemble(compsRead) 

self.assertIsInstance(retrievedExposure, type(exposure)) 

 

 

class PosixDatastoreTestCase(DatastoreFitsTests, lsst.utils.tests.TestCase): 

"""PosixDatastore specialization""" 

configFile = os.path.join(TESTDIR, "config/basic/butler.yaml") 

uriScheme = "file:" 

fileExt = ".fits" 

 

def setUp(self): 

# Override the working directory before calling the base class 

self.root = tempfile.mkdtemp(dir=TESTDIR) 

super().setUp() 

 

 

class InMemoryDatastoreTestCase(DatastoreFitsTests, lsst.utils.tests.TestCase): 

"""PosixDatastore specialization""" 

configFile = os.path.join(TESTDIR, "config/basic/inMemoryDatastore.yaml") 

uriScheme = "mem:" 

fileExt = None 

 

 

class ChainedDatastoreTestCase(PosixDatastoreTestCase): 

"""PosixDatastore specialization""" 

configFile = os.path.join(TESTDIR, "config/basic/chainedDatastore.yaml") 

 

 

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

unittest.main()