LSST Applications g013ef56533+63812263fb,g083dd6704c+a047e97985,g199a45376c+0ba108daf9,g1fd858c14a+fde7a7a78c,g210f2d0738+db0c280453,g262e1987ae+abed931625,g29ae962dfc+058d1915d8,g2cef7863aa+aef1011c0b,g35bb328faa+8c5ae1fdc5,g3fd5ace14f+64337f1634,g47891489e3+f459a6810c,g53246c7159+8c5ae1fdc5,g54cd7ddccb+890c8e1e5d,g5a60e81ecd+d9e514a434,g64539dfbff+db0c280453,g67b6fd64d1+f459a6810c,g6ebf1fc0d4+8c5ae1fdc5,g7382096ae9+36d16ea71a,g74acd417e5+c70e70fbf6,g786e29fd12+668abc6043,g87389fa792+8856018cbb,g89139ef638+f459a6810c,g8d7436a09f+1b779678e3,g8ea07a8fe4+81eaaadc04,g90f42f885a+34c0557caf,g97be763408+9583a964dd,g98a1a72a9c+028271c396,g98df359435+530b675b85,gb8cb2b794d+4e54f68785,gbf99507273+8c5ae1fdc5,gc2a301910b+db0c280453,gca7fc764a6+f459a6810c,gd7ef33dd92+f459a6810c,gdab6d2f7ff+c70e70fbf6,ge410e46f29+f459a6810c,ge41e95a9f2+db0c280453,geaed405ab2+e3b4b2a692,gf9a733ac38+8c5ae1fdc5,w.2025.43
LSST Data Management Base Package
Loading...
Searching...
No Matches
hierarchical_blend.py
Go to the documentation of this file.
1from __future__ import annotations
2
3from dataclasses import dataclass
4from typing import Any
5
6import numpy as np
7from numpy.typing import DTypeLike
8
9from .blend_base import ScarletBlendBaseData
10from .migration import PRE_SCHEMA, MigrationRegistry, migration
11from .utils import PersistenceError, decode_metadata, encode_metadata
12
13__all__ = ["HierarchicalBlendData"]
14
15CURRENT_SCHEMA = "1.0.0"
16BLEND_TYPE = "hierarchical"
17MigrationRegistry.set_current(BLEND_TYPE, CURRENT_SCHEMA)
18
19
20@dataclass(kw_only=True)
22 """Data for a hierarchical blend.
23
24 Attributes
25 ----------
26 blend_type :
27 The type of blend being stored
28 children :
29 Map from blend IDs to child blends.
30 version :
31 The schema version of the HierarchicalBlendData.
32 """
33
34 blend_type: str = BLEND_TYPE
35 children: dict[int, ScarletBlendBaseData]
36 version: str = CURRENT_SCHEMA
37
38 def as_dict(self) -> dict:
39 """Return the object encoded into a dict for JSON serialization
40
41 Returns
42 -------
43 result :
44 The object encoded as a JSON compatible dict
45 """
46 result: dict[str, Any] = {
47 "blend_type": self.blend_type,
48 "children": {bid: child.as_dict() for bid, child in self.children.items()},
49 "version": self.version,
50 }
51 if self.metadata is not None:
52 result["metadata"] = encode_metadata(self.metadata)
53 return result
54
55 @classmethod
56 def from_dict(cls, data: dict, dtype: DTypeLike = np.float32) -> HierarchicalBlendData:
57 """Reconstruct `HierarchicalBlendData` from JSON compatible dict.
58
59 Parameters
60 ----------
61 data :
62 Dictionary representation of the object
63 dtype :
64 Datatype of the resulting model.
65
66 Returns
67 -------
68 result :
69 The reconstructed object
70 """
71 data = MigrationRegistry.migrate(BLEND_TYPE, data)
72 children: dict[int, ScarletBlendBaseData] = {}
73 for blend_id, child in data["children"].items():
74 try:
75 children[int(blend_id)] = ScarletBlendBaseData.from_dict(child, dtype=dtype)
76 except KeyError:
77 raise PersistenceError(f"Unknown blend type: {child['blend_type']} for blend ID: {blend_id}")
78
79 metadata = decode_metadata(data.get("metadata", None))
80 return cls(children=children, metadata=metadata)
81
82
83HierarchicalBlendData.register()
84
85
86@migration(BLEND_TYPE, PRE_SCHEMA)
87def _to_1_0_0(data: dict) -> dict:
88 """Migrate a pre-schema hierarchical blend to schema version 1.0.0
89
90 There were no changes to this data model in v1.0.0 but we need
91 to provide a way to migrate pre-schema data.
92
93 Parameters
94 ----------
95 data :
96 The data to migrate.
97
98 Returns
99 -------
100 result :
101 The migrated data.
102 """
103 data["version"] = "1.0.0"
104 return data
HierarchicalBlendData from_dict(cls, dict data, DTypeLike dtype=np.float32)