Skip to content

Commit

Permalink
Gtnotebook (Pometry#882)
Browse files Browse the repository at this point in the history
* impl generic taint v2

* fix merge compile/test issues

* fix test

* intro v2

* impl hits with new apis

* fix example

* fix lotr

* update ingestion

* impl triplet count using new api

* rid old triangle count impl

* impl reciprocity using new api

* impl cluster using new api

* fix test
  • Loading branch information
shivam-880 authored May 16, 2023
1 parent c013a96 commit d621a57
Show file tree
Hide file tree
Showing 37 changed files with 1,444 additions and 2,506 deletions.
413 changes: 330 additions & 83 deletions examples/py/crypto/stable_coin_analysis.ipynb

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions examples/py/enron/enron.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,7 @@
"cell_type": "code",
"execution_count": 10,
"id": "5e8af6e0",
"metadata": {
"scrolled": false
},
"metadata": {},
"outputs": [
{
"data": {
Expand Down Expand Up @@ -823,9 +821,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "raphtory",
"display_name": "Python [conda env:raphtory] *",
"language": "python",
"name": "raphtory"
"name": "conda-env-raphtory-py"
},
"language_info": {
"codemirror_mode": {
Expand Down
31 changes: 19 additions & 12 deletions examples/rust/src/bin/crypto/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use chrono::NaiveDateTime;
use itertools::Itertools;
use raphtory::algorithms::generic_taint::generic_taint;
use raphtory::algorithms::pagerank::unweighted_page_rank;
use raphtory::core::time::TryIntoTime;
use raphtory::db::view_api::internal::GraphViewInternalOps;
use raphtory::db::view_api::layer::LayerOps;
use raphtory::db::view_api::time::WindowSet;
use raphtory::db::view_api::*;
use raphtory_io::graph_loader::example::stable_coins::stable_coin_graph;
use serde::Deserialize;
Expand Down Expand Up @@ -31,18 +35,11 @@ fn main() {
let g = stable_coin_graph(data_dir, 1);

assert_eq!(g.num_vertices(), 1523333);
assert_eq!(g.num_edges(), 2871269);
assert_eq!(g.num_edges(), 2814155);

assert_eq!(
g.get_unique_layers().into_iter().sorted().collect_vec(),
vec![
"0x6b175474e89094c44da98b954eedeac495271d0f",
"0x8e870d67f660d95d5be530380d0ec0bd388289e1",
"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"0xa47c8bf37f92abed4a126bda807a7b7498661acd",
"0xd2877702675e6ceb975b4a1dff9fb7baf4c91ea9",
"0xdac17f958d2ee523a2206206994597c13d831ec7"
]
vec!["Dai", "LUNC", "USD", "USDP", "USDT", "USTC"]
);

println!("Pagerank");
Expand All @@ -52,7 +49,16 @@ fn main() {

let now = Instant::now();
let _ = unweighted_page_rank(
&g.layer("0xdac17f958d2ee523a2206206994597c13d831ec7")
&g,
20,
None,
None,
);
println!("Time taken: {} secs", now.elapsed().as_secs());

let now = Instant::now();
let _ = unweighted_page_rank(
&g.layer("USDT")
.unwrap(),
20,
None,
Expand All @@ -63,12 +69,13 @@ fn main() {
println!("Generic taint");
let now = Instant::now();
let _ = generic_taint(
&g.layer("0xdac17f958d2ee523a2206206994597c13d831ec7")
&g.layer("USDT")
.unwrap(),
None,
20,
1651105815,
vec!["0xd30b438df65f4f788563b2b3611bd6059bff4ad9"],
vec![],
);
println!("Time taken: {}", now.elapsed().as_secs());
println!("Time taken: {} secs", now.elapsed().as_secs());
}
4 changes: 2 additions & 2 deletions examples/rust/src/bin/hulongbay/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::path::Path;

use itertools::Itertools;
use raphtory::algorithms::connected_components::weakly_connected_components;
use raphtory::algorithms::triangle_count::triangle_counting_fast;
use raphtory::algorithms::triangle_count::triangle_count;
use raphtory::core::{Direction, Prop};
use raphtory::db::graph::Graph;
use raphtory::db::view_api::*;
Expand Down Expand Up @@ -112,7 +112,7 @@ fn try_main() -> Result<(), Box<dyn Error>> {
let max_time = graph.end().ok_or(GraphEmptyError)?;
let mid_time = (min_time + max_time) / 2;
let now = Instant::now();
let actual_tri_count = triangle_counting_fast(&graph, None);
let actual_tri_count = triangle_count(&graph, None);

println!("Actual triangle count: {:?}", actual_tri_count);

Expand Down
2 changes: 1 addition & 1 deletion examples/rust/src/bin/lotr/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fn main() {
assert!(graph.has_vertex(gandalf));
assert_eq!(graph.vertex(gandalf).unwrap().name(), "Gandalf");

let r = generic_taint(&graph, 20, 31930, vec!["Gandalf"], vec![]);
let r = generic_taint(&graph, None, 20, 31930, vec!["Gandalf"], vec![]);
assert_eq!(
r.keys().sorted().collect_vec(),
vec!["Gandalf", "Saruman", "Wormtongue"]
Expand Down
13 changes: 7 additions & 6 deletions python/src/algorithms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub(crate) fn local_triangle_count(g: &PyGraphView, v: &PyAny) -> PyResult<Optio
pub(crate) fn weakly_connected_components(
g: &PyGraphView,
iter_count: usize,
) -> PyResult<FxHashMap<String, u64>> {
) -> PyResult<HashMap<String, u64>> {
Ok(connected_components::weakly_connected_components(
&g.graph, iter_count, None,
))
Expand All @@ -63,7 +63,7 @@ pub(crate) fn pagerank(
g: &PyGraphView,
iter_count: usize,
max_diff: Option<f32>,
) -> PyResult<FxHashMap<String, f32>> {
) -> PyResult<HashMap<String, f32>> {
Ok(unweighted_page_rank(&g.graph, iter_count, None, max_diff))
}

Expand All @@ -86,6 +86,7 @@ pub(crate) fn generic_taint(

Ok(generic_taint_rs(
&g.graph,
None,
iter_count,
start_time,
infected_nodes?,
Expand Down Expand Up @@ -181,7 +182,7 @@ pub(crate) fn min_in_degree(g: &PyGraphView) -> usize {
/// it could imply a less reciprocal or more one-sided relationship.
#[pyfunction]
pub(crate) fn global_reciprocity(g: &PyGraphView) -> f64 {
global_reciprocity_rs(&g.graph)
global_reciprocity_rs(&g.graph, None)
}

/// Reciprocity - measure of the symmetry of relationships in a graph.
Expand All @@ -202,8 +203,8 @@ pub(crate) fn global_reciprocity(g: &PyGraphView) -> f64 {
/// it could imply a less reciprocal or more one-sided relationship.
///
#[pyfunction]
pub(crate) fn all_local_reciprocity(g: &PyGraphView) -> HashMap<u64, f64> {
all_local_reciprocity_rs(&g.graph)
pub(crate) fn all_local_reciprocity(g: &PyGraphView) -> HashMap<String, f64> {
all_local_reciprocity_rs(&g.graph, None)
}

/// Computes the number of both open and closed triplets within a graph
Expand All @@ -212,7 +213,7 @@ pub(crate) fn all_local_reciprocity(g: &PyGraphView) -> HashMap<u64, f64> {
/// A closed triplet is one where a node has two neighbors, and an edge between them.
#[pyfunction]
pub(crate) fn triplet_count(g: &PyGraphView) -> usize {
raphtory::algorithms::triplet_count::triplet_count(&g.graph)
raphtory::algorithms::triplet_count::triplet_count(&g.graph, None)
}

/// Computes the global clustering coefficient of a graph. The global clustering coefficient is
Expand Down
20 changes: 13 additions & 7 deletions python/src/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::dynamic::{DynamicGraph, IntoDynamic};
use crate::types::repr::{iterator_repr, Repr};
use crate::utils::*;
use crate::vertex::{PyVertex, PyVertexIterable};
use crate::wrappers::iterators::{OptionPropIterable, I64Iterable};
use crate::wrappers::iterators::{I64Iterable, OptionPropIterable};
use crate::wrappers::prop::Prop;
use chrono::NaiveDateTime;
use itertools::Itertools;
Expand Down Expand Up @@ -198,7 +198,7 @@ impl PyEdge {
///
/// Returns:
/// the start datetime of the Edge.
pub fn start_date_time(&self) ->Option<NaiveDateTime> {
pub fn start_date_time(&self) -> Option<NaiveDateTime> {
let start_time = self.edge.start()?;
Some(NaiveDateTime::from_timestamp_millis(start_time).unwrap())
}
Expand All @@ -215,7 +215,7 @@ impl PyEdge {
///
/// Returns:
/// The end datetime of the Edge
pub fn end_date_time(&self) ->Option<NaiveDateTime> {
pub fn end_date_time(&self) -> Option<NaiveDateTime> {
let end_time = self.edge.end()?;
Some(NaiveDateTime::from_timestamp_millis(end_time).unwrap())
}
Expand Down Expand Up @@ -335,7 +335,7 @@ impl PyEdge {
pub fn layer_name(&self) -> String {
self.edge.layer_name()
}

/// Gets the datetime of an exploded edge.
///
/// Returns:
Expand Down Expand Up @@ -457,18 +457,24 @@ impl PyEdges {

/// Returns the earliest time of the edges.
fn earliest_time(&self) -> I64Iterable {
let edges: Arc<dyn Fn() -> Box<dyn Iterator<Item = EdgeView<DynamicGraph>> + Send> + Send + Sync> = self.builder.clone();
let edges: Arc<
dyn Fn() -> Box<dyn Iterator<Item = EdgeView<DynamicGraph>> + Send> + Send + Sync,
> = self.builder.clone();
(move || edges().earliest_time()).into()
}

/// Returns the latest time of the edges.
fn latest_time(&self) -> I64Iterable {
let edges: Arc<dyn Fn() -> Box<dyn Iterator<Item = EdgeView<DynamicGraph>> + Send> + Send + Sync> = self.builder.clone();
let edges: Arc<
dyn Fn() -> Box<dyn Iterator<Item = EdgeView<DynamicGraph>> + Send> + Send + Sync,
> = self.builder.clone();
(move || edges().latest_time()).into()
}

fn property(&self, name: String, include_static: Option<bool>) -> OptionPropIterable {
let edges: Arc<dyn Fn() -> Box<dyn Iterator<Item = EdgeView<DynamicGraph>> + Send> + Send + Sync> = self.builder.clone();
let edges: Arc<
dyn Fn() -> Box<dyn Iterator<Item = EdgeView<DynamicGraph>> + Send> + Send + Sync,
> = self.builder.clone();
(move || edges().property(name.clone(), include_static.unwrap_or(true))).into()
}

Expand Down
12 changes: 6 additions & 6 deletions python/src/graph_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use crate::utils::{
PyWindowSet,
};
use crate::vertex::{PyVertex, PyVertices};
use chrono::prelude::*;
use pyo3::prelude::*;
use raphtory::db::view_api::layer::LayerOps;
use raphtory::db::view_api::*;
use raphtory::*;
use chrono::prelude::*;

/// Graph view is a read-only version of a graph at a certain point in time.
#[pyclass(name = "GraphView", frozen, subclass)]
Expand Down Expand Up @@ -55,7 +55,7 @@ impl PyGraphView {
///
/// Returns:
/// the datetime of the earliest activity in the graph
pub fn earliest_date_time(&self) ->Option<NaiveDateTime> {
pub fn earliest_date_time(&self) -> Option<NaiveDateTime> {
let earliest_time = self.graph.earliest_time()?;
Some(NaiveDateTime::from_timestamp_millis(earliest_time).unwrap())
}
Expand All @@ -72,7 +72,7 @@ impl PyGraphView {
///
/// Returns:
/// the datetime of the latest activity in the graph
pub fn latest_date_time(&self) ->Option<NaiveDateTime> {
pub fn latest_date_time(&self) -> Option<NaiveDateTime> {
let latest_time = self.graph.latest_time()?;
Some(NaiveDateTime::from_timestamp_millis(latest_time).unwrap())
}
Expand Down Expand Up @@ -183,7 +183,7 @@ impl PyGraphView {
///
/// Returns:
/// the default start datetime for perspectives over the view
pub fn start_date_time(&self) ->Option<NaiveDateTime> {
pub fn start_date_time(&self) -> Option<NaiveDateTime> {
let start_time = self.graph.start()?;
Some(NaiveDateTime::from_timestamp_millis(start_time).unwrap())
}
Expand All @@ -200,12 +200,12 @@ impl PyGraphView {
pub fn window_size(&self) -> Option<u64> {
self.graph.window_size()
}

/// Returns the default end datetime for perspectives over the view
///
/// Returns:
/// the default end datetime for perspectives over the view
pub fn end_date_time(&self) ->Option<NaiveDateTime> {
pub fn end_date_time(&self) -> Option<NaiveDateTime> {
let end_time = self.graph.end()?;
Some(NaiveDateTime::from_timestamp_millis(end_time).unwrap())
}
Expand Down
5 changes: 4 additions & 1 deletion python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ fn raphtory(py: Python<'_>, m: &PyModule) -> PyResult<()> {
algorithm_module.add_function(wrap_pyfunction!(min_out_degree, algorithm_module)?)?;
algorithm_module.add_function(wrap_pyfunction!(min_in_degree, algorithm_module)?)?;
algorithm_module.add_function(wrap_pyfunction!(pagerank, algorithm_module)?)?;
algorithm_module.add_function(wrap_pyfunction!(weakly_connected_components, algorithm_module)?)?;
algorithm_module.add_function(wrap_pyfunction!(
weakly_connected_components,
algorithm_module
)?)?;
m.add_submodule(algorithm_module)?;

let graph_loader_module = PyModule::new(py, "graph_loader")?;
Expand Down
9 changes: 4 additions & 5 deletions python/src/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl PyVertex {
///
/// Returns:
/// The earliest datetime that the vertex exists as an integer.
pub fn earliest_date_time(&self) ->Option<NaiveDateTime> {
pub fn earliest_date_time(&self) -> Option<NaiveDateTime> {
let earliest_time = self.vertex.earliest_time()?;
Some(NaiveDateTime::from_timestamp_millis(earliest_time).unwrap())
}
Expand All @@ -117,12 +117,11 @@ impl PyVertex {
///
/// Returns:
/// The latest datetime that the vertex exists as an integer.
pub fn latest_date_time(&self) ->Option<NaiveDateTime> {
pub fn latest_date_time(&self) -> Option<NaiveDateTime> {
let latest_time = self.vertex.latest_time()?;
Some(NaiveDateTime::from_timestamp_millis(latest_time).unwrap())
}


/// Gets the property value of this vertex given the name of the property.
///
/// Arguments:
Expand Down Expand Up @@ -321,7 +320,7 @@ impl PyVertex {
///
/// Returns:
/// The earliest datetime that this vertex is valid or None if the vertex is valid for all times.
pub fn start_date_time(&self) ->Option<NaiveDateTime> {
pub fn start_date_time(&self) -> Option<NaiveDateTime> {
let start_time = self.vertex.start()?;
Some(NaiveDateTime::from_timestamp_millis(start_time).unwrap())
}
Expand All @@ -338,7 +337,7 @@ impl PyVertex {
///
/// Returns:
/// The latest datetime that this vertex is valid or None if the vertex is valid for all times.
pub fn end_date_time(&self) ->Option<NaiveDateTime> {
pub fn end_date_time(&self) -> Option<NaiveDateTime> {
let end_time = self.vertex.end()?;
Some(NaiveDateTime::from_timestamp_millis(end_time).unwrap())
}
Expand Down
Loading

0 comments on commit d621a57

Please sign in to comment.