ó ŸÃÒYc@spdZddgZddlZd„Zd„Zddedddd„Zddeddd„Z d „Z dS( sK Functions for constructing matrix-like objects from graph attributes. t attr_matrixtattr_sparse_matrixiÿÿÿÿNcsFˆdkrd„}n*tˆdƒs<‡‡fd†}nˆ}|S(sçReturns a function that returns a value from G.nodes[u]. We return a function expecting a node as its sole argument. Then, in the simplest scenario, the returned function will return G.nodes[u][node_attr]. However, we also handle the case when `node_attr` is None or when it is a function itself. Parameters ---------- G : graph A NetworkX graph node_attr : {None, str, callable} Specification of how the value of the node attribute should be obtained from the node attribute dictionary. Returns ------- value : function A function expecting a node as its sole argument. The function will returns a value from G.nodes[u] that depends on `edge_attr`. cSs|S(N((tu((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/attrmatrix.pytvalue#st__call__csˆj|ˆS(N(tnodes(R(tGt node_attr(sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/attrmatrix.pyR&sN(tNonethasattr(RRR((RRsp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/attrmatrix.pyt _node_value s   csLjd kr6ˆjƒr*‡fd†}qÃd„}ntˆdƒs½ˆdkr‡ˆjƒrr‡‡fd†}qº‡‡fd†}qÈjƒr¨‡‡fd†}qLJfd†}nˆ}|S( sÎReturns a function that returns a value from G[u][v]. Suppose there exists an edge between u and v. Then we return a function expecting u and v as arguments. For Graph and DiGraph, G[u][v] is the edge attribute dictionary, and the function (essentially) returns G[u][v][edge_attr]. However, we also handle cases when `edge_attr` is None and when it is a function itself. For MultiGraph and MultiDiGraph, G[u][v] is a dictionary of all edges between u and v. In this case, the returned function sums the value of `edge_attr` for every edge between u and v. Parameters ---------- G : graph A NetworkX graph edge_attr : {None, str, callable} Specification of how the value of the edge attribute should be obtained from the edge attribute dictionary, G[u][v]. For multigraphs, G[u][v] is a dictionary of all the edges between u and v. This allows for special treatment of multiedges. Returns ------- value : function A function expecting two nodes as parameters. The nodes should represent the from- and to- node of an edge. The function will return a value from G[u][v] that depends on `edge_attr`. cstˆ||ƒS(N(tlen(Rtv(R(sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/attrmatrix.pyRUscSsdS(Ni((RR ((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/attrmatrix.pyRWsRtweightcs7tgˆ||jƒD]}|jˆdƒ^qƒS(Ni(tsumtvaluestget(RR td(Rt edge_attr(sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/attrmatrix.pyR_scsˆ||jˆdƒS(Ni(R(RR (RR(sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/attrmatrix.pyRascs/tgˆ||jƒD]}|ˆ^qƒS(N(RR(RR R(RR(sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/attrmatrix.pyRescsˆ||ˆS(N((RR (RR(sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/attrmatrix.pyRgsN(Rt is_multigraphR (RRR((RRsp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/attrmatrix.pyt _edge_value2s      cCsyddl}Wntk r/tdƒ‚nXt||ƒ}t||ƒ} |dkrˆttg|D]} | | ƒ^qgƒƒ} n|} t| ƒ} |jƒ } t t | t | ƒƒƒ}|j | | fd|d|ƒ}tgƒ}x³|j ƒD]¥\}}x€|D]x}|| |ƒ|| |ƒ}}||kr|||fc|||ƒ7<| r‡|||f|||f>> G = nx.Graph() >>> G.add_edge(0,1,thickness=1,weight=3) >>> G.add_edge(0,2,thickness=2) >>> G.add_edge(1,2,thickness=3) >>> nx.attr_matrix(G, rc_order=[0,1,2]) matrix([[ 0., 1., 1.], [ 1., 0., 1.], [ 1., 1., 0.]]) Alternatively, we can obtain the matrix describing edge thickness. >>> nx.attr_matrix(G, edge_attr='thickness', rc_order=[0,1,2]) matrix([[ 0., 1., 2.], [ 1., 0., 3.], [ 2., 3., 0.]]) We can also color the nodes and ask for the probability distribution over all edges (u,v) describing: Pr(v has color Y | u has color X) >>> G.nodes[0]['color'] = 'red' >>> G.nodes[1]['color'] = 'red' >>> G.nodes[2]['color'] = 'blue' >>> rc = ['red', 'blue'] >>> nx.attr_matrix(G, node_attr='color', normalized=True, rc_order=rc) matrix([[ 0.33333333, 0.66666667], [ 1. , 0. ]]) For example, the above tells us that for all edges (u,v): Pr( v is red | u is red) = 1/3 Pr( v is blue | u is red) = 2/3 Pr( v is red | u is blue) = 1 Pr( v is blue | u is blue) = 0 Finally, we can obtain the total weights listed by the node colors. >>> nx.attr_matrix(G, edge_attr='weight', node_attr='color', rc_order=rc) matrix([[ 3., 2.], [ 2., 0.]]) Thus, the total weight over all edges (u,v) with u and v having colors: (red, red) is 3 # the sole contribution is from edge (0,1) (red, blue) is 2 # contributions from edges (0,2) and (1,2) (blue, red) is 2 # same as (red, blue) since graph is undirected (blue, blue) is 0 # there are no edges with blue endpoints iÿÿÿÿNs0attr_matrix() requires numpy: http://scipy.org/ tdtypetordertaxisi(tnumpyt ImportErrorRR RtlisttsetR t is_directedtdicttziptrangetzerost adjacencytaddRtreshapetasmatrix(RRRt normalizedtrc_orderRRtnpt edge_valuet node_valuetntorderingtNt undirectedtindextMtseenRtnbrdictR titj((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/attrmatrix.pyR}s<w   .  !  ! $(  cCs0y ddl}ddlm}Wntk r?tdƒ‚nXt||ƒ}t||ƒ} |dkr˜ttg|D]} | | ƒ^qwƒƒ} n|} t | ƒ} |j ƒ } t t | t | ƒƒƒ}|j| | fd|ƒ}tgƒ}x³|jƒD]¥\}}x€|D]x}|| |ƒ|| |ƒ}}||kr|||fc|||ƒ7<| r‘|||f|||f>> G = nx.Graph() >>> G.add_edge(0,1,thickness=1,weight=3) >>> G.add_edge(0,2,thickness=2) >>> G.add_edge(1,2,thickness=3) >>> M = nx.attr_sparse_matrix(G, rc_order=[0,1,2]) >>> M.todense() matrix([[ 0., 1., 1.], [ 1., 0., 1.], [ 1., 1., 0.]]) Alternatively, we can obtain the matrix describing edge thickness. >>> M = nx.attr_sparse_matrix(G, edge_attr='thickness', rc_order=[0,1,2]) >>> M.todense() matrix([[ 0., 1., 2.], [ 1., 0., 3.], [ 2., 3., 0.]]) We can also color the nodes and ask for the probability distribution over all edges (u,v) describing: Pr(v has color Y | u has color X) >>> G.nodes[0]['color'] = 'red' >>> G.nodes[1]['color'] = 'red' >>> G.nodes[2]['color'] = 'blue' >>> rc = ['red', 'blue'] >>> M = nx.attr_sparse_matrix(G, node_attr='color', normalized=True, rc_order=rc) >>> M.todense() matrix([[ 0.33333333, 0.66666667], [ 1. , 0. ]]) For example, the above tells us that for all edges (u,v): Pr( v is red | u is red) = 1/3 Pr( v is blue | u is red) = 2/3 Pr( v is red | u is blue) = 1 Pr( v is blue | u is blue) = 0 Finally, we can obtain the total weights listed by the node colors. >>> M = nx.attr_sparse_matrix(G, edge_attr='weight', node_attr='color', rc_order=rc) >>> M.todense() matrix([[ 3., 2.], [ 2., 0.]]) Thus, the total weight over all edges (u,v) with u and v having colors: (red, red) is 3 # the sole contribution is from edge (0,1) (red, blue) is 2 # contributions from edges (0,2) and (1,2) (blue, red) is 2 # same as (red, blue) since graph is undirected (blue, blue) is 0 # there are no edges with blue endpoints iÿÿÿÿN(tsparses7attr_sparse_matrix() requires scipy: http://scipy.org/ RRi(RtscipyR4RRR RRRR RRRRt lil_matrixR!R"tasarrayRtravelt enumerate(RRRR%R&RR'R4R(R)R*R+R,R-R.R/R0RR1R R2R3tnormstnorm((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/attrmatrix.pyRs@x    .    ! $!#  cCs`ddlm}yddl}Wn|dƒ‚nXyddl}Wn|dƒ‚nXdS(Niÿÿÿÿ(tSkipTestsNumPy not availablesSciPy not available(tnoseR<RR5(tmoduleR<RR5((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/attrmatrix.pyt setup_moduleÄs( t__doc__t__all__tnetworkxtnxR RRtFalseRRR?(((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/attrmatrix.pyts   ( K ¡¤