Files
Tom Kasper 820863a9c7 Complete E1-v2 workflow: stage A ladder + control + gate, stage B variants + baseline
- Snakefile: modules -> plain include: (single namespace; fixed cross-module
  helper resolution), rule all with stage-A/stage-B target sets driven by
  flat --config stage_b_arch/budget/stride switches.
- config/main.yaml: v2 run matrix (ladder 12-grid, control 1M cnn s4), store
  excludes (DCS spike-in, in-corpus escherichia trap reads), barcoding kit
  (SQK-RBK114-96 classified during dorado basecalls), removes v1 quota.
- baseline_pipeline: CUDA branch streams per-chunk dorado emit-sam through
  minimap2 (-N 10, tags preserved) and samtools view into per-batch BAMs,
  merged with samtools cat; ROCm/slorado fallback kept.
- ml_pipeline: label parquets -> signal stores (main / windows-ablation w4 /
  trap store) -> 12 ladder trainings + shuffled control -> custom_models test
  with the trap store (TP2) -> custom_models gate over the g11 runs; stage B
  adds windows ablation, holdout/core_clade(/bacillus_group) variants and the
  dorado-fast alignment baseline on the winner's identical test split.
- scripts: bam2annotation now emits source_run (barcoded -> barcodeNN from the
  dorado tags, dataset fallback); new label_qc.py (TP1 capture counts) and
  baseline_eval.py.
- custom_models submodule: bump to test_split.parquet export commit.
- README: two-stage run instructions (dev dry-run vs runner GPU).
2026-10-15 19:54:51 +01:00

79 lines
3.1 KiB
Python

"""Alignment baseline on the identical test read set of one run (E1-v2 §4.1).
Inputs: a run's test_split.parquet (exported by `custom_models test`), the
HAC label parquet (the probe's genus truth) and the FAST label parquet (the
dorado-fast + minimap2 + LCA baseline predictions). Reads missing from the
baseline parquet (below its min-qscore / not basecalled) count as failures
while also being reported separately. Writes the baseline report next to
the run's report.json.
"""
import json
import pandas as pd
if __name__ == '__main__':
test_split_path = snakemake.input[0]
truth_labels_path = snakemake.input[1]
baseline_labels_path = snakemake.input[2]
output_path = snakemake.output[0]
run_id = str(snakemake.params.run_id)
test_split = pd.read_parquet(test_split_path)
truth_labels = pd.read_parquet(
truth_labels_path, columns=['read_id', 'genus', 'identity']
).rename(columns={'genus': 'truth_genus', 'identity': 'hac_identity'})
baseline_labels = pd.read_parquet(
baseline_labels_path, columns=['read_id', 'genus', 'identity']
).rename(columns={'genus': 'baseline_genus', 'identity': 'fast_identity'})
# identical read set: probe test reads; missing baseline calls count as failures
df = test_split.merge(truth_labels, on='read_id', how='left')
df = df.merge(baseline_labels, on='read_id', how='left')
n_test = int(df.shape[0])
n_missing = int(df['baseline_genus'].isna().sum())
df['baseline_genus'] = df['baseline_genus'].fillna('<missing>')
per_genus = {}
for genus, group in df.groupby('truth_genus'):
n = int(group.shape[0])
tp = int((group['baseline_genus'] == genus).sum())
per_genus[genus] = {
'n_reads': n,
'n_correct': tp,
'recall': tp / n if n else 0.0,
}
confusion = {}
for (truth, pred), n in df.groupby(['truth_genus', 'baseline_genus']).size().items():
confusion.setdefault(truth, {})[pred] = int(n)
payload = {
'run_id': run_id,
'baseline': 'dorado-fast + minimap2 + LCA (single best-hit genus)',
'n_test_reads': n_test,
'n_missing_baseline_calls': n_missing,
'missing_call_rate': n_missing / n_test if n_test else 0.0,
'closed': {
'recall_micro': (
float(((df['baseline_genus'] == df['truth_genus'])).sum()) / n_test
if n_test else 0.0
),
'recall_macro': float(pd.Series({g: m['recall'] for g, m in per_genus.items()}).mean()),
'per_genus': per_genus,
},
'confusion': confusion,
'fast_identity': {
'mean': float(df['fast_identity'].mean()),
'min': float(df['fast_identity'].min()),
'max': float(df['fast_identity'].max()),
},
}
with open(output_path, 'w') as out:
json.dump(payload, out, indent=2, sort_keys=True)
print(f'baseline report -> {output_path}')
print(f' recall_macro: {payload["closed"]["recall_macro"]:.3f} '
f'(micro {payload["closed"]["recall_micro"]:.3f}, '
f'{n_missing}/{n_test} missing calls counted as failures)')