Coverage for tests / test_color_image.py: 26%

44 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-24 02: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 

12from __future__ import annotations 

13 

14import unittest 

15 

16import numpy as np 

17 

18from lsst.images import Box, ColorImage, Image, TractFrame 

19from lsst.images.tests import ( 

20 RoundtripFits, 

21 assert_images_equal, 

22 assert_projections_equal, 

23 make_random_projection, 

24) 

25 

26try: 

27 import astshim # noqa: F401 

28 

29 HAVE_ASTSHIM = True 

30except ImportError: 

31 HAVE_ASTSHIM = False 

32 

33 

34class ColorImageTestCase(unittest.TestCase): 

35 """Tests for ColorImage.""" 

36 

37 def setUp(self) -> None: 

38 self.maxDiff = None 

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

40 self.pixel_frame = TractFrame(skymap="test_skymap", tract=33, bbox=Box.factory[:50, :64]) 

41 self.bbox = Box.factory[20:25, 40:48] 

42 if HAVE_ASTSHIM: 

43 self.projection = make_random_projection(self.rng, self.pixel_frame, self.pixel_frame.bbox) 

44 else: 

45 self.projection = None 

46 self.array = self.rng.integers(low=0, high=255, size=self.bbox.shape + (3,), dtype=np.uint8) 

47 self.color_image = ColorImage(self.array, bbox=self.bbox, projection=self.projection) 

48 

49 def test_properties(self) -> None: 

50 """Test the properties of the nominal ColorImage constructed in 

51 setUp. 

52 """ 

53 self.assertEqual(self.color_image.bbox, self.bbox) 

54 self.assertTrue(np.may_share_memory(self.color_image.array, self.array)) 

55 assert_images_equal( 

56 self, 

57 self.color_image.red, 

58 Image(self.array[:, :, 0], bbox=self.bbox, projection=self.projection), 

59 expect_view="array", 

60 ) 

61 assert_images_equal( 

62 self, 

63 self.color_image.green, 

64 Image(self.array[:, :, 1], bbox=self.bbox, projection=self.projection), 

65 expect_view="array", 

66 ) 

67 assert_images_equal( 

68 self, 

69 self.color_image.blue, 

70 Image(self.array[:, :, 2], bbox=self.bbox, projection=self.projection), 

71 expect_view="array", 

72 ) 

73 if HAVE_ASTSHIM: 

74 assert_projections_equal(self, self.color_image.projection, self.projection, expect_identity=True) 

75 

76 def test_constructor(self) -> None: 

77 """Test alternate constructor arguments.""" 

78 self.assert_color_images_equal( 

79 ColorImage(self.array, start=self.bbox.start, projection=self.projection), 

80 self.color_image, 

81 expect_view=True, 

82 ) 

83 self.assert_color_images_equal( 

84 ColorImage.from_channels( 

85 self.color_image.red, 

86 self.color_image.green, 

87 self.color_image.blue, 

88 projection=self.projection, 

89 ), 

90 self.color_image, 

91 expect_view=False, 

92 ) 

93 

94 def test_fits_roundtrip(self) -> None: 

95 """Test round-tripping through FITS, via the butler if available.""" 

96 with RoundtripFits(self, self.color_image, "ColorImage") as roundtrip: 

97 pass 

98 self.assert_color_images_equal(roundtrip.result, self.color_image, expect_view=False) 

99 

100 def assert_color_images_equal( 

101 self, a: ColorImage, b: ColorImage, expect_view: bool | None = None 

102 ) -> None: 

103 """Check that the given ColorImage matches the nominal one constructed 

104 in setUp. 

105 """ 

106 if HAVE_ASTSHIM: 

107 assert_projections_equal(self, a.projection, b.projection) 

108 self.assertEqual(a.bbox, b.bbox) 

109 if expect_view is not None: 

110 self.assertEqual(np.may_share_memory(a.array, b.array), expect_view) 

111 if not expect_view: 

112 np.testing.assert_array_equal(a.array, b.array) 

113 

114 

115if __name__ == "__main__": 

116 unittest.main()