o
    ]Zh@S                     @   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   V   dddg dfddddgfd	dgfg}t  }|D ]\}}|D ]}||| qq|S )
N1BC2ABC3AB64r	   5ZABr   r   r   nxDiGraphadd_edgeselfZoriginal_matrixgraphsourcetargetstarget r   [/var/www/auris/lib/python3.10/site-packages/networkx/algorithms/tests/test_summarization.pybuild_original_graph   s   
	z0TestDirectedDedensification.build_original_graphc                 C   sd   dddgfdg dfddgfddd	gfd
dgfdg}t  }|D ]\}}|D ]}||| q&q |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   ,   t  }t j|dd\}}|t ksJ dS )zT
        Verify that an empty directed graph results in no compressor nodes
           	thresholdN)r   r   	dedensifyset)r   Gr!   c_nodesr   r   r   
test_empty,      z&TestDirectedDedensification.test_emptyTc           	      C   s   |r|   } |D ];}tt| |}t| |}|D ]}| || q|| }|D ]}| || |D ]}| || q4q*| | 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   densify4   s   z#TestDirectedDedensification.densifyc                 C   
   d| _ d S )N)r	   r*   r   r   r   r   setup_methodV      
z(TestDirectedDedensification.setup_methodc                 C   s   |   }|  }tj|dd\}}| D ]$\}}dt|}dt|}|||}	|||}
|	|
ks9J qt|t| j	ksEJ 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_edgelenr*   )r   r)   compressed_Gr!   r*   sto_so_tZcompressed_graph_existsZverified_compressed_existsr   r   r   test_dedensify_edgesY   s   z0TestDirectedDedensification.test_dedensify_edgesc                 C   s^   |   }t| }tj|dd\}}t| }||ksJ |  }|t| ks-J dS )zu
        Verifies that dedensify produced the correct number of compressor nodes
        in a directed graph
        r$   r%   N)r   r;   r7   r   r'   r"   )r   r)   original_edge_countc_Gr*   compressed_edge_countr<   r   r   r   test_dedensify_edge_counti   s   z5TestDirectedDedensification.test_dedensify_edge_countc                 C   sT   |   }| j|| jdd}|  }| D ]\}}||||||ks'J qdS )zq
        Verifies that densification produces the correct edges from the
        original directed graph
        T)r-   N)r"   r0   r*   r   r7   r:   )r   r<   original_graphr)   r=   r>   r   r   r   test_densify_edgesv   s   z.TestDirectedDedensification.test_densify_edgesc                 C   sZ   |   }t| }| || j}t| }||ksJ |  }|t| ks+J dS )zy
        Verifies that densification produces the correct number of edges in the
        original directed graph
        N)r"   r;   r7   r0   r*   r   )r   r<   rD   rF   rB   r)   r   r   r   test_densify_edge_count   s   z3TestDirectedDedensification.test_densify_edge_countN)T)__name__
__module____qualname__r   r"   r+   staticmethodr0   r4   rA   rE   rG   rH   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   r   )
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   r#   )zV
        Verify that an empty undirected graph results in no compressor nodes
        r$   r%   N)r   rO   r'   r(   )r   r)   r<   r*   r   r   r   r+      r,   z(TestUnDirectedDedensification.test_emptyc                 C   r1   )N)6ABr	   r2   r3   r   r   r   r4      r5   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 ]}||| q6q0|S )Nr   r   Cr   r	   r
   rP   r   r   r   r   rN   r    r   r   r   r"      s    



z4TestUnDirectedDedensification.build_compressed_graphc                 C   s   |   }tj|dd\}}|  }| D ]$\}}dt|}dt|}|||}	|||}
|	|
ks9J qt|t| j	ksEJ 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%   r6   N)
r   r   r'   r"   r7   r8   r9   r:   r;   r*   )r   r)   rC   r*   Zv_compressed_Gr=   r>   r?   r@   Zhas_compressed_edgeZverified_has_compressed_edger   r   r   rA      s   z2TestUnDirectedDedensification.test_dedensify_edgesc                 C   sd   |   }tj|ddd\}}t| }t| }||ks J |  }t| }||ks0J dS )zm
        Verifies that dedensify produced the correct number of edges in an
        undirected graph
        r$   T)r&   r-   N)r   r   r'   r;   r7   r"   )r   r)   rC   r*   rD   Zverified_original_edge_countZverified_compressed_GZverified_compressed_edge_countr   r   r   rE      s   z7TestUnDirectedDedensification.test_dedensify_edge_countN)	rI   rJ   rK   r   r+   r4   r"   rA   rE   r   r   r   r   rM      s    rM   
graph_typec                 C   s(   |  }t j|dd}t ||sJ d S )Ncolor)node_attributes)r   snap_aggregationis_isomorphic)rR   r)   summary_graphr   r   r   test_summarization_empty   s   rY   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )AbstractSNAPrS   c                 C      d S Nr   r3   r   r   r   r         z!AbstractSNAP.build_original_graphc                 C   r[   r\   r   r3   r   r   r   build_summary_graph   r]   z AbstractSNAP.build_summary_graphc                 C   sB   |   }|  }d}t|| j|}| |}t||sJ d S Ntyper   r^   r   rV   rU   deterministic_labelsrW   r   rF   rX   relationship_attributesZgenerated_summary_graphZrelabeled_summary_graphr   r   r   test_summary_graph   s   
zAbstractSNAP.test_summary_graphc                    sZ   t  j}t| fddd}|  i }t|D ]\}}d| }|||< qt |S )Nc                    s   t  j|  d d S )Ngroupr   )r9   nodes)nr)   r   r   <lambda>   s    z3AbstractSNAP.deterministic_labels.<locals>.<lambda>)keyz
Supernode-)listrh   r9   sort	enumerater   Zrelabel_nodes)r   r)   Znode_labelsZlabel_mappingindexnodelabelr   rj   r   rc      s   


z!AbstractSNAP.deterministic_labelsN)rI   rJ   rK   rU   r   r^   rf   rc   r   r   r   r   rZ      s    rZ   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J d S r_   rb   rd   r   r   r   rf     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}g d}t  }|D ]}|| }|j|fi | q1|D ]
\}}||| qC|S )NrT   RedBlueYellowr   r   rQ   DEFr)   HIJKL))r   r   )r   rQ   )r   ry   )r   r|   )r   rx   )r   r}   )r   rz   )rQ   r)   )rx   r{   )r|   r}   )r}   r~   )r|   r   r   rO   add_noder   )r   rh   r7   r)   rq   
attributesr   r   r   r   r   r     s*   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}g d}t  }|D ]}|| }|j|fi | q|D ]
\}}||| q1ddhd	d
hddhddhddhddhd}t ||d |S )NrT   rt   ru   rv   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   rQ   rx   ry   rz   r)   r{   r|   r}   r~   r   rg   r   rO   r   r   set_node_attributes)	r   rh   r7   r)   rq   r   r   r   
supernodesr   r   r   r^   >  s.   	z'TestSNAPNoEdgeTypes.build_summary_graphN)rI   rJ   rK   re   rf   r   r^   r   r   r   r   rs   	  s
    'rs   c                   @      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}g d}t  }|D ]}|| }|j|fi | q1|D ]\}}}|j|||d qC|S )NrT   rt   ru   rv   rw   ))r   r   Strong)r   rQ   Weakr   ry   r   )r   r|   r   )r   rx   r   )r   r}   r   r   rz   r   )rQ   r)   r   )rx   r{   r   )r|   r}   r   )r}   r~   r   )r|   r   r   r`   r   	r   rh   r7   r)   rq   r   r   r   ra   r   r   r   r   e  s*   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}g d}t  }|D ]}|| }|j|fi | q|D ]\}}}|j||d|igd q1d	d
hddhddhddhddhddhd}	t ||	d |S )NrT   rt   ru   rv   r   ))r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   ra   typesr   r   rQ   rx   ry   rz   r)   r{   r|   r}   r~   r   rg   r   )
r   rh   r7   r)   rq   r   r   r   ra   r   r   r   r   r^     s.   	z&TestSNAPUndirected.build_summary_graphNrI   rJ   rK   r   r^   r   r   r   r   r   d  s    'r   c                   @   r   )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}g d}t  }|D ]}|| }|j|fi | q%|D ]\}}}|j|||d q7|S )	NrT   rt   Greenru   rv   r   r   rQ   rx   ry   rz   r)   r{   ))r   rQ   r   r   )r   rz   r   )r   rx   r   )r   ry   r   r   )rQ   r)   r   )rQ   rz   r   )rx   ry   r   )rx   r{   r   )r)   ry   r   )r{   rz   r   r`   )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i | q=|D ]\}}}|j|||d qOddhddhddhddhddhddhd}	t ||	d |S )NrT   rt   r   ru   rv   r   r   r   r   r   r   ra   r   r   r   r   r   r   r   rQ   rx   ry   rz   r)   r{   r|   r}   r~   r   r   rg   )r   r   r   r   r   )
r   rh   r7   r)   rq   r   r   r   r   r   r   r   r   r^     s4   z$TestSNAPDirected.build_summary_graphNr   r   r   r   r   r     s    #r   c                   @   r   )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i | qS|D ]\}}}|D ]
}	|j|||	d qlqe|S )NrT   rt   ru   rv   )	r   r   rQ   rx   ry   rz   r)   r{   r|   r   rx   r   r   r   ry   r|   r{   rz   r)   r`   )r   
MultiGraphr   r   
r   rh   r7   r)   rq   r   r   r   r   ra   r   r   r   r     s8   



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i | qI|D ]\}}}|D ]
}	|j|||	d qbq[ddhddhddhddhddhddhd}
t ||
d |S )NrT   rt   ru   rv   r   r   r   ra   r   r   r   r   r   r`   r   r   rQ   rx   ry   rz   r)   r{   r|   r}   r~   r   rg   )r   r   r   r   r   r   rh   r7   r)   rq   r   r   r   r   ra   r   r   r   r   r^     s<   	z+TestSNAPUndirectedMulti.build_summary_graphNr   r   r   r   r   r     s    !r   c                   @   r   )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i | qc|D ]\}}}|D ]
}	|j|||	d q|qu|S )NrT   rt   r   ru   rv   r   r   rQ   r   r   ry   rz   r   rx   r)   r{   r`   )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i | q3|D ]\}}}|D ]
}	|j|||	d qLqEddhddhddhddhd}
t ||
d |S )NrT   rt   ru   rv   r   r   r   r   r   r   r   r`   r   r   rQ   rx   ry   rz   r)   r{   rg   )r   r   r   r   r   r   r   r   r   r^   d  s4   

z)TestSNAPDirectedMulti.build_summary_graphNr   r   r   r   r   r   ?  s    $r   )__doc__ZpytestZnetworkxr   r   rM   markZparametrizerO   r   r   r   rY   rZ   rs   r   r   r   r   r   r   r   r   <module>   s      P
![NFG