NOTEBOOK: leakage_stat.ipynb
SHA256: 702bbb264ec367db57dfb5be8c10ef67a099af93c67aef2a9d7e85645c5bf8a8
STATIC VIEW ONLY: no cells executed.

CELL 0, type=code, execution_count=1
import torch
from torch_geometric.utils.convert import to_networkx
import networkx as nx
import itertools
from tqdm import tqdm
import json

CELL 1, type=code, execution_count=2
stats = {}
datasets = ["Linux", "IMDB", "AIDS_label", "AIDS"]

for dataset in datasets:   
    print(dataset)
    stats[dataset] = {}
    overall_nx_graphs = []
    splits = ['train', 'val', 'test']
    
    train_graphs = torch.load(f'./data/leakage_data/{dataset}/train_graphs.pt')
    val_graphs = torch.load(f'./data/leakage_data/{dataset}/val_graphs.pt')
    test_graphs = torch.load(f'./data/leakage_data/{dataset}/test_graphs.pt')
    overall_nx_graphs = train_graphs + val_graphs + test_graphs
    stats[dataset]["num_graph"] = len(overall_nx_graphs)
    
    train_hashes = []
    for nx_graph in train_graphs:
        g_hash = nx.algorithms.graph_hashing.weisfeiler_lehman_graph_hash(nx_graph, node_attr="x" if "label" in dataset else None)
        train_hashes.append(g_hash)
    
    val_hashes = []
    for nx_graph in val_graphs:
        g_hash = nx.algorithms.graph_hashing.weisfeiler_lehman_graph_hash(nx_graph, node_attr="x" if "label" in dataset else None)
        val_hashes.append(g_hash)
    
    test_hashes = []
    for nx_graph in test_graphs:
        g_hash = nx.algorithms.graph_hashing.weisfeiler_lehman_graph_hash(nx_graph, node_attr="x" if "label" in dataset else None)
        test_hashes.append(g_hash)
    
    stats[dataset]["num_unique_graph"] = len(set(train_hashes + val_hashes + test_hashes))
    stats[dataset]["reduction_percent"] = 100 * (1- float(stats[dataset]["num_unique_graph"]) / stats[dataset]["num_graph"])
    # Train Graph pair hashes
    query_indices, target_indices = list(zip(*list(itertools.combinations_with_replacement(range(len(train_graphs)), 2))))
    train_graph_pair_hashes = set()
    print("Calculating train graph pair hashes")
    for i, j in zip(query_indices, target_indices):
        train_graph_pair_hashes.add(hash((train_hashes[i], train_hashes[j])))
    
    # Test scheme 1 Nc2
    query_indices, target_indices = list(zip(*list(itertools.combinations_with_replacement(range(len(test_graphs)), 2))))
    test_leak_count = 0
    print("Calculating test graph pair hashes (1)")
    for i, j in zip(query_indices, target_indices):
        t_hash = hash((test_hashes[i], test_hashes[j]))
        if t_hash in train_graph_pair_hashes:
            test_leak_count += 1
    stats[dataset]["num_test_pairs_1"] = len(query_indices)
    stats[dataset]["leaked_pairs_1"] = test_leak_count
    stats[dataset]["leak_percent_1"] = 100 * float(test_leak_count) / len(query_indices)
    
    # Test scheme 2 test_graphs x train_graphs
    query_indices, target_indices = list(zip(*list(itertools.product(range(len(test_graphs)), range(len(train_graphs))))))
    test_graph_pair_hashes = set()
    test_leak_count_2 = 0
    print("Calculating test graph pair hashes (2)")
    for i, j in zip(query_indices, target_indices):
        t_hash = hash((test_hashes[i], train_hashes[j]))
        if t_hash in train_graph_pair_hashes:
            test_leak_count_2 += 1
    stats[dataset]["num_test_pairs_2"] = len(query_indices)
    stats[dataset]["leaked_pairs_2"] = test_leak_count_2
    stats[dataset]["leak_percent_2"] = 100 * float(test_leak_count_2) / len(query_indices)
OUTPUT 0, type=stream
Linux
OUTPUT 1, type=stream
/tmp/ipykernel_223831/1257549547.py:10: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
  train_graphs = torch.load(f'./data/{dataset}/train_graphs.pt')
/tmp/ipykernel_223831/1257549547.py:11: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
  val_graphs = torch.load(f'./data/{dataset}/val_graphs.pt')
/tmp/ipykernel_223831/1257549547.py:12: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
  test_graphs = torch.load(f'./data/{dataset}/test_graphs.pt')
OUTPUT 2, type=stream
Calculating train graph pair hashes
Calculating test graph pair hashes (1)
Calculating test graph pair hashes (2)
IMDB
Calculating train graph pair hashes
Calculating test graph pair hashes (1)
Calculating test graph pair hashes (2)
AIDS_label
Calculating train graph pair hashes
Calculating test graph pair hashes (1)
Calculating test graph pair hashes (2)
AIDS
Calculating train graph pair hashes
Calculating test graph pair hashes (1)
Calculating test graph pair hashes (2)

CELL 2, type=code, execution_count=3
print(json.dumps(stats, indent=4))
OUTPUT 0, type=stream
{
    "Linux": {
        "num_graph": 1000,
        "num_unique_graph": 98,
        "reduction_percent": 90.2,
        "num_test_pairs_1": 20100,
        "leaked_pairs_1": 18350,
        "leak_percent_1": 91.29353233830845,
        "num_test_pairs_2": 120000,
        "leaked_pairs_2": 113716,
        "leak_percent_2": 94.76333333333334
    },
    "IMDB": {
        "num_graph": 1500,
        "num_unique_graph": 619,
        "reduction_percent": 58.73333333333333,
        "num_test_pairs_1": 45150,
        "leaked_pairs_1": 15382,
        "leak_percent_1": 34.06866002214839,
        "num_test_pairs_2": 270000,
        "leaked_pairs_2": 148107,
        "leak_percent_2": 54.85444444444445
    },
    "AIDS_label": {
        "num_graph": 700,
        "num_unique_graph": 673,
        "reduction_percent": 3.857142857142859,
        "num_test_pairs_1": 9870,
        "leaked_pairs_1": 24,
        "leak_percent_1": 0.24316109422492402,
        "num_test_pairs_2": 58800,
        "leaked_pairs_2": 1721,
        "leak_percent_2": 2.92687074829932
    },
    "AIDS": {
        "num_graph": 700,
        "num_unique_graph": 449,
        "reduction_percent": 35.857142857142854,
        "num_test_pairs_1": 9870,
        "leaked_pairs_1": 2082,
        "leak_percent_1": 21.09422492401216,
        "num_test_pairs_2": 58800,
        "leaked_pairs_2": 23958,
        "leak_percent_2": 40.744897959183675
    }
}

CELL 3, type=markdown, execution_count=None
\begin{table}[t]
    \centering
    \caption{Dataset statistics and test pair leakage analysis.}
    \label{tab:dataset_summary}
    \resizebox{\columnwidth}{!}{%
    \begin{tabular}{l|p{1cm}p{1cm}p{1.5cm}p{1.5cm}}
        \toprule
        &  \textbf{\linux} & \textbf{\imdb} & \textbf{AIDS} & \textbf{AIDS}\\ 
        &  & & (w label) & (w/o label) \\        \midrule
        \multicolumn{5}{c}{\textbf{Overall Dataset}} \\ \hline
        Number of graphs ($|\Dcal|$) &  & & & \\
        Unique graphs ($|\Dcal_{\text{unq}}|$) &  & & & \\
        \% Reduction (\( \downarrow\% \)) & & &  & \\
        \midrule
        \multicolumn{5}{c}{\textbf{Test Scheme 1}} \\\hline
        \# Test pairs  & & & & \\
        \# Leaked pairs & & & & \\
        \% Leakage & & & & \\
        \midrule
        \multicolumn{5}{c}{\textbf{Test Scheme 2}} \\\hline
        \# Test pairs \\
        \# Leaked pairs  & & & & \\
        \% Leakage & & & & \\
        \bottomrule
    \end{tabular}%
    }
\end{table}
