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