vtracker 1.0.0

For tracking the relationship between group membership changes across versions.
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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
use std::collections::{HashMap, HashSet};
use std::string::ToString;

use crate::edge::EdgeKey;
use crate::graph::Graph;
use crate::model::{SankeyD3, SankeyLink, SankeyNode};
use crate::node::NodeKey;

pub struct VTracker {
    pub ver_to_idx: HashMap<String, usize>,
    pub idx_to_ver: Vec<String>,
    pub graph: Graph,

    pub uid_to_node: HashMap<String, HashSet<NodeKey>>,
    pub uid_to_edge: HashMap<String, HashSet<EdgeKey>>,

    pub str_na: String,
}

impl VTracker {
    pub fn new(versions: &Vec<String>, str_na: Option<&String>) -> VTracker {

        // Convert the versions into keys
        let mut ver_to_idx: HashMap<String, usize> = HashMap::new();
        for version in versions {
            ver_to_idx.insert(version.to_string(), ver_to_idx.len());
        }
        let idx_to_ver: Vec<String> = versions.clone();

        let str_not_available = match str_na {
            Some(str_na) => str_na.to_string(),
            None => "Not Present".to_string(),
        };

        VTracker {
            ver_to_idx,
            idx_to_ver,
            graph: Graph::new(),
            uid_to_node: HashMap::new(),
            uid_to_edge: HashMap::new(),
            str_na: str_not_available,
        }
    }

    fn add_uid_to_node(&mut self, uid: &str, key: &NodeKey) {
        let cur_set = self.uid_to_node.get_mut(uid);
        if cur_set.is_none() {
            self.uid_to_node.insert(uid.to_string(), HashSet::new());
        }
        self.uid_to_node.get_mut(uid).unwrap().insert(key.clone());
    }

    fn add_uid_to_edge(&mut self, uid: &str, key: &EdgeKey) {
        let cur_set = self.uid_to_edge.get_mut(uid);
        if cur_set.is_none() {
            self.uid_to_edge.insert(uid.to_string(), HashSet::new());
        }
        self.uid_to_edge.get_mut(uid).unwrap().insert(key.clone());
    }

    fn add_nodes(&mut self, uid: &str, ver_states: &HashMap<String, String>) -> Vec<NodeKey> {
        let mut out: Vec<NodeKey> = Vec::new();
        for version in &self.idx_to_ver {

            // Check if this uid appears in this version
            let key = match ver_states.get(version) {
                Some(state) => NodeKey::new(version, state),
                None => NodeKey::new(version, &self.str_na),
            };

            if self.graph.get_node(&key).is_none() {
                let uids: HashSet<String> = HashSet::from_iter(vec![uid.to_string()]);
                self.graph.add_node(key.clone(), uids);
            } else {
                let node = self.graph.get_node_mut(&key);
                node.uids.insert(uid.to_string());
            }
            out.push(key);
        }
        out
    }

    fn add_edges(&mut self, uid: &str, ver_states: &HashMap<String, String>) -> Vec<EdgeKey> {
        let mut out: Vec<EdgeKey> = Vec::new();
        for i in 0..&self.idx_to_ver.len() - 1 {
            let ver_from = &self.idx_to_ver[i];
            let ver_to = &self.idx_to_ver[i + 1];

            // Check if this uid appears in this or the next version
            let key_from = match ver_states.get(ver_from) {
                Some(k) => NodeKey::new(ver_from, k),
                None => NodeKey::new(ver_from, &self.str_na),
            };
            let key_to = match ver_states.get(ver_to) {
                Some(k) => NodeKey::new(ver_to, k),
                None => NodeKey::new(ver_to, &self.str_na),
            };

            // Create the edge associated with this key
            let edge_key = EdgeKey::new(&key_from, &key_to);

            if self.graph.get_edge(&edge_key).is_none() {
                let uids: HashSet<String> = HashSet::from_iter(vec![uid.to_string()]);
                self.graph.add_edge(&key_from, &key_to, uids);
            } else {
                let edge = self.graph.get_edge_mut(&edge_key);
                edge.uids.insert(uid.to_string());
            }
            out.push(edge_key);
        }
        out
    }

    pub fn add(&mut self, uid: &str, ver_states: &HashMap<String, String>) {
        let ver_states_keys: HashSet<&String> = HashSet::from_iter(ver_states.keys());
        let ver_to_idx_keys: HashSet<&String> = HashSet::from_iter(self.ver_to_idx.keys());

        // Sanity checking
        if ver_states_keys.difference(&ver_to_idx_keys).count() > 0 {
            panic!("Specified version which is not a part of this tracker.");
        }
        if self.uid_to_node.contains_key(uid) {
            panic!("Specified uid already exists in this tracker.");
        }

        // Iterate over each expected version
        let node_keys_to_add = self.add_nodes(uid, &ver_states);
        for node_key in &node_keys_to_add {
            self.add_uid_to_node(uid, node_key);
        }

        // Create each of the edges
        let edge_keys_to_add: Vec<EdgeKey> = self.add_edges(uid, &ver_states);
        for edge_key in &edge_keys_to_add {
            self.add_uid_to_edge(uid, edge_key);
        }
    }

    pub fn build_uid_paths(&self) -> (HashMap<String, HashSet<usize>>, HashMap<String, HashSet<usize>>) {
        let mut edges: HashMap<String, HashSet<usize>> = HashMap::new();
        let mut nodes: HashMap<String, HashSet<usize>> = HashMap::new();

        for uid in self.uid_to_node.keys() {
            for edge_key in self.uid_to_edge.get(uid).unwrap() {
                let edge = self.graph.get_edge(edge_key).unwrap();
                if edges.get(uid).is_none() {
                    edges.insert(uid.to_string(), HashSet::new());
                }
                edges.get_mut(uid).unwrap().insert(edge.id);
            }

            for node_key in self.uid_to_node.get(uid).unwrap() {
                let node = self.graph.get_node(node_key).unwrap();
                if nodes.get(uid).is_none() {
                    nodes.insert(uid.to_string(), HashSet::new());
                }
                nodes.get_mut(uid).unwrap().insert(node.id);
            }
        }
        (nodes, edges)
    }

    pub fn get_sankey_nodes(&self, uid_travel_node_id: &HashMap<String, HashSet<usize>>, uid_travel_edge_id: &HashMap<String, HashSet<usize>>) -> Vec<SankeyNode> {
        let mut out_nodes: Vec<SankeyNode> = Vec::new();

        // Step 2: Calculate link highlighting
        for node in &self.graph.nodes {
            let ver = &node.key.version;
            let state = &node.key.state;

            let mut node_highlight_id: HashSet<usize> = HashSet::new();
            let mut link_highlight_id: HashSet<usize> = HashSet::new();

            for uid in node.uids.iter() {
                for node_uid in uid_travel_node_id.get(uid).unwrap() {
                    node_highlight_id.insert(*node_uid);
                }
                for edge_uid in uid_travel_edge_id.get(uid).unwrap() {
                    link_highlight_id.insert(*edge_uid);
                }
            }

            let mut node_highlight_id_sorted: Vec<usize> = Vec::from_iter(node_highlight_id);
            node_highlight_id_sorted.sort();

            let mut link_highlight_id_sorted: Vec<usize> = Vec::from_iter(link_highlight_id);
            link_highlight_id_sorted.sort();

            out_nodes.push(SankeyNode {
                col: ver.to_string(),
                id: node.id,
                link_highlight_id: Vec::from_iter(link_highlight_id_sorted),
                name: state.to_string(),
                node_highlight_id: Vec::from_iter(node_highlight_id_sorted),
                total: node.uids.len(),
            });
        }

        // Sort by id
        out_nodes.sort_by(|a, b| a.id.cmp(&b.id));
        out_nodes
    }

    pub fn get_sankey_edges(&self, uid_travel_node_id: &HashMap<String, HashSet<usize>>, uid_travel_edge_id: &HashMap<String, HashSet<usize>>) -> Vec<SankeyLink> {
        let mut out: Vec<SankeyLink> = Vec::new();

        // Create each of the edges in the sankey
        for edge in &self.graph.edges {
            let mut node_highlight_id: HashSet<usize> = HashSet::new();
            let mut link_highlight_id: HashSet<usize> = HashSet::new();

            for uid in &edge.uids {
                for node_uid in uid_travel_node_id.get(uid).unwrap() {
                    node_highlight_id.insert(*node_uid);
                }
                for edge_uid in uid_travel_edge_id.get(uid).unwrap() {
                    link_highlight_id.insert(*edge_uid);
                }
            }

            let mut node_highlight_id_sorted: Vec<usize> = Vec::from_iter(node_highlight_id);
            node_highlight_id_sorted.sort();

            let mut link_highlight_id_sorted: Vec<usize> = Vec::from_iter(link_highlight_id);
            link_highlight_id_sorted.sort();

            out.push(SankeyLink {
                id: edge.id,
                link_highlight_id: Vec::from_iter(link_highlight_id_sorted),
                node_highlight_id: Vec::from_iter(node_highlight_id_sorted),
                source: edge.from_node,
                target: edge.to_node,
                value: edge.uids.len(),
            })
        }

        // Sort by id
        out.sort_by(|a, b| a.id.cmp(&b.id));
        out
    }

    pub fn as_sankey_json(&self) -> SankeyD3 {

        // Compute the uid paths
        let (uid_travel_node_id, uid_travel_edge_id) = self.build_uid_paths();

        let out_nodes = self.get_sankey_nodes(&uid_travel_node_id, &uid_travel_edge_id);
        let out_edges = self.get_sankey_edges(&uid_travel_node_id, &uid_travel_edge_id);

        let out = SankeyD3 {
            nodes: out_nodes,
            links: out_edges,
        };

        out
    }
}


#[test]
fn test_v_tracker_new() {
    let versions: Vec<String> = vec!["a".to_string(), "b".to_string(), "c".to_string()];
    let vt = VTracker::new(&versions, None);

    let mut ver_to_idx_expected: HashMap<String, usize> = HashMap::new();
    ver_to_idx_expected.insert("a".to_string(), 0);
    ver_to_idx_expected.insert("b".to_string(), 1);
    ver_to_idx_expected.insert("c".to_string(), 2);
    assert_eq!(vt.ver_to_idx, ver_to_idx_expected);

    let idx_to_ver_expected: Vec<String> = vec!["a".to_string(), "b".to_string(), "c".to_string()];
    assert_eq!(vt.idx_to_ver, idx_to_ver_expected);

    assert_eq!(vt.uid_to_node.len(), 0);
    assert_eq!(vt.uid_to_edge.len(), 0);
}

fn test_init_v_tracker() -> VTracker {
    /*
        +--------------+--------+--------------+
       |     1        |   2    |      3       |
       +--------------+--------+--------------+
       | a: x         | a: x   | a: y         |
       |              | b: y,z |              |
       | Missing: y,z |        | Missing: x,z |
       +--------------+--------+--------------+
    */

    let versions: Vec<String> = vec!["1".to_string(), "2".to_string(), "3".to_string()];
    let mut vt = VTracker::new(&versions, None);

    // Add x
    {
        let states = HashMap::from_iter(vec![
            ("1".to_string(), "a".to_string()),
            ("2".to_string(), "a".to_string()),
        ]);
        vt.add("x", &states);
    }

    // Add y
    {
        let states = HashMap::from_iter(vec![
            ("2".to_string(), "b".to_string()),
            ("3".to_string(), "a".to_string()),
        ]);
        vt.add("y", &states);
    }

    // Add z
    {
        let states = HashMap::from_iter(vec![
            ("2".to_string(), "b".to_string()),
        ]);
        vt.add("z", &states);
    }

    vt
}

#[test]
fn test_v_tracker_add() {
    let vt = test_init_v_tracker();

    // Create NodeKeys for later use
    let n_1a_key = NodeKey::new("1", "a");
    let n_2a_key = NodeKey::new("2", "a");
    let n_2b_key = NodeKey::new("2", "b");
    let n_1x_key = NodeKey::new("1", &vt.str_na);
    let n_3x_key = NodeKey::new("3", &vt.str_na);
    let n_3a_key = NodeKey::new("3", "a");

    // Check the nodes
    assert_eq!(vt.graph.nodes.len(), 6);

    // Check n_1_x
    {
        let edges_in: HashSet<EdgeKey> = HashSet::new();
        let edges_out: HashSet<EdgeKey> = vec![
            EdgeKey::new(&n_1a_key, &n_2a_key),
        ].into_iter().collect();
        let uids: HashSet<String> = vec!["x".to_string()].into_iter().collect();


        let node = vt.graph.get_node(&n_1a_key).unwrap();
        assert_eq!(node.uids, uids);
        assert_eq!(node.edge_ids_in, edges_in);
        assert_eq!(node.edge_ids_out, edges_out);
    }


    // Check n_1_yz
    {
        let edges_in: HashSet<EdgeKey> = HashSet::new();
        let edges_out: HashSet<EdgeKey> = HashSet::from_iter(vec![
            EdgeKey::new(&n_1x_key, &n_2b_key),
        ]);
        let uids: HashSet<String> = HashSet::from_iter(vec!["y".to_string(), "z".to_string()]);

        let node = vt.graph.get_node(&n_1x_key).unwrap();
        assert_eq!(node.uids, uids);
        assert_eq!(node.edge_ids_in, edges_in);
        assert_eq!(node.edge_ids_out, edges_out);
    }

    // Check n_2_x
    {
        let edges_in: HashSet<EdgeKey> = HashSet::from_iter(vec![
            EdgeKey::new(&n_1a_key, &n_2a_key),
        ]);
        let edges_out: HashSet<EdgeKey> = HashSet::from_iter(vec![
            EdgeKey::new(&n_2a_key, &n_3x_key),
        ]);
        let uids: HashSet<String> = HashSet::from_iter(vec!["x".to_string()]);

        let node = vt.graph.get_node(&n_2a_key).unwrap();
        assert_eq!(node.uids, uids);
        assert_eq!(node.edge_ids_in, edges_in);
        assert_eq!(node.edge_ids_out, edges_out);
    }

    // Check n_2_yz
    {
        let edges_in: HashSet<EdgeKey> = HashSet::from_iter(vec![
            EdgeKey::new(&n_1x_key, &n_2b_key),
        ]);
        let edges_out: HashSet<EdgeKey> = HashSet::from_iter(vec![
            EdgeKey::new(&n_2b_key, &n_3a_key),
            EdgeKey::new(&n_2b_key, &n_3x_key),
        ]);
        let uids: HashSet<String> = HashSet::from_iter(vec!["y".to_string(), "z".to_string()]);

        let node = vt.graph.get_node(&n_2b_key).unwrap();
        assert_eq!(node.uids, uids);
        assert_eq!(node.edge_ids_in, edges_in);
        assert_eq!(node.edge_ids_out, edges_out);
    }

    // Check n_3_y
    {
        let edges_in: HashSet<EdgeKey> = HashSet::from_iter(vec![
            EdgeKey::new(&n_2b_key, &n_3a_key),
        ]);
        let edges_out: HashSet<EdgeKey> = HashSet::new();
        let uids: HashSet<String> = HashSet::from_iter(vec!["y".to_string()]);

        let node = vt.graph.get_node(&n_3a_key).unwrap();
        assert_eq!(node.uids, uids);
        assert_eq!(node.edge_ids_in, edges_in);
        assert_eq!(node.edge_ids_out, edges_out);
    }

    // Check n_3_xz
    {
        let edges_in: HashSet<EdgeKey> = HashSet::from_iter(vec![
            EdgeKey::new(&n_2a_key, &n_3x_key),
            EdgeKey::new(&n_2b_key, &n_3x_key),
        ]);
        let edges_out: HashSet<EdgeKey> = HashSet::new();
        let uids: HashSet<String> = HashSet::from_iter(vec!["x".to_string(), "z".to_string()]);

        let node = vt.graph.get_node(&n_3x_key).unwrap();
        assert_eq!(node.uids, uids);
        assert_eq!(node.edge_ids_in, edges_in);
        assert_eq!(node.edge_ids_out, edges_out);
    }

    // Check the edges
    assert_eq!(vt.graph.edges.len(), 5);

    // e_1a_2a
    {
        let edge = vt.graph.get_edge(&EdgeKey::new(&n_1a_key, &n_2a_key)).unwrap();
        let uids: HashSet<String> = HashSet::from_iter(vec!["x".to_string()]);
        assert_eq!(edge.uids, uids);
    }

    // e_mis1_2b
    {
        let edge = vt.graph.get_edge(&EdgeKey::new(&n_1x_key, &n_2b_key)).unwrap();
        let uids: HashSet<String> = HashSet::from_iter(vec!["y".to_string(), "z".to_string()]);
        assert_eq!(edge.uids, uids);
    }

    // e_2a_3_mis
    {
        let edge = vt.graph.get_edge(&EdgeKey::new(&n_2a_key, &n_3x_key)).unwrap();
        let uids: HashSet<String> = HashSet::from_iter(vec!["x".to_string()]);
        assert_eq!(edge.uids, uids);
    }

    // e_2b_3_mis
    {
        let edge = vt.graph.get_edge(&EdgeKey::new(&n_2b_key, &n_3x_key)).unwrap();
        let uids: HashSet<String> = HashSet::from_iter(vec!["z".to_string()]);
        assert_eq!(edge.uids, uids);
    }

    // e_2b_3a
    {
        let edge = vt.graph.get_edge(&EdgeKey::new(&n_2b_key, &n_3a_key)).unwrap();
        let uids: HashSet<String> = HashSet::from_iter(vec!["y".to_string()]);
        assert_eq!(edge.uids, uids);
    }

    // Check the node indices
    // x
    {
        let node_idx: HashSet<NodeKey> = HashSet::from_iter(vec![n_1a_key.clone(), n_2a_key.clone(), n_3x_key.clone()]);
        assert_eq!(vt.uid_to_node.get("x").unwrap(), &node_idx);
    }

    // y
    {
        let node_idx: HashSet<NodeKey> = HashSet::from_iter(vec![n_1x_key.clone(), n_2b_key.clone(), n_3a_key.clone()]);
        assert_eq!(vt.uid_to_node.get("y").unwrap(), &node_idx);
    }

    // z
    {
        let node_idx: HashSet<NodeKey> = HashSet::from_iter(vec![n_1x_key.clone(), n_2b_key.clone(), n_3x_key.clone()]);
        assert_eq!(vt.uid_to_node.get("z").unwrap(), &node_idx);
    }

    // Check the edge indices
    // x
    {
        let edge_idx: HashSet<EdgeKey> = HashSet::from_iter(vec![
            EdgeKey::new(&n_1a_key, &n_2a_key),
            EdgeKey::new(&n_2a_key, &n_3x_key),
        ]);
        assert_eq!(vt.uid_to_edge.get("x").unwrap(), &edge_idx);
    }

    // y
    {
        let edge_idx: HashSet<EdgeKey> = HashSet::from_iter(vec![
            EdgeKey::new(&n_1x_key, &n_2b_key),
            EdgeKey::new(&n_2b_key, &n_3a_key),
        ]);
        assert_eq!(vt.uid_to_edge.get("y").unwrap(), &edge_idx);
    }

    // z
    {
        let edge_idx: HashSet<EdgeKey> = HashSet::from_iter(vec![
            EdgeKey::new(&n_1x_key, &n_2b_key),
            EdgeKey::new(&n_2b_key, &n_3x_key),
        ]);
        assert_eq!(vt.uid_to_edge.get("z").unwrap(), &edge_idx);
    }
}

#[test]
fn test_build_uid_paths() {
    let vt = test_init_v_tracker();

    // Create NodeKeys for later use
    let n_1a_key = NodeKey::new("1", "a");
    let n_2a_key = NodeKey::new("2", "a");
    let n_2b_key = NodeKey::new("2", "b");
    let n_1x_key = NodeKey::new("1", &vt.str_na);
    let n_3x_key = NodeKey::new("3", &vt.str_na);
    let n_3a_key = NodeKey::new("3", "a");

    let (nodes, edges) = vt.build_uid_paths();

    // Check node highlighting
    // x
    {
        let ids: HashSet<usize> = HashSet::from_iter(vec![
            vt.graph.get_node(&n_1a_key).unwrap().id,
            vt.graph.get_node(&n_2a_key).unwrap().id,
            vt.graph.get_node(&n_3x_key).unwrap().id,
        ]);
        assert_eq!(nodes.get("x").unwrap(), &ids);
    }

    // y
    {
        let ids: HashSet<usize> = HashSet::from_iter(vec![
            vt.graph.get_node(&n_1x_key).unwrap().id,
            vt.graph.get_node(&n_2b_key).unwrap().id,
            vt.graph.get_node(&n_3a_key).unwrap().id,
        ]);
        assert_eq!(nodes.get("y").unwrap(), &ids);
    }

    // z
    {
        let ids: HashSet<usize> = HashSet::from_iter(vec![
            vt.graph.get_node(&n_1x_key).unwrap().id,
            vt.graph.get_node(&n_2b_key).unwrap().id,
            vt.graph.get_node(&n_3x_key).unwrap().id,
        ]);
        assert_eq!(nodes.get("z").unwrap(), &ids);
    }

    // Check edge highlighting
    // x
    {
        let ids: HashSet<usize> = HashSet::from_iter(vec![
            vt.graph.get_edge(&EdgeKey::new(&n_1a_key, &n_2a_key)).unwrap().id,
            vt.graph.get_edge(&EdgeKey::new(&n_2a_key, &n_3x_key)).unwrap().id,
        ]);
        assert_eq!(edges.get("x").unwrap(), &ids);
    }

    // y
    {
        let ids: HashSet<usize> = HashSet::from_iter(vec![
            vt.graph.get_edge(&EdgeKey::new(&n_1x_key, &n_2b_key)).unwrap().id,
            vt.graph.get_edge(&EdgeKey::new(&n_2b_key, &n_3a_key)).unwrap().id,
        ]);
        assert_eq!(edges.get("y").unwrap(), &ids);
    }

    // z
    {
        let ids: HashSet<usize> = HashSet::from_iter(vec![
            vt.graph.get_edge(&EdgeKey::new(&n_1x_key, &n_2b_key)).unwrap().id,
            vt.graph.get_edge(&EdgeKey::new(&n_2b_key, &n_3x_key)).unwrap().id,
        ]);
        assert_eq!(edges.get("z").unwrap(), &ids);
    }
}

#[test]
fn test_as_sankey_json() {
    let vt = test_init_v_tracker();

    // Create NodeKeys for later use
    let n_1a_key = NodeKey::new("1", "a");
    let n_2a_key = NodeKey::new("2", "a");
    let n_2b_key = NodeKey::new("2", "b");
    let n_1x_key = NodeKey::new("1", &vt.str_na);
    let n_3x_key = NodeKey::new("3", &vt.str_na);
    let n_3a_key = NodeKey::new("3", "a");

    let (nodes, edges) = vt.build_uid_paths();

    // Get the node IDs
    let id_a1 = vt.graph.get_node(&n_1a_key).unwrap().id;
    let id_x1 = vt.graph.get_node(&n_1x_key).unwrap().id;
    let id_a2 = vt.graph.get_node(&n_2a_key).unwrap().id;
    let id_b2 = vt.graph.get_node(&n_2b_key).unwrap().id;
    let id_x3 = vt.graph.get_node(&n_3x_key).unwrap().id;
    let id_a3 = vt.graph.get_node(&n_3a_key).unwrap().id;

    // Create the D3 object
    let sankey_json = vt.as_sankey_json();

    // Create the expected
    let mut expected_nodes: Vec<SankeyNode> = Vec::new();

    // id_a1
    {
        let mut cur_links: Vec<usize> = Vec::new();
        for link_id in edges.get("x").unwrap() {
            cur_links.push(*link_id);
        }
        cur_links.sort();

        let mut cur_nodes: Vec<usize> = Vec::new();
        for node_id in nodes.get("x").unwrap() {
            cur_nodes.push(*node_id);
        }
        cur_nodes.sort();

        expected_nodes.push(SankeyNode {
            id: id_a1,
            name: "a".to_string(),
            col: "1".to_string(),
            total: 1,
            link_highlight_id: cur_links,
            node_highlight_id: cur_nodes,
        });
    }

    // id_mis_1
    {
        let mut cur_links: Vec<usize> = Vec::new();
        for link_id in edges.get("y").unwrap().union(edges.get("z").unwrap()) {
            cur_links.push(*link_id);
        }
        cur_links.sort();

        let mut cur_nodes: Vec<usize> = Vec::new();
        for node_id in nodes.get("y").unwrap().union(nodes.get("z").unwrap()) {
            cur_nodes.push(*node_id);
        }
        cur_nodes.sort();

        expected_nodes.push(SankeyNode {
            id: id_x1,
            name: vt.str_na.to_string(),
            col: "1".to_string(),
            total: 2,
            link_highlight_id: cur_links,
            node_highlight_id: cur_nodes,
        });
    }

    // id_a2
    {
        let mut cur_links: Vec<usize> = Vec::new();
        for link_id in edges.get("x").unwrap() {
            cur_links.push(*link_id);
        }
        cur_links.sort();

        let mut cur_nodes: Vec<usize> = Vec::new();
        for node_id in nodes.get("x").unwrap() {
            cur_nodes.push(*node_id);
        }
        cur_nodes.sort();

        expected_nodes.push(SankeyNode {
            id: id_a2,
            name: "a".to_string(),
            col: "2".to_string(),
            total: 1,
            link_highlight_id: cur_links,
            node_highlight_id: cur_nodes,
        });
    }

    // id_b2
    {
        let mut cur_links: Vec<usize> = Vec::new();
        for link_id in edges.get("y").unwrap().union(edges.get("z").unwrap()) {
            cur_links.push(*link_id);
        }
        cur_links.sort();

        let mut cur_nodes: Vec<usize> = Vec::new();
        for node_id in nodes.get("y").unwrap().union(nodes.get("z").unwrap()) {
            cur_nodes.push(*node_id);
        }
        cur_nodes.sort();

        expected_nodes.push(SankeyNode {
            id: id_b2,
            name: "b".to_string(),
            col: "2".to_string(),
            total: 2,
            link_highlight_id: cur_links,
            node_highlight_id: cur_nodes,
        });
    }

    // id_a3
    {
        let mut cur_links: Vec<usize> = Vec::new();
        for link_id in edges.get("y").unwrap() {
            cur_links.push(*link_id);
        }
        cur_links.sort();

        let mut cur_nodes: Vec<usize> = Vec::new();
        for node_id in nodes.get("y").unwrap() {
            cur_nodes.push(*node_id);
        }
        cur_nodes.sort();

        expected_nodes.push(SankeyNode {
            id: id_a3,
            name: "a".to_string(),
            col: "3".to_string(),
            total: 1,
            link_highlight_id: cur_links,
            node_highlight_id: cur_nodes,
        });
    }

    // id_mis_3
    {
        let mut cur_links: Vec<usize> = Vec::new();
        for link_id in edges.get("x").unwrap().union(edges.get("z").unwrap()) {
            cur_links.push(*link_id);
        }
        cur_links.sort();

        let mut cur_nodes: Vec<usize> = Vec::new();
        for node_id in nodes.get("x").unwrap().union(nodes.get("z").unwrap()) {
            cur_nodes.push(*node_id);
        }
        cur_nodes.sort();

        expected_nodes.push(SankeyNode {
            id: id_x3,
            name: vt.str_na.to_string(),
            col: "3".to_string(),
            total: 2,
            link_highlight_id: cur_links,
            node_highlight_id: cur_nodes,
        });
    }

    // Sort the nodes by id
    expected_nodes.sort_by(|a, b| a.id.cmp(&b.id));

    // Test the nodes
    assert_eq!(sankey_json.nodes, expected_nodes);

    let mut edges_exp: Vec<SankeyLink> = Vec::new();

    // id_1
    {
        let id = vt.graph.get_edge(&EdgeKey::new(&n_1a_key, &n_2a_key)).unwrap().id;

        let mut cur_links: Vec<usize> = Vec::new();
        for link_id in edges.get("x").unwrap() {
            cur_links.push(*link_id);
        }
        cur_links.sort();

        let mut cur_nodes: Vec<usize> = Vec::new();
        for node_id in nodes.get("x").unwrap() {
            cur_nodes.push(*node_id);
        }
        cur_nodes.sort();

        edges_exp.push(SankeyLink {
            id,
            source: id_a1,
            target: id_a2,
            value: 1,
            link_highlight_id: cur_links,
            node_highlight_id: cur_nodes,
        });
    }

    // id_2
    {
        let id = vt.graph.get_edge(&EdgeKey::new(&n_1x_key, &n_2b_key)).unwrap().id;

        let mut cur_links: Vec<usize> = Vec::new();
        for link_id in edges.get("y").unwrap().union(edges.get("z").unwrap()) {
            cur_links.push(*link_id);
        }
        cur_links.sort();

        let mut cur_nodes: Vec<usize> = Vec::new();
        for node_id in nodes.get("y").unwrap().union(nodes.get("z").unwrap()) {
            cur_nodes.push(*node_id);
        }
        cur_nodes.sort();

        edges_exp.push(SankeyLink {
            id,
            source: id_x1,
            target: id_b2,
            value: 2,
            link_highlight_id: cur_links,
            node_highlight_id: cur_nodes,
        });
    }

    // id_3
    {
        let id = vt.graph.get_edge(&EdgeKey::new(&n_2a_key, &n_3x_key)).unwrap().id;

        let mut cur_links: Vec<usize> = Vec::new();
        for link_id in edges.get("x").unwrap() {
            cur_links.push(*link_id);
        }
        cur_links.sort();

        let mut cur_nodes: Vec<usize> = Vec::new();
        for node_id in nodes.get("x").unwrap() {
            cur_nodes.push(*node_id);
        }
        cur_nodes.sort();

        edges_exp.push(SankeyLink {
            id,
            source: id_a2,
            target: id_x3,
            value: 1,
            link_highlight_id: cur_links,
            node_highlight_id: cur_nodes,
        });
    }

    // id_4
    {
        let id = vt.graph.get_edge(&EdgeKey::new(&n_2b_key, &n_3a_key)).unwrap().id;

        let mut cur_links: Vec<usize> = Vec::new();
        for link_id in edges.get("y").unwrap() {
            cur_links.push(*link_id);
        }
        cur_links.sort();

        let mut cur_nodes: Vec<usize> = Vec::new();
        for node_id in nodes.get("y").unwrap() {
            cur_nodes.push(*node_id);
        }
        cur_nodes.sort();

        edges_exp.push(SankeyLink {
            id,
            source: id_b2,
            target: id_a3,
            value: 1,
            link_highlight_id: cur_links,
            node_highlight_id: cur_nodes,
        });
    }

    // id_5
    {
        let id = vt.graph.get_edge(&EdgeKey::new(&n_2b_key, &n_3x_key)).unwrap().id;

        let mut cur_links: Vec<usize> = Vec::new();
        for link_id in edges.get("z").unwrap() {
            cur_links.push(*link_id);
        }
        cur_links.sort();

        let mut cur_nodes: Vec<usize> = Vec::new();
        for node_id in nodes.get("z").unwrap() {
            cur_nodes.push(*node_id);
        }
        cur_nodes.sort();

        edges_exp.push(SankeyLink {
            id,
            source: id_b2,
            target: id_x3,
            value: 1,
            link_highlight_id: cur_links,
            node_highlight_id: cur_nodes,
        });
    }

    // Sort edges_exp by the id
    edges_exp.sort_by(|a, b| a.id.cmp(&b.id));
    assert_eq!(sankey_json.links, edges_exp);
}