U
    -ev
                     @   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 ]2Z"e	#dd eje" d D Z$e$j%dde!e"< qe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 ]\}}||| |< q4q$t|}t	|}t
tj|dd}	tt
}
tt
tt| |	D ]\}}|
| | qt
|
 S )z<Creates hierarchical cluster of graph G from distance matrixgffffff?)t)nxZall_pairs_shortest_path_lengthnpZzeroslenitemsr   Z
squareformr   ZcompletelistZfclusterr   ziprangeappendvalues)GZpath_lengthZ	distancesupvdYZZ
membership	partitionn r   /var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/../../../share/doc/networkx-3.1/examples/algorithms/plot_blockmodel.py	create_hc!   s    


r   zhartford_drug.edgelistT)Zrelabeld   S   )Z
iterationsseed   F
   )with_labels	node_sizec                 C   s   g | ]}t j| d  d qS )Znnodesr   )BMnodes).0xr   r   r   
<listcomp>E   s     r&   c                 C   s   g | ]\}}}d |d  qS )   weightr   )r$   r   r   r   r   r   r   r&   F   s     )datac                 C   s   g | ]}t | qS r   )pos)r$   r   r   r   r   r&   J   s     graph)axis   )r!   widthr    off)(__doc__collectionsr   Zmatplotlib.pyplotZpyplotZpltZnetworkxr   numpyr   Zscipy.clusterr   Zscipy.spatialr   r   Zread_edgelistr   ZsubgraphnextZconnected_componentsHZconvert_node_labels_to_integersZ
partitionsZquotient_graphr"   Zspring_layoutr*   ZsubplotZdrawr#   r!   edgesZ
edge_widthZposBMr   arrayZxyZmeanr,   showr   r   r   r   <module>   s2   




