1212logger = logging .getLogger ("dim" )
1313
1414TEMPLATE_DIR = os .path .join (
15- os .path .dirname (os .path .realpath (__file__ )), "utils" ,"templates"
15+ os .path .dirname (os .path .realpath (__file__ )), "utils" , "templates"
1616)
1717
18+
1819def save_output_graph (G , name ):
1920 nt = Network ()
2021
2122 # HTML template for view graph details panel
22- nt .set_template (os .path .join (TEMPLATE_DIR , "template.html" ).replace (' \\ ' , '/' ))
23+ nt .set_template (os .path .join (TEMPLATE_DIR , "template.html" ).replace (" \\ " , "/" ))
2324 nt .from_nx (G )
2425 nt .toggle_physics (False )
2526
@@ -41,9 +42,9 @@ def save_output_graph(G, name):
4142 )
4243 )
4344 html = nt .generate_html (name , notebook = False )
44- with open (name , mode = 'w' , encoding = ' utf-8' ) as fp :
45+ with open (name , mode = "w" , encoding = " utf-8" ) as fp :
4546 fp .write (html )
46- #nt.write_html(name, notebook=False, open_browser=False)
47+ # nt.write_html(name, notebook=False, open_browser=False)
4748
4849 return
4950
@@ -71,14 +72,6 @@ def view_graph(db, output_dir, imgs_dir):
7172 img2 = int (img2 )
7273 G .add_edge (img1 , img2 , matches = rows )
7374
74- """
75- Remove output files if they exist
76- """
77- try :
78- os .remove (os .path .join (output_dir , "communities.txt" ))
79- except OSError :
80- pass
81-
8275 # Create list of aligned images and
8376 # add NA prefix for not aligned images
8477 aligned_nodes = []
@@ -131,26 +124,28 @@ def view_graph(db, output_dir, imgs_dir):
131124 G .nodes [n ]["y" ] = - pos [1 ]
132125
133126 # Compute communities using modularity
134- C = nx .community .greedy_modularity_communities (AG , "matches" )
127+ C = nx .community .greedy_modularity_communities (AG , "matches" , resolution = 1 )
135128 i = 0
136129 Cs = []
137130 # Compute clustering coefficient for each node
138131 clustering = nx .clustering (AG , weight = "matches" )
139132
140- # Write communities output file
141- with open (os .path .join (output_dir , "communities.csv" ), "a" ) as comm_file :
142- print (
143- "IMG_ID,IMG_NAME,Community_ID,Clustering_coefficient(0,1),IS_OUTLIER?[0,1]" ,
144- file = comm_file ,
145- )
133+ # Compute maximum spannning tree
134+ MST = nx .maximum_spanning_tree (AG , "matches" )
135+ MST_raw = nx .maximum_spanning_tree (AG , "matches" )
136+
146137 for c in C :
147138 Cg = G .subgraph (c ) # Draw communities with different colors
148139 comm_clustering = [clustering [n ] for n in Cg .nodes ]
149140 avg_comm_clustering = mean (comm_clustering )
150141 threshold = 0.3
142+ MST .add_edges_from (Cg .edges (data = True ))
143+
151144 for n in Cg .nodes ():
152145 # Draw communities with different colors
153146 G .nodes [n ]["group" ] = i
147+ MST .nodes [n ]["group" ] = i
148+ MST_raw .nodes [n ]["group" ] = i
154149 # Draw probable outliers with larger shape
155150 if clustering [n ] < threshold * avg_comm_clustering :
156151 G .nodes [n ]["font" ] = {"size" : 12 }
@@ -164,16 +159,72 @@ def view_graph(db, output_dir, imgs_dir):
164159 )
165160 with open (comm_file .name , "a" ) as comm_file :
166161 print (out , file = comm_file )
162+
163+ l = []
164+
165+ # MST expansion
166+ # Find edges between communities
167+ for e in MST .edges ():
168+ # Adjacent communities
169+ if G .nodes [e [0 ]]["group" ] != G .nodes [e [1 ]]["group" ]:
170+ l = [
171+ (u , v , d )
172+ for u , v , d in G .edges (data = True )
173+ if (
174+ lambda u , v , d : G .nodes [u ]["group" ]
175+ == G .nodes [e [0 ]]["group" ]
176+ and G .nodes [v ]["group" ] == G .nodes [e [1 ]]["group" ]
177+ )(u , v , d )
178+ ]
179+ MST .add_edges_from (l )
180+
167181 i += 1
168182 Cs .append (Cg .number_of_nodes ())
169183 G .graph ["communities" ] = Cs
170184
185+ """
186+ Remove output files if they exist
187+ """
188+ try :
189+ os .remove (os .path .join (output_dir , "communities.txt" ))
190+ except OSError :
191+ pass
192+ try :
193+ os .remove (os .path .join (output_dir , "raw_mst_pairs.txt" ))
194+ except OSError :
195+ pass
196+ try :
197+ os .remove (os .path .join (output_dir , "exp_mst_pairs.txt" ))
198+ except OSError :
199+ pass
200+
201+ # Write communities output file
202+ with open (os .path .join (output_dir , "communities.csv" ), "a" ) as comm_file :
203+ print (
204+ "IMG_ID,IMG_NAME,Community_ID,Clustering_coefficient(0,1),IS_OUTLIER?[0,1]" ,
205+ file = comm_file ,
206+ )
207+
208+ # Write MST and MST_expanded pairs output files
209+ with open (os .path .join (output_dir , "raw_mst_pairs.txt" ), "w" ) as f :
210+ for e in MST_raw .edges ():
211+ print (
212+ "{} {}" .format (G .nodes [e [0 ]]["title" ], G .nodes [e [1 ]]["title" ]), file = f
213+ )
214+
215+ with open (os .path .join (output_dir , "exp_mst_pairs.txt" ), "w" ) as f :
216+ for e in MST .edges ():
217+ print (
218+ "{} {}" .format (G .nodes [e [0 ]]["title" ], G .nodes [e [1 ]]["title" ]), file = f
219+ )
220+
171221 cwd = os .getcwd ()
172222 os .chdir (output_dir )
173223 save_output_graph (G , "graph.html" )
174- logger .info (
175- "View graph written at {}" .format (os .path .join (output_dir , "graph.html" ))
176- )
224+ save_output_graph (MST , "exp_mst.html" )
225+ save_output_graph (MST_raw , "raw_mst.html" )
226+
227+ logger .info ("View graphs written at {}" .format (output_dir ))
177228 os .chdir (cwd )
178229
179230 return
0 commit comments