Coverage for tests / test_matrixPlot.py: 26%

68 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-26 09:36 +0000

1# This file is part of analysis_tools. 

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 matplotlib 

25import matplotlib.pyplot as plt 

26import numpy as np 

27 

28import lsst.utils.tests 

29from lsst.analysis.tools.actions.plot.matrixPlot import GuideLinesConfig, MatrixPlot 

30from lsst.analysis.tools.actions.vector.vectorActions import LoadVector 

31from lsst.analysis.tools.interfaces import AnalysisTool 

32 

33# No display needed. 

34matplotlib.use("Agg") 

35 

36 

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

38 """Test to catch basic errors or logical inconsistencies in matrix plot 

39 generation. 

40 """ 

41 

42 def setUp(self): 

43 # Configure the plot action. 

44 action = MatrixPlot() 

45 action.matrixKey = "custom_key" 

46 action.matrixOrigin = "lower" 

47 action.xAxisLabel = "X axis label" 

48 action.yAxisLabel = "Y axis label" 

49 action.colorbarLabel = "Colorbar label" 

50 action.title = "Title" 

51 action.titleFontSize = 7 

52 action.figsize = [4, 4] 

53 action.dpi = 310 

54 action.guideLines["x"] = GuideLinesConfig() 

55 action.guideLines["x"].lines = {2.5: "x=2.5", 6: ""} 

56 action.guideLines["x"].color = "red" 

57 action.guideLines["x"].linestyle = "--" 

58 action.guideLines["y"] = GuideLinesConfig() 

59 action.guideLines["y"].lines = {3.5: "Another label @ y = 3.5"} 

60 action.guideLines["y"].color = "white" 

61 action.guideLines["y"].outlineColor = "orange" 

62 action.guideLines["y"].linestyle = ":" 

63 action.xAxisTickValues = [0, 2, 4, 6, 8, 10] 

64 action.xAxisTickLabels = {1: "X0", 3: "X1", 5: "X2", 7: "X3", 9: "X4"} 

65 action.yAxisTickValues = [0, 3, 6, 8, 10] 

66 action.yAxisTickLabels = {1.5: "Y0", 4.5: "Y1", 7: "Y2", 9: "Y3"} 

67 action.setPositionsAtPixelBoundaries = True 

68 action.hideMinorTicks = ["y"] 

69 

70 # Set up `AnalysisTool` for the plot action. 

71 self.plot = AnalysisTool() 

72 self.plot.produce.plot = action 

73 

74 # Mock up the data. 

75 np.random.seed(1905) 

76 matrix = np.random.rand(10, 10) 

77 self.data = {"custom_key": matrix} 

78 

79 # Load the relevant column and finalize the plot. 

80 self.plot.process.buildActions.custom_key = LoadVector(vectorKey="custom_key") 

81 self.plot.finalize() 

82 

83 # Set up the plotInfo dictionary. 

84 self.plotInfo = {key: "test" for key in ("plotName", "run", "tableName")} 

85 self.plotInfo["bands"] = [] 

86 

87 def tearDown(self): 

88 del self.plot 

89 del self.data 

90 del self.plotInfo 

91 

92 def test_MatrixPlotTask(self): 

93 plt.rcParams.update(plt.rcParamsDefault) 

94 

95 # Run the `AnalysisTool` for the plot action and get the result. 

96 result = self.plot(data=self.data, skymap=None, plotInfo=self.plotInfo) 

97 

98 # Get the type of the plot. 

99 plot_type = type(self.plot.produce.plot).__name__ 

100 

101 # Assert that the `plot_type` key exists in the result dictionary. 

102 self.assertTrue(plot_type in result, msg=f"{plot_type} key is missing in the result dictionary") 

103 

104 # Unpack the figure object from the dictionary. 

105 fig = result[plot_type] 

106 

107 # Check that the returned object is indeed a matplotlib figure. 

108 self.assertTrue(isinstance(fig, plt.Figure), msg="The output is not a matplotlib figure.") 

109 

110 # Assert that the figure has the correct size and dpi. 

111 figsize = self.plot.produce.plot.figsize 

112 dpi = self.plot.produce.plot.dpi 

113 self.assertTrue( 

114 all(fig.get_size_inches() == figsize), 

115 msg=f"Figure size is not {figsize}, it is {fig.get_size_inches()}", 

116 ) 

117 self.assertTrue(fig.dpi == dpi, msg=f"Figure dpi is not {dpi}, it is {fig.dpi}") 

118 

119 # Assert that the figure has 2 axes. 

120 self.assertTrue(len(fig.axes) == 2, f"Figure does not have 2 axes, it has {len(fig.axes)}") 

121 

122 

123class MemoryTester(lsst.utils.tests.MemoryTestCase): 

124 pass 

125 

126 

127def setup_module(module): 

128 lsst.utils.tests.init() 

129 

130 

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

132 lsst.utils.tests.init() 

133 unittest.main()