Coverage for python / lsst / analysis / tools / atools / genericProduce.py: 13%

61 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-04 17:42 +0000

1# This file is part of analysis_tools. 

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/>. 

21from __future__ import annotations 

22 

23__all__ = ("MagnitudeScatterPlot",) 

24 

25from lsst.pex.config import ListField 

26 

27from ..actions.plot.scatterplotWithTwoHists import ScatterPlotStatsAction, ScatterPlotWithTwoHists 

28from ..actions.vector import DownselectVector 

29from ..actions.vector.selectors import VectorSelector 

30from .genericBuild import MagnitudeXTool 

31 

32 

33class MagnitudeScatterPlot(MagnitudeXTool): 

34 """A scatter plot with a magnitude on the x-axis.""" 

35 

36 suffixes_y_finalize = ListField[str]( 

37 doc="Suffixes for y-axis keys that require finalization of summary stats", 

38 default=[""], 

39 ) 

40 

41 def setDefaults(self): 

42 super().setDefaults() 

43 

44 # init with placeholders 

45 self.produce.plot = ScatterPlotWithTwoHists(xAxisLabel="", yAxisLabel="", magLabel="") 

46 self.produce.plot.plotTypes = ["galaxies", "stars"] 

47 self.produce.plot.addSummaryPlot = False 

48 

49 def finalize(self): 

50 super().finalize() 

51 # Set plot types based on config 

52 object_classes = [] 

53 if self.use_galaxies: 

54 object_classes.append("galaxy") 

55 if self.use_stars: 

56 object_classes.append("star") 

57 self.produce.plot.plotTypes = [ 

58 self.get_class_name_plural(object_class) for object_class in object_classes 

59 ] 

60 

61 config_x = self.config_mag_x 

62 label_x = f"{{band}} {config_x.name_flux} (mag)" 

63 # Hacky way to check if setup is complete 

64 if self.produce.plot.xAxisLabel == label_x: 

65 return 

66 self.produce.plot.xAxisLabel = label_x 

67 self.produce.plot.magLabel = self.produce.plot.xAxisLabel 

68 

69 # Can't compute S/N of magnitude with no errors (e.g. true mag) 

70 # Try to find another or give up 

71 key_err = config_x.key_flux_error 

72 name_err = config_x.name_flux_short 

73 if key_err is None: 

74 for key, config in self.fluxes.items(): 

75 if config.key_flux_error is not None: 

76 key_err = key 

77 name_err = config.name_flux_short 

78 break 

79 # Try to add PSF flux if all else fails 

80 if key_err is None: 

81 config_err = self.fluxes_default.psf_err 

82 key_err = config_err.key_flux_error 

83 name_err = config_err.name_flux_short 

84 self.fluxes["flux_sn"] = config_err 

85 else: 

86 key_err = self.mag_x 

87 name_err = self.config_mag_x.name_flux_short 

88 

89 keys_filter = [ 

90 ("", "flux_", self.mag_x, self.config_mag_x.name_flux_short), 

91 ("Err", "flux_err_", key_err, name_err), 

92 ] 

93 # The magnitude used for S/N is not the x-axis magnitude 

94 # So it has to be loaded and filtered separately 

95 if key_err != self.mag_x: 

96 keys_filter.append(("", "flux_", key_err, name_err)) 

97 

98 for object_class in object_classes: 

99 plural = self.get_class_name_plural(object_class) 

100 for suffix, prefix_vec, key, name_attr_mag in keys_filter: 

101 name_selector = self.get_name_attr_selector(object_class) 

102 setattr( 

103 self.process.filterActions, 

104 f"{object_class}_{name_attr_mag}_flux{suffix}", 

105 DownselectVector( 

106 vectorKey=f"{prefix_vec}{key}", 

107 selector=VectorSelector(vectorKey=name_selector), 

108 ), 

109 ) 

110 

111 name_y = self.get_name_attr_values(object_class) 

112 for suffix_y in self.suffixes_y_finalize: 

113 statAction = ScatterPlotStatsAction( 

114 vectorKey=f"{name_y}{suffix_y}", 

115 prefix=plural, 

116 suffix=suffix_y, 

117 ) 

118 fluxType = f"{object_class}_{name_err}_flux" 

119 statAction.highSNSelector.fluxType = fluxType 

120 statAction.highSNSelector.threshold = 200 

121 statAction.lowSNSelector.fluxType = fluxType 

122 statAction.lowSNSelector.threshold = 10 

123 statAction.fluxType = fluxType 

124 setattr(self.process.calculateActions, f"stats_{plural}{suffix_y}", statAction)