Coverage for python/lsst/obs/base/instrument_tests.py: 51%
114 statements
« prev ^ index » next coverage.py v6.4.4, created at 2022-09-02 02:23 -0700
« prev ^ index » next coverage.py v6.4.4, created at 2022-09-02 02:23 -0700
1# This file is part of obs_base.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://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/>.
22"""Helpers for writing tests against subclassses of Instrument.
24These are not tests themselves, but can be subclassed (plus unittest.TestCase)
25to get a functional test of an Instrument.
26"""
28from __future__ import annotations
30__all__ = [
31 "DummyCam",
32 "InstrumentTestData",
33 "InstrumentTests",
34 "DummyCamYamlWcsFormatter",
35 "CuratedCalibration",
36]
38import abc
39import dataclasses
40import json
41from functools import lru_cache
42from typing import TYPE_CHECKING, Any, ClassVar, Optional, Sequence, Set
44import pkg_resources
45from lsst.daf.butler import CollectionType, DatasetType, Registry, RegistryConfig
46from lsst.daf.butler.formatters.yaml import YamlFormatter
47from lsst.obs.base import FilterDefinition, FilterDefinitionCollection, Instrument
48from lsst.obs.base.yamlCamera import makeCamera
49from lsst.utils.introspection import get_full_type_name
50from pydantic import BaseModel
52from .utils import createInitialSkyWcsFromBoresight
54if TYPE_CHECKING: 54 ↛ 55line 54 didn't jump to line 55, because the condition on line 54 was never true
55 from lsst.daf.butler import Butler
57DUMMY_FILTER_DEFINITIONS = FilterDefinitionCollection(
58 FilterDefinition(physical_filter="dummy_u", band="u"),
59 FilterDefinition(physical_filter="dummy_g", band="g"),
60)
63class DummyCamYamlWcsFormatter(YamlFormatter):
64 """Specialist formatter for tests that can make a WCS."""
66 @classmethod
67 def makeRawSkyWcsFromBoresight(cls, boresight, orientation, detector):
68 """Class method to make a raw sky WCS from boresight and detector.
70 This uses the API expected by define-visits. A working example
71 can be found in `FitsRawFormatterBase`.
73 Notes
74 -----
75 This makes no attempt to create a proper WCS from geometry.
76 """
77 return createInitialSkyWcsFromBoresight(boresight, orientation, detector, flipX=False)
80class CuratedCalibration(BaseModel):
81 """Class that implements minimal read/write interface needed to support
82 curated calibration ingest.
83 """
85 metadata: dict[str, Any]
86 values: list[int]
88 @classmethod
89 def readText(cls, path: str) -> CuratedCalibration:
90 with open(path) as f:
91 data = f.read()
92 return cls.parse_obj(json.loads(data))
94 def writeText(self, path: str) -> None:
95 with open(path, "w") as f:
96 print(self.json(), file=f)
98 def getMetadata(self) -> dict[str, Any]:
99 return self.metadata
102class DummyCam(Instrument):
104 filterDefinitions = DUMMY_FILTER_DEFINITIONS
105 additionalCuratedDatasetTypes = frozenset(["testCalib"])
106 policyName = "dummycam"
107 dataPackageDir: Optional[str] = ""
109 @classmethod
110 def getName(cls):
111 return "DummyCam"
113 @classmethod
114 @lru_cache() # For mypy
115 def getObsDataPackageDir(cls) -> Optional[str]:
116 return cls.dataPackageDir
118 def getCamera(self):
119 # Return something that can be indexed by detector number
120 # but also has to support getIdIter.
121 filename = pkg_resources.resource_filename("lsst.obs.base", "test/dummycam.yaml")
122 return makeCamera(filename)
124 def register(self, registry, update=False):
125 """Insert Instrument, physical_filter, and detector entries into a
126 `Registry`.
127 """
128 detector_max = 2
129 dataId = {
130 "instrument": self.getName(),
131 "class_name": get_full_type_name(DummyCam),
132 "detector_max": detector_max,
133 "visit_max": 1_000_000,
134 "exposure_max": 1_000_000,
135 }
136 with registry.transaction():
137 registry.syncDimensionData("instrument", dataId, update=update)
138 self._registerFilters(registry, update=update)
139 for d in range(detector_max):
140 registry.syncDimensionData(
141 "detector",
142 dict(
143 dataId,
144 id=d,
145 full_name=f"RXX_S0{d}",
146 ),
147 update=update,
148 )
150 def getRawFormatter(self, dataId):
151 # Docstring inherited fromt Instrument.getRawFormatter.
152 return DummyCamYamlWcsFormatter
154 def applyConfigOverrides(self, name, config):
155 pass
157 def writeAdditionalCuratedCalibrations(
158 self, butler: Butler, collection: Optional[str] = None, labels: Sequence[str] = ()
159 ) -> None:
160 # We want to test the standard curated calibration ingest
161 # but we do not have a standard class to use. There is no way
162 # at the moment to inject a new class into the standard list
163 # that is a package constant, so instead use this "Additional"
164 # method but call the standard curated calibration code.
165 if collection is None:
166 collection = self.makeCalibrationCollectionName(*labels)
167 butler.registry.registerCollection(collection, type=CollectionType.CALIBRATION)
169 datasetType = DatasetType(
170 "testCalib",
171 universe=butler.registry.dimensions,
172 isCalibration=True,
173 dimensions=("instrument", "detector"),
174 storageClass="CuratedCalibration",
175 )
176 runs: Set[str] = set()
177 self._writeSpecificCuratedCalibrationDatasets(
178 butler, datasetType, collection, runs=runs, labels=labels
179 )
182@dataclasses.dataclass
183class InstrumentTestData:
184 """Values to test against in subclasses of `InstrumentTests`."""
186 name: str
187 """The name of the Camera this instrument describes."""
189 nDetectors: int
190 """The number of detectors in the Camera."""
192 firstDetectorName: str
193 """The name of the first detector in the Camera."""
195 physical_filters: Set[str]
196 """A subset of the physical filters should be registered."""
199class InstrumentTests(metaclass=abc.ABCMeta):
200 """Tests of sublcasses of Instrument.
202 TestCase subclasses must derive from this, then `TestCase`, and override
203 ``data`` and ``instrument``.
204 """
206 data: ClassVar[Optional[InstrumentTestData]] = None
207 """`InstrumentTestData` containing the values to test against."""
209 instrument: ClassVar[Optional[Instrument]] = None
210 """The `~lsst.obs.base.Instrument` to be tested."""
212 def test_name(self):
213 self.assertEqual(self.instrument.getName(), self.data.name)
215 def test_getCamera(self):
216 """Test that getCamera() returns a reasonable Camera definition."""
217 camera = self.instrument.getCamera()
218 self.assertEqual(camera.getName(), self.instrument.getName())
219 self.assertEqual(len(camera), self.data.nDetectors)
220 self.assertEqual(next(iter(camera)).getName(), self.data.firstDetectorName)
222 def test_register(self):
223 """Test that register() sets appropriate Dimensions."""
224 registryConfig = RegistryConfig()
225 registryConfig["db"] = "sqlite://"
226 registry = Registry.createFromConfig(registryConfig)
227 # Check that the registry starts out empty.
228 self.assertFalse(registry.queryDataIds(["instrument"]).toSequence())
229 self.assertFalse(registry.queryDataIds(["detector"]).toSequence())
230 self.assertFalse(registry.queryDataIds(["physical_filter"]).toSequence())
232 # Register the instrument and check that certain dimensions appear.
233 self.instrument.register(registry)
234 instrumentDataIds = registry.queryDataIds(["instrument"]).toSequence()
235 self.assertEqual(len(instrumentDataIds), 1)
236 instrumentNames = {dataId["instrument"] for dataId in instrumentDataIds}
237 self.assertEqual(instrumentNames, {self.data.name})
238 detectorDataIds = registry.queryDataIds(["detector"]).expanded().toSequence()
239 self.assertEqual(len(detectorDataIds), self.data.nDetectors)
240 detectorNames = {dataId.records["detector"].full_name for dataId in detectorDataIds}
241 self.assertIn(self.data.firstDetectorName, detectorNames)
242 physicalFilterDataIds = registry.queryDataIds(["physical_filter"]).toSequence()
243 filterNames = {dataId["physical_filter"] for dataId in physicalFilterDataIds}
244 self.assertGreaterEqual(filterNames, self.data.physical_filters)
246 # Check that the instrument class can be retrieved.
247 registeredInstrument = Instrument.fromName(self.instrument.getName(), registry)
248 self.assertEqual(type(registeredInstrument), type(self.instrument))
250 # Check that re-registration is not an error.
251 self.instrument.register(registry)