"""Minimal inference example for the released VS2-UNet-NegCap1 checkpoint.

Run:  python inference_example.py  (expects model.py and the checkpoint
      in the sibling ../weights/ directory)

The example feeds random tensors only to demonstrate tensor shapes and the
calling convention. Assembling real inputs requires the VIIRS L1 radiance
pipeline and pre-event Sentinel-2 composites described in channel_spec.json
and the README; those data assets are not part of this release.
"""
import numpy as np
import torch

from model import load_release_checkpoint

torch.manual_seed(0)
np.random.seed(0)

model, ck = load_release_checkpoint("../weights/vs2_unet_negcap1_seed42.pt")
spec_mean = np.array(ck["normalization"]["viirs_mean"], dtype=np.float32)
spec_std = np.array(ck["normalization"]["viirs_std"], dtype=np.float32)

# --- assemble one window (random placeholder values; see channel_spec.json) ---
# channels 1-23: named VIIRS spectral/thermal channels, standardized below
x_named = np.random.randn(23, 64, 64).astype(np.float32) * 50.0
x_named = (x_named - spec_mean[:, None, None]) / spec_std[:, None, None]
# channel 24: night indicator (1.0 night / 0.0 day, constant over the window)
x_night = np.zeros((1, 64, 64), np.float32)
# channels 25-27: persistence priors p30/p90/frp90, standardized per-window
# constants; use the no-history encoding here
x_persist = np.zeros((3, 64, 64), np.float32)
x = torch.from_numpy(np.concatenate([x_named, x_night, x_persist], 0))[None]

# --- S2 background patch (9 bands, DynamicWorld-style normalized) ---
s2 = torch.from_numpy(np.random.rand(1, 9, 64, 64).astype(np.float32))
ok = torch.ones(1, 1)  # 1.0 = valid composite (use 0.0 with zero-filled s2 if absent)

with torch.no_grad():
    heatmap_logits, obj_logit = model(x, s2, ok)

fire_prob_map = torch.sigmoid(heatmap_logits)      # (1, 1, 64, 64) per-pixel
window_prob = torch.sigmoid(obj_logit)             # (1, 1) window-level
print("NOTE: inputs here are random placeholders (out of distribution); "
      "the numbers below demonstrate tensor shapes and ranges only.")
print(f"heatmap probability map: {tuple(fire_prob_map.shape)}, "
      f"max={fire_prob_map.max():.3f}")
print(f"window fire-presence probability: {window_prob.item():.3f} "
      f"(decision threshold 0.5)")
