-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstruct_db_components.py
More file actions
45 lines (34 loc) · 1.42 KB
/
construct_db_components.py
File metadata and controls
45 lines (34 loc) · 1.42 KB
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
from pathlib import Path
import pandas as pd
import pickle
import networkx as nx
from helper_functions import read_graph_file, path_to_graph_name
# db_name = "graphs.csv"
db_name = "components.csv"
try:
df = pd.read_csv(db_name)
print(f"INFO: Openning an existing DB '{db_name}'.")
except FileNotFoundError:
df = pd.DataFrame(columns=["graph_name","parent_name","source","pickle_path","opt_sum"])
print(f"INFO: A new DB {db_name} was created.")
# parent je cel graf, graph je pa komponenta od parenta
paths = list(Path('data').rglob('*.al'))
paths += list(Path('data').rglob('*.d'))
for path_obj in paths:
path = str(path_obj)
parent_name = path_to_graph_name(path)
if parent_name not in list(df["parent_name"]):
# print(f"INFO: Reading {parent_name}")
G, _, source, their_result = read_graph_file(path)
sccs = nx.strongly_connected_components(G)
for i, scc_nodes in enumerate(sccs):
scc = nx.DiGraph(nx.subgraph(G,scc_nodes))
graph_name = f"{parent_name}_{i}"
pickle_path = f"pickles/{graph_name}.pickle"
with open(pickle_path, "wb") as f:
pickle.dump(scc, f)
df.loc[len(df)] = [graph_name,parent_name,source,pickle_path,their_result]
else:
print(f"INFO: Graph {parent_name} already in DB.")
df.to_csv(db_name, index=False)
print(f"INFO: A DB {db_name} was saved.")