Coverage for tests/test_schema_v1_legacy_fixtures.py: 25%

47 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-06-10 03:25 -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. 

11 

12"""Read-back tests for the legacy-derived v1 fixtures. 

13 

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. 

18 

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""" 

26 

27from __future__ import annotations 

28 

29import unittest 

30from pathlib import Path 

31 

32from lsst.images import VisitImage 

33from lsst.images.cells import CellCoadd 

34from lsst.images.serialization import ArchiveReadError, read 

35 

36SCHEMA_DIR = Path(__file__).parent / "data" / "schema_v1" 

37LEGACY_DIR = SCHEMA_DIR / "legacy" 

38 

39try: 

40 import piff # noqa: F401 

41 

42 HAVE_PIFF = True 

43except ImportError: 

44 HAVE_PIFF = False 

45 

46 

47class LegacyFixtureReadBackTestCase(unittest.TestCase): 

48 """Read each legacy-derived fixture back in and check its structure.""" 

49 

50 def test_cell_coadd(self): 

51 """The CellCoadd fixture reads back as a multi-cell coadd and can be 

52 subset down to a single cell. 

53 """ 

54 path = LEGACY_DIR / "cell_coadd.json" 

55 if not path.exists(): 

56 self.skipTest(f"{path} not present") 

57 

58 coadd = read(str(path), CellCoadd) 

59 

60 self.assertIsInstance(coadd.band, str) 

61 self.assertIsNotNone(coadd.image.unit) 

62 # The fixture deliberately keeps more than one cell so it is a useful 

63 # test, and includes a missing cell to exercise the sparse-grid path. 

64 present = list(coadd.bounds.cell_indices()) 

65 self.assertGreaterEqual(len(present), 2) 

66 self.assertTrue(coadd.bounds.missing, "fixture should retain a missing cell") 

67 # Provenance survived the minify and has the expected two-table shape. 

68 self.assertGreater(len(coadd.provenance.inputs), 0) 

69 self.assertGreater(len(coadd.provenance.contributions), 0) 

70 self.assertIn("polygon", coadd.provenance.inputs.colnames) 

71 

72 # Subsetting to a single present cell still works on the morphed grid. 

73 cell_bbox = coadd.grid.bbox_of(present[0]) 

74 single = coadd[cell_bbox] 

75 self.assertEqual(list(single.bounds.cell_indices()), [present[0]]) 

76 

77 def test_visit_images(self): 

78 """Each VisitImage fixture reads back with its real detector and a 

79 deferred PSF (raised only on access when piff is unavailable). 

80 """ 

81 for name in ("visit_image_dp1.json", "visit_image_dp2.json"): 

82 with self.subTest(name=name): 

83 path = LEGACY_DIR / name 

84 if not path.exists(): 

85 self.skipTest(f"{path} not present") 

86 

87 visit_image = read(str(path), VisitImage) 

88 

89 # Pixel planes were cropped to a small corner. 

90 self.assertEqual(visit_image.image.array.ndim, 2) 

91 self.assertLessEqual(max(visit_image.image.array.shape), 16) 

92 self.assertIsInstance(visit_image.band, str) 

93 self.assertIsNotNone(visit_image.image.unit) 

94 # The real detector (with trimmed amplifiers) round-tripped. 

95 self.assertGreaterEqual(len(visit_image.detector.amplifiers), 1) 

96 self.assertGreater(len(visit_image.aperture_corrections), 0) 

97 

98 if HAVE_PIFF: 

99 self.assertIsNotNone(visit_image.psf) 

100 else: 

101 # Reading succeeded above; the PSF error is deferred to 

102 # the point of access. 

103 with self.assertRaises(ArchiveReadError): 

104 visit_image.psf 

105 

106 

107if __name__ == "__main__": 

108 unittest.main()