-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_utils.py
176 lines (144 loc) · 6.27 KB
/
data_utils.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import os
import pickle
import networkx as nx
import torch
from torch_geometric.utils import from_networkx
from constants import FEATURE_RANGES, TARGET_RANGES
def min_max_scale(value, min_value, max_value):
return (value - min_value) / (max_value - min_value)
def load_topological_graphs_from_pickle(directory="networkx_graphs_topological"):
data_list = []
FEATURES = set()
counter = 0
for filename in sorted(os.listdir(directory)):
filepath = os.path.join(directory, filename)
# Load the graph from the pickle file
with open(filepath, "rb") as f:
G = pickle.load(f)
# Transform the node IDs to consecutive integers
G = nx.convert_node_labels_to_integers(G, label_attribute="original_id")
# Collect all edge attribute names
for _, _, attr in G.edges(data=True):
FEATURES.update(attr.keys())
# Convert edge attributes to floats
for u, v, attr in G.edges(data=True):
for key, value in attr.items():
min_val = FEATURE_RANGES[key]["min"]
max_val = FEATURE_RANGES[key]["max"]
attr[key] = min_max_scale(float(value), min_val, max_val)
# Convert the graph to a PyTorch Geometric Data object
data = from_networkx(G)
# Assign node_ids
data.node_ids = torch.arange(data.num_nodes)
num_edges = data.edge_index.size(1)
edge_attrs = []
# Create a dictionary to map node pairs to their attributes
edge_attr_dict = {}
for u, v, attr in G.edges(data=True):
# Ensure consistent order of FEATURES
edge_attr_dict[(u, v)] = [attr.get(key, 0.0) for key in FEATURES]
# Align the attributes with data.edge_index
for i in range(num_edges):
u = data.edge_index[0, i].item()
v = data.edge_index[1, i].item()
# Get the edge attributes for the pair (u, v) or (v, u)
attr = edge_attr_dict.get((u, v)) or edge_attr_dict.get((v, u))
if attr is None:
raise ValueError(
f"Edge ({u}, {v}) or ({v}, {u}) not found in edge_attr_dict"
)
edge_attrs.append(attr)
data.edge_attr = torch.tensor(edge_attrs, dtype=torch.float)
# Assign node features (x)
num_nodes = data.num_nodes
data.x = None # No features, will use node_embeddings
# y = [osnr, snr, ber]
labels = G.graph.get("labels", {})
y_scaled = []
for key in ["osnr", "snr", "ber"]:
min_val = TARGET_RANGES[key]["min"]
max_val = TARGET_RANGES[key]["max"]
value = labels.get(key, 0.0)
scaled_value = min_max_scale(float(value), min_val, max_val)
y_scaled.append(scaled_value)
data.y = torch.tensor(y_scaled, dtype=torch.float)
# Add the Data object to the list
data_list.append(data)
counter += 1
if counter % 15000 == 0:
print(f"Processed {counter} graphs")
# Convert FEATURES to a sorted list for consistent ordering
FEATURES = sorted(FEATURES)
return data_list, FEATURES
def load_lightpath_graphs_from_pickle(directory="networkx_graphs_lightpath"):
data_list = []
NODE_FEATURES = set()
counter = 0
for filename in sorted(os.listdir(directory)):
filepath = os.path.join(directory, filename)
# Load the graph from the pickle file
with open(filepath, "rb") as f:
G = pickle.load(f)
# Transform the node IDs to consecutive integers
G = nx.convert_node_labels_to_integers(G, label_attribute="original_id")
# remove the 'original_id' attribute
for node, attr in G.nodes(data=True):
if "original_id" in attr:
del attr["original_id"]
# Collect all node attribute names
for _, attr in G.nodes(data=True):
NODE_FEATURES.update(attr.keys())
# Convert node attributes to floats
for node, attr in G.nodes(data=True):
for key, value in attr.items():
if key == "is_lut":
attr[key] = float(value) # Keep original value (0.0 or 1.0)
else:
try:
min_val = FEATURE_RANGES[key]["min"]
max_val = FEATURE_RANGES[key]["max"]
attr[key] = min_max_scale(float(value), min_val, max_val)
except ValueError:
pass
# Convert the graph to a PyTorch Geometric Data object
data = from_networkx(G)
num_nodes = data.num_nodes
node_attrs = []
# Create a dictionary to map node IDs to their attributes
node_attr_dict = {}
for node, attr in G.nodes(data=True):
node_attr_dict[node] = [
attr.get(key, 0.0)
for key in sorted(NODE_FEATURES)
if isinstance(attr.get(key, 0.0), (int, float))
]
# Align the attributes with data.x
for i in range(num_nodes):
node_id = (
i # Since node labels are now consecutive integers starting from 0
)
attr = node_attr_dict.get(node_id)
if attr is None:
raise ValueError(f"Node {node_id} not found in node_attr_dict")
node_attrs.append(attr)
data.x = torch.tensor(node_attrs, dtype=torch.float)
data.edge_attr
# y = [osnr, snr, ber]
labels = G.graph.get("labels", {})
y_scaled = []
for key in ["osnr", "snr", "ber"]:
min_val = TARGET_RANGES[key]["min"]
max_val = TARGET_RANGES[key]["max"]
value = labels.get(key, 0.0)
scaled_value = min_max_scale(float(value), min_val, max_val)
y_scaled.append(scaled_value)
data.y = torch.tensor(y_scaled, dtype=torch.float)
# Add the Data object to the list
data_list.append(data)
counter += 1
if counter % 15000 == 0:
print(f"Processed {counter} graphs")
# Convert NODE_FEATURES to a sorted list for consistent ordering
NODE_FEATURES = sorted(NODE_FEATURES)
feature_indices = {key: idx for idx, key in enumerate(NODE_FEATURES)}
return data_list, NODE_FEATURES, feature_indices