Coverage for tests/test_plotting.py: 44%

58 statements  

« prev     ^ index     » next       coverage.py v7.16.0, created at 2026-09-04 10:06 +0000

1# This file is part of summit_utils. 

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/>. 

21 

22import os 

23import tempfile 

24import unittest 

25 

26import numpy as np 

27 

28import lsst.afw.image as afwImage 

29import lsst.utils 

30import lsst.utils.tests 

31from lsst.afw.detection import Footprint 

32from lsst.afw.geom import SpanSet 

33from lsst.summit.utils.plotting import plot 

34from lsst.summit.utils.utils import detectObjectsInExp 

35 

36 

37class PlottingTestCase(lsst.utils.tests.TestCase): 

38 outputDir: str 

39 

40 try: 

41 afwDataDir = lsst.utils.getPackageDir("afwdata") 

42 except Exception: 

43 # Empty rather than None so the attribute stays a str for the 

44 # os.path.join below; skipUnless treats it as falsy either way. 

45 afwDataDir = "" 

46 filename = "postISRCCD_2020021800224-EMPTY~EMPTY-det000.fits.fz" 

47 

48 @classmethod 

49 def setUpClass(cls) -> None: 

50 cls.outputDir = tempfile.mkdtemp() 

51 

52 @unittest.skipUnless(afwDataDir, "afwdata not available") 

53 def test_plot(self) -> None: 

54 """Test that the the plot is made and saved""" 

55 fullName = os.path.join(self.afwDataDir, "LATISS/postISRCCD", self.filename) 

56 exp = afwImage.ExposureF(fullName) 

57 centroids = [(567, 746), (576, 599), (678, 989)] 

58 

59 foot1 = Footprint(SpanSet.fromShape(5, offset=(690, 710))) 

60 foot2 = Footprint(SpanSet.fromShape(6, offset=(159, 216))) 

61 fpset = detectObjectsInExp(exp) 

62 

63 # Input is an exposure 

64 outputFilename = os.path.join(self.outputDir, "testPlotting_exp.jpg") 

65 plot(exp, centroids=centroids, footprints=fpset, addLegend=True, savePlotAs=outputFilename) 

66 self.assertTrue(os.path.isfile(outputFilename)) 

67 self.assertTrue(os.path.getsize(outputFilename) > 10000) 

68 

69 # Input is an image 

70 outputFilename = os.path.join(self.outputDir, "testPlotting_image.jpg") 

71 im = exp.image 

72 plot(im, footprints=[foot1, foot2], savePlotAs=outputFilename) 

73 self.assertTrue(os.path.isfile(outputFilename)) 

74 self.assertTrue(os.path.getsize(outputFilename) > 10000) 

75 

76 # Input is a masked image 

77 outputFilename = os.path.join(self.outputDir, "testPloting_mask.jpg") 

78 masked = exp.maskedImage 

79 plot(masked, savePlotAs=outputFilename) 

80 self.assertTrue(os.path.isfile(outputFilename)) 

81 self.assertTrue(os.path.getsize(outputFilename) > 10000) 

82 

83 # Input is a numpy array 

84 outputFilename = os.path.join(self.outputDir, "testPlotting_nparr.jpg") 

85 nparr = exp.image.array 

86 plot(nparr, footprints=foot1, savePlotAs=outputFilename) 

87 self.assertTrue(os.path.isfile(outputFilename)) 

88 self.assertTrue(os.path.getsize(outputFilename) > 10000) 

89 

90 # Nans in the image 

91 nparr[1200:1250, 1300:1345] = np.nan 

92 for stretch in ["ccs", "asinh", "power", "log", "linear", "sqrt"]: 

93 plot(nparr, showCompass=False, stretch=stretch) 

94 

95 # Image consists of nans 

96 nparr[:, :] = np.nan 

97 plot(nparr) 

98 

99 

100def setup_module(module: object) -> None: 

101 lsst.utils.tests.init() 

102 

103 

104if __name__ == "__main__": 104 ↛ 105line 104 didn't jump to line 105 because the condition on line 104 was never true

105 lsst.utils.tests.init() 

106 unittest.main()