Coverage for tests/test_display.py: 28%
106 statements
« prev ^ index » next coverage.py v6.4.1, created at 2022-07-09 06:10 -0700
« prev ^ index » next coverage.py v6.4.1, created at 2022-07-09 06:10 -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"""
23Tests for displaying devices
25Run with:
26 python test_display.py [backend]
27"""
28import os
29import unittest
31import lsst.utils.tests
32import lsst.afw.image as afwImage
33import lsst.afw.display as afwDisplay
35try:
36 type(backend)
37except NameError:
38 backend = "virtualDevice"
39 oldBackend = None
42class DisplayTestCase(unittest.TestCase):
43 """A test case for Display"""
45 def setUp(self):
46 global oldBackend
47 if backend != oldBackend:
48 afwDisplay.setDefaultBackend(backend)
49 afwDisplay.delAllDisplays() # as some may use the old backend
51 oldBackend = backend
53 dirName = os.path.split(__file__)[0]
54 self.fileName = os.path.join(
55 dirName, "data", "HSC-0908120-056-small.fits")
56 self.display0 = afwDisplay.Display(frame=0, verbose=True)
58 def testMtv(self):
59 """Test basic image display"""
60 exp = afwImage.ExposureF(self.fileName)
61 self.display0.mtv(exp, title="parent")
63 def testMaskPlanes(self):
64 """Test basic image display"""
65 self.display0.setMaskTransparency(50)
66 self.display0.setMaskPlaneColor("CROSSTALK", "orange")
68 def testWith(self):
69 """Test using displays with with statement"""
70 with afwDisplay.Display(0) as disp:
71 self.assertIsNotNone(disp)
73 def testTwoDisplays(self):
74 """Test that we can do things with two frames"""
76 exp = afwImage.ExposureF(self.fileName)
78 for frame in (0, 1):
79 with afwDisplay.Display(frame, verbose=False) as disp:
80 disp.setMaskTransparency(50)
82 if frame == 1:
83 disp.setMaskPlaneColor("CROSSTALK", "ignore")
84 disp.mtv(exp, title="parent")
86 disp.erase()
87 disp.dot('o', 205, 180, size=6, ctype=afwDisplay.RED)
89 def testZoomPan(self):
90 self.display0.pan(205, 180)
91 self.display0.zoom(4)
93 afwDisplay.Display(1).zoom(4, 205, 180)
95 def testStackingOrder(self):
96 """ Un-iconise and raise the display to the top of the stacking order if appropriate"""
97 self.display0.show()
99 def testDrawing(self):
100 """Test drawing lines and glyphs"""
101 self.display0.erase()
103 exp = afwImage.ExposureF(self.fileName)
104 # tells display0 about the image's xy0
105 self.display0.mtv(exp, title="parent")
107 with self.display0.Buffering():
108 self.display0.dot('o', 200, 220)
109 vertices = [(200, 220), (210, 230), (224, 230),
110 (214, 220), (200, 220)]
111 self.display0.line(vertices, ctype=afwDisplay.CYAN)
112 self.display0.line(vertices[:-1], symbs="+x+x", size=3)
114 def testStretch(self):
115 """Test playing with the lookup table"""
116 self.display0.show()
118 self.display0.scale("linear", "zscale")
120 def testMaskColorGeneration(self):
121 """Demonstrate the utility routine to generate mask plane colours
122 (used by e.g. the ds9 implementation of _mtv)"""
124 colorGenerator = self.display0.maskColorGenerator(omitBW=True)
125 for i in range(10):
126 print(i, next(colorGenerator), end=' ')
127 print()
129 def testImageTypes(self):
130 """Check that we can display a range of types of image"""
131 with afwDisplay.Display("dummy", "virtualDevice") as dummy:
132 for imageType in [afwImage.DecoratedImageF,
133 afwImage.ExposureF,
134 afwImage.ImageF,
135 afwImage.MaskedImageF,
136 ]:
137 im = imageType(self.fileName)
138 dummy.mtv(im)
140 for imageType in [afwImage.ImageU, afwImage.ImageI]:
141 im = imageType(self.fileName, hdu=2, allowUnsafe=True)
142 dummy.mtv(im)
144 im = afwImage.Mask(self.fileName, hdu=2)
145 dummy.mtv(im)
147 def testInteract(self):
148 r"""Check that interact exits when a q, \c CR, or \c ESC is pressed, or if a callback function
149 returns a ``True`` value.
150 If this is run using the virtualDevice a "q" is automatically triggered.
151 If running the tests using ds9 you will be expected to do this manually.
152 """
153 print("Hit q to exit interactive mode")
154 self.display0.interact()
156 def testGetMaskPlaneColor(self):
157 """Test that we can return mask colours either as a dict or maskplane by maskplane
158 """
159 mpc = self.display0.getMaskPlaneColor()
161 maskPlane = 'DETECTED'
162 self.assertEqual(mpc[maskPlane], self.display0.getMaskPlaneColor(maskPlane))
164 def testSetDefaultImageColormap(self):
165 """Test that we can set the default colourmap
166 """
167 self.display0.setDefaultImageColormap("gray")
169 def testSetImageColormap(self):
170 """Test that we can set a colourmap
171 """
172 self.display0.setImageColormap("gray")
174 def testClose(self):
175 """Test that we can close devices."""
176 self.display0.close()
178 def tearDown(self):
179 for d in self.display0._displays.values():
180 d.verbose = False # ensure that display9.close() call is quiet
182 del self.display0
183 afwDisplay.delAllDisplays()
186class MemoryTester(lsst.utils.tests.MemoryTestCase):
187 pass
190def setup_module(module):
191 lsst.utils.tests.init()
194if __name__ == "__main__": 194 ↛ 195line 194 didn't jump to line 195, because the condition on line 194 was never true
195 import argparse
196 import sys
198 parser = argparse.ArgumentParser(
199 description="Run the image display test suite")
201 parser.add_argument('backend', type=str, nargs="?", default="virtualDevice",
202 help="The backend to use, e.g. ds9. You may need to have the device setup")
203 args = parser.parse_args()
205 # check that that backend is valid
206 with afwDisplay.Display("test", backend=args.backend) as disp:
207 pass
209 backend = args.backend # backend is just a variable in this file
210 lsst.utils.tests.init()
211 del sys.argv[1:]
212 unittest.main()