ó žÃÒYc@sÛdZddlZddlmZddlmZmZdjdgƒZ ddd d d d gZ de d „Z dded„Zdddde d„Zdddded„Zded„Ze d„Zd„ZdS(s1 Mixing matrices for node attributes and degree. iÿÿÿÿN(tdict_to_numpy_array(tnode_degree_xytnode_attribute_xyt s%Aric Hagberg tattribute_mixing_matrixtattribute_mixing_dicttdegree_mixing_matrixtdegree_mixing_dicttnumeric_mixing_matrixt mixing_dictcCs"t|||ƒ}t|d|ƒS(sIReturn dictionary representation of mixing matrix for attribute. Parameters ---------- G : graph NetworkX graph object. attribute : string Node attribute key. nodes: list or iterable (optional) Unse nodes in container to build the dict. The default is all nodes. normalized : bool (default=False) Return counts if False or probabilities if True. Examples -------- >>> G=nx.Graph() >>> G.add_nodes_from([0,1],color='red') >>> G.add_nodes_from([2,3],color='blue') >>> G.add_edge(1,3) >>> d=nx.attribute_mixing_dict(G,'color') >>> print(d['red']['blue']) 1 >>> print(d['blue']['red']) # d symmetric for undirected graphs 1 Returns ------- d : dictionary Counts or joint probability of occurrence of attribute pairs. t normalized(RR (tGt attributetnodesR txy_iter((s~/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/assortativity/mixing.pyRs"cCsAt|||ƒ}t|d|ƒ}|r=||jƒ}n|S(s«Return mixing matrix for attribute. Parameters ---------- G : graph NetworkX graph object. attribute : string Node attribute key. nodes: list or iterable (optional) Use only nodes in container to build the matrix. The default is all nodes. mapping : dictionary, optional Mapping from node attribute to integer index in matrix. If not specified, an arbitrary ordering will be used. normalized : bool (default=False) Return counts if False or probabilities if True. Returns ------- m: numpy array Counts or joint probability of occurrence of attribute pairs. tmapping(RRtsum(R R R RR tdta((s~/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/assortativity/mixing.pyR7s touttinc Cs4t|d|d|d|d|ƒ}t|d|ƒS(sReturn dictionary representation of mixing matrix for degree. Parameters ---------- G : graph NetworkX graph object. 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). weight: string or None, optional (default=None) The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. The degree is the sum of the edge weights adjacent to the node. normalized : bool (default=False) Return counts if False or probabilities if True. Returns ------- d: dictionary Counts or joint probability of occurrence of degree pairs. txtyR tweightR (RR (R RRRR R R((s~/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/assortativity/mixing.pyRZs$c CsÊt|d|d|d|d|ƒ}t|jƒƒ}x-|jƒD]\}} |j| jƒƒqCWt|ƒ} ttt| dƒt| dƒƒƒ} t |d| ƒ} |rÆ| | j ƒ} n| S(sqReturn mixing matrix for attribute. Parameters ---------- G : graph NetworkX graph object. 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). nodes: list or iterable (optional) Build the matrix using only nodes in container. The default is all nodes. weight: string or None, optional (default=None) The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. The degree is the sum of the edge weights adjacent to the node. normalized : bool (default=False) Return counts if False or probabilities if True. Returns ------- m: numpy array Counts, or joint probability, of occurrence of node degree. RRR RiR( RtsettkeystitemstupdatetmaxtdicttziptrangeRR( R RRRR R RtstktvtmRR((s~/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/assortativity/mixing.pyR{s $ )c Cs¸t|||ƒ}t|jƒƒ}x-|jƒD]\}}|j|jƒƒq1Wt|ƒ}ttt|dƒt|dƒƒƒ} t |d| ƒ} |r´| | j ƒ} n| S(s\Return numeric mixing matrix for attribute. The attribute must be an integer. Parameters ---------- G : graph NetworkX graph object. attribute : string Node attribute key. The corresponding attribute must be an integer. nodes: list or iterable (optional) Build the matrix only with nodes in container. The default is all nodes. normalized : bool (default=False) Return counts if False or probabilities if True. Returns ------- m: numpy array Counts, or joint, probability of occurrence of node attribute pairs. iR( RRRRRRRRRRR( R R R R RR R!R"R#RR((s~/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/assortativity/mixing.pyR¦s )c CsÒi}d}xx|D]p\}}||kr8i|||jƒD]-\}}x|D]} || c|:s(  & "  *# (