Skip to content

Commit

Permalink
Modify AlgoResult.get to accept Strings in rust (#1341)
Browse files Browse the repository at this point in the history
* modify get so you dont need the into

* remove old get fn
  • Loading branch information
Haaroon authored Oct 19, 2023
1 parent b4eef2e commit 11e103d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use csv::StringRecord;
use flate2::read::GzDecoder;
use raphtory::{
algorithms::{
algorithm_result::AlgorithmResult, centrality::pagerank::unweighted_page_rank,
centrality::pagerank::unweighted_page_rank,
community_detection::connected_components::weakly_connected_components,
},
graph_loader::{fetch_file, source::csv_loader::CsvLoader},
Expand Down
24 changes: 10 additions & 14 deletions raphtory/src/algorithms/algorithm_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,11 @@ where
/// Returns the value corresponding to the provided key in the `result` hashmap.
///
/// Arguments:
/// `key`: The key of type `H` for which the value is to be retrieved.
pub fn get(&self, v_ref: VertexRef) -> Option<&V> {
if self.graph.has_vertex(v_ref) {
let internal_id = self.graph.vertex(v_ref).unwrap().vertex.0;
/// `key`: The key of the vertex, can be the vertex object, or namraphtory/src/algorithms/motifs/three_node_local_single_thread.rse.
pub fn get<T: Into<VertexRef>>(&self, name: T) -> Option<&V> {
let v = name.into();
if self.graph.has_vertex(v) {
let internal_id = self.graph.vertex(v).unwrap().vertex.0;
self.result.get(&internal_id)
} else {
None
Expand Down Expand Up @@ -544,19 +545,14 @@ mod algorithm_result_test {
let algo_result = create_algo_result_u64();
let vertex_c = algo_result.graph.vertex("C").unwrap();
let vertex_d = algo_result.graph.vertex("D").unwrap();
assert_eq!(algo_result.get(vertex_c.clone().into()), Some(&30u64));
assert_eq!(algo_result.get(vertex_d.into()), None);
assert_eq!(algo_result.get(vertex_c.clone()), Some(&30u64));
assert_eq!(algo_result.get(vertex_d.clone()), None);
let algo_result = create_algo_result_f64();
assert_eq!(algo_result.get(vertex_c.clone().into()), Some(&30.0f64));
assert_eq!(algo_result.get(vertex_c.clone()), Some(&30.0f64));
let algo_result = create_algo_result_tuple();
assert_eq!(algo_result.get(vertex_c.clone().into()).unwrap().0, 30.0f32);
assert_eq!(algo_result.get(vertex_c.clone()).unwrap().0, 30.0f32);
let algo_result = create_algo_result_hashmap_vec();
let answer = algo_result
.get(vertex_c.clone().into())
.unwrap()
.get(0)
.unwrap()
.0;
let answer = algo_result.get(vertex_c.clone()).unwrap().get(0).unwrap().0;
assert_eq!(answer, 22i32);
}

Expand Down
62 changes: 31 additions & 31 deletions raphtory/src/algorithms/centrality/pagerank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ mod page_rank_tests {

let results = unweighted_page_rank(&graph, 1000, Some(1), None, true);

assert_eq_f64(results.get("1".into()), Some(&0.38694), 5);
assert_eq_f64(results.get("2".into()), Some(&0.20195), 5);
assert_eq_f64(results.get("4".into()), Some(&0.20195), 5);
assert_eq_f64(results.get("3".into()), Some(&0.20916), 5);
assert_eq_f64(results.get("1"), Some(&0.38694), 5);
assert_eq_f64(results.get("2"), Some(&0.20195), 5);
assert_eq_f64(results.get("4"), Some(&0.20195), 5);
assert_eq_f64(results.get("3"), Some(&0.20916), 5);
}

#[test]
Expand Down Expand Up @@ -257,17 +257,17 @@ mod page_rank_tests {

let results = unweighted_page_rank(&graph, 1000, Some(4), None, true);

assert_eq_f64(results.get("10".into()), Some(&0.072082), 5);
assert_eq_f64(results.get("8".into()), Some(&0.136473), 5);
assert_eq_f64(results.get("3".into()), Some(&0.15484), 5);
assert_eq_f64(results.get("6".into()), Some(&0.07208), 5);
assert_eq_f64(results.get("11".into()), Some(&0.06186), 5);
assert_eq_f64(results.get("2".into()), Some(&0.03557), 5);
assert_eq_f64(results.get("1".into()), Some(&0.11284), 5);
assert_eq_f64(results.get("4".into()), Some(&0.07944), 5);
assert_eq_f64(results.get("7".into()), Some(&0.01638), 5);
assert_eq_f64(results.get("9".into()), Some(&0.06186), 5);
assert_eq_f64(results.get("5".into()), Some(&0.19658), 5);
assert_eq_f64(results.get("10"), Some(&0.072082), 5);
assert_eq_f64(results.get("8"), Some(&0.136473), 5);
assert_eq_f64(results.get("3"), Some(&0.15484), 5);
assert_eq_f64(results.get("6"), Some(&0.07208), 5);
assert_eq_f64(results.get("11"), Some(&0.06186), 5);
assert_eq_f64(results.get("2"), Some(&0.03557), 5);
assert_eq_f64(results.get("1"), Some(&0.11284), 5);
assert_eq_f64(results.get("4"), Some(&0.07944), 5);
assert_eq_f64(results.get("7"), Some(&0.01638), 5);
assert_eq_f64(results.get("9"), Some(&0.06186), 5);
assert_eq_f64(results.get("5"), Some(&0.19658), 5);
}

#[test]
Expand All @@ -282,8 +282,8 @@ mod page_rank_tests {

let results = unweighted_page_rank(&graph, 1000, Some(4), None, false);

assert_eq_f64(results.get("1".into()), Some(&0.5), 3);
assert_eq_f64(results.get("2".into()), Some(&0.5), 3);
assert_eq_f64(results.get("1"), Some(&0.5), 3);
assert_eq_f64(results.get("2"), Some(&0.5), 3);
}

#[test]
Expand All @@ -298,9 +298,9 @@ mod page_rank_tests {

let results = unweighted_page_rank(&graph, 10, Some(4), None, false);

assert_eq_f64(results.get("1".into()), Some(&0.303), 3);
assert_eq_f64(results.get("2".into()), Some(&0.393), 3);
assert_eq_f64(results.get("3".into()), Some(&0.303), 3);
assert_eq_f64(results.get("1"), Some(&0.303), 3);
assert_eq_f64(results.get("2"), Some(&0.393), 3);
assert_eq_f64(results.get("3"), Some(&0.303), 3);
}

#[test]
Expand Down Expand Up @@ -334,17 +334,17 @@ mod page_rank_tests {

let results = unweighted_page_rank(&graph, 1000, Some(4), None, true);

assert_eq_f64(results.get("1".into()), Some(&0.055), 3);
assert_eq_f64(results.get("2".into()), Some(&0.079), 3);
assert_eq_f64(results.get("3".into()), Some(&0.113), 3);
assert_eq_f64(results.get("4".into()), Some(&0.055), 3);
assert_eq_f64(results.get("5".into()), Some(&0.070), 3);
assert_eq_f64(results.get("6".into()), Some(&0.083), 3);
assert_eq_f64(results.get("7".into()), Some(&0.093), 3);
assert_eq_f64(results.get("8".into()), Some(&0.102), 3);
assert_eq_f64(results.get("9".into()), Some(&0.110), 3);
assert_eq_f64(results.get("10".into()), Some(&0.117), 3);
assert_eq_f64(results.get("11".into()), Some(&0.122), 3);
assert_eq_f64(results.get("1"), Some(&0.055), 3);
assert_eq_f64(results.get("2"), Some(&0.079), 3);
assert_eq_f64(results.get("3"), Some(&0.113), 3);
assert_eq_f64(results.get("4"), Some(&0.055), 3);
assert_eq_f64(results.get("5"), Some(&0.070), 3);
assert_eq_f64(results.get("6"), Some(&0.083), 3);
assert_eq_f64(results.get("7"), Some(&0.093), 3);
assert_eq_f64(results.get("8"), Some(&0.102), 3);
assert_eq_f64(results.get("9"), Some(&0.110), 3);
assert_eq_f64(results.get("10"), Some(&0.117), 3);
assert_eq_f64(results.get("11"), Some(&0.122), 3);
}

fn assert_eq_f64<T: Borrow<f64> + PartialEq + std::fmt::Debug>(
Expand Down
2 changes: 1 addition & 1 deletion raphtory/src/algorithms/metrics/reciprocity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,6 @@ mod reciprocity_test {
hash_map_result.insert("5".to_string(), 0.0);

let res = all_local_reciprocity(&graph, None);
assert_eq!(res.get("1".into()), hash_map_result.get("1"));
assert_eq!(res.get("1"), hash_map_result.get("1"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ pub fn local_temporal_three_node_motifs<G: GraphViewOps>(
let mut final_cts = Vec::new();
final_cts.extend(stars.into_iter());
final_cts.extend(two_nodes.into_iter());
final_cts.extend(counts.get(v.clone().into()).unwrap().into_iter());
final_cts.extend(counts.get(v.clone()).unwrap().into_iter());
counts.result.insert(v.vertex.0, final_cts);
}

Expand Down Expand Up @@ -462,10 +462,7 @@ mod local_motif_test {
),
]);
for ind in 1..12 {
assert_eq!(
counts_result.get(ind.into()).unwrap(),
expected.get(&ind).unwrap()
)
assert_eq!(counts_result.get(ind).unwrap(), expected.get(&ind).unwrap())
}
// print!("{:?}", global_counts);
}
Expand Down

0 comments on commit 11e103d

Please sign in to comment.