File size: 12,086 Bytes
dde53dd 839dea4 dde53dd 95923eb dde53dd 95923eb dde53dd 95923eb dde53dd 95923eb dde53dd 95923eb dde53dd 95923eb dde53dd | 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 | import argparse
import logging
import sys
from pathlib import Path
from typing import List, Optional, Sequence, Tuple
import matplotlib.pyplot as plt
import numpy as np
import torch # type: ignore
# Make sure the package root is on sys.path when running the example directly.
ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
try: # Optional HF-style dataset utilities (may be absent in this checkout)
from LWMTemporal.data import AngleDelayDatasetConfig, AngleDelaySequenceDataset # type: ignore
except ImportError: # pragma: no cover - keep script functional without data module
AngleDelayDatasetConfig = None # type: ignore
AngleDelaySequenceDataset = None # type: ignore
EPS = 1e-8
logger = logging.getLogger("ad_temporal_evolution")
def configure_style() -> None:
plt.style.use("dark_background")
plt.rcParams.update(
{
"figure.facecolor": "#0b0e11",
"axes.facecolor": "#0b0e11",
"axes.edgecolor": "#374151",
"axes.labelcolor": "#e5e7eb",
"axes.titleweight": "semibold",
"text.color": "#e5e7eb",
"xtick.color": "#9ca3af",
"ytick.color": "#9ca3af",
"grid.color": "#1f2937",
"figure.autolayout": False,
"font.size": 11,
"legend.frameon": False,
}
)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Visualise how angle-delay bins evolve over time using the LWM-Temporal preprocessing stack.",
)
parser.add_argument(
"--data_path",
type=Path,
default=Path("examples/data/city_12_chiyoda_3p5_20_32_32.p"),
help="Path to a .p payload (dict with 'channel') or raw tensor file.",
)
parser.add_argument(
"--sample_idx",
type=int,
default=0,
help="Sample index to select when the payload has a batch dimension (S, T, H, W).",
)
parser.add_argument(
"--keep_percentage",
type=float,
default=0.25,
help="Fraction of strongest delay taps to keep when converting to angle-delay.",
)
parser.add_argument(
"--normalize",
choices=["none", "per_sample_rms", "global_rms"],
default="global_rms",
help="Normalization mode applied after the angle-delay transform.",
)
parser.add_argument(
"--bins",
type=int,
default=6,
help="Number of angle-delay bins to visualise (top-K by average magnitude).",
)
parser.add_argument(
"--coords",
type=int,
nargs="*",
help="Optional explicit bin coordinates supplied as n0 m0 n1 m1 ...",
)
parser.add_argument(
"--out_path",
type=Path,
default=Path("examples/data/figs/ad_temporal_evolution.png"),
help="Destination path for the saved figure.",
)
parser.add_argument(
"--max_time_steps",
type=int,
default=None,
help="Optional temporal truncation applied before preprocessing.",
)
parser.add_argument(
"--cache_dir",
type=Path,
default=Path("cache"),
help="Cache directory used by the dataset API.",
)
parser.add_argument(
"--no_cache",
action="store_true",
help="Disable caching when using the dataset API.",
)
parser.add_argument(
"--overwrite_cache",
action="store_true",
help="Overwrite cached tensors when using the dataset API.",
)
parser.add_argument(
"--phase_mode",
choices=["real_imag", "mag_phase"],
default="real_imag",
help="Phase representation expected by downstream models (dataset API).",
)
parser.add_argument(
"--patch_height",
type=int,
default=1,
help="Patch height provided to the dataset API (ignored when unavailable).",
)
parser.add_argument(
"--patch_width",
type=int,
default=1,
help="Patch width provided to the dataset API (ignored when unavailable).",
)
parser.add_argument(
"--title",
type=str,
default=None,
help="Optional custom figure title.",
)
parser.add_argument(
"--verbose",
action="store_true",
help="Enable debug logging for troubleshooting.",
)
return parser.parse_args()
def _configure_logging(verbose: bool) -> None:
level = logging.DEBUG if verbose else logging.INFO
logging.basicConfig(level=level, format="[%(levelname)s] %(message)s")
def _parse_coord_pairs(raw: Optional[Sequence[int]]) -> Optional[List[Tuple[int, int]]]:
if not raw:
return None
if len(raw) % 2 != 0:
raise ValueError("coords must be provided as pairs: n0 m0 n1 m1 ...")
pairs = []
for i in range(0, len(raw), 2):
pairs.append((int(raw[i]), int(raw[i + 1])))
return pairs
def _ensure_complex(tensor: torch.Tensor) -> torch.Tensor:
if tensor.is_complex():
return tensor.to(torch.complex64)
if tensor.ndim >= 1 and tensor.size(-1) == 2:
real = tensor[..., 0].float()
imag = tensor[..., 1].float()
return torch.complex(real, imag)
return torch.complex(tensor.float(), torch.zeros_like(tensor.float()))
def load_sequence(args: argparse.Namespace) -> torch.Tensor:
if AngleDelayDatasetConfig is None or AngleDelaySequenceDataset is None:
raise ImportError(
"LWMTemporal.data.datasets is required. Install the full LWMTemporal package to use this example.",
)
cfg = AngleDelayDatasetConfig(
raw_path=args.data_path,
keep_percentage=args.keep_percentage,
normalize=args.normalize,
cache_dir=args.cache_dir,
use_cache=not args.no_cache,
overwrite_cache=args.overwrite_cache,
snr_db=None,
noise_seed=None,
max_time_steps=args.max_time_steps,
patch_size=(args.patch_height, args.patch_width),
phase_mode=args.phase_mode,
)
dataset = AngleDelaySequenceDataset(cfg)
if len(dataset) == 0:
raise RuntimeError("AngleDelaySequenceDataset returned zero samples.")
idx = max(0, min(args.sample_idx, len(dataset) - 1))
sample = dataset[idx]
if isinstance(sample, dict):
if "sequence" in sample:
tensor = sample["sequence"]
elif "angle_delay" in sample:
tensor = sample["angle_delay"]
else:
raise KeyError("Dataset item missing 'sequence' or 'angle_delay' entries.")
else:
tensor = sample
tensor = _ensure_complex(torch.as_tensor(tensor))
if tensor.ndim == 4 and tensor.size(0) == 1:
tensor = tensor.squeeze(0)
if tensor.ndim != 3:
raise ValueError(f"Expected dataset sample with shape (T, H, W); received {tuple(tensor.shape)}")
logger.debug("Loaded sequence via dataset API with shape %s", tuple(tensor.shape))
return tensor
def pick_bins(
sequence: torch.Tensor,
k: int,
coords: Optional[List[Tuple[int, int]]],
) -> List[Tuple[int, int]]:
if sequence.ndim != 3:
raise ValueError(f"Expected angle-delay tensor with shape (T, H, W); got {tuple(sequence.shape)}")
_, H, W = sequence.shape
picks: List[Tuple[int, int]] = []
if coords:
for n, m in coords:
if 0 <= n < H and 0 <= m < W and (n, m) not in picks:
picks.append((n, m))
if len(picks) >= k:
return picks[:k]
remaining = max(0, k - len(picks))
if remaining == 0:
return picks
mag = sequence.abs().mean(dim=0)
topk = torch.topk(mag.flatten(), k=min(remaining, H * W - len(picks)))
for idx in topk.indices.tolist():
n = idx // W
m = idx % W
if (n, m) not in picks:
picks.append((n, m))
if len(picks) == k:
break
return picks
def fit_line(y: np.ndarray) -> Tuple[float, float, float]:
x = np.arange(len(y))
A = np.vstack([x, np.ones_like(x)]).T
sol, *_ = np.linalg.lstsq(A, y, rcond=None)
slope, intercept = sol
y_pred = slope * x + intercept
ss_res = np.sum((y - y_pred) ** 2)
ss_tot = np.sum((y - y.mean()) ** 2) + EPS
r2 = 1.0 - ss_res / ss_tot
return float(slope), float(intercept), float(r2)
def plot_curves(
sequence: torch.Tensor,
picks: List[Tuple[int, int]],
out_path: Path,
title: str,
) -> None:
if sequence.ndim != 3:
raise ValueError("plot_curves expects a tensor with shape (T, H, W).")
T = sequence.shape[0]
times = np.arange(T)
num_bins = len(picks)
if num_bins == 0:
raise ValueError("No bins were selected for plotting.")
fig, axes = plt.subplots(
num_bins,
2,
figsize=(11, 3 * max(1, num_bins)),
dpi=150,
constrained_layout=True,
)
fig.patch.set_facecolor("#0b0e11")
axes = np.atleast_2d(axes)
label_color = "#cbd5f5"
title_color = "#f8fafc"
for row, (n, m) in enumerate(picks):
series = sequence[:, n, m]
mag = series.abs().cpu().numpy()
phase = torch.angle(series).cpu().numpy()
phase = np.unwrap(phase)
slope_mag, _, r2_mag = fit_line(mag)
slope_phase, _, r2_phase = fit_line(phase)
ax_mag = axes[row, 0]
ax_mag.set_facecolor("#111827")
ax_mag.plot(
times,
mag,
label=f"|H| slope={slope_mag:.3g}, R²={r2_mag:.2f}",
color="#38bdf8",
linewidth=2.2,
)
ax_mag.fill_between(times, mag, color="#38bdf8", alpha=0.08)
ax_mag.set_ylim(mag.min(), mag.max())
ax_mag.set_title(f"Bin (n={n}, m={m}) magnitude", color=title_color)
ax_mag.set_xlabel("time index", color=label_color)
ax_mag.set_ylabel("|H|", color=label_color)
ax_mag.tick_params(colors=label_color)
ax_mag.grid(True, linestyle="--", linewidth=0.6, alpha=0.4)
for spine in ax_mag.spines.values():
spine.set_color("#1f2937")
legend_mag = ax_mag.legend(loc="upper left", fontsize=9)
legend_mag.get_frame().set_facecolor("#111827")
legend_mag.get_frame().set_alpha(0.6)
for text in legend_mag.get_texts():
text.set_color(label_color)
ax_phase = axes[row, 1]
ax_phase.set_facecolor("#111827")
ax_phase.plot(
times,
phase,
label=f"∠H slope={slope_phase:.3g}, R²={r2_phase:.2f}",
color="#f87171",
linewidth=2.2,
)
ax_phase.set_title(f"Bin (n={n}, m={m}) phase (unwrapped)", color=title_color)
ax_phase.set_xlabel("time index", color=label_color)
ax_phase.set_ylabel("radians", color=label_color)
ax_phase.tick_params(colors=label_color)
ax_phase.grid(True, linestyle="--", linewidth=0.6, alpha=0.4)
for spine in ax_phase.spines.values():
spine.set_color("#1f2937")
legend_phase = ax_phase.legend(loc="upper left", fontsize=9)
legend_phase.get_frame().set_facecolor("#111827")
legend_phase.get_frame().set_alpha(0.6)
for text in legend_phase.get_texts():
text.set_color(label_color)
fig.suptitle(title, fontsize=12, color=title_color)
out_path.parent.mkdir(parents=True, exist_ok=True)
fig.savefig(out_path)
plt.close(fig)
def main() -> None:
args = parse_args()
_configure_logging(args.verbose)
configure_style()
coords = _parse_coord_pairs(args.coords)
sequence = load_sequence(args)
picks = pick_bins(sequence, args.bins, coords)
if not picks:
raise RuntimeError("Unable to select any angle-delay bins for visualisation.")
title = args.title or f"Angle-delay temporal curves | keep={args.keep_percentage:.2f} | norm={args.normalize}"
plot_curves(sequence, picks, args.out_path, title)
logger.info("Saved figure to %s", args.out_path)
if __name__ == "__main__":
main()
|