From 8f63cf9f41bbdb8d55cd4679872d2b4ae2129324 Mon Sep 17 00:00:00 2001 From: YurenHao0426 Date: Tue, 24 Feb 2026 08:40:49 +0000 Subject: EC-SBM community detection analysis: full pipeline and writeup Implement community detection on 3 EC-SBM networks (polblogs, topology, internet_as) using 5 methods (Leiden-Mod, Leiden-CPM at 0.1 and 0.01, Infomap, graph-tool SBM). Compute AMI/ARI/NMI accuracy, cluster statistics, and generate figures and LaTeX report. --- scripts/load_data.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 scripts/load_data.py (limited to 'scripts/load_data.py') diff --git a/scripts/load_data.py b/scripts/load_data.py new file mode 100644 index 0000000..5a0362b --- /dev/null +++ b/scripts/load_data.py @@ -0,0 +1,74 @@ +"""Shared data loading utilities for EC-SBM analysis.""" + +import pandas as pd +import numpy as np + + +def load_edge_list(path): + """Load a tab-separated edge list (no header, two columns: node1, node2).""" + df = pd.read_csv(path, sep="\t", header=None, names=["src", "tgt"], + dtype=str, comment="#") + return df + + +def load_communities(path): + """Load a tab-separated community file (no header: node, community). + Returns dict {node_str: community_str}.""" + node2com = {} + with open(path, "r") as f: + for line in f: + line = line.strip() + if not line or line.startswith("#"): + continue + parts = line.split("\t") + if len(parts) >= 2: + node2com[parts[0]] = parts[1] + return node2com + + +def build_igraph(edge_df): + """Build an igraph Graph from an edge DataFrame. + Returns (graph, name_to_idx, idx_to_name).""" + import igraph as ig + + all_nodes = pd.unique(edge_df[["src", "tgt"]].values.ravel("K")) + name_to_idx = {name: i for i, name in enumerate(all_nodes)} + idx_to_name = {i: name for name, i in name_to_idx.items()} + + src_ids = edge_df["src"].map(name_to_idx).values + tgt_ids = edge_df["tgt"].map(name_to_idx).values + + n = len(all_nodes) + g = ig.Graph(n=n, edges=list(zip(src_ids, tgt_ids)), directed=False) + g.simplify() # remove multi-edges and self-loops + return g, name_to_idx, idx_to_name + + +def build_graphtool_graph(edge_df): + """Build a graph-tool Graph from an edge DataFrame. + Returns (graph, name_to_idx, idx_to_name).""" + import graph_tool.all as gt + + all_nodes = pd.unique(edge_df[["src", "tgt"]].values.ravel("K")) + name_to_idx = {name: i for i, name in enumerate(all_nodes)} + idx_to_name = {i: name for name, i in name_to_idx.items()} + + src_ids = edge_df["src"].map(name_to_idx).values.astype(np.int64) + tgt_ids = edge_df["tgt"].map(name_to_idx).values.astype(np.int64) + + n = len(all_nodes) + g = gt.Graph(directed=False) + g.add_vertex(n) + g.add_edge_list(np.column_stack([src_ids, tgt_ids])) + gt.remove_parallel_edges(g) + gt.remove_self_loops(g) + return g, name_to_idx, idx_to_name + + +def save_communities(node2com, path): + """Save community assignments as TSV (node\tcommunity).""" + import os + os.makedirs(os.path.dirname(path), exist_ok=True) + with open(path, "w") as f: + for node, com in sorted(node2com.items(), key=lambda x: x[0]): + f.write(f"{node}\t{com}\n") -- cgit v1.2.3