Coverage for tests/test_scatterPlot.py: 22%
99 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-08-06 04:01 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-08-06 04:01 +0000
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/>.
23import os
24import shutil
25import tempfile
26import unittest
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
48matplotlib.use("Agg")
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")
55class ScatterPlotWithTwoHistsTaskTestCase(lsst.utils.tests.TestCase):
56 """ScatterPlotWithTwoHistsTask test case."""
58 def setUp(self):
59 self.testDir = tempfile.mkdtemp(dir=ROOT, prefix="test_output")
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]
74 # Configure the plot to show observed vs true mags
75 action = ScatterPlotWithTwoHists(
76 xAxisLabel="mag",
77 yAxisLabel="mag meas - ref",
78 magLabel="mag",
79 plotTypes=[
80 "galaxies",
81 "stars",
82 ],
83 xLims=(20, 30),
84 yLims=(-1000, 1000),
85 )
86 plot = AnalysisTool()
87 plot.produce.plot = action
89 # Load the relevant columns
90 key_flux = "meas_Flux"
91 plot.process.buildActions.fluxes_meas = LoadVector(vectorKey=key_flux)
92 plot.process.buildActions.fluxes_err = LoadVector(vectorKey=f"{key_flux}Err")
93 plot.process.buildActions.fluxes_ref = LoadVector(vectorKey="ref_Flux")
94 plot.process.buildActions.mags_ref = ConvertFluxToMag(
95 vectorKey=plot.process.buildActions.fluxes_ref.vectorKey
96 )
98 # Compute the y-axis quantity
99 plot.process.buildActions.diff = SubtractVector(
100 actionA=ConvertFluxToMag(
101 vectorKey=plot.process.buildActions.fluxes_meas.vectorKey, returnMillimags=True
102 ),
103 actionB=DivideVector(
104 actionA=plot.process.buildActions.mags_ref,
105 actionB=ConstantValue(value=1e-3),
106 ),
107 )
109 # Filter stars/galaxies, storing quantities separately
110 plot.process.buildActions.galaxySelector = GalaxySelector(vectorKey="refExtendedness")
111 plot.process.buildActions.starSelector = StarSelector(vectorKey="refExtendedness")
112 for singular, plural in (("galaxy", "Galaxies"), ("star", "Stars")):
113 setattr(
114 plot.process.filterActions,
115 f"x{plural}",
116 DownselectVector(
117 vectorKey="mags_ref", selector=VectorSelector(vectorKey=f"{singular}Selector")
118 ),
119 )
120 setattr(
121 plot.process.filterActions,
122 f"y{plural}",
123 DownselectVector(vectorKey="diff", selector=VectorSelector(vectorKey=f"{singular}Selector")),
124 )
125 setattr(
126 plot.process.filterActions,
127 f"flux{plural}",
128 DownselectVector(
129 vectorKey="fluxes_meas", selector=VectorSelector(vectorKey=f"{singular}Selector")
130 ),
131 )
132 setattr(
133 plot.process.filterActions,
134 f"flux{plural}Err",
135 DownselectVector(
136 vectorKey="fluxes_err", selector=VectorSelector(vectorKey=f"{singular}Selector")
137 ),
138 )
140 # Compute low/high SN summary stats
141 statAction = ScatterPlotStatsAction(
142 vectorKey=f"y{plural}",
143 fluxType=f"flux{plural}",
144 highSNSelector=SnSelector(fluxType=f"flux{plural}", threshold=50),
145 lowSNSelector=SnSelector(fluxType=f"flux{plural}", threshold=20),
146 )
147 setattr(plot.process.calculateActions, plural.lower(), statAction)
149 data = {
150 "ref_Flux": flux,
151 key_flux: flux_meas,
152 f"{key_flux}Err": flux_err,
153 "refExtendedness": extendedness,
154 }
156 self.data = pd.DataFrame(data)
157 self.plot = plot
158 self.plot.finalize()
159 plotInfo = {key: "test" for key in ("plotName", "run", "tableName")}
160 plotInfo["bands"] = []
161 self.plotInfo = plotInfo
163 def tearDown(self):
164 if os.path.exists(self.testDir):
165 shutil.rmtree(self.testDir, True)
166 del self.data
167 del self.plot
168 del self.plotInfo
169 del self.testDir
171 def test_ScatterPlotWithTwoHistsTask(self):
172 plt.rcParams.update(plt.rcParamsDefault)
173 result = self.plot(
174 data=self.data,
175 skymap=None,
176 plotInfo=self.plotInfo,
177 )
178 # unpack the result from the dictionary
179 result = result[type(self.plot.produce.plot).__name__]
180 self.assertTrue(isinstance(result, plt.Figure))
182 # Set to true to save plots as PNGs
183 # Use matplotlib.testing.compare.compare_images if needed
184 save_images = False
185 if save_images:
186 result.savefig(os.path.join(ROOT, "data", "test_scatterPlot.png"))
188 texts, lines = get_and_remove_figure_text(result)
189 if save_images:
190 result.savefig(os.path.join(ROOT, "data", "test_scatterPlot_unlabeled.png"))
192 # Set to true to re-generate reference data
193 resave = False
195 # Compare line values
196 for idx, line in enumerate(lines):
197 filename = os.path.join(path_lines_ref, f"line_{idx}.txt")
198 if resave:
199 np.savetxt(filename, line)
200 arr = np.loadtxt(filename)
201 # Differences of order 1e-12 possible between MacOS and Linux
202 # Plots are generally not expected to be that precise
203 # Differences to 1e-3 should not be visible with this test data
204 self.assertFloatsAlmostEqual(arr, line, atol=1e-3, rtol=1e-4)
206 # Ensure that newlines within labels are replaced by a sentinel
207 newline = "\n"
208 newline_replace = "[newline]"
209 # Compare text labels
210 if resave:
211 with open(filename_texts_ref, "w") as f:
212 f.writelines(f"{text.strip().replace(newline, newline_replace)}\n" for text in texts)
214 with open(filename_texts_ref, "r") as f:
215 texts_ref = set(x.strip() for x in f.readlines())
216 texts_set = set(x.strip().replace(newline, newline_replace) for x in texts)
218 self.assertTrue(texts_ref.issuperset(texts_set))
221class MemoryTester(lsst.utils.tests.MemoryTestCase):
222 pass
225def setup_module(module):
226 lsst.utils.tests.init()
229if __name__ == "__main__": 229 ↛ 230line 229 didn't jump to line 230, because the condition on line 229 was never true
230 lsst.utils.tests.init()
231 unittest.main()