LSST Applications g04e9c324dd+8c5ae1fdc5,g0644efc3f0+366663dc37,g123d84c11c+8c5ae1fdc5,g1ec0fe41b4+6ec6b74de1,g1fd858c14a+1be88e80db,g3533f9d6cb+366663dc37,g35bb328faa+8c5ae1fdc5,g35ef7ab7cf+285dd5b202,g53246c7159+8c5ae1fdc5,g60b5630c4e+366663dc37,g663da51e9b+41529343ca,g6735e52a0d+29de3d959a,g67b6fd64d1+57193d00fb,g7605de067c+8f72e4d2dc,g78460c75b0+7e33a9eb6d,g786e29fd12+668abc6043,g844c57033c+03ddc13274,g8852436030+e345a59dd4,g89139ef638+57193d00fb,g989de1cb63+57193d00fb,g9a0bdda227+852181cf57,g9f33ca652e+a2d35689ce,ga1e959baac+5fbc491aed,ga2f891cd6c+366663dc37,gabe3b4be73+8856018cbb,gabf8522325+cc757f8247,gac2eed3f23+57193d00fb,gb1101e3267+f6b489998a,gb89ab40317+57193d00fb,gcf25f946ba+e345a59dd4,gd107969129+227687db21,gd6cbbdb0b4+8e46defd2a,gde0f65d7ad+2dad650f79,ge278dab8ac+2322f1d6ea,ge410e46f29+57193d00fb,gf30d85a44d+8e3077faf9,gf5e32f922b+8c5ae1fdc5,gff02db199a+5c78c1866e,w.2025.28
LSST Data Management Base Package
Loading...
Searching...
No Matches
lsst.scarlet.lite.io Namespace Reference

Classes

class  ComponentCube
 
class  HierarchicalBlendData
 
class  ScarletBlendData
 
class  ScarletComponentBaseData
 
class  ScarletComponentData
 
class  ScarletFactorizedComponentData
 
class  ScarletModelData
 
class  ScarletSourceData
 

Functions

dict[str, Any] _numpy_to_json (np.ndarray arr)
 
np.ndarray _json_to_numpy (dict[str, Any] encoded_dict)
 
dict[str, Any]|None _encode_metadata (dict[str, Any]|None metadata)
 
dict[str, Any]|None _decode_metadata (dict[str, Any]|None metadata)
 
Any _extract_from_metadata (Any data, dict[str, Any]|None metadata, str key)
 

Variables

 logger = logging.getLogger(__name__)
 

Function Documentation

◆ _decode_metadata()

dict[str, Any] | None lsst.scarlet.lite.io._decode_metadata ( dict[str, Any] | None metadata)
protected
Unpack metadata from a JSON compatible format.

Parameters
----------
metadata :
    The metadata to be unpacked.

Returns
-------
result :
    The unpacked metadata.

Definition at line 116 of file io.py.

116def _decode_metadata(metadata: dict[str, Any] | None) -> dict[str, Any] | None:
117 """Unpack metadata from a JSON compatible format.
118
119 Parameters
120 ----------
121 metadata :
122 The metadata to be unpacked.
123
124 Returns
125 -------
126 result :
127 The unpacked metadata.
128 """
129 if metadata is None:
130 return None
131 if "array_keys" in metadata:
132 for key in metadata["array_keys"]:
133 # Default dtype is float32 to support legacy models
134 dtype = metadata.pop(f"{key}_dtype", "float32")
135 shape = metadata.pop(f"{key}_shape", None)
136 if shape is None and f"{key}Shape" in metadata:
137 # Support legacy models that use `keyShape`
138 shape = metadata[f"{key}Shape"]
139 decoded = _json_to_numpy({"dtype": dtype, "shape": shape, "data": metadata[key]})
140 metadata[key] = decoded
141 # Remove the array keys after decoding
142 del metadata["array_keys"]
143 return metadata
144
145

◆ _encode_metadata()

dict[str, Any] | None lsst.scarlet.lite.io._encode_metadata ( dict[str, Any] | None metadata)
protected
Pack metadata into a JSON compatible format.

Parameters
----------
metadata :
    The metadata to be packed.

Returns
-------
result :
    The packed metadata.

Definition at line 85 of file io.py.

85def _encode_metadata(metadata: dict[str, Any] | None) -> dict[str, Any] | None:
86 """Pack metadata into a JSON compatible format.
87
88 Parameters
89 ----------
90 metadata :
91 The metadata to be packed.
92
93 Returns
94 -------
95 result :
96 The packed metadata.
97 """
98 if metadata is None:
99 return None
100 encoded = {}
101 array_keys = []
102 for key, value in metadata.items():
103 if isinstance(value, np.ndarray):
104 _encoded = _numpy_to_json(value)
105 encoded[key] = _encoded["data"]
106 encoded[f"{key}_shape"] = _encoded["shape"]
107 encoded[f"{key}_dtype"] = _encoded["dtype"]
108 array_keys.append(key)
109 else:
110 encoded[key] = value
111 if len(array_keys) > 0:
112 encoded["array_keys"] = array_keys
113 return encoded
114
115

◆ _extract_from_metadata()

Any lsst.scarlet.lite.io._extract_from_metadata ( Any data,
dict[str, Any] | None metadata,
str key )
protected
Extract relevant information from the metadata.

Parameters
----------
data :
    The data to extract information from.
metadata :
    The metadata to extract information from.
key :
    The key to extract from the metadata.

Returns
-------
result :
    A tuple containing the extracted data and metadata.

Definition at line 146 of file io.py.

150) -> Any:
151 """Extract relevant information from the metadata.
152
153 Parameters
154 ----------
155 data :
156 The data to extract information from.
157 metadata :
158 The metadata to extract information from.
159 key :
160 The key to extract from the metadata.
161
162 Returns
163 -------
164 result :
165 A tuple containing the extracted data and metadata.
166 """
167 if data is not None:
168 return data
169 if metadata is None:
170 raise ValueError("Both data and metadata cannot be None")
171 if key not in metadata:
172 raise ValueError(f"'{key}' not found in metadata")
173 return metadata[key]
174
175
176@dataclass(kw_only=True)

◆ _json_to_numpy()

np.ndarray lsst.scarlet.lite.io._json_to_numpy ( dict[str, Any] encoded_dict)
protected
Decode a JSON dictionary back to a numpy array.

Parameters
----------
encoded_dict :
    Dictionary with 'dtype', 'shape', and 'data' keys.

Returns
-------
result :
    The reconstructed numpy array.

Definition at line 66 of file io.py.

66def _json_to_numpy(encoded_dict: dict[str, Any]) -> np.ndarray:
67 """
68 Decode a JSON dictionary back to a numpy array.
69
70 Parameters
71 ----------
72 encoded_dict :
73 Dictionary with 'dtype', 'shape', and 'data' keys.
74
75 Returns
76 -------
77 result :
78 The reconstructed numpy array.
79 """
80 if "dtype" not in encoded_dict or "shape" not in encoded_dict or "data" not in encoded_dict:
81 raise ValueError("Encoded dictionary must contain 'dtype', 'shape', and 'data' keys.")
82 return np.array(encoded_dict["data"], dtype=encoded_dict["dtype"]).reshape(encoded_dict["shape"])
83
84

◆ _numpy_to_json()

dict[str, Any] lsst.scarlet.lite.io._numpy_to_json ( np.ndarray arr)
protected
Encode a numpy array as JSON-serializable dictionary.

Parameters
----------
arr :
    The numpy array to encode

Returns
-------
result :
    A JSON formatted dictionary containing the dtype, shape,
    and data of the array.

Definition at line 32 of file io.py.

32def _numpy_to_json(arr: np.ndarray) -> dict[str, Any]:
33 """
34 Encode a numpy array as JSON-serializable dictionary.
35
36 Parameters
37 ----------
38 arr :
39 The numpy array to encode
40
41 Returns
42 -------
43 result :
44 A JSON formatted dictionary containing the dtype, shape,
45 and data of the array.
46 """
47 # Convert to native Python types for JSON serialization
48 flattened = arr.flatten()
49
50 # Convert numpy scalars to native Python types
51 if np.issubdtype(arr.dtype, np.integer):
52 data: list = [int(x) for x in flattened]
53 elif np.issubdtype(arr.dtype, np.floating):
54 data = [float(x) for x in flattened]
55 elif np.issubdtype(arr.dtype, np.complexfloating):
56 data = [complex(x) for x in flattened]
57 elif np.issubdtype(arr.dtype, np.bool_):
58 data = [bool(x) for x in flattened]
59 else:
60 # For other types (strings, objects, etc.), convert to string
61 data = [str(x) for x in flattened]
62
63 return {"dtype": str(arr.dtype), "shape": tuple(arr.shape), "data": data}
64
65

Variable Documentation

◆ logger

lsst.scarlet.lite.io.logger = logging.getLogger(__name__)

Definition at line 29 of file io.py.