Coverage for tests / test_image.py: 30%
37 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-05 02:32 -0800
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-05 02:32 -0800
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 os
15import unittest
17import numpy as np
19from lsst.images import Box, Image
20from lsst.images.tests import assert_close, compare_image_to_legacy
22DATA_DIR = os.environ.get("TESTDATA_IMAGES_DIR", None)
25class ImageTestCase(unittest.TestCase):
26 """Tests for the Image class."""
28 def test_basics(self):
29 """Test basic constructor patterns."""
30 image = Image(42, shape=(5, 5))
31 assert_close(self, image.array, np.zeros([5, 5], dtype=np.int64) + 42)
33 data = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
34 image = Image(data)
35 subset = image[Box.factory[:3, 1:3]]
36 subset2 = image.absolute[:3, 1:3]
37 self.assertEqual(subset2, subset)
39 # Add an explicit bounding box and then slice it.
40 image = Image(data, bbox=Box.factory[-2:1, 10:14])
41 with self.assertRaises(IndexError):
42 # Same slice no longer works in absolute slicing because we have
43 # moved origin.
44 image.absolute[:3, 1:3]
45 # That slice does still work in local coordinates.
46 assert_close(self, image.local[:3, 1:3].array, subset2.array)
47 # And we can write an equivalent slice in absolute coordinates.
48 assert_close(self, image.absolute[:0, 11:13].array, np.array([[2, 3], [6, 7]]))
50 @unittest.skipUnless(DATA_DIR is not None, "TESTDATA_IMAGES_DIR is not in the environment.")
51 def test_legacy(self) -> None:
52 """Test Image.read_legacy, Image.to_legacy, and Image.from_legacy."""
53 assert DATA_DIR is not None, "Guaranteed by decorator."
54 filename = os.path.join(DATA_DIR, "dp2", "legacy", "visit_image.fits")
55 image = Image.read_legacy(filename, preserve_quantization=True)
56 try:
57 from lsst.afw.image import MaskedImageFitsReader
58 except ImportError:
59 raise unittest.SkipTest("'lsst.afw.image' could not be imported.") from None
60 reader = MaskedImageFitsReader(filename)
61 legacy_image = reader.readImage()
62 compare_image_to_legacy(self, image, legacy_image, expect_view=False)
63 # Converting back to afw will not share memory, because
64 # preserve_quantization=True makes the array read-only and to_legacy
65 # has to copy in that case.
66 compare_image_to_legacy(self, image, image.to_legacy(), expect_view=False)
67 # Converting from afw will always share memory.
68 image_view = Image.from_legacy(legacy_image)
69 compare_image_to_legacy(self, image_view, legacy_image, expect_view=True)
70 # Converting back to afw from the in-memory view will be another view.
71 compare_image_to_legacy(self, image_view, image_view.to_legacy(), expect_view=True)
74if __name__ == "__main__":
75 unittest.main()