372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535 | def build_time_decode_report(
summary_csv: Path,
*,
subject_csvs: list[Path] | None = None,
chance: float = 0.5,
baseline_window: tuple[float, float] = (-0.1, 0.0),
effect_window: tuple[float, float] = (0.1, 0.8),
selection_metric: str = "accuracy",
) -> str:
"""Build a Markdown report for a time-resolved decoding benchmark."""
selection_metric = _validate_selection_metric(selection_metric)
summary = pd.read_csv(summary_csv)
comparison_columns = _comparison_group_columns(summary)
has_comparison = "decoder" in summary.columns and summary.groupby(comparison_columns).ngroups > 1
if has_comparison:
comparison = summarize_decoder_comparison(
summary,
baseline_window=baseline_window,
effect_window=effect_window,
selection_metric=selection_metric,
)
display_group_columns = _display_group_columns(comparison)
group_headers = [
SUBJECT_TABLE_LABELS.get(column, column.replace("_", " ").title()) for column in display_group_columns
]
if selection_metric == "accuracy":
headers = [
*group_headers,
"Subjects",
"Peak time (s)",
"Peak accuracy",
"Baseline accuracy",
"Effect accuracy",
"Effect minus baseline",
"Peak log loss",
"Peak Brier",
"Peak ECE",
]
alignments = [*("---" for _ in group_headers), "---:", "---:", "---:", "---:", "---:", "---:", "---:", "---:", "---:"]
else:
metric_label = _metric_label(selection_metric).title()
headers = [
*group_headers,
"Subjects",
"Selected time (s)",
f"Selected {metric_label}",
f"Baseline {metric_label}",
f"Effect {metric_label}",
"Selection improvement",
"Accuracy at selected time",
"Log loss at selected time",
"Brier at selected time",
"ECE at selected time",
]
alignments = [
*("---" for _ in group_headers),
"---:",
"---:",
"---:",
"---:",
"---:",
"---:",
"---:",
"---:",
"---:",
"---:",
]
lines = [
"# NeuRepTrace Decoder Comparison Report",
"",
f"- Summary CSV: `{summary_csv}`",
f"- Baseline window: {_format_float(baseline_window[0])} to {_format_float(baseline_window[1])} s",
f"- Effect window: {_format_float(effect_window[0])} to {_format_float(effect_window[1])} s",
f"- Selection metric: {_metric_descriptor(selection_metric)}",
"",
f"| {' | '.join(headers)} |",
f"| {' | '.join(alignments)} |",
]
for row in comparison.itertuples(index=False):
group_values = [str(getattr(row, column)) for column in display_group_columns]
if selection_metric == "accuracy":
values = [
*group_values,
str(row.n_subjects),
_format_float(row.peak_time),
_format_float(row.peak_accuracy),
_format_float(row.baseline_accuracy_mean),
_format_float(row.effect_accuracy_mean),
_format_float(row.effect_minus_baseline),
_format_float(row.peak_log_loss),
_format_float(row.peak_brier),
_format_float(row.peak_ece),
]
else:
values = [
*group_values,
str(row.n_subjects),
_format_float(row.selected_time),
_format_float(row.selected_score),
_format_float(row.baseline_selection_mean),
_format_float(row.effect_selection_mean),
_format_float(row.selection_improvement),
_format_float(row.peak_accuracy),
_format_float(row.peak_log_loss),
_format_float(row.peak_brier),
_format_float(row.peak_ece),
]
lines.append(f"| {' | '.join(values)} |")
if subject_csvs:
_append_subject_time_decode_section(lines, subject_csvs, effect_window=effect_window, selection_metric=selection_metric)
lines.append("")
return "\n".join(lines)
aggregate = summarize_aggregate_time_decode(
summary,
chance=chance,
baseline_window=baseline_window,
effect_window=effect_window,
selection_metric=selection_metric,
)
lines = [
"# NeuRepTrace Time-Decoding Report",
"",
f"- Summary CSV: `{summary_csv}`",
f"- Subjects: {int(aggregate['n_subjects'])}",
f"- Chance level: {_format_float(aggregate['chance'])}",
f"- Baseline window: {_format_float(baseline_window[0])} to {_format_float(baseline_window[1])} s",
f"- Effect window: {_format_float(effect_window[0])} to {_format_float(effect_window[1])} s",
f"- Selection metric: {_metric_descriptor(selection_metric)}",
"",
"## Aggregate",
"",
"| Metric | Value |",
"| --- | ---: |",
f"| Peak aggregate accuracy | {_format_float(aggregate['peak_accuracy'])} |",
f"| Peak time | {_format_float(aggregate['peak_time'])} s |",
f"| Accuracy SEM at peak | {_format_float(aggregate['peak_accuracy_sem'])} |",
f"| Baseline-window mean accuracy | {_format_float(aggregate['baseline_accuracy_mean'])} |",
f"| Effect-window mean accuracy | {_format_float(aggregate['effect_accuracy_mean'])} |",
f"| Effect-window delta from chance | {_format_float(aggregate['effect_accuracy_delta'])} |",
f"| Log loss at peak | {_format_float(aggregate['peak_log_loss'])} |",
f"| Brier score at peak | {_format_float(aggregate['peak_brier'])} |",
f"| ECE at peak | {_format_float(aggregate['peak_ece'])} |",
]
if selection_metric != "accuracy":
metric_label = _metric_label(selection_metric).title()
lines.extend(
[
f"| Selected metric | {_metric_descriptor(selection_metric)} |",
f"| Selected time | {_format_float(aggregate['selected_time'])} s |",
f"| Selected {metric_label} | {_format_float(aggregate['selected_score'])} |",
f"| Accuracy at selected time | {_format_float(aggregate['selected_accuracy'])} |",
f"| Baseline-window mean {metric_label} | {_format_float(aggregate['baseline_selection_mean'])} |",
f"| Effect-window mean {metric_label} | {_format_float(aggregate['effect_selection_mean'])} |",
f"| Effect-window improvement in {metric_label} | {_format_float(aggregate['selection_improvement'])} |",
]
)
if subject_csvs:
_append_subject_time_decode_section(lines, subject_csvs, effect_window=effect_window, summary=summary, selection_metric=selection_metric)
lines.append("")
return "\n".join(lines)
|