Coverage for python/lsst/images/aperture_corrections.py: 74%
32 statements
« prev ^ index » next coverage.py v7.14.3, created at 2026-07-02 08:55 +0000
« prev ^ index » next coverage.py v7.14.3, created at 2026-07-02 08:55 +0000
1# This file is part of lsst-images.
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# Use of this source code is governed by a 3-clause BSD-style
10# license that can be found in the LICENSE file.
11from __future__ import annotations
13__all__ = (
14 "ApertureCorrectionMap",
15 "ApertureCorrectionMapSerializationModel",
16 "aperture_corrections_from_legacy",
17 "aperture_corrections_to_legacy",
18)
20from typing import TYPE_CHECKING, Any, ClassVar, cast, final
22import pydantic
24from .fields import Field, FieldSerializationModel, field_from_legacy
25from .serialization import ArchiveTree, InputArchive, InvalidParameterError, OutputArchive
27if TYPE_CHECKING:
28 try:
29 from lsst.afw.image import ApCorrMap as LegacyApCorrMap
30 except ImportError:
31 type LegacyApCorrMap = Any # type: ignore[no-redef]
33type ApertureCorrectionMap = dict[str, Field]
36def aperture_corrections_from_legacy(legacy_ap_corr_map: LegacyApCorrMap) -> ApertureCorrectionMap:
37 """Convert a `lsst.afw.image.ApCorrMap` instance to a `dict` mapping
38 `str` algorithm name to `~.fields.BaseField`.
40 Parameters
41 ----------
42 legacy_ap_corr_map
43 Legacy aperture correction map to convert.
44 """
45 return {name: field_from_legacy(legacy_field) for name, legacy_field in legacy_ap_corr_map.items()}
48def aperture_corrections_to_legacy(aperture_corrections: ApertureCorrectionMap) -> LegacyApCorrMap:
49 """Convert from a `dict` (mapping `str` algorithm name to
50 `~.fields.BaseField`) to a `lsst.afw.image.ApCorrMap` instance.
52 Parameters
53 ----------
54 aperture_corrections
55 Aperture correction map to convert to the legacy type.
56 """
57 from lsst.afw.image import ApCorrMap
59 result = ApCorrMap()
60 for name, field in aperture_corrections.items():
61 # Not all Field types have a to_legacy, but the ones we care about do;
62 # if we're wrong about that, the AttributeError is probably the best
63 # we can do.
64 result[name] = field.to_legacy()
65 return result
68@final
69class ApertureCorrectionMapSerializationModel(ArchiveTree):
70 """Serialization model for aperture correction maps.
72 Notes
73 -----
74 The in-memory aperture correction map type is just a `dict` from `str`
75 to `~.fields.BaseField`, so the `serialize` and `deserialize` methods are
76 defined here.
77 """
79 SCHEMA_NAME: ClassVar[str] = "aperture_correction_map"
80 SCHEMA_VERSION: ClassVar[str] = "1.0.0"
81 MIN_READ_VERSION: ClassVar[int] = 1
82 PUBLIC_TYPE: ClassVar[type] = dict
84 fields: dict[str, FieldSerializationModel] = pydantic.Field(
85 default_factory=dict,
86 description="Mapping from flux algorithm name to the aperture correction field for that algorithm.",
87 )
89 @staticmethod
90 def serialize(
91 map: ApertureCorrectionMap, archive: OutputArchive[Any]
92 ) -> ApertureCorrectionMapSerializationModel:
93 """Write an aperture correction map to an archive.
95 Parameters
96 ----------
97 map
98 Aperture correction map to serialize.
99 archive
100 Archive to write to.
101 """
102 result = ApertureCorrectionMapSerializationModel()
103 for name, field in map.items():
104 result.fields[name] = cast(
105 FieldSerializationModel, archive.serialize_direct(f"fields/{name}", field.serialize)
106 )
107 return result
109 def deserialize(self, archive: InputArchive[Any], **kwargs: Any) -> ApertureCorrectionMap:
110 """Read an aperture correction map from an archive.
112 Parameters
113 ----------
114 archive
115 Archive to read from.
116 **kwargs
117 Unsupported keyword arguments are accepted only to provide
118 better error messages (raising
119 `.serialization.InvalidParameterError`).
120 """
121 if kwargs: 121 ↛ 122line 121 didn't jump to line 122 because the condition on line 121 was never true
122 raise InvalidParameterError(f"Unrecognized parameters for Image: {set(kwargs.keys())}.")
123 return {name: field.deserialize(archive) for name, field in self.fields.items()}