Coverage for tests/test_schema_v1_legacy_fixtures.py: 82%
45 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-07-16 15:24 -0700
« prev ^ index » next coverage.py v7.15.2, created at 2026-07-16 15:24 -0700
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 the legacy-derived v1 fixtures.
14The fixtures under ``tests/data/schema_v1/legacy/`` are derived from real
15files (converted from legacy formats) by ``_minify_for_fixtures``. Their
16whole point is to prove that we can still read real-data serializations back
17in -- so these tests just read each fixture and check its structure.
19These tests do not require ``lsst.cell_coadds`` (the ``CellCoadd`` fixture is
20read purely through ``lsst.images``). They also do not require ``piff``: a
21``VisitImage`` whose PSF cannot be deserialized still reads successfully, with
22the error deferred until the PSF is actually accessed. We assert that
23deferred behaviour directly, and only dereference the PSF when ``piff`` is
24importable.
25"""
27from __future__ import annotations
29from pathlib import Path
31import pytest
33from lsst.images import VisitImage
34from lsst.images.cells import CellCoadd
35from lsst.images.serialization import ArchiveReadError, read_archive
37SCHEMA_DIR = Path(__file__).parent / "data" / "schema_v1"
38LEGACY_DIR = SCHEMA_DIR / "legacy"
40try:
41 import piff # noqa: F401
43 HAVE_PIFF = True
44except ImportError:
45 HAVE_PIFF = False
48def test_cell_coadd() -> None:
49 """Verify the CellCoadd fixture reads back as a multi-cell coadd.
51 Also verifies it can be subset down to a single cell.
52 """
53 path = LEGACY_DIR / "cell_coadd.json"
54 if not path.exists(): 54 ↛ 55line 54 didn't jump to line 55 because the condition on line 54 was never true
55 pytest.skip(f"{path} not present")
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("name", ["visit_image_dp1.json", "visit_image_dp2.json"])
78def test_visit_images(name: 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 = LEGACY_DIR / name
84 if not path.exists(): 84 ↛ 85line 84 didn't jump to line 85 because the condition on line 84 was never true
85 pytest.skip(f"{path} not present")
87 visit_image = read_archive(path, VisitImage)
89 # Pixel planes were cropped to a small corner.
90 assert visit_image.image.array.ndim == 2
91 assert max(visit_image.image.array.shape) <= 16
92 assert isinstance(visit_image.band, str)
93 assert visit_image.image.unit is not None
94 # The real detector (with trimmed amplifiers) round-tripped.
95 assert len(visit_image.detector.amplifiers) >= 1
96 assert len(visit_image.aperture_corrections) > 0
98 if HAVE_PIFF: 98 ↛ 103line 98 didn't jump to line 103 because the condition on line 98 was always true
99 assert visit_image.psf is not None
100 else:
101 # Reading succeeded above; the PSF error is deferred to
102 # the point of access.
103 with pytest.raises(ArchiveReadError):
104 visit_image.psf