-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_defense.py
More file actions
executable file
·381 lines (309 loc) · 16.4 KB
/
Copy pathplot_defense.py
File metadata and controls
executable file
·381 lines (309 loc) · 16.4 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env python3
"""
IEEE-Style Defense Visualization for FL Free-Rider Detection.
Reads results/defense_history.csv and generates three publication-quality charts:
Chart 1 — Defense Separation Scatter (positive_sum × variance)
Chart 2 — Fingerprint Bar Chart (variance comparison)
Chart 3 — Convergence Curves (Macro-F1 over rounds)
All figures are saved as 300 DPI PDFs in results/plots/.
"""
from __future__ import annotations
import os
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from matplotlib.patches import Patch
import seaborn as sns
# ═══════════════════════════════════════════════════════════════════════════════
# IEEE Style Setup
# ═══════════════════════════════════════════════════════════════════════════════
IEEE_COLORS = {
"honest": "#2166AC", # deep blue
"attacker": "#B2182B", # deep red
"baseline": "#4D4D4D", # dark gray
"defense": "#1B7837", # dark green
"threshold": "#B2182B", # red dashed
"honest_edge": "#053061",
"attacker_edge": "#67001F",
}
plt.rcParams.update({
"font.family": "serif",
"font.serif": ["Times New Roman", "DejaVu Serif"],
"font.size": 10,
"axes.titlesize": 12,
"axes.labelsize": 11,
"xtick.labelsize": 9,
"ytick.labelsize": 9,
"legend.fontsize": 8,
"figure.dpi": 150,
"savefig.dpi": 300,
"savefig.bbox": "tight",
"savefig.format": "pdf",
"text.usetex": False,
"axes.spines.top": False,
"axes.spines.right": False,
})
OUTPUT_DIR = "results/plots"
os.makedirs(OUTPUT_DIR, exist_ok=True)
def _save(fig, name: str):
path = os.path.join(OUTPUT_DIR, f"{name}.pdf")
fig.savefig(path, dpi=300, bbox_inches="tight")
path_png = os.path.join(OUTPUT_DIR, f"{name}.png")
fig.savefig(path_png, dpi=300, bbox_inches="tight")
print(f" Saved: {path}")
plt.close(fig)
# ═══════════════════════════════════════════════════════════════════════════════
# Chart 1 — Defense Separation Scatter Plot
# ═══════════════════════════════════════════════════════════════════════════════
def chart_scatter(
df: pd.DataFrame,
pos_threshold: float = 0.01,
var_threshold: float = 0.001,
):
"""Scatter: positive_sum (X) vs variance (Y), colored by attacker status.
Two dashed threshold lines mark the defense decision boundaries.
"""
fig, ax = plt.subplots(figsize=(7, 5.5))
honest = df[~df["is_attacker"]]
attackers = df[df["is_attacker"]]
# Scatter
ax.scatter(
honest["positive_sum"], honest["variance"],
c=IEEE_COLORS["honest"], s=30, marker="o", alpha=0.7,
edgecolors=IEEE_COLORS["honest_edge"], linewidth=0.3,
label="Honest Clients", zorder=3,
)
ax.scatter(
attackers["positive_sum"], attackers["variance"],
c=IEEE_COLORS["attacker"], s=40, marker="^", alpha=0.8,
edgecolors=IEEE_COLORS["attacker_edge"], linewidth=0.3,
label="Free-Rider Attackers", zorder=3,
)
# Threshold lines
ax.axvline(x=pos_threshold, color=IEEE_COLORS["threshold"],
linestyle="--", linewidth=1.2, alpha=0.7,
label=f"Pos-Sum Threshold = {pos_threshold}")
ax.axhline(y=var_threshold, color=IEEE_COLORS["threshold"],
linestyle=":", linewidth=1.2, alpha=0.7,
label=f"Variance Threshold = {var_threshold}")
# Shaded defense zones
ax.axvspan(0, pos_threshold, alpha=0.06, color="red", zorder=0)
ax.axhspan(0, var_threshold, xmin=0, xmax=pos_threshold / (ax.get_xlim()[1] or 1),
alpha=0.08, color="orange", zorder=0)
ax.set_xlabel("Positive Per-Class SV Sum $\\Sigma_c \\max(SV_{i,c}, 0)$")
ax.set_ylabel("Per-Class SV Variance $\\mathrm{Var}_c(SV_{i,c})$")
ax.set_title("Defense Separation: Honest vs Free-Rider Clients")
ax.legend(loc="upper right", framealpha=0.9, edgecolor="gray", fontsize=7.5)
ax.grid(True, alpha=0.2)
# Annotation
ax.text(0.98, 0.06,
"Shaded region = Defense detection zone\n"
"(low positive sum & low variance → suspected free-rider)",
transform=ax.transAxes, ha="right", va="bottom",
fontsize=7, fontstyle="italic", color="gray",
bbox=dict(boxstyle="round,pad=0.3", facecolor="white", alpha=0.8))
fig.tight_layout()
_save(fig, "fig1_defense_scatter")
# ═══════════════════════════════════════════════════════════════════════════════
# Chart 2 — Variance Fingerprint Bar Chart
# ═══════════════════════════════════════════════════════════════════════════════
def chart_fingerprint(df: pd.DataFrame, var_threshold: float = 0.001):
"""Grouped bar chart: variance distribution of typical honest vs attacker clients."""
fig, ax = plt.subplots(figsize=(8, 5))
# Aggregate: mean variance per client across all rounds
client_stats = df.groupby(["client_id", "is_attacker"])["variance"].mean().reset_index()
client_stats = client_stats.sort_values("variance", ascending=False)
honest_stats = client_stats[~client_stats["is_attacker"]]
attacker_stats = client_stats[client_stats["is_attacker"]]
# Plot bars
x_h = np.arange(len(honest_stats))
x_a = np.arange(len(attacker_stats)) + len(honest_stats) + 1 # gap between groups
bars_h = ax.bar(
x_h, honest_stats["variance"].values,
color=IEEE_COLORS["honest"], alpha=0.85, edgecolor=IEEE_COLORS["honest_edge"],
linewidth=0.5, label="Honest Clients",
)
bars_a = ax.bar(
x_a, attacker_stats["variance"].values,
color=IEEE_COLORS["attacker"], alpha=0.85, edgecolor=IEEE_COLORS["attacker_edge"],
linewidth=0.5, label="Free-Rider Attackers",
)
# Threshold line
ax.axhline(y=var_threshold, color=IEEE_COLORS["threshold"],
linestyle="--", linewidth=1.2, alpha=0.7,
label=f"Detection Threshold = {var_threshold}")
# Labels
all_labels = [f"C{c}" for c in honest_stats["client_id"].values] + \
[f"A{c}" for c in attacker_stats["client_id"].values]
all_x = np.concatenate([x_h, x_a])
ax.set_xticks(all_x)
ax.set_xticklabels(all_labels, rotation=45, fontsize=8)
ax.set_ylabel("Mean Per-Class SV Variance $\\mathrm{Var}_c(SV_{i,c})$")
ax.set_xlabel("Client ID (C = Honest, A = Attacker)")
ax.set_title("Variance Fingerprint: Honest vs Free-Rider Clients")
ax.legend(loc="upper right", framealpha=0.9, fontsize=8)
ax.grid(True, axis="y", alpha=0.2)
# Annotation
ax.text(0.98, 0.95,
"Honest clients exhibit high variance\n(uneven class contributions).\n"
"Free-riders show near-zero variance\n(flat, uninformative SV profile).",
transform=ax.transAxes, ha="right", va="top",
fontsize=7, fontstyle="italic", color="gray",
bbox=dict(boxstyle="round,pad=0.3", facecolor="white", alpha=0.8))
fig.tight_layout()
_save(fig, "fig2_variance_fingerprint")
# ═══════════════════════════════════════════════════════════════════════════════
# Chart 3 — Convergence: Baseline vs Defense
# ═══════════════════════════════════════════════════════════════════════════════
def chart_convergence(baseline_f1s: list[float], defense_f1s: list[float]):
"""Convergence curve: Macro-F1 over rounds, baseline vs with defense."""
fig, ax = plt.subplots(figsize=(7, 5))
rounds = np.arange(1, len(baseline_f1s) + 1)
ax.plot(rounds, baseline_f1s,
color=IEEE_COLORS["baseline"], linewidth=1.6,
linestyle="--", dashes=(4, 2),
label="FedAvg Baseline (No Defense)")
ax.plot(rounds, defense_f1s,
color=IEEE_COLORS["defense"], linewidth=2.0,
linestyle="-",
label="FedAvg + DefenseController (Ours)")
# Highlight the gap at the end
final_b = baseline_f1s[-1]
final_d = defense_f1s[-1]
ax.annotate(
f"$\\Delta$ = {final_d - final_b:+.3f}",
xy=(len(rounds), final_d),
xytext=(len(rounds) - 5, final_d + 0.02),
fontsize=9, fontweight="bold",
color=IEEE_COLORS["defense"],
arrowprops=dict(arrowstyle="->", color=IEEE_COLORS["defense"], lw=1.2),
)
ax.set_xlabel("Communication Round")
ax.set_ylabel("Macro-F1 Score")
ax.set_title("Model Convergence: Baseline vs Defense-Enhanced FedAvg")
ax.legend(loc="lower right", framealpha=0.9, fontsize=8.5)
ax.grid(True, alpha=0.2)
# Inset: zoom on the last 10 rounds
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
ax_inset = inset_axes(ax, width="40%", height="35%", loc="center right",
bbox_to_anchor=(-0.02, -0.02, 1, 1),
bbox_transform=ax.transAxes)
last_n = min(10, len(rounds))
ax_inset.plot(rounds[-last_n:], baseline_f1s[-last_n:],
color=IEEE_COLORS["baseline"], linewidth=1.4, linestyle="--")
ax_inset.plot(rounds[-last_n:], defense_f1s[-last_n:],
color=IEEE_COLORS["defense"], linewidth=1.6)
ax_inset.set_title(f"Last {last_n} Rounds (Zoom)", fontsize=7)
ax_inset.tick_params(labelsize=6)
ax_inset.grid(True, alpha=0.2)
fig.tight_layout()
_save(fig, "fig3_convergence")
# ═══════════════════════════════════════════════════════════════════════════════
# Chart 4 — Fancy: Defense Phase Classification (Sankey-style stacked bar)
# ═══════════════════════════════════════════════════════════════════════════════
def chart_defense_phases(df: pd.DataFrame):
"""Show how many clients pass each defense phase per round."""
fig, ax = plt.subplots(figsize=(8, 5))
rounds = sorted(df["round"].unique())
phase_counts = []
for r in rounds:
rdf = df[df["round"] == r]
phase_counts.append({
"round": r,
"clean": (rdf["phase"] == "none").sum(),
"phase1": (rdf["phase"] == "phase1_pos_sum").sum(),
"phase2": (rdf["phase"] == "phase2_variance").sum(),
})
pc = pd.DataFrame(phase_counts)
x = np.arange(len(rounds))
w = 0.6
ax.bar(x, pc["clean"], w, color="#4393C3", alpha=0.85, label="Passed Both Phases (Clean)")
ax.bar(x, pc["phase2"], w, bottom=pc["clean"], color="#F4A582", alpha=0.85,
label="Flagged: Phase 2 (Low Variance)")
ax.bar(x, pc["phase1"], w, bottom=pc["clean"] + pc["phase2"], color="#CA0020", alpha=0.85,
label="Flagged: Phase 1 (Low Pos-Sum)")
ax.set_xlabel("Communication Round")
ax.set_ylabel("Number of Clients")
ax.set_title("Defense Phase Classification per Round")
ax.set_xticks(x[::5])
ax.set_xticklabels(rounds[::5])
ax.legend(loc="upper right", framealpha=0.9, fontsize=7.5)
ax.grid(True, axis="y", alpha=0.2)
fig.tight_layout()
_save(fig, "fig4_defense_phases")
# ═══════════════════════════════════════════════════════════════════════════════
# Chart 5 — Fancy: 3D-style Defense Decision Boundary
# ═══════════════════════════════════════════════════════════════════════════════
def chart_decision_contour(df: pd.DataFrame,
pos_threshold: float = 0.01,
var_threshold: float = 0.001):
"""Density contour showing the defense decision boundary."""
fig, ax = plt.subplots(figsize=(7, 5.5))
honest = df[~df["is_attacker"]]
attackers = df[df["is_attacker"]]
# KDE contours for honest clients
if len(honest) > 5:
sns.kdeplot(
data=honest, x="positive_sum", y="variance",
ax=ax, levels=5, color=IEEE_COLORS["honest"], alpha=0.4,
linewidths=0.8, label="Honest Density",
)
# KDE contours for attackers
if len(attackers) > 5:
sns.kdeplot(
data=attackers, x="positive_sum", y="variance",
ax=ax, levels=3, color=IEEE_COLORS["attacker"], alpha=0.5,
linewidths=0.8, linestyles="--", label="Attacker Density",
)
# Scatter overlay
ax.scatter(honest["positive_sum"], honest["variance"],
c=IEEE_COLORS["honest"], s=15, alpha=0.5, zorder=3)
ax.scatter(attackers["positive_sum"], attackers["variance"],
c=IEEE_COLORS["attacker"], s=20, marker="^", alpha=0.6, zorder=3)
# Threshold lines
ax.axvline(pos_threshold, color="black", linestyle="--", linewidth=1.0, alpha=0.6)
ax.axhline(var_threshold, color="black", linestyle="--", linewidth=1.0, alpha=0.6)
# Quadrant labels
ax.text(pos_threshold * 0.5, var_threshold * 0.5,
"REMOVED\n(Both low)", fontsize=6, ha="center", va="center",
color="darkred", fontstyle="italic")
ax.text(pos_threshold * 0.5, var_threshold * 5,
"Phase 1\n(No positive\ncontribution)", fontsize=6, ha="center", va="center",
color="darkred", fontstyle="italic")
ax.set_xlabel("Positive Per-Class SV Sum")
ax.set_ylabel("Per-Class SV Variance")
ax.set_title("Defense Decision Boundary: Density Contours")
ax.legend(fontsize=7.5)
fig.tight_layout()
_save(fig, "fig5_decision_contour")
# ═══════════════════════════════════════════════════════════════════════════════
# Main
# ═══════════════════════════════════════════════════════════════════════════════
def main():
csv_path = "results/defense_history.csv"
if not os.path.exists(csv_path):
print(f"ERROR: {csv_path} not found. Run main.py first to generate data.")
return
df = pd.read_csv(csv_path)
print(f"Loaded {len(df)} records from {csv_path}")
print(f" Rounds: {df['round'].nunique()}")
print(f" Clients: {df['client_id'].nunique()}")
print(f" Attackers detected: {df[df['is_attacker']]['suspected'].sum()} / {df[df['is_attacker']].shape[0]}")
thresholds = {
"pos_threshold": df["positive_sum"].quantile(0.05) if len(df) > 0 else 0.01,
"var_threshold": df["variance"].quantile(0.05) if len(df) > 0 else 0.001,
}
print(f" Auto-detected thresholds: {thresholds}")
print("\nGenerating charts...")
chart_scatter(df, **thresholds)
chart_fingerprint(df, thresholds["var_threshold"])
chart_defense_phases(df)
chart_decision_contour(df, **thresholds)
# Convergence chart needs separate data; placeholder message
print("\n Note: fig3_convergence requires F1 arrays from main.py.")
print(" To generate, save baseline_f1s and defense_f1s as JSON and pass here.")
print(f"\nAll charts saved to {OUTPUT_DIR}/")
if __name__ == "__main__":
main()