Coverage for tests / test_matplotlib_figures.py: 20%

83 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-28 08:31 +0000

1# This file is part of 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 unittest 

23 

24import lsst.utils.tests 

25from lsst.utils.plotting import ( 

26 accent_color, 

27 divergent_cmap, 

28 galaxies_cmap, 

29 galaxies_color, 

30 get_band_dicts, 

31 get_multiband_plot_colors, 

32 get_multiband_plot_linestyles, 

33 get_multiband_plot_symbols, 

34 make_figure, 

35 mk_colormap, 

36 set_rubin_plotstyle, 

37 sso_cmap, 

38 sso_color, 

39 stars_cmap, 

40 stars_color, 

41) 

42 

43try: 

44 from matplotlib import rcParams 

45 from matplotlib.backends.backend_agg import FigureCanvasAgg 

46 from matplotlib.figure import Figure 

47except ImportError: 

48 Figure = None 

49 

50 

51@unittest.skipIf(Figure is None, "matplotlib is not available.") 

52class MakeFigureTestCase(unittest.TestCase): 

53 """Tests for make_figure.""" 

54 

55 def testMakeFigure(self): 

56 with lsst.utils.tests.getTempFilePath(".png") as tmpFile: 

57 fig = make_figure(figsize=(10, 6), dpi=300) 

58 

59 self.assertIsInstance(fig, Figure) 

60 self.assertIsInstance(fig.canvas, FigureCanvasAgg) 

61 self.assertEqual(fig.get_figwidth(), 10) 

62 self.assertEqual(fig.get_figheight(), 6) 

63 self.assertEqual(fig.get_dpi(), 300) 

64 

65 bands = ["u", "g", "r", "i", "z", "y"] 

66 

67 ax = fig.add_subplot(111) 

68 for dark_background in True, False: 

69 colors = get_multiband_plot_colors(dark_background=dark_background) 

70 symbols = get_multiband_plot_symbols() 

71 linestyles = get_multiband_plot_linestyles() 

72 

73 ax = fig.add_subplot(111) 

74 for band in bands: 

75 ax.plot( 

76 [0, 1], 

77 [0, 1], 

78 c=colors[band], 

79 marker=symbols[band], 

80 linestyle=linestyles[band], 

81 ) 

82 

83 fig.savefig(tmpFile) 

84 

85 

86@unittest.skipIf(Figure is None, "matplotlib is not available.") 

87class PublicationPlotsTestCase(unittest.TestCase): 

88 """Tests for publication_plots.""" 

89 

90 def testMplStyle(self): 

91 # Set the plot style 

92 set_rubin_plotstyle() 

93 # Confirm that the settings took effect by checking one of them 

94 self.assertEqual(rcParams["errorbar.capsize"], 3.0) 

95 

96 def testMultibandPlotColors(self): 

97 bands_dict = get_band_dicts() 

98 self.assertEqual(bands_dict["colors"]["r"], "#B52626") 

99 self.assertEqual(bands_dict["colors_black"]["r"], "#ff7e00") 

100 self.assertEqual(bands_dict["symbols"]["r"], "v") 

101 self.assertEqual(bands_dict["line_styles"]["r"], "-.") 

102 

103 def testStarColors(self): 

104 with lsst.utils.tests.getTempFilePath(".png") as tmpFile: 

105 fig = make_figure() 

106 ax = fig.add_subplot(111) 

107 xs = [0, 1] 

108 ys = [0, 1] 

109 for cmap in [stars_cmap(), stars_cmap(single_color=True)]: 

110 ax.hexbin(xs, ys, cmap=cmap) 

111 ax.axhline(0, color=stars_color()) 

112 fig.savefig(tmpFile) 

113 

114 def testGalaxyColor(self): 

115 with lsst.utils.tests.getTempFilePath(".png") as tmpFile: 

116 fig = make_figure() 

117 ax = fig.add_subplot(111) 

118 xs = [0, 1] 

119 ys = [0, 1] 

120 for cmap in [galaxies_cmap(), galaxies_cmap(single_color=True)]: 

121 ax.hexbin(xs, ys, cmap=cmap) 

122 ax.axhline(0, color=galaxies_color()) 

123 fig.savefig(tmpFile) 

124 

125 def testDivergentColor(self): 

126 with lsst.utils.tests.getTempFilePath(".png") as tmpFile: 

127 fig = make_figure() 

128 ax = fig.add_subplot(111) 

129 xs = [0, 1] 

130 ys = [0, 1] 

131 ax.hexbin(xs, ys, cmap=divergent_cmap()) 

132 ax.axhline(0, color=accent_color()) 

133 fig.savefig(tmpFile) 

134 

135 def testSSOColor(self): 

136 with lsst.utils.tests.getTempFilePath(".png") as tmpFile: 

137 fig = make_figure() 

138 ax = fig.add_subplot(111) 

139 xs = [0, 1] 

140 ys = [0, 1] 

141 for cmap in [sso_cmap(), sso_cmap(single_color=True)]: 

142 ax.hexbin(xs, ys, cmap=cmap) 

143 ax.axhline(0, color=sso_color()) 

144 fig.savefig(tmpFile) 

145 

146 def testMkColormap(self): 

147 mk_colormap(["#fde725", "#21918c", "#440154"]) 

148 mk_colormap(["#fde725", "#5ec962", "#21918c", "#3b528b"]) 

149 mk_colormap(["#fde725", "#5ec962", "#21918c", "#3b528b", "#440154"])