LSST Applications 30.0.7,g0e76e35be5+e8e946ae08,g19811a7679+138f7293ba,g199a45376c+5e234f8357,g1fd858c14a+2f48dbc4c4,g262e1987ae+fb36cac54d,g29ae962dfc+d9108a0941,g2c21b0017a+4f59a27f16,g31e44d4a5c+b0138be388,g33ac35c1f1+28b9f72785,g35bb328faa+b0138be388,g40c9b15c53+823ad735c1,g47891489e3+bcc48a0b46,g53246c7159+b0138be388,g64539dfbff+e8e946ae08,g67b6fd64d1+bcc48a0b46,g74acd417e5+422380537a,g76965917b2+a5ca99c4d9,g786e29fd12+796b79145d,g7aefaa3e3d+dc0c200193,g86b635cae8+734fe384f0,g87389fa792+d8b5378923,g89139ef638+bcc48a0b46,g8bbb235e95+3f4f7f9447,g8ea07a8fe4+78a4c88802,g9290983e33+ffdc83c6f7,g92c671f44c+e8e946ae08,gaa753fd333+03f406da14,gbf99507273+b0138be388,gc49b57b85e+8df26ee1f0,gca7fc764a6+bcc48a0b46,gd7ef33dd92+bcc48a0b46,gdab6d2f7ff+422380537a,ge1c02a5578+b0138be388,ge410e46f29+bcc48a0b46,ge80df9fc40+e6db5413d1,geaed405ab2+1de65a85c6,gf5dcc679e7+35a0ce2edd,gf5f1c85443+e8e946ae08
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)