-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6_plot.py
More file actions
657 lines (581 loc) · 18.7 KB
/
6_plot.py
File metadata and controls
657 lines (581 loc) · 18.7 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
# 6_plot.py
"""Plot grouped panels, single-protocol plots, and orthogonality comparisons."""
from __future__ import annotations
import json
import pathlib
import typing
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import numpy as np
import fives_shared as fs
OUTPUT_DIR = pathlib.Path("plot_results")
LEGEND_NCOL = 6
ORTHO_LEGEND_NCOL = 3
# Plot defaults
plt.rcParams["font.family"] = "serif"
plt.rcParams["font.serif"] = ["Garamond"] + plt.rcParams["font.serif"]
plt.rcParams["font.size"] = 12
BASE_FONT_SIZE = float(plt.rcParams.get("font.size", 12))
LEGEND_FONT_SIZE = BASE_FONT_SIZE * 1.5
ORTHO_LEGEND_FONT_SIZE = BASE_FONT_SIZE * 1.0
PerfRecord = dict[str, typing.Any]
SeriesMap = dict[str, list[float]]
PROTOCOL_META: dict[str, tuple[str, str]] = {
"drift": ("Drift", "Illumination Offset"),
"rotation": ("Rotation", "Rotation Angle (Degrees)"),
"gamma": ("Gamma", "Gamma"),
"contrast": ("Contrast", "Contrast Factor"),
"blur": ("Blur", "Gaussian Sigma"),
"gau_noise": ("Gaussian Noise", "Gaussian Sigma"),
"poi_noise": ("Poisson Noise", "Severity"),
"spepper_noise": ("Salt and Pepper", "Severity"),
"bit_depth": ("Bit Depth", "Bit Depth (bits)"),
"resolution": ("Resolution", "Resolution (px)"),
}
BASELINE_LEVELS: dict[str, float] = {
"drift": 0.0,
"rotation": 0.0,
"gamma": 1.0,
"contrast": 1.0,
"blur": 0.0,
"gau_noise": 0.0,
"poi_noise": 0.0,
"spepper_noise": 0.0,
"bit_depth": 8.0,
"resolution": 2048.0,
}
SERIES_ORDER: typing.Sequence[str] = (
"acc_h0",
"acc_h1",
"acc_hs",
"acc_combined",
"acc_hu",
)
SERIES_STYLES: dict[str, dict[str, typing.Any]] = {
"acc_h0": {"label": "H0", "color": "#cc0000"},
"acc_h1": {"label": "H1", "color": "#1c5fd4"},
"acc_hs": {"label": "HS", "color": "#f28c28"},
"acc_combined": {"label": "H0H1HS", "color": "#2e7d32"},
"acc_hu": {"label": "Hu Moments", "color": "#7f7f7f"},
}
LINESTYLE_MAP: dict[str, typing.Any] = {
"acc_h0": "-",
"acc_h1": "-",
"acc_hs": "-",
"acc_combined": "-",
"acc_hu": (0, (2, 2)),
}
PANEL_CONFIG: dict[str, tuple[str, typing.Sequence[str]]] = {
"Mechanical": (
"plot_panel_mechanical.png",
("rotation", "blur", "resolution"),
),
"Radiometric": (
"plot_panel_radiometric.png",
("drift", "gamma", "contrast"),
),
"Failure": (
"plot_panel_failure.png",
("gau_noise", "spepper_noise", "poi_noise"),
),
}
ORTHO_PROTOCOLS: typing.Sequence[str] = ("rotation", "drift")
ORTHO_KEYS: typing.Sequence[str] = (
"acc_h0",
"acc_h1",
"acc_hs",
"acc_combined",
"acc_hu",
)
def load_perf_log(path: pathlib.Path) -> list[PerfRecord]:
"""Load performance records from a JSONL log.
Parameters
----------
path : pathlib.Path
JSONL log path.
Returns
-------
list of dict
Performance records.
"""
if not path.exists():
raise FileNotFoundError(f"Missing perf log: {path}")
records: list[PerfRecord] = []
with path.open("r", encoding="utf-8") as handle:
for line in handle:
line = line.strip()
if not line:
continue
records.append(json.loads(line))
return records
def select_latest_run(records: typing.Sequence[PerfRecord]) -> str:
"""Return the latest run_id.
Parameters
----------
records : typing.Sequence[dict]
Performance records.
Returns
-------
str
Latest run identifier.
"""
run_ids = [record.get("run_id") for record in records if record.get("run_id")]
if not run_ids:
raise ValueError("Missing run_id values in perf log.")
return max(run_ids)
def build_series(
records: typing.Sequence[PerfRecord],
protocol: str,
baseline: typing.Optional[PerfRecord],
baseline_level: typing.Optional[float],
) -> tuple[list[float], SeriesMap, list[str]]:
"""Build the accuracy series for one protocol.
Parameters
----------
records : typing.Sequence[dict]
Performance records for a run.
protocol : str
Protocol name.
baseline : dict or None
Baseline record to inject if missing.
baseline_level : float or None
Baseline level to inject if missing.
Returns
-------
tuple
Levels, series map, and available keys.
"""
proto_records = [
record
for record in records
if record.get("protocol") == protocol and "level" in record
]
if not proto_records:
raise ValueError(f"Missing protocol data for {protocol}.")
available = [key for key in SERIES_ORDER if key in proto_records[0]]
if not available:
raise ValueError("Missing accuracy keys in perf log records.")
series: list[dict[str, float]] = []
for record in proto_records:
point = {"level": float(record["level"])}
for key in available:
if key not in record:
raise ValueError(
"Missing {key} in perf_log; rerun 5_ablate.".format(key=key)
)
point[key] = float(record[key]) * 100.0
series.append(point)
levels_present = {row["level"] for row in series}
if (
baseline is not None
and baseline_level is not None
and baseline_level not in levels_present
):
point = {"level": float(baseline_level)}
for key in available:
if key not in baseline:
raise ValueError(
"Missing {key} in perf_log baseline; rerun 5_ablate.".format(
key=key
)
)
point[key] = float(baseline[key]) * 100.0
series.append(point)
series.sort(key=lambda row: row["level"])
levels = [row["level"] for row in series]
series_map = {key: [row[key] for row in series] for key in available}
return levels, series_map, available
def set_accuracy_limits(
ax: plt.Axes, series: typing.Sequence[list[float]]
) -> None:
"""Set y-axis limits from the data range.
Parameters
----------
ax : matplotlib.axes.Axes
Axis to update.
series : typing.Sequence[list[float]]
Series values for limits.
Returns
-------
None
"""
values: list[float] = []
for values_list in series:
values.extend(values_list)
lower = max(0.0, min(values) - 5.0)
upper = min(100.0, max(values) + 5.0)
ax.set_ylim(lower, upper)
def build_bit_depth_ticks(
levels: typing.Sequence[float],
) -> tuple[list[float], list[str]]:
"""Return tick positions and integer labels for bit depth.
Parameters
----------
levels : typing.Sequence[float]
Bit depth levels.
Returns
-------
tuple
Tick positions and labels.
"""
ticks: list[float] = []
labels: list[str] = []
for level in levels:
if level <= 0:
continue
ticks.append(level)
labels.append(str(int(round(level))))
return ticks, labels
def thin_ticks(
levels: typing.Sequence[float],
labels: typing.Optional[typing.Sequence[str]] = None,
max_ticks: int = 10,
) -> tuple[list[float], typing.Optional[list[str]]]:
"""Thin tick marks to roughly max_ticks evenly spaced values.
Parameters
----------
levels : typing.Sequence[float]
Full list of level values.
labels : typing.Optional[typing.Sequence[str]], optional
Tick labels aligned with levels, if precomputed.
max_ticks : int, optional
Target tick count after thinning.
Returns
-------
tuple
Thinned levels and labels (if provided).
"""
if len(levels) <= max_ticks:
return list(levels), list(labels) if labels is not None else None
indices = np.linspace(0, len(levels) - 1, max_ticks, dtype=int)
indices = np.unique(indices)
thinned_levels = [levels[int(idx)] for idx in indices]
if labels is None:
return thinned_levels, None
thinned_labels = [labels[int(idx)] for idx in indices]
return thinned_levels, thinned_labels
def plot_panel(
filename: str,
protocols: typing.Sequence[str],
run_records: typing.Sequence[PerfRecord],
baseline: typing.Optional[PerfRecord],
) -> None:
"""Plot a multi-panel figure for the specified protocols.
Parameters
----------
filename : str
Output filename.
protocols : typing.Sequence[str]
Protocols to plot.
run_records : typing.Sequence[dict]
Performance records for a run.
baseline : dict or None
Baseline record for injection.
Returns
-------
None
"""
series_data: list[
tuple[list[float], SeriesMap, list[str]]
] = []
for protocol in protocols:
series_data.append(
build_series(
run_records,
protocol,
baseline,
BASELINE_LEVELS.get(protocol),
)
)
ncols = len(protocols)
fig, axes = plt.subplots(
1,
ncols,
figsize=(5.4 * ncols * 1.1, 4.8),
sharey=True,
)
if ncols == 1:
axes = [axes]
for idx, (protocol, ax) in enumerate(zip(protocols, axes)):
levels, series_map, keys = series_data[idx]
for key in SERIES_ORDER:
if key not in series_map:
continue
style = SERIES_STYLES.get(key, {})
label = style.get("label", key)
color = style.get("color")
plot_kwargs = {
"linewidth": 1,
"linestyle": LINESTYLE_MAP.get(key, "-"),
"label": label if idx == 0 else "_nolegend_",
}
if key == "acc_combined":
plot_kwargs["linewidth"] = 2.5
elif key == "acc_hu":
plot_kwargs["linewidth"] = 1.5
if color:
plot_kwargs["color"] = color
ax.plot(levels, series_map[key], **plot_kwargs)
baseline_x = BASELINE_LEVELS.get(protocol)
if baseline_x is not None:
ax.axvline(
baseline_x,
color="#7f7f7f",
linestyle="--",
linewidth=2,
label="baseline" if idx == 0 else "_nolegend_",
)
title, xlabel = PROTOCOL_META[protocol]
ax.set_title(title, fontweight="bold")
ax.set_xlabel(xlabel)
if idx == 0:
ax.set_ylabel("Classification Accuracy (%)")
else:
ax.set_ylabel("")
ax.yaxis.set_major_locator(MultipleLocator(5))
if protocol == "bit_depth":
ticks, labels = build_bit_depth_ticks(levels)
ticks, labels = thin_ticks(ticks, labels)
ax.set_xticks(ticks)
ax.set_xticklabels(labels)
else:
ticks, _ = thin_ticks(levels)
ax.set_xticks(ticks)
if protocol == "resolution":
base_size = float(plt.rcParams.get("font.size", 12))
ax.tick_params(axis="x", labelsize=base_size * 0.75)
if protocol in ("poi_noise", "spepper_noise"):
base_size = float(plt.rcParams.get("font.size", 12))
ax.tick_params(axis="x", labelsize=base_size * 0.7)
ax.grid(True, which="major", axis="both", linestyle=":", alpha=0.6)
all_series: list[list[float]] = []
for _, series_map, _ in series_data:
all_series.extend(series_map.values())
set_accuracy_limits(axes[0], all_series)
handles, labels = axes[0].get_legend_handles_labels()
fig.legend(
handles,
labels,
loc="lower left",
bbox_to_anchor=(0.0, -0.06, 1.0, 0.2),
mode="expand",
ncol=LEGEND_NCOL,
frameon=False,
fontsize=LEGEND_FONT_SIZE,
)
fig.tight_layout()
fig.subplots_adjust(bottom=0.25, wspace=0.08)
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
fig.savefig(OUTPUT_DIR / filename, dpi=300)
plt.close(fig)
def plot_orthogonality(
run_records: typing.Sequence[PerfRecord],
baseline: typing.Optional[PerfRecord],
) -> None:
"""Plot rotation vs drift orthogonality panels.
Parameters
----------
run_records : typing.Sequence[dict]
Performance records for a run.
baseline : dict or None
Baseline record for injection.
Returns
-------
None
"""
fig, axes = plt.subplots(1, 2, figsize=(10.8, 4.5), sharey=True)
for idx, protocol in enumerate(ORTHO_PROTOCOLS):
levels, series_map, _ = build_series(
run_records,
protocol,
baseline,
BASELINE_LEVELS.get(protocol),
)
ax = axes[idx]
for key in ORTHO_KEYS:
style = SERIES_STYLES.get(key, {})
ax.plot(
levels,
series_map[key],
label=style.get("label", key) if idx == 0 else "_nolegend_",
color=style.get("color"),
linestyle=LINESTYLE_MAP.get(key, "-"),
linewidth=2,
)
baseline_x = BASELINE_LEVELS.get(protocol)
if baseline_x is not None:
ax.axvline(
baseline_x,
color="#7f7f7f",
linestyle="--",
linewidth=2,
label="baseline" if idx == 0 else "_nolegend_",
)
title, xlabel = PROTOCOL_META[protocol]
ax.set_title(title, fontweight="bold")
ax.set_xlabel(xlabel)
if idx == 0:
ax.set_ylabel("Classification Accuracy (%)")
else:
ax.set_ylabel("")
ax.yaxis.set_major_locator(MultipleLocator(5))
ticks, _ = thin_ticks(levels)
ax.set_xticks(ticks)
ax.grid(True, which="major", axis="both", linestyle=":", alpha=0.6)
all_series: list[list[float]] = []
for protocol in ORTHO_PROTOCOLS:
_, series_map, _ = build_series(
run_records,
protocol,
baseline,
BASELINE_LEVELS.get(protocol),
)
for key in ORTHO_KEYS:
all_series.append(series_map[key])
set_accuracy_limits(axes[0], all_series)
handles, labels = axes[0].get_legend_handles_labels()
fig.legend(
handles,
labels,
loc="lower left",
bbox_to_anchor=(0.0, -0.08, 1.0, 0.2),
mode="expand",
ncol=ORTHO_LEGEND_NCOL,
frameon=False,
fontsize=ORTHO_LEGEND_FONT_SIZE,
)
fig.tight_layout()
fig.subplots_adjust(bottom=0.25, wspace=0.1)
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
fig.savefig(
OUTPUT_DIR / "plot_orthogonality.png",
dpi=300,
bbox_inches="tight",
pad_inches=0.05,
)
plt.close(fig)
def plot_single_protocol(
protocol: str,
run_records: typing.Sequence[PerfRecord],
baseline: typing.Optional[PerfRecord],
) -> None:
"""Plot a single protocol series with legend.
Parameters
----------
protocol : str
Protocol name.
run_records : typing.Sequence[dict]
Performance records for a run.
baseline : dict or None
Baseline record for injection.
Returns
-------
None
"""
levels, series_map, _ = build_series(
run_records,
protocol,
baseline,
BASELINE_LEVELS.get(protocol),
)
fig, ax = plt.subplots(figsize=(7.2, 4.8))
for key in SERIES_ORDER:
if key not in series_map:
continue
style = SERIES_STYLES.get(key, {})
label = style.get("label", key)
color = style.get("color")
linewidth = 1.4
if key == "acc_combined":
linewidth = 2.5
elif key == "acc_hu":
linewidth = 1.6
ax.plot(
levels,
series_map[key],
label=label,
color=color,
linestyle=LINESTYLE_MAP.get(key, "-"),
linewidth=linewidth,
)
baseline_x = BASELINE_LEVELS.get(protocol)
if baseline_x is not None:
ax.axvline(
baseline_x,
color="#7f7f7f",
linestyle="--",
linewidth=2,
label="baseline",
)
title, xlabel = PROTOCOL_META[protocol]
ax.set_title(title, fontweight="bold")
ax.set_xlabel(xlabel)
ax.set_ylabel("Classification Accuracy (%)")
ax.yaxis.set_major_locator(MultipleLocator(5))
if protocol == "bit_depth":
ticks, labels = build_bit_depth_ticks(levels)
ticks, labels = thin_ticks(ticks, labels)
ax.set_xticks(ticks)
ax.set_xticklabels(labels)
else:
ticks, _ = thin_ticks(levels)
ax.set_xticks(ticks)
if protocol == "resolution":
base_size = float(plt.rcParams.get("font.size", 12))
ax.tick_params(axis="x", labelsize=base_size * 0.75)
if protocol in ("poi_noise", "spepper_noise"):
base_size = float(plt.rcParams.get("font.size", 12))
ax.tick_params(axis="x", labelsize=base_size * 0.7)
ax.grid(True, which="major", axis="both", linestyle=":", alpha=0.6)
set_accuracy_limits(ax, list(series_map.values()))
handles, labels = ax.get_legend_handles_labels()
if protocol == "bit_depth":
ax.legend(
handles,
labels,
loc="lower right",
bbox_to_anchor=(0.98, 0.05),
ncol=3,
frameon=True,
fontsize=BASE_FONT_SIZE,
)
else:
fig.legend(
handles,
labels,
loc="lower left",
bbox_to_anchor=(0.0, 0.02, 1.0, 0.2),
mode="expand",
ncol=LEGEND_NCOL,
frameon=True,
fontsize=LEGEND_FONT_SIZE,
)
fig.tight_layout()
fig.subplots_adjust(bottom=0.25)
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
output_path = OUTPUT_DIR / f"plot_single_{protocol}.png"
if protocol == "bit_depth":
fig.savefig(output_path, dpi=300, bbox_inches="tight", pad_inches=0.05)
else:
fig.savefig(output_path, dpi=300)
plt.close(fig)
def main() -> None:
"""Generate grouped panels, single-protocol plots, and orthogonality plot."""
fs.seed_everything()
records = load_perf_log(fs.PERF_LOG_PATH)
step_records = [record for record in records if record.get("step") == "5_ablate"]
if not step_records:
raise ValueError("Missing 5_ablate records in perf log.")
run_id = select_latest_run(step_records)
run_records = [record for record in step_records if record.get("run_id") == run_id]
baseline = next(
(record for record in run_records if record.get("protocol") == "baseline"),
None,
)
for _, (filename, protocols) in PANEL_CONFIG.items():
plot_panel(filename, protocols, run_records, baseline)
plot_orthogonality(run_records, baseline)
for protocol in PROTOCOL_META:
plot_single_protocol(protocol, run_records, baseline)
if __name__ == "__main__":
main()