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

43 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-08 04:38 -0700

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 ..actions.plot.scatterplotWithTwoHists import ScatterPlotStatsAction, ScatterPlotWithTwoHists 

26from ..actions.vector.vectorActions import DownselectVector, VectorSelector 

27from .genericBuild import MagnitudeXTool 

28 

29 

30class MagnitudeScatterPlot(MagnitudeXTool): 

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

32 

33 def setDefaults(self): 

34 super().setDefaults() 

35 

36 # init with placeholders 

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

38 self.produce.plotTypes = ["galaxies", "stars"] 

39 

40 def finalize(self): 

41 super().finalize() 

42 config_x = self.config_mag_x 

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

44 # Hacky way to check if setup is complete 

45 if self.produce.xAxisLabel == label_x: 

46 return 

47 self.produce.xAxisLabel = label_x 

48 self.produce.magLabel = self.produce.xAxisLabel 

49 

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

51 # Try to find another or give up 

52 key_err = config_x.key_flux_error 

53 if key_err is None: 

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

55 if config.key_flux_error is not None: 

56 key_err = key 

57 break 

58 # Try to add PSF flux if all else fails 

59 if key_err is None: 

60 config_err = self.fluxes_default.psf_err 

61 key_err = config_err.key_flux_error 

62 self.fluxes["flux_sn"] = config_err 

63 else: 

64 key_err = self.mag_x 

65 

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

67 if key_err != self.mag_x: 

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

69 

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

71 for suffix, prefix_vec, key in keys_filter: 

72 setattr( 

73 self.process.filterActions, 

74 f"{prefix}_{key}{suffix}", 

75 DownselectVector( 

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

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

78 ), 

79 ) 

80 

81 statAction = ScatterPlotStatsAction(vectorKey=f"y{plural.capitalize()}") 

82 fluxType = f"{prefix}_{key_err}" 

83 statAction.highSNSelector.fluxType = fluxType 

84 statAction.highSNSelector.threshold = 200 

85 statAction.lowSNSelector.fluxType = fluxType 

86 statAction.lowSNSelector.threshold = 10 

87 statAction.fluxType = fluxType 

88 setattr(self.process.calculateActions, plural, statAction)