Coverage for tests / test_image.py: 39%

23 statements  

« prev     ^ index     » next       coverage.py v7.13.2, created at 2026-02-26 12:40 -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 

17from lsst.images import Image 

18from lsst.images.tests import compare_image_to_legacy 

19 

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

21 

22 

23class ImageTestCase(unittest.TestCase): 

24 """Tests for the Image class.""" 

25 

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

27 def test_legacy(self) -> None: 

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

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

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

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

32 try: 

33 from lsst.afw.image import MaskedImageFitsReader 

34 except ImportError: 

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

36 reader = MaskedImageFitsReader(filename) 

37 legacy_image = reader.readImage() 

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

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

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

41 # has to copy in that case. 

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

43 # Converting from afw will always share memory. 

44 image_view = Image.from_legacy(legacy_image) 

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

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

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

48 

49 

50if __name__ == "__main__": 

51 unittest.main()