Skip to content

Commit

Permalink
supported empty node types while node type filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
shivam-880 committed May 29, 2024
1 parent c3289dc commit 62b999a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
7 changes: 5 additions & 2 deletions python/tests/test_graphdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2092,6 +2092,9 @@ def test_type_filter():
g.add_node(1, 4, node_type="a")
g.add_node(1, 5, node_type="c")
g.add_node(1, 6, node_type="e")
g.add_node(1, 7)
g.add_node(1, 8)
g.add_node(1, 9)
g.add_edge(2, 1, 2, layer="a")
g.add_edge(2, 3, 2, layer="a")
g.add_edge(2, 2, 4, layer="a")
Expand All @@ -2104,12 +2107,12 @@ def test_type_filter():
assert g.nodes.type_filter(["a", "c"]).name.collect() == ['1', '4', '5']
assert g.nodes.type_filter(["a"]).neighbours.name.collect() == [['2'], ['2', '5']]

assert g.nodes.degree().collect() == [1, 3, 2, 2, 2, 2]
assert g.nodes.degree().collect() == [1, 3, 2, 2, 2, 2, 0, 0, 0]
assert g.nodes.type_filter(['a']).degree().collect() == [1, 2]
assert g.nodes.type_filter(['d']).degree().collect() == []
assert g.nodes.type_filter([]).name.collect() == []

assert len(g.nodes) == 6
assert len(g.nodes) == 9
assert len(g.nodes.type_filter(['b'])) == 2
assert len(g.nodes.type_filter(['d'])) == 0

Expand Down
6 changes: 5 additions & 1 deletion raphtory/src/db/api/view/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub trait NodeViewOps<'graph>: Clone + TimeOps<'graph> + LayerOps<'graph> {

/// Returns the type of node
fn node_type(&self) -> Self::ValueType<Option<ArcStr>>;

fn node_type_id(&self) -> Self::ValueType<usize>;
/// Get the timestamp for the earliest activity of the node
fn earliest_time(&self) -> Self::ValueType<Option<i64>>;

Expand Down Expand Up @@ -186,6 +186,10 @@ impl<'graph, V: BaseNodeViewOps<'graph> + 'graph> NodeViewOps<'graph> for V {
self.map(|_cg, g, v| g.node_type(v))
}
#[inline]
fn node_type_id(&self) -> Self::ValueType<usize> {
self.map(|_cg, g, v| g.node_type_id(v))
}
#[inline]
fn earliest_time(&self) -> Self::ValueType<Option<i64>> {
self.map(|_cg, g, v| g.node_earliest_time(v))
}
Expand Down
36 changes: 32 additions & 4 deletions raphtory/src/db/graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ mod db_tests {
use super::*;
use crate::{
algorithms::components::weakly_connected_components,
arrow::graph_impl::ArrowGraph,
core::{
utils::time::{error::ParseTimeError, TryIntoTime},
ArcStr, OptionAsStr, Prop,
Expand Down Expand Up @@ -2510,6 +2511,9 @@ mod db_tests {
g.add_node(1, 4, NO_PROPS, Some("a")).unwrap();
g.add_node(1, 5, NO_PROPS, Some("c")).unwrap();
g.add_node(1, 6, NO_PROPS, Some("e")).unwrap();
g.add_node(1, 7, NO_PROPS, None).unwrap();
g.add_node(1, 8, NO_PROPS, None).unwrap();
g.add_node(1, 9, NO_PROPS, None).unwrap();
g.add_edge(2, 1, 2, NO_PROPS, Some("a")).unwrap();
g.add_edge(2, 3, 2, NO_PROPS, Some("a")).unwrap();
g.add_edge(2, 2, 4, NO_PROPS, Some("a")).unwrap();
Expand All @@ -2518,6 +2522,27 @@ mod db_tests {
g.add_edge(2, 5, 6, NO_PROPS, Some("a")).unwrap();
g.add_edge(2, 3, 6, NO_PROPS, Some("a")).unwrap();

assert_eq!(
g.nodes()
.type_filter(&vec!["a", "b", "c", "e"])
.name()
.collect_vec(),
vec!["1", "2", "3", "4", "5", "6"]
);

assert_eq!(
g.nodes()
.type_filter(&Vec::<String>::new())
.name()
.collect_vec(),
Vec::<String>::new()
);

assert_eq!(
g.nodes().type_filter(&vec![""]).name().collect_vec(),
vec!["7", "8", "9"]
);

let w = g.window(1, 4);
assert_eq!(
w.nodes()
Expand Down Expand Up @@ -2580,7 +2605,7 @@ mod db_tests {

assert_eq!(
g.nodes().iter().map(|v| v.degree()).collect::<Vec<_>>(),
vec![1, 3, 2, 2, 2, 2]
vec![1, 3, 2, 2, 2, 2, 0, 0, 0]
);
assert_eq!(
g.nodes()
Expand Down Expand Up @@ -2634,7 +2659,7 @@ mod db_tests {
Vec::<&str>::new()
);

assert_eq!(g.nodes().len(), 6);
assert_eq!(g.nodes().len(), 9);
assert_eq!(g.nodes().type_filter(&vec!["b"]).len(), 2);
assert_eq!(g.nodes().type_filter(&vec!["d"]).len(), 0);

Expand Down Expand Up @@ -2756,6 +2781,9 @@ mod db_tests {
vec!["1", "3", "4", "4", "6"],
vec!["2", "5", "3", "5"],
vec!["2", "6", "4", "6"],
vec![],
vec![],
vec![],
]
);

Expand Down Expand Up @@ -3037,7 +3065,7 @@ mod db_tests {
(2, "open".to_string()),
(3, "review".to_string()),
(4, "open".to_string()),
(10, "in-progress".to_string())
(10, "in-progress".to_string()),
]
);

Expand All @@ -3059,7 +3087,7 @@ mod db_tests {
(1, "open".to_string()),
(3, "review".to_string()),
(4, "open".to_string()),
(5, "in-progress".to_string())
(5, "in-progress".to_string()),
]
);
}
Expand Down
7 changes: 5 additions & 2 deletions raphtory/src/db/graph/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::core::entities::properties::props::DictMapper;
use itertools::Itertools;
use std::sync::Arc;

pub mod edge;
Expand All @@ -17,10 +18,12 @@ pub(crate) fn create_node_type_filter(
let mut bool_arr = vec![false; len];

for nt in node_types {
if let Some(id) = dict_mapper.get_id(nt.as_ref()) {
let nt = nt.as_ref();
if nt.is_empty() {
bool_arr[0] = true;
} else if let Some(id) = dict_mapper.get_id(nt) {
bool_arr[id] = true;
}
}

bool_arr.into()
}

0 comments on commit 62b999a

Please sign in to comment.