Coverage for tests / test_imageIo1.py: 24%
76 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-28 01:42 -0700
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-28 01:42 -0700
1# This file is part of afw.
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# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
22"""
23Test cases to test image I/O
24"""
25import itertools
26import os.path
27import unittest
29import lsst.afw.image as afwImage
30import lsst.daf.base as dafBase
31import lsst.utils.tests
33try:
34 dataDir = os.path.join(lsst.utils.getPackageDir("afwdata"), "data")
35except LookupError:
36 dataDir = None
39class ReadFitsTestCase(lsst.utils.tests.TestCase):
40 """A test case for reading FITS images"""
42 def setUp(self):
43 pass
45 def tearDown(self):
46 pass
48 def testWriteBool(self):
49 """Test that we can read and write bools"""
50 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile:
51 im = afwImage.ImageF(lsst.geom.ExtentI(10, 20))
52 md = dafBase.PropertySet()
53 keys = {"BAD": False,
54 "GOOD": True,
55 }
56 for k, v in keys.items():
57 md.add(k, v)
59 im.writeFits(tmpFile, md)
61 jim = afwImage.DecoratedImageF(tmpFile)
63 for k, v in keys.items():
64 self.assertEqual(jim.getMetadata().getScalar(k), v)
66 def testLongStrings(self):
67 keyWord = 'ZZZ'
68 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile:
69 longString = ' '.join(['This is a long string.'] * 8)
71 expOrig = afwImage.ExposureF(100, 100)
72 mdOrig = expOrig.getMetadata()
73 mdOrig.set(keyWord, longString)
74 expOrig.writeFits(tmpFile)
76 expNew = afwImage.ExposureF(tmpFile)
77 self.assertEqual(expNew.getMetadata().getScalar(keyWord), longString)
79 @unittest.skipIf(dataDir is None, "afwdata not setup")
80 def testReadFitsWithOptions(self):
81 xy0Offset = lsst.geom.Extent2I(7, 5)
82 bbox = lsst.geom.Box2I(lsst.geom.Point2I(10, 11), lsst.geom.Extent2I(31, 22))
84 with lsst.utils.tests.getTempFilePath(".fits") as filepath:
85 # write a temporary version of the image with non-zero XY0
86 imagePath = os.path.join(dataDir, "med.fits")
87 maskedImage = afwImage.MaskedImageD(imagePath)
88 maskedImage.setXY0(lsst.geom.Point2I(xy0Offset))
89 maskedImage.writeFits(filepath)
91 for ImageClass, imageOrigin in itertools.product(
92 (afwImage.ImageF, afwImage.ImageD),
93 (None, "LOCAL", "PARENT"),
94 ):
95 with self.subTest(ImageClass=str(ImageClass), imageOrigin=repr(imageOrigin)):
96 fullImage = ImageClass(filepath)
97 options = dafBase.PropertySet()
98 options.set("llcX", bbox.getMinX())
99 options.set("llcY", bbox.getMinY())
100 options.set("width", bbox.getWidth())
101 options.set("height", bbox.getHeight())
102 if imageOrigin is not None:
103 options.set("imageOrigin", imageOrigin)
104 image1 = ImageClass.readFitsWithOptions(filepath, options)
105 readBBoxParent = lsst.geom.Box2I(bbox)
106 if imageOrigin == "LOCAL":
107 readBBoxParent.shift(xy0Offset)
108 self.assertImagesEqual(image1, ImageClass(fullImage, readBBoxParent))
110 for name in ("llcY", "width", "height"):
111 badOptions = options.deepCopy()
112 badOptions.remove(name)
113 with self.assertRaises(LookupError):
114 ImageClass.readFitsWithOptions(filepath, badOptions)
116 badOptions = options.deepCopy()
117 badOptions.set("imageOrigin", "INVALID")
118 with self.assertRaises(RuntimeError):
119 ImageClass.readFitsWithOptions(filepath, badOptions)
122class TestMemory(lsst.utils.tests.MemoryTestCase):
123 pass
126def setup_module(module):
127 lsst.utils.tests.init()
130if __name__ == "__main__": 130 ↛ 131line 130 didn't jump to line 131 because the condition on line 130 was never true
131 lsst.utils.tests.init()
132 unittest.main()