Coverage for tests/test_legacy.py: 96%
50 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-30 04:21 -0700
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-30 04:21 -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.
12from __future__ import annotations
14import numpy as np
15import pytest
17from lsst.images import Box, Image, Interval
18from lsst.images.fits import FitsCompressionOptions
19from lsst.images.tests import reset_afw_mask_planes # noqa: F401
21try:
22 import lsst.afw.image
23 import lsst.geom
25 HAVE_LEGACY = True
26except ImportError:
27 HAVE_LEGACY = False
29skip_no_legacy = pytest.mark.skipif(not HAVE_LEGACY, reason="lsst legacy packages could not be imported.")
32@pytest.fixture
33def rng() -> np.random.Generator:
34 """Return a seeded random number generator."""
35 return np.random.default_rng(500)
38@skip_no_legacy
39def test_interval(rng: np.random.Generator) -> None:
40 """Test Interval to/from legacy lsst.geom.IntervalI conversion."""
41 i = Interval.factory[3:6]
42 j = i.to_legacy()
43 assert isinstance(j, lsst.geom.IntervalI)
44 assert j.min == 3
45 assert j.max == 5
46 k = Interval.from_legacy(j)
47 assert i == k
50@skip_no_legacy
51def test_box(rng: np.random.Generator) -> None:
52 """Test Box to/from legacy lsst.geom.Box2I conversion."""
53 b = Box.factory[3:6, -2:1]
54 c = b.to_legacy()
55 assert isinstance(c, lsst.geom.Box2I)
56 assert c.y.min == 3
57 assert c.y.max == 5
58 assert c.x.min == -2
59 assert c.x.max == 0
60 d = Box.from_legacy(c)
61 assert b == d
64@skip_no_legacy
65def test_image(rng: np.random.Generator, reset_afw_mask_planes: None) -> None: # noqa: F811
66 """Test Image to/from legacy lsst.afw.image.ImageD conversion."""
67 i = Image(rng.normal(100.0, 8.0, size=(200, 251)), dtype=np.float64, yx0=(5, 8))
68 j = i.to_legacy()
69 assert isinstance(j, lsst.afw.image.ImageD)
70 assert Box.from_legacy(j.getBBox()) == i.bbox
71 np.testing.assert_array_equal(i.array, j.array)
72 k = Image.from_legacy(j)
73 assert i == k
76@skip_no_legacy
77def test_fits_compression_from_recipe(rng: np.random.Generator) -> None:
78 """Test that we can convert butler configuration for a compression
79 write recipe into a FitsCompressionOptions dict.
80 """
81 config = {
82 "image": {
83 "algorithm": "RICE_1",
84 "quantization": {
85 "dither": "SUBTRACTIVE_DITHER_2",
86 "scaling": "STDEV_MASKED",
87 "mask_planes": ["NO_DATA", "INTRP"],
88 "level": 16.0,
89 },
90 },
91 "mask": {
92 "algorithm": "GZIP_2",
93 },
94 }
95 assert FitsCompressionOptions.model_validate(config["image"]) == FitsCompressionOptions.LOSSY
96 assert FitsCompressionOptions.model_validate(config["mask"]) == FitsCompressionOptions.DEFAULT