Coverage for tests/test_scatterPlot.py: 22%
100 statements
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-18 11:18 +0000
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-18 11:18 +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 addSummaryPlot=False,
86 )
87 plot = AnalysisTool()
88 plot.produce.plot = action
90 # Load the relevant columns
91 key_flux = "meas_Flux"
92 plot.process.buildActions.fluxes_meas = LoadVector(vectorKey=key_flux)
93 plot.process.buildActions.fluxes_err = LoadVector(vectorKey=f"{key_flux}Err")
94 plot.process.buildActions.fluxes_ref = LoadVector(vectorKey="ref_Flux")
95 plot.process.buildActions.mags_ref = ConvertFluxToMag(
96 vectorKey=plot.process.buildActions.fluxes_ref.vectorKey
97 )
99 # Compute the y-axis quantity
100 plot.process.buildActions.diff = SubtractVector(
101 actionA=ConvertFluxToMag(
102 vectorKey=plot.process.buildActions.fluxes_meas.vectorKey, returnMillimags=True
103 ),
104 actionB=DivideVector(
105 actionA=plot.process.buildActions.mags_ref,
106 actionB=ConstantValue(value=1e-3),
107 ),
108 )
110 # Filter stars/galaxies, storing quantities separately
111 plot.process.buildActions.galaxySelector = GalaxySelector(vectorKey="refExtendedness")
112 plot.process.buildActions.starSelector = StarSelector(vectorKey="refExtendedness")
113 for singular, plural in (("galaxy", "Galaxies"), ("star", "Stars")):
114 setattr(
115 plot.process.filterActions,
116 f"x{plural}",
117 DownselectVector(
118 vectorKey="mags_ref", selector=VectorSelector(vectorKey=f"{singular}Selector")
119 ),
120 )
121 setattr(
122 plot.process.filterActions,
123 f"y{plural}",
124 DownselectVector(vectorKey="diff", selector=VectorSelector(vectorKey=f"{singular}Selector")),
125 )
126 setattr(
127 plot.process.filterActions,
128 f"flux{plural}",
129 DownselectVector(
130 vectorKey="fluxes_meas", selector=VectorSelector(vectorKey=f"{singular}Selector")
131 ),
132 )
133 setattr(
134 plot.process.filterActions,
135 f"fluxErr{plural}",
136 DownselectVector(
137 vectorKey="fluxes_err", selector=VectorSelector(vectorKey=f"{singular}Selector")
138 ),
139 )
141 # Compute low/high SN summary stats
142 statAction = ScatterPlotStatsAction(
143 vectorKey=f"y{plural}",
144 fluxType=f"flux{plural}",
145 highSNSelector=SnSelector(fluxType=f"flux{plural}", threshold=50),
146 lowSNSelector=SnSelector(fluxType=f"flux{plural}", threshold=20),
147 )
148 setattr(plot.process.calculateActions, plural.lower(), statAction)
150 data = {
151 "ref_Flux": flux,
152 key_flux: flux_meas,
153 f"{key_flux}Err": flux_err,
154 "refExtendedness": extendedness,
155 }
157 self.data = pd.DataFrame(data)
158 print(self.data.columns)
159 self.plot = plot
160 self.plot.finalize()
161 plotInfo = {key: "test" for key in ("plotName", "run", "tableName")}
162 plotInfo["bands"] = []
163 self.plotInfo = plotInfo
165 def tearDown(self):
166 if os.path.exists(self.testDir):
167 shutil.rmtree(self.testDir, True)
168 del self.data
169 del self.plot
170 del self.plotInfo
171 del self.testDir
173 def test_ScatterPlotWithTwoHistsTask(self):
174 plt.rcParams.update(plt.rcParamsDefault)
175 result = self.plot(
176 data=self.data,
177 skymap=None,
178 plotInfo=self.plotInfo,
179 )
180 # unpack the result from the dictionary
181 result = result[type(self.plot.produce.plot).__name__]
182 self.assertTrue(isinstance(result, plt.Figure))
184 # Set to true to save plots as PNGs
185 # Use matplotlib.testing.compare.compare_images if needed
186 save_images = False
187 if save_images:
188 result.savefig(os.path.join(ROOT, "data", "test_scatterPlot.png"))
190 texts, lines = get_and_remove_figure_text(result)
191 if save_images:
192 result.savefig(os.path.join(ROOT, "data", "test_scatterPlot_unlabeled.png"))
194 # Set to true to re-generate reference data
195 resave = False
197 # Compare line values
198 for idx, line in enumerate(lines):
199 filename = os.path.join(path_lines_ref, f"line_{idx}.txt")
200 if resave:
201 np.savetxt(filename, line)
202 arr = np.loadtxt(filename)
203 # Differences of order 1e-12 possible between MacOS and Linux
204 # Plots are generally not expected to be that precise
205 # Differences to 1e-3 should not be visible with this test data
206 self.assertFloatsAlmostEqual(arr, line, atol=1e-3, rtol=1e-4)
208 # Ensure that newlines within labels are replaced by a sentinel
209 newline = "\n"
210 newline_replace = "[newline]"
211 # Compare text labels
212 if resave:
213 with open(filename_texts_ref, "w") as f:
214 f.writelines(f"{text.strip().replace(newline, newline_replace)}\n" for text in texts)
216 with open(filename_texts_ref, "r") as f:
217 texts_ref = set(x.strip() for x in f.readlines())
218 texts_set = set(x.strip().replace(newline, newline_replace) for x in texts)
220 self.assertTrue(texts_ref.issuperset(texts_set))
223class MemoryTester(lsst.utils.tests.MemoryTestCase):
224 pass
227def setup_module(module):
228 lsst.utils.tests.init()
231if __name__ == "__main__": 231 ↛ 232line 231 didn't jump to line 232, because the condition on line 231 was never true
232 lsst.utils.tests.init()
233 unittest.main()