import duckdb import torch from torch_geometric.data import Data from torch_geometric.data.data import DataEdgeAttr, DataTensorAttr from torch_geometric.data.storage import BaseStorage, EdgeStorage, GlobalStorage from torch_geometric.utils import is_undirected from paths import DB_PATH, GRAPH_PATH PATH = GRAPH_PATH # torch>=2.6 defaults to weights_only=True; allowlist PyG's classes to keep it on. torch.serialization.add_safe_globals( [Data, DataEdgeAttr, DataTensorAttr, BaseStorage, EdgeStorage, GlobalStorage] ) data = torch.load(PATH, weights_only=True) print(data) print("validate():", data.validate()) # is_undirected with edge_attr also requires the attrs to match on reversed pairs print("is_undirected (attr-aware):", is_undirected(data.edge_index, data.edge_attr, num_nodes=data.num_nodes)) # Round-trip three edges back against the source table. con = duckdb.connect(DB_PATH, read_only=True) print("\nround-trip check (saved value vs. table value):") for i in (0, 1_000_000, 5_418_417): s, t = data.edge_index[:, i].tolist() a, b = data.node_id[s], data.node_id[t] saved = data.edge_attr[i].tolist() row = con.execute( """SELECT OddsRatio_log2, ChiTestsPValue FROM edge_ML_expected_ge5 WHERE (start_id = ? AND end_id = ?) OR (start_id = ? AND end_id = ?)""", [a, b, b, a], ).fetchone() print(f" col {i}: {a} <-> {b}") print(f" saved={saved}") print(f" table={list(row)} match={saved == list(row)}")