
    wg@S                     ^   d Z ddlZddlZ G d d      Z G d d      Zej                  j                  dej                  ej                  ej                  ej                  g      d        Z G d	 d
      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zy)z8
Unit tests for dedensification and graph summarization
    Nc                   N    e Zd Zd Zd Zd Zedd       Zd Zd Z	d Z
d Zd	 Zy
)TestDirectedDedensificationc                     dddg dfddddgfd	dgfg}t        j                         }|D ]  \  }}|D ]  }|j                  ||          |S )
N1BC2ABC3AB64r   5ABr   r   r   nxDiGraphadd_edgeselforiginal_matrixgraphsourcetargetstargets         q/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/algorithms/tests/test_summarization.pybuild_original_graphz0TestDirectedDedensification.build_original_graph   ss    /"3%L3%L
 

. 	/OFG! /vv./	/     c                     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        j                         }|D ]  \  }}|D ]  }|j                  ||          |S )Nr   r
   r   r   r   r   r   r   r   r   )r   r   r   r   compressed_matrixcompressed_graphr   r   r    s         r!   build_compressed_graphz2TestDirectedDedensification.build_compressed_graph   s    5'N/"5'N3%L3%L	
 ::<0 	:OFG! : ))&&9:	:  r#   c                 ~    t        j                         }t        j                  |d      \  }}|t               k(  sJ y)zT
        Verify that an empty directed graph results in no compressor nodes
           	thresholdN)r   r   	dedensifyset)r   Gr'   c_nodess       r!   
test_emptyz&TestDirectedDedensification.test_empty,   s5     JJL$&LLa$@!'#%r#   c                 l   |r| j                         } |D ]  }t        t        j                  | |            }t        | j	                  |            }|D ]  }| j                  ||        ||z
  }|D ]-  }| j                  ||       |D ]  }| j                  ||        / | j                  |        | 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_neighbors	neighborsremove_edger   remove_node)	r/   compressor_nodesr3   compressor_noder4   out_neighborsout_neighborin_neighborsin_neighbors	            r!   densifyz#TestDirectedDedensification.densify4   s    & A/ 
	+O 0 0O DEMO <=M - =o|<=(=8L+ :k?;$1 :LJJ{L9:: MM/*
	+ r#   c                     d| _         y )N)r   r0   r   s    r!   setup_methodz(TestDirectedDedensification.setup_methodV   s	    r#   c                    | j                         }| j                         }t        j                  |d      \  }}|j	                         D ]d  \  }}dj                  t        |            }dj                  t        |            }|j                  ||      }	|j                  ||      }
|	|
k(  rdJ  t        |      t        | j                        k(  sJ y)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lenr0   )r   r/   compressed_Gr'   r0   sto_so_tcompressed_graph_existsverified_compressed_existss              r!   test_dedensify_edgesz0TestDirectedDedensification.test_dedensify_edgesY   s    
 %%'224$&LLa$@!'$**, 	IDAq''&)$C''&)$C&6&?&?1&E#)5)>)>sC)H&*.HHHH	I 7|s4<<0000r#   c                 &   | j                         }t        |j                               }t        j                  |d      \  }}t        |j                               }||k  sJ | j                         }|t        |j                               k(  sJ y)zu
        Verifies that dedensify produced the correct number of compressor nodes
        in a directed graph
        r*   r+   N)r"   rI   rE   r   r-   r(   )r   r/   original_edge_countc_Gr0   compressed_edge_countrJ   s          r!   test_dedensify_edge_countz5TestDirectedDedensification.test_dedensify_edge_counti   s    
 %%'!!'')n||A3W #CIIK 0$(;;;;224$L,>,>,@(AAAAr#   c                     | j                         }| j                  || j                  d      }| j                         }|j	                         D ],  \  }}|j                  ||      |j                  ||      k(  r,J  y)zq
        Verifies that densification produces the correct edges from the
        original directed graph
        T)r3   N)r(   r>   r0   r"   rE   rH   )r   rJ   original_graphr/   rK   rL   s         r!   test_densify_edgesz.TestDirectedDedensification.test_densify_edgesv   sy    
 224lDLLtL%%'GGI 	EDAq::a#~'>'>q!'DDDD	Er#   c                 *   | j                         }t        |j                               }| j                  || j                        }t        |j                               }||k  sJ | j                         }|t        |j                               k(  sJ y)zy
        Verifies that densification produces the correct number of edges in the
        original directed graph
        N)r(   rI   rE   r>   r0   r"   )r   rJ   rU   rX   rS   r/   s         r!   test_densify_edge_countz3TestDirectedDedensification.test_densify_edge_count   s    
 224 #L$6$6$8 9lDLLA!."6"6"89$(;;;;%%'"c!'')n444r#   N)T)__name__
__module____qualname__r"   r(   r1   staticmethodr>   rB   rQ   rV   rY   r[    r#   r!   r   r   
   sA      "   B 1 B	E5r#   r   c                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)TestUnDirectedDedensificationc                     dddg dfddddgfd	dgfg}t        j                         }|D ]  \  }}|D ]  }|j                  ||          |S )
zC
        Builds graph shown in the original research paper
        )r   CBr	   r   r   r   r   r   r   r   r   Graphr   r   s         r!   r"   z2TestUnDirectedDedensification.build_original_graph   su    
 /"3%L3%L
 
. 	/OFG! /vv./	/ r#   c                 ~    t        j                         }t        j                  |d      \  }}|t               k(  sJ y)zV
        Verify that an empty undirected graph results in no compressor nodes
        r*   r+   N)r   rf   r-   r.   )r   r/   rJ   r0   s       r!   r1   z(TestUnDirectedDedensification.test_empty   s4     HHJ "Q! <g#%r#   c                     d| _         y )N)6ABr   r@   rA   s    r!   rB   z*TestUnDirectedDedensification.setup_method   s	    %r#   c           
          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        j                         }|D ]  \  }}|D ]  }|j                  ||          |S )Nr   r   Cr
   r   r   ri   r   r   r   r   re   r%   s         r!   r(   z4TestUnDirectedDedensification.build_compressed_graph   s    3*5'N5'N5'N5'N5#,5%.!5%.!5'N

 88:0 	:OFG! : ))&&9:	:  r#   c                    | j                         }t        j                  |d      \  }}| j                         }|j	                         D ]d  \  }}dj                  t        |            }dj                  t        |            }|j                  ||      }	|j                  ||      }
|	|
k(  rdJ  t        |      t        | j                        k(  sJ y)z
        Verifies that dedensify produced correct compressor nodes and the
        correct edges to/from the compressor nodes in an undirected graph
        r*   r+   rD   N)
r"   r   r-   r(   rE   rF   rG   rH   rI   r0   )r   r/   rT   r0   v_compressed_GrK   rL   rM   rN   has_compressed_edgeverified_has_compressed_edges              r!   rQ   z2TestUnDirectedDedensification.test_dedensify_edges   s    
 %%'||A3W446IIK 	GDAq''&)$C''&)$C"%,,q!"4+9+B+B3+L(&*FFFF	G 7|s4<<0000r#   c                 ,   | j                         }t        j                  |dd      \  }}t        |j	                               }t        |j	                               }||k  sJ | j                         }t        |j	                               }||k(  sJ y)zm
        Verifies that dedensify produced the correct number of edges in an
        undirected graph
        r*   T)r,   r3   N)r"   r   r-   rI   rE   r(   )r   r/   rT   r0   rU   verified_original_edge_countverified_compressed_Gverified_compressed_edge_counts           r!   rV   z7TestUnDirectedDedensification.test_dedensify_edge_count   s    
 %%'||A>W #CIIK 0'*1779~$$(DDDD $ ; ; =),-B-H-H-J)K&$(FFFFr#   N)	r\   r]   r^   r"   r1   rB   r(   rQ   rV   r`   r#   r!   rb   rb      s!    & & $1 Gr#   rb   
graph_typec                 p     |        }t        j                  |d      }t        j                  ||      sJ y )Ncolor)node_attributes)r   snap_aggregationis_isomorphic)rt   r/   summary_graphs      r!   test_summarization_emptyr|      s3     	A'':FMM1---r#   c                   (    e Zd ZdZd Zd Zd Zd Zy)AbstractSNAPrv   c                      y Nr`   rA   s    r!   r"   z!AbstractSNAP.build_original_graph       r#   c                      y r   r`   rA   s    r!   build_summary_graphz AbstractSNAP.build_summary_graph   r   r#   c                     | j                         }| j                         }d}t        j                  || j                  |      }| j                  |      }t        j                  ||      sJ y Ntyper"   r   r   ry   rx   deterministic_labelsrz   r   rX   r{   relationship_attributesgenerated_summary_graphrelabeled_summary_graphs         r!   test_summary_graphzAbstractSNAP.test_summary_graph   sn    224002"+"$"5"5D002I#
 #'";";<S"T/FGGGr#   c                     t        j                        }t        |fd      }|j                          i }t	        |      D ]  \  }}d| }|||<    t        j                  |      S )Nc                 @    t        j                  |    d         d   S )Ngroupr   )rG   nodes)nr/   s    r!   <lambda>z3AbstractSNAP.deterministic_labels.<locals>.<lambda>   s    qwwqz'?R8STU8V r#   )keyz
Supernode-)listr   rG   sort	enumerater   relabel_nodes)r   r/   node_labelslabel_mappingindexnodelabels    `     r!   r   z!AbstractSNAP.deterministic_labels   sq    177m[.VW$[1 	(KE4 (E"'M$	( =11r#   N)r\   r]   r^   rx   r"   r   r   r   r`   r#   r!   r~   r~      s     O	H
2r#   r~   c                   "    e Zd ZdZd Zd Zd Zy)TestSNAPNoEdgeTypesr`   c                     | j                         }| j                         }d}t        j                  || j                        }| j                  |      }t        j                  ||      sJ y r   r   r   s         r!   r   z&TestSNAPNoEdgeTypes.test_summary_graph  sk    224002"+"$"5"5D00#
 #'";";<S"T/FGGGr#   c                     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        j                         }|D ]  }||   } |j                  |fi |  |D ]  \  }}|j                  ||        |S )Nrw   RedBlueYellowr   r   rk   DEFr/   HIJKL))r   r   )r   rk   )r   r   )r   r   )r   r   )r   r   )r   r   )rk   r/   )r   r   )r   r   )r   r   )r   r   r   rf   add_noder   )r   r   rE   r/   r   
attributesr   r    s           r!   r"   z(TestSNAPNoEdgeTypes.build_original_graph  s    5!5!5!5!6"6"6"6"8$8$8$8$

 HHJ 	+DtJAJJt*z*	+ $ 	'NFFJJvv&	' r#   c                 .   ddiddiddiddiddiddid}g d}t        j                         }|D ]  }||   } |j                  |fi |  |D ]  \  }}|j                  ||        ddhd	d
hddhddhddhddhd}t        j                  ||d       |S )Nrw   r   r   r   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   rk   r   r   r   r/   r   r   r   r   r   r   r   rf   r   r   set_node_attributes)	r   r   rE   r/   r   r   r   r    
supernodess	            r!   r   z'TestSNAPNoEdgeTypes.build_summary_graph>  s    #U+#U+#V,#V,#X.#X.

 HHJ 	+DtJAJJt*z*	+ $ 	'NFFJJvv&	'  ::::::

 	q*g6r#   N)r\   r]   r^   r   r   r"   r   r`   r#   r!   r   r   	  s     	H%N#r#   r   c                       e Zd Zd Zd Zy)TestSNAPUndirectedc                     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        j                         }|D ]  }||   } |j                  |fi |  |D ]  \  }}}|j                  |||        |S )Nrw   r   r   r   r   ))r   r   Strong)r   rk   Weakr   r   r   )r   r   r   )r   r   r   )r   r   r   r   r   r   )rk   r/   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   r   r   	r   r   rE   r/   r   r   r   r    r   s	            r!   r"   z'TestSNAPUndirected.build_original_graphe  s    5!5!5!5!6"6"6"6"8$8$8$8$

 HHJ 	+DtJAJJt*z*	+ %* 	2 FFDJJvvDJ1	2 r#   c                 :   ddiddiddiddiddiddid}g d}t        j                         }|D ]  }||   } |j                  |fi |  |D ]  \  }}}|j                  ||d|ig        d	d
hddhddhddhddhddhd}	t        j                  ||	d       |S )Nrw   r   r   r   r   ))r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   r   typesr   r   rk   r   r   r   r/   r   r   r   r   r   r   r   )
r   r   rE   r/   r   r   r   r    r   r   s
             r!   r   z&TestSNAPUndirected.build_summary_graph  s    #U+#U+#V,#V,#X.#X.

 HHJ 	+DtJAJJt*z*	+ %* 	? FFDJJvvvtn-=J>	?  ::::::

 	q*g6r#   Nr\   r]   r^   r"   r   r`   r#   r!   r   r   d  s    %N#r#   r   c                       e Zd Zd Zd Zy)TestSNAPDirectedc           	          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        j                         }|D ]  }||   } |j                  |fi |  |D ]  \  }}}|j                  |||        |S )	Nrw   r   Greenr   r   r   r   rk   r   r   r   r/   r   ))r   rk   r   r   )r   r   r   )r   r   r   )r   r   r   r   )rk   r/   r   )rk   r   r   )r   r   r   )r   r   r   )r/   r   r   )r   r   r   r   )r   r   r   r   r   s	            r!   r"   z%TestSNAPDirected.build_original_graph  s    5!5!7#7#6"6"8$8$	

 JJL 	+DtJAJJt*z*	+ %* 	2 FFDJJvvDJ1	2 r#   c                 p   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        j                         }|D ]  }||   } |j                  |fi |  |D ]  \  }}}|j                  |||        ddhddhddhddhddhddhd}	t        j                  ||	d       |S )Nrw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rk   r   r   r   r/   r   r   r   r   r   r   r   )r   r   r   r   r   )
r   r   rE   r/   r   r   r   r    r   r   s
             r!   r   z$TestSNAPDirected.build_summary_graph  sA   #U+#W-#V,#X.	
 MVX,>+?@MVV,<vx>P+QRMVX,>+?@MVX,>+?@MVX,>+?@
 JJL 	+DtJAJJt*z*	+ &+ 	4!FFEJJvvUJ3	4  ::::::

 	q*g6r#   Nr   r`   r#   r!   r   r     s    !Fr#   r   c                       e Zd Zd Zd Zy)TestSNAPUndirectedMultic                 R   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        j                         }|D ]  }||   } |j                  |fi |  |D ]!  \  }}}|D ]  }	|j                  |||	        # |S )Nrw   r   r   r   )	r   r   rk   r   r   r   r/   r   r   r   r   r   r   r   r   r   r   r   r/   r   )r   
MultiGraphr   r   
r   r   rE   r/   r   r   r   r    r   r   s
             r!   r"   z,TestSNAPUndirectedMulti.build_original_graph  sA   5!5!5!6"6"6"8$8$8$

 #)*#)*#z"#z"#x #)*#)*#)*	
 MMO 	+DtJAJJt*z*	+ &+ 	6!FFE 6

66
56	6 r#   c           	         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        j                         }|D ]  }||   } |j                  |fi |  |D ]!  \  }}}|D ]  }	|j                  |||	        # ddhddhddhddhddhddhd}
t        j                  ||
d       |S )Nrw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rk   r   r   r   r/   r   r   r   r   r   r   )r   r   r   r   r   r   r   rE   r/   r   r   r   r    r   r   r   s              r!   r   z+TestSNAPUndirectedMulti.build_summary_graph  sn   #U+#V,#X.#V,#X.#U+
 MVV,<+=>MVV,<vx>P+QRMVX,>+?@MVV,<vx>P+QRMVV,<vx>P+QR
 MMO 	+DtJAJJt*z*	+ &+ 	6!FFE 6

66
56	6
  ::::::

 	q*g6r#   Nr   r`   r#   r!   r   r     s    B"r#   r   c                       e Zd Zd Zd Zy)TestSNAPDirectedMultic                 r   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        j                         }|D ]  }||   } |j                  |fi |  |D ]!  \  }}}|D ]  }	|j                  |||	        # |S )Nrw   r   r   r   r   r   r   rk   r   r   r   r   r   r   r/   r   r   )r   MultiDiGraphr   r   r   s
             r!   r"   z*TestSNAPDirectedMulti.build_original_graph@  sg   5!5!7#7#6"6"8$8$	
 #)*#z"#x #)*#x #z"#)*#z"#z"#)*#z"#z"
 OO 	+DtJAJJt*z*	+ &+ 	6!FFE 6

66
56	6 r#   c                 ^   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        j                         }|D ]  }||   } |j                  |fi |  |D ]!  \  }}}|D ]  }	|j                  |||	        # ddhddhddhddhd}
t        j                  ||
d       |S )Nrw   r   r   r   r   r   r   r   r   r   r   r   r   r   rk   r   r   r   r/   r   r   )r   r   r   r   r   r   s              r!   r   z)TestSNAPDirectedMulti.build_summary_graphd  s'   #U+#V,#X.#V,	
 MFH+=>MFH+=>MH:6MFH+=>MH:6
 OO 	+DtJAJJt*z*	+ &+ 	6!FFE 6

66
56	6
  ::::	

 	q*g6r#   Nr   r`   r#   r!   r   r   ?  s    "Hr#   r   )__doc__pytestnetworkxr   r   rb   markparametrizerf   r   r   r   r|   r~   r   r   r   r   r   r`   r#   r!   <module>r      s     B5 B5JMG MG` 288RZZH..2 2BX, XvK K\C| CLDl DNCL Cr#   