o
    3ήcJ                     @   sx   d dl Zd dlmZmZmZmZmZ dd Zdd Z	dd Z
d	d
 Zdd Zdd Zdd Zdd Zdd Zdd ZdS )    N)is_partitionlouvain_communitieslouvain_partitions
modularitypartition_qualityc               	   C   sR   t jdddddddd} d	d
 |  D }t| |}t| }t| ||ks'J d S )N            ?;On?      
   average_degreemin_communityseedc                 S   s   g | ]}|hqS  r   ).0ur   r   W/tmp/pip-target-vg8gfxp4/lib/python/networkx/algorithms/community/tests/test_louvain.py
<listcomp>   s    z,test_modularity_increase.<locals>.<listcomp>)nxLFR_benchmark_graphnodesr   r   )G	partitionmodr   r   r   test_modularity_increase   s   
r   c               	   C   sP   t jdddddddd} |  }t| }t|}t| |sJ t||s&J d S )	Nr   r   r	   r
   r   r   r   r   )r   r   to_directedr   r   )r   Hr   
partition2r   r   r   test_valid_partition   s   r!   c                  C   sB   t  } h dh dh dh dg}t| dd d}||ksJ d S )N   r         r      	                           r      r                                          r                   !   r$   r   weight)r   karate_club_graphr   )r   partr   r   r   r   test_karate_club_partition"   s   rG   c                  C   s`   t d} t| dd}t|}dd |D }|d |d ks J t|}|d |d ks.J d S )Nr;   *   r   c                 S   s   g | ]}|  qS r   )copy)r   sr   r   r   r   3   s    z+test_partition_iterator.<locals>.<listcomp>r   )r   
path_graphr   next)r   
parts_iter
first_part
first_copysecond_partr   r   r   test_partition_iterator/   s   
rR   c                  C   s   t  } t  }| td |g d g d}g d}| | || h dddhdhd	hd
dhddhg}t| ddd}h dh dh dg}t|ddd}||ksYJ ||ks_J dS )zS
    Test 2 cases that were looping infinitely
    from issues #5175 and #5704
    r   )r#   r$   r   r.   r   r/   r%   r9   r&   r   r'   ))r   r$   )r   r#   )r#   r   )r$   r#   )r$   r   r   r.   r.   r   )r%   r9   )r9   r%   r&   r   )r   r&   ))r#   r$   )r#   r/   )r#   r&   )r$   r   )r$   r.   )r$   r   rS   rT   )r.   r   )r   r.   )r/   r%   )r/   r9   rU   )r&   r'   )r   r'   )r'   r   >   r   r#   r$   r   r.   r   r/   r9   r%   r&   {   NrC   >   r$   r   r.   r   >   r#   r/   r%   r9   >   r&   r   r'   )r   DiGraphadd_nodes_fromrangeadd_edges_fromr   )r   r   G_edgesH_edgesG_expected_partitionG_partitionH_expected_partitionH_partitionr   r   r   test_directed_partition;   s   

$ra   c                  C   s   t  } t j| dd t| jD dd h dh dh dh dg}t| d d	d
}t| dd	d
}t| dd	d
}||ks=J ||ksCJ ||ksIJ ||ksOJ d S )Nc                 S   s   i | ]	\}}||| qS r   r   )r   iedger   r   r   
<dictcomp>s   s    z*test_none_weight_param.<locals>.<dictcomp>foo)namer"   r-   r1   r8   r$   rD   r   rD   )r   rE   set_edge_attributes	enumerateedgesr   )r   rF   
partition1r    
partition3r   r   r   test_none_weight_paramp   s    rm   c               	   C   s   t jdddddddd} t jd	d
d}t | }t |}t| }t|}t|}t|}t| |d }t||d }	t||d }
t||d }|dksOJ |	dksUJ |
dks[J |dksaJ d S )Nr   r   r	   r
   r   r   r   r        rI   r   g?)r   r   gn_graph
MultiGraphMultiDiGraphr   r   )r   r   IJr   r    rl   
partition4qualityquality2quality3quality4r   r   r   test_quality   s$   

rz   c                  C   s   t  } t | }| jdddd |jdddd | jdddd |jdddd t| dd	}t|dd	}t|d
dd}||  krG|ksJJ  J d S )Nr   r#   r   )rD   r&   r   )re   ro   rI   re   rg   )r   rE   rq   add_edger   )r   r   rk   r    rl   r   r   r   test_multigraph   s   
 r|   c               	   C   sl   t jdddddddd} t| d	d
d}t| d
d}t| dd
d}t|t|  kr1t|ks4J  J d S )Nr   r   r	   r
   r   r   r   r   g      ?r(   )
resolutionr   rI   r$   )r   r   r   len)r   rk   r    rl   r   r   r   test_resolution   s   ,r   c               	   C   sV   t jdddddddd} t| d	d
d}t| d
d}t| |}t| |}||k s)J d S )Nr   r   r	   r
   r   r   r   r   g333333?r$   )	thresholdr   rI   )r   r   r   r   )r   rk   r    mod1mod2r   r   r   test_threshold   s   

r   )networkxr   networkx.algorithms.communityr   r   r   r   r   r   r!   rG   rR   ra   rm   rz   r|   r   r   r   r   r   r   <module>   s    	5