Coverage for tests/test_gen3.py : 36%

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# This file is part of obs_lsst.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22import os
23import unittest
24import shutil
25import tempfile
26from cProfile import Profile
27from pstats import Stats
29import numpy as np
31from lsst.obs.lsst import (LsstCam, LsstComCam, LsstImSim, LsstPhoSim,
32 LsstTS8, LsstTS3, LsstUCDCam, Latiss)
34try:
35 from lsst.daf.butler import Butler, DatasetType, StorageClassFactory
36 haveGen3 = True
37except ImportError:
38 haveGen3 = False
40TESTDIR = os.path.abspath(os.path.dirname(__file__))
42# This test is unfortunately slow; leave a profiling option in in case we want
43# to improve it later. Initial version is about 60% YamlCamera construction
44# (!) and 40% SQLite operations inside Butler.
45PRINT_PROFILE = False
48@unittest.skipUnless(haveGen3, "daf_butler not setup")
49class TestInstruments(unittest.TestCase):
51 def setUp(self):
52 self.root = tempfile.mkdtemp(dir=TESTDIR)
53 self.rng = np.random.RandomState(50) # arbitrary deterministic seed
55 @classmethod
56 def setUpClass(cls):
57 if PRINT_PROFILE:
58 cls.profile = Profile()
59 cls.profile.enable()
61 def tearDown(self):
62 if self.root is not None and os.path.exists(self.root):
63 shutil.rmtree(self.root, ignore_errors=True)
65 @classmethod
66 def tearDownClass(cls):
67 if PRINT_PROFILE:
68 stats = Stats(cls.profile)
69 stats.strip_dirs()
70 stats.sort_stats("cumtime")
71 stats.print_stats()
73 def checkInstrumentWithRegistry(self, cls):
75 Butler.makeRepo(self.root)
76 butler = Butler(self.root, run="tests")
77 instrument = cls()
78 scFactory = StorageClassFactory()
80 # Add Instrument, Detector, and PhysicalFilter entries to the
81 # Butler Registry.
82 instrument.register(butler.registry)
84 # Define a DatasetType for the cameraGeom.Camera, which can be
85 # accessed just by identifying its Instrument.
86 # A real-world Camera DatasetType should be identified by a
87 # validity range as well.
88 cameraDatasetType = DatasetType("camera", dimensions=["instrument"],
89 storageClass=scFactory.getStorageClass("Camera"),
90 universe=butler.registry.dimensions)
91 butler.registry.registerDatasetType(cameraDatasetType)
93 # Define a DatasetType for cameraGeom.Detectors, which can be
94 # accessed by identifying its Instrument and (Butler) Detector.
95 # A real-world Detector DatasetType probably doesn't need to exist,
96 # as it would just duplicate information in the Camera, and
97 # reading a full Camera just to get a single Detector should be
98 # plenty efficient.
99 detectorDatasetType = DatasetType("detector", dimensions=["instrument", "detector"],
100 storageClass=scFactory.getStorageClass("Detector"),
101 universe=butler.registry.dimensions)
102 butler.registry.registerDatasetType(detectorDatasetType)
104 # Put and get the Camera.
105 dataId = dict(instrument=instrument.instrument)
106 butler.put(instrument.getCamera(), "camera", dataId=dataId)
107 camera = butler.get("camera", dataId)
108 # Full camera comparisons are *slow*; just compare names.
109 self.assertEqual(instrument.getCamera().getName(), camera.getName())
111 # Put and get a random subset of the Detectors.
112 allDetectors = list(instrument.getCamera())
113 numDetectors = min(3, len(allDetectors))
114 someDetectors = [allDetectors[i] for i in self.rng.choice(len(allDetectors),
115 size=numDetectors, replace=False)]
116 for cameraGeomDetector in someDetectors:
117 # Right now we only support integer detector IDs in data IDs;
118 # support for detector names and groups (i.e. rafts) is
119 # definitely planned but not yet implemented.
120 dataId = dict(instrument=instrument.instrument, detector=cameraGeomDetector.getId())
121 butler.put(cameraGeomDetector, "detector", dataId=dataId)
122 cameraGeomDetector2 = butler.get("detector", dataId=dataId)
123 # Full detector comparisons are *slow*; just compare names and
124 # serials.
125 self.assertEqual(cameraGeomDetector.getName(), cameraGeomDetector2.getName())
126 self.assertEqual(cameraGeomDetector.getSerial(), cameraGeomDetector2.getSerial())
128 def testLsstCam(self):
129 self.checkInstrumentWithRegistry(LsstCam)
131 def testComCam(self):
132 self.checkInstrumentWithRegistry(LsstComCam)
134 def testImSim(self):
135 self.checkInstrumentWithRegistry(LsstImSim)
137 def testPhoSim(self):
138 self.checkInstrumentWithRegistry(LsstPhoSim)
140 def testTs8(self):
141 self.checkInstrumentWithRegistry(LsstTS8)
143 def testTs3(self):
144 self.checkInstrumentWithRegistry(LsstTS3)
146 def testUcdCam(self):
147 self.checkInstrumentWithRegistry(LsstUCDCam)
149 def testLatiss(self):
150 self.checkInstrumentWithRegistry(Latiss)
153if __name__ == "__main__": 153 ↛ 154line 153 didn't jump to line 154, because the condition on line 153 was never true
154 unittest.main()