o
    ˷emB                     @   sJ   d Z ddlZddlZedZed G dd dZdd Zd	d
 ZdS )z Unit tests for layout functions.    Nnumpyscipyc                   @   s   e Zd Ze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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)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9S ):
TestLayoutc                 C   s8   t dd| _t  | _t | jd t dd| _d S )N   abcdef   )nxgrid_2d_graphGiGraphGsadd_pathbigG)cls r   Y/var/www/ideatree/venv/lib/python3.10/site-packages/networkx/drawing/tests/test_layout.pysetup_class   s   
zTestLayout.setup_classc                 C   s\   t d}tjtt j|dgd ddd}tjtt j|ddg|d t j|dd	g|d d S )
N   r   )fixed   r   r   r   r      r   )r   posr   )r   
path_graphpytestraises
ValueErrorspring_layout)selfGr   r   r   r   test_spring_fixed_without_pos   s
   

z(TestLayout.test_spring_fixed_without_posc                    sf   dd l  t }|g d ddi}dg}tj|||d}t fdd| D }|r1J dd S )Nr   )r   r   r   r   )r   r   )r              r'   r   r   c                 3   s$    | ]}|D ]}  |V  qqd S N)isnan).0coordscmathr   r   	<genexpr>#   s   " z2TestLayout.test_spring_init_pos.<locals>.<genexpr>zvalues should not be nan)r/   r   r   add_edges_fromfruchterman_reingold_layoutanyvalues)r    r!   init_pos	fixed_posr   has_nanr   r.   r   test_spring_init_pos   s   zTestLayout.test_spring_init_posc                 C   sx   g }t | t | t | t | t | t | t | t || t 	| t 
| t | d S r)   )r   random_layoutcircular_layoutplanar_layoutr   r2   spectral_layoutshell_layoutbipartite_layoutspiral_layoutmultipartite_layoutkamada_kawai_layoutr    r!   r   r   r   test_smoke_empty_graph&   s   








z!TestLayout.test_smoke_empty_graphc                 C   s   | j }t| t| t| t| t| t| j t| t|	  t| j t| j	  t
| t| t| tj|dd tj|dd d S Nr   dimr%   )r
   r   r9   r:   r;   r   r2   r   r<   to_directedr=   r?   rA   rB   r   r   r   test_smoke_int4   s    








zTestLayout.test_smoke_intc                 C   s   | j }t| t| t| t| t| t| t| t	| t
| tj
|dd tj
|dd d S rD   )r   r   r9   r:   r;   r   r2   r<   r=   r?   rA   rB   r   r   r   test_smoke_stringF   s   








zTestLayout.test_smoke_stringc                 C   sx   t |}|| }|| }t t| }|d|d }|d| k s*J ||k s2J ||k s:J d S )Nr   r   )nparraylistr4   maxminall)r    r   scalecenterlowhivposlengthr   r   r   check_scale_and_centerT   s   
z!TestLayout.check_scale_and_centerc                 C   s   | j }d}td}|d |tj||dddd |tj|d|dd|d |tj|d|dd|d |tj|d|dd|d |tj|d|dd|d |tj	|d|dd|d |tj
|d|dd|d d}|tj
|d	d|d
d|d d S )N)r   r   	   rQ         ?)g      @g      @rP   rQ   r   )r   r%   r   r%   )rF   rP   rQ   rV   r   complete_graphadd_noder9   r   r<   r:   r=   r?   rA   r    scr-   r!   r   r   r   test_scale_and_center_arg^   s   

 z$TestLayout.test_scale_and_center_argc                 C   s    t d}tt jt j| d S )NrW   )r   r\   r   r   NetworkXExceptionr;   rB   r   r   r   #test_planar_layout_non_planar_inputo   s   
z.TestLayout.test_planar_layout_non_planar_inputc                 C   s4   t  }|ddgddgddgd t | d S )Nr   r   r   r   r   r   )r   PlanarEmbeddingset_datar;   )r    	embeddingr   r   r   (test_smoke_planar_layout_embedding_inputs   s   z3TestLayout.test_smoke_planar_layout_embedding_inputc                 C   s   | j }d}td}|d |t|ddd |t|d|d |t|d|d |t|d|d |t|d|d |t	|d|d |t
|d|d d}|tj
|dd	d|d d S )
Nr   rW   rY   rY   rY   rZ   r   )r   r   r   r%   rE   r[   r^   r   r   r   test_default_scale_and_centerx   s   

z(TestLayout.test_default_scale_and_centerc                 C   sr   t d}tjtt j|dd tjtt j|dd tjtt j|dd tjtt j|dd tjtt j|dd d S )Nr   r   rE   r%   )r   r   r   r   r   r:   r=   r;   rB   r   r   r   (test_circular_planar_and_shell_dim_error   s   
z3TestLayout.test_circular_planar_and_shell_dim_errorc                 C   sh   t | j}t jj|}|jdksJ t jjj|dd}|jdks$J t jj|}|jdks2J d S )N   r   r%   rE   rl   r%   )r   to_numpy_arrayr   drawinglayout_fruchterman_reingoldshape_sparse_fruchterman_reingoldr    Ar   r   r   r   test_adjacency_interface_numpy   s   z)TestLayout.test_adjacency_interface_numpyc                 C   sl   t j| jdd}t jj|}|jdksJ t jj|}|jdks$J t jjj|dd}|jdks4J d S )Nd)dtyperk   r%   rE   rm   )r   to_scipy_sparse_arrayr   ro   rp   rs   rr   _sparse_spectralrt   r   r   r   test_adjacency_interface_scipy   s   z)TestLayout.test_adjacency_interface_scipyc                 C   s   t d}t |}|d  rJ t d}t |dgddgdgg}|d  r,J |d  s4J tj|d dks@J t j|dgddgdggdd}tj|d dks[J d S )Nr   r   r   r   r%   )rotate)r   r   r=   r3   rJ   linalgnormr    r!   rT   r   r   r   test_single_nodes   s   


zTestLayout.test_single_nodesc                 C   s    t | j}t j| j|d}d S )N)r   )r   r:   r
   r2   )r    r   nposr   r   r   +test_smoke_initial_pos_fruchterman_reingold   s   z6TestLayout.test_smoke_initial_pos_fruchterman_reingoldc                 C   s   t | j}t j| j|dgd}t|d t|d ksJ t | j}t j| j|dgd}tdD ]}|d | tj|d | ddksGJ q2d S )Nr   r(   r   Hz>abs)	r   r:   r
   r   tupler   ranger   approx)r    r   r   axisr   r   r   $test_fixed_node_fruchterman_reingold   s   (z/TestLayout.test_fixed_node_fruchterman_reingoldc                 C   s
  t d}t j|dd t j|dd}t|d dksJ t j|dd}t|d dks.J t j|dd}t|d dks?J t j|dd}t|d dksPJ t j|dd}t|d dksaJ t j	|dd}t|d dksrJ t j
|dd}t|d dksJ d S )Nr   r   rX   r   )r   r   r9   r:   r   r;   r   r2   r<   r=   r?   r   r   r   r   test_center_parameter   s    
z TestLayout.test_center_parameterc                 C   s   t d}tt jtt jksJ tjtt j|dd tjtt j	|dd tjtt j
|dd tjtt j|dd tjtt j|ddd tjtt j|dd tjtt j|ddd tjtt j|dd tjtt j|dd tjtt j|dd d S )Nr   )r   r   r   rX   r%   r   )rF   rQ   )r   r   idr   r2   r   r   r   r9   r:   r;   r<   r=   r?   rA   rB   r   r   r   test_center_wrong_dimensions   s   
z'TestLayout.test_center_wrong_dimensionsc                 C   s(  t  }t j|dd}|i ksJ t j|dd}|i ksJ t j|dd}|i ks+J t ||}|i ks7J t j|dd}|i ksDJ t j|dd}|i ksQJ t j|dd}|i ks^J t j	|dd}|i kskJ t j
|dd}|i ksxJ t j|dd}|i ksJ t j|dd}|i ksJ d S )Nr   rX   )r   empty_graphr9   r:   r;   r>   r   r2   r<   r=   r?   r@   rA   r   r   r   r   test_empty_graph   s.   zTestLayout.test_empty_graphc           
      C   sH  t dd}t j|\}}t ||}t|t|ksJ |t|d  d }|t|d  d }|D ]}|| d |ks@J q4|D ]}|| d |ksOJ qCt j||ddddd}t|t|kseJ |t|d  d }|t|d  d }	|D ]}|| d |ksJ q{|D ]}|| d |	ksJ qtjt	t j||d	d
 d S )Nr%   r   r   
horizontalr   r   r   r   )alignrQ   rP   aspect_ratiofoor   )
r   complete_bipartite_graph	bipartitesetsr>   lenrL   r   r   r   )
r    r!   topbottomrT   top_xbottom_xnodetop_ybottom_yr   r   r   test_bipartite_layout   s*   z TestLayout.test_bipartite_layoutc                    s   d}t j| }t |tt|ksJ d |D ]} | }t fddt d |D s3J  |7  qt j|dddd	tt|ksKJ d |D ]} | }t fd
dt d |D shJ  |7  qOtjtt j|dd d S )N)r   r      r      r   c                 3   (    | ]}  d  | d  kV  qdS )r   Nr   r+   istartrT   r   r   r0        & z6TestLayout.test_multipartite_layout.<locals>.<genexpr>r   r   r   r   )r   rP   rQ   c                 3   r   )r   Nr   r   r   r   r   r0   %  r   r   r   )	r   complete_multipartite_graphr@   r   rO   r   r   r   r   )r    sizesr!   nendr   r   r   test_multipartite_layout  s"   

&
&
z#TestLayout.test_multipartite_layoutc                 C   s   t jjj}tddg}dtddgddgg }||t|ddd\}}|tjd	d
dks/J |d tjdd
dks<J |d tjdd
dksIJ d S )Ng      @g      @r   皙?g       @333333?r   
meanweightrF   g      ?r   r         rY   )r   ro   rp   _kamada_kawai_costfnrJ   rK   r   r   )r    costfnr   invdistcostgradr   r   r   test_kamada_kawai_costfn_1d*  s   
z&TestLayout.test_kamada_kawai_costfn_1dc              	   C   s  t jjj}|| t|||d\}}d| ttj|ddd  }t|jd D ])}	t|	d |jd D ]}
tj	
||	 ||
  }||||	 |
  d d 7 }q5q)|tj|dd	ks^J d
}t|jd D ]Z}t|jd D ]P}||jd  | }| }||  |7  < ||t|||jd dd }||  d| 8  < ||t|||jd dd }|| tj|| d|  dd	ksJ qpqgd S )Nr   rY   r   r   r   r         ?r   r   g-C6?gh㈵>)r   ro   rp   r   ravelrJ   sumr   rr   r}   r~   r   r   flatten)r    r   r   meanwtrF   r   r   r   expected_costr   jdiffdxnddmidxpscpluscminusr   r   r   check_kamada_kawai_costfn6  s*   
 (z$TestLayout.check_kamada_kawai_costfnc                 C   s   dt g dg dg dg }d}t ddgdd	gd
dgg}| |||d t g dg dg dg}| |||d d S )Nr   )r    @333333?)r   g?333333?)r   r   r   r   g?g	g@g333333ӿgffffff@g      @r   )g?g333333!@gffffff!)ir   gffffff)g333333"@g333333 g?r%   )rJ   rK   r   )r    r   r   r   r   r   r   test_kamada_kawai_costfnQ  s    z#TestLayout.test_kamada_kawai_costfnc                 C   s   | j }tttj|dd }tttj|dd }tjj|d d |dd   dd}tjj|d d |dd   dd}t	|t	|ksMJ tttj|dd }tjj|d d |dd   dd}tj
|dd  |d d	d
s}J d S )Ngffffff?)
resolutiong(\?r   r   Tequidistantg{Gz?atol)r   rJ   rK   rL   r   r?   r4   r}   r~   r   allclose)r    r!   pos_standardpos_tighter	distancesdistances_tighterpos_equidistantdistances_equidistantr   r   r   test_spiral_layout_  s   $$zTestLayout.test_spiral_layoutc                 C   sl   t d}t j|dd}tt| }tjj|dd  |d d  dd}tj	t
|ddd	s4J d S )
N
   Tr   r   r   r   r   gMbP?r   )r   r   r?   rJ   rK   rL   r4   r}   r~   r   r   )r    r!   r   pdistr   r   r   test_spiral_layout_equidistantv  s
   
$z)TestLayout.test_spiral_layout_equidistantc                 C   s&  t  }t j|dd}t |i ksJ t d}ddd}t |}tjdd t|  D d	k s6J t d
}dddd}t |}t	dt	dt	dd}|
 D ]\}}|| |k sgJ qYt j|dd}t	dt	dt	dd}|
 D ]\}}|| |k sJ qd S )Nr   rX   r   r&   )r   r   r#   c                 S   s   g | ]}t |qS r   )r   )r+   xr   r   r   
<listcomp>  s    z7TestLayout.test_rescale_layout_dict.<locals>.<listcomp>gư>r%   r   rh   rc   )r   r   )rP   )r   r   )r   r   r9   rescale_layout_dictrJ   r}   r~   zipr4   rK   itemsrO   )r    r!   rT   s_vposexpectationkvr   r   r   test_rescale_layout_dict  s0   


&

z#TestLayout.test_rescale_layout_dictN) __name__
__module____qualname__classmethodr   r"   r8   rC   rH   rI   rV   r`   rb   rg   ri   rj   rv   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   
   s<    

			r   c                  C   sp   t  } | jddd | jddd | jddd | jddd | g d t | }t|t| ks6J d	S )
zSee gh-5123.r   s0subsetr   r   s1r%   )r   )r   r%   r$   N)r   r   r]   r1   r@   r   )r!   r   r   r   r   4test_multipartite_layout_nonnumeric_partition_labels  s   
r   c                  C   s   t  } tddD ]\}}| j||d q	t j| dd}|d d |d d ks*J |d	 d |d d   k rJ|d
 d   k rJ|d d k sMJ  J d| jd d< t | }| | kscJ dS )zgReturn the layers in sorted order if the layers of the multipartite
    graph are sortable. See gh-5691)abr-   rw   e)r   r%   r   r   r   r   r   r   r   r   rw   r-   r   r   layer_0r   N)r   r   r   r]   r@   nodeskeys)r!   r   layerr   
pos_nosortr   r   r   $test_multipartite_layout_layer_order  s   F
r   )	__doc__r   networkxr   importorskiprJ   r   r   r   r   r   r   r   <module>   s    

   