-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraph_risk_engine.py
More file actions
402 lines (322 loc) · 12.6 KB
/
Copy pathgraph_risk_engine.py
File metadata and controls
402 lines (322 loc) · 12.6 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import json
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch_geometric.nn import GATConv
from torch_geometric.data import Data
import sqlite3
from datetime import datetime
from typing import List, Dict, Tuple
from dataclasses import dataclass
@dataclass
class GraphNode:
node_id: str
latitude: float
longitude: float
risk_vector: np.ndarray
@dataclass
class GraphEdge:
source: str
target: str
distance_km: float
trade_volume: float
class GraphAttentionRiskModel(nn.Module):
def __init__(self,
input_dim: int = 6,
hidden_dim: int = 16,
output_dim: int = 6,
num_heads: int = 4):
super(GraphAttentionRiskModel, self).__init__()
self.gat1 = GATConv(
in_channels=input_dim,
out_channels=hidden_dim,
heads=num_heads,
dropout=0.2,
concat=True
)
self.gat2 = GATConv(
in_channels=hidden_dim * num_heads,
out_channels=output_dim,
heads=1,
dropout=0.2,
concat=False
)
self.temporal_decay = nn.Parameter(torch.tensor(0.9))
def forward(self, x, edge_index, edge_attr=None):
x = self.gat1(x, edge_index)
x = F.elu(x)
x = F.dropout(x, p=0.2, training=self.training)
x = self.gat2(x, edge_index)
x = torch.sigmoid(x)
return x
class GraphRiskEngine:
def __init__(self, db_path: str = "supply_chain_graph.db"):
self.db_path = db_path
self.model = GraphAttentionRiskModel()
self.optimizer = torch.optim.Adam(self.model.parameters(), lr=0.001)
self.nodes: Dict[str, GraphNode] = {}
self.edges: List[GraphEdge] = []
self.history_length = 10
self._init_database()
print("Graph Risk Engine initialized")
def _init_database(self):
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS nodes (
node_id TEXT PRIMARY KEY,
latitude REAL,
longitude REAL,
current_risk REAL,
last_updated TEXT
)
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS edges (
edge_id INTEGER PRIMARY KEY AUTOINCREMENT,
source TEXT,
target TEXT,
distance_km REAL,
trade_volume REAL,
UNIQUE(source, target)
)
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS risk_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
node_id TEXT,
timestamp TEXT,
gscpi_risk REAL,
news_risk REAL,
political_risk REAL,
trade_risk REAL,
weather_risk REAL,
reporter_confidence REAL,
overall_risk REAL,
FOREIGN KEY (node_id) REFERENCES nodes(node_id)
)
""")
cursor.execute("""
CREATE INDEX IF NOT EXISTS idx_history_node_time
ON risk_history(node_id, timestamp DESC)
""")
conn.commit()
conn.close()
print("Database initialized")
def load_graph_structure(self, nodes_file: str, edges_file: str):
with open(nodes_file, 'r') as f:
nodes_data = json.load(f)
with open(edges_file, 'r') as f:
edges_data = json.load(f)
self.load_graph_structure_from_data(
nodes_data.get("nodes", []),
edges_data.get("edges", []),
)
def load_graph_structure_from_data(self, nodes: List[Dict], edges: List[Dict]):
self.nodes = {}
self.edges = []
for node in nodes:
self.nodes[node["node_id"]] = GraphNode(
node_id=node["node_id"],
latitude=node["latitude"],
longitude=node["longitude"],
risk_vector=np.zeros(6),
)
for edge in edges:
self.edges.append(
GraphEdge(
source=edge["source"],
target=edge["target"],
distance_km=edge["distance_km"],
trade_volume=edge.get("trade_volume", 0.0),
)
)
self._store_edges()
print(f"Loaded graph: {len(self.nodes)} nodes, {len(self.edges)} edges")
def _store_edges(self):
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
for edge in self.edges:
cursor.execute(
"""
INSERT OR REPLACE INTO edges (source, target, distance_km, trade_volume)
VALUES (?, ?, ?, ?)
""",
(edge.source, edge.target, edge.distance_km, edge.trade_volume),
)
conn.commit()
conn.close()
def update_risk_vectors(self, risk_vectors_file: str):
with open(risk_vectors_file, 'r') as f:
data = json.load(f)
for rv in data["risk_vectors"]:
node_id = rv["node_id"]
if node_id in self.nodes:
self.nodes[node_id].risk_vector = np.array([
rv["gscpi_risk"],
rv["news_risk"],
rv["political_risk"],
rv["trade_risk"],
rv["weather_risk"],
rv["reporter_confidence"]
])
print(f"Updated {len(data['risk_vectors'])} node risk vectors")
def update_edge_trade_volumes(self, node_trade_map: Dict[str, float]):
updated_edges: List[GraphEdge] = []
for edge in self.edges:
source_trade = max(float(node_trade_map.get(edge.source, 0.0)), 0.0)
target_trade = max(float(node_trade_map.get(edge.target, 0.0)), 0.0)
if source_trade == 0.0 and target_trade == 0.0:
trade_volume = 0.0
else:
trade_volume = (source_trade + target_trade) / 2
updated_edges.append(
GraphEdge(
source=edge.source,
target=edge.target,
distance_km=edge.distance_km,
trade_volume=trade_volume,
)
)
self.edges = updated_edges
self._store_edges()
def _build_pytorch_geometric_data(self) -> Data:
node_ids = list(self.nodes.keys())
node_id_to_idx = {nid: idx for idx, nid in enumerate(node_ids)}
if not node_ids:
raise ValueError("Graph has no nodes loaded")
x = torch.tensor(
np.array([self.nodes[nid].risk_vector for nid in node_ids]),
dtype=torch.float32
)
edge_index = []
edge_weights = []
for edge in self.edges:
if edge.source in node_id_to_idx and edge.target in node_id_to_idx:
src_idx = node_id_to_idx[edge.source]
tgt_idx = node_id_to_idx[edge.target]
edge_index.append([src_idx, tgt_idx])
edge_index.append([tgt_idx, src_idx])
weight = 1.0 / (1.0 + edge.distance_km / 1000.0)
edge_weights.extend([weight, weight])
if not edge_index:
edge_index = torch.empty((2, 0), dtype=torch.long)
edge_weights = torch.empty((0,), dtype=torch.float32)
else:
edge_index = torch.tensor(edge_index, dtype=torch.long).t().contiguous()
edge_weights = torch.tensor(edge_weights, dtype=torch.float32)
data = Data(x=x, edge_index=edge_index, edge_attr=edge_weights)
return data, node_ids
def propagate_risks(self) -> Dict[str, np.ndarray]:
print("Running risk propagation through graph")
data, node_ids = self._build_pytorch_geometric_data()
if data.edge_index.numel() == 0:
updated_features = data.x
else:
self.model.eval()
with torch.no_grad():
updated_features = self.model(data.x, data.edge_index)
updated_risks = {}
for idx, node_id in enumerate(node_ids):
updated_risks[node_id] = updated_features[idx].numpy()
self.nodes[node_id].risk_vector = updated_features[idx].numpy()
print(f"Risk propagation complete for {len(updated_risks)} nodes")
return updated_risks
def store_snapshot(self, timestamp: str = None):
if timestamp is None:
timestamp = datetime.utcnow().isoformat()
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
for node_id, node in self.nodes.items():
rv = node.risk_vector
overall_risk = float(np.mean(rv[:5]))
cursor.execute("""
INSERT OR REPLACE INTO nodes (node_id, latitude, longitude, current_risk, last_updated)
VALUES (?, ?, ?, ?, ?)
""", (node_id, node.latitude, node.longitude, overall_risk, timestamp))
cursor.execute("""
INSERT INTO risk_history
(node_id, timestamp, gscpi_risk, news_risk, political_risk,
trade_risk, weather_risk, reporter_confidence, overall_risk)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (node_id, timestamp, float(rv[0]), float(rv[1]), float(rv[2]),
float(rv[3]), float(rv[4]), float(rv[5]), overall_risk))
cursor.execute(
"""
DELETE FROM risk_history
WHERE id IN (
SELECT id FROM (
SELECT id,
ROW_NUMBER() OVER (
PARTITION BY node_id
ORDER BY timestamp DESC
) AS row_num
FROM risk_history
)
WHERE row_num > ?
)
""",
(self.history_length,),
)
conn.commit()
conn.close()
print(f"Stored snapshot at {timestamp}")
def get_node_risk_score(self, node_id: str) -> float:
if node_id not in self.nodes:
return 0.5
rv = self.nodes[node_id].risk_vector
return float(np.mean(rv[:5]))
def export_graph_state(self, output_file: str):
graph_state = {
"timestamp": datetime.utcnow().isoformat(),
"nodes": [
{
"node_id": node_id,
"latitude": node.latitude,
"longitude": node.longitude,
"risk_vector": node.risk_vector.tolist(),
"overall_risk": self.get_node_risk_score(node_id)
}
for node_id, node in self.nodes.items()
],
"edges": [
{
"source": edge.source,
"target": edge.target,
"distance_km": edge.distance_km,
"trade_volume": edge.trade_volume
}
for edge in self.edges
]
}
with open(output_file, 'w') as f:
json.dump(graph_state, f, indent=2)
print(f"Exported graph state to {output_file}")
def get_historical_trends(self, node_id: str, limit: int = 10) -> List[Dict]:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("""
SELECT timestamp, gscpi_risk, news_risk, political_risk,
trade_risk, weather_risk, overall_risk
FROM risk_history
WHERE node_id = ?
ORDER BY timestamp DESC
LIMIT ?
""", (node_id, limit))
rows = cursor.fetchall()
conn.close()
history = []
for row in rows:
history.append({
"timestamp": row[0],
"gscpi_risk": row[1],
"news_risk": row[2],
"political_risk": row[3],
"trade_risk": row[4],
"weather_risk": row[5],
"overall_risk": row[6]
})
return history
if __name__ == "__main__":
print("Run api_server.py to execute the live graph pipeline.")