ó
 ,µ[c        	   @   s  d  Z  d d l m Z d d l j Z d d l Z d d l Z d d l	 m
 Z
 d d l m Z d „  Z e d k re j d ƒ Z e e j e ƒ ƒ Z e j e ƒ Z e e ƒ Z e j e e d	 e ƒZ e j e d
 d ƒZ e j d ƒ e j e e d e d d ƒg  e j ƒ  D] Z e j e d d ^ qZ  g  e j! d e ƒ D] \ Z" Z# Z$ d e$ d ^ qHZ% i  Z& xU e D]M Z' e j( g  e j e' d D] Z" e e" ^ q–ƒ Z) e) j* d d ƒ e& e' <qxWe j d ƒ e j e e& d e  d e% d e ƒe j+ d ƒ e j, ƒ  n  d S(   s}  
==========
Blockmodel
==========

Example of creating a block model using the quotient_graph function in NX.  Data
used is the Hartford, CT drug users network::

    @article{weeks2002social,
      title={Social networks of drug users in high-risk sites: Finding the connections},
      url = {https://doi.org/10.1023/A:1015457400897},
      doi = {10.1023/A:1015457400897},
      author={Weeks, Margaret R and Clair, Scott and Borgatti, Stephen P and Radda, Kim and Schensul, Jean J},
      journal={{AIDS and Behavior}},
      volume={6},
      number={2},
      pages={193--206},
      year={2002},
      publisher={Springer}
    }

iÿÿÿÿ(   t   defaultdictN(   t	   hierarchy(   t   distancec         C   s
  t  j |  ƒ } t j t |  ƒ t |  ƒ f ƒ } x? | D]7 \ } } x( | j ƒ  D] \ } } | | | | <qP Wq7 Wt j | ƒ } t j	 | ƒ } t
 t j | d d ƒƒ }	 t t
 ƒ }
 x@ t t
 t t |  ƒ ƒ ƒ |	 ƒ D] \ } } |
 | j | ƒ qÙ Wt
 |
 j ƒ  ƒ S(   s<   Creates hierarchical cluster of graph G from distance matrixt   tgffffffò?(   t   nxt   all_pairs_shortest_path_lengtht   numpyt   zerost   lent   itemsR   t
   squareformR   t   completet   listt   fclusterR    t   zipt   ranget   appendt   values(   t   Gt   path_lengtht	   distancest   ut   pt   vt   dt   Yt   Zt
   membershipt	   partitiont   n(    (    s=   share/doc/networkx-2.2/examples/algorithms/plot_blockmodel.pyt	   create_hc$   s    !.t   __main__s   hartford_drug.edgelistt   relabelt
   iterationsid   iÓ   t   with_labelst	   node_sizei
   t   nnodest   datai   t   weightt   grapht   axisi    iÔ   t   widtht   off(-   t   __doc__t   collectionsR    t   matplotlib.pyplott   pyplott   pltt   networkxR   R   t   scipy.clusterR   t   scipy.spatialR   R   t   __name__t   read_edgelistR   t   nextt   connected_component_subgraphst   Ht   convert_node_labels_to_integerst
   partitionst   quotient_grapht   Truet   BMt   spring_layoutt   post   subplott   drawt   Falset   nodest   xR#   t   edgesR   R   R   t
   edge_widtht   posBMR   t   arrayt   xyt   meanR(   t   show(    (    (    s=   share/doc/networkx-2.2/examples/algorithms/plot_blockmodel.pyt   <module>   s4   	.61"