LSST Applications g00d0e8bbd7+edbf708997,g03191d30f7+6b31559d11,g118115db7c+ac820e85d2,g199a45376c+5137f08352,g1fd858c14a+90100aa1a7,g262e1987ae+64df5f6984,g29ae962dfc+1eb4aece83,g2cef7863aa+73c82f25e4,g3541666cd7+1e37cdad5c,g35bb328faa+edbf708997,g3fd5ace14f+fb4e2866cc,g47891489e3+19fcc35de2,g53246c7159+edbf708997,g5b326b94bb+d622351b67,g64539dfbff+dfe1dff262,g67b6fd64d1+19fcc35de2,g74acd417e5+cfdc02aca8,g786e29fd12+af89c03590,g7aefaa3e3d+dc1a598170,g87389fa792+a4172ec7da,g88cb488625+60ba2c3075,g89139ef638+19fcc35de2,g8d4809ba88+dfe1dff262,g8d7436a09f+db94b797be,g8ea07a8fe4+79658f16ab,g90f42f885a+6577634e1f,g9722cb1a7f+d8f85438e7,g98df359435+7fdd888faa,ga2180abaac+edbf708997,ga9e74d7ce9+128cc68277,gbf99507273+edbf708997,gca7fc764a6+19fcc35de2,gd7ef33dd92+19fcc35de2,gdab6d2f7ff+cfdc02aca8,gdbb4c4dda9+dfe1dff262,ge410e46f29+19fcc35de2,ge41e95a9f2+dfe1dff262,geaed405ab2+062dfc8cdc,w.2025.46
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# Register the legacy blend_type in the blend registry
85ScarletBlendBaseData.blend_registry["hierarchical_blend"] = HierarchicalBlendData
86
87
88@migration(BLEND_TYPE, PRE_SCHEMA)
89def _to_1_0_0(data: dict) -> dict:
90 """Migrate a pre-schema hierarchical blend to schema version 1.0.0
91
92 There were no changes to this data model in v1.0.0 but we need
93 to provide a way to migrate pre-schema data.
94
95 Parameters
96 ----------
97 data :
98 The data to migrate.
99
100 Returns
101 -------
102 result :
103 The migrated data.
104 """
105 data["version"] = "1.0.0"
106 return data
HierarchicalBlendData from_dict(cls, dict data, DTypeLike dtype=np.float32)