22 """Base class for writing Gen3 raw data ingest tests.
25 __all__ = (
"IngestTestBase",)
33 from lsst.daf.butler
import Butler
34 from lsst.daf.butler.cli.butler
import cli
as butlerCli
35 from lsst.daf.butler.cli.utils
import LogCliRunner
38 from .utils
import getInstrument
43 """Base class for tests of gen3 ingest. Subclass from this, then
44 `unittest.TestCase` to get a working test suite.
48 """Root path to ingest files into. Typically `obs_package/tests/`; the
49 actual directory will be a tempdir under this one.
53 """list of butler data IDs of files that should have been ingested."""
56 """Full path to a file to ingest in tests."""
58 rawIngestTask =
"lsst.obs.base.RawIngestTask"
59 """The task to use in the Ingest test."""
61 curatedCalibrationDatasetTypes =
None
62 """List or tuple of Datasets types that should be present after calling
63 writeCuratedCalibrations. If `None` writeCuratedCalibrations will
64 not be called and the test will be skipped."""
67 """The task to use to define visits from groups of exposures.
68 This is ignored if ``visits`` is `None`.
72 """A dictionary mapping visit data IDs the lists of exposure data IDs that
73 are associated with them.
74 If this is empty (but not `None`), visit definition will be run but no
75 visits will be expected (e.g. because no exposures are on-sky
80 """The name of the output run to use in tests.
86 """The fully qualified instrument class name.
91 The fully qualified instrument class name.
97 """The instrument class."""
102 """The name of the instrument.
107 The name of the instrument.
120 if os.path.exists(self.
root):
121 shutil.rmtree(self.
root, ignore_errors=
True)
125 Test that RawIngestTask ingested the expected files.
129 files : `list` [`str`], or None
130 List of files to be ingested, or None to use ``self.file``
133 datasets = butler.registry.queryDatasets(self.
outputRun, collections=...)
134 self.assertEqual(len(list(datasets)), len(self.
dataIds))
136 exposure = butler.get(self.
outputRun, dataId)
137 metadata = butler.get(
"raw.metadata", dataId)
138 self.assertEqual(metadata.toDict(), exposure.getMetadata().toDict())
143 wcs = butler.get(
"raw.wcs", dataId)
144 self.assertEqual(wcs, exposure.getWcs())
146 rawImage = butler.get(
"raw.image", dataId)
147 self.assertEqual(rawImage.getBBox(), exposure.getBBox())
152 """Check the state of the repository after ingest.
154 This is an optional hook provided for subclasses; by default it does
159 files : `list` [`str`], or None
160 List of files to be ingested, or None to use ``self.file``
164 def _createRepo(self):
165 """Use the Click `testing` module to call the butler command line api
166 to create a repository."""
167 runner = LogCliRunner()
168 result = runner.invoke(butlerCli, [
"create", self.root])
169 self.assertEqual(result.exit_code, 0, f
"output: {result.output} exception: {result.exception}")
171 def _ingestRaws(self, transfer):
172 """Use the Click `testing` module to call the butler command line api
178 The external data transfer type.
180 runner = LogCliRunner()
181 result = runner.invoke(butlerCli, [
"ingest-raws", self.root, self.file,
182 "--output-run", self.outputRun,
183 "--transfer", transfer,
184 "--ingest-task", self.rawIngestTask])
185 self.assertEqual(result.exit_code, 0, f
"output: {result.output} exception: {result.exception}")
187 def _registerInstrument(self):
188 """Use the Click `testing` module to call the butler command line api
189 to register the instrument."""
190 runner = LogCliRunner()
191 result = runner.invoke(butlerCli, [
"register-instrument", self.root, self.instrumentClassName])
192 self.assertEqual(result.exit_code, 0, f
"output: {result.output} exception: {result.exception}")
194 def _writeCuratedCalibrations(self):
195 """Use the Click `testing` module to call the butler command line api
196 to write curated calibrations."""
197 runner = LogCliRunner()
198 result = runner.invoke(butlerCli, [
"write-curated-calibrations", self.root,
199 "--instrument", self.instrumentName])
200 self.assertEqual(result.exit_code, 0, f
"output: {result.output} exception: {result.exception}")
218 except PermissionError
as err:
219 raise unittest.SkipTest(
"Skipping hard-link test because input data"
220 " is on a different filesystem.")
from err
223 """Test that files already in the directory can be added to the
228 newPath = butler.datastore.root.join(os.path.basename(self.
file))
229 os.symlink(os.path.abspath(self.
file), newPath.ospath)
234 """Re-ingesting the same data into the repository should fail.
237 with self.assertRaises(Exception):
241 """Test that we can ingest the curated calibrations"""
243 raise unittest.SkipTest(
"Class requests disabling of writeCuratedCalibrations test")
247 butler = Butler(self.
root, writeable=
False)
249 with self.subTest(dtype=datasetTypeName):
251 butler.registry.queryDatasetAssociations(
256 self.assertGreater(len(found), 0, f
"Checking {datasetTypeName}")
260 self.skipTest(
"Expected visits were not defined.")
266 script.defineVisits(self.
root, config_file=
None, collections=self.
outputRun,
271 visits = butler.registry.queryDataIds([
"visit"]).expanded().toSet()
272 self.assertCountEqual(visits, self.
visits.keys())
274 camera = instr.getCamera()
275 for foundVisit, (expectedVisit, expectedExposures)
in zip(visits, self.
visits.items()):
277 foundExposures = butler.registry.queryDataIds([
"exposure"], dataId=expectedVisit
279 self.assertCountEqual(foundExposures, expectedExposures)
282 self.assertIsNotNone(foundVisit.region)
283 detectorVisitDataIds = butler.registry.queryDataIds([
"visit",
"detector"], dataId=expectedVisit
285 self.assertEqual(len(detectorVisitDataIds), len(camera))
286 for dataId
in detectorVisitDataIds:
287 self.assertTrue(foundVisit.region.contains(dataId.region))