sightloom-analysis 0.1.7

Zone analytics, pattern miners, and statistical anomalies for SightLoom
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
//! Graph / multi-camera **relational** anomaly backend.
//!
//! Pure-Rust rules over host-prepared [`AnalysisSeries`] + optional
//! [`CameraGraph`] travel constraints. Complements statistical / iForest /
//! OCSVM backends without depending on `sightloom-reid` (facade maps topology).

#![allow(
    clippy::cast_possible_truncation,
    clippy::cast_precision_loss,
    clippy::cast_sign_loss
)]

extern crate alloc;

use crate::anomaly::{AnomalyEvent, AnomalyReason, Severity};
use crate::anomaly_backend::AnomalyDetector;
use crate::input::{AnalysisSeries, PairSample, RouteSample, TimedSubjectEvent};
use alloc::{vec, vec::Vec};
use sightloom_core::{AnomalyId, EventId, MediaTime, SourceId, SubjectId, ZoneId};

/// Directed camera hop constraint (analysis-local; mirrors re-id topology shape).
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct GraphEdge {
    /// From camera.
    pub from: SourceId,
    /// To camera.
    pub to: SourceId,
    /// Minimum plausible travel time (nanoseconds).
    pub min_travel_ns: i64,
    /// Optional maximum travel time.
    pub max_travel_ns: Option<i64>,
}

/// Sparse camera graph used for impossible-hop checks.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct CameraGraph {
    edges: Vec<GraphEdge>,
    /// When true, unknown edges are impossible (strict topology).
    pub strict_unknown: bool,
}

impl CameraGraph {
    /// Empty graph.
    #[must_use]
    pub const fn new(strict_unknown: bool) -> Self {
        Self {
            edges: Vec::new(),
            strict_unknown,
        }
    }

    /// Adds or replaces a directed edge.
    pub fn set_edge_window(
        &mut self,
        from: SourceId,
        to: SourceId,
        min_travel_ns: i64,
        max_travel_ns: Option<i64>,
    ) {
        if let Some(edge) = self.edges.iter_mut().find(|e| e.from == from && e.to == to) {
            edge.min_travel_ns = min_travel_ns;
            edge.max_travel_ns = max_travel_ns;
        } else {
            self.edges.push(GraphEdge {
                from,
                to,
                min_travel_ns,
                max_travel_ns,
            });
        }
    }

    /// Bidirectional edges with the same window.
    pub fn set_bidirectional_window(
        &mut self,
        a: SourceId,
        b: SourceId,
        min_travel_ns: i64,
        max_travel_ns: Option<i64>,
    ) {
        self.set_edge_window(a, b, min_travel_ns, max_travel_ns);
        self.set_edge_window(b, a, min_travel_ns, max_travel_ns);
    }

    /// Whether a hop is physically allowed.
    #[must_use]
    pub fn allows_hop(&self, from: SourceId, to: SourceId, elapsed_ns: i64) -> bool {
        if from == to {
            return true;
        }
        match self.edges.iter().find(|e| e.from == from && e.to == to) {
            Some(edge) if elapsed_ns < edge.min_travel_ns => false,
            Some(edge) if edge.max_travel_ns.is_some_and(|max| elapsed_ns > max) => false,
            None if self.strict_unknown => false,
            Some(_) | None => true,
        }
    }

    /// Edges in insertion order.
    #[must_use]
    pub fn edges(&self) -> &[GraphEdge] {
        &self.edges
    }
}

/// Configuration for relational / graph anomaly detection.
#[derive(Clone, Copy, Debug, PartialEq)]
#[allow(clippy::struct_excessive_bools)]
pub struct GraphAnomalyConfig {
    /// Minimum hop samples before rare-hop scoring.
    pub min_hop_samples: usize,
    /// Baseline hop fraction below this is "rare".
    pub rare_hop_fraction: f32,
    /// Minimum pair samples before rare-pair scoring.
    pub min_pair_samples: usize,
    /// Baseline pair fraction below this is rare.
    pub rare_pair_fraction: f32,
    /// Minimum route samples before rare-route scoring.
    pub min_route_samples: usize,
    /// Baseline route fraction below this is rare.
    pub rare_route_fraction: f32,
    /// Flag topology-impossible cross-camera hops.
    pub flag_impossible_hops: bool,
    /// Flag rare camera transitions vs baseline.
    pub flag_rare_hops: bool,
    /// Flag rare co-occurrence pairs.
    pub flag_rare_pairs: bool,
    /// Flag rare zone routes.
    pub flag_rare_routes: bool,
}

impl Default for GraphAnomalyConfig {
    fn default() -> Self {
        Self {
            min_hop_samples: 8,
            rare_hop_fraction: 0.05,
            min_pair_samples: 6,
            rare_pair_fraction: 0.05,
            min_route_samples: 6,
            rare_route_fraction: 0.05,
            flag_impossible_hops: true,
            flag_rare_hops: true,
            flag_rare_pairs: true,
            flag_rare_routes: true,
        }
    }
}

/// One observed subject hop between cameras.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct CameraHop {
    /// Subject when known.
    pub subject_id: Option<SubjectId>,
    /// From camera.
    pub from: SourceId,
    /// To camera.
    pub to: SourceId,
    /// Elapsed nanoseconds between sightings.
    pub elapsed_ns: i64,
    /// Arrival time.
    pub at_ns: i64,
    /// Optional evidence at destination.
    pub event_id: Option<EventId>,
}

/// Relational baseline histograms.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct GraphBaseline {
    /// `(from, to, count)` directed hop histogram.
    pub hop_counts: Vec<(SourceId, SourceId, u32)>,
    /// Total hops.
    pub hop_n: usize,
    /// Normalized co-occurrence pairs `(a, b, count)` with `a.0 <= b.0`.
    pub pair_counts: Vec<(SubjectId, SubjectId, u32)>,
    /// Total pairs.
    pub pair_n: usize,
    /// Zone route keys as sorted zone id lists + count.
    pub route_counts: Vec<(Vec<ZoneId>, u32)>,
    /// Total routes.
    pub route_n: usize,
}

impl GraphBaseline {
    /// Fraction of hops on `from → to`.
    #[must_use]
    pub fn hop_fraction(&self, from: SourceId, to: SourceId) -> f32 {
        if self.hop_n == 0 {
            return 0.0;
        }
        let c = self
            .hop_counts
            .iter()
            .find(|(a, b, _)| *a == from && *b == to)
            .map_or(0, |(_, _, n)| *n);
        c as f32 / self.hop_n as f32
    }

    /// Fraction of co-occurrence pairs.
    #[must_use]
    pub fn pair_fraction(&self, a: SubjectId, b: SubjectId) -> f32 {
        if self.pair_n == 0 {
            return 0.0;
        }
        let (lo, hi) = order_subjects(a, b);
        let c = self
            .pair_counts
            .iter()
            .find(|(x, y, _)| *x == lo && *y == hi)
            .map_or(0, |(_, _, n)| *n);
        c as f32 / self.pair_n as f32
    }

    /// Fraction of identical zone routes.
    #[must_use]
    pub fn route_fraction(&self, zones: &[ZoneId]) -> f32 {
        if self.route_n == 0 {
            return 0.0;
        }
        let c = self
            .route_counts
            .iter()
            .find(|(z, _)| z.as_slice() == zones)
            .map_or(0, |(_, n)| *n);
        c as f32 / self.route_n as f32
    }
}

/// Extracts consecutive cross-camera hops from timed subject events.
#[must_use]
pub fn extract_camera_hops(events: &[TimedSubjectEvent]) -> Vec<CameraHop> {
    let mut by_subject: Vec<(Option<SubjectId>, Vec<TimedSubjectEvent>)> = Vec::new();
    for event in events {
        if event.source_id.is_none() {
            continue;
        }
        if let Some((_, list)) = by_subject
            .iter_mut()
            .find(|(sid, _)| *sid == event.subject_id)
        {
            list.push(*event);
        } else {
            by_subject.push((event.subject_id, vec![*event]));
        }
    }

    let mut hops = Vec::new();
    for (subject_id, mut list) in by_subject {
        list.sort_by_key(|e| e.at_ns);
        for window in list.windows(2) {
            let a = window[0];
            let b = window[1];
            let (Some(from), Some(to)) = (a.source_id, b.source_id) else {
                continue;
            };
            if from == to {
                continue;
            }
            let elapsed = b.at_ns.saturating_sub(a.at_ns);
            if elapsed <= 0 {
                continue;
            }
            hops.push(CameraHop {
                subject_id,
                from,
                to,
                elapsed_ns: elapsed,
                at_ns: b.at_ns,
                event_id: b.event_id,
            });
        }
    }
    hops
}

/// Builds relational histograms from a history series.
#[must_use]
pub fn build_graph_baseline(series: &AnalysisSeries) -> GraphBaseline {
    let mut baseline = GraphBaseline::default();

    for hop in extract_camera_hops(&series.timed) {
        bump_hop(&mut baseline.hop_counts, hop.from, hop.to);
        baseline.hop_n = baseline.hop_n.saturating_add(1);
    }

    for pair in &series.pairs {
        let (lo, hi) = order_subjects(pair.subject_a, pair.subject_b);
        bump_pair(&mut baseline.pair_counts, lo, hi);
        baseline.pair_n = baseline.pair_n.saturating_add(1);
    }

    for route in &series.routes {
        bump_route(&mut baseline.route_counts, &route.zones);
        baseline.route_n = baseline.route_n.saturating_add(1);
    }

    baseline
}

/// Detects relational anomalies in `live` against `baseline` + optional graph.
#[must_use]
pub fn detect_graph_anomalies(
    live: &AnalysisSeries,
    baseline: &GraphBaseline,
    graph: &CameraGraph,
    config: GraphAnomalyConfig,
    next_id: &mut u64,
) -> Vec<AnomalyEvent> {
    let mut out = Vec::new();
    if config.flag_impossible_hops || config.flag_rare_hops {
        out.extend(detect_hop_anomalies(
            &extract_camera_hops(&live.timed),
            baseline,
            graph,
            config,
            next_id,
        ));
    }
    if config.flag_rare_pairs {
        out.extend(detect_pair_anomalies(
            &live.pairs,
            baseline,
            config,
            next_id,
        ));
    }
    if config.flag_rare_routes {
        out.extend(detect_route_anomalies(
            &live.routes,
            baseline,
            config,
            next_id,
        ));
    }
    out
}

fn detect_hop_anomalies(
    hops: &[CameraHop],
    baseline: &GraphBaseline,
    graph: &CameraGraph,
    config: GraphAnomalyConfig,
    next_id: &mut u64,
) -> Vec<AnomalyEvent> {
    let mut out = Vec::new();
    let rare = config.rare_hop_fraction.clamp(0.0, 0.5);
    for hop in hops {
        if config.flag_impossible_hops && !graph.allows_hop(hop.from, hop.to, hop.elapsed_ns) {
            out.push(make_event(
                next_id,
                8.0,
                AnomalyReason::ImpossibleCrossCameraHop,
                hop.subject_id,
                Some(hop.to),
                hop.event_id,
                hop.at_ns,
            ));
            continue;
        }
        if !config.flag_rare_hops || baseline.hop_n < config.min_hop_samples {
            continue;
        }
        let frac = baseline.hop_fraction(hop.from, hop.to);
        if frac > rare {
            continue;
        }
        // Never-seen or rare hop: score inverse rarity.
        let score = (1.0 / (frac + 1e-3)).min(20.0);
        out.push(make_event(
            next_id,
            score,
            AnomalyReason::UnusualCameraTransition,
            hop.subject_id,
            Some(hop.to),
            hop.event_id,
            hop.at_ns,
        ));
    }
    out
}

fn detect_pair_anomalies(
    pairs: &[PairSample],
    baseline: &GraphBaseline,
    config: GraphAnomalyConfig,
    next_id: &mut u64,
) -> Vec<AnomalyEvent> {
    if baseline.pair_n < config.min_pair_samples {
        return Vec::new();
    }
    let rare = config.rare_pair_fraction.clamp(0.0, 0.5);
    let mut out = Vec::new();
    for pair in pairs {
        let frac = baseline.pair_fraction(pair.subject_a, pair.subject_b);
        if frac > rare {
            continue;
        }
        let score = (1.0 / (frac + 1e-3)).min(20.0);
        out.push(make_event(
            next_id,
            score,
            AnomalyReason::UnusualCoOccurrence,
            Some(pair.subject_a),
            pair.source_id,
            pair.event_id,
            pair.at_ns,
        ));
    }
    out
}

fn detect_route_anomalies(
    routes: &[RouteSample],
    baseline: &GraphBaseline,
    config: GraphAnomalyConfig,
    next_id: &mut u64,
) -> Vec<AnomalyEvent> {
    if baseline.route_n < config.min_route_samples {
        return Vec::new();
    }
    let rare = config.rare_route_fraction.clamp(0.0, 0.5);
    let mut out = Vec::new();
    for route in routes {
        if route.zones.len() < 2 {
            continue;
        }
        let frac = baseline.route_fraction(&route.zones);
        if frac > rare {
            continue;
        }
        let score = (1.0 / (frac + 1e-3)).min(20.0);
        out.push(make_event(
            next_id,
            score,
            AnomalyReason::UnusualRoute,
            Some(route.subject_id),
            None,
            route.event_id,
            route.at_ns,
        ));
    }
    out
}

fn make_event(
    next_id: &mut u64,
    score: f32,
    reason: AnomalyReason,
    subject_id: Option<SubjectId>,
    source_id: Option<SourceId>,
    event_id: Option<EventId>,
    at_ns: i64,
) -> AnomalyEvent {
    let id = AnomalyId(*next_id);
    *next_id = next_id.saturating_add(1);
    let mut evidence = Vec::new();
    if let Some(event_id) = event_id {
        evidence.push(event_id);
    }
    let severity = if score >= 10.0 {
        Severity::Critical
    } else if score >= 6.0 {
        Severity::High
    } else if score >= 3.5 {
        Severity::Medium
    } else {
        Severity::Low
    };
    AnomalyEvent {
        anomaly_id: id,
        score: if score.is_finite() { score } else { 100.0 },
        severity,
        reasons: vec![reason],
        evidence,
        subject_id,
        source_id,
        at: MediaTime::new(at_ns, 1_000_000_000).unwrap_or_default(),
    }
}

fn order_subjects(a: SubjectId, b: SubjectId) -> (SubjectId, SubjectId) {
    if a.0 <= b.0 { (a, b) } else { (b, a) }
}

fn bump_hop(counts: &mut Vec<(SourceId, SourceId, u32)>, from: SourceId, to: SourceId) {
    if let Some(slot) = counts.iter_mut().find(|(a, b, _)| *a == from && *b == to) {
        slot.2 = slot.2.saturating_add(1);
    } else {
        counts.push((from, to, 1));
    }
}

fn bump_pair(counts: &mut Vec<(SubjectId, SubjectId, u32)>, a: SubjectId, b: SubjectId) {
    if let Some(slot) = counts.iter_mut().find(|(x, y, _)| *x == a && *y == b) {
        slot.2 = slot.2.saturating_add(1);
    } else {
        counts.push((a, b, 1));
    }
}

fn bump_route(counts: &mut Vec<(Vec<ZoneId>, u32)>, zones: &[ZoneId]) {
    if let Some(slot) = counts.iter_mut().find(|(z, _)| z.as_slice() == zones) {
        slot.1 = slot.1.saturating_add(1);
    } else {
        counts.push((zones.to_vec(), 1));
    }
}

/// Graph / relational detector implementing [`AnomalyDetector`].
#[derive(Clone, Debug, Default)]
pub struct GraphRelationalDetector {
    /// Detection config.
    pub config: GraphAnomalyConfig,
    /// Camera travel graph (impossible hop checks).
    pub graph: CameraGraph,
    /// Fitted baseline.
    pub baseline: Option<GraphBaseline>,
}

impl GraphRelationalDetector {
    /// Creates with config and empty graph.
    #[must_use]
    pub fn new(config: GraphAnomalyConfig) -> Self {
        Self {
            config,
            graph: CameraGraph::new(true),
            baseline: None,
        }
    }

    /// Creates with config and camera graph.
    #[must_use]
    pub fn with_graph(config: GraphAnomalyConfig, graph: CameraGraph) -> Self {
        Self {
            config,
            graph,
            baseline: None,
        }
    }

    /// Replaces the camera graph.
    pub fn set_graph(&mut self, graph: CameraGraph) {
        self.graph = graph;
    }
}

impl AnomalyDetector for GraphRelationalDetector {
    type Error = &'static str;

    fn fit(&mut self, history: &AnalysisSeries) -> Result<(), Self::Error> {
        self.baseline = Some(build_graph_baseline(history));
        Ok(())
    }

    fn detect(
        &mut self,
        live: &AnalysisSeries,
        next_id: &mut u64,
    ) -> Result<Vec<AnomalyEvent>, Self::Error> {
        let baseline = self
            .baseline
            .clone()
            .unwrap_or_else(|| build_graph_baseline(live));
        Ok(detect_graph_anomalies(
            live,
            &baseline,
            &self.graph,
            self.config,
            next_id,
        ))
    }
}

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

    fn cam(id: u32) -> SourceId {
        SourceId(id)
    }

    fn sub(id: u64) -> SubjectId {
        SubjectId(id)
    }

    fn timed(subject: u64, source: u32, at_ns: i64) -> TimedSubjectEvent {
        TimedSubjectEvent {
            subject_id: Some(sub(subject)),
            source_id: Some(cam(source)),
            at_ns,
            event_id: Some(EventId(at_ns as u64)),
            kind_tag: 0,
        }
    }

    #[test]
    fn extract_hops_skips_same_camera() {
        let events = [timed(1, 1, 1_000), timed(1, 1, 2_000), timed(1, 2, 5_000)];
        let hops = extract_camera_hops(&events);
        assert_eq!(hops.len(), 1);
        assert_eq!(hops[0].from, cam(1));
        assert_eq!(hops[0].to, cam(2));
        assert_eq!(hops[0].elapsed_ns, 3_000);
    }

    #[test]
    fn impossible_hop_flagged() {
        let mut graph = CameraGraph::new(true);
        // Cam1 → Cam2 requires at least 10s.
        graph.set_edge_window(cam(1), cam(2), 10_000_000_000, None);

        let mut history = AnalysisSeries::default();
        for i in 0..10 {
            history.timed.push(timed(1, 1, i * 20_000_000_000));
            history
                .timed
                .push(timed(1, 2, i * 20_000_000_000 + 15_000_000_000));
        }
        let baseline = build_graph_baseline(&history);

        let mut live = AnalysisSeries::default();
        // Too fast: 1s travel.
        live.timed.push(timed(1, 1, 100_000_000_000));
        live.timed.push(timed(1, 2, 101_000_000_000));

        let mut next_id = 1;
        let found = detect_graph_anomalies(
            &live,
            &baseline,
            &graph,
            GraphAnomalyConfig {
                flag_rare_hops: false,
                flag_rare_pairs: false,
                flag_rare_routes: false,
                ..GraphAnomalyConfig::default()
            },
            &mut next_id,
        );
        assert!(
            found
                .iter()
                .any(|a| a.reasons.contains(&AnomalyReason::ImpossibleCrossCameraHop)),
            "{found:?}"
        );
    }

    #[test]
    fn rare_hop_flagged_against_baseline() {
        let mut history = AnalysisSeries::default();
        // Baseline: only 1→2 hops (distinct subjects so no reverse hop).
        for i in 0_i64..12 {
            history
                .timed
                .push(timed(i as u64 + 1, 1, i * 10_000_000_000));
            history
                .timed
                .push(timed(i as u64 + 1, 2, i * 10_000_000_000 + 5_000_000_000));
        }
        let baseline = build_graph_baseline(&history);
        assert_eq!(baseline.hop_n, 12);
        assert!((baseline.hop_fraction(cam(1), cam(2)) - 1.0).abs() < 1e-5);

        let mut live = AnalysisSeries::default();
        // Novel 1→3 hop.
        live.timed.push(timed(1, 1, 200_000_000_000));
        live.timed.push(timed(1, 3, 205_000_000_000));

        let mut next_id = 1;
        let found = detect_graph_anomalies(
            &live,
            &baseline,
            &CameraGraph::new(false),
            GraphAnomalyConfig {
                flag_impossible_hops: false,
                flag_rare_pairs: false,
                flag_rare_routes: false,
                ..GraphAnomalyConfig::default()
            },
            &mut next_id,
        );
        assert!(
            found
                .iter()
                .any(|a| a.reasons.contains(&AnomalyReason::UnusualCameraTransition)),
            "{found:?}"
        );
    }

    #[test]
    fn rare_pair_and_route() {
        let mut history = AnalysisSeries::default();
        for i in 0..10 {
            history.pairs.push(PairSample {
                subject_a: sub(1),
                subject_b: sub(2),
                source_id: Some(cam(1)),
                at_ns: i * 1_000,
                event_id: None,
            });
            history.routes.push(RouteSample {
                subject_id: sub(1),
                zones: vec![ZoneId(1), ZoneId(2)],
                at_ns: i * 1_000,
                event_id: None,
            });
        }
        let baseline = build_graph_baseline(&history);

        let mut live = AnalysisSeries::default();
        live.pairs.push(PairSample {
            subject_a: sub(1),
            subject_b: sub(9),
            source_id: Some(cam(1)),
            at_ns: 99_000,
            event_id: None,
        });
        live.routes.push(RouteSample {
            subject_id: sub(1),
            zones: vec![ZoneId(9), ZoneId(8)],
            at_ns: 99_000,
            event_id: None,
        });

        let mut next_id = 1;
        let found = detect_graph_anomalies(
            &live,
            &baseline,
            &CameraGraph::new(false),
            GraphAnomalyConfig {
                flag_impossible_hops: false,
                flag_rare_hops: false,
                ..GraphAnomalyConfig::default()
            },
            &mut next_id,
        );
        assert!(
            found
                .iter()
                .any(|a| a.reasons.contains(&AnomalyReason::UnusualCoOccurrence))
        );
        assert!(
            found
                .iter()
                .any(|a| a.reasons.contains(&AnomalyReason::UnusualRoute))
        );
    }

    #[test]
    fn detector_trait_fit_detect() {
        let mut det = GraphRelationalDetector::new(GraphAnomalyConfig {
            flag_impossible_hops: false,
            flag_rare_pairs: false,
            flag_rare_routes: false,
            ..GraphAnomalyConfig::default()
        });
        let mut history = AnalysisSeries::default();
        for i in 0_i64..12 {
            history
                .timed
                .push(timed(i as u64 + 1, 1, i * 10_000_000_000));
            history
                .timed
                .push(timed(i as u64 + 1, 2, i * 10_000_000_000 + 5_000_000_000));
        }
        det.fit(&history).unwrap();
        let mut live = AnalysisSeries::default();
        live.timed.push(timed(1, 1, 500_000_000_000));
        live.timed.push(timed(1, 3, 505_000_000_000));
        let mut next_id = 1;
        let found = det.detect(&live, &mut next_id).unwrap();
        assert!(!found.is_empty());
    }
}