Skip to content

Commit

Permalink
add lotr_graph_with_props function (#1881)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricopinazo authored Dec 2, 2024
1 parent c340a1b commit 092195f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 21 deletions.
83 changes: 62 additions & 21 deletions raphtory/src/graph_loader/lotr_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
//! ```
use crate::{graph_loader::fetch_file, io::csv_loader::CsvLoader, prelude::*};
use serde::Deserialize;
use std::path::PathBuf;
use std::{collections::HashMap, path::PathBuf};
use tracing::error;

#[derive(Deserialize, std::fmt::Debug)]
Expand All @@ -47,6 +47,27 @@ pub fn lotr_file() -> Result<PathBuf, Box<dyn std::error::Error>> {
)
}

#[derive(Deserialize, std::fmt::Debug)]
struct Character {
pub name: String,
pub race: String,
pub gender: String,
}

/// Downloads the LOTR.csv file from Github
/// and returns the path to the file
///
/// Returns:
/// - A PathBuf to the LOTR.csv file
fn lotr_properties_file() -> Result<PathBuf, Box<dyn std::error::Error>> {
fetch_file(
"lotr_properties.csv",
true,
"https://raw.githubusercontent.com/Raphtory/Data/main/lotr_properties.csv",
600,
)
}

/// Constructs a graph from the LOTR dataset
/// Including all edges, nodes and timestamps
///
Expand All @@ -57,26 +78,46 @@ pub fn lotr_file() -> Result<PathBuf, Box<dyn std::error::Error>> {
/// Returns:
/// - A Graph containing the LOTR dataset
pub fn lotr_graph() -> Graph {
let graph = {
let g = Graph::new();
let g = Graph::new();
CsvLoader::new(lotr_file().unwrap())
.load_into_graph(&g, |lotr: Lotr, g: &Graph| {
let src_id = lotr.src_id;
let dst_id = lotr.dst_id;
let time = lotr.time;

CsvLoader::new(lotr_file().unwrap())
.load_into_graph(&g, |lotr: Lotr, g: &Graph| {
let src_id = lotr.src_id;
let dst_id = lotr.dst_id;
let time = lotr.time;
g.add_node(time, src_id.clone(), NO_PROPS, None)
.map_err(|err| error!("{:?}", err))
.ok();
g.add_node(time, dst_id.clone(), NO_PROPS, None)
.map_err(|err| error!("{:?}", err))
.ok();
g.add_edge(time, src_id.clone(), dst_id.clone(), NO_PROPS, None)
.expect("Error: Unable to add edge");
})
.expect("Failed to load graph from CSV data files");
g
}

g.add_node(time, src_id.clone(), NO_PROPS, None)
.map_err(|err| error!("{:?}", err))
.ok();
g.add_node(time, dst_id.clone(), NO_PROPS, None)
.map_err(|err| error!("{:?}", err))
.ok();
g.add_edge(time, src_id.clone(), dst_id.clone(), NO_PROPS, None)
.expect("Error: Unable to add edge");
})
.expect("Failed to load graph from CSV data files");
g
};
graph
/// Constructs a graph from the LOTR dataset
/// Including all edges, nodes, and timestamps with some node types
///
/// # Arguments
///
/// - shards: The number of shards to use for the graph
///
/// Returns:
/// - A Graph containing the LOTR dataset
pub fn lotr_graph_with_props() -> Graph {
let g = lotr_graph();
CsvLoader::new(lotr_properties_file().unwrap())
.load_into_graph(&g, |char: Character, g: &Graph| {
if let Some(node) = g.node(char.name) {
let _ = node.add_constant_properties(HashMap::from([
("race", char.race),
("gender", char.gender),
]));
}
})
.expect("Failed to load graph from CSV data files");
g
}
1 change: 1 addition & 0 deletions raphtory/src/python/packages/base_modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ pub fn base_graph_loader_module(py: Python<'_>) -> Result<Bound<PyModule>, PyErr
add_functions!(
&graph_loader_module,
lotr_graph,
lotr_graph_with_props,
neo4j_movie_graph,
stable_coin_graph,
reddit_hyperlink_graph,
Expand Down
6 changes: 6 additions & 0 deletions raphtory/src/python/packages/graph_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ pub fn lotr_graph() -> PyResult<Py<PyGraph>> {
PyGraph::py_from_db_graph(crate::graph_loader::lotr_graph::lotr_graph())
}

/// Same as `lotr_graph()` but with additional properties race and gender for some of the nodes
#[pyfunction]
pub fn lotr_graph_with_props() -> PyResult<Py<PyGraph>> {
PyGraph::py_from_db_graph(crate::graph_loader::lotr_graph::lotr_graph_with_props())
}

/// Load (a subset of) Reddit hyperlinks dataset into a graph.
/// The dataset is available at http://snap.stanford.edu/data/soc-redditHyperlinks-title.tsv
/// The hyperlink network represents the directed connections between two subreddits (a subreddit
Expand Down

0 comments on commit 092195f

Please sign in to comment.