Files
ats-native-fsd/tools/assess_navigation_recording.py
2026-10-06 18:11:59 +00:00

84 lines
5.0 KiB
Python

"""Summarize paired GPS/model evidence; no controls or game process access."""
import argparse
import json
import math
from pathlib import Path
import statistics
def assess(directory):
directory = Path(directory)
metadata = json.loads((directory/'metadata.json').read_text(encoding='utf-8'))
if (not isinstance(metadata.get('dry_run'), bool) or
not (metadata.get('navigation_preview') or metadata.get('navigation_observe')
or metadata.get('navigation_driving'))):
raise ValueError('Expected navigation recording with an explicit dry_run flag')
passive = metadata['dry_run'] is True
rows = [json.loads(line) for line in (directory/'samples.jsonl').read_text(encoding='utf-8').splitlines()]
moving = [r for r in rows if r['telemetry'].get('valid') and r['telemetry'].get('speed_mph', 0) > 2]
available = [r for r in moving if r.get('navigation_preview', {}).get('available')]
lead_ins = [r for r in available if r['navigation_preview'].get('lead_in_estimated')
or r['navigation_preview'].get('segment_kind') == 'lead-in']
matched = [r for r in available if not (r['navigation_preview'].get('lead_in_estimated')
or r['navigation_preview'].get('segment_kind') == 'lead-in')]
gaps, current = [], None
for row in moving:
preview = row.get('navigation_preview', {})
if not preview.get('available'):
reason = preview.get('reason', 'No route sample')
if current is None or current['reason'] != reason:
if current:
gaps.append(current)
current = dict(first_sequence=row['sequence'], last_sequence=row['sequence'],
started_at=row['monotonic'], last_at=row['monotonic'], reason=reason)
else:
current.update(last_sequence=row['sequence'], last_at=row['monotonic'])
elif current:
current['recovered_at'] = row['monotonic']
gaps.append(current)
current = None
if current:
gaps.append(current)
for gap in gaps:
gap['duration_s'] = gap.get('recovered_at', gap['last_at'])-gap['started_at']
report = dict(directory=str(directory.resolve()), passive=passive, active=not passive,
recording_mode='passive' if passive else 'active simulator trial',
usable_for_control=False,
samples=len(rows), moving_samples=len(moving), matched_samples=len(matched),
available_preview_samples=len(available), estimated_lead_in_samples=len(lead_ins),
moving_span_s=moving[-1]['monotonic']-moving[0]['monotonic'] if moving else 0,
matched_fraction=len(matched)/len(moving) if moving else 0,
gaps=gaps, model_usable_samples=sum(not r['vision_reason'] for r in moving),
limitations=['GPS graph matching is not lane-center validation',
'Route bends, exits, intersections and rerouting need separate moving checks',
'Recorded commands and engagement do not establish safe GPS driving'])
engaged = [r for r in rows if r.get('fsd_engaged') is True]
commands = [r for r in rows if isinstance(r.get('control_command'), dict)]
applied = [r for r in commands if r['control_command'].get('applied') is True]
report.update(engagement_samples=len(engaged),
engagement_state_recorded_samples=sum(isinstance(r.get('fsd_engaged'), bool) for r in rows),
command_samples=len(commands), applied_command_samples=len(applied),
engaged_route_matched_samples=sum(bool(r.get('navigation_preview', {}).get('available')) for r in engaged),
navigation_driver_samples=sum(isinstance(r.get('navigation_driver'), dict) for r in rows))
if matched:
lateral = sorted(v for r in matched
if isinstance(v := r['navigation_preview'].get('lateral_error_world_m'), (int,float))
and math.isfinite(v))
headings = [v for r in matched
if isinstance(v := r['navigation_preview'].get('heading_dot'), (int,float))
and math.isfinite(v)]
report.update(lateral_error_world_m=(dict(median=statistics.median(lateral),
p95=lateral[int(.95*(len(lateral)-1))], maximum=max(lateral)) if lateral else None),
minimum_heading_dot=min(headings) if headings else None,
matched_segments=sorted({r['navigation_preview']['segment'] for r in matched}),
route_revisions=len({r['navigation_preview']['revision'] for r in matched}))
(directory/'navigation-assessment.json').write_text(json.dumps(report, indent=2, allow_nan=False), encoding='utf-8')
return report
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('directory')
args = parser.parse_args()
print(json.dumps(assess(args.directory), indent=2))