U
    9vfE                     @   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.    NZnumpyZ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d9d: Z d;d< Z!d=d> Z"d?S )@
TestLayoutc                 C   s8   t dd| _t  | _t | jd t dd| _d S )N   Zabcdef   )nxZgrid_2d_graphGiGraphGsZadd_pathbigG)cls r   F/tmp/pip-unpacked-wheel-_lngutwb/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                    sj   dd l  t }|ddddg ddi}dg}tj|||d}t fdd	| D }|rft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).0Z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AssertionError)r   r   Zinit_posZ	fixed_posr   Z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   r,   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 t| d S Nr   dimr    )r   r   r1   r2   r3   r   r,   r	   r4   Zto_directedr5   r7   r9   
arf_layoutr:   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 t| d S r<   )r   r   r1   r2   r3   r   r,   r4   r5   r7   r9   r?   r:   r   r   r   test_smoke_stringG   s    








zTestLayout.test_smoke_stringc                 C   sx   t |}|| }|| }t t| }|d|d }|d| k sTt||k sdt||k sttd S )Nr   r   )nparraylistr.   maxminallr/   )r   r   scalecenterlowhivposlengthr   r   r   check_scale_and_centerV   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   	   rI         ?)g      @g      @rH   rI   r   )r   r    r   r    )r>   rH   rI   rN   r   complete_graphadd_noder1   r   r4   r2   r5   r7   r9   r   Z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 )NrO   )r   rT   r   r   ZNetworkXExceptionr3   r:   r   r   r   #test_planar_layout_non_planar_inputq   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   ZPlanarEmbeddingset_datar3   )r   Z	embeddingr   r   r   (test_smoke_planar_layout_embedding_inputu   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   rO   rQ   rQ   rQ   rR   r   )r   r   r   r    r=   rS   rV   r   r   r   test_default_scale_and_centerz   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   r=   r    )r   r   r   r   r   r2   r5   r3   r:   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(tt jjj|dd}|jdksHtt jj|}|jdksdtd S )N   r   r    r=   r`   r    )	r   Zto_numpy_arrayr   drawinglayoutZ_fruchterman_reingoldshaper/   _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,tt jj|}|jdksHtt jjj|dd}|jdkshtd S )Nd)Zdtyper_   r    r=   ra   )	r   Zto_scipy_sparse_arrayr   rb   rc   re   rd   r/   Z_sparse_spectralrf   r   r   r   test_adjacency_interface_scipy   s    z)TestLayout.test_adjacency_interface_scipyc                 C   s   t d}t |}|d  r$tt d}t |dgddgdgg}|d  rXt|d  shttj|d dkstt j|dgddgdggdd}tj|d dkstd S )Nr   r   r   r   r    )rotate)r   r   r5   r-   r/   rB   linalgnormr   r   rL   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   r2   r   r,   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}d S rp   )r   r2   r   r?   rr   r   r   r   test_smoke_initial_pos_arf   s    z%TestLayout.test_smoke_initial_pos_arfc                 C   s   t | j}t j| j|dgd}t|d t|d ks<tt | j}t j| j|dgd}tdD ]*}|d | tj	|d | ddksdtqdd S )Nr   r#   r   Hz>abs)
r   r2   r   r   tupler/   r	   ranger   approx)r   r   rs   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:tt j|dd}t|d dks\tt j|dd}t|d dks~tt j|dd}t|d dkstt j	|dd}t|d dkstt j
|dd}t|d dkstt j|dd}t|d dkstd S )Nr   r   rP   r   )r   r   r1   r2   ry   r/   r3   r   r,   r4   r5   r7   rn   r   r   r   test_center_parameter   s     
z TestLayout.test_center_parameterc                 C   s   t d}tt jtt jks"t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   rP   r    r   )r>   rI   )r   r   idr   r,   r/   r   r   r   r1   r2   r3   r4   r5   r7   r9   r:   r   r   r   test_center_wrong_dimensions   s    
z'TestLayout.test_center_wrong_dimensionsc                 C   sD  t  }t j|dd}|i ks"tt j|dd}|i ks<tt j|dd}|i ksVtt ||}|i ksntt j|dd}|i kstt j|dd}|i kstt j	|dd}|i kstt j
|dd}|i kstt j|dd}|i kstt j|dd}|i kstt j|dd}|i ks(tt |}|i ks@td S )Nr   rP   )r   empty_graphr1   r/   r2   r3   r6   r   r,   r4   r5   r7   r8   r9   r?   rn   r   r   r   test_empty_graph   s2    
zTestLayout.test_empty_graphc           
      C   sL  t dd}t j|\}}t ||}t|t|ks<t|t|d  d }|t|d  d }|D ]}|| d |kshtqh|D ]}|| d |kstqt j||ddddd}t|t|kst|t|d  d }|t|d  d }	|D ]}|| d |kstq|D ]}|| d |	kstqtj	t
t j||d	d
 d S )Nr    r   r   
horizontalr   r   r   r   )alignrI   rH   Zaspect_ratiofoor   )r   Zcomplete_bipartite_graphZ	bipartiteZsetsr6   lenr/   rD   r   r   r   )
r   r   topZbottomrL   Ztop_xZbottom_xnodeZtop_yZbottom_yr   r   r   test_bipartite_layout   s4         z TestLayout.test_bipartite_layoutc                    s   d}t j| }t |tt|ks,td |D ]:} | }t fddt d |D sft |7  q4t j|dddd	tt|kstd |D ]:} | }t fd
dt d |D st |7  qtjt	t j|dd d S )N)r   r      r      r   c                 3   s&   | ]}  d  | d  kV  qdS )r   Nr   r&   istartrL   r   r   r*   $  s     z6TestLayout.test_multipartite_layout.<locals>.<genexpr>r   r   r   r   )r   rH   rI   c                 3   s&   | ]}  d  | d  kV  qdS )r   Nr   r   r   r   r   r*   -  s     r   r   )
r   Zcomplete_multipartite_graphr8   r   r/   rG   rz   r   r   r   )r   Z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^t|d tjdd
dksxt|d tjdd
dkstd S )Ng      @g      @r   皙?g       @333333?r   Z
meanweightr>   g      ?rv   rw         rQ   )	r   rb   rc   _kamada_kawai_costfnrB   rC   r   r{   r/   )r   costfnr   invdistcostgradr   r   r   test_kamada_kawai_costfn_1d2  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 ]R}	t|	d |jd D ]8}
tj	
||	 ||
  }||||	 |
  d d 7 }qjqR|tj|dd	kstd
}t|jd D ]}t|jd D ]}||jd  | }| }||  |7  < ||t|||jd dd }||  d| 8  < ||t|||jd dd }|| tj|| d|  dd	kstqqd S )Nr   rQ   r   r|   r   r         ?rv   rw   g-C6?gh㈵>)r   rb   rc   r   ZravelrB   sumrz   rd   rl   rm   r   r{   r/   flatten)r   r   r   meanwtr>   r   r   r   Zexpected_costr   jdiffZdxZnddmidxZpsZcplusZcminusr   r   r   check_kamada_kawai_costfn>  s$    
  z$TestLayout.check_kamada_kawai_costfnc                 C   s   dt dddgdddgdddgg }d}t dd	gd
dgddgg}| |||d t dddgdddgdddgg}| |||d d S )Nr   r   g @g333333?g?g333333?r   g?g	g@g333333ӿgffffff@g      @r   g?g333333!@gffffff!ir   gffffffg333333"@g333333 g?r    )rB   rC   r   )r   r   r   r   r   r   r   test_kamada_kawai_costfnY  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st
tttj|dd }tjj|d d |dd   dd}tj|dd  |d d	d
st
d S )Ngffffff?)
resolutiong(\?r   r   TZequidistantg{Gz?Zatol)r   rB   rC   rD   r   r7   r.   rl   rm   r   r/   allclose)r   r   Zpos_standardZpos_tighterZ	distancesZdistances_tighterZpos_equidistantZdistances_equidistantr   r   r   test_spiral_layoutg  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	shtd S )
N
   Tr   r   r   r   r   gMbP?r   )r   r   r7   rB   rC   rD   r.   rl   rm   r   r   r/   )r   r   r   pdistr   r   r   test_spiral_layout_equidistant}  s
    
$z)TestLayout.test_spiral_layout_equidistantc                 C   s*  t  }t j|dd}t |i ks(tt d}ddd}t |}tjdd t|	  D d	k sltt d
}dddd}t |}t
dt
dt
dd}| D ]\}}|| |k stqt j|dd}t
dt
dt
dd}| D ] \}}|| |k stqd S )Nr   rP   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   r\   rY   )r   r   )rH   )r   r   )r   r   r1   Zrescale_layout_dictr/   rB   rl   rm   zipr.   rC   itemsrG   )r   r   rL   Zs_vposZexpectationkvr   r   r   test_rescale_layout_dict  s.    


&

z#TestLayout.test_rescale_layout_dictc                 C   sH   | j }tj|}t|}||= tj||d}t|t|ksDtdS )zS
        Checks whether partial pos input still returns a proper position.
        rq   N)r   r   utilsZarbitrary_elementr2   r?   r   r/   )r   r   r   r   r   r   r   "test_arf_layout_partial_input_test  s    
z-TestLayout.test_arf_layout_partial_input_testc                 C   s   | j }tjttj|dd dS )zl
        Checks input parameters correctly raises errors. For example,  `a` should be larger than 1
        r   )r   aN)r   r   r   r   r   r?   r:   r   r   r    test_arf_layout_negative_a_check  s    z+TestLayout.test_arf_layout_negative_a_checkN)#__name__
__module____qualname__classmethodr   r   r0   r;   r@   rA   rN   rW   rX   r[   r]   r^   rh   rj   ro   rt   ru   r}   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   
   s@   

			r   c                  C   sr   t  } | jddd | jddd | jddd | jddd | dd	d
g t | }t|t| ksntdS )zSee gh-5123.r   s0subsetr   r   s1r    r   )r   r    r   N)r   r   rU   r+   r8   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sTt|d	 d |d d   k r|d
 d   k r|d d k sn td| jd d< t | }| | kstdS )zgReturn the layers in sorted order if the layers of the multipartite
    graph are sortable. See gh-5691)r   br'   ri   e)r   r    r   r   r   r   r   r   r   r   ri   r'   r   r   Zlayer_0r   N)r   r   r   rU   r8   r/   Znodeskeys)r   r   Zlayerr   Z
pos_nosortr   r   r   $test_multipartite_layout_layer_order  s    D
r   )	__doc__r   Znetworkxr   ZimportorskiprB   r   r   r   r   r   r   r   <module>   s   

   0