Coverage for tests/test_scatterPlot.py: 29%

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

78 statements  

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 unittest 

24import lsst.utils.tests 

25 

26from lsst.analysis.drp.calcFunctors import MagDiff 

27from lsst.analysis.drp.dataSelectors import GalaxyIdentifier 

28from lsst.analysis.drp.scatterPlot import ScatterPlotWithTwoHistsTask, ScatterPlotWithTwoHistsTaskConfig 

29 

30import matplotlib 

31import matplotlib.pyplot as plt 

32from matplotlib.testing.compare import compare_images, ImageComparisonFailure 

33 

34import numpy as np 

35from numpy.random import default_rng 

36import os 

37import pandas as pd 

38import shutil 

39import tempfile 

40 

41matplotlib.use("Agg") 

42 

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

44filename_figure_ref = os.path.join(ROOT, "data", "test_scatterPlot.png") 

45 

46 

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

48 """ScatterPlotWithTwoHistsTask test case.""" 

49 def setUp(self): 

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

51 

52 # Set up a quasi-plausible measurement catalog 

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

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

55 rng = default_rng(0) 

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

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

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

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

60 extendedness = extendedness[good] 

61 flux = flux[good] 

62 flux_meas = flux_meas[good] 

63 flux_err = flux_err[good] 

64 

65 # Configure the plot to show observed vs true mags 

66 config = ScatterPlotWithTwoHistsTaskConfig( 

67 axisLabels={"x": "mag", "y": "mag meas - ref", "mag": "mag"}, 

68 ) 

69 config.selectorActions.flagSelector.bands = ["i"] 

70 config.axisActions.yAction = MagDiff(col1="refcat_flux", col2="refcat_flux") 

71 config.nonBandColumnPrefixes.append("refcat") 

72 config.sourceSelectorActions.galaxySelector = GalaxyIdentifier 

73 config.highSnStatisticSelectorActions.statSelector.threshold = 50 

74 config.lowSnStatisticSelectorActions.statSelector.threshold = 20 

75 self.task = ScatterPlotWithTwoHistsTask(config=config) 

76 

77 n = len(flux) 

78 self.bands, columns = config.get_requirements() 

79 data = { 

80 "refcat_flux": flux, 

81 "patch": np.zeros(n, dtype=int), 

82 } 

83 

84 # Assign values to columns based on their unchanged default names 

85 for column in columns: 

86 if column not in data: 

87 if column.startswith("detect"): 

88 data[column] = np.ones(n, dtype=bool) 

89 elif column.endswith("_flag") or "Flag" in column: 

90 data[column] = np.zeros(n, dtype=bool) 

91 elif column.endswith("Flux"): 

92 config.axisActions.yAction.col1 = column 

93 data[column] = flux_meas 

94 elif column.endswith("FluxErr"): 

95 data[column] = flux_err 

96 elif column.endswith("_extendedness"): 

97 data[column] = extendedness 

98 else: 

99 raise RuntimeError(f"Unexpected column {column} in ScatterPlotWithTwoHistsTaskConfig") 

100 

101 self.data = pd.DataFrame(data) 

102 

103 def tearDown(self): 

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

105 shutil.rmtree(self.testDir, True) 

106 del self.bands 

107 del self.data 

108 del self.task 

109 

110 def test_ScatterPlotWithTwoHistsTask(self): 

111 plt.rcParams.update(plt.rcParamsDefault) 

112 result = self.task.run(self.data, 

113 dataId={}, 

114 runName="test", 

115 skymap=None, 

116 tableName="test", 

117 bands=self.bands, 

118 plotName="test") 

119 

120 filename_figure_tmp = os.path.join(self.testDir, "test_scatterPlot.png") 

121 result.scatterPlot.savefig(filename_figure_tmp) 

122 diff = compare_images(filename_figure_tmp, filename_figure_ref, 0) 

123 if diff is not None: 

124 raise ImageComparisonFailure(diff) 

125 

126 

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

128 pass 

129 

130 

131def setup_module(module): 

132 lsst.utils.tests.init() 

133 

134 

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

136 lsst.utils.tests.init() 

137 unittest.main()