File size: 12,893 Bytes
571a68d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
"""
Full skeleton audit: naming + structural features for all 79 skeletons.

Checks:
1. Canonical name quality (empty, duplicate, semantic sense)
2. Side tag correctness (L/R balance, missed sides)
3. Symmetry pair validity (matched pairs have similar bone lengths/depths)
4. Topology structure (tree depth, branching factor, connected)
5. Geodesic distance matrix sanity
6. Rest pose geometry (bone lengths, height, spread)
7. Cross-dataset consistency for shared canonical names
"""

import sys
import os
from pathlib import Path
import numpy as np
from collections import Counter, defaultdict

project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))

from src.data.skeleton_graph import SkeletonGraph

RESULT_DIR = project_root / 'logs' / 'data_fix_20260318'

# Severity levels
ERRORS = []    # Must fix
WARNINGS = []  # Should review
INFO = []      # FYI


def log_error(dataset, msg):
    ERRORS.append(f'[ERROR] {dataset}: {msg}')

def log_warn(dataset, msg):
    WARNINGS.append(f'[WARN]  {dataset}: {msg}')

def log_info(dataset, msg):
    INFO.append(f'[INFO]  {dataset}: {msg}')


def audit_skeleton(dataset_id: str, skel_data: dict, motion_sample: dict = None):
    """Full audit of one skeleton."""
    sg = SkeletonGraph.from_dict(skel_data)
    J = sg.num_joints
    canon = [str(n) for n in skel_data.get('canonical_names', [])]
    raw = [str(n) for n in skel_data['joint_names']]

    results = {'dataset': dataset_id, 'num_joints': J, 'issues': []}

    # ===== 1. Canonical Name Quality =====
    if not canon:
        log_error(dataset_id, 'No canonical_names field in skeleton.npz')
        results['issues'].append('no_canonical')
    else:
        # Empty names
        empty = [i for i, c in enumerate(canon) if not c.strip()]
        if empty:
            log_error(dataset_id, f'{len(empty)} empty canonical names at indices {empty[:5]}')

        # Duplicate canonical names (within same skeleton)
        dupes = {c: [i for i, x in enumerate(canon) if x == c] for c in set(canon)}
        dupes = {c: idxs for c, idxs in dupes.items() if len(idxs) > 1}
        if dupes:
            for c, idxs in list(dupes.items())[:3]:
                raw_names = [raw[i] for i in idxs]
                log_warn(dataset_id, f'Duplicate canonical "{c}" for raw: {raw_names}')

        # Names that are just numbers or single chars
        bad_names = [c for c in canon if len(c) <= 2 and not c.isalpha()]
        if bad_names:
            log_warn(dataset_id, f'Very short canonical names: {bad_names[:5]}')

        # Prefix residue (bip01, bn_, jt_, mixamorig still present)
        prefix_residue = [c for c in canon if any(p in c.lower() for p in ['bip01', 'bn_', 'jt_', 'mixamorig', 'npc_'])]
        if prefix_residue:
            log_warn(dataset_id, f'Prefix residue in canonical: {prefix_residue[:5]}')

    # ===== 2. Side Tag Correctness =====
    n_left = sum(1 for t in sg.side_tags if t == 'left')
    n_right = sum(1 for t in sg.side_tags if t == 'right')
    n_center = sum(1 for t in sg.side_tags if t == 'center')

    if n_left != n_right and J > 5:  # small skeletons may be asymmetric
        log_warn(dataset_id, f'L/R imbalance: L={n_left} R={n_right} C={n_center}')

    # Check if canonical names agree with side tags
    if canon:
        for i, (c, t) in enumerate(zip(canon, sg.side_tags)):
            if 'left' in c and t != 'left':
                log_warn(dataset_id, f'Joint {i} "{raw[i]}" canonical="{c}" but side_tag="{t}"')
            elif 'right' in c and t != 'right':
                log_warn(dataset_id, f'Joint {i} "{raw[i]}" canonical="{c}" but side_tag="{t}"')

    # ===== 3. Symmetry Pair Validation =====
    for i, j in sg.symmetry_pairs:
        # Check bone lengths match (should be ~equal for symmetric joints)
        bl_i = sg.bone_lengths[i]
        bl_j = sg.bone_lengths[j]
        if bl_i > 0.01 and bl_j > 0.01:
            ratio = max(bl_i, bl_j) / min(bl_i, bl_j)
            if ratio > 1.5:
                log_warn(dataset_id, f'Sym pair ({raw[i]}, {raw[j]}) bone length mismatch: {bl_i:.3f} vs {bl_j:.3f}')

        # Check depths match
        if sg.depths[i] != sg.depths[j]:
            log_warn(dataset_id, f'Sym pair ({raw[i]}, {raw[j]}) depth mismatch: {sg.depths[i]} vs {sg.depths[j]}')

    # ===== 4. Topology Structure =====
    max_depth = sg.depths.max()
    max_degree = sg.degrees.max()
    leaf_count = sum(1 for d in sg.degrees if d == 0)
    root_count = sum(1 for p in sg.parent_indices if p == -1)

    if root_count != 1:
        log_error(dataset_id, f'Expected 1 root, found {root_count}')

    if max_depth > 20:
        log_warn(dataset_id, f'Very deep tree: max_depth={max_depth}')

    # Check connectivity (all joints reachable from root)
    reachable = set()
    queue = [i for i, p in enumerate(sg.parent_indices) if p == -1]
    while queue:
        curr = queue.pop(0)
        reachable.add(curr)
        for j in range(J):
            if sg.parent_indices[j] == curr and j not in reachable:
                queue.append(j)
    if len(reachable) != J:
        log_error(dataset_id, f'Disconnected: only {len(reachable)}/{J} joints reachable from root')

    # ===== 5. Geodesic Distance Sanity =====
    geo = sg.geodesic_dist
    if geo.shape != (J, J):
        log_error(dataset_id, f'Geodesic distance shape mismatch: {geo.shape} vs ({J},{J})')
    else:
        # Should be symmetric
        asym = np.abs(geo - geo.T).max()
        if asym > 0.01:
            log_error(dataset_id, f'Geodesic distance not symmetric: max asymmetry={asym}')

        # Diagonal should be 0
        diag_max = np.diag(geo).max()
        if diag_max > 0.01:
            log_error(dataset_id, f'Geodesic distance diagonal non-zero: max={diag_max}')

        # Max distance should be reasonable
        max_geo = geo.max()
        if max_geo > J:
            log_warn(dataset_id, f'Geodesic max={max_geo} exceeds num_joints={J}')

        # No unreachable pairs (distance should be < J+1)
        unreachable = (geo >= J + 1).sum()
        if unreachable > 0:
            log_error(dataset_id, f'{unreachable} unreachable joint pairs in geodesic matrix')

    # ===== 6. Rest Pose Geometry =====
    offsets = sg.rest_offsets
    bone_lengths = sg.bone_lengths

    # Zero-length bones (excluding root)
    zero_bones = sum(1 for i, bl in enumerate(bone_lengths) if bl < 1e-6 and sg.parent_indices[i] >= 0)
    if zero_bones > 0:
        log_info(dataset_id, f'{zero_bones} zero-length bones')

    # Very long bones (> 10x median)
    nonzero_bl = bone_lengths[bone_lengths > 1e-6]
    if len(nonzero_bl) > 0:
        median_bl = np.median(nonzero_bl)
        long_bones = [(raw[i], bl) for i, bl in enumerate(bone_lengths)
                      if bl > 10 * median_bl and sg.parent_indices[i] >= 0]
        if long_bones:
            log_warn(dataset_id, f'Unusually long bones (>10x median={median_bl:.4f}): {long_bones[:3]}')

    # ===== 7. Motion Data Spot Check =====
    if motion_sample is not None:
        T = int(motion_sample['num_frames'])
        jp = motion_sample['joint_positions'][:T]
        lp = motion_sample['local_positions'][:T]
        vel = motion_sample['velocities'][:T]

        # Check joint count matches
        if jp.shape[1] != J:
            log_error(dataset_id, f'Motion joints={jp.shape[1]} != skeleton joints={J}')

        # Check for NaN/Inf
        for key in ['joint_positions', 'local_positions', 'velocities']:
            arr = motion_sample[key][:T]
            if np.any(np.isnan(arr)):
                log_error(dataset_id, f'NaN in motion {key}')
            if np.any(np.isinf(arr)):
                log_error(dataset_id, f'Inf in motion {key}')

        # Height sanity
        height = jp[0, :, 1].max() - jp[0, :, 1].min()
        if height < 0.05:
            log_warn(dataset_id, f'Very small body height: {height:.4f}m')
        elif height > 10:
            log_warn(dataset_id, f'Very large body height: {height:.2f}m (scale issue?)')

        # Velocity sanity
        vel_max = np.linalg.norm(vel, axis=-1).max()
        if vel_max > 50:
            log_warn(dataset_id, f'Very high velocity: max={vel_max:.1f} m/s')

    # Collect summary
    results['n_left'] = n_left
    results['n_right'] = n_right
    results['n_center'] = n_center
    results['n_sym'] = len(sg.symmetry_pairs)
    results['max_depth'] = int(max_depth)
    results['max_degree'] = int(max_degree)
    results['leaf_count'] = leaf_count
    results['n_canonical'] = len(canon)
    results['n_dupe_canonical'] = sum(len(v) - 1 for v in dupes.values()) if canon and dupes else 0

    return results


def main():
    all_results = []

    # Audit human datasets
    human_datasets = ['humanml3d', 'lafan1', '100style', 'bandai_namco', 'cmu_mocap', 'mixamo']
    for ds in human_datasets:
        ds_path = project_root / 'data' / 'processed' / ds
        skel_data = dict(np.load(ds_path / 'skeleton.npz', allow_pickle=True))

        # Load a motion sample
        motions_dir = ds_path / 'motions'
        files = sorted(os.listdir(motions_dir))
        motion_sample = dict(np.load(motions_dir / files[0], allow_pickle=True)) if files else None

        r = audit_skeleton(ds, skel_data, motion_sample)
        all_results.append(r)

    # Audit Zoo species
    zoo_path = project_root / 'data' / 'processed' / 'truebones_zoo'
    skel_dir = zoo_path / 'skeletons'
    motions_dir = zoo_path / 'motions'

    # Build species → motion mapping
    species_motions = {}
    for f in sorted(os.listdir(motions_dir))[:200]:
        d = dict(np.load(motions_dir / f, allow_pickle=True))
        sp = str(d.get('species', ''))
        if sp and sp not in species_motions:
            species_motions[sp] = d

    for skel_file in sorted(skel_dir.glob('*.npz')):
        species = skel_file.stem
        skel_data = dict(np.load(skel_file, allow_pickle=True))
        motion_sample = species_motions.get(species)
        r = audit_skeleton(f'zoo/{species}', skel_data, motion_sample)
        all_results.append(r)

    # ===== Cross-dataset canonical consistency =====
    # For human datasets, check that same canonical name → similar tree depth
    canonical_depths = defaultdict(list)
    for ds in human_datasets:
        skel_data = dict(np.load(project_root / 'data' / 'processed' / ds / 'skeleton.npz', allow_pickle=True))
        sg = SkeletonGraph.from_dict(skel_data)
        canon = [str(n) for n in skel_data.get('canonical_names', [])]
        for c, d in zip(canon, sg.depths):
            canonical_depths[c].append((ds, int(d)))

    for c, entries in canonical_depths.items():
        depths = [d for _, d in entries]
        if len(set(depths)) > 1 and max(depths) - min(depths) > 2:
            sources = [(ds, d) for ds, d in entries]
            log_warn('cross-dataset', f'Canonical "{c}" has depth variance: {sources}')

    # ===== Write Report =====
    report_path = RESULT_DIR / 'skeleton_audit_report.md'
    with open(report_path, 'w') as f:
        f.write('# Skeleton Audit Report\n\n')
        f.write(f'**Date**: 2026-03-18\n')
        f.write(f'**Datasets**: {len(human_datasets)} human + {len(list(skel_dir.glob("*.npz")))} zoo species\n\n')

        # Summary table
        f.write('## Summary\n\n')
        f.write(f'| Dataset | J | L | R | C | Sym | Depth | Leaves | Canon | Dupes |\n')
        f.write(f'|---------|:-:|:-:|:-:|:-:|:---:|:-----:|:------:|:-----:|:-----:|\n')
        for r in all_results:
            f.write(f'| {r["dataset"]:20s} | {r["num_joints"]:3d} | {r["n_left"]:2d} | {r["n_right"]:2d} | '
                    f'{r["n_center"]:2d} | {r["n_sym"]:2d} | {r["max_depth"]:2d} | {r["leaf_count"]:2d} | '
                    f'{r["n_canonical"]:3d} | {r["n_dupe_canonical"]:2d} |\n')

        # Issues
        f.write(f'\n## Errors ({len(ERRORS)})\n\n')
        if ERRORS:
            for e in ERRORS:
                f.write(f'- {e}\n')
        else:
            f.write('None.\n')

        f.write(f'\n## Warnings ({len(WARNINGS)})\n\n')
        if WARNINGS:
            for w in WARNINGS:
                f.write(f'- {w}\n')
        else:
            f.write('None.\n')

        f.write(f'\n## Info ({len(INFO)})\n\n')
        if INFO:
            for i in INFO:
                f.write(f'- {i}\n')
        else:
            f.write('None.\n')

    print(f'Audit complete: {len(ERRORS)} errors, {len(WARNINGS)} warnings, {len(INFO)} info')
    print(f'Report: {report_path}')

    # Print to console too
    if ERRORS:
        print(f'\n=== ERRORS ({len(ERRORS)}) ===')
        for e in ERRORS:
            print(f'  {e}')
    if WARNINGS:
        print(f'\n=== WARNINGS ({len(WARNINGS)}) ===')
        for w in WARNINGS[:30]:
            print(f'  {w}')
        if len(WARNINGS) > 30:
            print(f'  ... and {len(WARNINGS) - 30} more')


if __name__ == '__main__':
    main()