Coverage for python / lsst / analysis / tools / actions / config / binning.py: 52%

19 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-28 09:21 +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__ = ("MagnitudeBinConfig",) 

24 

25import lsst.pex.config as pexConfig 

26 

27 

28class MagnitudeBinConfig(pexConfig.Config): 

29 """Configuration for magnitude-binned metrics. 

30 

31 Bins may only be specified in mmag widths 

32 """ 

33 

34 mag_low_min = pexConfig.Field[int]( 

35 doc="Lower bound for the smallest magnitude bin in millimags", 

36 default=16000, 

37 ) 

38 mag_low_max = pexConfig.Field[int]( 

39 doc="Lower bound for the first excluded (largest) magnitude bin in millimags", 

40 default=31000, 

41 ) 

42 mag_interval = pexConfig.Field[int]( 

43 doc="Spacing interval of magnitude bins in millimags", 

44 default=1000, 

45 check=lambda x: x >= 10, 

46 ) 

47 mag_width = pexConfig.Field[int]( 

48 doc="Width of magnitude bins in millimags", 

49 default=1000, 

50 check=lambda x: x >= 10, 

51 ) 

52 

53 def get_bins(self): 

54 """Get the lower limit for each bin. 

55 

56 Returns 

57 ------- 

58 bins 

59 A list of the lower limits for each bin. 

60 """ 

61 bins = list(range(self.mag_low_min, self.mag_low_max, self.mag_interval)) 

62 return bins 

63 

64 def get_name_bin(self, value_bin: int) -> str: 

65 """Get the default name of a bin for metrics. 

66 

67 Parameters 

68 ---------- 

69 value_bin 

70 The value of the bin (usually the lower limit). 

71 

72 Returns 

73 ------- 

74 name 

75 A string name for the bin. 

76 """ 

77 prefix = str(value_bin // 1000) 

78 suffix = str(value_bin % 1000) 

79 name = f"{prefix}{'p' if suffix else ''}{suffix}" 

80 return name 

81 

82 def validate(self): 

83 if not self.mag_low_max > self.mag_low_min: 

84 raise ValueError(f"{self.mag_low_max} !> {self.mag_low_min}")