iso-safety-trajectories / examples /check_samples.py
spiralfg's picture
Upload examples/check_samples.py with huggingface_hub
a7f576e verified
Raw
History Blame Contribute Delete
2.96 kB
#!/usr/bin/env python3
"""
Example: Run ISO Safety Checker on the bundled representative trajectories.
Usage:
python examples/check_samples.py
# Or check your own data:
python examples/check_samples.py --input path/to/your_data.json
"""
import json
import sys
import os
# Add parent directory to path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from iso_safety_checker import ISOComplianceChecker
from iso_safety_checker.report import export_json, export_csv
def main():
# Parse args
input_file = None
if len(sys.argv) > 2 and sys.argv[1] == "--input":
input_file = sys.argv[2]
# Load data
if input_file:
print(f"Loading: {input_file}")
with open(input_file, 'r', encoding='utf-8') as f:
data = json.load(f)
else:
# Use bundled representative trajectories
default_path = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
"data", "representative_trajectories.json"
)
print(f"Loading bundled dataset: {default_path}")
with open(default_path, 'r', encoding='utf-8') as f:
data = json.load(f)
# Initialize checker
fleet = data.get("metadata", {}).get("robot_fleet", {})
checker = ISOComplianceChecker(robot_fleet=fleet)
samples = data.get("samples", [])
print(f"Loaded {len(samples)} samples")
print("=" * 60)
# Check each sample
reports = []
for sample in samples:
report = checker.check_sample(sample)
reports.append(report)
# Print summary
status = "PASS" if report.overall_pass else "FAIL"
print(f"[{status}] {report.sample_id} | "
f"{report.robot_type} | {report.action_type} | "
f"Score: {report.compliance_score}/100")
# Show failures
if not report.overall_pass:
for check in report.checks:
if check.severity in ("FAIL", "CRITICAL"):
print(f" -> {check.check_name}: {check.message}")
# Summary
print("\n" + "=" * 60)
print("SUMMARY")
print("=" * 60)
total = len(reports)
passed = sum(1 for r in reports if r.overall_pass)
avg_score = sum(r.compliance_score for r in reports) / total if total else 0
print(f" Total: {total}")
print(f" Passed: {passed}")
print(f" Failed: {total - passed}")
print(f" Pass Rate: {passed/total*100:.1f}%" if total else " Pass Rate: N/A")
print(f" Avg Score: {avg_score:.1f}/100")
# Export reports
output_dir = os.path.dirname(os.path.abspath(__file__))
export_json(reports, os.path.join(output_dir, "compliance_report.json"))
export_csv(reports, os.path.join(output_dir, "compliance_report.csv"))
print("\nDone! Reports saved to examples/compliance_report.{json,csv}")
if __name__ == "__main__":
main()