
    fi@S                     l   d Z ddlZddlZ G d d          Z G d d          Ze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dS )z8
Unit tests for dedensification and graph summarization
    Nc                   V    e Zd Zd Zd Zd Zedd            Zd Zd Z	d Z
d	 Zd
 ZdS )TestDirectedDedensificationc                     dddg dfddddgfd	dgfg}t          j                    }|D ] \  }}|D ]}|                    ||           !|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         /var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/networkx/algorithms/tests/test_summarization.pybuild_original_graphz0TestDirectedDedensification.build_original_graph   s    ///"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 ]}|                    ||           !|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999:r#   c                     t          j                    }t          j        |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_nodess       r!   
test_emptyz&TestDirectedDedensification.test_empty,   sD     JLL$&La$@$@$@!'#%%r#   Tc                    |r|                                  } |D ]}t          t          j        | |                    }t          |                     |                    }|D ]}|                     ||           ||z
  }|D ]3}|                     ||           |D ]}|                     ||           4|                     |           | 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O D DEEMO < <==M - = =o|<<<<(=8L+ : :k?;;;$1 : :LJJ{L9999:MM/****r#   c                     d| _         d S )N)r   r0   r   s    r!   setup_methodz(TestDirectedDedensification.setup_methodV   s    r#   c                    |                                  }|                                 }t          j        |d          \  }}|                                D ]}\  }}d                    t          |                    }d                    t          |                    }|                    ||          }	|                    ||          }
|	|
k    sJ ~t          |          t          | j	                  k    sJ 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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4$&La$@$@$@!'$**,, 	I 	IDAq''&))$$C''&))$$C&6&?&?1&E&E#)5)>)>sC)H)H&*.HHHHHH7||s4<00000000r#   c                 j   |                                  }t          |                                          }t          j        |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"   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    
 %%''!!''))nn|A333W #CIIKK 0 0$(;;;;;2244$L,>,>,@,@(A(AAAAAAAr#   c                 $   |                                  }|                     || j        d          }|                                 }|                                D ]5\  }}|                    ||          |                    ||          k    sJ 6dS )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   s    
 2244lDLtLL%%''GGII 	E 	EDAq::a##~'>'>q!'D'DDDDDD	E 	Er#   c                 n   |                                  }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(   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4 #L$6$6$8$8 9 9lDLAA!."6"6"8"899$(;;;;;%%''"c!''))nn444444r#   N)T)__name__
__module____qualname__r"   r(   r1   staticmethodr>   rB   rQ   rV   rY   r[    r#   r!   r   r   
   s                "         \B     1 1 1 B B B	E 	E 	E5 5 5 5 5r#   r   c                   2    e Zd Zd Zd Zd Zd Zd Zd ZdS )TestUnDirectedDedensificationc                     dddg dfddddgfd	dgfg}t          j                    }|D ] \  }}|D ]}|                    ||           !|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   s    
 ///"3%L3%L
 

. 	/ 	/OFG! / /vv..../r#   c                     t          j                    }t          j        |d          \  }}|t                      k    sJ dS )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   sC     HJJ "Q! < < <g#%%r#   c                     d| _         d S )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 ]}|                    ||           !|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

 8::0 	: 	:OFG! : : ))&&9999:r#   c                    |                                  }t          j        |d          \  }}|                                 }|                                D ]}\  }}d                    t          |                    }d                    t          |                    }|                    ||          }	|                    ||          }
|	|
k    sJ ~t          |          t          | j	                  k    sJ 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+   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33W4466IIKK 	G 	GDAq''&))$$C''&))$$C"%,,q!"4"4+9+B+B3+L+L(&*FFFFFF7||s4<00000000r#   c                 p   |                                  }t          j        |dd          \  }}t          |                                          }t          |                                          }||k    sJ |                                 }t          |                                          }||k    sJ dS )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 #CIIKK 0 0'*17799~~$$(DDDDD $ ; ; = =),-B-H-H-J-J)K)K&$(FFFFFFFr#   N)	r\   r]   r^   r"   r1   rB   r(   rQ   rV   r`   r#   r!   rb   rb      ss          &     & & &     $1 1 1 G G G G Gr#   rb   
graph_typec                 t     |             }t          j        |d          }t          j        ||          sJ d S )Ncolor)node_attributes)r   snap_aggregationis_isomorphic)rt   r/   summary_graphs      r!   test_summarization_emptyr|      sC     	
A':FFFMM1-------r#   c                   *    e Zd ZdZd Zd Zd Zd ZdS )AbstractSNAPrv   c                     d S Nr`   rA   s    r!   r"   z!AbstractSNAP.build_original_graph       r#   c                     d S r   r`   rA   s    r!   build_summary_graphz AbstractSNAP.build_summary_graph   r   r#   c                     |                                  }|                                 }d}t          j        || j        |          }|                     |          }t          j        ||          sJ d S 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   s    22440022"+"$"5D02I#
 #
 #'";";<S"T"T/FGGGGGGGr#   c                     t          j                  }t          |fd          }|                                 i }t	          |          D ]\  }}d| }|||<   t          j        |          S )Nc                 P    t          j        |          d                   d         S )Ngroupr   )rG   nodes)nr/   s    r!   <lambda>z3AbstractSNAP.deterministic_labels.<locals>.<lambda>   s     qwqz'?R8S8STU8V r#   )keyz
Supernode-)listr   rG   sort	enumerater   relabel_nodes)r   r/   node_labelslabel_mappingindexnodelabels    `     r!   r   z!AbstractSNAP.deterministic_labels   s    17mm[.V.V.V.VWWW$[11 	( 	(KE4(((E"'M$=111r#   N)r\   r]   r^   rx   r"   r   r   r   r`   r#   r!   r~   r~      sX         O    	H 	H 	H
2 
2 
2 
2 
2r#   r~   c                   $    e Zd ZdZd Zd Zd ZdS )TestSNAPNoEdgeTypesr`   c                     |                                  }|                                 }d}t          j        || j                  }|                     |          }t          j        ||          sJ d S r   r   r   s         r!   r   z&TestSNAPNoEdgeTypes.test_summary_graph  s|    22440022"+"$"5D0#
 #
 #'";";<S"T"T/FGGGG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 ]\  }}|                    ||           |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$
 

 
 
 HJJ 	+ 	+DtJAJt**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 ]\  }}|                    ||           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.
 

 
 
 HJJ 	+ 	+DtJAJt**z****# 	' 	'NFFJJvv&&&&  ::::::
 

 	q*g666r#   N)r\   r]   r^   r   r   r"   r   r`   r#   r!   r   r   	  sK         	H 	H 	H% % %N# # # # #r#   r   c                       e Zd Zd Zd ZdS )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 ]\  }}}|                    |||           |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$
 

 
 
 HJJ 	+ 	+DtJAJt**z****$) 	2 	2 FFDJJvvDJ1111r#   c                 2   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 ]!\  }}}|                    ||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.
 

 
 
 HJJ 	+ 	+DtJAJt**z****$) 	? 	? FFDJJvvvtn-=J>>>>  ::::::
 

 	q*g666r#   Nr\   r]   r^   r"   r   r`   r#   r!   r   r   d  s3        % % %N# # # # #r#   r   c                       e Zd Zd Zd ZdS )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 ]\  }}}|                    |||           |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$	
 	

 
 
 JLL 	+ 	+DtJAJt**z****$) 	2 	2 FFDJJvvDJ1111r#   c                 h   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 ]\  }}}|                    |||           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  s_   #U+#W-#V,#X.	
 
 MVX,>+?@MVV,<vx>P+QRMVX,>+?@MVX,>+?@MVX,>+?@
 JLL 	+ 	+DtJAJt**z****%* 	4 	4!FFEJJvvUJ3333  ::::::
 

 	q*g666r#   Nr   r`   r#   r!   r   r     s3        ! ! !F    r#   r   c                       e Zd Zd Zd ZdS )TestSNAPUndirectedMultic                 H   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 ]}	|                    |||	           $|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  s\   5!5!5!6"6"6"8$8$8$

 

 #)*#)*#z"#z"#x #)*#)*#)*	
 MOO 	+ 	+DtJAJt**z****%* 	6 	6!FFE 6 6

66
5555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 ]}	|                    |||	           $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  s   #U+#V,#X.#V,#X.#U+
 
 MVV,<+=>MVV,<vx>P+QRMVX,>+?@MVV,<vx>P+QRMVV,<vx>P+QR
 MOO 	+ 	+DtJAJt**z****%* 	6 	6!FFE 6 6

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

 	q*g666r#   Nr   r`   r#   r!   r   r     s3          B" " " " "r#   r   c                       e Zd Zd Zd ZdS )TestSNAPDirectedMultic                 h   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 ]}	|                    |||	           $|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@  s   5!5!7#7#6"6"8$8$	
 	
 #)*#z"#x #)*#x #z"#)*#z"#z"#)*#z"#z"
 O 	+ 	+DtJAJt**z****%* 	6 	6!FFE 6 6

66
55556 r#   c                 R   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 ]}	|                    |||	           $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  sK   #U+#V,#X.#V,	
 
 MFH+=>MFH+=>MH:6MFH+=>MH:6
 O 	+ 	+DtJAJt**z****%* 	6 	6!FFE 6 6

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

 	q*g666r#   Nr   r`   r#   r!   r   r   ?  s3        " " "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5 B5 B5 B5 B5 B5 B5JMG MG MG MG MG MG MG MG` 28RZH . . .2 2 2 2 2 2 2 2BX X X X X, X X XvK K K K K K K K\C C C C C| C C CLD D D D Dl D D DNC C C C CL C C C C Cr#   