Coverage for tests/test_scatterPlot.py: 21%
98 statements
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-04 11:09 +0000
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-04 11:09 +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.selectors import (
39 GalaxySelector,
40 SnSelector,
41 StarSelector,
42 VectorSelector,
43)
44from lsst.analysis.tools.actions.vector.vectorActions import (
45 ConstantValue,
46 DivideVector,
47 DownselectVector,
48 LoadVector,
49 MagColumnNanoJansky,
50 SubtractVector,
51)
52from lsst.analysis.tools.interfaces import AnalysisTool
54matplotlib.use("Agg")
56ROOT = os.path.abspath(os.path.dirname(__file__))
57filename_texts_ref = os.path.join(ROOT, "data", "test_scatterPlot_texts.txt")
58path_lines_ref = os.path.join(ROOT, "data", "test_scatterPlot_lines")
61class ScatterPlotWithTwoHistsTaskTestCase(lsst.utils.tests.TestCase):
62 """ScatterPlotWithTwoHistsTask test case."""
64 def setUp(self):
65 self.testDir = tempfile.mkdtemp(dir=ROOT, prefix="test_output")
67 # Set up a quasi-plausible measurement catalog
68 mag = 12.5 + 2.5 * np.log10(np.arange(10, 100000))
69 flux = 10 ** (-0.4 * (mag - (mag[-1] + 1)))
70 rng = np.random.default_rng(0)
71 extendedness = 0.0 + (rng.uniform(size=len(mag)) < 0.99 * (mag - mag[0]) / (mag[-1] - mag[0]))
72 flux_meas = flux + rng.normal(scale=np.sqrt(flux * (1 + extendedness)))
73 flux_err = np.sqrt(flux_meas * (1 + extendedness))
74 good = (flux_meas / np.sqrt(flux * (1 + extendedness))) > 3
75 extendedness = extendedness[good]
76 flux = flux[good]
77 flux_meas = flux_meas[good]
78 flux_err = flux_err[good]
80 # Configure the plot to show observed vs true mags
81 action = ScatterPlotWithTwoHists(
82 xAxisLabel="mag",
83 yAxisLabel="mag meas - ref",
84 magLabel="mag",
85 plotTypes=[
86 "stars",
87 "galaxies",
88 ],
89 xLims=(20, 30),
90 yLims=(-1000, 1000),
91 )
92 plot = AnalysisTool()
93 plot.produce.plot = action
95 # Load the relevant columns
96 key_flux = "meas_Flux"
97 plot.process.buildActions.fluxes_meas = LoadVector(vectorKey=key_flux)
98 plot.process.buildActions.fluxes_err = LoadVector(vectorKey=f"{key_flux}Err")
99 plot.process.buildActions.fluxes_ref = LoadVector(vectorKey="ref_Flux")
100 plot.process.buildActions.mags_ref = MagColumnNanoJansky(
101 vectorKey=plot.process.buildActions.fluxes_ref.vectorKey
102 )
104 # Compute the y-axis quantity
105 plot.process.buildActions.diff = SubtractVector(
106 actionA=MagColumnNanoJansky(
107 vectorKey=plot.process.buildActions.fluxes_meas.vectorKey, returnMillimags=True
108 ),
109 actionB=DivideVector(
110 actionA=plot.process.buildActions.mags_ref,
111 actionB=ConstantValue(value=1e-3),
112 ),
113 )
115 # Filter stars/galaxies, storing quantities separately
116 plot.process.buildActions.galaxySelector = GalaxySelector(vectorKey="refExtendedness")
117 plot.process.buildActions.starSelector = StarSelector(vectorKey="refExtendedness")
118 for singular, plural in (("galaxy", "Galaxies"), ("star", "Stars")):
119 setattr(
120 plot.process.filterActions,
121 f"x{plural}",
122 DownselectVector(
123 vectorKey="mags_ref", selector=VectorSelector(vectorKey=f"{singular}Selector")
124 ),
125 )
126 setattr(
127 plot.process.filterActions,
128 f"y{plural}",
129 DownselectVector(vectorKey="diff", selector=VectorSelector(vectorKey=f"{singular}Selector")),
130 )
131 setattr(
132 plot.process.filterActions,
133 f"flux{plural}",
134 DownselectVector(
135 vectorKey="fluxes_meas", selector=VectorSelector(vectorKey=f"{singular}Selector")
136 ),
137 )
138 setattr(
139 plot.process.filterActions,
140 f"flux{plural}Err",
141 DownselectVector(
142 vectorKey="fluxes_err", selector=VectorSelector(vectorKey=f"{singular}Selector")
143 ),
144 )
146 # Compute low/high SN summary stats
147 statAction = ScatterPlotStatsAction(
148 vectorKey=f"y{plural}",
149 fluxType=f"flux{plural}",
150 highSNSelector=SnSelector(fluxType=f"flux{plural}", threshold=50),
151 lowSNSelector=SnSelector(fluxType=f"flux{plural}", threshold=20),
152 )
153 setattr(plot.process.calculateActions, plural.lower(), statAction)
155 data = {
156 "ref_Flux": flux,
157 key_flux: flux_meas,
158 f"{key_flux}Err": flux_err,
159 "refExtendedness": extendedness,
160 }
162 self.data = pd.DataFrame(data)
163 self.plot = plot
164 self.plot.finalize()
165 plotInfo = {key: "test" for key in ("plotName", "run", "tableName")}
166 plotInfo["bands"] = []
167 self.plotInfo = plotInfo
169 def tearDown(self):
170 if os.path.exists(self.testDir):
171 shutil.rmtree(self.testDir, True)
172 del self.data
173 del self.plot
174 del self.plotInfo
175 del self.testDir
177 def test_ScatterPlotWithTwoHistsTask(self):
178 plt.rcParams.update(plt.rcParamsDefault)
179 result = self.plot(
180 data=self.data,
181 skymap=None,
182 plotInfo=self.plotInfo,
183 )
184 # unpack the result from the dictionary
185 result = result[type(self.plot.produce.plot).__name__]
186 self.assertTrue(isinstance(result, plt.Figure))
188 # Set to true to save plots as PNGs
189 # Use matplotlib.testing.compare.compare_images if needed
190 save_images = False
191 if save_images:
192 result.savefig(os.path.join(ROOT, "data", "test_scatterPlot.png"))
194 texts, lines = get_and_remove_figure_text(result)
195 if save_images:
196 result.savefig(os.path.join(ROOT, "data", "test_scatterPlot_unlabeled.png"))
198 # Set to true to re-generate reference data
199 resave = False
201 # Compare line values
202 for idx, line in enumerate(lines):
203 filename = os.path.join(path_lines_ref, f"line_{idx}.txt")
204 if resave:
205 np.savetxt(filename, line)
206 arr = np.loadtxt(filename)
207 # Differences of order 1e-12 possible between MacOS and Linux
208 # Plots are generally not expected to be that precise
209 # Differences to 1e-3 should not be visible with this test data
210 self.assertFloatsAlmostEqual(arr, line, atol=1e-3, rtol=1e-4)
212 # Ensure that newlines within labels are replaced by a sentinel
213 newline = "\n"
214 newline_replace = "[newline]"
215 # Compare text labels
216 if resave:
217 with open(filename_texts_ref, "w") as f:
218 f.writelines(f"{text.strip().replace(newline, newline_replace)}\n" for text in texts)
220 with open(filename_texts_ref, "r") as f:
221 texts_ref = set(x.strip() for x in f.readlines())
222 texts_set = set(x.strip().replace(newline, newline_replace) for x in texts)
224 self.assertTrue(texts_ref.issuperset(texts_set))
227class MemoryTester(lsst.utils.tests.MemoryTestCase):
228 pass
231def setup_module(module):
232 lsst.utils.tests.init()
235if __name__ == "__main__": 235 ↛ 236line 235 didn't jump to line 236, because the condition on line 235 was never true
236 lsst.utils.tests.init()
237 unittest.main()