o
    ˷ew
                     @   sP  d Z ddlmZ ddlmZ ddlZddlZ	ddl
mZ ddlmZ dd ZedZeeeeZeeZeeZejeed	d
ZejedddZed ejeeddd dd e D Zdd ejd	dD Z i Z!eD ]Z"e	#dd eje" d D Z$e$j%dde!e"< qted ejee!ee dd e&d e'  dS )a}  
==========
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}
    }

    )defaultdictN)	hierarchy)distancec                 C   s   t | }tt| t| f}|D ]\}}| D ]
\}}||| |< qqt|}t	|}t
tj|dd}	tt
}
tt
tt| |	D ]\}}|
| | qHt
|
 S )z<Creates hierarchical cluster of graph G from distance matrixgffffff?)t)nxall_pairs_shortest_path_lengthnpzeroslenitemsr   
squareformr   completelistfclusterr   ziprangeappendvalues)Gpath_length	distancesupvdYZ
membership	partitionn r    |/var/www/ideatree/venv/lib/python3.10/site-packages/../../../share/doc/networkx-2.8.6/examples/algorithms/plot_blockmodel.py	create_hc!   s   


r"   zhartford_drug.edgelistT)relabeld   S   )
iterationsseed   F
   )with_labels	node_sizec                 C   s   g | ]}t j| d  d qS )nnodesr)   )BMnodes).0xr    r    r!   
<listcomp>E       r1   c                 C   s   g | ]\}}}d |d  qS )   weightr    )r/   r   r   r   r    r    r!   r1   F   r2   )datac                 C   s   g | ]}t | qS r    )pos)r/   r   r    r    r!   r1   J   s    graph)axis   )r+   widthr*   off)(__doc__collectionsr   matplotlib.pyplotpyplotpltnetworkxr   numpyr   scipy.clusterr   scipy.spatialr   r"   read_edgelistr   subgraphnextconnected_componentsHconvert_node_labels_to_integers
partitionsquotient_graphr-   spring_layoutr6   subplotdrawr.   r+   edges
edge_widthposBMr   arrayxymeanr8   showr    r    r    r!   <module>   s4    




