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
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