|
| 1 | +import numpy as np, time, tracemalloc, sys, gc |
| 2 | +from pycleora import SparseMatrix, embed |
| 3 | +from pycleora.algorithms import embed_prone, embed_randne |
| 4 | +from pycleora.classify import mlp_classify |
| 5 | +from pycleora.metrics import node_classification_scores |
| 6 | +from pycleora.community import detect_communities_louvain |
| 7 | +from pycleora.datasets import load_dataset |
| 8 | +DIM = 256 |
| 9 | + |
| 10 | +def measure(fn, g): |
| 11 | + gc.collect(); tracemalloc.start() |
| 12 | + t0 = time.time(); r = fn(g); t = time.time() - t0 |
| 13 | + _, peak = tracemalloc.get_traced_memory(); tracemalloc.stop() |
| 14 | + return r, t, peak/1024/1024 |
| 15 | + |
| 16 | +def run(name, fn, g, lb): |
| 17 | + emb, t, mem = measure(fn, g) |
| 18 | + nc = node_classification_scores(g, emb, lb, seed=42) |
| 19 | + mlp = mlp_classify(g, emb, lb, hidden_dim=128, num_epochs=200, learning_rate=0.01, seed=42) |
| 20 | + print(f"{name:<18s} {nc['accuracy']:>8.4f} {nc['macro_f1']:>8.4f} {mlp['accuracy']:>8.4f} {mlp['macro_f1']:>8.4f} {t:>7.3f}s {mem:>7.1f}") |
| 21 | + sys.stdout.flush(); gc.collect() |
| 22 | + |
| 23 | +# Facebook - remaining algos |
| 24 | +print("=== FACEBOOK (ProNE, RandNE) ===") |
| 25 | +ds = load_dataset("facebook") |
| 26 | +g = SparseMatrix.from_iterator(iter(ds["edges"]), ds["columns"]) |
| 27 | +lb = detect_communities_louvain(g) |
| 28 | +run("ProNE", lambda g: embed_prone(g, DIM), g, lb) |
| 29 | +run("RandNE", lambda g: embed_randne(g, DIM), g, lb) |
| 30 | + |
| 31 | +# PPI-large |
| 32 | +print("\n=== PPI-LARGE ===") |
| 33 | +ds = load_dataset("ppi_large") |
| 34 | +g = SparseMatrix.from_iterator(iter(ds["edges"]), ds["columns"]) |
| 35 | +lb = detect_communities_louvain(g) |
| 36 | +print(f"Nodes: {ds['num_nodes']}, Labels: {len(lb)}") |
| 37 | +print(f"{'Algo':<18s} {'NC_Acc':>8s} {'NC_F1':>8s} {'MLP_Acc':>8s} {'MLP_F1':>8s} {'Time':>8s} {'Mem':>8s}") |
| 38 | +run("Cleora(w,8it)", lambda g: embed(g, DIM, 8, whiten=True), g, lb) |
| 39 | +run("Cleora(base)", lambda g: embed(g, DIM, 4), g, lb) |
| 40 | +run("ProNE", lambda g: embed_prone(g, DIM), g, lb) |
| 41 | +run("RandNE", lambda g: embed_randne(g, DIM), g, lb) |
| 42 | + |
| 43 | +# Flickr |
| 44 | +print("\n=== FLICKR ===") |
| 45 | +ds = load_dataset("flickr") |
| 46 | +g = SparseMatrix.from_iterator(iter(ds["edges"]), ds["columns"]) |
| 47 | +lb = detect_communities_louvain(g) |
| 48 | +print(f"Nodes: {ds['num_nodes']}, Labels: {len(lb)}") |
| 49 | +print(f"{'Algo':<18s} {'NC_Acc':>8s} {'NC_F1':>8s} {'MLP_Acc':>8s} {'MLP_F1':>8s} {'Time':>8s} {'Mem':>8s}") |
| 50 | +run("Cleora(w,8it)", lambda g: embed(g, DIM, 8, whiten=True), g, lb) |
| 51 | +run("Cleora(base)", lambda g: embed(g, DIM, 4), g, lb) |
| 52 | +run("ProNE", lambda g: embed_prone(g, DIM), g, lb) |
| 53 | +run("RandNE", lambda g: embed_randne(g, DIM), g, lb) |
| 54 | + |
| 55 | +print("\nDONE!") |
0 commit comments