-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualize_dataset.py
More file actions
298 lines (252 loc) · 12.3 KB
/
Copy pathvisualize_dataset.py
File metadata and controls
298 lines (252 loc) · 12.3 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
"""
Visualize a saved toy training dataset (.pt file).
Loads the dict written by `generate_dataset.py` (keys: c, X, y) and produces a set
of diagnostic plots of the NOMINAL dataset:
1. x-space 2D density per class → ds_x_2d.{pdf,png}
2. x-space 1D marginals per class (x₁, x₂) → ds_x_1d.{pdf,png}
3. y-space 2D density per class → ds_y_2d.{pdf,png}
4. y-space 1D marginals per class (y₁, y₂) → ds_y_1d.{pdf,png}
5. y-space binned in x₁ — sees the x-conditioning of p(y|x,c) → ds_y_xbinned.{pdf,png}
6. stacked histograms of the total mixture (x and y) → ds_stacked_x.{pdf,png},
ds_stacked_y.{pdf,png}
7. class balance / sample counts → printed to stdout
Usage:
python visualize_dataset.py data/dataset.pt
python visualize_dataset.py data/dataset.pt --out-dir validation/dataset
"""
import argparse
import os
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib.patches import Patch
import numpy as np
from scipy.ndimage import gaussian_filter
import torch
CLASS_COLORS = {0: "dodgerblue", 1: "darkorange"}
CLASS_LABELS = {0: "Class A", 1: "Class B"}
def _save(fig, out_dir, name):
for ext in ("pdf", "png"):
fig.savefig(os.path.join(out_dir, f"{name}.{ext}"), dpi=150, bbox_inches="tight")
print(f" saved {name}")
plt.close(fig)
def _class_idx(c):
"""one-hot [N, 2] → int [N] in {0, 1}."""
arr = c.cpu().numpy() if torch.is_tensor(c) else c
if arr.ndim == 1:
return arr.astype(int)
return arr[:, 1].astype(int)
def _contour2d(ax, arr, bins=60, range2d=None, smooth=1.2, cmap=None, levels=7, alpha=0.6):
"""Filled smoothed 2D histogram. Returns (xc, yc, h_smoothed)."""
h, xe, ye = np.histogram2d(arr[:, 0], arr[:, 1], bins=bins,
range=list(range2d) if range2d is not None else None,
density=True)
h = gaussian_filter(h, sigma=smooth)
xc = 0.5 * (xe[:-1] + xe[1:])
yc = 0.5 * (ye[:-1] + ye[1:])
if h.max() > 0:
lvls = np.linspace(h.max() * 0.04, h.max() * 0.95, levels)
ax.contourf(xc, yc, h.T, levels=lvls, cmap=cmap, alpha=alpha)
return xc, yc, h
def _auto_range_2d(*arrays_2d, pct=(0.1, 99.9), pad_frac=0.15):
"""Per-dim percentile range for 2D plots — returns ((x_lo, x_hi), (y_lo, y_hi)).
Default (0.1, 99.9) + 15% pad sits comfortably outside the outer (4% × max
density) contour so the smoothed cluster envelope is never clipped.
"""
out = []
for d in range(2):
vals = np.concatenate([np.asarray(a)[:, d] for a in arrays_2d if a is not None])
lo, hi = np.percentile(vals, pct)
pad = pad_frac * (hi - lo + 1e-6)
out.append((float(lo - pad), float(hi + pad)))
return tuple(out)
def _auto_range_1d(*arrays_1d, pct=(0.1, 99.9), pad_frac=0.10):
"""Single-axis percentile range from any number of 1D arrays."""
vals = np.concatenate([np.asarray(a).ravel() for a in arrays_1d if a is not None])
lo, hi = np.percentile(vals, pct)
pad = pad_frac * (hi - lo + 1e-6)
return float(lo - pad), float(hi + pad)
def _y_range(y):
lo = float(y.min())
hi = float(y.max())
pad = 0.05 * (hi - lo + 1e-6)
return ((lo - pad, hi + pad),) * 2
def plot_x_2d(c_idx, X, out_dir):
"""2D distributions of x per class (filled contours)."""
range_2d = _auto_range_2d(X)
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
for cl in [0, 1]:
mask = c_idx == cl
ax = axes[cl]
cmap = "Blues" if cl == 0 else "Oranges"
_contour2d(ax, X[mask], bins=80, range2d=range_2d, smooth=1.2, cmap=cmap, alpha=0.6)
ax.set_xlim(*range_2d[0]); ax.set_ylim(*range_2d[1]); ax.set_aspect("equal")
ax.set_xlabel("x₁"); ax.set_ylabel("x₂")
ax.set_title(f"{CLASS_LABELS[cl]} — p(x | c)")
ax.legend(handles=[Patch(facecolor=CLASS_COLORS[cl], alpha=0.6, label="x (nominal)")],
fontsize=9)
plt.suptitle("Dataset — x distribution per class", fontsize=13)
plt.tight_layout()
_save(fig, out_dir, "ds_x_2d")
def plot_x_1d(c_idx, X, out_dir):
# Per-dim percentile range so each marginal zooms to its actual support.
bins_dim = {dim: np.linspace(*_auto_range_1d(X[:, dim]), 60) for dim in (0, 1)}
fig, axes = plt.subplots(2, 2, figsize=(12, 8))
for cl in [0, 1]:
mask = c_idx == cl
for dim in [0, 1]:
ax = axes[cl, dim]
bins = bins_dim[dim]
ax.hist(X[mask, dim], bins=bins, density=True, histtype="stepfilled",
alpha=0.4, color=CLASS_COLORS[cl], label="x (nominal)")
ax.set_title(f"{CLASS_LABELS[cl]} — x{'₁' if dim == 0 else '₂'}")
ax.set_xlabel(f"x{'₁' if dim == 0 else '₂'}")
ax.set_ylabel("density")
ax.set_xlim(bins[0], bins[-1])
ax.legend(fontsize=9)
plt.suptitle("Dataset — x 1D marginals", fontsize=13)
plt.tight_layout()
_save(fig, out_dir, "ds_x_1d")
def plot_y_2d(c_idx, y, out_dir):
range_y = _y_range(y)
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
for cl in [0, 1]:
mask = c_idx == cl
ax = axes[cl]
cmap = "Blues" if cl == 0 else "Oranges"
_contour2d(ax, y[mask], bins=80, range2d=range_y, smooth=1.5, cmap=cmap, alpha=0.6)
ax.set_xlim(*range_y[0]); ax.set_ylim(*range_y[1]); ax.set_aspect("equal")
ax.set_xlabel("y₁"); ax.set_ylabel("y₂")
ax.set_title(f"{CLASS_LABELS[cl]} — p(y | c)")
ax.legend(handles=[Patch(facecolor=CLASS_COLORS[cl], alpha=0.6, label="y (nominal)")],
fontsize=9)
plt.suptitle("Dataset — y distribution per class", fontsize=13)
plt.tight_layout()
_save(fig, out_dir, "ds_y_2d")
def plot_y_1d(c_idx, y, out_dir):
range_y = _y_range(y)
bins = np.linspace(range_y[0][0], range_y[0][1], 60)
fig, axes = plt.subplots(2, 2, figsize=(12, 8))
for cl in [0, 1]:
mask = c_idx == cl
for dim in [0, 1]:
ax = axes[cl, dim]
ax.hist(y[mask, dim], bins=bins, density=True, histtype="stepfilled",
alpha=0.4, color=CLASS_COLORS[cl], label="y (nominal)")
ax.set_title(f"{CLASS_LABELS[cl]} — y{'₁' if dim == 0 else '₂'}")
ax.set_xlabel(f"y{'₁' if dim == 0 else '₂'}")
ax.set_ylabel("density")
ax.legend(fontsize=9)
plt.suptitle("Dataset — y 1D marginals", fontsize=13)
plt.tight_layout()
_save(fig, out_dir, "ds_y_1d")
def plot_y_xbinned(c_idx, X, y, out_dir):
"""For each class, slice events by x₁ band (with |x₂|<0.5) and plot y density.
Shows how p(y|x,c) varies with x₁."""
x1_edges = np.array([-2.5, -1.0, 0.0, 1.0, 2.5])
n_bins = len(x1_edges) - 1
range_y = _y_range(y)
x2_central = np.abs(X[:, 1]) < 0.5
fig, axes = plt.subplots(2, n_bins, figsize=(4 * n_bins, 8))
for cl in [0, 1]:
for xb in range(n_bins):
xl, xr = x1_edges[xb], x1_edges[xb + 1]
mask = ((X[:, 0] >= xl) & (X[:, 0] < xr) & x2_central & (c_idx == cl))
ax = axes[cl, xb]
if mask.sum() > 50:
cmap = "Blues" if cl == 0 else "Oranges"
_contour2d(ax, y[mask], bins=40, range2d=range_y, smooth=1.5,
cmap=cmap, alpha=0.7)
ax.set_xlim(*range_y[0]); ax.set_ylim(*range_y[1]); ax.set_aspect("equal")
ax.set_xlabel("y₁", fontsize=8); ax.set_ylabel("y₂", fontsize=8)
ax.set_title(f"{CLASS_LABELS[cl]} x₁∈[{xl:.1f},{xr:.1f}), |x₂|<0.5", fontsize=9)
plt.suptitle("Dataset — p(y | x₁, c) (|x₂|<0.5 slices)", fontsize=13)
plt.tight_layout()
_save(fig, out_dir, "ds_y_xbinned")
def plot_stacked_1d(c_idx, arr, dim_names, bin_range, out_dir, fname, title):
"""Stacked 1D histograms of the total mixture decomposed into class A + class B.
arr: [N, D] nominal samples (e.g. X or y).
bin_range: either a single (lo, hi) tuple shared across dims, or None to auto-zoom
per dim from the data percentiles. Each axis of `arr` gets its own subplot.
Shows counts (not density) so the stacking is meaningful as event yield.
"""
n_dim = arr.shape[1]
# Per-dim bins: auto from percentiles if no fixed range given.
if bin_range is None:
bins_dim = {dim: np.linspace(*_auto_range_1d(arr[:, dim]), 60) for dim in range(n_dim)}
else:
shared = np.linspace(bin_range[0], bin_range[1], 60)
bins_dim = {dim: shared for dim in range(n_dim)}
fig, axes = plt.subplots(1, n_dim, figsize=(6 * n_dim, 5), squeeze=False)
axes = axes[0]
for dim in range(n_dim):
ax = axes[dim]
bins = bins_dim[dim]
arrs = [arr[c_idx == 0, dim], arr[c_idx == 1, dim]]
labels = [f"{CLASS_LABELS[0]} ({len(arrs[0])})",
f"{CLASS_LABELS[1]} ({len(arrs[1])})"]
ax.hist(arrs, bins=bins, stacked=True, histtype="stepfilled",
color=[CLASS_COLORS[0], CLASS_COLORS[1]],
alpha=0.75, edgecolor="white", linewidth=0.4,
label=labels)
ax.set_xlabel(dim_names[dim])
ax.set_ylabel("Events / bin")
ax.set_xlim(bins[0], bins[-1])
ax.legend(fontsize=9, loc="best")
ax.set_title(f"Stacked — {dim_names[dim]}")
plt.suptitle(title, fontsize=13)
plt.tight_layout()
_save(fig, out_dir, fname)
def main():
parser = argparse.ArgumentParser(description="Visualize a toy training dataset (.pt).")
parser.add_argument("dataset", help="Path to the dataset .pt file (e.g. data/dataset.pt)")
parser.add_argument("--out-dir", default=None,
help="Output directory (default: validation/<dataset-basename>/)")
args = parser.parse_args()
if not os.path.isfile(args.dataset):
raise FileNotFoundError(args.dataset)
out_dir = args.out_dir or os.path.join(
"validation", os.path.splitext(os.path.basename(args.dataset))[0]
)
os.makedirs(out_dir, exist_ok=True)
print(f"Loading {args.dataset}")
print(f"Output: {out_dir}/")
data = torch.load(args.dataset, map_location="cpu", weights_only=False)
expected = {"c", "X", "y"}
missing = expected - set(data.keys())
if missing:
raise KeyError(f"dataset missing required keys: {missing}")
c = data["c"]
X = data["X"].cpu().numpy() if torch.is_tensor(data["X"]) else np.asarray(data["X"])
y = data["y"].cpu().numpy() if torch.is_tensor(data["y"]) else np.asarray(data["y"])
c_idx = _class_idx(c)
n_total = len(c_idx); n_A = int((c_idx == 0).sum()); n_B = int((c_idx == 1).sum())
print("\n--- Dataset summary ---")
print(f"N total = {n_total}")
print(f"N class A = {n_A} ({100 * n_A / n_total:.2f}%)")
print(f"N class B = {n_B} ({100 * n_B / n_total:.2f}%)")
print(f"X shape = {X.shape} range x₁ [{X[:, 0].min():+.3f}, {X[:, 0].max():+.3f}]"
f" x₂ [{X[:, 1].min():+.3f}, {X[:, 1].max():+.3f}]")
print(f"y shape = {y.shape} range y₁ [{y[:, 0].min():+.3f}, {y[:, 0].max():+.3f}]"
f" y₂ [{y[:, 1].min():+.3f}, {y[:, 1].max():+.3f}]")
for cl in [0, 1]:
mask = c_idx == cl
print(f" {CLASS_LABELS[cl]}: ⟨x⟩ = ({X[mask, 0].mean():+.3f}, {X[mask, 1].mean():+.3f}),"
f" σ(x) = ({X[mask, 0].std():.3f}, {X[mask, 1].std():.3f})"
f" ⟨y⟩ = ({y[mask, 0].mean():+.3f}, {y[mask, 1].mean():+.3f})")
print()
plot_x_2d(c_idx, X, out_dir)
plot_x_1d(c_idx, X, out_dir)
plot_y_2d(c_idx, y, out_dir)
plot_y_1d(c_idx, y, out_dir)
plot_y_xbinned(c_idx, X, y, out_dir)
# Stacked total-mixture histograms (counts, decomposed by class).
plot_stacked_1d(c_idx, X, ("x₁", "x₂"), None,
out_dir, "ds_stacked_x", "Dataset — stacked x distribution (A+B)")
y_lo, y_hi = float(y.min()), float(y.max())
pad = 0.05 * (y_hi - y_lo + 1e-6)
plot_stacked_1d(c_idx, y, ("y₁", "y₂"), (y_lo - pad, y_hi + pad),
out_dir, "ds_stacked_y", "Dataset — stacked y distribution (A+B)")
print(f"\nDone. All plots in {out_dir}/")
if __name__ == "__main__":
main()