Coverage for tests / test_color_image.py: 29%
34 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-07 08:34 +0000
« 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.
12from __future__ import annotations
14import unittest
16import numpy as np
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)
27class ColorImageTestCase(unittest.TestCase):
28 """Tests for ColorImage."""
30 def setUp(self) -> None:
31 self.maxDiff = None
32 self.rng = np.random.default_rng(500)
33 self.pixel_frame = TractFrame(skymap="test_skymap", tract=33, bbox=Box.factory[:50, :64])
34 self.bbox = Box.factory[20:25, 40:48]
35 self.projection = make_random_projection(self.rng, self.pixel_frame, self.pixel_frame.bbox)
36 self.array = self.rng.integers(low=0, high=255, size=self.bbox.shape + (3,), dtype=np.uint8)
37 self.color_image = ColorImage(self.array, bbox=self.bbox, projection=self.projection)
39 def test_properties(self) -> None:
40 """Test the properties of the nominal ColorImage constructed in
41 setUp.
42 """
43 self.assertEqual(self.color_image.bbox, self.bbox)
44 self.assertTrue(np.may_share_memory(self.color_image.array, self.array))
45 assert_images_equal(
46 self,
47 self.color_image.red,
48 Image(self.array[:, :, 0], bbox=self.bbox, projection=self.projection),
49 expect_view="array",
50 )
51 assert_images_equal(
52 self,
53 self.color_image.green,
54 Image(self.array[:, :, 1], bbox=self.bbox, projection=self.projection),
55 expect_view="array",
56 )
57 assert_images_equal(
58 self,
59 self.color_image.blue,
60 Image(self.array[:, :, 2], bbox=self.bbox, projection=self.projection),
61 expect_view="array",
62 )
63 assert_projections_equal(self, self.color_image.projection, self.projection, expect_identity=True)
65 def test_constructor(self) -> None:
66 """Test alternate constructor arguments."""
67 self.assert_color_images_equal(
68 ColorImage(self.array, start=self.bbox.start, projection=self.projection),
69 self.color_image,
70 expect_view=True,
71 )
72 self.assert_color_images_equal(
73 ColorImage.from_channels(
74 self.color_image.red,
75 self.color_image.green,
76 self.color_image.blue,
77 projection=self.projection,
78 ),
79 self.color_image,
80 expect_view=False,
81 )
83 def test_fits_roundtrip(self) -> None:
84 """Test round-tripping through FITS, via the butler if available."""
85 with RoundtripFits(self, self.color_image, "ColorImage") as roundtrip:
86 pass
87 self.assert_color_images_equal(roundtrip.result, self.color_image, expect_view=False)
89 def assert_color_images_equal(
90 self, a: ColorImage, b: ColorImage, expect_view: bool | None = None
91 ) -> None:
92 """Check that the given ColorImage matches the nominal one constructed
93 in setUp.
94 """
95 assert_projections_equal(self, a.projection, b.projection)
96 if expect_view is not None:
97 self.assertEqual(np.may_share_memory(a.array, b.array), expect_view)
98 if not expect_view:
99 np.testing.assert_array_equal(a.array, b.array)
102if __name__ == "__main__":
103 unittest.main()