-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsplit_network_graphml.py
61 lines (40 loc) · 1.79 KB
/
split_network_graphml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/python
import sys
import os
import networkx as nx
import argparse
import pandas as pd
def main():
# define args
parser = argparse.ArgumentParser(description='Creating Component Summary')
parser.add_argument('input_graphml', help='input_graphml')
parser.add_argument('output_component_summary', help='output_component_summary')
parser.add_argument('output_component_graphml_folder', help='output_component_graphml_folder')
args = parser.parse_args()
# reading the graphml
G = nx.read_graphml(args.input_graphml)
# getting the components
components = list(nx.connected_components(G))
component_list = []
for i, component in enumerate(components):
subgraph = G.subgraph(component)
# This component number should come from the file itself
# Getting the component number
node_id = component.pop()
# Getting the compeont from the node in the component
component_id = G.nodes[node_id]["component"]
#component_id = i + 1
output_component_filename = os.path.join(args.output_component_graphml_folder, f"component_{component_id}.graphml")
nx.write_graphml(subgraph, output_component_filename)
component_dict = {}
component_dict["component_id"] = component_id
component_dict["number_of_nodes"] = len(component)
component_dict["number_of_edges"] = subgraph.number_of_edges()
component_list.append(component_dict)
# Sort this by descending number_of_nodes
component_list = sorted(component_list, key=lambda x: x["number_of_nodes"], reverse=True)
# writing the component summary
component_df = pd.DataFrame(component_list)
component_df.to_csv(args.output_component_summary, sep="\t", index=False)
if __name__ == "__main__":
main()