U
    :vfí(  ã                   @   st   d dl Z e  d¡Ze  d¡Zd dlZd dlZd dlm	Z	m
Z
mZ d dlmZ G dd„ dƒZe j dd	¡d
d„ ƒZdS )é    NZnumpyÚscipy)Úbarbell_graphÚcycle_graphÚ
path_graph)Úgraphs_equalc                   @   s¬   e Z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)S )*ÚTestConvertScipyc                 C   s@   t ddƒ| _tdtjd| _|  t ¡ ¡| _|  t ¡ ¡| _	d S )Né
   é   ©Úcreate_using)
r   ÚG1r   ÚnxÚDiGraphÚG2Úcreate_weightedÚGraphÚG3ÚG4©Úself© r   úE/tmp/pip-unpacked-wheel-_lngutwb/networkx/tests/test_convert_scipy.pyÚsetup_method   s    zTestConvertScipy.setup_methodc                 C   s$   G dd„ dƒ}t  tjtj|¡ d S )Nc                   @   s   e Zd ZdZdS )z+TestConvertScipy.test_exceptions.<locals>.GN)Ú__name__Ú
__module__Ú__qualname__Úformatr   r   r   r   ÚG   s   r   )ÚpytestÚraisesr   ÚNetworkXErrorÚto_networkx_graph)r   r   r   r   r   Útest_exceptions   s    z TestConvertScipy.test_exceptionsc                 C   sX   t dƒ}t| ¡ ƒ}dd„ |D ƒ}dd„ |D ƒ}dd„ |D ƒ}t|||ƒ}| |¡ |S )Né   c                 S   s   g | ]\}}|‘qS r   r   ©Ú.0ÚuÚvr   r   r   Ú
<listcomp>   s     z4TestConvertScipy.create_weighted.<locals>.<listcomp>c                 S   s   g | ]\}}|‘qS r   r   r$   r   r   r   r(      s     c                 S   s   g | ]}|d  ‘qS )r   r   )r%   Úsr   r   r   r(      s     )r   ÚlistÚedgesÚzipÚadd_weighted_edges_from)r   r   ÚgÚeÚsourceÚdestÚweightÚexr   r   r   r      s    
z TestConvertScipy.create_weightedc                 C   s8  t j||d}t  ||¡st‚t j||d}t  ||¡s<t‚t  d|¡ |¡}t  ||¡s^t‚| ¡ }t  d|¡ |¡}t  ||¡sˆt‚| ¡ }t  d|¡ |¡}t  ||¡s²t‚| 	¡ }	t  d|¡ |	¡}t  ||¡sÜt‚| 
¡ }
t  d|¡ |
¡}t  ||¡st‚| ¡ }t  d|¡ |¡}t  ||¡s4t‚d S )Nr
   r   )r   Úfrom_scipy_sparse_arrayÚis_isomorphicÚAssertionErrorr!   Zempty_graphÚ	__class__ZtocsrZtocooZtocscÚtodenseÚtoarray)r   r   ÚAr   ZGGZGWZGIZACSRZACOOZACSCZADZAAr   r   r   Úidentity_conversion$   s*    z$TestConvertScipy.identity_conversionc                 C   s2   t j dddgdddgg¡}t tjtj|¡ dS )z(Conversion from non-square sparse array.é   é   r	   r#   é   é   N)ÚspÚsparseZ	lil_arrayr   r   r   r    r4   ©r   r:   r   r   r   Ú
test_shapeB   s    zTestConvertScipy.test_shapec                 C   s$   t  | j¡}|  | j|t  ¡ ¡ dS )z0Conversion from graph to sparse matrix to graph.N)r   Úto_scipy_sparse_arrayr   r;   r   rB   r   r   r   Útest_identity_graph_matrixG   s    z+TestConvertScipy.test_identity_graph_matrixc                 C   s$   t  | j¡}|  | j|t  ¡ ¡ dS )z4Conversion from digraph to sparse matrix to digraph.N)r   rD   r   r;   r   rB   r   r   r   Útest_identity_digraph_matrixL   s    z-TestConvertScipy.test_identity_digraph_matrixc                 C   s$   t  | j¡}|  | j|t  ¡ ¡ dS )zBConversion from weighted graph to sparse matrix to weighted graph.N)r   rD   r   r;   r   rB   r   r   r   Ú#test_identity_weighted_graph_matrixQ   s    z4TestConvertScipy.test_identity_weighted_graph_matrixc                 C   s$   t  | j¡}|  | j|t  ¡ ¡ dS )zFConversion from weighted digraph to sparse matrix to weighted digraph.N)r   rD   r   r;   r   rB   r   r   r   Ú%test_identity_weighted_digraph_matrixV   s    z6TestConvertScipy.test_identity_weighted_digraph_matrixc                 C   s    t dƒ}t dƒ}t| ¡ ƒ}tj||d}t |¡}t ||¡sDt‚tj	tj
tj|g d |dg }tj	tj
tj||d ddddg}tj	tj
tj||d dS )	z>Conversion from graph to sparse matrix to graph with nodelist.r#   r	   ©Únodelistr   éÿÿÿÿr<   r=   N)r   r*   Znodesr   rD   r   r5   r6   r   r   r    )r   ÚP4ZP3rJ   r:   ZGAZlong_nlZnon_nlr   r   r   Útest_nodelist[   s    

zTestConvertScipy.test_nodelistc                 C   sš   t  ¡ }| dd„ tdƒD ƒ¡ tdƒ}t  |¡}tj | 	¡ t j|d d 	¡ ¡ tj d| 	¡  t  |¡ 	¡ ¡ tj d| 	¡  t j|dd 	¡ ¡ d S )	Nc                 s   s"   | ]}||d  dddœfV  qdS ©r<   ç      à?ç333333Ó?)r2   ÚotherNr   ©r%   Únr   r   r   Ú	<genexpr>o   s     z7TestConvertScipy.test_weight_keyword.<locals>.<genexpr>r	   r#   ©r2   rO   rP   rQ   ©
r   r   Úadd_edges_fromÚranger   rD   ÚnpÚtestingÚassert_equalr8   ©r   ÚWP4rL   r:   r   r   r   Útest_weight_keywordm   s     
 ÿ
 ÿ
 ÿz$TestConvertScipy.test_weight_keywordc                 C   sn  t  ¡ }| dd„ tdƒD ƒ¡ tdƒ}t j|dd}tj | 	¡ t j|d d 	¡ ¡ t j|dd}tj | 	¡ t j|d d 	¡ ¡ t j|d	d}tj | 	¡ t j|d d 	¡ ¡ t j|d
d}tj | 	¡ t j|d d 	¡ ¡ t j|dd}tj | 	¡ t j|d d 	¡ ¡ t j|dd}tj | 	¡ t j|d d 	¡ ¡ t j|dd}tj | 	¡ t j|d d 	¡ ¡ d S )Nc                 s   s"   | ]}||d  dddœfV  qdS rN   r   rR   r   r   r   rT   ~   s     z7TestConvertScipy.test_format_keyword.<locals>.<genexpr>r	   r#   Úcsr©r   rU   ÚcscZcooZbsrZlilZdiaÚdokrV   r\   r   r   r   Útest_format_keyword|   sL     ÿ ÿ ÿ ÿ ÿ ÿ ÿz$TestConvertScipy.test_format_keywordc              	   C   sR   t  tj¡< t ¡ }| dd„ tdƒD ƒ¡ tdƒ}tj|dd W 5 Q R X d S )Nc                 s   s"   | ]}||d  dddœfV  qdS rN   r   rR   r   r   r   rT   ¦   s    z=TestConvertScipy.test_format_keyword_raise.<locals>.<genexpr>r	   r#   Z	any_otherr`   )	r   r   r   r    r   rW   rX   r   rD   )r   r]   rL   r   r   r   Útest_format_keyword_raise£   s    
ÿz*TestConvertScipy.test_format_keyword_raisec              	   C   s*   t  tj¡ t t ¡ ¡ W 5 Q R X d S )N)r   r   r   r    rD   r   r   r   r   r   Útest_null_raise¬   s    z TestConvertScipy.test_null_raisec                 C   s<   t  ¡ }| d¡ t  |¡}tj | ¡ t dgg¡¡ d S )Nr<   r   )	r   r   Úadd_noderD   rY   rZ   r[   r9   Úarray©r   r   ÚMr   r   r   Ú
test_empty°   s    

zTestConvertScipy.test_emptyc              
   C   st   t  ¡ }| dd¡ | dd¡ | dd¡ t j|dddgd}tj | ¡ t dddgdddgdddgg¡¡ d S )Nr<   r=   r	   rI   r   )	r   r   Úadd_edgerD   rY   rZ   r[   r9   rg   rh   r   r   r   Útest_ordering¶   s      ÿzTestConvertScipy.test_orderingc              
   C   sˆ   t  dg¡}t  |¡}tj | ¡ t dgg¡¡ | ddg¡ t j|dddgd}tj | ¡ t d	dd	gdd	dgd	dd	gg¡¡ d S ©
N©r<   r<   r<   )r=   r	   )r	   r#   r=   r	   r#   rI   r   )	r   r   rD   rY   rZ   r[   r9   rg   rW   rh   r   r   r   Útest_selfloop_graphÀ   s    
  ÿz$TestConvertScipy.test_selfloop_graphc              
   C   sˆ   t  dg¡}t  |¡}tj | ¡ t dgg¡¡ | ddg¡ t j|dddgd}tj | ¡ t d	dd	gd	d	dgd	d	d	gg¡¡ d S rm   )	r   r   rD   rY   rZ   r[   r9   rg   rW   rh   r   r   r   Útest_selfloop_digraphË   s    
  ÿz&TestConvertScipy.test_selfloop_digraphc                 C   s,  t j ddgddgg¡}t ¡ }dddg}| dd„ |D ƒ¡ |jdddd tj|d	tjd
}t||ƒsnt	‚tj|dtjd
}t||ƒsŽt	‚dddddg}t 
¡ }| dd„ |D ƒ¡ tj|d	tj
d
}t||ƒsØt	‚t 
¡ }|jt|ƒdd d|d d d d< tj|dtj
d
}t||ƒs(t	‚dS )z±Tests that the :func:`networkx.from_scipy_sparse_array` function
        interprets integer weights as the number of parallel edges when
        creating a multigraph.

        r<   r=   )r   r   )r   r<   )r<   r   c                 S   s   g | ]\}}||d f‘qS ©r<   r   r$   r   r   r   r(   á   s     zPTestConvertScipy.test_from_scipy_sparse_array_parallel_edges.<locals>.<listcomp>rU   T)Zparallel_edgesr   Frn   c                 S   s   g | ]\}}||d f‘qS rq   r   r$   r   r   r   r(   ð   s     r   r2   N)r@   rA   Ú	csr_arrayr   r   r-   rk   r4   r   r6   ZMultiDiGraphrW   Úset)r   r:   Úexpectedr+   Úactualr   r   r   Ú+test_from_scipy_sparse_array_parallel_edgesÖ   sF    
  ÿ  ÿ  ÿ  ÿz<TestConvertScipy.test_from_scipy_sparse_array_parallel_edgesc                 C   sR   t j ddgddgg¡}tj|tjd}t ¡ }|jdddd t||ƒsNt‚dS )z¢Tests that a symmetric matrix has edges added only once to an
        undirected multigraph when using
        :func:`networkx.from_scipy_sparse_array`.

        r   r<   r
   rU   N)	r@   rA   rr   r   r4   Z
MultiGraphrk   r   r6   )r   r:   r   rt   r   r   r   Útest_symmetricþ   s
    zTestConvertScipy.test_symmetricN)r   r   r   r   r"   r   r;   rC   rE   rF   rG   rH   rM   r^   rc   rd   re   rj   rl   ro   rp   rv   rw   r   r   r   r   r      s(   
'	
(r   Úsparse_format)r_   ra   rb   c                 C   sœ   t  ¡ }| ddddifddddifddddifddddifddddifddddifg¡ tj dddgdddgdddgg¡ | ¡}t|t  |¡ƒs˜t	‚dS )z7Test all formats supported by _generate_weighted_edges.r   r<   r2   r	   r=   N)
r   r   rW   r@   rA   Z	coo_arrayZasformatr   r4   r6   )rx   rt   r:   r   r   r   Ú$test_from_scipy_sparse_array_formats  s    úÿ
*ry   )r   ZimportorskiprY   r@   Zscipy.sparser   Znetworkxr   Znetworkx.generators.classicr   r   r   Znetworkx.utilsr   r   ÚmarkZparametrizery   r   r   r   r   Ú<module>   s   

  