Coverage for python/lsst/multiprofit/utils.py: 90%
29 statements
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-22 09:35 +0000
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-22 09:35 +0000
1# This file is part of multiprofit.
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/>.
22__all__ = [
23 "arbitrary_allowed_config",
24 "frozen_arbitrary_allowed_config",
25 "get_params_uniq",
26 "set_config_from_dict",
27]
29import logging
30from typing import Any
32import pydantic
34import lsst.gauss2d.fit as g2f
35import lsst.pex.config as pexConfig
37_LOG = logging.getLogger(__name__)
39# Pydantic config to allow arbitrary typed Fields.
40arbitrary_allowed_config = pydantic.ConfigDict(
41 arbitrary_types_allowed=True,
42 extra="forbid",
43)
45# As above, but frozen
46frozen_arbitrary_allowed_config = pydantic.ConfigDict(
47 arbitrary_types_allowed=True,
48 extra="forbid",
49 frozen=True,
50)
53def get_params_uniq(parametric: g2f.Parametric, **kwargs: Any) -> list[g2f.ParameterD]:
54 """Get a sorted set of parameters matching a filter.
56 Parameters
57 ----------
58 parametric
59 The parametric object to get parameters from.
60 **kwargs
61 Keyword arguments to pass to g2f.ParamFilter.
63 Returns
64 -------
65 params
66 The unique parameters from the parametric object matching the filter.
67 """
68 params = parametric.parameters(paramfilter=g2f.ParamFilter(**kwargs))
69 # This should always return the same list as:
70 # list({p: None for p in }.keys())
71 return g2f.params_unique(params)
74def set_config_from_dict(
75 config: pexConfig.Config | pexConfig.dictField.Dict | pexConfig.configDictField.ConfigDict | dict,
76 overrides: dict[str, Any],
77) -> None:
78 """Set `lsst.pex.config` params from a dict.
80 Parameters
81 ----------
82 config
83 A config, dictField or configDictField object.
84 overrides
85 A dict of key-value pairs to override in the config.
86 """
87 is_config_dict = hasattr(config, "__getitem__")
88 if is_config_dict:
89 keys = tuple(config.keys())
90 for key in keys:
91 if key not in overrides: 91 ↛ 92line 91 didn't jump to line 92 because the condition on line 91 was never true
92 del config[key]
93 for key, value in overrides.items():
94 if isinstance(value, dict):
95 # Note that this only works on a ConfigDict if a value is set
96 attr = config[key] if is_config_dict else getattr(config, key)
97 set_config_from_dict(attr, value)
98 else:
99 try:
100 if is_config_dict:
101 config[key] = value
102 else:
103 setattr(config, key, value)
104 # TODO: Check if a narrower Exception type is better in DM-45577
105 except Exception as e:
106 _LOG.warning(e)