Coverage for tests/test_legacy_fixtures.py: 89%
43 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-19 09:51 +0000
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-19 09:51 +0000
1# This file is part of lsst-images.
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# Use of this source code is governed by a 3-clause BSD-style
10# license that can be found in the LICENSE file.
12"""Read-back tests for fixtures derived from real legacy-format files.
14The ``cell_coadd`` ``as_shipped`` fixture and the ``visit_image`` ``dp1`` and
15``dp2`` variants are derived from real archives (converted from legacy
16formats) by ``lsst.images.tests._minify_for_fixtures``. Their whole point is
17to prove that we can still read real-data serializations back in -- so these
18tests just read each fixture and check its structure.
20These tests do not require ``lsst.cell_coadds`` (the ``CellCoadd`` fixture is
21read purely through ``lsst.images``). They also do not require ``piff``: a
22``VisitImage`` whose PSF cannot be deserialized still reads successfully, with
23the error deferred until the PSF is actually accessed. We assert that
24deferred behaviour directly, and only dereference the PSF when ``piff`` is
25importable.
26"""
28from __future__ import annotations
30from pathlib import Path
32import pytest
34from lsst.images import VisitImage
35from lsst.images.cells import CellCoadd
36from lsst.images.serialization import ArchiveReadError, read_archive
37from lsst.images.tests import current_fixture_path
39FIXTURE_DIR = Path(__file__).parent / "data" / "schemas"
41try:
42 import piff # noqa: F401
44 HAVE_PIFF = True
45except ImportError:
46 HAVE_PIFF = False
49def test_cell_coadd() -> None:
50 """Verify the CellCoadd fixture reads back as a multi-cell coadd.
52 Also verifies it can be subset down to a single cell.
53 """
54 path = current_fixture_path(FIXTURE_DIR, "cell_coadd", variant="as_shipped")
55 assert path.exists(), f"{path} is a committed fixture and must not go missing"
57 coadd = read_archive(path, CellCoadd)
59 assert isinstance(coadd.band, str)
60 assert coadd.image.unit is not None
61 # The fixture deliberately keeps more than one cell so it is a useful
62 # test, and includes a missing cell to exercise the sparse-grid path.
63 present = list(coadd.bounds.cell_indices())
64 assert len(present) >= 2
65 assert coadd.bounds.missing, "fixture should retain a missing cell"
66 # Provenance survived the minify and has the expected two-table shape.
67 assert len(coadd.provenance.inputs) > 0
68 assert len(coadd.provenance.contributions) > 0
69 assert "polygon" in coadd.provenance.inputs.colnames
71 # Subsetting to a single present cell still works on the morphed grid.
72 cell_bbox = coadd.grid.bbox_of(present[0])
73 single = coadd[cell_bbox]
74 assert list(single.bounds.cell_indices()) == [present[0]]
77@pytest.mark.parametrize("variant", ["dp1", "dp2"])
78def test_visit_images(variant: str) -> None:
79 """Verify each VisitImage legacy fixture reads back with its real detector.
81 Also verifies deferred PSF behaviour when piff is unavailable.
82 """
83 path = current_fixture_path(FIXTURE_DIR, "visit_image", variant=variant)
84 assert path.exists(), f"{path} is a committed fixture and must not go missing"
86 visit_image = read_archive(path, VisitImage)
88 # Pixel planes were cropped to a small corner.
89 assert visit_image.image.array.ndim == 2
90 assert max(visit_image.image.array.shape) <= 16
91 assert isinstance(visit_image.band, str)
92 assert visit_image.image.unit is not None
93 # The real detector (with trimmed amplifiers) round-tripped.
94 assert len(visit_image.detector.amplifiers) >= 1
95 assert len(visit_image.aperture_corrections) > 0
97 if HAVE_PIFF: 97 ↛ 102line 97 didn't jump to line 102 because the condition on line 97 was always true
98 assert visit_image.psf is not None
99 else:
100 # Reading succeeded above; the PSF error is deferred to
101 # the point of access.
102 with pytest.raises(ArchiveReadError):
103 visit_image.psf