Coverage for metadetect / lsst / configs.py: 12%
66 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 00:04 -0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 00:04 -0800
1from copy import deepcopy
3from .defaults import (
4 DEFAULT_MDET_CONFIG,
5 DEFAULT_WEIGHT_FWHMS,
6 DEFAULT_FWHM_SMOOTH,
7 DEFAULT_FWHM_REG,
8 DEFAULT_STAMP_SIZES,
9)
12def get_config(config=None):
13 """
14 extract a full config, overriding defaults with the input
16 Parameters
17 ----------
18 config: dict, optional
19 Entries in this dict override defaults stored in
20 defaults.DEFAULT_MDET_CONFIG
22 Returns
23 -------
24 Full config
25 """
27 new_config = deepcopy(DEFAULT_MDET_CONFIG)
29 if config is not None:
30 new_config.update(deepcopy(config))
32 _fill_config(new_config)
34 return new_config
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'
44 if 'model' in config:
45 config['meas_type'] = config.pop('model')
47 for key in config:
48 if key not in DEFAULT_MDET_CONFIG:
49 raise ValueError('bad key in mdet config: %s' % key)
51 meas_type = config['meas_type']
53 if 'stamp_size' not in config or config['stamp_size'] is None:
54 config['stamp_size'] = get_default_stamp_size(meas_type)
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
62 else:
63 # we will ignore this for am/gauss, so use wmom defaults
64 config['weight'] = get_default_weight_config('wmom')
66 _verify_weight_config(config['weight'])
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'])
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)}
81 if meas_type in ('pgauss', 'ksigma'):
82 weight_config['fwhm_smooth'] = DEFAULT_FWHM_SMOOTH
83 weight_config['fwhm_reg'] = DEFAULT_FWHM_REG
85 return weight_config
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)
95 return DEFAULT_STAMP_SIZES[meas_type]
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)
105 return DEFAULT_WEIGHT_FWHMS[meas_type]
108def _verify_psf_config(config):
110 name = 'psf'
111 _check_required_keywords(
112 config=config, required_keys=['model'], name=name,
113 )
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']
123 _check_keywords(
124 config=config,
125 allowed_keys=allowed_keys,
126 name=name,
127 )
130def _verify_detect_config(config):
132 name = 'detect'
133 _check_required_keywords(
134 config=config, required_keys=['thresh'], name=name,
135 )
137 _check_keywords(config=config, allowed_keys=['thresh'], name=name)
140def _verify_weight_config(config):
142 name = 'weight'
143 _check_required_keywords(
144 config=config, required_keys=['fwhm'], name=name,
145 )
147 _check_keywords(
148 config=config, allowed_keys=['fwhm', 'fwhm_smooth', 'fwhm_reg'],
149 name=name,
150 )
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')
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')