Coverage for python/lsst/images/aperture_corrections.py: 49%
31 statements
« prev ^ index » next coverage.py v7.14.1, created at 2026-06-03 08:08 +0000
« prev ^ index » next coverage.py v7.14.1, created at 2026-06-03 08:08 +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`.
39 """
40 return {name: field_from_legacy(legacy_field) for name, legacy_field in legacy_ap_corr_map.items()}
43def aperture_corrections_to_legacy(aperture_corrections: ApertureCorrectionMap) -> LegacyApCorrMap:
44 """Convert from a `dict` (mapping `str` algorithm name to
45 `~.fields.BaseField`) to a `lsst.afw.image.ApCorrMap` instance.
46 """
47 from lsst.afw.image import ApCorrMap
49 result = ApCorrMap()
50 for name, field in aperture_corrections.items():
51 # Not all Field types have a to_legacy, but the ones we care about do;
52 # if we're wrong about that, the AttributeError is probably the best
53 # we can do.
54 result[name] = field.to_legacy() # type: ignore[union-attr]
55 return result
58@final
59class ApertureCorrectionMapSerializationModel(ArchiveTree):
60 """Serialization model for aperture correction maps.
62 Notes
63 -----
64 The in-memory aperture correction map type is just a `dict` from `str`
65 to `~.fields.BaseField`, so the `serialize` and `deserialize` methods are
66 defined here.
67 """
69 SCHEMA_NAME: ClassVar[str] = "aperture_correction_map"
70 SCHEMA_VERSION: ClassVar[str] = "1.0.0"
71 MIN_READ_VERSION: ClassVar[int] = 1
73 fields: dict[str, FieldSerializationModel] = pydantic.Field(
74 default_factory=dict,
75 description="Mapping from flux algorithm name to the aperture correction field for that algorithm.",
76 )
78 @staticmethod
79 def serialize(
80 map: ApertureCorrectionMap, archive: OutputArchive[Any]
81 ) -> ApertureCorrectionMapSerializationModel:
82 """Write an aperture correction map to an archive."""
83 result = ApertureCorrectionMapSerializationModel()
84 for name, field in map.items():
85 result.fields[name] = cast(
86 FieldSerializationModel, archive.serialize_direct(f"fields/{name}", field.serialize)
87 )
88 return result
90 def deserialize(self, archive: InputArchive[Any], **kwargs: Any) -> ApertureCorrectionMap:
91 """Read an aperture correction map from an archive."""
92 if kwargs:
93 raise InvalidParameterError(f"Unrecognized parameters for Image: {set(kwargs.keys())}.")
94 return {name: field.deserialize(archive) for name, field in self.fields.items()}