U
    ª9%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"< qèe 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-API/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relabeléd   é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   




