Coverage for tests / test_matrixPlot.py: 26%

68 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-22 09:09 +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 lsst.utils.tests 

25import matplotlib 

26import matplotlib.pyplot as plt 

27import numpy as np 

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

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

30from lsst.analysis.tools.interfaces import AnalysisTool 

31 

32# No display needed. 

33matplotlib.use("Agg") 

34 

35 

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

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

38 generation. 

39 """ 

40 

41 def setUp(self): 

42 # Configure the plot action. 

43 action = MatrixPlot() 

44 action.matrixKey = "custom_key" 

45 action.matrixOrigin = "lower" 

46 action.xAxisLabel = "X axis label" 

47 action.yAxisLabel = "Y axis label" 

48 action.colorbarLabel = "Colorbar label" 

49 action.title = "Title" 

50 action.titleFontSize = 7 

51 action.figsize = [4, 4] 

52 action.dpi = 310 

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

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

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

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

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

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

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

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

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

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

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

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

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

66 action.setPositionsAtPixelBoundaries = True 

67 action.hideMinorTicks = ["y"] 

68 

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

70 self.plot = AnalysisTool() 

71 self.plot.produce.plot = action 

72 

73 # Mock up the data. 

74 np.random.seed(1905) 

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

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

77 

78 # Load the relevant column and finalize the plot. 

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

80 self.plot.finalize() 

81 

82 # Set up the plotInfo dictionary. 

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

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

85 

86 def tearDown(self): 

87 del self.plot 

88 del self.data 

89 del self.plotInfo 

90 

91 def test_MatrixPlotTask(self): 

92 plt.rcParams.update(plt.rcParamsDefault) 

93 

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

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

96 

97 # Get the type of the plot. 

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

99 

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

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

102 

103 # Unpack the figure object from the dictionary. 

104 fig = result[plot_type] 

105 

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

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

108 

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

110 figsize = self.plot.produce.plot.figsize 

111 dpi = self.plot.produce.plot.dpi 

112 self.assertTrue( 

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

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

115 ) 

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

117 

118 # Assert that the figure has 2 axes. 

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

120 

121 

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

123 pass 

124 

125 

126def setup_module(module): 

127 lsst.utils.tests.init() 

128 

129 

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()