LSST Applications g00274db5b6+edbf708997,g00d0e8bbd7+edbf708997,g199a45376c+5137f08352,g1fd858c14a+1d4b6db739,g262e1987ae+f4d9505c4f,g29ae962dfc+7156fb1a53,g2cef7863aa+73c82f25e4,g35bb328faa+edbf708997,g3e17d7035e+5b3adc59f5,g3fd5ace14f+852fa6fbcb,g47891489e3+6dc8069a4c,g53246c7159+edbf708997,g64539dfbff+9f17e571f4,g67b6fd64d1+6dc8069a4c,g74acd417e5+ae494d68d9,g786e29fd12+af89c03590,g7ae74a0b1c+a25e60b391,g7aefaa3e3d+536efcc10a,g7cc15d900a+d121454f8d,g87389fa792+a4172ec7da,g89139ef638+6dc8069a4c,g8d7436a09f+28c28d8d6d,g8ea07a8fe4+db21c37724,g92c671f44c+9f17e571f4,g98df359435+b2e6376b13,g99af87f6a8+b0f4ad7b8d,gac66b60396+966efe6077,gb88ae4c679+7dec8f19df,gbaa8f7a6c5+38b34f4976,gbf99507273+edbf708997,gc24b5d6ed1+9f17e571f4,gca7fc764a6+6dc8069a4c,gcc769fe2a4+97d0256649,gd7ef33dd92+6dc8069a4c,gdab6d2f7ff+ae494d68d9,gdbb4c4dda9+9f17e571f4,ge410e46f29+6dc8069a4c,geaed405ab2+e194be0d2b,w.2025.47
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 ..bbox import Box
10from .blend_base import ScarletBlendBaseData
11from .migration import PRE_SCHEMA, MigrationRegistry, migration
12from .utils import PersistenceError, decode_metadata, encode_metadata
13
14__all__ = ["HierarchicalBlendData"]
15
16CURRENT_SCHEMA = "1.0.0"
17BLEND_TYPE = "hierarchical"
18MigrationRegistry.set_current(BLEND_TYPE, CURRENT_SCHEMA)
19
20
21@dataclass(kw_only=True)
23 """Data for a hierarchical blend.
24
25 Attributes
26 ----------
27 blend_type :
28 The type of blend being stored
29 children :
30 Map from blend IDs to child blends.
31 version :
32 The schema version of the HierarchicalBlendData.
33 """
34
35 blend_type: str = BLEND_TYPE
36 children: dict[int, ScarletBlendBaseData]
37 version: str = CURRENT_SCHEMA
38
39 @property
40 def bbox(self) -> Box:
41 """The bounding box of the blend"""
42 # Compute the bounding box that contains all children
43 if not self.children:
44 raise ValueError("HierarchicalBlendData has no children to compute bbox from.")
45 bboxes = [child.bbox for child in self.children.values()]
46 min_y = min(bbox.origin[0] for bbox in bboxes)
47 min_x = min(bbox.origin[1] for bbox in bboxes)
48 max_y = max(bbox.origin[0] + bbox.shape[0] for bbox in bboxes)
49 max_x = max(bbox.origin[1] + bbox.shape[1] for bbox in bboxes)
50 origin = (min_y, min_x)
51 shape = (max_y - min_y, max_x - min_x)
52 return Box(shape, origin=origin)
53
54 def as_dict(self) -> dict:
55 """Return the object encoded into a dict for JSON serialization
56
57 Returns
58 -------
59 result :
60 The object encoded as a JSON compatible dict
61 """
62 result: dict[str, Any] = {
63 "blend_type": self.blend_type,
64 "children": {bid: child.as_dict() for bid, child in self.children.items()},
65 "version": self.version,
66 }
67 if self.metadata is not None:
68 result["metadata"] = encode_metadata(self.metadata)
69 return result
70
71 @classmethod
72 def from_dict(cls, data: dict, dtype: DTypeLike = np.float32) -> HierarchicalBlendData:
73 """Reconstruct `HierarchicalBlendData` from JSON compatible dict.
74
75 Parameters
76 ----------
77 data :
78 Dictionary representation of the object
79 dtype :
80 Datatype of the resulting model.
81
82 Returns
83 -------
84 result :
85 The reconstructed object
86 """
87 data = MigrationRegistry.migrate(BLEND_TYPE, data)
88 children: dict[int, ScarletBlendBaseData] = {}
89 for blend_id, child in data["children"].items():
90 try:
91 children[int(blend_id)] = ScarletBlendBaseData.from_dict(child, dtype=dtype)
92 except KeyError:
93 raise PersistenceError(f"Unknown blend type: {child['blend_type']} for blend ID: {blend_id}")
94
95 metadata = decode_metadata(data.get("metadata", None))
96 return cls(children=children, metadata=metadata)
97
98
99HierarchicalBlendData.register()
100# Register the legacy blend_type in the blend registry
101ScarletBlendBaseData.blend_registry["hierarchical_blend"] = HierarchicalBlendData
102
103
104@migration(BLEND_TYPE, PRE_SCHEMA)
105def _to_1_0_0(data: dict) -> dict:
106 """Migrate a pre-schema hierarchical blend to schema version 1.0.0
107
108 There were no changes to this data model in v1.0.0 but we need
109 to provide a way to migrate pre-schema data.
110
111 Parameters
112 ----------
113 data :
114 The data to migrate.
115
116 Returns
117 -------
118 result :
119 The migrated data.
120 """
121 data["version"] = "1.0.0"
122 return data
HierarchicalBlendData from_dict(cls, dict data, DTypeLike dtype=np.float32)