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
lsst.scarlet.lite.io.utils Namespace Reference

Classes

class  PersistenceError
 

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)
 

Function Documentation

◆ decode_metadata()

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

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

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

Definition at line 98 of file utils.py.

98def decode_metadata(metadata: dict[str, Any] | None) -> dict[str, Any] | None:
99 """Unpack metadata from a JSON compatible format.
100
101 Parameters
102 ----------
103 metadata :
104 The metadata to be unpacked.
105
106 Returns
107 -------
108 result :
109 The unpacked metadata.
110 """
111 if metadata is None:
112 return None
113 if "array_keys" in metadata:
114 for key in metadata["array_keys"]:
115 # Default dtype is float32 to support legacy models
116 dtype = metadata.pop(f"{key}_dtype", "float32")
117 shape = metadata.pop(f"{key}_shape", None)
118 if shape is None and f"{key}Shape" in metadata:
119 # Support legacy models that use `keyShape`
120 shape = metadata[f"{key}Shape"]
121 decoded = json_to_numpy({"dtype": dtype, "shape": shape, "data": metadata[key]})
122 metadata[key] = decoded
123 # Remove the array keys after decoding
124 del metadata["array_keys"]
125 return metadata
126
127

◆ encode_metadata()

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

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

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

Definition at line 67 of file utils.py.

67def encode_metadata(metadata: dict[str, Any] | None) -> dict[str, Any] | None:
68 """Pack metadata into a JSON compatible format.
69
70 Parameters
71 ----------
72 metadata :
73 The metadata to be packed.
74
75 Returns
76 -------
77 result :
78 The packed metadata.
79 """
80 if metadata is None:
81 return None
82 encoded = {}
83 array_keys = []
84 for key, value in metadata.items():
85 if isinstance(value, np.ndarray):
86 _encoded = numpy_to_json(value)
87 encoded[key] = _encoded["data"]
88 encoded[f"{key}_shape"] = _encoded["shape"]
89 encoded[f"{key}_dtype"] = _encoded["dtype"]
90 array_keys.append(key)
91 else:
92 encoded[key] = value
93 if len(array_keys) > 0:
94 encoded["array_keys"] = array_keys
95 return encoded
96
97

◆ extract_from_metadata()

Any lsst.scarlet.lite.io.utils.extract_from_metadata ( Any data,
dict[str, Any] | None metadata,
str key )
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 128 of file utils.py.

132) -> Any:
133 """Extract relevant information from the metadata.
134
135 Parameters
136 ----------
137 data :
138 The data to extract information from.
139 metadata :
140 The metadata to extract information from.
141 key :
142 The key to extract from the metadata.
143
144 Returns
145 -------
146 result :
147 A tuple containing the extracted data and metadata.
148 """
149 if data is not None:
150 return data
151 if metadata is None:
152 raise ValueError("Both data and metadata cannot be None")
153 if key not in metadata:
154 raise ValueError(f"'{key}' not found in metadata")
155 return metadata[key]

◆ json_to_numpy()

np.ndarray lsst.scarlet.lite.io.utils.json_to_numpy ( dict[str, Any] encoded_dict)
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 48 of file utils.py.

48def json_to_numpy(encoded_dict: dict[str, Any]) -> np.ndarray:
49 """
50 Decode a JSON dictionary back to a numpy array.
51
52 Parameters
53 ----------
54 encoded_dict :
55 Dictionary with 'dtype', 'shape', and 'data' keys.
56
57 Returns
58 -------
59 result :
60 The reconstructed numpy array.
61 """
62 if "dtype" not in encoded_dict or "shape" not in encoded_dict or "data" not in encoded_dict:
63 raise ValueError("Encoded dictionary must contain 'dtype', 'shape', and 'data' keys.")
64 return np.array(encoded_dict["data"], dtype=encoded_dict["dtype"]).reshape(encoded_dict["shape"])
65
66

◆ numpy_to_json()

dict[str, Any] lsst.scarlet.lite.io.utils.numpy_to_json ( np.ndarray arr)
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 14 of file utils.py.

14def numpy_to_json(arr: np.ndarray) -> dict[str, Any]:
15 """
16 Encode a numpy array as JSON-serializable dictionary.
17
18 Parameters
19 ----------
20 arr :
21 The numpy array to encode
22
23 Returns
24 -------
25 result :
26 A JSON formatted dictionary containing the dtype, shape,
27 and data of the array.
28 """
29 # Convert to native Python types for JSON serialization
30 flattened = arr.flatten()
31
32 # Convert numpy scalars to native Python types
33 if np.issubdtype(arr.dtype, np.integer):
34 data: list = [int(x) for x in flattened]
35 elif np.issubdtype(arr.dtype, np.floating):
36 data = [float(x) for x in flattened]
37 elif np.issubdtype(arr.dtype, np.complexfloating):
38 data = [complex(x) for x in flattened]
39 elif np.issubdtype(arr.dtype, np.bool_):
40 data = [bool(x) for x in flattened]
41 else:
42 # For other types (strings, objects, etc.), convert to string
43 data = [str(x) for x in flattened]
44
45 return {"dtype": str(arr.dtype), "shape": tuple(arr.shape), "data": data}
46
47