summaryrefslogtreecommitdiff
path: root/scripts/compute_accuracy.py
blob: 4aeb6a218b63fcf5b806cf1546e4b818e69db4b4 (plain)
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
"""Compute AMI, ARI, NMI for all (network, method) pairs."""

import argparse
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

import numpy as np
import pandas as pd
from sklearn.metrics import adjusted_mutual_info_score, adjusted_rand_score, normalized_mutual_info_score

from config import NETWORKS, METHODS, RESULTS_DIR
from load_data import load_edge_list, load_communities


def align_labels(gt_com, est_com, edge_path):
    """Align ground truth and estimated labels over the full node set from edges.
    Nodes missing from a clustering get unique singleton community IDs."""
    edge_df = load_edge_list(edge_path)
    all_nodes = sorted(set(
        pd.unique(edge_df[["src", "tgt"]].values.ravel("K"))
    ))

    gt_labels = []
    est_labels = []
    # For nodes not in GT or EST, assign unique singleton IDs
    gt_next = max((int(v) for v in gt_com.values() if v.lstrip('-').isdigit()), default=0) + 1
    est_next = max((int(v) for v in est_com.values() if v.lstrip('-').isdigit()), default=0) + 1

    for node in all_nodes:
        if node in gt_com:
            gt_labels.append(gt_com[node])
        else:
            gt_labels.append(f"gt_singleton_{gt_next}")
            gt_next += 1

        if node in est_com:
            est_labels.append(est_com[node])
        else:
            est_labels.append(f"est_singleton_{est_next}")
            est_next += 1

    return gt_labels, est_labels


def compute_accuracy(network_name, method_name):
    """Compute AMI, ARI, NMI for a single (network, method) pair."""
    net = NETWORKS[network_name]
    gt_com = load_communities(net["com_gt_tsv"])

    est_path = os.path.join(RESULTS_DIR, network_name, method_name, "com.tsv")
    if not os.path.exists(est_path):
        print(f"  WARNING: {est_path} not found, skipping")
        return None

    est_com = load_communities(est_path)
    gt_labels, est_labels = align_labels(gt_com, est_com, net["edge_tsv"])

    ami = adjusted_mutual_info_score(gt_labels, est_labels, average_method="arithmetic")
    ari = adjusted_rand_score(gt_labels, est_labels)
    nmi = normalized_mutual_info_score(gt_labels, est_labels, average_method="arithmetic")

    return {"ami": ami, "ari": ari, "nmi": nmi}


def compute_all_accuracy():
    """Compute accuracy for all (network, method) pairs and save CSV."""
    rows = []
    for net_name in NETWORKS:
        for method in METHODS:
            m_name = method["name"]
            print(f"Computing accuracy: {net_name} / {m_name}")
            result = compute_accuracy(net_name, m_name)
            if result is not None:
                rows.append({
                    "network": net_name,
                    "method": m_name,
                    **result,
                })

    df = pd.DataFrame(rows)
    out_dir = os.path.join(RESULTS_DIR, "accuracy")
    os.makedirs(out_dir, exist_ok=True)
    out_path = os.path.join(out_dir, "accuracy_table.csv")
    df.to_csv(out_path, index=False)
    print(f"\nAccuracy table saved to {out_path}")
    print(df.to_string(index=False))
    return df


if __name__ == "__main__":
    compute_all_accuracy()