Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / algorithms / assortativity / pairs.py @ 5cef0f13

History | View | Annotate | Download (3.6 KB)

1
#-*- coding: utf-8 -*-
2
"""Generators of  x-y pairs of node data."""
3
import networkx as nx
4
__author__ = ' '.join(['Aric Hagberg <aric.hagberg@gmail.com>'])
5
__all__ = ['node_attribute_xy',
6
           'node_degree_xy']
7

    
8

    
9
def node_attribute_xy(G, attribute, nodes=None):
10
    """Returns iterator of node-attribute pairs for all edges in G.
11

12
    Parameters
13
    ----------
14
    G: NetworkX graph
15

16
    attribute: key
17
       The node attribute key.
18

19
    nodes: list or iterable (optional)
20
        Use only edges that are adjacency to specified nodes.
21
        The default is all nodes.
22

23
    Returns
24
    -------
25
    (x,y): 2-tuple
26
        Generates 2-tuple of (attribute,attribute) values.
27

28
    Examples
29
    --------
30
    >>> G = nx.DiGraph()
31
    >>> G.add_node(1,color='red')
32
    >>> G.add_node(2,color='blue')
33
    >>> G.add_edge(1,2)
34
    >>> list(nx.node_attribute_xy(G,'color'))
35
    [('red', 'blue')]
36

37
    Notes
38
    -----
39
    For undirected graphs each edge is produced twice, once for each edge 
40
    representation (u,v) and (v,u), with the exception of self-loop edges 
41
    which only appear once.
42
    """
43
    if nodes is None:
44
        nodes = set(G)
45
    else:
46
        nodes = set(nodes)
47
    Gnodes = G.nodes
48
    for u, nbrsdict in G.adjacency():
49
        if u not in nodes:
50
            continue
51
        uattr = Gnodes[u].get(attribute, None)
52
        if G.is_multigraph():
53
            for v, keys in nbrsdict.items():
54
                vattr = Gnodes[v].get(attribute, None)
55
                for k, d in keys.items():
56
                    yield (uattr, vattr)
57
        else:
58
            for v, eattr in nbrsdict.items():
59
                vattr = Gnodes[v].get(attribute, None)
60
                yield (uattr, vattr)
61

    
62

    
63
def node_degree_xy(G, x='out', y='in', weight=None, nodes=None):
64
    """Generate node degree-degree pairs for edges in G.
65

66
    Parameters
67
    ----------
68
    G: NetworkX graph
69

70
    x: string ('in','out')
71
       The degree type for source node (directed graphs only).
72

73
    y: string ('in','out')
74
       The degree type for target node (directed graphs only).
75

76
    weight: string or None, optional (default=None)
77
       The edge attribute that holds the numerical value used 
78
       as a weight.  If None, then each edge has weight 1.
79
       The degree is the sum of the edge weights adjacent to the node.
80

81
    nodes: list or iterable (optional)
82
        Use only edges that are adjacency to specified nodes.
83
        The default is all nodes.
84

85
    Returns
86
    -------
87
    (x,y): 2-tuple
88
        Generates 2-tuple of (degree,degree) values.
89

90

91
    Examples
92
    --------
93
    >>> G = nx.DiGraph()
94
    >>> G.add_edge(1,2)
95
    >>> list(nx.node_degree_xy(G,x='out',y='in'))
96
    [(1, 1)]
97
    >>> list(nx.node_degree_xy(G,x='in',y='out'))
98
    [(0, 0)]
99

100
    Notes
101
    -----
102
    For undirected graphs each edge is produced twice, once for each edge 
103
    representation (u,v) and (v,u), with the exception of self-loop edges 
104
    which only appear once.
105
    """
106
    if nodes is None:
107
        nodes = set(G)
108
    else:
109
        nodes = set(nodes)
110
    xdeg = G.degree
111
    ydeg = G.degree
112
    if G.is_directed():
113
        direction = {'out': G.out_degree,
114
                     'in': G.in_degree}
115
        xdeg = direction[x]
116
        ydeg = direction[y]
117

    
118
    for u, degu in xdeg(nodes, weight=weight):
119
        neighbors = (nbr for _, nbr in G.edges(u) if nbr in nodes)
120
        for v, degv in ydeg(neighbors, weight=weight):
121
            yield degu, degv
122

    
123

    
124
# fixture for nose tests
125
def setup_module(module):
126
    from nose import SkipTest
127
    try:
128
        import numpy
129
    except:
130
        raise SkipTest("NumPy not available")
131
    try:
132
        import scipy
133
    except:
134
        raise SkipTest("SciPy not available")