#!/usr/bin/env python3
"""Stage-1 confused-entropy audit on the official ICBHI test fold.

Loads the frozen F12 head (SpecAug off), scores every test cycle, and writes
compact tables under save/analysis/. Does not dump per-cycle raw files.
"""

import argparse
import csv
import json
import math
import os
import sys

import numpy as np
import pandas as pd
import torch

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from method.feedback_decoder import CONFUSED_ENTROPY, cycle_decision
from method.frame_asd import (
    align_frame_energy,
    mil_pool_logits,
    probs_from_logits,
    search_dual_threshold,
)
from models import get_backbone_class
from util.frame_asd_train import add_frame_asd_args, beats_kwargs, set_seed
from util.dataset import ICBHIDataset
from util.icbhi_util import get_score

NAMES = ('normal', 'crackle', 'wheeze', 'both')
DEVICES = ('Meditron', 'LittC2SE', 'Litt3200', 'AKGC417L')
DEFAULT_CKPT = (
    'save/icbhi_beats_frame_asd_frame_asd_F12_sastruct_s1_rerun2/best.pth'
)
TAU_C = 0.91
TAU_W = 0.75


def parse_args():
    parser = argparse.ArgumentParser('Stage-1 confused entropy on the official test fold')
    add_frame_asd_args(parser)
    parser.add_argument('--out_dir', type=str, default='save/analysis')
    args = parser.parse_args()
    args.model = 'beats'
    args.use_specaug = False
    args.freeze_backbone = True
    args.mil_pool = 'topk'
    args.mil_topk = 3
    args.beats_wave_scale = 32768.0
    args.head_type = 'linear'
    args.num_workers = 0
    args.test_fold = 'official'
    args.class_split = 'lungsound'
    args.n_cls = 4
    args.dataset = 'icbhi'
    args.desired_length = 5
    args.pad_types = 'repeat'
    args.sample_rate = 16000
    return args


def _label_from_flags(crackle, wheeze):
    crackle = int(crackle)
    wheeze = int(wheeze)
    if crackle == 0 and wheeze == 0:
        return 0
    if crackle == 1 and wheeze == 0:
        return 1
    if crackle == 0 and wheeze == 1:
        return 2
    return 3


def official_cycle_meta(args):
    """Duration / device / patient in the same order as ICBHIDataset (no audio)."""
    data_folder = os.path.join(args.data_folder, 'icbhi_dataset/audio_test_data')
    split_path = os.path.join(args.data_folder, 'icbhi_dataset/official_split.txt')
    patient_dict = {}
    for line in open(split_path):
        fpath, fold = line.strip().split('\t')
        if fold == 'test':
            patient_dict[fpath] = fold
    names = sorted(os.listdir(data_folder))
    names = sorted(set(
        name.strip().split('.')[0] for name in names
        if name.endswith('.wav') or name.endswith('.txt')))
    rows = []
    for filename in names:
        if filename not in patient_dict:
            continue
        ann = pd.read_csv(
            os.path.join(data_folder, filename + '.txt'),
            names=['Start', 'End', 'Crackles', 'Wheezes'],
            delimiter='\t')
        device = filename.strip().split('_')[-1]
        patient = int(filename.split('_')[0])
        for _, row in ann.iterrows():
            rows.append({
                'filename': filename,
                'patient': patient,
                'device': device,
                'duration_s': float(row['End']) - float(row['Start']),
                'label': _label_from_flags(row['Crackles'], row['Wheezes']),
            })
    return rows


def _crest_snr_db(waves, n_frames):
    """Within-cycle contrast: 20 log10 of the 90th / 10th percentile frame RMS.

    Not a recording-level SNR. Repeat-padding makes this a contrast proxy.
    """
    energy = align_frame_energy(waves, n_frames).clamp(min=1e-12).sqrt()
    low = torch.quantile(energy, 0.10, dim=1)
    high = torch.quantile(energy, 0.90, dim=1)
    return (20.0 * torch.log10(high / low.clamp(min=1e-6))).cpu()


def _score_of(labels, preds):
    hits = [0.0] * 4
    counts = [0.0] * 4
    for truth, pred in zip(labels, preds):
        counts[int(truth)] += 1.0
        if int(truth) == int(pred):
            hits[int(truth)] += 1.0
    sp, se, score, _ = get_score(hits, counts)
    return {'sp': float(sp), 'se': float(se), 'score': float(score),
            'hits': hits, 'counts': counts}


@torch.no_grad()
def collect(model, head, loader, device, args):
    model.eval()
    head.eval()
    packs = []
    for batch_index, (waves, labels, patient_ids) in enumerate(loader):
        waves = waves.to(device, non_blocking=True)
        labels = labels.to(device)
        frames = model.encode_frames(waves)
        logit_c, logit_w = head(frames)
        p_c, p_w = probs_from_logits(
            logit_c, logit_w, mode='topk', topk=args.mil_topk)
        p_c_max = torch.sigmoid(mil_pool_logits(logit_c, mode='max'))
        p_w_max = torch.sigmoid(mil_pool_logits(logit_w, mode='max'))
        decision = cycle_decision(p_c, p_w, tau_c=TAU_C, tau_w=TAU_W)
        snr = _crest_snr_db(waves, logit_c.size(1))
        packs.append({
            'label': labels.cpu(),
            'patient': patient_ids.cpu(),
            'p_c': p_c.cpu(),
            'p_w': p_w.cpu(),
            'p_c_max': p_c_max.cpu(),
            'p_w_max': p_w_max.cpu(),
            'pred': decision['pred'].cpu(),
            'entropy': decision['entropy'].cpu(),
            'margin': decision['margin'].cpu(),
            'top1': decision['top1'].cpu(),
            'top2': decision['top2'].cpu(),
            'snr_db': snr,
            'n_tokens': logit_c.size(1),
            'token_dim': frames.size(-1),
            'wave_len': waves.size(-1),
        })
        if (batch_index + 1) % 20 == 0:
            print('  batch {}/{}'.format(batch_index + 1, len(loader)), flush=True)
    out = {}
    for key in packs[0]:
        if key in ('n_tokens', 'token_dim', 'wave_len'):
            out[key] = packs[0][key]
        else:
            out[key] = torch.cat([pack[key] for pack in packs])
    return out


def _write_csv(path, rows, fieldnames):
    with open(path, 'w', newline='') as handle:
        writer = csv.DictWriter(handle, fieldnames=fieldnames)
        writer.writeheader()
        for row in rows:
            writer.writerow(row)


def _bucket(entropy, correct):
    high = entropy >= CONFUSED_ENTROPY
    if high and correct:
        return 'high_entropy_correct'
    if high and not correct:
        return 'high_entropy_wrong'
    if (not high) and not correct:
        return 'low_entropy_wrong'
    return 'low_entropy_correct'


def _length_bin(duration):
    if duration < 1.0:
        return '<1s'
    if duration < 2.0:
        return '1-2s'
    if duration < 3.0:
        return '2-3s'
    return '>=3s'


def _snr_bin(snr):
    if snr < 6.0:
        return '<6dB'
    if snr < 12.0:
        return '6-12dB'
    if snr < 18.0:
        return '12-18dB'
    return '>=18dB'


def _mean(values):
    if not values:
        return 0.0
    return float(sum(values) / len(values))


def main():
    args = parse_args()
    if not args.resume:
        args.resume = DEFAULT_CKPT
    if not os.path.isfile(args.resume):
        raise SystemExit('Missing checkpoint {}'.format(args.resume))
    set_seed(args.seed)
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    print('Device', device, flush=True)
    print('Checkpoint', args.resume, flush=True)

    meta = official_cycle_meta(args)
    print('Annotation cycles', len(meta), flush=True)
    print('Loading official test waves (no train cache)...', flush=True)
    val_set = ICBHIDataset(train_flag=False, args=args, print_flag=True)
    if len(val_set) != len(meta):
        raise SystemExit('meta {} != dataset {}'.format(len(meta), len(val_set)))
    for index, row in enumerate(meta):
        item = val_set.audio_images[index]
        if int(item[1]) != int(row['label']) or int(item[3]) != int(row['patient']):
            raise SystemExit('metadata misaligned at cycle {}'.format(index))

    loader = torch.utils.data.DataLoader(
        val_set, batch_size=16, shuffle=False, num_workers=0, pin_memory=True)

    backbone = get_backbone_class('beats')
    model = backbone(
        freeze_encoder=True, spec_transform=None, frame_multiscale=False,
        **beats_kwargs(args),
    ).to(device)
    from method.frame_asd import build_frame_asd_head
    head = build_frame_asd_head('linear', in_dim=model.encoder_dim).to(device)
    checkpoint = torch.load(args.resume, map_location=device, weights_only=False)
    model.load_state_dict(checkpoint['model'], strict=True)
    head.load_state_dict(checkpoint['classifier'], strict=True)
    model.eval()
    head.eval()

    print('Scoring test cycles', flush=True)
    packed = collect(model, head, loader, device, args)
    n = int(packed['label'].numel())
    labels = packed['label'].tolist()
    preds = packed['pred'].tolist()
    official = _score_of(labels, preds)
    print('Official taus {:.2f}/{:.2f} -> Sp {:.2f} Se {:.2f} Score {:.2f}'.format(
        TAU_C, TAU_W, official['sp'], official['se'], official['score']), flush=True)

    p_c = packed['p_c'].numpy()
    p_w = packed['p_w'].numpy()
    p_c_max = packed['p_c_max'].numpy()
    p_w_max = packed['p_w_max'].numpy()
    entropy = packed['entropy'].numpy()
    margin = packed['margin'].numpy()

    max_dual = search_dual_threshold(
        p_c_max, p_w, labels, n_points=41, min_se=48.0, min_sp=72.0)
    both_heads_max = search_dual_threshold(
        p_c_max, p_w_max, labels, n_points=41, min_se=48.0, min_sp=72.0)
    print('Counterfactual crackle=max wheeze=topk Score {:.2f} (tau {:.2f}/{:.2f})'.format(
        max_dual['score'], max_dual['tau_crackle'], max_dual['tau_wheeze']), flush=True)
    print('Counterfactual both heads max Score {:.2f}'.format(both_heads_max['score']), flush=True)

    rows = []
    for index in range(n):
        truth = int(labels[index])
        pred = int(preds[index])
        correct = truth == pred
        rows.append({
            'truth': truth,
            'pred': pred,
            'correct': correct,
            'bucket': _bucket(float(entropy[index]), correct),
            'entropy': float(entropy[index]),
            'margin': float(margin[index]),
            'p_c': float(p_c[index]),
            'p_w': float(p_w[index]),
            'p_c_max': float(p_c_max[index]),
            'p_w_max': float(p_w_max[index]),
            'device': meta[index]['device'],
            'patient': meta[index]['patient'],
            'duration_s': meta[index]['duration_s'],
            'snr_db': float(packed['snr_db'][index]),
            'length_bin': _length_bin(meta[index]['duration_s']),
            'snr_bin': _snr_bin(float(packed['snr_db'][index])),
            'top1': int(packed['top1'][index]),
            'top2': int(packed['top2'][index]),
        })

    n_normal = sum(1 for row in rows if row['truth'] == 0)
    n_abnormal = n - n_normal
    sp_point = 100.0 / max(n_normal, 1)
    se_point = 100.0 / max(n_abnormal, 1)

    def group_table(key):
        grouped = {}
        for row in rows:
            grouped.setdefault(row[key], []).append(row)
        table = []
        for name in sorted(grouped, key=lambda item: str(item)):
            group = grouped[name]
            wrong = [row for row in group if not row['correct']]
            table.append({
                'group': name,
                'n': len(group),
                'accuracy': 100.0 * (len(group) - len(wrong)) / len(group),
                'mean_entropy': _mean([row['entropy'] for row in group]),
                'mean_entropy_wrong': _mean([row['entropy'] for row in wrong]),
                'mean_margin': _mean([row['margin'] for row in group]),
                'high_entropy_wrong': sum(1 for row in group if row['bucket'] == 'high_entropy_wrong'),
                'low_entropy_wrong': sum(1 for row in group if row['bucket'] == 'low_entropy_wrong'),
                'high_entropy_correct': sum(1 for row in group if row['bucket'] == 'high_entropy_correct'),
            })
        return table

    bucket_counts = {name: 0 for name in (
        'high_entropy_correct', 'high_entropy_wrong',
        'low_entropy_correct', 'low_entropy_wrong')}
    for row in rows:
        bucket_counts[row['bucket']] += 1

    class_rows = []
    for class_id, name in enumerate(NAMES):
        group = [row for row in rows if row['truth'] == class_id]
        wrong = [row for row in group if not row['correct']]
        class_rows.append({
            'class': name,
            'n': len(group),
            'recall': 100.0 * (len(group) - len(wrong)) / max(len(group), 1),
            'mean_entropy_correct': _mean([row['entropy'] for row in group if row['correct']]),
            'mean_entropy_wrong': _mean([row['entropy'] for row in wrong]),
            'mean_margin_wrong': _mean([row['margin'] for row in wrong]),
            'high_entropy_wrong': sum(row['bucket'] == 'high_entropy_wrong' for row in group),
            'low_entropy_wrong': sum(row['bucket'] == 'low_entropy_wrong' for row in group),
            'high_entropy_correct': sum(row['bucket'] == 'high_entropy_correct' for row in group),
            'mean_p_c_wrong': _mean([row['p_c'] for row in wrong]),
            'mean_p_c_max_wrong': _mean([row['p_c_max'] for row in wrong]),
            'mean_p_w_wrong': _mean([row['p_w'] for row in wrong]),
        })

    confusion_rows = []
    for truth in range(4):
        for pred in range(4):
            cell = [row for row in rows if row['truth'] == truth and row['pred'] == pred]
            if not cell:
                continue
            hurts = 'none'
            score_if_fixed = 0.0
            if truth == 0 and pred != 0:
                hurts = 'Sp'
                score_if_fixed = 0.5 * len(cell) * sp_point
            elif truth != 0 and pred != truth:
                hurts = 'Se'
                score_if_fixed = 0.5 * len(cell) * se_point
            near = 0
            overconfident = 0
            if truth in (1, 3) and pred in (0, 2):
                for row in cell:
                    if row['p_c'] >= 0.75:
                        near += 1
                    elif row['p_c'] < 0.30:
                        overconfident += 1
            confusion_rows.append({
                'truth': NAMES[truth],
                'pred': NAMES[pred],
                'n': len(cell),
                'hurts': hurts,
                'score_points_if_fixed': round(score_if_fixed, 3),
                'mean_entropy': round(_mean([row['entropy'] for row in cell]), 4),
                'mean_margin': round(_mean([row['margin'] for row in cell]), 4),
                'mean_p_c': round(_mean([row['p_c'] for row in cell]), 4),
                'mean_p_c_max': round(_mean([row['p_c_max'] for row in cell]), 4),
                'mean_p_w': round(_mean([row['p_w'] for row in cell]), 4),
                'mean_p_w_max': round(_mean([row['p_w_max'] for row in cell]), 4),
                'frac_high_entropy': round(
                    sum(row['entropy'] >= CONFUSED_ENTROPY for row in cell) / len(cell), 4),
                'crackle_near_tau': near,
                'crackle_p_below_0.30': overconfident,
                'mean_duration_s': round(_mean([row['duration_s'] for row in cell]), 3),
                'mean_snr_db': round(_mean([row['snr_db'] for row in cell]), 3),
            })
    confusion_rows.sort(key=lambda row: -row['score_points_if_fixed'])

    patient_errors = {}
    for row in rows:
        if row['correct']:
            continue
        slot = patient_errors.setdefault(row['patient'], {
            'patient': row['patient'], 'device': row['device'], 'errors': 0,
            'se_errors': 0, 'sp_errors': 0, 'mean_entropy': [],
        })
        slot['errors'] += 1
        slot['mean_entropy'].append(row['entropy'])
        if row['truth'] == 0:
            slot['sp_errors'] += 1
        else:
            slot['se_errors'] += 1
    patient_rows = []
    for slot in patient_errors.values():
        patient_rows.append({
            'patient': slot['patient'],
            'device': slot['device'],
            'errors': slot['errors'],
            'se_errors': slot['se_errors'],
            'sp_errors': slot['sp_errors'],
            'mean_entropy': round(_mean(slot['mean_entropy']), 4),
        })
    patient_rows.sort(key=lambda row: (-row['se_errors'], -row['errors']))
    patient_rows = patient_rows[:15]

    # Probability bins for the two Se leaks that dominate the matrix.
    def pc_hist(truth, pred):
        cell = [row['p_c'] for row in rows if row['truth'] == truth and row['pred'] == pred]
        edges = (0.0, 0.15, 0.30, 0.50, 0.75, 0.91, 1.01)
        hist = []
        for left, right in zip(edges, edges[1:]):
            count = sum(left <= value < right for value in cell)
            hist.append({'truth': NAMES[truth], 'pred': NAMES[pred],
                         'p_c_lo': left, 'p_c_hi': right, 'n': count})
        return hist

    hist_rows = pc_hist(1, 0) + pc_hist(3, 2) + pc_hist(2, 0) + pc_hist(0, 1)

    def trace_for(truth, pred, k=2):
        cell = [row for row in rows if row['truth'] == truth and row['pred'] == pred]
        cell.sort(key=lambda row: -row['entropy'])
        picked = []
        for row in cell[:k]:
            picked.append({
                'stage': 'stage1_official_test',
                'input': {
                    'wave': [1, packed['wave_len']],
                    'tokens': [packed['n_tokens'], packed['token_dim']],
                    'duration_s': round(row['duration_s'], 3),
                    'device': row['device'],
                    'patient': row['patient'],
                    'snr_proxy_db': round(row['snr_db'], 2),
                },
                'stage1_output': {
                    'p_crackle_topk3': round(row['p_c'], 4),
                    'p_wheeze_topk3': round(row['p_w'], 4),
                    'p_crackle_max': round(row['p_c_max'], 4),
                    'p_wheeze_max': round(row['p_w_max'], 4),
                    'tau_crackle': TAU_C,
                    'tau_wheeze': TAU_W,
                    'pred': NAMES[row['pred']],
                    'truth': NAMES[row['truth']],
                    'entropy_nats': round(row['entropy'], 4),
                    'top2_margin': round(row['margin'], 4),
                    'top1': NAMES[row['top1']],
                    'top2': NAMES[row['top2']],
                    'bucket': row['bucket'],
                },
                'stage2_handoff': {
                    'consumes': 'continuous p_c, p_w, binary entropy, max-vs-topk gap (9-d stats)',
                    'not': 'one-hot of pred',
                    'confused': row['entropy'] >= CONFUSED_ENTROPY,
                },
            })
        return picked

    traces = []
    for pair in ((1, 0), (2, 0), (3, 2), (0, 1), (0, 2), (3, 3)):
        traces.extend(trace_for(*pair, k=1))

    os.makedirs(args.out_dir, exist_ok=True)
    bucket_rows = []
    for name, count in bucket_counts.items():
        group = [row for row in rows if row['bucket'] == name]
        bucket_rows.append({
            'bucket': name,
            'n': count,
            'share': round(count / n, 4),
            'mean_entropy': round(_mean([row['entropy'] for row in group]), 4),
            'mean_margin': round(_mean([row['margin'] for row in group]), 4),
            'gate_nats': round(CONFUSED_ENTROPY, 4),
        })
    _write_csv(os.path.join(args.out_dir, 's1_entropy_buckets.csv'), bucket_rows,
               ['bucket', 'n', 'share', 'mean_entropy', 'mean_margin', 'gate_nats'])
    _write_csv(
        os.path.join(args.out_dir, 's1_entropy_confusion.csv'), confusion_rows,
        list(confusion_rows[0].keys()))
    _write_csv(
        os.path.join(args.out_dir, 's1_entropy_by_class.csv'), class_rows,
        list(class_rows[0].keys()))
    device_rows = group_table('device')
    length_rows = group_table('length_bin')
    snr_rows = group_table('snr_bin')
    _write_csv(os.path.join(args.out_dir, 's1_entropy_by_device.csv'), device_rows,
               list(device_rows[0].keys()))
    _write_csv(os.path.join(args.out_dir, 's1_entropy_by_length.csv'), length_rows,
               list(length_rows[0].keys()))
    _write_csv(os.path.join(args.out_dir, 's1_entropy_by_snr.csv'), snr_rows,
               list(snr_rows[0].keys()))
    _write_csv(os.path.join(args.out_dir, 's1_entropy_patients.csv'), patient_rows,
               list(patient_rows[0].keys()))
    _write_csv(os.path.join(args.out_dir, 's1_entropy_pc_hist.csv'), hist_rows,
               ['truth', 'pred', 'p_c_lo', 'p_c_hi', 'n'])

    summary = {
        'checkpoint': os.path.abspath(args.resume),
        'split': 'official test fold, cycle level',
        'n_cycles': n,
        'wave': [packed['wave_len']],
        'tokens': [packed['n_tokens'], packed['token_dim']],
        'tau_crackle': TAU_C,
        'tau_wheeze': TAU_W,
        'entropy_gate_nats': CONFUSED_ENTROPY,
        'official': official,
        'counterfactual_crackle_max': {
            'score': max_dual['score'], 'sp': max_dual['sp'], 'se': max_dual['se'],
            'tau_crackle': max_dual['tau_crackle'], 'tau_wheeze': max_dual['tau_wheeze'],
            'constraint_fallback': max_dual['constraint_fallback'],
        },
        'counterfactual_both_max': {
            'score': both_heads_max['score'], 'sp': both_heads_max['sp'],
            'se': both_heads_max['se'],
            'tau_crackle': both_heads_max['tau_crackle'],
            'tau_wheeze': both_heads_max['tau_wheeze'],
        },
        'buckets': bucket_counts,
        'score_point_per_fixed_normal': 0.5 * sp_point,
        'score_point_per_fixed_abnormal': 0.5 * se_point,
    }
    with open(os.path.join(args.out_dir, 's1_entropy_summary.json'), 'w') as handle:
        json.dump(summary, handle, indent=2)
    with open(os.path.join(args.out_dir, 's1_trace_cycles.json'), 'w') as handle:
        json.dump({'traces': traces, 'shapes': {
            'wave': 'B x {}'.format(packed['wave_len']),
            'tokens': 'B x {} x {}'.format(packed['n_tokens'], packed['token_dim']),
            'logit_crackle': 'B x {}'.format(packed['n_tokens']),
            'logit_wheeze': 'B x {}'.format(packed['n_tokens']),
            'p_mil': 'B',
            'decision': 'pred, entropy, top2_margin',
        }}, handle, indent=2)

    def fmt_table(title, table, cols):
        lines = ['', '### ' + title, '', '| ' + ' | '.join(cols) + ' |',
                 '| ' + ' | '.join(['---'] * len(cols)) + ' |']
        for row in table:
            lines.append('| ' + ' | '.join(str(row[col]) for col in cols) + ' |')
        return lines

    md = [
        '# Stage-1 confused entropy (official test)',
        '',
        'Checkpoint: `{}`.'.format(args.resume),
        'Split: official 60/40 test fold, one row per respiratory cycle (n={}).'.format(n),
        'SpecAug off. MIL top-3. Dual threshold tau_c={:.2f}, tau_w={:.2f}.'.format(TAU_C, TAU_W),
        'Entropy gate for "high" is ln(2) = {:.3f} nats (max ln 4 = {:.3f}).'.format(
            CONFUSED_ENTROPY, math.log(4.0)),
        'SNR column is a within-cycle frame-RMS contrast (p90/p10), not a dataset SNR label.',
        '',
        'Reproduced score at the saved thresholds: Sp {:.2f}, Se {:.2f}, Score {:.2f}.'.format(
            official['sp'], official['se'], official['score']),
        'Fixing one normal error is worth {:.3f} score points. Fixing one abnormal error is worth {:.3f}.'.format(
            0.5 * sp_point, 0.5 * se_point),
        '',
        'Counterfactual, no retrain: crackle pooled by max, wheeze still top-3, thresholds re-searched '
        'under Se>=48 and Sp>=72: Score {:.2f} (Sp {:.2f}, Se {:.2f}, tau {:.2f}/{:.2f}).'.format(
            max_dual['score'], max_dual['sp'], max_dual['se'],
            max_dual['tau_crackle'], max_dual['tau_wheeze']),
        'Both heads max-pooled: Score {:.2f} (Sp {:.2f}, Se {:.2f}).'.format(
            both_heads_max['score'], both_heads_max['sp'], both_heads_max['se']),
        '',
    ]
    md += fmt_table('Buckets', bucket_rows,
                    ['bucket', 'n', 'share', 'mean_entropy', 'mean_margin'])
    show_conf = [row for row in confusion_rows if row['n'] >= 7]
    md += fmt_table(
        'Confusion cells (n>=7), sorted by score points if every cycle in the cell were fixed',
        show_conf,
        ['truth', 'pred', 'n', 'hurts', 'score_points_if_fixed', 'mean_entropy',
         'mean_margin', 'mean_p_c', 'mean_p_c_max', 'frac_high_entropy',
         'crackle_near_tau', 'crackle_p_below_0.30'])
    md += fmt_table('By true class', class_rows,
                    ['class', 'n', 'recall', 'mean_entropy_correct', 'mean_entropy_wrong',
                     'high_entropy_wrong', 'low_entropy_wrong', 'mean_p_c_wrong',
                     'mean_p_c_max_wrong'])
    md += fmt_table('By device', device_rows,
                    ['group', 'n', 'accuracy', 'mean_entropy', 'high_entropy_wrong',
                     'low_entropy_wrong'])
    md += fmt_table('By cycle length', length_rows,
                    ['group', 'n', 'accuracy', 'mean_entropy', 'high_entropy_wrong',
                     'low_entropy_wrong'])
    md += fmt_table('By within-cycle contrast', snr_rows,
                    ['group', 'n', 'accuracy', 'mean_entropy', 'high_entropy_wrong',
                     'low_entropy_wrong'])
    md += fmt_table('Patients with the most sensitivity errors (top 15)', patient_rows,
                    ['patient', 'device', 'errors', 'se_errors', 'sp_errors', 'mean_entropy'])
    md += [
        '',
        'Low-entropy wrong means the four-class product is peaked (H < ln 2) and the dual-threshold label is still wrong.',
        'That is an overconfident error. High-entropy wrong is a confused miss.',
        'crackle_near_tau counts crackle-positive cycles whose top-3 p_c is in [0.75, 1) but the threshold still says no crackle.',
        'crackle_p_below_0.30 counts crackle-positive cycles the crackle head has essentially given up on.',
        '',
        'Shapes on this run: wave B x {}, tokens B x {} x {}, each head logit B x {}.'.format(
            packed['wave_len'], packed['n_tokens'], packed['token_dim'], packed['n_tokens']),
        'A few cycles are in `s1_trace_cycles.json`.',
        '',
    ]
    md_path = os.path.join(args.out_dir, 's1_entropy.md')
    with open(md_path, 'w') as handle:
        handle.write('\n'.join(md) + '\n')
    print('Wrote', md_path, flush=True)


if __name__ == '__main__':
    main()
