Coverage for tests/test_scatterPlot.py: 22%

101 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-05-04 03:35 -0700

1# This file is part of analysis_drp. 

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 

22 

23import os 

24import shutil 

25import tempfile 

26import unittest 

27 

28import lsst.utils.tests 

29import matplotlib 

30import matplotlib.pyplot as plt 

31import numpy as np 

32import pandas as pd 

33from lsst.analysis.tools.actions.plot.plotUtils import get_and_remove_figure_text 

34from lsst.analysis.tools.actions.plot.scatterplotWithTwoHists import ( 

35 ScatterPlotStatsAction, 

36 ScatterPlotWithTwoHists, 

37) 

38from lsst.analysis.tools.actions.vector.mathActions import ConstantValue, DivideVector, SubtractVector 

39from lsst.analysis.tools.actions.vector.selectors import ( 

40 GalaxySelector, 

41 SnSelector, 

42 StarSelector, 

43 VectorSelector, 

44) 

45from lsst.analysis.tools.actions.vector.vectorActions import ConvertFluxToMag, DownselectVector, LoadVector 

46from lsst.analysis.tools.interfaces import AnalysisTool 

47 

48matplotlib.use("Agg") 

49 

50ROOT = os.path.abspath(os.path.dirname(__file__)) 

51filename_texts_ref = os.path.join(ROOT, "data", "test_scatterPlot_texts.txt") 

52path_lines_ref = os.path.join(ROOT, "data", "test_scatterPlot_lines") 

53 

54 

55class ScatterPlotWithTwoHistsTaskTestCase(lsst.utils.tests.TestCase): 

56 """ScatterPlotWithTwoHistsTask test case.""" 

57 

58 def setUp(self): 

59 self.testDir = tempfile.mkdtemp(dir=ROOT, prefix="test_output") 

60 

61 # Set up a quasi-plausible measurement catalog 

62 mag = 12.5 + 2.5 * np.log10(np.arange(10, 100000)) 

63 flux = 10 ** (-0.4 * (mag - (mag[-1] + 1))) 

64 rng = np.random.default_rng(0) 

65 extendedness = 0.0 + (rng.uniform(size=len(mag)) < 0.99 * (mag - mag[0]) / (mag[-1] - mag[0])) 

66 flux_meas = flux + rng.normal(scale=np.sqrt(flux * (1 + extendedness))) 

67 flux_err = np.sqrt(flux_meas * (1 + extendedness)) 

68 good = (flux_meas / np.sqrt(flux * (1 + extendedness))) > 3 

69 extendedness = extendedness[good] 

70 flux = flux[good] 

71 flux_meas = flux_meas[good] 

72 flux_err = flux_err[good] 

73 

74 suffix_x, suffix_y, suffix_stat = "_x", "_y", "_stat" 

75 

76 # Configure the plot to show observed vs true mags 

77 action = ScatterPlotWithTwoHists( 

78 xAxisLabel="mag", 

79 yAxisLabel="mag meas - ref", 

80 magLabel="mag", 

81 plotTypes=[ 

82 "galaxies", 

83 "stars", 

84 ], 

85 xLims=(20, 30), 

86 yLims=(-1000, 1000), 

87 addSummaryPlot=False, 

88 # Make sure adding a suffix works to produce multiple plots 

89 suffix_x=suffix_x, 

90 suffix_y=suffix_y, 

91 suffix_stat=suffix_stat, 

92 ) 

93 plot = AnalysisTool() 

94 plot.produce.plot = action 

95 

96 # Load the relevant columns 

97 key_flux = "meas_Flux" 

98 plot.process.buildActions.fluxes_meas = LoadVector(vectorKey=key_flux) 

99 plot.process.buildActions.fluxes_err = LoadVector(vectorKey=f"{key_flux}Err") 

100 plot.process.buildActions.fluxes_ref = LoadVector(vectorKey="ref_Flux") 

101 plot.process.buildActions.mags_ref = ConvertFluxToMag( 

102 vectorKey=plot.process.buildActions.fluxes_ref.vectorKey 

103 ) 

104 

105 # Compute the y-axis quantity 

106 plot.process.buildActions.diff = SubtractVector( 

107 actionA=ConvertFluxToMag( 

108 vectorKey=plot.process.buildActions.fluxes_meas.vectorKey, returnMillimags=True 

109 ), 

110 actionB=DivideVector( 

111 actionA=plot.process.buildActions.mags_ref, 

112 actionB=ConstantValue(value=1e-3), 

113 ), 

114 ) 

115 

116 # Filter stars/galaxies, storing quantities separately 

117 plot.process.buildActions.galaxySelector = GalaxySelector(vectorKey="refExtendedness") 

118 plot.process.buildActions.starSelector = StarSelector(vectorKey="refExtendedness") 

119 for singular, plural in (("galaxy", "Galaxies"), ("star", "Stars")): 

120 setattr( 

121 plot.process.filterActions, 

122 f"x{plural}{suffix_x}", 

123 DownselectVector( 

124 vectorKey="mags_ref", selector=VectorSelector(vectorKey=f"{singular}Selector") 

125 ), 

126 ) 

127 setattr( 

128 plot.process.filterActions, 

129 f"y{plural}{suffix_y}", 

130 DownselectVector(vectorKey="diff", selector=VectorSelector(vectorKey=f"{singular}Selector")), 

131 ) 

132 setattr( 

133 plot.process.filterActions, 

134 f"flux{plural}", 

135 DownselectVector( 

136 vectorKey="fluxes_meas", selector=VectorSelector(vectorKey=f"{singular}Selector") 

137 ), 

138 ) 

139 setattr( 

140 plot.process.filterActions, 

141 f"fluxErr{plural}", 

142 DownselectVector( 

143 vectorKey="fluxes_err", selector=VectorSelector(vectorKey=f"{singular}Selector") 

144 ), 

145 ) 

146 

147 # Compute low/high SN summary stats 

148 statAction = ScatterPlotStatsAction( 

149 vectorKey=f"y{plural}{suffix_y}", 

150 fluxType=f"flux{plural}", 

151 highSNSelector=SnSelector(fluxType=f"flux{plural}", threshold=50), 

152 lowSNSelector=SnSelector(fluxType=f"flux{plural}", threshold=20), 

153 suffix=suffix_stat, 

154 ) 

155 setattr(plot.process.calculateActions, plural.lower(), statAction) 

156 

157 data = { 

158 "ref_Flux": flux, 

159 key_flux: flux_meas, 

160 f"{key_flux}Err": flux_err, 

161 "refExtendedness": extendedness, 

162 } 

163 

164 self.data = pd.DataFrame(data) 

165 print(self.data.columns) 

166 self.plot = plot 

167 self.plot.finalize() 

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

169 plotInfo["bands"] = [] 

170 self.plotInfo = plotInfo 

171 

172 def tearDown(self): 

173 if os.path.exists(self.testDir): 

174 shutil.rmtree(self.testDir, True) 

175 del self.data 

176 del self.plot 

177 del self.plotInfo 

178 del self.testDir 

179 

180 def test_ScatterPlotWithTwoHistsTask(self): 

181 plt.rcParams.update(plt.rcParamsDefault) 

182 result = self.plot( 

183 data=self.data, 

184 skymap=None, 

185 plotInfo=self.plotInfo, 

186 ) 

187 # unpack the result from the dictionary 

188 result = result[type(self.plot.produce.plot).__name__] 

189 self.assertTrue(isinstance(result, plt.Figure)) 

190 

191 # Set to true to save plots as PNGs 

192 # Use matplotlib.testing.compare.compare_images if needed 

193 save_images = False 

194 if save_images: 

195 result.savefig(os.path.join(ROOT, "data", "test_scatterPlot.png")) 

196 

197 texts, lines = get_and_remove_figure_text(result) 

198 if save_images: 

199 result.savefig(os.path.join(ROOT, "data", "test_scatterPlot_unlabeled.png")) 

200 

201 # Set to true to re-generate reference data 

202 resave = False 

203 

204 # Compare line values 

205 for idx, line in enumerate(lines): 

206 filename = os.path.join(path_lines_ref, f"line_{idx}.txt") 

207 if resave: 

208 np.savetxt(filename, line) 

209 arr = np.loadtxt(filename) 

210 # Differences of order 1e-12 possible between MacOS and Linux 

211 # Plots are generally not expected to be that precise 

212 # Differences to 1e-3 should not be visible with this test data 

213 self.assertFloatsAlmostEqual(arr, line, atol=1e-3, rtol=1e-4) 

214 

215 # Ensure that newlines within labels are replaced by a sentinel 

216 newline = "\n" 

217 newline_replace = "[newline]" 

218 # Compare text labels 

219 if resave: 

220 with open(filename_texts_ref, "w") as f: 

221 f.writelines(f"{text.strip().replace(newline, newline_replace)}\n" for text in texts) 

222 

223 with open(filename_texts_ref, "r") as f: 

224 texts_ref = set(x.strip() for x in f.readlines()) 

225 texts_set = set(x.strip().replace(newline, newline_replace) for x in texts) 

226 

227 self.assertEqual(texts_ref, texts_set) 

228 

229 

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

231 pass 

232 

233 

234def setup_module(module): 

235 lsst.utils.tests.init() 

236 

237 

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

239 lsst.utils.tests.init() 

240 unittest.main()