~ubuntu-branches/ubuntu/utopic/python-networkx/utopic

« back to all changes in this revision

Viewing changes to networkx/algorithms/assortativity/pairs.py

  • Committer: Package Import Robot
  • Author(s): Sandro Tosi
  • Date: 2012-08-11 12:41:30 UTC
  • mfrom: (1.4.1) (5.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20120811124130-whr6uso7fncyg8bi
Tags: 1.7-1
* New upstream release
* debian/patches/changeset_*
  - removed, included upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#-*- coding: utf-8 -*-
 
2
"""Generators of  x-y pairs of node data."""
 
3
import networkx as nx
 
4
from networkx.utils import dict_to_numpy_array
 
5
__author__ = ' '.join(['Aric Hagberg <aric.hagberg@gmail.com>'])
 
6
__all__ = ['node_attribute_xy',
 
7
           'node_degree_xy']
 
8
 
 
9
def node_attribute_xy(G, attribute, nodes=None):
 
10
    """Return 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
    node = G.node 
 
48
    for u,nbrsdict in G.adjacency_iter():
 
49
        if u not in nodes:
 
50
            continue
 
51
        uattr = node[u].get(attribute,None)
 
52
        if G.is_multigraph():
 
53
            for v,keys in nbrsdict.items():
 
54
                vattr = node[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 = node[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_iter
 
111
    ydeg = G.degree_iter
 
112
    if G.is_directed():
 
113
        direction = {'out':G.out_degree_iter,
 
114
                     'in':G.in_degree_iter}
 
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_iter(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")