LSST Applications g013ef56533+63812263fb,g083dd6704c+a047e97985,g199a45376c+0ba108daf9,g1fd858c14a+fde7a7a78c,g210f2d0738+db0c280453,g262e1987ae+abed931625,g29ae962dfc+058d1915d8,g2cef7863aa+aef1011c0b,g35bb328faa+8c5ae1fdc5,g3fd5ace14f+64337f1634,g47891489e3+f459a6810c,g53246c7159+8c5ae1fdc5,g54cd7ddccb+890c8e1e5d,g5a60e81ecd+d9e514a434,g64539dfbff+db0c280453,g67b6fd64d1+f459a6810c,g6ebf1fc0d4+8c5ae1fdc5,g7382096ae9+36d16ea71a,g74acd417e5+c70e70fbf6,g786e29fd12+668abc6043,g87389fa792+8856018cbb,g89139ef638+f459a6810c,g8d7436a09f+1b779678e3,g8ea07a8fe4+81eaaadc04,g90f42f885a+34c0557caf,g97be763408+9583a964dd,g98a1a72a9c+028271c396,g98df359435+530b675b85,gb8cb2b794d+4e54f68785,gbf99507273+8c5ae1fdc5,gc2a301910b+db0c280453,gca7fc764a6+f459a6810c,gd7ef33dd92+f459a6810c,gdab6d2f7ff+c70e70fbf6,ge410e46f29+f459a6810c,ge41e95a9f2+db0c280453,geaed405ab2+e3b4b2a692,gf9a733ac38+8c5ae1fdc5,w.2025.43
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