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

47 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-13 11:47 +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.vectorActions import DownselectVector, VectorSelector 

29from .genericBuild import MagnitudeXTool 

30 

31 

32class MagnitudeScatterPlot(MagnitudeXTool): 

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

34 

35 suffixes_y_finalize = ListField[str]( 

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

37 default=[""], 

38 ) 

39 

40 def setDefaults(self): 

41 super().setDefaults() 

42 

43 # init with placeholders 

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

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

46 self.produce.plot.addSummaryPlot = False 

47 

48 def finalize(self): 

49 super().finalize() 

50 config_x = self.config_mag_x 

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

52 # Hacky way to check if setup is complete 

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

54 return 

55 self.produce.plot.xAxisLabel = label_x 

56 self.produce.plot.magLabel = self.produce.plot.xAxisLabel 

57 

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

59 # Try to find another or give up 

60 key_err = config_x.key_flux_error 

61 if key_err is None: 

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

63 if config.key_flux_error is not None: 

64 key_err = key 

65 break 

66 # Try to add PSF flux if all else fails 

67 if key_err is None: 

68 config_err = self.fluxes_default.psf_err 

69 key_err = config_err.key_flux_error 

70 self.fluxes["flux_sn"] = config_err 

71 else: 

72 key_err = self.mag_x 

73 

74 keys_filter = [("", "flux_", self.mag_x), ("Err", "flux_err_", key_err)] 

75 if key_err != self.mag_x: 

76 keys_filter.append(("", "flux_", key_err)) 

77 

78 for prefix, plural in (("star", "Stars"), ("galaxy", "Galaxies")): 

79 for suffix, prefix_vec, key in keys_filter: 

80 setattr( 

81 self.process.filterActions, 

82 f"{prefix}_{key}_flux{suffix}", 

83 DownselectVector( 

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

85 selector=VectorSelector(vectorKey=f"{prefix}Selector"), 

86 ), 

87 ) 

88 

89 for suffix_y in self.suffixes_y_finalize: 

90 statAction = ScatterPlotStatsAction( 

91 vectorKey=f"y{plural.capitalize()}{suffix_y}", 

92 prefix=plural, 

93 suffix=suffix_y, 

94 ) 

95 fluxType = f"{prefix}_{key_err}_flux" 

96 statAction.highSNSelector.fluxType = fluxType 

97 statAction.highSNSelector.threshold = 200 

98 statAction.lowSNSelector.fluxType = fluxType 

99 statAction.lowSNSelector.threshold = 10 

100 statAction.fluxType = fluxType 

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