B
    ²ô`†%  ã               @   sB   d dl Z d dlZd dlmZ d dlmZmZmZ G dd„ dƒZ	dS )é    N)Úassert_graphs_equal)Úbarbell_graphÚcycle_graphÚ
path_graphc               @   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-S ).ÚTestConvertNumpyc             C   s&   t  d¡at  d¡atjatjjad S )NÚnumpyZscipy)ÚpytestZimportorskipÚnpÚspÚsparseZtestingZassert_equalÚnp_assert_equal)Úcls© r   úP/home/dcms/DCMS/lib/python3.7/site-packages/networkx/tests/test_convert_scipy.pyÚsetup_class	   s    

zTestConvertNumpy.setup_classc             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)Úselfr   r   r   Úsetup_method   s    zTestConvertNumpy.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+TestConvertNumpy.test_exceptions.<locals>.GN)Ú__name__Ú
__module__Ú__qualname__Úformatr   r   r   r   ÚG   s   r"   )r   Úraisesr   ÚNetworkXErrorÚto_networkx_graph)r   r"   r   r   r   Útest_exceptions   s    z TestConvertNumpy.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    z4TestConvertNumpy.create_weighted.<locals>.<listcomp>c             S   s   g | ]\}}|‘qS r   r   )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 TestConvertNumpy.create_weightedc             C   s   t  ||¡st‚d S )N)r   Zis_isomorphicÚAssertionError)r   r   r   r   r   r   Úassert_isomorphic(   s    z"TestConvertNumpy.assert_isomorphicc             C   s  t j||d}|  ||¡ t j||d}|  ||¡ t  d|¡ |¡}|  ||¡ | ¡ }t  d|¡ |¡}|  ||¡ | ¡ }t  d|¡ |¡}|  ||¡ | ¡ }	t  d|¡ |	¡}|  ||¡ | 	¡ }
t  d|¡ |
¡}|  ||¡ | 
¡ }t  d|¡ |¡}|  ||¡ d S )N)r   r   )r   Úfrom_scipy_sparse_matrixr8   r%   Zempty_graphÚ	__class__ZtocsrZtocooZtocscÚtodenseZtoarray)r   r"   ÚAr   ZGGZGWZGIZACSRZACOOZACSCZADZAAr   r   r   Úidentity_conversion+   s*    z$TestConvertNumpy.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)r
   r   Z
lil_matrixr   r#   r   r$   r9   )r   r<   r   r   r   Ú
test_shapeI   s    zTestConvertNumpy.test_shapec             C   s$   t  | j¡}|  | j|t  ¡ ¡ dS )z0Conversion from graph to sparse matrix to graph.N)r   Úto_scipy_sparse_matrixr   r=   r   )r   r<   r   r   r   Útest_identity_graph_matrixN   s    z+TestConvertNumpy.test_identity_graph_matrixc             C   s$   t  | j¡}|  | j|t  ¡ ¡ dS )z4Conversion from digraph to sparse matrix to digraph.N)r   rC   r   r=   r   )r   r<   r   r   r   Útest_identity_digraph_matrixS   s    z-TestConvertNumpy.test_identity_digraph_matrixc             C   s$   t  | j¡}|  | j|t  ¡ ¡ dS )zBConversion from weighted graph to sparse matrix to weighted graph.N)r   rC   r   r=   r   )r   r<   r   r   r   Ú#test_identity_weighted_graph_matrixX   s    z4TestConvertNumpy.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   rC   r   r=   r   )r   r<   r   r   r   Ú%test_identity_weighted_digraph_matrix]   s    z6TestConvertNumpy.test_identity_weighted_digraph_matrixc             C   sh   t dƒ}t dƒ}t| ¡ ƒ}tj||d}t |¡}|  ||¡ ||d g7 }tjtj	tj
||d dS )z>Conversion from graph to sparse matrix to graph with nodelist.r'   r   )Únodelistr   N)r   r-   Znodesr   rC   r   r8   r   r#   r$   Zto_numpy_matrix)r   ÚP4ZP3rH   r<   ZGAr   r   r   Útest_nodelistb   s    
zTestConvertNumpy.test_nodelistc             C   sŽ   t  ¡ }| dd„ tdƒD ƒ¡ tdƒ}t  |¡}t| ¡ t j|d d ¡ ƒ td| ¡  t  |¡ ¡ ƒ td| ¡  t j|dd ¡ ƒ d S )	Nc             s   s$   | ]}||d  t dddfV  qdS )r>   g      à?g333333Ó?)r5   ÚotherN)Údict)r(   Únr   r   r   ú	<genexpr>q   s    z7TestConvertNumpy.test_weight_keyword.<locals>.<genexpr>r   r'   )r5   g      à?g333333Ó?rK   )r   r   Úadd_edges_fromÚranger   rC   r   r;   )r   ÚWP4rI   r<   r   r   r   Útest_weight_keywordo   s    
z$TestConvertNumpy.test_weight_keywordc             C   sR  t  ¡ }| dd„ tdƒD ƒ¡ tdƒ}t j|dd}t| ¡ t j|d d ¡ ƒ t j|dd}t| ¡ t j|d d ¡ ƒ t j|d	d}t| ¡ t j|d d ¡ ƒ t j|d
d}t| ¡ t j|d d ¡ ƒ t j|dd}t| ¡ t j|d d ¡ ƒ t j|dd}t| ¡ t j|d d ¡ ƒ t j|dd}t| ¡ t j|d d ¡ ƒ d S )Nc             s   s$   | ]}||d  t dddfV  qdS )r>   g      à?g333333Ó?)r5   rK   N)rL   )r(   rM   r   r   r   rN   ~   s    z7TestConvertNumpy.test_format_keyword.<locals>.<genexpr>r   r'   Zcsr)r!   )r5   ZcscZcooZbsrZlilZdiaZdok)r   r   rO   rP   r   rC   r   r;   )r   rQ   rI   r<   r   r   r   Útest_format_keyword|   s0    z$TestConvertNumpy.test_format_keywordc          	   C   sR   t  tj¡< t ¡ }| dd„ tdƒD ƒ¡ tdƒ}tj|dd W d Q R X d S )Nc             s   s$   | ]}||d  t dddfV  qdS )r>   g      à?g333333Ó?)r5   rK   N)rL   )r(   rM   r   r   r   rN   §   s    z=TestConvertNumpy.test_format_keyword_raise.<locals>.<genexpr>r   r'   Z	any_other)r!   )	r   r#   r   r$   r   rO   rP   r   rC   )r   rQ   rI   r   r   r   Útest_format_keyword_raise£   s    z*TestConvertNumpy.test_format_keyword_raisec          	   C   s*   t  tj¡ t t ¡ ¡ W d Q R X d S )N)r   r#   r   r$   rC   r   )r   r   r   r   Útest_null_raise¬   s    z TestConvertNumpy.test_null_raisec             C   s8   t  ¡ }| d¡ t  |¡}t| ¡ t dgg¡ƒ d S )Nr>   r   )r   r   Úadd_noderC   r   r;   r	   Úmatrix)r   r"   ÚMr   r   r   Ú
test_empty°   s    

zTestConvertNumpy.test_emptyc          	   C   sp   t  ¡ }| dd¡ | dd¡ | dd¡ t j|dddgd}t| ¡ t dddgdddgdddgg¡ƒ d S )Nr>   r?   r   )rH   r   )r   r   Úadd_edgerC   r   r;   r	   rW   )r   r"   rX   r   r   r   Útest_ordering¶   s    zTestConvertNumpy.test_orderingc          	   C   s€   t  dg¡}t  |¡}t| ¡ t dgg¡ƒ | ddg¡ t j|dddgd}t| ¡ 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'   )rH   r   )r   r   rC   r   r;   r	   rW   rO   )r   r"   rX   r   r   r   Útest_selfloop_graph¾   s    
z$TestConvertNumpy.test_selfloop_graphc          	   C   s€   t  dg¡}t  |¡}t| ¡ t dgg¡ƒ | ddg¡ t j|dddgd}t| ¡ 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'   )rH   r   )r   r   rC   r   r;   r	   rW   rO   )r   r"   rX   r   r   r   Útest_selfloop_digraphÇ   s    
z&TestConvertNumpy.test_selfloop_digraphc             C   s  t  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||ƒ tj|dtjd
}t||ƒ dddddg}t ¡ }| dd„ |D ƒ¡ tj|d	tjd
}t||ƒ t ¡ }|j	t
|ƒdd d|d d d d< tj|dtjd
}t||ƒ dS )z²Tests that the :func:`networkx.from_scipy_sparse_matrix` 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   r   r+   Û   s    zQTestConvertNumpy.test_from_scipy_sparse_matrix_parallel_edges.<locals>.<listcomp>)r5   T)Zparallel_edgesr   F)r>   r>   c             S   s   g | ]\}}||d f‘qS )r>   r   )r(   r)   r*   r   r   r   r+   ê   s    r   r5   N)r   Ú
csr_matrixr   r   r0   rZ   r9   r   ZMultiDiGraphrO   Úset)r   r<   Úexpectedr.   Úactualr   r   r   Ú,test_from_scipy_sparse_matrix_parallel_edgesÐ   s.    



z=TestConvertNumpy.test_from_scipy_sparse_matrix_parallel_edgesc             C   sL   t  ddgddgg¡}tj|tjd}t ¡ }|jdddd t||ƒ dS )z£Tests that a symmetric matrix has edges added only once to an
        undirected multigraph when using
        :func:`networkx.from_scipy_sparse_matrix`.

        r   r>   )r   )r5   N)r   r^   r   r9   Z
MultiGraphrZ   r   )r   r<   r"   r`   r   r   r   Útest_symmetricø   s
    zTestConvertNumpy.test_symmetricN)r   r   r    Úclassmethodr   r   r&   r   r8   r=   rB   rD   rE   rF   rG   rJ   rR   rS   rT   rU   rY   r[   r\   r]   rb   rc   r   r   r   r   r      s,   
'			(r   )
r   Znetworkxr   Znetworkx.testingr   Znetworkx.generators.classicr   r   r   r   r   r   r   r   Ú<module>   s   