yscv-eval 0.1.7

Evaluation metrics (mAP, MOTA, HOTA) and dataset adapters
Documentation
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
use std::fmt::Write as FmtWrite;

use crate::EvalError;

/// Averaging strategy for multi-class F1 score.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum F1Average {
    /// Compute F1 per class and average (unweighted).
    Macro,
    /// Compute global TP/FP/FN then derive a single F1.
    Micro,
    /// Compute F1 per class and average weighted by support (class count in targets).
    Weighted,
}

/// Compute classification accuracy as the fraction of correct predictions.
///
/// Returns a value in `[0, 1]`. Returns an error if the slices have different lengths.
pub fn accuracy(predictions: &[usize], targets: &[usize]) -> Result<f32, EvalError> {
    if predictions.len() != targets.len() {
        return Err(EvalError::CountLengthMismatch {
            ground_truth: targets.len(),
            predictions: predictions.len(),
        });
    }
    if predictions.is_empty() {
        return Ok(0.0);
    }
    let correct = predictions
        .iter()
        .zip(targets.iter())
        .filter(|(p, t)| p == t)
        .count();
    Ok(correct as f32 / predictions.len() as f32)
}

/// Compute a confusion matrix for `num_classes` classes.
///
/// `result[actual][predicted]` contains the count of samples with true class
/// `actual` that were predicted as class `predicted`.
pub fn confusion_matrix(
    predictions: &[usize],
    targets: &[usize],
    num_classes: usize,
) -> Result<Vec<Vec<usize>>, EvalError> {
    if predictions.len() != targets.len() {
        return Err(EvalError::CountLengthMismatch {
            ground_truth: targets.len(),
            predictions: predictions.len(),
        });
    }
    let mut cm = vec![vec![0usize; num_classes]; num_classes];
    for (&pred, &target) in predictions.iter().zip(targets.iter()) {
        if target < num_classes && pred < num_classes {
            cm[target][pred] += 1;
        }
    }
    Ok(cm)
}

/// Compute per-class precision and recall from a confusion matrix.
///
/// Returns a `Vec` of `(precision, recall)` tuples, one per class.
/// If a class has no predictions, precision is `0.0`; if no ground truth, recall is `0.0`.
pub fn per_class_precision_recall(cm: &[Vec<usize>]) -> Vec<(f32, f32)> {
    let n = cm.len();
    let mut result = Vec::with_capacity(n);
    for c in 0..n {
        let tp = cm[c][c] as f32;
        let col_sum: f32 = cm.iter().map(|row| row[c] as f32).sum();
        let row_sum: f32 = cm[c].iter().sum::<usize>() as f32;

        let precision = if col_sum > 0.0 { tp / col_sum } else { 0.0 };
        let recall = if row_sum > 0.0 { tp / row_sum } else { 0.0 };
        result.push((precision, recall));
    }
    result
}

/// Generate a human-readable classification report (similar to scikit-learn's
/// `classification_report`).
///
/// Example output:
/// ```text
///               precision  recall  f1-score  support
///          cat      0.800   0.889     0.842        9
///          dog      0.857   0.750     0.800        8
///     accuracy                        0.824       17
/// ```
pub fn classification_report(
    predictions: &[usize],
    targets: &[usize],
    labels: &[&str],
) -> Result<String, EvalError> {
    let num_classes = labels.len();
    let cm = confusion_matrix(predictions, targets, num_classes)?;
    let pr = per_class_precision_recall(&cm);
    let acc = accuracy(predictions, targets)?;

    let max_label = labels.iter().map(|l| l.len()).max().unwrap_or(5).max(10);
    let mut report = String::new();

    writeln!(
        report,
        "{:>width$}  precision  recall  f1-score  support",
        "",
        width = max_label
    )
    .expect("write to String");

    let total_support = targets.len();

    for (i, label) in labels.iter().enumerate() {
        let (prec, rec) = pr[i];
        let f1 = if prec + rec > 0.0 {
            2.0 * prec * rec / (prec + rec)
        } else {
            0.0
        };
        let support: usize = cm[i].iter().sum();
        writeln!(
            report,
            "{:>width$}    {:.3}   {:.3}     {:.3}     {:>4}",
            label,
            prec,
            rec,
            f1,
            support,
            width = max_label
        )
        .expect("write to String");
    }

    writeln!(
        report,
        "{:>width$}                      {:.3}     {:>4}",
        "accuracy",
        acc,
        total_support,
        width = max_label
    )
    .expect("write to String");

    Ok(report)
}

/// Compute F1 score with the specified averaging strategy.
///
/// `num_classes` must be at least as large as the maximum label value + 1.
pub fn f1_score(
    predictions: &[usize],
    targets: &[usize],
    num_classes: usize,
    average: F1Average,
) -> Result<f32, EvalError> {
    if predictions.len() != targets.len() {
        return Err(EvalError::CountLengthMismatch {
            ground_truth: targets.len(),
            predictions: predictions.len(),
        });
    }

    let cm = confusion_matrix(predictions, targets, num_classes)?;

    match average {
        F1Average::Macro => {
            let pr = per_class_precision_recall(&cm);
            let mut sum_f1 = 0.0f32;
            for &(prec, rec) in &pr {
                let f1 = if prec + rec > 0.0 {
                    2.0 * prec * rec / (prec + rec)
                } else {
                    0.0
                };
                sum_f1 += f1;
            }
            Ok(sum_f1 / num_classes as f32)
        }
        F1Average::Micro => {
            let mut tp_total = 0usize;
            let mut fp_total = 0usize;
            let mut fn_total = 0usize;
            for c in 0..num_classes {
                let tp = cm[c][c];
                let fp: usize = cm.iter().map(|row| row[c]).sum::<usize>() - tp;
                let fn_c: usize = cm[c].iter().sum::<usize>() - tp;
                tp_total += tp;
                fp_total += fp;
                fn_total += fn_c;
            }
            let precision = if tp_total + fp_total > 0 {
                tp_total as f32 / (tp_total + fp_total) as f32
            } else {
                0.0
            };
            let recall = if tp_total + fn_total > 0 {
                tp_total as f32 / (tp_total + fn_total) as f32
            } else {
                0.0
            };
            if precision + recall > 0.0 {
                Ok(2.0 * precision * recall / (precision + recall))
            } else {
                Ok(0.0)
            }
        }
        F1Average::Weighted => {
            let pr = per_class_precision_recall(&cm);
            let mut weighted_f1 = 0.0f32;
            let total: usize = targets.len();
            for c in 0..num_classes {
                let support: usize = cm[c].iter().sum();
                let (prec, rec) = pr[c];
                let f1 = if prec + rec > 0.0 {
                    2.0 * prec * rec / (prec + rec)
                } else {
                    0.0
                };
                weighted_f1 += f1 * support as f32;
            }
            if total > 0 {
                Ok(weighted_f1 / total as f32)
            } else {
                Ok(0.0)
            }
        }
    }
}

/// Compute precision-recall curve from binary classification scores and labels.
///
/// Returns `(precisions, recalls, thresholds)` sorted by decreasing threshold.
pub fn precision_recall_curve(
    scores: &[f32],
    labels: &[bool],
) -> Result<(Vec<f32>, Vec<f32>, Vec<f32>), EvalError> {
    if scores.len() != labels.len() {
        return Err(EvalError::CountLengthMismatch {
            ground_truth: labels.len(),
            predictions: scores.len(),
        });
    }

    let n = scores.len();
    let total_pos = labels.iter().filter(|&&l| l).count() as f32;

    // Sort indices by score descending
    let mut indices: Vec<usize> = (0..n).collect();
    indices.sort_unstable_by(|&a, &b| {
        scores[b]
            .partial_cmp(&scores[a])
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    let mut precisions = Vec::with_capacity(n);
    let mut recalls = Vec::with_capacity(n);
    let mut thresholds = Vec::with_capacity(n);

    let mut tp = 0.0f32;

    for (rank, &i) in indices.iter().enumerate() {
        if labels[i] {
            tp += 1.0;
        }
        let predicted_pos = (rank + 1) as f32;
        precisions.push(tp / predicted_pos);
        recalls.push(if total_pos > 0.0 { tp / total_pos } else { 0.0 });
        thresholds.push(scores[i]);
    }

    Ok((precisions, recalls, thresholds))
}

/// Compute average precision (area under the precision-recall curve) using the trapezoidal rule.
///
/// Prepends the point (recall=0, precision=1) to ensure the full area is captured.
pub fn average_precision(scores: &[f32], labels: &[bool]) -> Result<f32, EvalError> {
    let (precisions, recalls, _) = precision_recall_curve(scores, labels)?;

    if recalls.is_empty() {
        return Ok(0.0);
    }

    // Prepend (recall=0, precision=1.0) as the starting point of the PR curve.
    let mut full_recalls = Vec::with_capacity(recalls.len() + 1);
    let mut full_precisions = Vec::with_capacity(precisions.len() + 1);
    full_recalls.push(0.0f32);
    full_precisions.push(1.0f32);
    full_recalls.extend_from_slice(&recalls);
    full_precisions.extend_from_slice(&precisions);

    // Trapezoidal rule over recall (which is monotonically non-decreasing)
    let mut ap = 0.0f32;
    for i in 1..full_recalls.len() {
        let dr = full_recalls[i] - full_recalls[i - 1];
        ap += dr * (full_precisions[i] + full_precisions[i - 1]) / 2.0;
    }
    Ok(ap)
}

/// Cohen's kappa coefficient measuring inter-annotator agreement.
///
/// κ = (p_o - p_e) / (1 - p_e) where p_o is observed agreement and p_e is expected agreement.
pub fn cohens_kappa(
    predictions: &[usize],
    targets: &[usize],
    num_classes: usize,
) -> Result<f32, EvalError> {
    if predictions.len() != targets.len() {
        return Err(EvalError::CountLengthMismatch {
            ground_truth: targets.len(),
            predictions: predictions.len(),
        });
    }

    let n = predictions.len();
    if n == 0 {
        return Ok(0.0);
    }

    let cm = confusion_matrix(predictions, targets, num_classes)?;
    let n_f = n as f32;

    // Observed agreement
    let p_o: f32 = (0..num_classes).map(|c| cm[c][c] as f32).sum::<f32>() / n_f;

    // Expected agreement
    let mut p_e = 0.0f32;
    for c in 0..num_classes {
        let row_sum: f32 = cm[c].iter().sum::<usize>() as f32; // true class c count
        let col_sum: f32 = cm.iter().map(|row| row[c]).sum::<usize>() as f32; // predicted class c count
        p_e += (row_sum / n_f) * (col_sum / n_f);
    }

    if (1.0 - p_e).abs() < 1e-10 {
        return Ok(1.0); // perfect agreement when p_e ≈ 1
    }

    Ok((p_o - p_e) / (1.0 - p_e))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_accuracy_perfect() {
        let preds = vec![0, 1, 2, 0, 1];
        let targets = vec![0, 1, 2, 0, 1];
        let acc = accuracy(&preds, &targets).unwrap();
        assert!((acc - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_accuracy_half() {
        let preds = vec![0, 0, 1, 1];
        let targets = vec![0, 1, 0, 1];
        let acc = accuracy(&preds, &targets).unwrap();
        assert!((acc - 0.5).abs() < 1e-6);
    }

    #[test]
    fn test_accuracy_length_mismatch() {
        assert!(accuracy(&[0, 1], &[0]).is_err());
    }

    #[test]
    fn test_confusion_matrix_basic() {
        let preds = vec![0, 0, 1, 1, 2, 2];
        let targets = vec![0, 1, 1, 2, 2, 0];
        let cm = confusion_matrix(&preds, &targets, 3).unwrap();

        // Diagonal: correct predictions.
        assert_eq!(cm[0][0], 1); // target=0, pred=0
        assert_eq!(cm[1][1], 1); // target=1, pred=1
        assert_eq!(cm[2][2], 1); // target=2, pred=2

        // Off-diagonal: misclassifications.
        assert_eq!(cm[1][0], 1); // target=1, pred=0
        assert_eq!(cm[2][1], 1); // target=2, pred=1
        assert_eq!(cm[0][2], 1); // target=0, pred=2
    }

    #[test]
    fn test_per_class_precision_recall() {
        // 2 classes: [0, 0, 1, 1] vs [0, 1, 0, 1]
        let cm = confusion_matrix(&[0, 0, 1, 1], &[0, 1, 0, 1], 2).unwrap();
        let pr = per_class_precision_recall(&cm);
        // Class 0: TP=1, FP=1 (pred=0 when target=1), FN=1 => precision=0.5, recall=0.5
        assert!((pr[0].0 - 0.5).abs() < 1e-5);
        assert!((pr[0].1 - 0.5).abs() < 1e-5);
        // Class 1: same situation
        assert!((pr[1].0 - 0.5).abs() < 1e-5);
        assert!((pr[1].1 - 0.5).abs() < 1e-5);
    }

    #[test]
    fn test_classification_report_format() {
        let preds = vec![0, 0, 1, 1, 1];
        let targets = vec![0, 1, 1, 1, 0];
        let report = classification_report(&preds, &targets, &["cat", "dog"]).unwrap();

        assert!(report.contains("precision"));
        assert!(report.contains("recall"));
        assert!(report.contains("cat"));
        assert!(report.contains("dog"));
        assert!(report.contains("accuracy"));
    }

    #[test]
    fn test_accuracy_empty() {
        let acc = accuracy(&[], &[]).unwrap();
        assert_eq!(acc, 0.0);
    }
}