vernier-cli 0.0.3

Command-line driver for the vernier evaluation library
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
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
//! `vernier eval --manifest` — partitioned dispatch path (ADR-0046).
//!
//! Branched out of [`super::eval::run`] so the un-partitioned lane
//! stays byte-for-byte identical to the v0.2 release. The two lanes
//! share `EvaluateParams` construction and the same kernel entry
//! points; only matching-output consumption differs (this lane runs
//! `partition::evaluate_partitioned` instead of `accumulate +
//! summarize_detection`).
//!
//! This module deliberately does not link `vernier-ffi`. Per ADR-0015
//! the CLI is a pure-Rust binary; it calls vernier-core entry points
//! directly and skips PyO3 entirely.

use std::collections::HashMap;
use std::fs;
use std::io::{self, Write};
use std::path::Path;

use vernier_core::accumulate::sort_max_dets;
use vernier_core::boundary_parity::BOUNDARY_DILATION_RATIO_DEFAULT;
use vernier_core::dataset::ImageId;
use vernier_core::lrp::{self, LrpKernelMarker, LrpParams};
use vernier_core::manifest;
use vernier_core::manifest_csv;
use vernier_core::parity::iou_thresholds;
use vernier_core::partition::{
    self, evaluate_partitioned_lrp, image_id_to_idx as build_image_id_to_idx, GridDims, KeyKind,
    PartitionSpec, PartitionedLrpReport, SummaryPlan,
};
use vernier_core::similarity::{BboxIou, BoundaryIou, OksSimilarity, SegmIou};
use vernier_core::{
    evaluate_bbox, evaluate_boundary, evaluate_keypoints, evaluate_segm, AreaRange, CocoDataset,
    CocoDetections, EvalError, EvalGrid, EvaluateParams, ParityMode,
};

use crate::cli::{EmitDestination, EvalArgs, IouTypeArg, MetricArg};
use crate::error::CliError;
use crate::format::{registry, EvalArtifact, FormatContext, FormatName, Formatter};

// Re-exposed so eval.rs / aggregate.rs can reuse the same atomic-write
// machinery without duplicating it. Kept in eval.rs as the canonical
// implementation per ADR-0015.
use super::eval::{write_atomic, DETECTION_MAX_DETS_DEFAULT, KEYPOINTS_MAX_DETS_DEFAULT};

// Re-import the warning-printing path so the partitioned lane mirrors
// the existing CLI stderr discipline.
use vernier_core::manifest::ManifestWarning as CoreWarning;

/// Run the partitioned-eval lane. Caller has already validated
/// `args.manifest.is_some()` and produced the parsed emit list.
pub(crate) fn run(args: &EvalArgs) -> Result<(), CliError> {
    let emits = args.validate()?;
    let parity_mode: ParityMode = args.parity_mode.into();
    let use_cats = args.effective_use_cats();

    if matches!(args.metric, MetricArg::Olrp) {
        return run_lrp(args, &emits, parity_mode, use_cats);
    }

    let inputs = load_partitioned_inputs(args)?;

    let grid = run_kernel(
        args.iou_type,
        &inputs.gt,
        &inputs.dt,
        parity_mode,
        &inputs.max_dets,
        use_cats,
        inputs.dilation_ratio,
        inputs.sigmas,
    )?;

    let summary_kind = match args.iou_type {
        IouTypeArg::Keypoints => SummaryPlan::KeypointsDefault,
        _ => SummaryPlan::DetectionDefault,
    };

    let dims = GridDims {
        n_categories: grid.n_categories,
        n_area_ranges: grid.n_area_ranges,
        n_images: grid.n_images,
    };
    let partitioned = partition::evaluate_partitioned(
        &grid.eval_imgs,
        dims,
        &inputs.spec,
        iou_thresholds(),
        parity_mode,
        summary_kind,
    )?;

    let ctx = FormatContext {
        iou_type: args.iou_type,
        parity_mode,
        max_dets: &inputs.max_dets,
        use_cats,
    };
    let artifact = EvalArtifact::Partitioned {
        summary: &partitioned,
        label: args.label.as_deref(),
    };
    emit_artifact(&emits, &ctx, &artifact)
}

/// Shared preamble across `run` and `run_lrp`: read GT/DT, resolve
/// kernel-specific defaults (max_dets, sigmas, dilation), parse the
/// manifest, build the `PartitionSpec`, surface warnings.
struct PartitionedInputs {
    gt: CocoDataset,
    dt: CocoDetections,
    max_dets: Vec<usize>,
    sigmas: Option<HashMap<i64, Vec<f64>>>,
    dilation_ratio: f64,
    spec: PartitionSpec,
}

fn load_partitioned_inputs(args: &EvalArgs) -> Result<PartitionedInputs, CliError> {
    let parsed_max_dets = args.parsed_max_dets()?;
    let mut max_dets: Vec<usize> = match (parsed_max_dets, args.iou_type) {
        (Some(d), _) => d,
        (None, IouTypeArg::Keypoints) => KEYPOINTS_MAX_DETS_DEFAULT.to_vec(),
        (None, _) => DETECTION_MAX_DETS_DEFAULT.to_vec(),
    };
    sort_max_dets(&mut max_dets);

    let gt_bytes = fs::read(&args.gt).map_err(|source| CliError::InputRead {
        path: args.gt.clone(),
        source,
    })?;
    let dt_bytes = fs::read(&args.dt).map_err(|source| CliError::InputRead {
        path: args.dt.clone(),
        source,
    })?;
    let gt = CocoDataset::from_json_bytes(&gt_bytes)?;
    let dt = CocoDetections::from_json_bytes(&dt_bytes)?;

    let sigmas = match (&args.sigmas, args.iou_type) {
        (Some(path), IouTypeArg::Keypoints) => Some(load_sigmas(path)?),
        (Some(_), _) => {
            return Err(CliError::Validation(
                "--sigmas is only valid with --iou-type keypoints".into(),
            ));
        }
        (None, _) => None,
    };

    let dilation_ratio = match (args.dilation_ratio, args.iou_type) {
        (Some(d), IouTypeArg::Boundary) => d,
        (None, IouTypeArg::Boundary) => BOUNDARY_DILATION_RATIO_DEFAULT,
        _ => 0.0,
    };

    let image_id_to_idx = build_image_id_to_idx(&gt);

    let manifest_path = args.manifest.as_ref().ok_or_else(|| {
        // Caller (eval::run) is supposed to have routed only manifest
        // dispatch here; defensive guard prevents a silent panic if
        // that ever drifts.
        CliError::Validation("internal: partitioned dispatch invoked without --manifest".into())
    })?;
    let manifest_bytes = fs::read(manifest_path).map_err(|source| CliError::InputRead {
        path: manifest_path.clone(),
        source,
    })?;
    let cross_axes = args.parsed_cross_axes()?;
    let (spec, warnings) = build_spec(
        manifest_path,
        &manifest_bytes,
        &image_id_to_idx,
        &cross_axes,
    )?;

    if !args.quiet {
        report_warnings(&warnings);
    }

    Ok(PartitionedInputs {
        gt,
        dt,
        max_dets,
        sigmas,
        dilation_ratio,
        spec,
    })
}

/// Walk the parsed `--emit` list, render the artifact to each
/// destination. Shared by the AP and LRP partition arms.
fn emit_artifact(
    emits: &[crate::cli::EmitSpec],
    ctx: &FormatContext<'_>,
    artifact: &EvalArtifact<'_>,
) -> Result<(), CliError> {
    for spec in emits {
        let formatter = lookup_formatter(spec.format).ok_or_else(|| {
            CliError::Validation(format!(
                "internal: format {:?} disappeared from registry",
                spec.format
            ))
        })?;
        match &spec.destination {
            EmitDestination::Stdout => {
                let stdout = io::stdout();
                let mut handle = stdout.lock();
                formatter.render(artifact, ctx, &mut handle)?;
            }
            EmitDestination::File(path) => {
                write_atomic(path, |w| formatter.render(artifact, ctx, w))?;
            }
        }
    }
    Ok(())
}

// Allow the same lint exception eval.rs takes; threading 8 args is the
// price of keeping the per-kernel branch in one place.
#[allow(clippy::too_many_arguments)]
fn run_kernel(
    iou_type: IouTypeArg,
    gt: &CocoDataset,
    dt: &CocoDetections,
    parity: ParityMode,
    max_dets: &[usize],
    use_cats: bool,
    dilation_ratio: f64,
    sigmas: Option<HashMap<i64, Vec<f64>>>,
) -> Result<EvalGrid, EvalError> {
    let iou_thr = iou_thresholds();
    let area: Vec<AreaRange> = iou_type.default_area_ranges();
    let max_det_top = max_dets.iter().copied().max().unwrap_or(100);
    let eval_params = EvaluateParams {
        iou_thresholds: iou_thr,
        area_ranges: &area,
        max_dets_per_image: max_det_top,
        use_cats,
        retain_iou: false,
    };
    let grid = match iou_type {
        IouTypeArg::Bbox => evaluate_bbox(gt, dt, eval_params, parity)?,
        IouTypeArg::Segm => evaluate_segm(gt, dt, eval_params, parity)?,
        IouTypeArg::Boundary => evaluate_boundary(gt, dt, eval_params, parity, dilation_ratio)?,
        IouTypeArg::Keypoints => {
            evaluate_keypoints(gt, dt, eval_params, parity, sigmas.unwrap_or_default())?
        }
    };
    Ok(grid)
}

fn build_spec(
    manifest_path: &Path,
    bytes: &[u8],
    image_id_to_idx: &HashMap<ImageId, usize>,
    cross_axes: &[Vec<String>],
) -> Result<(PartitionSpec, Vec<CoreWarning>), CliError> {
    let ext = manifest_path
        .extension()
        .and_then(|e| e.to_str())
        .map(str::to_ascii_lowercase);
    let known_ids: std::collections::HashSet<ImageId> = image_id_to_idx.keys().copied().collect();
    let parsed = match ext.as_deref() {
        Some("json") | None => manifest::parse_manifest(bytes, &known_ids, &Default::default())?,
        Some("csv") => manifest_csv::parse_csv_manifest(
            bytes,
            KeyKind::Image,
            &known_ids,
            &Default::default(),
        )?,
        Some(other) => {
            return Err(CliError::Validation(format!(
                "manifest extension {other:?} is not recognized; use .json or .csv"
            )));
        }
    };
    if !matches!(parsed.key_kind, KeyKind::Image) {
        return Err(CliError::Validation(
            "vernier eval --manifest consumes key_kind=\"image_id\" manifests; \
             a key_kind=\"result\" manifest must be routed through `vernier aggregate`"
                .into(),
        ));
    }

    let spec = PartitionSpec::build(
        parsed.key_kind,
        &parsed.per_axis_image,
        &known_ids,
        image_id_to_idx,
        cross_axes,
    )?;

    Ok((spec, parsed.warnings))
}

fn report_warnings(warnings: &[CoreWarning]) {
    if warnings.is_empty() {
        return;
    }
    let stderr = io::stderr();
    let mut handle = stderr.lock();
    for w in warnings {
        match w {
            CoreWarning::UnknownKey { key } => {
                // Best-effort write: stderr failure here is itself
                // unrecoverable; ignore so the eval continues to its
                // exit-code path.
                let _ = writeln!(
                    handle,
                    "warning: manifest references unknown key {key:?}; row skipped"
                );
            }
        }
    }
}

fn lookup_formatter(name: FormatName) -> Option<&'static dyn Formatter> {
    registry().iter().copied().find(|f| f.id() == name)
}

/// Partitioned LRP dispatch (ADR-0046 + ADR-0043). Mirrors the AP
/// arm's shape but routes through `evaluate_partitioned_lrp` — the LRP
/// kernel runs its own internal matching pass, so we never build an
/// `EvalGrid` here.
fn run_lrp(
    args: &EvalArgs,
    emits: &[crate::cli::EmitSpec],
    parity_mode: ParityMode,
    use_cats: bool,
) -> Result<(), CliError> {
    let inputs = load_partitioned_inputs(args)?;
    // LRP runs at a single `max_dets_per_image` rung — the top of the
    // ladder (mirrors the un-partitioned LRP path in `eval.rs`).
    let max_dets_per_image = inputs.max_dets.iter().copied().max().unwrap_or(100);

    // Resolve LRP params per ADR-0044 (single threshold, canonical
    // tau grid). Same shape as `run_lrp_pipeline` in `eval.rs` — the
    // partitioned and un-partitioned paths share the same kernel
    // defaults.
    let tp_threshold = lrp::tp_threshold_for(args.iou_type.kernel_kind());
    let iou_thr = [tp_threshold];
    let tau_grid = lrp::default_tau_grid();
    let area: Vec<AreaRange> = args.iou_type.default_area_ranges();
    let params = LrpParams {
        tp_threshold,
        tau_grid,
        max_dets_per_image,
        use_cats,
        iou_thresholds: &iou_thr,
        area_ranges: &area,
    };

    let partitioned = run_lrp_kernel(
        args.iou_type,
        &inputs.gt,
        &inputs.dt,
        params,
        parity_mode,
        &inputs.spec,
        inputs.dilation_ratio,
        inputs.sigmas,
    )?;

    let ctx = FormatContext {
        iou_type: args.iou_type,
        parity_mode,
        max_dets: &inputs.max_dets,
        use_cats,
    };
    let artifact = EvalArtifact::PartitionedLrp {
        summary: &partitioned,
        label: args.label.as_deref(),
    };
    emit_artifact(emits, &ctx, &artifact)
}

#[allow(clippy::too_many_arguments)]
fn run_lrp_kernel(
    iou_type: IouTypeArg,
    gt: &CocoDataset,
    dt: &CocoDetections,
    params: LrpParams<'_>,
    parity: ParityMode,
    spec: &PartitionSpec,
    dilation_ratio: f64,
    sigmas: Option<HashMap<i64, Vec<f64>>>,
) -> Result<PartitionedLrpReport, CliError> {
    let report = match iou_type {
        IouTypeArg::Bbox => evaluate_partitioned_lrp(
            gt,
            dt,
            &BboxIou,
            LrpKernelMarker::Bbox,
            params,
            parity,
            spec,
        ),
        IouTypeArg::Segm => evaluate_partitioned_lrp(
            gt,
            dt,
            &SegmIou,
            LrpKernelMarker::Segm,
            params,
            parity,
            spec,
        ),
        IouTypeArg::Boundary => {
            let kernel = BoundaryIou { dilation_ratio };
            evaluate_partitioned_lrp(
                gt,
                dt,
                &kernel,
                LrpKernelMarker::Boundary,
                params,
                parity,
                spec,
            )
        }
        IouTypeArg::Keypoints => {
            let kernel = OksSimilarity::new(sigmas.unwrap_or_default());
            evaluate_partitioned_lrp(
                gt,
                dt,
                &kernel,
                LrpKernelMarker::Keypoints,
                params,
                parity,
                spec,
            )
        }
    }
    .map_err(CliError::from)?;
    Ok(report)
}

fn load_sigmas(path: &Path) -> Result<HashMap<i64, Vec<f64>>, CliError> {
    let bytes = fs::read(path).map_err(|source| CliError::InputRead {
        path: path.to_path_buf(),
        source,
    })?;
    let parsed: HashMap<String, Vec<f64>> = serde_json::from_slice(&bytes)
        .map_err(|e| CliError::InvalidSigmas(format!("could not parse {}: {e}", path.display())))?;
    let mut out: HashMap<i64, Vec<f64>> = HashMap::with_capacity(parsed.len());
    for (k, v) in parsed {
        let key: i64 = k.parse().map_err(|_| {
            CliError::InvalidSigmas(format!(
                "sigmas key {k:?} is not a valid integer category_id"
            ))
        })?;
        out.insert(key, v);
    }
    Ok(out)
}