import csv, sys
B = sys.argv[1]
ci = {(r['engine'], r['dataset']): r for r in csv.DictReader(open(B + '/wer_ci.csv'))}
rows = list(csv.DictReader(open(B + '/results.csv')))
for r in rows:
    key = (r['engine'].split('/')[1], r['dataset'])
    r['wer_ci95_low'] = ci.get(key, {}).get('ci95_low', '')
    r['wer_ci95_high'] = ci.get(key, {}).get('ci95_high', '')
    r['notes'] = ('peak_memory_footprint_mb excludes the memory-mapped GGUF weights (read by Metal from the file); '
                  'budget roughly footprint + disk_mb') if r['backend'] == 'MTL0' else ''
    if r['backend'] == 'apple':
        r['disk_mb'] = 395
        r['notes'] = ('disk = /System/Library/AssetsV2/com_apple_MobileAsset_UAF_Speech_AutomaticSpeechRecognition (en asset 3100.52005.81607); '
                      'model runs in the system localspeechrecognition XPC service, sampled max RSS 146 MB; footprint column is the helper only')
cols = ['engine','name','dataset','utterances','audio_minutes','wer_whisper_norm','wer_ci95_low','wer_ci95_high','wer_basic_norm',
        'seconds_per_30s_audio','rtf_median','rtf_launches','median_clip_latency_s','first_clip_after_load_s','load_seconds_first',
        'load_seconds_repeat','peak_memory_footprint_mb','max_rss_mb','disk_mb','failed_clips','empty_outputs',
        'identical_text_across_launches','backend','language_sent','handy_published_en_error_percent','handy_published_seconds_per_30s','notes']
w = csv.DictWriter(open(B + '/results.csv', 'w', newline=''), fieldnames=cols); w.writeheader(); w.writerows(rows)
