<gh_stars>1000+
#!/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: <NAME>
#


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"]


@project_to_simple
@not_compatible_for("arrow_property")
def degree_assortativity_coefficient(G, 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
    ----------
    G : NetworkX 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 as gs
        sess = gs.session()
        g = sess.g()
        pg = g.project(vertices={"vlabel": []}, edges={"elabel": []})
        r = gs.degree_assortativity_coefficient(pg)
        s.close()

    See Also
    --------
    attribute_assortativity_coefficient

    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] <NAME>, Mixing patterns in networks,
       Physical Review E, 67 026126, 2003
    .. [2] <NAME>., <NAME>., <NAME>. & <NAME>.
       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")(
        G,
        source_degree_type=x,
        target_degree_type=y,
        weighted=weighted,
    )
    return ctx.to_numpy("r", axis=0)[0]

#!/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: <NAME>
#


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"]


@project_to_simple
@not_compatible_for("arrow_property")
def degree_assortativity_coefficient(G, 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
    ----------
    G : NetworkX 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 as gs
        sess = gs.session()
        g = sess.g()
        pg = g.project(vertices={"vlabel": []}, edges={"elabel": []})
        r = gs.degree_assortativity_coefficient(pg)
        s.close()

    See Also
    --------
    attribute_assortativity_coefficient

    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] <NAME>, Mixing patterns in networks,
       Physical Review E, 67 026126, 2003
    .. [2] <NAME>., <NAME>., <NAME>. & <NAME>.
       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")(
        G,
        source_degree_type=x,
        target_degree_type=y,
        weighted=weighted,
    )
    return ctx.to_numpy("r", axis=0)[0]
#!/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: <NAME> # Compute degree assortativity of graph.      Assortativity measures the similarity of connections     in the graph with respect to the node degree.      Parameters     ----------     G : NetworkX 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 as gs         sess = gs.session()         g = sess.g()         pg = g.project(vertices={"vlabel": []}, edges={"elabel": []})         r = gs.degree_assortativity_coefficient(pg)         s.close()      See Also     --------     attribute_assortativity_coefficient      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] <NAME>, Mixing patterns in networks,        Physical Review E, 67 026126, 2003     .. [2] <NAME>., <NAME>., <NAME>. & <NAME>.        Edge direction and the structure of networks, PNAS 107, 10815-20 (2010).