File size: 8,672 Bytes
e8c3964
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env python
"""
run_experiments.py
==================

High-level driver that wires together:

1.  YAML / CLI β†’ `PipelineConfig` + `LoggingConfig`
2.  Initialises dual-sink logging (console + rotating file)
3.  Builds a `RAGPipeline`
4.  Streams a list of questions through the pipeline
5.  Logs progress, writes per-query JSONL results, and
    (optionally) prints aggregate statistics.

You can keep it minimal – or expand the marked TODO sections to:
* compute metrics immediately
* push results to a tracker (W&B, MLflow, etc.)
* spawn multiple configs in parallel.
"""
from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path
from typing import Any, Dict, Iterable, List, Mapping

import yaml

from evaluation import (
    PipelineConfig,
    RetrieverConfig,
    GeneratorConfig,
    CrossEncoderConfig,
    StatsConfig,
    LoggingConfig,
    RAGPipeline,
)
from evaluation.utils.logger import init_logging

from evaluation.stats import (
    corr_ci,
    wilcoxon_signed_rank,
    holm_bonferroni,
)

import matplotlib.pyplot as plt

# ──────────────────────────────────────────────────────────────────────────────
# Helpers
# ──────────────────────────────────────────────────────────────────────────────


def _merge_dataclass(dc_cls, default, override: Mapping[str, Any]):
    """Return a new *dc_cls* where fields from *override* overwrite *default*."""
    from dataclasses import asdict

    merged = asdict(default)
    merged.update({k: v for k, v in override.items() if v is not None})
    return dc_cls(**merged)


def _load_pipeline_config(yaml_path: Path | None) -> PipelineConfig:
    """Parse YAML into nested dataclasses; fall back to defaults."""
    if yaml_path is None:
        return PipelineConfig()  # all defaults

    data = yaml.safe_load(yaml_path.read_text())

    retr_cfg = _merge_dataclass(
        RetrieverConfig(), RetrieverConfig(), data.get("retriever", {})
    )
    gen_cfg = _merge_dataclass(
        GeneratorConfig(), GeneratorConfig(), data.get("generator", {})
    )
    rr_cfg = _merge_dataclass(
        CrossEncoderConfig(), CrossEncoderConfig(), data.get("reranker", {})
    )
    stats_cfg = _merge_dataclass(StatsConfig(), StatsConfig(), data.get("stats", {}))
    log_cfg = _merge_dataclass(LoggingConfig(), LoggingConfig(), data.get("logging", {}))

    return PipelineConfig(
        retriever=retr_cfg,
        generator=gen_cfg,
        reranker=rr_cfg,
        stats=stats_cfg,
        logging=log_cfg,
    )


def _read_jsonl(path: Path) -> List[Dict[str, Any]]:
    with path.open() as f:
        return [json.loads(line) for line in f]


def _write_jsonl(path: Path, rows: Iterable[Mapping[str, Any]]):
    path.parent.mkdir(parents=True, exist_ok=True)
    with path.open("w") as f:
        for row in rows:
            f.write(json.dumps(row) + "\n")

# Stats Helper
def aggregate_metrics(rows: list[dict[str, Any]]) -> dict[str, float]:
    """Return mean of every numeric metric found under row['metrics']."""
    import numpy as np
    keys = rows[0]["metrics"].keys()
    return {k: float(np.mean([r["metrics"][k] for r in rows])) for k in keys}


def correlation_with_gold(rows: list[dict[str, Any]], cfg: StatsConfig):
    """Spearman/Kendall correlation between retrieval scores and correctness flag."""
    if "human_correct" not in rows[0]:
        return None  # nothing to correlate
    mrr = [r["metrics"].get("mrr", float("nan")) for r in rows]
    gold = [1.0 if r["human_correct"] else 0.0 for r in rows]
    r, (lo, hi), p = corr_ci(
        mrr, gold, method=cfg.correlation_method, n_boot=cfg.n_boot, ci=cfg.ci
    )
    return dict(r=r, ci_low=lo, ci_high=hi, p=p)


def wilcoxon_against_baseline(
    cur: list[dict[str, Any]],
    base: list[dict[str, Any]],
    cfg: StatsConfig,
):
    """Paired Wilcoxon + Holm-Bonferroni across all metric keys."""
    from evaluation.stats import wilcoxon_signed_rank, holm_bonferroni

    assert len(cur) == len(base), "Runs must have same #queries"
    metrics = cur[0]["metrics"].keys()
    p_raw = {}
    for m in metrics:
        cur_m = [r["metrics"][m] for r in cur]
        base_m = [r["metrics"][m] for r in base]
        _, p = wilcoxon_signed_rank(cur_m, base_m, alternative=cfg.wilcoxon_alternative)
        p_raw[m] = p
    return holm_bonferroni(p_raw)

# Plot helper
def save_scatter(rows, out_dir: Path):
    out_dir.mkdir(parents=True, exist_ok=True)
    x = [r["metrics"]["mrr"] for r in rows if "mrr" in r["metrics"]]
    y = [1.0 if r.get("human_correct") else 0.0 for r in rows]
    plt.figure()
    plt.scatter(x, y, alpha=0.6)
    plt.xlabel("MRR")
    plt.ylabel("Correct (1=yes)")
    plt.title("MRR vs. Human Correctness")
    path = out_dir / "mrr_vs_correct.png"
    plt.savefig(path, bbox_inches="tight")
    plt.close()
    return path

# ──────────────────────────────────────────────────────────────────────────────
# Main
# ──────────────────────────────────────────────────────────────────────────────
def main(argv: list[str] | None = None) -> None:
    ap = argparse.ArgumentParser(description="Run RAG evaluation experiments.")
    ap.add_argument("--config", type=Path, help="YAML config with pipeline settings")
    ap.add_argument(
        "--queries",
        type=Path,
        required=True,
        help="JSONL file – each line must contain at least {'question': ...}",
    )
    ap.add_argument(
        "--output",
        type=Path,
        default=Path("outputs/results.jsonl"),
        help="Where to write JSONL results",
    )
    ap.add_argument("--dry-run", action="store_true", help="Do not execute pipeline")
    ap.add_argument(
        "--baseline",
        type=Path,
        help="Optional: JSONL with baseline run for significance tests",
    )
    ap.add_argument(
        "--plots",
        action="store_true",
        help="Save diagnostic plots (PNG) alongside results",
    )
    args = ap.parse_args(argv)

    # 1. Parse configuration
    cfg = _load_pipeline_config(args.config)

    # 2. Initialise logging (file + stderr)
    init_logging(
        log_dir=cfg.logging.log_dir,
        level=cfg.logging.level,
        max_mb=cfg.logging.max_mb,
        backups=cfg.logging.backups,
    )

    import logging

    logger = logging.getLogger(__name__)
    logger.info("Loaded PipelineConfig:\n%s", cfg)

    # 3. Build pipeline (retrieval β†’ (rerank) β†’ generation)
    pipeline = RAGPipeline(cfg)

    # 4. Load queries
    rows = _read_jsonl(args.queries)
    logger.info("Loaded %d queries from %s", len(rows), args.queries)

    if args.dry_run:
        logger.warning("Dry-run flag active – exiting before execution.")
        sys.exit(0)

    # 5. Execute pipeline
    results: List[Dict[str, Any]] = []
    for i, row in enumerate(rows, 1):
        q = row["question"]
        logger.info("[%d/%d] Q: %s", i, len(rows), q)
        out = pipeline.run(q)
        merged = {**row, **out}  # keep any gold labels or metadata
        results.append(merged)

    # 6. Persist results
    _write_jsonl(args.output, results)
    logger.info("Wrote %d results to %s", len(results), args.output)

    # 7. Aggregate statistics, significance tests, plots
    agg = aggregate_metrics(results)
    logger.info("Mean metrics: %s", json.dumps(agg, indent=2))

    corr = correlation_with_gold(results, cfg.stats)
    if corr:
        logger.info(
            "Correlation MRR↔gold  %s=%.3f  95%%CI=[%.3f, %.3f]  p=%.3g",
            cfg.stats.correlation_method,
            corr["r"],
            corr["ci_low"],
            corr["ci_high"],
            corr["p"],
        )

    if args.baseline:
        baseline_rows = _read_jsonl(args.baseline)
        p_adj = wilcoxon_against_baseline(results, baseline_rows, cfg.stats)
        logger.info("Wilcoxon vs baseline (Holm-Bonferroni Ξ±=%s): %s", cfg.stats.alpha, p_adj)

    if args.plots:
        plot_path = save_scatter(results, args.output.parent)
        logger.info("Saved plot β†’ %s", plot_path)

if __name__ == "__main__":
    main()