forked from YichengDuan/topog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_stats.py
More file actions
66 lines (60 loc) · 1.91 KB
/
Copy pathgraph_stats.py
File metadata and controls
66 lines (60 loc) · 1.91 KB
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
#!/usr/bin/env python3
import os
import glob
import networkx as nx
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Directory paths
raw_dir = 'datasets/raw'
result_dir = './results'
os.makedirs(result_dir, exist_ok=True)
# Collect all graph file paths (support both .gml and .graphml)
file_paths = glob.glob(os.path.join(raw_dir, '*.gml')) + glob.glob(os.path.join(raw_dir, '*.graphml'))
# Initialize metric lists
metrics = {
'num_nodes': [],
'num_edges': [],
'density': [],
'avg_clustering': [],
'transitivity': []
}
# Process each graph
for path in file_paths:
try:
G = nx.read_graphml(path)
except:
G = nx.read_gml(path)
metrics['num_nodes'].append(G.number_of_nodes())
metrics['num_edges'].append(G.number_of_edges())
metrics['density'].append(nx.density(G))
metrics['avg_clustering'].append(nx.average_clustering(G))
metrics['transitivity'].append(nx.transitivity(G))
# Check if any graphs were found
if not metrics['num_nodes']:
print(f"No graph files found in {raw_dir}")
else:
# Compute summary statistics
summary = []
for name, vals in metrics.items():
arr = np.array(vals)
summary.append({
'metric': name,
'min': float(arr.min()),
'max': float(arr.max()),
'mean': float(arr.mean()),
'std': float(arr.std())
})
summary_df = pd.DataFrame(summary)
print(summary_df)
# Plot histograms for each metric
for name, vals in metrics.items():
plt.figure(figsize=(6, 4))
plt.hist(vals, bins=20, edgecolor='black')
plt.xlabel(name.replace('_', ' ').title())
plt.ylabel('Frequency')
plt.title(f"Distribution of {name.replace('_', ' ').title()}")
plt.tight_layout()
plt.savefig(os.path.join(result_dir, f"{name}_hist.png"))
plt.close()
print(f"Histograms saved to {result_dir}")