Coverage for python / lsst / images / aperture_corrections.py: 48%

27 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-07 08:34 +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 

12 

13__all__ = ( 

14 "ApertureCorrectionMap", 

15 "ApertureCorrectionMapSerializationModel", 

16 "aperture_corrections_from_legacy", 

17 "aperture_corrections_to_legacy", 

18) 

19 

20from typing import TYPE_CHECKING, Any, final 

21 

22import pydantic 

23 

24from .fields import Field, FieldSerializationModel, field_from_legacy 

25from .serialization import ArchiveTree, InputArchive, OutputArchive 

26 

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] 

32 

33type ApertureCorrectionMap = dict[str, Field] 

34 

35 

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()} 

41 

42 

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 

48 

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 

56 

57 

58@final 

59class ApertureCorrectionMapSerializationModel(ArchiveTree): 

60 """Serialization model for aperture correction maps. 

61 

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 """ 

68 

69 fields: dict[str, FieldSerializationModel] = pydantic.Field( 

70 default_factory=dict, 

71 description="Mapping from flux algorithm name to the aperture correction field for that algorithm.", 

72 ) 

73 

74 @staticmethod 

75 def serialize( 

76 map: ApertureCorrectionMap, archive: OutputArchive[Any] 

77 ) -> ApertureCorrectionMapSerializationModel: 

78 """Write an aperture correction map to an archive.""" 

79 result = ApertureCorrectionMapSerializationModel() 

80 for name, field in map.items(): 

81 result.fields[name] = field.serialize(archive) 

82 return result 

83 

84 def deserialize(self, archive: InputArchive[Any]) -> ApertureCorrectionMap: 

85 """Read an aperture correction map from an archive.""" 

86 from .fields import deserialize_field 

87 

88 return {name: deserialize_field(field, archive) for name, field in self.fields.items()}