Coverage for python / lsst / scarlet / lite / io / blend_base.py: 78%

30 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-17 08:40 +0000

1from __future__ import annotations 

2 

3from abc import ABC, abstractmethod 

4from dataclasses import dataclass 

5from typing import Any, ClassVar 

6 

7import numpy as np 

8from numpy.typing import DTypeLike 

9 

10from ..bbox import Box 

11from .utils import PersistenceError 

12 

13__all__ = ["ScarletBlendBaseData"] 

14 

15 

16@dataclass(kw_only=True) 

17class ScarletBlendBaseData(ABC): 

18 """Base data for a scarlet Blend data. 

19 

20 Attributes 

21 ---------- 

22 blend_registry : 

23 A registry of all known blend types used for deserialization. 

24 blend_type : 

25 The type of blend being stored. 

26 metadata : 

27 Metadata associated with the blend. 

28 version : 

29 The schema version of the exact data class. 

30 """ 

31 

32 blend_registry: ClassVar[dict[str, type[ScarletBlendBaseData]]] = {} 

33 blend_type: str 

34 metadata: dict[str, Any] | None = None 

35 version: str 

36 

37 @property 

38 @abstractmethod 

39 def bbox(self) -> Box: 

40 """The bounding box of the blend""" 

41 

42 @classmethod 

43 def register(cls) -> None: 

44 """Register a new Blend type""" 

45 ScarletBlendBaseData.blend_registry[cls.blend_type] = cls 

46 

47 @abstractmethod 

48 def as_dict(self) -> dict: 

49 """Return the object encoded into a dict for JSON serialization 

50 

51 Returns 

52 ------- 

53 result : 

54 The object encoded as a JSON compatible dict 

55 """ 

56 

57 @staticmethod 

58 def from_dict(data: dict, dtype: DTypeLike = np.float32) -> ScarletBlendBaseData: 

59 """Reconstruct `ScarletBlendBaseData` from JSON compatible dict. 

60 

61 Parameters 

62 ---------- 

63 data : 

64 Dictionary representation of the object 

65 dtype : 

66 Datatype of the resulting model. 

67 

68 Returns 

69 ------- 

70 result : 

71 The reconstructed object 

72 """ 

73 # Default to "blend" for backwards compatibility 

74 blend_type = data.get("blend_type", "blend") 

75 

76 if blend_type not in ScarletBlendBaseData.blend_registry: 

77 raise PersistenceError(f"Unknown blend type: {blend_type}") 

78 

79 cls = ScarletBlendBaseData.blend_registry[blend_type] 

80 return cls.from_dict(data, dtype=dtype)