#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2020 Alibaba Group Holding Limited. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Author: Ning Xin
#
from graphscope.framework.app import AppAssets
from graphscope.framework.app import not_compatible_for
from graphscope.framework.app import project_to_simple
__all__ = ["degree_assortativity_coefficient"]
[docs]@project_to_simple
@not_compatible_for("arrow_property")
def degree_assortativity_coefficient(graph, x="out", y="in", weight=None):
"""Compute degree assortativity of graph.
Assortativity measures the similarity of connections
in the graph with respect to the node degree.
Parameters
----------
graph (:class:`graphscope.Graph`): A simple graph.
x: string ('in','out')
The degree type for source node (directed graphs only).
y: string ('in','out')
The degree type for target node (directed graphs only).
weighted: bool (True, False)
weighted graph or unweighted graph
Returns
-------
r : float
Assortativity of graph by degree.
Examples
.. code:: python
>>> import graphscope
>>> from graphscope.dataset import load_modern_graph
>>> sess = graphscope.session(cluster_type="hosts", mode="eager")
>>> g = load_modern_graph(sess)
>>> g.schema
>>> c = graphscope.degree_assortativity_coefficient(g, weight="weight")
>>> sess.close()
Notes
-----
This computes Eq. (21) in Ref. [1]_ , where e is the joint
probability distribution (mixing matrix) of the degrees. If G is
directed than the matrix e is the joint probability of the
user-specified degree type for the source and target.
References
----------
.. [1] M. E. J. Newman, Mixing patterns in networks,
Physical Review E, 67 026126, 2003
.. [2] Foster, J.G., Foster, D.V., Grassberger, P. & Paczuski, M.
Edge direction and the structure of networks, PNAS 107, 10815-20 (2010).
"""
weighted = False if weight is None else True
ctx = AppAssets(algo="degree_assortativity_coefficient", context="tensor")(
graph,
source_degree_type=x,
target_degree_type=y,
weighted=weighted,
)
return ctx.to_numpy("r", axis=0)[0]