Coverage for tests / test_legacy.py: 35%

46 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-07 08:34 +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. 

11 

12from __future__ import annotations 

13 

14import unittest 

15 

16import numpy as np 

17 

18from lsst.images import Box, Image, Interval 

19from lsst.images.fits import FitsCompressionOptions 

20 

21try: 

22 import lsst.afw.image 

23 import lsst.geom 

24except ImportError: 

25 skip_all = True 

26else: 

27 skip_all = False 

28 

29 

30@unittest.skipIf(skip_all, "lsst legacy packages could not be imported.") 

31class LegacyConversionTestCase(unittest.TestCase): 

32 """Tests for conversions between lsst.images and their corresponding 

33 legacy types. 

34 """ 

35 

36 def setUp(self): 

37 self.maxDiff = None 

38 self.rng = np.random.default_rng(500) 

39 

40 def test_interval(self) -> None: 

41 i = Interval.factory[3:6] 

42 j = i.to_legacy() 

43 self.assertIsInstance(j, lsst.geom.IntervalI) 

44 self.assertEqual(j.min, 3) 

45 self.assertEqual(j.max, 5) 

46 k = Interval.from_legacy(j) 

47 self.assertEqual(i, k) 

48 

49 def test_box(self) -> None: 

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

51 c = b.to_legacy() 

52 self.assertIsInstance(c, lsst.geom.Box2I) 

53 self.assertEqual(c.y.min, 3) 

54 self.assertEqual(c.y.max, 5) 

55 self.assertEqual(c.x.min, -2) 

56 self.assertEqual(c.x.max, 0) 

57 d = Box.from_legacy(c) 

58 self.assertEqual(b, d) 

59 

60 def test_image(self) -> None: 

61 i = Image(self.rng.normal(100.0, 8.0, size=(200, 251)), dtype=np.float64, start=(5, 8)) 

62 j = i.to_legacy() 

63 self.assertIsInstance(j, lsst.afw.image.ImageD) 

64 self.assertEqual(Box.from_legacy(j.getBBox()), i.bbox) 

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

66 k = Image.from_legacy(j) 

67 self.assertEqual(i, k) 

68 

69 def test_fits_compression_from_recipe(self) -> None: 

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

71 write recipe into a FitsCompressionOptions dict. 

72 """ 

73 config = { 

74 "image": { 

75 "algorithm": "RICE_1", 

76 "quantization": { 

77 "dither": "SUBTRACTIVE_DITHER_2", 

78 "scaling": "STDEV_MASKED", 

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

80 "level": 16.0, 

81 }, 

82 }, 

83 "mask": { 

84 "algorithm": "GZIP_2", 

85 }, 

86 } 

87 self.assertEqual( 

88 FitsCompressionOptions.model_validate(config["image"]), 

89 FitsCompressionOptions.LOSSY, 

90 ) 

91 self.assertEqual( 

92 FitsCompressionOptions.model_validate(config["mask"]), 

93 FitsCompressionOptions.DEFAULT, 

94 ) 

95 

96 

97if __name__ == "__main__": 

98 unittest.main()