videre-core 0.37.0

Shared SQLite, caching, and search helpers for the videre media library CLI
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
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
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};
use std::fmt;

pub const EVALUATION_PROTOCOL_VERSION: u32 = 1;

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct LabeledFace {
    pub face_id: i64,
    pub identity: String,
}

impl LabeledFace {
    pub fn new(face_id: i64, identity: impl Into<String>) -> Self {
        Self {
            face_id,
            identity: identity.into(),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ClusterPrediction {
    pub face_id: i64,
    pub cluster_id: Option<i64>,
}

impl ClusterPrediction {
    pub fn new(face_id: i64, cluster_id: Option<i64>) -> Self {
        Self {
            face_id,
            cluster_id,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct SuggestionPrediction {
    pub face_id: i64,
    pub identity: Option<String>,
    pub confidence: f64,
}

impl SuggestionPrediction {
    pub fn new<S: Into<String>>(face_id: i64, identity: Option<S>, confidence: f64) -> Self {
        Self {
            face_id,
            identity: identity.map(Into::into),
            confidence,
        }
    }
}

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct CandidatePredictions {
    pub clusters: Vec<ClusterPrediction>,
    pub suggestions: Vec<SuggestionPrediction>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ClusteringMetrics {
    pub labeled_faces: usize,
    pub labeled_identities: usize,
    pub predicted_clusters: usize,
    pub true_positive_pairs: u64,
    pub false_positive_pairs: u64,
    pub false_negative_pairs: u64,
    pub pair_precision: Option<f64>,
    pub pair_recall: Option<f64>,
    pub pair_f1: Option<f64>,
    pub mixed_clusters: usize,
    pub fragmented_identities: usize,
    pub unassigned_labeled_faces: usize,
    pub unassigned_rate: f64,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct SuggestionMetrics {
    pub correct: usize,
    pub incorrect: usize,
    pub not_suggested: usize,
    pub precision: Option<f64>,
    pub coverage: f64,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct DatasetEvaluation {
    pub protocol_version: u32,
    pub clustering: ClusteringMetrics,
    pub suggestions: Option<SuggestionMetrics>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EvaluationError {
    NoLabeledFaces,
    DuplicateTruthFaceId(i64),
    DuplicateClusterPrediction(i64),
    DuplicateSuggestionPrediction(i64),
    MissingClusterPrediction(i64),
    UnknownPredictionFaceId(i64),
    NonFiniteSuggestionConfidence(i64),
    SuggestionConfidenceOutOfRange(i64),
}

impl fmt::Display for EvaluationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NoLabeledFaces => write!(f, "no confirmed labeled faces are available"),
            Self::DuplicateTruthFaceId(id) => write!(f, "duplicate truth face id {id}"),
            Self::DuplicateClusterPrediction(id) => {
                write!(f, "duplicate cluster prediction for face {id}")
            }
            Self::DuplicateSuggestionPrediction(id) => {
                write!(f, "duplicate suggestion prediction for face {id}")
            }
            Self::MissingClusterPrediction(id) => {
                write!(f, "missing cluster prediction for labeled face {id}")
            }
            Self::UnknownPredictionFaceId(id) => {
                write!(f, "prediction references unknown face {id}")
            }
            Self::NonFiniteSuggestionConfidence(id) => {
                write!(f, "suggestion confidence for face {id} is not finite")
            }
            Self::SuggestionConfidenceOutOfRange(id) => {
                write!(f, "suggestion confidence for face {id} is outside 0..=1")
            }
        }
    }
}

impl std::error::Error for EvaluationError {}

fn choose_two(n: usize) -> u64 {
    let n = n as u64;
    n.saturating_mul(n.saturating_sub(1)) / 2
}

fn ratio(numerator: u64, denominator: u64) -> Option<f64> {
    (denominator != 0).then(|| numerator as f64 / denominator as f64)
}

pub fn evaluate_candidate(
    labels: &[LabeledFace],
    predictions: &CandidatePredictions,
) -> Result<DatasetEvaluation, EvaluationError> {
    if labels.is_empty() {
        return Err(EvaluationError::NoLabeledFaces);
    }

    let mut truth = BTreeMap::new();
    for label in labels {
        if truth
            .insert(label.face_id, label.identity.as_str())
            .is_some()
        {
            return Err(EvaluationError::DuplicateTruthFaceId(label.face_id));
        }
    }

    let mut clusters = BTreeMap::new();
    for prediction in &predictions.clusters {
        if !truth.contains_key(&prediction.face_id) {
            return Err(EvaluationError::UnknownPredictionFaceId(prediction.face_id));
        }
        if clusters
            .insert(prediction.face_id, prediction.cluster_id)
            .is_some()
        {
            return Err(EvaluationError::DuplicateClusterPrediction(
                prediction.face_id,
            ));
        }
    }
    for face_id in truth.keys() {
        if !clusters.contains_key(face_id) {
            return Err(EvaluationError::MissingClusterPrediction(*face_id));
        }
    }

    let mut suggestions = BTreeMap::new();
    for prediction in &predictions.suggestions {
        if !truth.contains_key(&prediction.face_id) {
            return Err(EvaluationError::UnknownPredictionFaceId(prediction.face_id));
        }
        if !prediction.confidence.is_finite() {
            return Err(EvaluationError::NonFiniteSuggestionConfidence(
                prediction.face_id,
            ));
        }
        if !(0.0..=1.0).contains(&prediction.confidence) {
            return Err(EvaluationError::SuggestionConfidenceOutOfRange(
                prediction.face_id,
            ));
        }
        if suggestions.insert(prediction.face_id, prediction).is_some() {
            return Err(EvaluationError::DuplicateSuggestionPrediction(
                prediction.face_id,
            ));
        }
    }

    let mut identity_counts: BTreeMap<&str, usize> = BTreeMap::new();
    let mut cluster_counts: BTreeMap<i64, usize> = BTreeMap::new();
    let mut cell_counts: BTreeMap<(i64, &str), usize> = BTreeMap::new();
    let mut cluster_identities: BTreeMap<i64, BTreeSet<&str>> = BTreeMap::new();
    let mut identity_outcomes: BTreeMap<&str, BTreeSet<Option<i64>>> = BTreeMap::new();
    let mut unassigned_labeled_faces = 0usize;

    for (face_id, identity) in &truth {
        *identity_counts.entry(identity).or_default() += 1;
        let cluster_id = clusters[face_id];
        identity_outcomes
            .entry(identity)
            .or_default()
            .insert(cluster_id);
        match cluster_id {
            Some(cluster_id) => {
                *cluster_counts.entry(cluster_id).or_default() += 1;
                *cell_counts.entry((cluster_id, identity)).or_default() += 1;
                cluster_identities
                    .entry(cluster_id)
                    .or_default()
                    .insert(identity);
            }
            None => unassigned_labeled_faces += 1,
        }
    }

    let true_positive_pairs = cell_counts.values().map(|count| choose_two(*count)).sum();
    let predicted_positive_pairs: u64 = cluster_counts
        .values()
        .map(|count| choose_two(*count))
        .sum();
    let actual_positive_pairs: u64 = identity_counts
        .values()
        .map(|count| choose_two(*count))
        .sum();
    let false_positive_pairs = predicted_positive_pairs - true_positive_pairs;
    let false_negative_pairs = actual_positive_pairs - true_positive_pairs;
    let pair_precision = ratio(true_positive_pairs, predicted_positive_pairs);
    let pair_recall = ratio(true_positive_pairs, actual_positive_pairs);
    let pair_f1 = match (pair_precision, pair_recall) {
        (Some(precision), Some(recall)) if precision + recall > 0.0 => {
            Some(2.0 * precision * recall / (precision + recall))
        }
        (Some(_), Some(_)) => Some(0.0),
        _ => None,
    };

    let clustering = ClusteringMetrics {
        labeled_faces: truth.len(),
        labeled_identities: identity_counts.len(),
        predicted_clusters: cluster_counts.len(),
        true_positive_pairs,
        false_positive_pairs,
        false_negative_pairs,
        pair_precision,
        pair_recall,
        pair_f1,
        mixed_clusters: cluster_identities
            .values()
            .filter(|identities| identities.len() > 1)
            .count(),
        fragmented_identities: identity_outcomes
            .values()
            .filter(|outcomes| outcomes.len() > 1)
            .count(),
        unassigned_labeled_faces,
        unassigned_rate: unassigned_labeled_faces as f64 / truth.len() as f64,
    };

    let suggestion_metrics = if predictions.suggestions.is_empty() {
        None
    } else {
        let mut correct = 0usize;
        let mut incorrect = 0usize;
        let mut explicit_none = 0usize;
        for (face_id, prediction) in &suggestions {
            match prediction.identity.as_deref() {
                Some(identity) if Some(identity) == truth.get(face_id).copied() => correct += 1,
                Some(_) => incorrect += 1,
                None => explicit_none += 1,
            }
        }
        let suggested = correct + incorrect;
        let not_suggested = truth.len() - suggestions.len() + explicit_none;
        Some(SuggestionMetrics {
            correct,
            incorrect,
            not_suggested,
            precision: (suggested != 0).then(|| correct as f64 / suggested as f64),
            coverage: suggested as f64 / truth.len() as f64,
        })
    };

    Ok(DatasetEvaluation {
        protocol_version: EVALUATION_PROTOCOL_VERSION,
        clustering,
        suggestions: suggestion_metrics,
    })
}

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

    fn fixture() -> (Vec<LabeledFace>, CandidatePredictions) {
        (
            vec![
                LabeledFace::new(1, "ada"),
                LabeledFace::new(2, "ada"),
                LabeledFace::new(3, "grace"),
                LabeledFace::new(4, "grace"),
            ],
            CandidatePredictions {
                clusters: vec![
                    ClusterPrediction::new(1, Some(10)),
                    ClusterPrediction::new(2, Some(10)),
                    ClusterPrediction::new(3, Some(10)),
                    ClusterPrediction::new(4, None),
                ],
                suggestions: vec![
                    SuggestionPrediction::new(1, Some("ada"), 0.95),
                    SuggestionPrediction::new(2, None::<String>, 0.40),
                    SuggestionPrediction::new(3, Some("ada"), 0.80),
                    SuggestionPrediction::new(4, None::<String>, 0.30),
                ],
            },
        )
    }

    #[test]
    fn evaluates_cluster_and_suggestion_predictions() {
        let (labels, predictions) = fixture();
        let report = evaluate_candidate(&labels, &predictions).unwrap();

        assert_eq!(report.clustering.true_positive_pairs, 1);
        assert_eq!(report.clustering.false_positive_pairs, 2);
        assert_eq!(report.clustering.false_negative_pairs, 1);
        assert_eq!(report.clustering.mixed_clusters, 1);
        assert_eq!(report.clustering.fragmented_identities, 1);
        assert_eq!(report.clustering.unassigned_labeled_faces, 1);
        assert_eq!(report.clustering.pair_precision, Some(1.0 / 3.0));
        assert_eq!(report.clustering.pair_recall, Some(0.5));
        assert_eq!(report.clustering.unassigned_rate, 0.25);

        let suggestions = report.suggestions.unwrap();
        assert_eq!(suggestions.correct, 1);
        assert_eq!(suggestions.incorrect, 1);
        assert_eq!(suggestions.not_suggested, 2);
        assert_eq!(suggestions.precision, Some(0.5));
        assert_eq!(suggestions.coverage, 0.5);
    }

    #[test]
    fn evaluation_is_independent_of_input_order() {
        let (labels, predictions) = fixture();
        let expected = evaluate_candidate(&labels, &predictions).unwrap();

        let mut reversed_labels = labels.clone();
        reversed_labels.reverse();
        let mut reversed_predictions = predictions.clone();
        reversed_predictions.clusters.reverse();
        reversed_predictions.suggestions.rotate_left(1);

        assert_eq!(
            evaluate_candidate(&reversed_labels, &reversed_predictions).unwrap(),
            expected
        );
    }

    #[test]
    fn duplicate_truth_ids_are_rejected() {
        let (mut labels, predictions) = fixture();
        labels.push(LabeledFace::new(1, "ada"));
        assert_eq!(
            evaluate_candidate(&labels, &predictions),
            Err(EvaluationError::DuplicateTruthFaceId(1))
        );
    }

    #[test]
    fn duplicate_cluster_predictions_are_rejected() {
        let (labels, mut predictions) = fixture();
        predictions
            .clusters
            .push(ClusterPrediction::new(1, Some(99)));
        assert_eq!(
            evaluate_candidate(&labels, &predictions),
            Err(EvaluationError::DuplicateClusterPrediction(1))
        );
    }

    #[test]
    fn duplicate_suggestion_predictions_are_rejected() {
        let (labels, mut predictions) = fixture();
        predictions
            .suggestions
            .push(SuggestionPrediction::new(1, Some("ada"), 0.99));
        assert_eq!(
            evaluate_candidate(&labels, &predictions),
            Err(EvaluationError::DuplicateSuggestionPrediction(1))
        );
    }

    #[test]
    fn missing_cluster_prediction_is_rejected() {
        let (labels, mut predictions) = fixture();
        predictions
            .clusters
            .retain(|prediction| prediction.face_id != 4);
        assert_eq!(
            evaluate_candidate(&labels, &predictions),
            Err(EvaluationError::MissingClusterPrediction(4))
        );
    }

    #[test]
    fn unknown_prediction_face_is_rejected() {
        let (labels, mut predictions) = fixture();
        predictions.clusters.push(ClusterPrediction::new(99, None));
        assert_eq!(
            evaluate_candidate(&labels, &predictions),
            Err(EvaluationError::UnknownPredictionFaceId(99))
        );
    }

    #[test]
    fn non_finite_suggestion_confidence_is_rejected() {
        let (labels, mut predictions) = fixture();
        predictions.suggestions[0].confidence = f64::NAN;
        assert_eq!(
            evaluate_candidate(&labels, &predictions),
            Err(EvaluationError::NonFiniteSuggestionConfidence(1))
        );
    }

    #[test]
    fn no_labeled_faces_is_rejected() {
        assert_eq!(
            evaluate_candidate(
                &[],
                &CandidatePredictions {
                    clusters: Vec::new(),
                    suggestions: Vec::new(),
                }
            ),
            Err(EvaluationError::NoLabeledFaces)
        );
    }

    #[test]
    fn pair_metrics_are_undefined_without_positive_or_predicted_pairs() {
        let labels = vec![LabeledFace::new(1, "ada"), LabeledFace::new(2, "grace")];
        let predictions = CandidatePredictions {
            clusters: vec![
                ClusterPrediction::new(1, None),
                ClusterPrediction::new(2, None),
            ],
            suggestions: vec![
                SuggestionPrediction::new(1, Some("ada"), 0.9),
                SuggestionPrediction::new(2, None::<String>, 0.1),
            ],
        };

        let report = evaluate_candidate(&labels, &predictions).unwrap();
        assert_eq!(report.clustering.pair_precision, None);
        assert_eq!(report.clustering.pair_recall, None);
        assert_eq!(report.suggestions.unwrap().precision, Some(1.0));
    }

    #[test]
    fn empty_suggestion_input_serializes_as_null() {
        let labels = vec![LabeledFace::new(1, "ada")];
        let predictions = CandidatePredictions {
            clusters: vec![ClusterPrediction::new(1, None)],
            suggestions: Vec::new(),
        };

        let report = evaluate_candidate(&labels, &predictions).unwrap();
        assert_eq!(report.suggestions, None);
        let json = serde_json::to_value(report).unwrap();
        assert_eq!(json["suggestions"], serde_json::Value::Null);
    }
}