text
stringlengths
0
1.47k
if data is not None:
meta[key] = data
return meta
def check_metadata_object_types_consistent(
metadata,
keys=('clean_joint_names', 'face_joint_names', 'joint_names'),
):
# type: (dict, tuple) -> None
"""Verify per-object-type metadata dicts cover the same object types.
Compares the case-insensitive key sets of each present dict named by
*keys* in *metadata*; absent or empty dicts are skipped. Raises
``ValueError`` on mismatch so drivers fail loudly on misaligned JSON
sources, and prints a consistency-OK line when all present dicts agree.
"""
present = {n: {k.lower() for k in metadata[n].keys()}
for n in keys if metadata.get(n)}
if len(present) < 2:
return
ref_name, ref_set = next(iter(present.items()))
for other_name, other_set in present.items():
if other_name == ref_name:
continue
only_ref = sorted(ref_set - other_set)
only_other = sorted(other_set - ref_set)
if only_ref or only_other:
raise ValueError(
f"Object type mismatch between '{ref_name}' and '{other_name}': "
f"only in {ref_name}: {only_ref}; "
f"only in {other_name}: {only_other}"
)
print(f'Metadata consistency OK across {list(present.keys())}: '
f'{len(ref_set)} object types')
def _dict_get_ci(d, key):
# type: (dict, str) -> object
"""Case-insensitive dict lookup. Tries exact match first, then lowercase."""
if key in d:
return d[key]
key_lower = key.lower()
for k, v in d.items():
if k.lower() == key_lower:
return v
return None
def get_object_metadata(object_type, metadata_dict):
# type: (str, Optional[dict]) -> object
"""Case-insensitive lookup of *object_type* in a metadata dict, or None."""
if metadata_dict:
return _dict_get_ci(metadata_dict, object_type)
return None
def get_object_face_joints(object_type, face_joint_names=None):
# type: (str, Optional[Dict[str, dict]]) -> Optional[dict]
"""Return face joint info for *object_type*, or None."""
return get_object_metadata(object_type, face_joint_names)
def get_object_captions(object_type, motion_captions=None):
# type: (str, Optional[Dict[str, str]]) -> Dict[str, str]
"""Filter motion captions to entries for *object_type*.
Caption keys follow the ``{object_type}-{motion_name}`` base-name
convention used throughout the pipeline; matching is case-insensitive
on the prefix. Returns an empty dict when no captions are supplied or
none match.
"""
if not motion_captions:
return {}
prefix = f'{object_type.lower()}-'
return {k: v for k, v in motion_captions.items()
if k.lower().startswith(prefix)}
def save_json(path, data):
"""Write *data* as indented JSON to *path*, atomically."""
with atomic_output_path(path) as tmp_path:
with open(tmp_path, 'w') as f:
json.dump(data, f, indent=2)
def collect_captions(cond):
# type: (Dict[str, dict]) -> Dict[str, str]
"""Merge per-object ``captions`` sub-dicts into a single flat dict."""
all_captions = {}
for obj_cond in cond.values():
if 'captions' in obj_cond:
all_captions.update(obj_cond['captions'])
return all_captions
def _remove_stale(save_dir, name, why):
"""Remove ``<save_dir>/<name>`` left by an earlier run, if any."""
path = pjoin(save_dir, name)
if os.path.isfile(path):