|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
import warnings
|
|
|
from pathlib import Path
|
|
|
from typing import TypeVar
|
|
|
|
|
|
import imageio
|
|
|
|
|
|
JsonLike = str | int | float | bool | None | list["JsonLike"] | dict[str, "JsonLike"] | tuple["JsonLike", ...]
|
|
|
T = TypeVar("T", bound=JsonLike)
|
|
|
|
|
|
|
|
|
def write_video(video_path, stacked_frames, fps):
|
|
|
|
|
|
with warnings.catch_warnings():
|
|
|
warnings.filterwarnings(
|
|
|
"ignore", "pkg_resources is deprecated as an API", category=DeprecationWarning
|
|
|
)
|
|
|
imageio.mimsave(video_path, stacked_frames, fps=fps)
|
|
|
|
|
|
|
|
|
def deserialize_json_into_object(fpath: Path, obj: T) -> T:
|
|
|
"""
|
|
|
Loads the JSON data from `fpath` and recursively fills `obj` with the
|
|
|
corresponding values (strictly matching structure and types).
|
|
|
Tuples in `obj` are expected to be lists in the JSON data, which will be
|
|
|
converted back into tuples.
|
|
|
"""
|
|
|
with open(fpath, encoding="utf-8") as f:
|
|
|
data = json.load(f)
|
|
|
|
|
|
def _deserialize(target, source):
|
|
|
"""
|
|
|
Recursively overwrite the structure in `target` with data from `source`,
|
|
|
performing strict checks on structure and type.
|
|
|
Returns the updated version of `target` (especially important for tuples).
|
|
|
"""
|
|
|
|
|
|
|
|
|
if isinstance(target, dict):
|
|
|
if not isinstance(source, dict):
|
|
|
raise TypeError(f"Type mismatch: expected dict, got {type(source)}")
|
|
|
|
|
|
|
|
|
if target.keys() != source.keys():
|
|
|
raise ValueError(
|
|
|
f"Dictionary keys do not match.\nExpected: {target.keys()}, got: {source.keys()}"
|
|
|
)
|
|
|
|
|
|
|
|
|
for k in target:
|
|
|
target[k] = _deserialize(target[k], source[k])
|
|
|
|
|
|
return target
|
|
|
|
|
|
|
|
|
elif isinstance(target, list):
|
|
|
if not isinstance(source, list):
|
|
|
raise TypeError(f"Type mismatch: expected list, got {type(source)}")
|
|
|
|
|
|
|
|
|
if len(target) != len(source):
|
|
|
raise ValueError(f"List length mismatch: expected {len(target)}, got {len(source)}")
|
|
|
|
|
|
|
|
|
for i in range(len(target)):
|
|
|
target[i] = _deserialize(target[i], source[i])
|
|
|
|
|
|
return target
|
|
|
|
|
|
|
|
|
|
|
|
elif isinstance(target, tuple):
|
|
|
if not isinstance(source, list):
|
|
|
raise TypeError(f"Type mismatch: expected list (for tuple), got {type(source)}")
|
|
|
|
|
|
if len(target) != len(source):
|
|
|
raise ValueError(f"Tuple length mismatch: expected {len(target)}, got {len(source)}")
|
|
|
|
|
|
|
|
|
converted_items = []
|
|
|
for t_item, s_item in zip(target, source, strict=False):
|
|
|
converted_items.append(_deserialize(t_item, s_item))
|
|
|
|
|
|
|
|
|
return tuple(converted_items)
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
if type(target) is not type(source):
|
|
|
raise TypeError(f"Type mismatch: expected {type(target)}, got {type(source)}")
|
|
|
return source
|
|
|
|
|
|
|
|
|
updated_obj = _deserialize(obj, data)
|
|
|
return updated_obj
|
|
|
|