#!/usr/bin/env python3 """ Select 80 representative trajectories from 500-sample VLA dataset. Ensures balanced coverage across all robot types, action types, risk levels, and scene categories. Selection target: 80 samples (was 34, user requested 50-100) """ import json import random import os def main(): source_file = "D:/数据引擎/500en.txt" print(f"Loading source: {source_file}") with open(source_file, 'r', encoding='utf-8') as f: data = json.load(f) samples = data["samples"] metadata = data["metadata"] # Index by various dimensions by_robot = {} by_action = {} by_risk = {} by_scene = {} for s in samples: for dim, store in [("robot_type", by_robot), ("action_type", by_action), ("risk_level", by_risk), ("scene_category", by_scene)]: key = s.get(dim, "unknown") store.setdefault(key, []).append(s) print(f"Total: {len(samples)} samples") print(f"Robot types ({len(by_robot)}): {dict((k, len(v)) for k,v in by_robot.items())}") print(f"Action types ({len(by_action)}): {dict((k, len(v)) for k,v in by_action.items())}") print(f"Risk levels ({len(by_risk)}): {dict((k, len(v)) for k,v in by_risk.items())}") print(f"Scenes ({len(by_scene)}): {dict((k, len(v)) for k,v in by_scene.items())}") random.seed(42) selected = [] seen = set() def add_sample(s): if s["sample_id"] not in seen: selected.append(s) seen.add(s["sample_id"]) return True return False def available_pool(pool): return [s for s in pool if s["sample_id"] not in seen] # === Round 1: 3 from each action type (16 x 3 = 48) === print("\n--- Round 1: 3 per action type ---") for at in sorted(by_action.keys()): pool = available_pool(by_action[at]) # Sort by confidence to pick low/mid/high variety pool_sorted = sorted(pool, key=lambda x: x.get("confidence_level", 0.5)) n = min(3, len(pool_sorted)) if n >= 3: # Pick low, mid, high confidence picks = [pool_sorted[0], pool_sorted[len(pool_sorted)//2], pool_sorted[-1]] else: picks = pool_sorted[:n] for p in picks: add_sample(p) print(f" After R1: {len(selected)} samples") # === Round 2: Ensure each robot type has at least 15 (4 x 15 = 60) === print("--- Round 2: 15 per robot type ---") for rt in sorted(by_robot.keys()): count = sum(1 for s in selected if s["robot_type"] == rt) needed = max(0, 15 - count) pool = available_pool(by_robot[rt]) random.shuffle(pool) for s in pool[:needed]: add_sample(s) print(f" After R2: {len(selected)} samples") # === Round 3: Ensure each risk level has at least 12 (4 x 12 = 48) === print("--- Round 3: 12 per risk level ---") for rl in sorted(by_risk.keys()): count = sum(1 for s in selected if s.get("risk_level") == rl) needed = max(0, 12 - count) pool = available_pool(by_risk[rl]) random.shuffle(pool) for s in pool[:needed]: add_sample(s) print(f" After R3: {len(selected)} samples") # === Round 4: Ensure each scene category has at least 15 === print("--- Round 4: 15 per scene category ---") for sc in sorted(by_scene.keys()): count = sum(1 for s in selected if s.get("scene_category") == sc) needed = max(0, 15 - count) pool = available_pool(by_scene[sc]) random.shuffle(pool) for s in pool[:needed]: add_sample(s) print(f" After R4: {len(selected)} samples") # === Round 5: Ensure each (robot_type x risk_level) combo has at least 3 === print("--- Round 5: 3 per robot x risk combo ---") for rt in sorted(by_robot.keys()): for rl in sorted(by_risk.keys()): pool = available_pool([s for s in by_robot[rt] if s.get("risk_level") == rl]) count = sum(1 for s in selected if s["robot_type"] == rt and s.get("risk_level") == rl) needed = max(0, 3 - count) random.shuffle(pool) for s in pool[:needed]: add_sample(s) print(f" After R5: {len(selected)} samples") # === Round 6: Ensure each (action_type x robot_type) combo has at least 1 === print("--- Round 6: 1 per action x robot combo ---") for at in sorted(by_action.keys()): for rt in sorted(by_robot.keys()): pool = available_pool([s for s in by_action[at] if s["robot_type"] == rt]) count = sum(1 for s in selected if s.get("action_type") == at and s["robot_type"] == rt) if count == 0 and pool: add_sample(random.choice(pool)) print(f" After R6: {len(selected)} samples") # === Round 7: Ensure 3-step and 4-step trajectories are balanced === print("--- Round 7: Balance step counts ---") for n_steps in [3, 4]: count = sum(1 for s in selected if len(s.get("trajectory", [])) == n_steps) target = 35 # ~35 of each for 70 total, rest can be either needed = max(0, target - count) pool = available_pool([s for s in samples if len(s.get("trajectory", [])) == n_steps]) random.shuffle(pool) for s in pool[:needed]: add_sample(s) print(f" After R7: {len(selected)} samples") # === Round 8: Fill to 80 with diverse samples === print("--- Round 8: Fill to 80 ---") if len(selected) < 80: remaining = available_pool(samples) # Prioritize samples from under-represented action types action_counts = {} for s in selected: at = s.get("action_type", "?") action_counts[at] = action_counts.get(at, 0) + 1 remaining.sort(key=lambda s: action_counts.get(s.get("action_type", "?"), 0)) for s in remaining: if len(selected) >= 80: break add_sample(s) print(f" After R8: {len(selected)} samples") # Cap at 80 if len(selected) > 80: selected = selected[:80] # Sort by sample_id for reproducibility selected.sort(key=lambda s: s["sample_id"]) # Report coverage print(f"\n{'='*60}") print(f"Selected: {len(selected)} samples") print(f"{'='*60}") dim_map = {"Robot types": "robot_type", "Action types": "action_type", "Risk levels": "risk_level", "Scenes": "scene_category"} for dim_name, field in dim_map.items(): store = {"robot_type": by_robot, "action_type": by_action, "risk_level": by_risk, "scene_category": by_scene}[field] cov = {} for k in store: n = sum(1 for s in selected if s.get(field) == k) cov[k] = n print(f"\n{dim_name}: {cov}") # Step count distribution steps_cov = {} for s in selected: n = len(s.get("trajectory", [])) steps_cov[n] = steps_cov.get(n, 0) + 1 print(f"\nStep counts: {dict(sorted(steps_cov.items()))}") # Build output output = { "metadata": { **metadata, "dataset_info": { "name": "VLA Representative Trajectories - ISO Safety Benchmark", "version": "v2.0", "source": "VLA Data Generation Framework v3.1 (500 samples)", "selected_count": len(selected), "selection_criteria": [ "All 4 robot types covered (>= 15 each)", "All 16 action types covered (>= 3 each)", "All 4 risk levels covered (>= 12 each)", "All 4 scene categories covered (>= 15 each)", "All robot x risk combinations covered (>= 3 each)", "All action x robot combinations covered (>= 1 each)", "Balanced 3-step and 4-step trajectory counts", "Varies in confidence levels (low/mid/high per action type)", "Includes diverse force/velocity profiles for benchmarking" ], "intended_use": "ISO 10218 / ISO/TS 15066 safety compliance benchmarking", "license": "MIT", "citation": "If using this dataset in research, please cite the source repository." } }, "samples": selected } output_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "representative_trajectories.json") with open(output_path, 'w', encoding='utf-8') as f: json.dump(output, f, ensure_ascii=False, indent=2) print(f"\nSaved: {output_path}") print(f"File size: {os.path.getsize(output_path) / 1024:.1f} KB") if __name__ == "__main__": main()