Skip to content

Commit

Permalink
Support avg_clustering builtin app in client side (#1475)
Browse files Browse the repository at this point in the history
* Support avg_clustering builtin app in client side
* Check directed/undirected in not_compatible_for decorator
  • Loading branch information
lidongze0629 committed Apr 23, 2022
1 parent f27689c commit daa8649
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/analytics_engine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ property graph or not is described in its docstring.
- :func:`average_shortest_path_length`
- :func:`attribute_assortativity_coefficient`
- :func:`bfs`
- :func:`avg_clustering`
- :func:`clustering`
- :func:`degree_centrality`
- :func:`degree_assortativity_coefficient`
Expand Down
1 change: 1 addition & 0 deletions docs/reference/app.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ BuiltIn apps
.. autofunction:: graphscope.pagerank
.. autofunction:: graphscope.sssp
.. autofunction:: graphscope.wcc
.. autofunction:: graphscope.avg_clustering
.. autofunction:: graphscope.clustering
.. autofunction:: graphscope.degree_centrality
.. autofunction:: graphscope.eigenvector_centrality
Expand Down
1 change: 1 addition & 0 deletions docs/zh/analytics_engine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ GraphScope 图分析引擎内置了许多常用的图分析算法,包括连通
- :func:`average_shortest_path_length`
- :func:`attribute_assortativity_coefficient`
- :func:`bfs`
- :func:`avg_clustering`
- :func:`clustering`
- :func:`degree_centrality`
- :func:`degree_assortativity_coefficient`
Expand Down
1 change: 1 addition & 0 deletions python/graphscope/analytical/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

# fmt: on
from graphscope.analytical.app.bfs import bfs
from graphscope.analytical.app.clustering import avg_clustering
from graphscope.analytical.app.clustering import clustering

# fmt: off
Expand Down
31 changes: 30 additions & 1 deletion python/graphscope/analytical/app/clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from graphscope.framework.app import not_compatible_for
from graphscope.framework.app import project_to_simple

__all__ = ["clustering"]
__all__ = ["avg_clustering", "clustering"]


@project_to_simple
Expand Down Expand Up @@ -54,3 +54,32 @@ def clustering(graph):
return AppAssets(algo="clustering", context="vertex_data")(graph)
else:
return AppAssets(algo="lcc", context="vertex_data")(graph)


@project_to_simple
@not_compatible_for("arrow_property", "dynamic_property", "undirected")
def avg_clustering(graph):
"""Compute the average clustering coefficient for the directed graph.
Args:
graph (:class:`graphscope.Graph`): A simple graph.
Returns:
r: float
The average clustering coefficient.
Examples:
.. code:: python
>>> import graphscope
>>> from graphscope.dataset import load_p2p_network
>>> sess = graphscope.session(cluster_type="hosts", mode="eager")
>>> g = load_p2p_network(sess)
>>> # project to a simple graph
>>> pg = g.project(vertices={"host": ["id"]}, edges={"connect": ["dist"]})
>>> c = graphscope.avg_clustering(pg)
>>> print(c.to_numpy("r", axis=0)[0])
>>> sess.close()
"""
return AppAssets(algo="avg_clustering", context="tensor")(graph)
8 changes: 6 additions & 2 deletions python/graphscope/framework/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def not_compatible_for(*graph_types):
Args:
graph_types: list of string
Entries must be one of 'arrow_property', 'dynamic_property', 'arrow_projected', 'dynamic_projected'
Entries must be one of 'arrow_property', 'dynamic_property',
'arrow_projected', 'dynamic_projected', 'directed', 'undirected'
Returns:
The decorated function.
Expand Down Expand Up @@ -118,14 +119,17 @@ def wrapper(*args, **kwargs):
"dynamic_projected": graph.graph_type
== graph_def_pb2.DYNAMIC_PROJECTED,
"arrow_flattened": graph.graph_type == graph_def_pb2.ARROW_FLATTENED,
"directed": graph.is_directed(),
"undirected": graph.is_directed() is False,
}
match = False
try:
for t in graph_types:
match = match or terms[t]
except KeyError:
raise InvalidArgumentError(
"Use one or more of arrow_property,dynamic_property,arrow_projected,dynamic_projected,arrow_flattened",
"Use one or more of arrow_property,dynamic_property,"
"arrow_projected,dynamic_projected,arrow_flattened,directed,undirected",
)
if match:
raise InvalidArgumentError(
Expand Down
12 changes: 10 additions & 2 deletions python/graphscope/framework/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class GraphInterface(metaclass=ABCMeta):

def __init__(self):
self._session = None
self._directed = False

@abstractmethod
def add_column(self, results, selector):
Expand All @@ -78,6 +79,9 @@ def add_edges(
):
raise NotImplementedError

def is_directed(self):
return self._directed

def to_numpy(self, selector, vertex_range=None):
raise NotImplementedError

Expand Down Expand Up @@ -297,7 +301,9 @@ def _project_to_simple(self, v_prop=None, e_prop=None):
self, str(v_prop), str(e_prop)
)
# construct dag node
graph_dag_node = GraphDAGNode(self._session, op)
graph_dag_node = GraphDAGNode(
self._session, op, self._oid_type, self._directed, self._generate_eid
)
graph_dag_node._base_graph = self
return graph_dag_node

Expand Down Expand Up @@ -646,7 +652,9 @@ def project(
self, json.dumps(vertices), json.dumps(edges)
)
# construct dag node
graph_dag_node = GraphDAGNode(self._session, op)
graph_dag_node = GraphDAGNode(
self._session, op, self._oid_type, self._directed, self._generate_eid
)
graph_dag_node._base_graph = self
return graph_dag_node

Expand Down
6 changes: 6 additions & 0 deletions python/graphscope/tests/unittest/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import pytest

import graphscope
from graphscope import avg_clustering
from graphscope import bfs
from graphscope import clustering
from graphscope import degree_centrality
Expand Down Expand Up @@ -192,6 +193,11 @@ def test_run_app_on_directed_graph(
)
assert np.allclose(ret_clustering, clustering_result["directed"])

# avg_clustering
ctx_avg_clustering = avg_clustering(p2p_project_directed_graph)
ret_avg_clustering = ctx_avg_clustering.to_numpy("r", axis=0)[0]
assert ret_avg_clustering is not None

# degree_centrality
ctx_dc = degree_centrality(p2p_project_directed_graph)
ret_dc = (
Expand Down

0 comments on commit daa8649

Please sign in to comment.