Coverage for tests / test_image.py: 30%

37 statements  

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

11 

12from __future__ import annotations 

13 

14import os 

15import unittest 

16 

17import numpy as np 

18 

19from lsst.images import Box, Image 

20from lsst.images.tests import assert_close, compare_image_to_legacy 

21 

22DATA_DIR = os.environ.get("TESTDATA_IMAGES_DIR", None) 

23 

24 

25class ImageTestCase(unittest.TestCase): 

26 """Tests for the Image class.""" 

27 

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) 

32 

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[:3, 1:3] 

37 self.assertEqual(subset2, subset) 

38 

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 because we have moved origin. 

43 image[:3, 1:3] 

44 subset = image[:0, 11:13] 

45 assert_close(self, subset.array, np.array([[2, 3], [6, 7]])) 

46 

47 @unittest.skipUnless(DATA_DIR is not None, "TESTDATA_IMAGES_DIR is not in the environment.") 

48 def test_legacy(self) -> None: 

49 """Test Image.read_legacy, Image.to_legacy, and Image.from_legacy.""" 

50 assert DATA_DIR is not None, "Guaranteed by decorator." 

51 filename = os.path.join(DATA_DIR, "dp2", "legacy", "visit_image.fits") 

52 image = Image.read_legacy(filename, preserve_quantization=True) 

53 try: 

54 from lsst.afw.image import MaskedImageFitsReader 

55 except ImportError: 

56 raise unittest.SkipTest("'lsst.afw.image' could not be imported.") from None 

57 reader = MaskedImageFitsReader(filename) 

58 legacy_image = reader.readImage() 

59 compare_image_to_legacy(self, image, legacy_image, expect_view=False) 

60 # Converting back to afw will not share memory, because 

61 # preserve_quantization=True makes the array read-only and to_legacy 

62 # has to copy in that case. 

63 compare_image_to_legacy(self, image, image.to_legacy(), expect_view=False) 

64 # Converting from afw will always share memory. 

65 image_view = Image.from_legacy(legacy_image) 

66 compare_image_to_legacy(self, image_view, legacy_image, expect_view=True) 

67 # Converting back to afw from the in-memory view will be another view. 

68 compare_image_to_legacy(self, image_view, image_view.to_legacy(), expect_view=True) 

69 

70 

71if __name__ == "__main__": 

72 unittest.main()