Coverage for tests/test_legacy.py: 96%

49 statements  

« 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. 

11 

12from __future__ import annotations 

13 

14import numpy as np 

15import pytest 

16 

17from lsst.images import Box, Image, Interval 

18from lsst.images.fits import FitsCompressionOptions 

19 

20try: 

21 import lsst.afw.image 

22 import lsst.geom 

23 

24 HAVE_LEGACY = True 

25except ImportError: 

26 HAVE_LEGACY = False 

27 

28skip_no_legacy = pytest.mark.skipif(not HAVE_LEGACY, reason="lsst legacy packages could not be imported.") 

29 

30 

31@pytest.fixture 

32def rng() -> np.random.Generator: 

33 """Return a seeded random number generator.""" 

34 return np.random.default_rng(500) 

35 

36 

37@skip_no_legacy 

38def test_interval(rng: np.random.Generator) -> None: 

39 """Test Interval to/from legacy lsst.geom.IntervalI conversion.""" 

40 i = Interval.factory[3:6] 

41 j = i.to_legacy() 

42 assert isinstance(j, lsst.geom.IntervalI) 

43 assert j.min == 3 

44 assert j.max == 5 

45 k = Interval.from_legacy(j) 

46 assert i == k 

47 

48 

49@skip_no_legacy 

50def test_box(rng: np.random.Generator) -> None: 

51 """Test Box to/from legacy lsst.geom.Box2I conversion.""" 

52 b = Box.factory[3:6, -2:1] 

53 c = b.to_legacy() 

54 assert isinstance(c, lsst.geom.Box2I) 

55 assert c.y.min == 3 

56 assert c.y.max == 5 

57 assert c.x.min == -2 

58 assert c.x.max == 0 

59 d = Box.from_legacy(c) 

60 assert b == d 

61 

62 

63@skip_no_legacy 

64def test_image(rng: np.random.Generator) -> None: 

65 """Test Image to/from legacy lsst.afw.image.ImageD conversion.""" 

66 i = Image(rng.normal(100.0, 8.0, size=(200, 251)), dtype=np.float64, yx0=(5, 8)) 

67 j = i.to_legacy() 

68 assert isinstance(j, lsst.afw.image.ImageD) 

69 assert Box.from_legacy(j.getBBox()) == i.bbox 

70 np.testing.assert_array_equal(i.array, j.array) 

71 k = Image.from_legacy(j) 

72 assert i == k 

73 

74 

75@skip_no_legacy 

76def test_fits_compression_from_recipe(rng: np.random.Generator) -> None: 

77 """Test that we can convert butler configuration for a compression 

78 write recipe into a FitsCompressionOptions dict. 

79 """ 

80 config = { 

81 "image": { 

82 "algorithm": "RICE_1", 

83 "quantization": { 

84 "dither": "SUBTRACTIVE_DITHER_2", 

85 "scaling": "STDEV_MASKED", 

86 "mask_planes": ["NO_DATA", "INTRP"], 

87 "level": 16.0, 

88 }, 

89 }, 

90 "mask": { 

91 "algorithm": "GZIP_2", 

92 }, 

93 } 

94 assert FitsCompressionOptions.model_validate(config["image"]) == FitsCompressionOptions.LOSSY 

95 assert FitsCompressionOptions.model_validate(config["mask"]) == FitsCompressionOptions.DEFAULT