Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add edge count backend call #49

Merged
merged 5 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 35 additions & 18 deletions grand/backends/_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,13 @@ def all_nodes_as_iterable(self, include_metadata: bool = False):
if self._node_df is not None:
return [
(
node_id,
row.to_dict(),
(
node_id,
row.to_dict(),
)
if include_metadata
else node_id
)
if include_metadata
else node_id
for node_id, row in self._node_df.iterrows()
]

Expand Down Expand Up @@ -287,14 +289,12 @@ def get_edge_by_id(self, u: Hashable, v: Hashable):

"""
if self._directed:
return (
self._edge_df[
(self._edge_df[self._edge_df_source_column] == u)
& (self._edge_df[self._edge_df_target_column] == v)
]
.iloc[0]
.to_dict()
)
result = self._edge_df[
(self._edge_df[self._edge_df_source_column] == u)
& (self._edge_df[self._edge_df_target_column] == v)
]
if len(result):
return self._edge_as_dict(result.iloc[0])

else:
left = self._edge_df[
Expand Down Expand Up @@ -355,9 +355,11 @@ def get_node_neighbors(self, u: Hashable, include_metadata: bool = False):
else:
return iter(
[
row[self._edge_df_source_column]
if row[self._edge_df_source_column] != u
else row[self._edge_df_target_column]
(
row[self._edge_df_source_column]
if row[self._edge_df_source_column] != u
else row[self._edge_df_target_column]
)
for _, row in self._edge_df[
(self._edge_df[self._edge_df_source_column] == u)
| (self._edge_df[self._edge_df_target_column] == u)
Expand Down Expand Up @@ -430,9 +432,11 @@ def get_node_predecessors(self, u: Hashable, include_metadata: bool = False):
else:
return iter(
[
row[self._edge_df_source_column]
if row[self._edge_df_target_column] != u
else row[self._edge_df_target_column]
(
row[self._edge_df_source_column]
if row[self._edge_df_target_column] != u
else row[self._edge_df_target_column]
)
for _, row in self._edge_df[
(self._edge_df[self._edge_df_target_column] == u)
| (self._edge_df[self._edge_df_source_column] == u)
Expand Down Expand Up @@ -460,6 +464,19 @@ def get_node_count(self) -> int:
)
)

def get_edge_count(self) -> int:
"""
Get an integer count of the number of edges in this graph.

Arguments:
None

Returns:
int: The count of edges

"""
return len(self._edge_df)

def ingest_from_edgelist_dataframe(
self, edgelist: pd.DataFrame, source_column: str, target_column: str
) -> dict:
Expand Down
33 changes: 26 additions & 7 deletions grand/backends/_dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,13 @@ def all_nodes_as_iterable(self, include_metadata: bool = False) -> Collection:
"""
return [
(
node[self._primary_key],
{k: v for k, v in node.items() if k not in [self._primary_key]},
(
node[self._primary_key],
{k: v for k, v in node.items() if k not in [self._primary_key]},
)
if include_metadata
else node[self._primary_key]
)
if include_metadata
else node[self._primary_key]
for node in self._scan_table(self._node_table)
]

Expand Down Expand Up @@ -264,9 +266,11 @@ def all_edges_as_iterable(self, include_metadata: bool = False) -> Collection:

"""
return [
(edge[self._edge_source_key], edge[self._edge_target_key], edge)
if include_metadata
else (edge[self._edge_source_key], edge[self._edge_target_key])
(
(edge[self._edge_source_key], edge[self._edge_target_key], edge)
if include_metadata
else (edge[self._edge_source_key], edge[self._edge_target_key])
)
for edge in self._scan_table(self._edge_table)
]

Expand Down Expand Up @@ -435,6 +439,21 @@ def get_node_count(self) -> int:
"ItemCount"
]

def get_edge_count(self) -> int:
"""
Get an integer count of the number of edges in this graph.

Arguments:
None

Returns:
int: The count of edges

"""
return self._client.describe_table(TableName=self._edge_table_name)["Table"][
"ItemCount"
]

# Ingesting

def ingest_from_edgelist_dataframe(
Expand Down
13 changes: 13 additions & 0 deletions grand/backends/_gremlin.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,5 +314,18 @@ def get_node_count(self) -> int:
"""
return self._g.V().count().toList()[0]

def get_edge_count(self) -> int:
"""
Get an integer count of the number of edges in this graph.

Arguments:
None

Returns:
int: The count of edges

"""
return self._g.E().count().toList()[0]

def teardown(self) -> None:
self._g.V().drop().toList()
13 changes: 13 additions & 0 deletions grand/backends/_igraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,16 @@ def get_node_count(self) -> int:

"""
return self._ig.vcount()

def get_edge_count(self) -> int:
"""
Get an integer count of the number of edges in this graph.

Arguments:
None

Returns:
int: The count of edges

"""
return self._ig.ecount()
17 changes: 15 additions & 2 deletions grand/backends/_networkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def get_node_predecessors(
[self._names.get_name(i) for i in self._nk_graph.iterInNeighbors(my_id)]
)

def get_node_count(self) -> Iterable:
def get_node_count(self) -> int:
"""
Get an integer count of the number of nodes in this graph.

Expand All @@ -292,4 +292,17 @@ def get_node_count(self) -> Iterable:
int: The count of nodes

"""
return len([i for i in self.all_nodes_as_iterable()])
return self._nk_graph.numberOfNodes()

def get_edge_count(self) -> int:
"""
Get an integer count of the number of edges in this graph.

Arguments:
None

Returns:
int: The count of edges

"""
return self._nk_graph.numberOfEdges()
16 changes: 13 additions & 3 deletions grand/backends/_networkx.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,19 @@ def get_node_count(self) -> int:
"""
return len(self._nx_graph)

def get_edge_count(self) -> int:
"""
Get an integer count of the number of edges in this graph.

Arguments:
None

Returns:
int: The count of edges

"""
return len(self._nx_graph.edges)

def ingest_from_edgelist_dataframe(
self, edgelist: pd.DataFrame, source_column: str, target_column: str
) -> dict:
Expand Down Expand Up @@ -200,6 +213,3 @@ def ingest_from_edgelist_dataframe(
"edge_count": len(edgelist),
"edge_duration": time.time() - tic,
}

def teardown(self) -> None:
return
Loading
Loading