Coverage for metadetect / lsst / configs.py: 12%

66 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-22 02:30 -0800

1from copy import deepcopy 

2 

3from .defaults import ( 

4 DEFAULT_MDET_CONFIG, 

5 DEFAULT_WEIGHT_FWHMS, 

6 DEFAULT_FWHM_SMOOTH, 

7 DEFAULT_FWHM_REG, 

8 DEFAULT_STAMP_SIZES, 

9) 

10 

11 

12def get_config(config=None): 

13 """ 

14 extract a full config, overriding defaults with the input 

15 

16 Parameters 

17 ---------- 

18 config: dict, optional 

19 Entries in this dict override defaults stored in 

20 defaults.DEFAULT_MDET_CONFIG 

21 

22 Returns 

23 ------- 

24 Full config 

25 """ 

26 

27 new_config = deepcopy(DEFAULT_MDET_CONFIG) 

28 

29 if config is not None: 

30 new_config.update(deepcopy(config)) 

31 

32 _fill_config(new_config) 

33 

34 return new_config 

35 

36 

37def _fill_config(config): 

38 """ 

39 check the config and fill in some defaults programatically 

40 """ 

41 # moments are not a model, use more generic description measurement_type 

42 # or meas_type for short. But support old name 'model' 

43 

44 if 'model' in config: 

45 config['meas_type'] = config.pop('model') 

46 

47 for key in config: 

48 if key not in DEFAULT_MDET_CONFIG: 

49 raise ValueError('bad key in mdet config: %s' % key) 

50 

51 meas_type = config['meas_type'] 

52 

53 if 'stamp_size' not in config or config['stamp_size'] is None: 

54 config['stamp_size'] = get_default_stamp_size(meas_type) 

55 

56 if meas_type not in ['am', 'gauss']: 

57 wc = get_default_weight_config(meas_type) 

58 if 'weight' in config and config['weight'] is not None: 

59 wc.update(config['weight']) 

60 config['weight'] = wc 

61 

62 else: 

63 # we will ignore this for am/gauss, so use wmom defaults 

64 config['weight'] = get_default_weight_config('wmom') 

65 

66 _verify_weight_config(config['weight']) 

67 

68 # note we allow ngmix.metacal.get_all_metacal to do its 

69 # own verification 

70 _verify_psf_config(config['psf']) 

71 _verify_detect_config(config['detect']) 

72 

73 

74def get_default_weight_config(meas_type): 

75 """ 

76 get the default weight function configuration based 

77 on the measurement type 

78 """ 

79 weight_config = {'fwhm': get_default_weight_fwhm(meas_type)} 

80 

81 if meas_type in ('pgauss', 'ksigma'): 

82 weight_config['fwhm_smooth'] = DEFAULT_FWHM_SMOOTH 

83 weight_config['fwhm_reg'] = DEFAULT_FWHM_REG 

84 

85 return weight_config 

86 

87 

88def get_default_stamp_size(meas_type): 

89 """ 

90 get default stamp size for the input measurement type 

91 """ 

92 if meas_type not in DEFAULT_STAMP_SIZES: 

93 raise ValueError('bad meas type: %s' % meas_type) 

94 

95 return DEFAULT_STAMP_SIZES[meas_type] 

96 

97 

98def get_default_weight_fwhm(meas_type): 

99 """ 

100 get default weight fwhm for the input measurement type 

101 """ 

102 if meas_type not in DEFAULT_WEIGHT_FWHMS: 

103 raise ValueError('bad meas type: %s' % meas_type) 

104 

105 return DEFAULT_WEIGHT_FWHMS[meas_type] 

106 

107 

108def _verify_psf_config(config): 

109 

110 name = 'psf' 

111 _check_required_keywords( 

112 config=config, required_keys=['model'], name=name, 

113 ) 

114 

115 model = config['model'] 

116 if model in ['am', 'admom']: 

117 allowed_keys = ['model', 'ntry'] 

118 elif model == 'wmom': 

119 allowed_keys = ['model', 'weight_fwhm'] 

120 else: 

121 allowed_keys = ['model', 'lm_pars', 'ntry'] 

122 

123 _check_keywords( 

124 config=config, 

125 allowed_keys=allowed_keys, 

126 name=name, 

127 ) 

128 

129 

130def _verify_detect_config(config): 

131 

132 name = 'detect' 

133 _check_required_keywords( 

134 config=config, required_keys=['thresh'], name=name, 

135 ) 

136 

137 _check_keywords(config=config, allowed_keys=['thresh'], name=name) 

138 

139 

140def _verify_weight_config(config): 

141 

142 name = 'weight' 

143 _check_required_keywords( 

144 config=config, required_keys=['fwhm'], name=name, 

145 ) 

146 

147 _check_keywords( 

148 config=config, allowed_keys=['fwhm', 'fwhm_smooth', 'fwhm_reg'], 

149 name=name, 

150 ) 

151 

152 

153def _check_required_keywords(config, required_keys, name): 

154 for key in required_keys: 

155 if key not in config: 

156 raise ValueError(f'key "{key}" must be present in {name} config') 

157 

158 

159def _check_keywords(config, allowed_keys, name): 

160 for key in config: 

161 if key not in allowed_keys: 

162 raise ValueError(f'key "{key}" not allowed in {name} config')