-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvisualise.py
More file actions
118 lines (89 loc) · 2.63 KB
/
visualise.py
File metadata and controls
118 lines (89 loc) · 2.63 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
import networkx as nx
from scipy.spatial import ConvexHull
import glob
#Load edges
edges_df = pd.read_csv("edges.csv")
edges = list(zip(edges_df.u, edges_df.v))
#Load snapshots
files = sorted(
glob.glob("nodes_step_*.csv"),
key=lambda x: int(x.split("_")[-1].split(".")[0])
)
snapshots = [pd.read_csv(f) for f in files]
steps = len(snapshots)
N = len(snapshots[0])
G = nx.Graph()
G.add_nodes_from(range(N))
G.add_edges_from(edges)
pos = {}
for _, row in snapshots[0].iterrows():
pos[row.id] = (row.x, row.y)
fig, ax = plt.subplots(figsize=(10, 8))
plt.axis('off')
def frame(t):
ax.clear()
ax.set_title(f"Opinion Graph — Step {t}", fontsize=14)
ax.axis('off')
snap = snapshots[t]
colors = []
sizes = []
for _, row in snap.iterrows():
op = row.opinion
# Node color by polarity
if op > 0.05:
colors.append((1, 0.4, 0.4)) #red
elif op < -0.05:
colors.append((0.4, 0.4, 1)) #blue
else:
colors.append((0.5, 0.5, 0.5)) #gray
sizes.append(50 + 300 * abs(op))
nx.draw_networkx_edges(G, pos, alpha=0.1, width=0.4, ax=ax)
nx.draw_networkx_nodes(G, pos, node_color=colors, node_size=sizes, ax=ax)
ani = animation.FuncAnimation(
fig, frame, frames=steps, interval=50, repeat=False
)
plt.show()
ghetto_df = pd.read_csv("ghettos.csv") #ghetto_id, node
final_snap = snapshots[-1]
ghettos = {}
for _, row in ghetto_df.iterrows():
gid = row.ghetto_id
ghettos.setdefault(gid, []).append(row.node)
plt.figure(figsize=(10, 8))
plt.title("Ideological Communities")
colors = []
sizes = []
for _, row in final_snap.iterrows():
op = row.opinion
if op > 0.01:
colors.append((1, 0.4, 0.4))
elif op < -0.01:
colors.append((0.4, 0.4, 1))
else:
colors.append((0.5, 0.5, 0.5))
sizes.append(50 + 300*abs(op))
nx.draw_networkx_edges(G, pos, alpha=0.1)
nx.draw_networkx_nodes(G, pos, node_color=colors, node_size=sizes)
for gid, nodes_list in ghettos.items():
pts = np.array([pos[n] for n in nodes_list])
if len(pts) < 3:
continue
try:
hull = ConvexHull(pts)
hull_pts = pts[hull.vertices]
except:
hull_pts = pts
avg_opinion = np.mean([final_snap.loc[n].opinion for n in nodes_list])
if avg_opinion > 0.05:
blob = (1, 0.3, 0.3, 0.25)
elif avg_opinion < -0.05:
blob = (0.3, 0.3, 1, 0.25)
else:
blob = (0.7, 0.7, 0.7, 0.25)
plt.fill(hull_pts[:, 0], hull_pts[:, 1], color=blob)
plt.axis('off')
plt.show()