Skip to content

Commit

Permalink
Feature/temporal edges (Pometry#1241)
Browse files Browse the repository at this point in the history
* basic temporal edges function

* added python view

* fixed format
  • Loading branch information
narnolddd authored Sep 6, 2023
1 parent 42b4bea commit 8f42868
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
23 changes: 23 additions & 0 deletions raphtory/src/db/api/view/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ pub trait GraphViewOps: BoxableGraphView + Clone + Sized {
/// Return the number of edges in the graph.
fn num_edges(&self) -> usize;

// Return the number of temporal edges in the graph.
fn num_temporal_edges(&self) -> usize;

/// Check if the graph contains a vertex `v`.
fn has_vertex<T: Into<VertexRef>>(&self, v: T) -> bool;

Expand Down Expand Up @@ -116,6 +119,10 @@ impl<G: BoxableGraphView + Sized + Clone> GraphViewOps for G {
self.vertices_len(self.layer_ids(), self.edge_filter())
}

fn num_temporal_edges(&self) -> usize {
self.edges().explode().count()
}

#[inline]
fn num_edges(&self) -> usize {
self.edges_len(self.layer_ids(), self.edge_filter())
Expand Down Expand Up @@ -260,6 +267,22 @@ impl<G: GraphViewOps> LayerOps for G {
}
}

#[cfg(test)]
mod test_exploded_edges {
use crate::prelude::*;

#[test]
fn test_exploded_edges() {
let g: Graph = Graph::new();
g.add_edge(0, 0, 1, NO_PROPS, None);
g.add_edge(1, 0, 1, NO_PROPS, None);
g.add_edge(2, 0, 1, NO_PROPS, None);
g.add_edge(3, 0, 1, NO_PROPS, None);

assert_eq!(g.num_temporal_edges(), 4)
}
}

#[cfg(test)]
mod test_materialize {
use crate::prelude::*;
Expand Down
17 changes: 13 additions & 4 deletions raphtory/src/python/graph/views/graph_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ impl PyGraphView {
self.graph.num_edges()
}

/// Number of edges in the graph
///
/// Returns:
/// the number of temporal edges in the graph
pub fn num_temporal_edges(&self) -> usize {
self.graph.num_temporal_edges()
}

/// Number of vertices in the graph
///
/// Returns:
Expand Down Expand Up @@ -383,6 +391,7 @@ impl Repr for PyGraphView {
fn repr(&self) -> String {
let num_edges = self.graph.num_edges();
let num_vertices = self.graph.num_vertices();
let num_temporal_edges: usize = self.graph.num_temporal_edges();
let earliest_time = self.graph.earliest_time().unwrap_or_default();
let latest_time = self.graph.latest_time().unwrap_or_default();
let properties: String = self
Expand All @@ -393,14 +402,14 @@ impl Repr for PyGraphView {
.join(", ");
if (properties.is_empty()) {
return format!(
"Graph(number_of_edges={:?}, number_of_vertices={:?}, earliest_time={:?}, latest_time={:?})",
num_edges, num_vertices, earliest_time, latest_time
"Graph(number_of_edges={:?}, number_of_vertices={:?}, number_of_temporal_edges={:?}, earliest_time={:?}, latest_time={:?})",
num_edges, num_vertices, num_temporal_edges, earliest_time, latest_time
);
} else {
let property_string: String = format!("{{{properties}}}");
return format!(
"Graph(number_of_edges={:?}, number_of_vertices={:?}, earliest_time={:?}, latest_time={:?}, properties={})",
num_edges, num_vertices, earliest_time, latest_time, property_string
"Graph(number_of_edges={:?}, number_of_vertices={:?}, number_of_temporal_edges={:?}, earliest_time={:?}, latest_time={:?}, properties={})",
num_edges, num_vertices, num_temporal_edges, earliest_time, latest_time, property_string
);
}
}
Expand Down

0 comments on commit 8f42868

Please sign in to comment.