U
    ?hAS                     @   s   d Z ddlZddlZG dd dZG dd dZejdejej	ej
ejgdd	 ZG d
d dZG dd deZG dd deZG dd deZG dd deZG dd deZdS )z8
Unit tests for dedensification and graph summarization
    Nc                   @   sZ   e Zd Zdd Zdd Zdd Zeddd	Zd
d Zdd Z	dd Z
dd Zdd ZdS )TestDirectedDedensificationc                 C   sX   ddddddgfdddd	gfddgfg}t  }|D ]\}}|D ]}||| q@q4|S )
N1BC2ABC3AB64r   5ZABr   nxDiGraphadd_edgeselfZoriginal_matrixgraphsourcetargetstarget r   ^/var/www/html/venv/lib/python3.8/site-packages/networkx/algorithms/tests/test_summarization.pybuild_original_graph
   s    	z0TestDirectedDedensification.build_original_graphc                 C   sf   dddgfddddgfddgfd	dd
gfddgfdg}t  }|D ]\}}|D ]}||| qNqB|S )Nr   r   r   r	   r
   r   r   r   r   r   )r   r   r   r   Zcompressed_matrixcompressed_graphr   r   r   r   r   r   build_compressed_graph   s    
z2TestDirectedDedensification.build_compressed_graphc                 C   s,   t  }t j|dd\}}|t ks(tdS )zT
        Verify that an empty directed graph results in no compressor nodes
           	thresholdN)r   r   	dedensifysetAssertionError)r   Gr   c_nodesr   r   r   
test_empty+   s    z&TestDirectedDedensification.test_emptyTc           	      C   s   |r|   } |D ]v}tt| |}t| |}|D ]}| || q6|| }|D ]&}| || |D ]}| || qhqT| | q| S )a  
        Reconstructs the original graph from a dedensified, directed graph

        Parameters
        ----------
        G: dedensified graph
           A networkx graph
        compressor_nodes: iterable
           Iterable of compressor nodes in the dedensified graph
        inplace: bool, optional (default: False)
           Indicates if densification should be done inplace

        Returns
        -------
        G: graph
           A densified networkx graph
        )copyr%   r   all_neighborsZ	neighborsZremove_edger   remove_node)	r'   Zcompressor_nodesr*   Zcompressor_noder+   Zout_neighborsZout_neighborZin_neighborsZin_neighborr   r   r   densify3   s    z#TestDirectedDedensification.densifyc                 C   s
   d| _ d S )N)r   r(   r   r   r   r   setup_methodU   s    z(TestDirectedDedensification.setup_methodc                 C   s   |   }|  }tj|dd\}}| D ]H\}}dt|}dt|}|||}	|||}
|	|
ks*tq*t	|t	| j
kstdS )zy
        Verifies that dedensify produced the correct edges to/from compressor
        nodes in a directed graph
        r!   r"    N)r   r    r   r$   edgesjoinsortedhas_edger&   lenr(   )r   r'   compressed_Gr   r(   sto_so_tZcompressed_graph_existsZverified_compressed_existsr   r   r   test_dedensify_edgesX   s    z0TestDirectedDedensification.test_dedensify_edgesc                 C   s^   |   }t| }tj|dd\}}t| }||ks>t|  }|t| ksZtdS )zu
        Verifies that dedensify produced the correct number of comrpessor nodes
        in a directed graph
        r!   r"   N)r   r6   r2   r   r$   r&   r    )r   r'   original_edge_countc_Gr(   compressed_edge_countr7   r   r   r   test_dedensify_edge_counth   s    z5TestDirectedDedensification.test_dedensify_edge_countc                 C   sT   |   }| j|| jdd}|  }| D ]$\}}||||||ks*tq*dS )zq
        Verifies that densification produces the correct edges from the
        original directed graph
        T)r*   N)r    r-   r(   r   r2   r5   r&   )r   r7   original_graphr'   r8   r9   r   r   r   test_densify_edgesu   s
    z.TestDirectedDedensification.test_densify_edgesc                 C   sZ   |   }t| }| || j}t| }||ks:t|  }|t| ksVtdS )zy
        Verifies that densification produces the correct number of edges in the
        original directed graph
        N)r    r6   r2   r-   r(   r&   r   )r   r7   r?   rA   r=   r'   r   r   r   test_densify_edge_count   s    z3TestDirectedDedensification.test_densify_edge_countN)T)__name__
__module____qualname__r   r    r)   staticmethodr-   r0   r<   r@   rB   rC   r   r   r   r   r   	   s   !r   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )TestUnDirectedDedensificationc                 C   sX   ddddddgfdddd	gfddgfg}t  }|D ]\}}|D ]}||| q@q4|S )
zC
        Builds graph shown in the original research paper
        )r   ZCBr   r	   r
   r   r   r   r   r   r   Graphr   r   r   r   r   r      s    	z2TestUnDirectedDedensification.build_original_graphc                 C   s,   t  }t j|dd\}}|t ks(tdS )zV
        Verify that an empty undirected graph results in no compressor nodes
        r!   r"   N)r   rJ   r$   r%   r&   )r   r'   r7   r(   r   r   r   r)      s    z(TestUnDirectedDedensification.test_emptyc                 C   s
   d| _ d S )N)6ABr   r.   r/   r   r   r   r0      s    z*TestUnDirectedDedensification.setup_methodc              
   C   s   dddgfddgfddgfddgfd	dgfd
ddgfdddgfdddgfddgfg	}t  }|D ]\}}|D ]}||| qlq`|S )Nr   r   Cr   r   r	   rK   r   r   r   r
   rI   r   r   r   r   r       s    



z4TestUnDirectedDedensification.build_compressed_graphc                 C   s   |   }tj|dd\}}|  }| D ]H\}}dt|}dt|}|||}	|||}
|	|
ks*tq*t	|t	| j
kstdS )z
        Verifies that dedensify produced correct compressor nodes and the
        correct edges to/from the compressor nodes in an undirected graph
        r!   r"   r1   N)r   r   r$   r    r2   r3   r4   r5   r&   r6   r(   )r   r'   r>   r(   Zv_compressed_Gr8   r9   r:   r;   Zhas_compressed_edgeZverified_has_compressed_edger   r   r   r<      s    z2TestUnDirectedDedensification.test_dedensify_edgesc                 C   sd   |   }tj|ddd\}}t| }t| }||ks@t|  }t| }||ks`tdS )zm
        Verifies that dedensify produced the correct number of edges in an
        undirected graph
        r!   T)r#   r*   N)r   r   r$   r6   r2   r&   r    )r   r'   r>   r(   r?   Zverified_original_edge_countZverified_compressed_GZverified_compressed_edge_countr   r   r   r@      s    z7TestUnDirectedDedensification.test_dedensify_edge_countN)	rD   rE   rF   r   r)   r0   r    r<   r@   r   r   r   r   rH      s   rH   
graph_typec                 C   s(   |  }t j|dd}t ||s$td S )Ncolor)node_attributes)r   snap_aggregationis_isomorphicr&   )rM   r'   summary_graphr   r   r   test_summarization_empty   s    rT   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )AbstractSNAPrN   c                 C   s   d S Nr   r/   r   r   r   r      s    z!AbstractSNAP.build_original_graphc                 C   s   d S rV   r   r/   r   r   r   build_summary_graph   s    z AbstractSNAP.build_summary_graphc                 C   sB   |   }|  }d}t|| j|}| |}t||s>td S Ntyper   rW   r   rQ   rP   deterministic_labelsrR   r&   r   rA   rS   relationship_attributesZgenerated_summary_graphZrelabeled_summary_graphr   r   r   test_summary_graph   s      
zAbstractSNAP.test_summary_graphc                    sX   t  j}t| fddd}|  i }t|D ]\}}d| }|||< q2t |S )Nc                    s   t  j|  d d S )Ngroupr   )r4   nodes)nr'   r   r   <lambda>       z3AbstractSNAP.deterministic_labels.<locals>.<lambda>)keyzSupernode-%s)listra   r4   sort	enumerater   Zrelabel_nodes)r   r'   Znode_labelsZlabel_mappingindexnodelabelr   rc   r   r\      s    

z!AbstractSNAP.deterministic_labelsN)rD   rE   rF   rP   r   rW   r_   r\   r   r   r   r   rU      s
   rU   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	TestSNAPNoEdgeTypesr   c                 C   s@   |   }|  }d}t|| j}| |}t||s<td S rX   r[   r]   r   r   r   r_     s     
z&TestSNAPNoEdgeTypes.test_summary_graphc                 C   s   ddiddiddiddiddiddiddiddiddiddiddiddid}dddd	d
dddddddg}t  }|D ]}|| }|j|f| qv|D ]\}}||| q|S )NrO   RedBlueYellowr
   r   rL   DEFr'   HIJKL)r
   r   )r
   rL   )r
   rs   )r
   rv   )r   rr   )r   rw   )r   rt   )rL   r'   )rr   ru   )rv   rw   )rw   rx   )rv   ry   r   rJ   add_noder   )r   ra   r2   r'   rk   
attributesr   r   r   r   r   r     sB    z(TestSNAPNoEdgeTypes.build_original_graphc           	      C   s   ddiddiddiddiddiddid}dddd	d
ddg}t  }|D ]}|| }|j|f| qH|D ]\}}||| qhddhddhddhddhddhddhd}t ||d |S )NrO   rn   ro   rp   Supernode-0Supernode-1Supernode-2Supernode-3Supernode-4Supernode-5)r~   r~   )r~   r   )r~   r   )r~   r   )r   r   )r   r   )r   r   r
   r   rL   rr   rs   rt   r'   ru   rv   rw   rx   ry   r`   r   rJ   r{   r   set_node_attributes)	r   ra   r2   r'   rk   r|   r   r   
supernodesr   r   r   rW   =  s<    		z'TestSNAPNoEdgeTypes.build_summary_graphN)rD   rE   rF   r^   r_   r   rW   r   r   r   r   rm     s   'rm   c                   @   s   e Zd Zdd Zdd ZdS )TestSNAPUndirectedc           	      C   s   ddiddiddiddiddiddiddiddiddiddiddiddid}dddd	d
dddddddg}t  }|D ]}|| }|j|f| qv|D ]\}}}|j|||d q|S )NrO   rn   ro   rp   rq   )r
   r   Strong)r
   rL   Weakr
   rs   r   )r
   rv   r   )r   rr   r   )r   rw   r   r   rt   r   )rL   r'   r   )rr   ru   r   )rv   rw   r   )rw   rx   r   )rv   ry   r   rY   rz   	r   ra   r2   r'   rk   r|   r   r   rZ   r   r   r   r   d  sB    z'TestSNAPUndirected.build_original_graphc           
      C   s   ddiddiddiddiddiddid}dddd	d
ddg}t  }|D ]}|| }|j|f| qH|D ] \}}}|j||d|igd qhddhddhddhddhddhddhd}	t ||	d |S )NrO   rn   ro   rp   r}   )r~   r~   r   )r~   r   r   )r~   r   r   )r~   r   r   )r   r   r   )r   r   r   )r   r   r   rZ   typesr
   r   rL   rr   rs   rt   r'   ru   rv   rw   rx   ry   r`   r   )
r   ra   r2   r'   rk   r|   r   r   rZ   r   r   r   r   rW     s<    		z&TestSNAPUndirected.build_summary_graphNrD   rE   rF   r   rW   r   r   r   r   r   c  s   'r   c                   @   s   e Zd Zdd Zdd ZdS )TestSNAPDirectedc           	      C   s   ddiddiddiddiddiddiddiddid}ddd	d
ddddddddg}t  }|D ]}|| }|j|f| q^|D ]\}}}|j|||d q~|S )NrO   rn   Greenro   rp   r
   r   rL   rr   rs   rt   r'   ru   )r
   rL   r   r   )r
   rt   r   )r   rr   r   )r   rs   r   r   )rL   r'   r   )rL   rt   r   )rr   rs   r   )rr   ru   r   )r'   rs   r   )ru   rt   r   rY   )r   r   r{   r   r   r   r   r   r     s:    z%TestSNAPDirected.build_original_graphc           
      C   s   ddiddiddiddid}ddd	d
igfddd	did	d
igfddd	d
igfddd	d
igfddd	d
igfg}t  }|D ]}|| }|j|f| qz|D ]\}}}|j|||d qddhddhddhddhddhddhd}	t ||	d |S )NrO   rn   r   ro   rp   r~   r   r   r   r~   r   rZ   r   r   r   r   r   r
   r   rL   rr   rs   rt   r'   ru   rv   rw   rx   ry   r}   r`   )r   r   r{   r   r   )
r   ra   r2   r'   rk   r|   r   r   r   r   r   r   r   rW     s4    z$TestSNAPDirected.build_summary_graphNr   r   r   r   r   r     s   #r   c                   @   s   e Zd Zdd Zdd ZdS )TestSNAPUndirectedMultic           
      C   s   ddiddiddiddiddiddiddiddiddid	}dddd	gfd
ddd	gfddd	gfddd	gfdddgfdddd	gfdddd	gfdddd	gfg}t  }|D ]}|| }|j|f| q|D ]$\}}}|D ]}	|j|||	d qq|S )NrO   rn   ro   rp   )	r
   r   rL   rr   rs   rt   r'   ru   rv   r
   rr   r   r   r   rs   rv   ru   rt   r'   rY   )r   
MultiGraphr{   r   
r   ra   r2   r'   rk   r|   r   r   r   rZ   r   r   r   r     s6    



z,TestSNAPUndirectedMulti.build_original_graphc              	   C   s  ddiddiddiddiddiddid}dddd	igfdd
dd	iddigfdd
ddigfdddd	iddigfd
d
dd	iddigfg}t  }|D ]}|| }|j|f| q|D ]$\}}}|D ]}	|j|||	d qqddhddhddhddhddhddhd}
t ||
d |S )NrO   rn   ro   rp   r}   r   r   rZ   r   r   r   r   r   rY   r
   r   rL   rr   rs   rt   r'   ru   rv   rw   rx   ry   r`   )r   r   r{   r   r   r   ra   r2   r'   rk   r|   r   r   r   rZ   r   r   r   r   rW     s:    	z+TestSNAPUndirectedMulti.build_summary_graphNr   r   r   r   r   r     s   !r   c                   @   s   e Zd Zdd Zdd ZdS )TestSNAPDirectedMultic           
      C   s  ddiddiddiddiddiddiddiddid}ddd	d
gfddd
gfddd	gfddd	d
gfddd	gfddd
gfddd	d
gfddd
gfddd
gfddd	d
gfddd
gfddd
gfg}t  }|D ]}|| }|j|f| q|D ]$\}}}|D ]}	|j|||	d qq|S )NrO   rn   r   ro   rp   r   r
   rL   r   r   rs   rt   r   rr   r'   ru   rY   )r   MultiDiGraphr{   r   r   r   r   r   r   ?  s<    







z*TestSNAPDirectedMulti.build_original_graphc                 C   s   ddiddiddiddid}dddd	gfdd
dd	gfdd
d	gfdddd	gfdd
d	gfg}t  }|D ]}|| }|j|f| qf|D ]$\}}}|D ]}	|j|||	d qqddhddhddhddhd}
t ||
d |S )NrO   rn   ro   rp   r   r~   r   r   r   r   r   rY   r
   r   rL   rr   rs   rt   r'   ru   r`   )r   r   r{   r   r   r   r   r   r   rW   c  s2    

z)TestSNAPDirectedMulti.build_summary_graphNr   r   r   r   r   r   >  s   $r   )__doc__ZpytestZnetworkxr   r   rH   markZparametrizerJ   r   r   r   rT   rU   rm   r   r   r   r   r   r   r   r   <module>   s     P 
![NFG