From a7a55e1d5e9460f51f1b5586f3e561d9ee6d3dac Mon Sep 17 00:00:00 2001 From: tHarvey303 Date: Fri, 1 May 2026 18:56:23 +0100 Subject: [PATCH 01/22] Initial score-based diffusion uncertainity model, based on the pop-cosmos approach. --- src/synference/noise_models.py | 549 ++++++++++++++++++++++++++++++++- 1 file changed, 548 insertions(+), 1 deletion(-) diff --git a/src/synference/noise_models.py b/src/synference/noise_models.py index 814338f..cf9ba4f 100644 --- a/src/synference/noise_models.py +++ b/src/synference/noise_models.py @@ -6,11 +6,13 @@ import warnings from abc import ABC, abstractmethod -from typing import Any, Dict, Optional, Tuple, Union +from typing import Any, Dict, List, Optional, Tuple, Union import h5py import matplotlib.pyplot as plt import numpy as np +import torch +import torch.nn as nn from astropy.table import Table from scipy import stats from scipy.interpolate import interp1d @@ -1099,6 +1101,550 @@ def apply_scalings( return flux_internal, final_error +# ============================================================================= +# SCORE-BASED DIFFUSION UNCERTAINTY MODEL +# ============================================================================= + + +class _ScoreNetwork(nn.Module): + """Conditional score network sχ(x, t; m) for VP-SDE. + + Five-layer densely-connected network with 256 units per layer and tanh + activations (Y. Song et al. 2021). The diffusion time t is embedded via a + two-layer MLP and concatenated with the noisy log-uncertainty x and the + conditioning magnitudes m before being passed through the main trunk. + + Args: + n_filters: Dimensionality of x (and m). + hidden_dim: Width of each hidden layer. + n_layers: Number of hidden layers (paper: 5). + time_embed_dim: Dimension of the time embedding. + """ + + def __init__( + self, + n_filters: int, + hidden_dim: int = 256, + n_layers: int = 5, + time_embed_dim: int = 32, + ): + super().__init__() + self.time_embed = nn.Sequential( + nn.Linear(1, time_embed_dim), + nn.Tanh(), + nn.Linear(time_embed_dim, time_embed_dim), + nn.Tanh(), + ) + input_dim = n_filters + n_filters + time_embed_dim + layers: List[nn.Module] = [] + in_dim = input_dim + for _ in range(n_layers): + layers += [nn.Linear(in_dim, hidden_dim), nn.Tanh()] + in_dim = hidden_dim + layers.append(nn.Linear(hidden_dim, n_filters)) + self.net = nn.Sequential(*layers) + + def forward( + self, x: torch.Tensor, t: torch.Tensor, m: torch.Tensor + ) -> torch.Tensor: + """Compute score estimate sχ(x, t; m). + + Args: + x: Noisy log-uncertainty (batch, n_filters). + t: Diffusion time, shape (batch,). + m: Conditioning magnitudes (batch, n_filters). + + Returns: + Score estimate, shape (batch, n_filters). + """ + t_emb = self.time_embed(t.float().view(-1, 1)) + return self.net(torch.cat([x, m, t_emb], dim=-1)) + + +class ScoreBasedUncertaintyModel(UncertaintyModel): + """Multi-filter photometric uncertainty model via VP-SDE score diffusion. + + Implements the model P(ln σ_P | m) from A24, where σ_P is the per-filter + flux uncertainty and m is the vector of calibrated model magnitudes. A + conditional score network sχ(x, t; m) is trained by denoising score + matching (Y. Song & S. Ermon 2019; Y. Song et al. 2021) using a VP SDE: + + dx = −½ β(t) x dt + √β(t) dw, β(t) = βmin + (t/T)(βmax − βmin) + + with βmin = 0.1, βmax = 20.0, T = 1.0 (J. Ho et al. 2020). + + The network is a five-layer densely-connected network with 256 units per + layer and tanh activations, optimised with Adam (D. P. Kingma & J. Ba 2015). + + Unlike the per-filter empirical models, this model operates on ALL filters + jointly, capturing cross-filter correlations in the noise. + + Args: + n_filters: Number of photometric filters. + hidden_dim: Hidden layer width (paper: 256). + n_layers: Number of hidden layers (paper: 5). + time_embed_dim: Dimension of the time-embedding MLP output. + beta_min: Minimum noise schedule value β_min (paper: 0.1). + beta_max: Maximum noise schedule value β_max (paper: 20.0). + T: Terminal diffusion time (paper: 1.0). + n_sampling_steps: Euler-Maruyama steps for the reverse SDE. + device: PyTorch device string. Defaults to CUDA if available. + return_noise: If True, apply_noise returns (noisy_flux, sigma). + """ + + def __init__( + self, + n_filters: int, + hidden_dim: int = 256, + n_layers: int = 5, + time_embed_dim: int = 32, + beta_min: float = 0.1, + beta_max: float = 20.0, + T: float = 1.0, + n_sampling_steps: int = 500, + device: Optional[str] = None, + **kwargs: Any, + ): + super().__init__(**kwargs) + self.n_filters = n_filters + self.hidden_dim = hidden_dim + self.n_layers = n_layers + self.time_embed_dim = time_embed_dim + self.beta_min = beta_min + self.beta_max = beta_max + self.T = T + self.n_sampling_steps = n_sampling_steps + + if device is None: + device = "cuda" if torch.cuda.is_available() else "cpu" + self.device = device + + self.score_net = _ScoreNetwork( + n_filters, hidden_dim, n_layers, time_embed_dim + ).to(device) + + # Normalisation statistics (populated during fit) + self._ln_sigma_mean: Optional[torch.Tensor] = None + self._ln_sigma_std: Optional[torch.Tensor] = None + self._mag_mean: Optional[torch.Tensor] = None + self._mag_std: Optional[torch.Tensor] = None + self._is_trained: bool = False + + # ------------------------------------------------------------------ + # VP-SDE helpers + # ------------------------------------------------------------------ + + def _beta(self, t: torch.Tensor) -> torch.Tensor: + """Noise schedule β(t) = βmin + (t/T)(βmax − βmin).""" + return self.beta_min + (t / self.T) * (self.beta_max - self.beta_min) + + def _alpha(self, t: torch.Tensor) -> torch.Tensor: + """Cumulative noise ∫₀ᵗ β(s)ds.""" + return self.beta_min * t + (t**2 / (2.0 * self.T)) * ( + self.beta_max - self.beta_min + ) + + def _marginal_params( + self, t: torch.Tensor + ) -> Tuple[torch.Tensor, torch.Tensor]: + """Transition kernel parameters p(xₜ|x₀) = N(s(t)x₀, σ²(t)I). + + Returns: + s: Scale factor exp(−α(t)/2), shape matching t. + sigma: Standard deviation √(1 − exp(−α(t))), shape matching t. + """ + A = self._alpha(t) + s = torch.exp(-0.5 * A) + sigma = torch.sqrt(torch.clamp(1.0 - torch.exp(-A), min=1e-8)) + return s, sigma + + # ------------------------------------------------------------------ + # Normalisation helpers + # ------------------------------------------------------------------ + + def _normalise_ln_sigma(self, ln_sigma: torch.Tensor) -> torch.Tensor: + return (ln_sigma - self._ln_sigma_mean) / (self._ln_sigma_std + 1e-8) + + def _denormalise_ln_sigma(self, z: torch.Tensor) -> torch.Tensor: + return z * self._ln_sigma_std + self._ln_sigma_mean + + def _normalise_mag(self, mag: torch.Tensor) -> torch.Tensor: + return (mag - self._mag_mean) / (self._mag_std + 1e-8) + + # ------------------------------------------------------------------ + # Training + # ------------------------------------------------------------------ + + def fit( + self, + magnitudes: np.ndarray, + flux_uncertainties: np.ndarray, + n_epochs: int = 2000, + batch_size: int = 512, + learning_rate: float = 1e-3, + t_eps: float = 1e-3, + valid_fraction: float = 0.1, + patience: int = 50, + verbose: bool = True, + ) -> Dict[str, List[float]]: + """Train the score network on catalog (magnitudes, flux uncertainties). + + The model learns P(ln σ_P | m) via denoising score matching. Inputs + are standardised per-filter before training; the statistics are stored + for use at inference time. + + Args: + magnitudes: (N, n_filters) AB magnitudes (calibrated model mags). + flux_uncertainties: (N, n_filters) flux uncertainties in Jy + (must be strictly positive). + n_epochs: Maximum number of training epochs. + batch_size: Mini-batch size. + learning_rate: Adam initial learning rate. + t_eps: Minimum diffusion time (avoids t → 0 singularity). + valid_fraction: Fraction of data held out for validation / + early stopping. + patience: Early-stopping patience measured in validation-check + intervals (one check every 10 epochs). + verbose: Print training progress. + + Returns: + History dict with keys ``"train_loss"`` and ``"val_loss"``. + """ + if magnitudes.ndim != 2 or magnitudes.shape[1] != self.n_filters: + raise ValueError( + f"magnitudes must be (N, {self.n_filters}), got {magnitudes.shape}" + ) + if flux_uncertainties.shape != magnitudes.shape: + raise ValueError( + "flux_uncertainties must have the same shape as magnitudes." + ) + if np.any(flux_uncertainties <= 0): + warnings.warn( + "flux_uncertainties contains non-positive values; " + "these rows will be discarded." + ) + + ln_sigma = np.log(flux_uncertainties) + valid = np.all( + np.isfinite(ln_sigma) & np.isfinite(magnitudes), axis=1 + ) + magnitudes = magnitudes[valid] + ln_sigma = ln_sigma[valid] + + if len(magnitudes) < 2: + raise ValueError("Too few valid training samples after filtering.") + + # Per-filter normalisation statistics + ln_sigma_mean = ln_sigma.mean(axis=0).astype(np.float32) + ln_sigma_std = ln_sigma.std(axis=0).astype(np.float32) + mag_mean = magnitudes.mean(axis=0).astype(np.float32) + mag_std = magnitudes.std(axis=0).astype(np.float32) + + self._ln_sigma_mean = torch.tensor(ln_sigma_mean, device=self.device) + self._ln_sigma_std = torch.tensor(ln_sigma_std, device=self.device) + self._mag_mean = torch.tensor(mag_mean, device=self.device) + self._mag_std = torch.tensor(mag_std, device=self.device) + + ln_sigma_norm = (ln_sigma - ln_sigma_mean) / (ln_sigma_std + 1e-8) + mag_norm = (magnitudes - mag_mean) / (mag_std + 1e-8) + + N = len(ln_sigma_norm) + n_val = max(1, int(N * valid_fraction)) + rng = np.random.default_rng() + idx = rng.permutation(N) + val_idx, train_idx = idx[:n_val], idx[n_val:] + + x_train = torch.tensor( + ln_sigma_norm[train_idx], dtype=torch.float32, device=self.device + ) + m_train = torch.tensor( + mag_norm[train_idx], dtype=torch.float32, device=self.device + ) + x_val = torch.tensor( + ln_sigma_norm[val_idx], dtype=torch.float32, device=self.device + ) + m_val = torch.tensor( + mag_norm[val_idx], dtype=torch.float32, device=self.device + ) + + optimizer = torch.optim.Adam(self.score_net.parameters(), lr=learning_rate) + + history: Dict[str, List[float]] = {"train_loss": [], "val_loss": []} + best_val_loss = float("inf") + best_state: Dict[str, torch.Tensor] = {} + patience_counter = 0 + + for epoch in range(n_epochs): + self.score_net.train() + perm = torch.randperm(len(x_train), device=self.device) + x_ep, m_ep = x_train[perm], m_train[perm] + + epoch_loss = 0.0 + n_batches = 0 + for i in range(0, len(x_ep), batch_size): + x0 = x_ep[i : i + batch_size] + m = m_ep[i : i + batch_size] + B = len(x0) + + t = torch.rand(B, device=self.device) * (self.T - t_eps) + t_eps + s, sigma = self._marginal_params(t) + eps = torch.randn_like(x0) + x_t = s.unsqueeze(-1) * x0 + sigma.unsqueeze(-1) * eps + + score = self.score_net(x_t, t, m) + # DSM loss: E[‖sχ + ε/σ‖²] + target = -eps / sigma.unsqueeze(-1) + loss = ((score - target) ** 2).mean() + + optimizer.zero_grad() + loss.backward() + optimizer.step() + + epoch_loss += loss.item() + n_batches += 1 + + train_loss = epoch_loss / max(n_batches, 1) + history["train_loss"].append(train_loss) + + if epoch % 10 == 0: + self.score_net.eval() + with torch.no_grad(): + t_v = ( + torch.rand(len(x_val), device=self.device) + * (self.T - t_eps) + + t_eps + ) + s_v, sigma_v = self._marginal_params(t_v) + eps_v = torch.randn_like(x_val) + x_t_v = s_v.unsqueeze(-1) * x_val + sigma_v.unsqueeze(-1) * eps_v + score_v = self.score_net(x_t_v, t_v, m_val) + val_loss = ( + ((score_v + eps_v / sigma_v.unsqueeze(-1)) ** 2).mean().item() + ) + + history["val_loss"].append(val_loss) + + if verbose: + print( + f"Epoch {epoch:5d}/{n_epochs} " + f"train={train_loss:.4f} val={val_loss:.4f}" + ) + + if val_loss < best_val_loss: + best_val_loss = val_loss + best_state = { + k: v.clone() for k, v in self.score_net.state_dict().items() + } + patience_counter = 0 + else: + patience_counter += 1 + if patience_counter >= patience: + if verbose: + print(f"Early stopping at epoch {epoch}.") + break + + if best_state: + self.score_net.load_state_dict(best_state) + self._is_trained = True + return history + + # ------------------------------------------------------------------ + # Inference + # ------------------------------------------------------------------ + + @torch.no_grad() + def sample_uncertainty( + self, + magnitudes: np.ndarray, + n_samples: int = 1, + n_steps: Optional[int] = None, + t_eps: float = 1e-3, + ) -> np.ndarray: + """Sample flux uncertainties via the reverse VP-SDE. + + Args: + magnitudes: (N, n_filters) or (n_filters,) AB magnitudes. + n_samples: Number of independent samples per input row. + n_steps: Euler-Maruyama steps (default: self.n_sampling_steps). + t_eps: Minimum time to stop the reverse SDE at. + + Returns: + sigma: Flux uncertainties in the units used during training. + Shape (N, n_samples, n_filters) when n_samples > 1, + (N, n_filters) when n_samples=1, or (n_filters,) for a + scalar (1-row) input with n_samples=1. + """ + if not self._is_trained: + raise RuntimeError( + "Model must be trained before sampling. Call fit() first." + ) + + scalar_input = magnitudes.ndim == 1 + if scalar_input: + magnitudes = magnitudes[np.newaxis] + + N = len(magnitudes) + if n_steps is None: + n_steps = self.n_sampling_steps + + mag_t = torch.tensor(magnitudes, dtype=torch.float32, device=self.device) + mag_norm = self._normalise_mag(mag_t) + + # Tile conditioning for n_samples + mag_norm = mag_norm.repeat_interleave(n_samples, dim=0) # (N*S, F) + + # Initialise from the base density p(x(T)) ≈ N(0, I) + x = torch.randn(N * n_samples, self.n_filters, device=self.device) + + self.score_net.eval() + + dt = (self.T - t_eps) / n_steps # positive step magnitude + times = torch.linspace(self.T, t_eps, n_steps + 1, device=self.device) + + for i in range(n_steps): + t_val = times[i].item() + t_tensor = torch.full( + (N * n_samples,), t_val, device=self.device, dtype=torch.float32 + ) + beta_t = self._beta(t_tensor) # (N*S,) + score = self.score_net(x, t_tensor, mag_norm) + + # Reverse SDE Euler-Maruyama step (going backward from T to 0): + # x_{t−dt} = x + [β/2·x + β·score]·dt + √(β·dt)·z + drift = beta_t.unsqueeze(-1) / 2 * x + beta_t.unsqueeze(-1) * score + z = torch.randn_like(x) + x = x + drift * dt + torch.sqrt(beta_t * dt).unsqueeze(-1) * z + + # Denormalise and exponentiate to recover σ + ln_sigma = self._denormalise_ln_sigma(x) + sigma = torch.exp(ln_sigma) + sigma_np = sigma.cpu().numpy().reshape(N, n_samples, self.n_filters) + + if n_samples == 1: + sigma_np = sigma_np[:, 0, :] # (N, F) + + return sigma_np[0] if scalar_input else sigma_np + + def apply_noise( + self, + flux: np.ndarray, + magnitudes: np.ndarray, + n_steps: Optional[int] = None, + **kwargs: Any, + ) -> Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]: + """Sample uncertainties and apply Gaussian noise to flux. + + Args: + flux: (N, n_filters) true flux array in the same units as training. + magnitudes: (N, n_filters) calibrated model AB magnitudes used as + the conditioning variable m. + n_steps: Reverse SDE steps for uncertainty sampling. + + Returns: + noisy_flux of shape (N, n_filters), or (noisy_flux, sigma) when + self.return_noise is True. + """ + if kwargs: + warnings.warn( + f"ScoreBasedUncertaintyModel.apply_noise received unexpected " + f"keyword arguments {list(kwargs.keys())} which will be ignored." + ) + + sigma = self.sample_uncertainty( + magnitudes, n_samples=1, n_steps=n_steps + ) # (N, n_filters) + noise = np.random.normal(0.0, sigma) + noisy_flux = flux + noise + + if self.return_noise: + return noisy_flux, sigma + return noisy_flux + + # ------------------------------------------------------------------ + # Serialisation + # ------------------------------------------------------------------ + + def serialize_to_hdf5(self, hdf5_group: h5py.Group) -> None: + """Saves the model hyperparameters, normalisation stats, and weights.""" + attrs = hdf5_group.attrs + attrs["__class__"] = self.__class__.__name__ + attrs["n_filters"] = self.n_filters + attrs["hidden_dim"] = self.hidden_dim + attrs["n_layers"] = self.n_layers + attrs["time_embed_dim"] = self.time_embed_dim + attrs["beta_min"] = self.beta_min + attrs["beta_max"] = self.beta_max + attrs["T"] = self.T + attrs["n_sampling_steps"] = self.n_sampling_steps + attrs["return_noise"] = self.return_noise + attrs["is_trained"] = self._is_trained + + if not self._is_trained: + return + + hdf5_group.create_dataset( + "ln_sigma_mean", data=self._ln_sigma_mean.cpu().numpy() + ) + hdf5_group.create_dataset( + "ln_sigma_std", data=self._ln_sigma_std.cpu().numpy() + ) + hdf5_group.create_dataset("mag_mean", data=self._mag_mean.cpu().numpy()) + hdf5_group.create_dataset("mag_std", data=self._mag_std.cpu().numpy()) + + weights_group = hdf5_group.create_group("score_network_weights") + for key, tensor in self.score_net.state_dict().items(): + # Replace "." with "__" so the key is a valid flat HDF5 dataset name + safe_key = key.replace(".", "__") + weights_group.create_dataset(safe_key, data=tensor.cpu().numpy()) + + @classmethod + def _from_hdf5_group( + cls, hdf5_group: h5py.Group + ) -> "ScoreBasedUncertaintyModel": + """Loads a ScoreBasedUncertaintyModel from an HDF5 group.""" + attrs = hdf5_group.attrs + instance = cls( + n_filters=int(attrs["n_filters"]), + hidden_dim=int(attrs["hidden_dim"]), + n_layers=int(attrs["n_layers"]), + time_embed_dim=int(attrs["time_embed_dim"]), + beta_min=float(attrs["beta_min"]), + beta_max=float(attrs["beta_max"]), + T=float(attrs["T"]), + n_sampling_steps=int(attrs["n_sampling_steps"]), + return_noise=bool(attrs["return_noise"]), + ) + + if not attrs.get("is_trained", False): + return instance + + dev = instance.device + instance._ln_sigma_mean = torch.tensor( + hdf5_group["ln_sigma_mean"][:], dtype=torch.float32, device=dev + ) + instance._ln_sigma_std = torch.tensor( + hdf5_group["ln_sigma_std"][:], dtype=torch.float32, device=dev + ) + instance._mag_mean = torch.tensor( + hdf5_group["mag_mean"][:], dtype=torch.float32, device=dev + ) + instance._mag_std = torch.tensor( + hdf5_group["mag_std"][:], dtype=torch.float32, device=dev + ) + + state_dict: Dict[str, torch.Tensor] = {} + weights_group = hdf5_group["score_network_weights"] + for safe_key in weights_group.keys(): + original_key = safe_key.replace("__", ".") + state_dict[original_key] = torch.tensor( + weights_group[safe_key][:], dtype=torch.float32 + ) + instance.score_net.load_state_dict(state_dict) + instance.score_net.to(dev) + instance._is_trained = True + return instance + + # ============================================================================= # SERIALIZATION FACTORY FUNCTIONS # ============================================================================= @@ -1107,6 +1653,7 @@ def apply_scalings( "DepthUncertaintyModel": DepthUncertaintyModel, "AsinhEmpiricalUncertaintyModel": AsinhEmpiricalUncertaintyModel, "GeneralEmpiricalUncertaintyModel": GeneralEmpiricalUncertaintyModel, + "ScoreBasedUncertaintyModel": ScoreBasedUncertaintyModel, } From 9f38413e443afafc94be24e6529dea0c4793eb12 Mon Sep 17 00:00:00 2001 From: tHarvey303 Date: Sat, 2 May 2026 00:54:47 +0100 Subject: [PATCH 02/22] Improved noise model architecture. --- src/synference/noise_models.py | 116 ++++++++++++--------------------- 1 file changed, 42 insertions(+), 74 deletions(-) diff --git a/src/synference/noise_models.py b/src/synference/noise_models.py index cf9ba4f..7732085 100644 --- a/src/synference/noise_models.py +++ b/src/synference/noise_models.py @@ -1144,9 +1144,7 @@ def __init__( layers.append(nn.Linear(hidden_dim, n_filters)) self.net = nn.Sequential(*layers) - def forward( - self, x: torch.Tensor, t: torch.Tensor, m: torch.Tensor - ) -> torch.Tensor: + def forward(self, x: torch.Tensor, t: torch.Tensor, m: torch.Tensor) -> torch.Tensor: """Compute score estimate sχ(x, t; m). Args: @@ -1205,6 +1203,7 @@ def __init__( device: Optional[str] = None, **kwargs: Any, ): + """Initializes the score-based uncertainty model.""" super().__init__(**kwargs) self.n_filters = n_filters self.hidden_dim = hidden_dim @@ -1219,9 +1218,7 @@ def __init__( device = "cuda" if torch.cuda.is_available() else "cpu" self.device = device - self.score_net = _ScoreNetwork( - n_filters, hidden_dim, n_layers, time_embed_dim - ).to(device) + self.score_net = _ScoreNetwork(n_filters, hidden_dim, n_layers, time_embed_dim).to(device) # Normalisation statistics (populated during fit) self._ln_sigma_mean: Optional[torch.Tensor] = None @@ -1240,13 +1237,9 @@ def _beta(self, t: torch.Tensor) -> torch.Tensor: def _alpha(self, t: torch.Tensor) -> torch.Tensor: """Cumulative noise ∫₀ᵗ β(s)ds.""" - return self.beta_min * t + (t**2 / (2.0 * self.T)) * ( - self.beta_max - self.beta_min - ) + return self.beta_min * t + (t**2 / (2.0 * self.T)) * (self.beta_max - self.beta_min) - def _marginal_params( - self, t: torch.Tensor - ) -> Tuple[torch.Tensor, torch.Tensor]: + def _marginal_params(self, t: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: """Transition kernel parameters p(xₜ|x₀) = N(s(t)x₀, σ²(t)I). Returns: @@ -1281,8 +1274,8 @@ def fit( flux_uncertainties: np.ndarray, n_epochs: int = 2000, batch_size: int = 512, - learning_rate: float = 1e-3, - t_eps: float = 1e-3, + learning_rate: float = 3e-4, + t_eps: float = 1e-2, valid_fraction: float = 0.1, patience: int = 50, verbose: bool = True, @@ -1311,23 +1304,16 @@ def fit( History dict with keys ``"train_loss"`` and ``"val_loss"``. """ if magnitudes.ndim != 2 or magnitudes.shape[1] != self.n_filters: - raise ValueError( - f"magnitudes must be (N, {self.n_filters}), got {magnitudes.shape}" - ) + raise ValueError(f"magnitudes must be (N, {self.n_filters}), got {magnitudes.shape}") if flux_uncertainties.shape != magnitudes.shape: - raise ValueError( - "flux_uncertainties must have the same shape as magnitudes." - ) + raise ValueError("flux_uncertainties must have the same shape as magnitudes.") if np.any(flux_uncertainties <= 0): warnings.warn( - "flux_uncertainties contains non-positive values; " - "these rows will be discarded." + "flux_uncertainties contains non-positive values; these rows will be discarded." ) ln_sigma = np.log(flux_uncertainties) - valid = np.all( - np.isfinite(ln_sigma) & np.isfinite(magnitudes), axis=1 - ) + valid = np.all(np.isfinite(ln_sigma) & np.isfinite(magnitudes), axis=1) magnitudes = magnitudes[valid] ln_sigma = ln_sigma[valid] @@ -1354,20 +1340,15 @@ def fit( idx = rng.permutation(N) val_idx, train_idx = idx[:n_val], idx[n_val:] - x_train = torch.tensor( - ln_sigma_norm[train_idx], dtype=torch.float32, device=self.device - ) - m_train = torch.tensor( - mag_norm[train_idx], dtype=torch.float32, device=self.device - ) - x_val = torch.tensor( - ln_sigma_norm[val_idx], dtype=torch.float32, device=self.device - ) - m_val = torch.tensor( - mag_norm[val_idx], dtype=torch.float32, device=self.device - ) + x_train = torch.tensor(ln_sigma_norm[train_idx], dtype=torch.float32, device=self.device) + m_train = torch.tensor(mag_norm[train_idx], dtype=torch.float32, device=self.device) + x_val = torch.tensor(ln_sigma_norm[val_idx], dtype=torch.float32, device=self.device) + m_val = torch.tensor(mag_norm[val_idx], dtype=torch.float32, device=self.device) optimizer = torch.optim.Adam(self.score_net.parameters(), lr=learning_rate) + scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau( + optimizer, factor=0.5, patience=10, min_lr=1e-5 + ) history: Dict[str, List[float]] = {"train_loss": [], "val_loss": []} best_val_loss = float("inf") @@ -1392,12 +1373,15 @@ def fit( x_t = s.unsqueeze(-1) * x0 + sigma.unsqueeze(-1) * eps score = self.score_net(x_t, t, m) - # DSM loss: E[‖sχ + ε/σ‖²] + # σ²-weighted DSM loss (Song et al. 2021 eq. 7); upweights + # large-t and bounds the contribution from small-t where + # the unweighted target -ε/σ diverges. target = -eps / sigma.unsqueeze(-1) - loss = ((score - target) ** 2).mean() + loss = (sigma.unsqueeze(-1) ** 2 * (score - target) ** 2).mean() optimizer.zero_grad() loss.backward() + torch.nn.utils.clip_grad_norm_(self.score_net.parameters(), max_norm=1.0) optimizer.step() epoch_loss += loss.item() @@ -1409,32 +1393,31 @@ def fit( if epoch % 10 == 0: self.score_net.eval() with torch.no_grad(): - t_v = ( - torch.rand(len(x_val), device=self.device) - * (self.T - t_eps) - + t_eps - ) + t_v = torch.rand(len(x_val), device=self.device) * (self.T - t_eps) + t_eps s_v, sigma_v = self._marginal_params(t_v) eps_v = torch.randn_like(x_val) x_t_v = s_v.unsqueeze(-1) * x_val + sigma_v.unsqueeze(-1) * eps_v score_v = self.score_net(x_t_v, t_v, m_val) val_loss = ( - ((score_v + eps_v / sigma_v.unsqueeze(-1)) ** 2).mean().item() + ( + sigma_v.unsqueeze(-1) ** 2 + * (score_v + eps_v / sigma_v.unsqueeze(-1)) ** 2 + ) + .mean() + .item() ) history["val_loss"].append(val_loss) + scheduler.step(val_loss) if verbose: print( - f"Epoch {epoch:5d}/{n_epochs} " - f"train={train_loss:.4f} val={val_loss:.4f}" + f"Epoch {epoch:5d}/{n_epochs} train={train_loss:.4f} val={val_loss:.4f}" ) if val_loss < best_val_loss: best_val_loss = val_loss - best_state = { - k: v.clone() for k, v in self.score_net.state_dict().items() - } + best_state = {k: v.clone() for k, v in self.score_net.state_dict().items()} patience_counter = 0 else: patience_counter += 1 @@ -1475,9 +1458,7 @@ def sample_uncertainty( scalar (1-row) input with n_samples=1. """ if not self._is_trained: - raise RuntimeError( - "Model must be trained before sampling. Call fit() first." - ) + raise RuntimeError("Model must be trained before sampling. Call fit() first.") scalar_input = magnitudes.ndim == 1 if scalar_input: @@ -1503,9 +1484,7 @@ def sample_uncertainty( for i in range(n_steps): t_val = times[i].item() - t_tensor = torch.full( - (N * n_samples,), t_val, device=self.device, dtype=torch.float32 - ) + t_tensor = torch.full((N * n_samples,), t_val, device=self.device, dtype=torch.float32) beta_t = self._beta(t_tensor) # (N*S,) score = self.score_net(x, t_tensor, mag_norm) @@ -1539,6 +1518,7 @@ def apply_noise( magnitudes: (N, n_filters) calibrated model AB magnitudes used as the conditioning variable m. n_steps: Reverse SDE steps for uncertainty sampling. + kwargs: Ignored, but accepted for compatibility with the base class. Returns: noisy_flux of shape (N, n_filters), or (noisy_flux, sigma) when @@ -1550,9 +1530,7 @@ def apply_noise( f"keyword arguments {list(kwargs.keys())} which will be ignored." ) - sigma = self.sample_uncertainty( - magnitudes, n_samples=1, n_steps=n_steps - ) # (N, n_filters) + sigma = self.sample_uncertainty(magnitudes, n_samples=1, n_steps=n_steps) # (N, n_filters) noise = np.random.normal(0.0, sigma) noisy_flux = flux + noise @@ -1582,12 +1560,8 @@ def serialize_to_hdf5(self, hdf5_group: h5py.Group) -> None: if not self._is_trained: return - hdf5_group.create_dataset( - "ln_sigma_mean", data=self._ln_sigma_mean.cpu().numpy() - ) - hdf5_group.create_dataset( - "ln_sigma_std", data=self._ln_sigma_std.cpu().numpy() - ) + hdf5_group.create_dataset("ln_sigma_mean", data=self._ln_sigma_mean.cpu().numpy()) + hdf5_group.create_dataset("ln_sigma_std", data=self._ln_sigma_std.cpu().numpy()) hdf5_group.create_dataset("mag_mean", data=self._mag_mean.cpu().numpy()) hdf5_group.create_dataset("mag_std", data=self._mag_std.cpu().numpy()) @@ -1598,9 +1572,7 @@ def serialize_to_hdf5(self, hdf5_group: h5py.Group) -> None: weights_group.create_dataset(safe_key, data=tensor.cpu().numpy()) @classmethod - def _from_hdf5_group( - cls, hdf5_group: h5py.Group - ) -> "ScoreBasedUncertaintyModel": + def _from_hdf5_group(cls, hdf5_group: h5py.Group) -> "ScoreBasedUncertaintyModel": """Loads a ScoreBasedUncertaintyModel from an HDF5 group.""" attrs = hdf5_group.attrs instance = cls( @@ -1628,17 +1600,13 @@ def _from_hdf5_group( instance._mag_mean = torch.tensor( hdf5_group["mag_mean"][:], dtype=torch.float32, device=dev ) - instance._mag_std = torch.tensor( - hdf5_group["mag_std"][:], dtype=torch.float32, device=dev - ) + instance._mag_std = torch.tensor(hdf5_group["mag_std"][:], dtype=torch.float32, device=dev) state_dict: Dict[str, torch.Tensor] = {} weights_group = hdf5_group["score_network_weights"] for safe_key in weights_group.keys(): original_key = safe_key.replace("__", ".") - state_dict[original_key] = torch.tensor( - weights_group[safe_key][:], dtype=torch.float32 - ) + state_dict[original_key] = torch.tensor(weights_group[safe_key][:], dtype=torch.float32) instance.score_net.load_state_dict(state_dict) instance.score_net.to(dev) instance._is_trained = True From 0c466ffbd645e53f348d65e05ba9c6a9dd4db9c9 Mon Sep 17 00:00:00 2001 From: tHarvey303 Date: Sat, 2 May 2026 01:24:35 +0100 Subject: [PATCH 03/22] balance validation loss score --- src/synference/noise_models.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/synference/noise_models.py b/src/synference/noise_models.py index 7732085..338fd53 100644 --- a/src/synference/noise_models.py +++ b/src/synference/noise_models.py @@ -1393,19 +1393,18 @@ def fit( if epoch % 10 == 0: self.score_net.eval() with torch.no_grad(): - t_v = torch.rand(len(x_val), device=self.device) * (self.T - t_eps) + t_eps - s_v, sigma_v = self._marginal_params(t_v) - eps_v = torch.randn_like(x_val) - x_t_v = s_v.unsqueeze(-1) * x_val + sigma_v.unsqueeze(-1) * eps_v - score_v = self.score_net(x_t_v, t_v, m_val) - val_loss = ( - ( + val_loss = 0.0 + for _ in range(5): + t_v = torch.rand(len(x_val), device=self.device) * (self.T - t_eps) + t_eps + s_v, sigma_v = self._marginal_params(t_v) + eps_v = torch.randn_like(x_val) + x_t_v = s_v.unsqueeze(-1) * x_val + sigma_v.unsqueeze(-1) * eps_v + score_v = self.score_net(x_t_v, t_v, m_val) + val_loss += ( sigma_v.unsqueeze(-1) ** 2 * (score_v + eps_v / sigma_v.unsqueeze(-1)) ** 2 - ) - .mean() - .item() - ) + ).mean().item() + val_loss /= 5 history["val_loss"].append(val_loss) scheduler.step(val_loss) From fc63a26e4e2927a202a41741f92fb5fa0e082550 Mon Sep 17 00:00:00 2001 From: Thomas Harvey Date: Tue, 5 May 2026 14:43:07 +0100 Subject: [PATCH 04/22] Improving noise models --- src/synference/noise_models.py | 410 ++++++++++++--------------------- 1 file changed, 147 insertions(+), 263 deletions(-) diff --git a/src/synference/noise_models.py b/src/synference/noise_models.py index 338fd53..e390ac5 100644 --- a/src/synference/noise_models.py +++ b/src/synference/noise_models.py @@ -12,7 +12,9 @@ import matplotlib.pyplot as plt import numpy as np import torch +import math import torch.nn as nn +from torch.optim.swa_utils import AveragedModel, get_ema_multi_avg_fn from astropy.table import Table from scipy import stats from scipy.interpolate import interp1d @@ -1106,105 +1108,76 @@ def apply_scalings( # ============================================================================= -class _ScoreNetwork(nn.Module): - """Conditional score network sχ(x, t; m) for VP-SDE. +class GaussianFourierProjection(nn.Module): + def __init__(self, embed_dim: int, scale: float = 30.0): + super().__init__() + self.W = nn.Parameter(torch.randn(embed_dim // 2) * scale, requires_grad=False) + + def forward(self, x: torch.Tensor) -> torch.Tensor: + x_proj = x * self.W[None, :] * 2 * math.pi + return torch.cat([torch.sin(x_proj), torch.cos(x_proj)], dim=-1) - Five-layer densely-connected network with 256 units per layer and tanh - activations (Y. Song et al. 2021). The diffusion time t is embedded via a - two-layer MLP and concatenated with the noisy log-uncertainty x and the - conditioning magnitudes m before being passed through the main trunk. - Args: - n_filters: Dimensionality of x (and m). - hidden_dim: Width of each hidden layer. - n_layers: Number of hidden layers (paper: 5). - time_embed_dim: Dimension of the time embedding. - """ +class _ResidualBlock(nn.Module): + def __init__(self, dim: int): + super().__init__() + self.linear = nn.Linear(dim, dim) + self.act = nn.SiLU() + + def forward(self, x: torch.Tensor) -> torch.Tensor: + return x + self.linear(self.act(x)) + +class _RobustScoreNetwork(nn.Module): def __init__( self, n_filters: int, hidden_dim: int = 256, n_layers: int = 5, - time_embed_dim: int = 32, + time_embed_dim: int = 64, ): super().__init__() self.time_embed = nn.Sequential( - nn.Linear(1, time_embed_dim), - nn.Tanh(), + GaussianFourierProjection(embed_dim=time_embed_dim), nn.Linear(time_embed_dim, time_embed_dim), - nn.Tanh(), + nn.SiLU() + ) + + input_dim = n_filters * 2 + time_embed_dim + self.proj_in = nn.Linear(input_dim, hidden_dim) + + self.res_blocks = nn.ModuleList([ + _ResidualBlock(hidden_dim) for _ in range(n_layers) + ]) + + self.proj_out = nn.Sequential( + nn.SiLU(), + nn.Linear(hidden_dim, n_filters) ) - input_dim = n_filters + n_filters + time_embed_dim - layers: List[nn.Module] = [] - in_dim = input_dim - for _ in range(n_layers): - layers += [nn.Linear(in_dim, hidden_dim), nn.Tanh()] - in_dim = hidden_dim - layers.append(nn.Linear(hidden_dim, n_filters)) - self.net = nn.Sequential(*layers) def forward(self, x: torch.Tensor, t: torch.Tensor, m: torch.Tensor) -> torch.Tensor: - """Compute score estimate sχ(x, t; m). - - Args: - x: Noisy log-uncertainty (batch, n_filters). - t: Diffusion time, shape (batch,). - m: Conditioning magnitudes (batch, n_filters). - - Returns: - Score estimate, shape (batch, n_filters). - """ - t_emb = self.time_embed(t.float().view(-1, 1)) - return self.net(torch.cat([x, m, t_emb], dim=-1)) - - -class ScoreBasedUncertaintyModel(UncertaintyModel): - """Multi-filter photometric uncertainty model via VP-SDE score diffusion. - - Implements the model P(ln σ_P | m) from A24, where σ_P is the per-filter - flux uncertainty and m is the vector of calibrated model magnitudes. A - conditional score network sχ(x, t; m) is trained by denoising score - matching (Y. Song & S. Ermon 2019; Y. Song et al. 2021) using a VP SDE: - - dx = −½ β(t) x dt + √β(t) dw, β(t) = βmin + (t/T)(βmax − βmin) - - with βmin = 0.1, βmax = 20.0, T = 1.0 (J. Ho et al. 2020). - - The network is a five-layer densely-connected network with 256 units per - layer and tanh activations, optimised with Adam (D. P. Kingma & J. Ba 2015). + t_emb = self.time_embed(t.unsqueeze(-1)) + h = self.proj_in(torch.cat([x, m, t_emb], dim=-1)) + + for block in self.res_blocks: + h = block(h) + + return self.proj_out(h) - Unlike the per-filter empirical models, this model operates on ALL filters - jointly, capturing cross-filter correlations in the noise. - - Args: - n_filters: Number of photometric filters. - hidden_dim: Hidden layer width (paper: 256). - n_layers: Number of hidden layers (paper: 5). - time_embed_dim: Dimension of the time-embedding MLP output. - beta_min: Minimum noise schedule value β_min (paper: 0.1). - beta_max: Maximum noise schedule value β_max (paper: 20.0). - T: Terminal diffusion time (paper: 1.0). - n_sampling_steps: Euler-Maruyama steps for the reverse SDE. - device: PyTorch device string. Defaults to CUDA if available. - return_noise: If True, apply_noise returns (noisy_flux, sigma). - """ +class ScoreBasedUncertaintyModel: def __init__( self, n_filters: int, hidden_dim: int = 256, n_layers: int = 5, - time_embed_dim: int = 32, + time_embed_dim: int = 64, beta_min: float = 0.1, beta_max: float = 20.0, T: float = 1.0, - n_sampling_steps: int = 500, device: Optional[str] = None, - **kwargs: Any, + return_noise: bool = False, ): - """Initializes the score-based uncertainty model.""" - super().__init__(**kwargs) self.n_filters = n_filters self.hidden_dim = hidden_dim self.n_layers = n_layers @@ -1212,67 +1185,50 @@ def __init__( self.beta_min = beta_min self.beta_max = beta_max self.T = T - self.n_sampling_steps = n_sampling_steps + self.return_noise = return_noise - if device is None: - device = "cuda" if torch.cuda.is_available() else "cpu" - self.device = device + self.device = device or ("cuda" if torch.cuda.is_available() else "cpu") - self.score_net = _ScoreNetwork(n_filters, hidden_dim, n_layers, time_embed_dim).to(device) + self.score_net = _RobustScoreNetwork( + n_filters, hidden_dim, n_layers, time_embed_dim + ).to(self.device) + + self.ema_net = AveragedModel( + self.score_net, multi_avg_fn=get_ema_multi_avg_fn(0.999) + ) - # Normalisation statistics (populated during fit) - self._ln_sigma_mean: Optional[torch.Tensor] = None - self._ln_sigma_std: Optional[torch.Tensor] = None - self._mag_mean: Optional[torch.Tensor] = None - self._mag_std: Optional[torch.Tensor] = None + self._ln_sigma_median: Optional[torch.Tensor] = None + self._ln_sigma_iqr: Optional[torch.Tensor] = None + self._mag_median: Optional[torch.Tensor] = None + self._mag_iqr: Optional[torch.Tensor] = None self._is_trained: bool = False - # ------------------------------------------------------------------ - # VP-SDE helpers - # ------------------------------------------------------------------ - def _beta(self, t: torch.Tensor) -> torch.Tensor: - """Noise schedule β(t) = βmin + (t/T)(βmax − βmin).""" return self.beta_min + (t / self.T) * (self.beta_max - self.beta_min) def _alpha(self, t: torch.Tensor) -> torch.Tensor: - """Cumulative noise ∫₀ᵗ β(s)ds.""" return self.beta_min * t + (t**2 / (2.0 * self.T)) * (self.beta_max - self.beta_min) def _marginal_params(self, t: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: - """Transition kernel parameters p(xₜ|x₀) = N(s(t)x₀, σ²(t)I). - - Returns: - s: Scale factor exp(−α(t)/2), shape matching t. - sigma: Standard deviation √(1 − exp(−α(t))), shape matching t. - """ A = self._alpha(t) s = torch.exp(-0.5 * A) sigma = torch.sqrt(torch.clamp(1.0 - torch.exp(-A), min=1e-8)) return s, sigma - # ------------------------------------------------------------------ - # Normalisation helpers - # ------------------------------------------------------------------ - def _normalise_ln_sigma(self, ln_sigma: torch.Tensor) -> torch.Tensor: - return (ln_sigma - self._ln_sigma_mean) / (self._ln_sigma_std + 1e-8) + return (ln_sigma - self._ln_sigma_median) / (self._ln_sigma_iqr + 1e-8) def _denormalise_ln_sigma(self, z: torch.Tensor) -> torch.Tensor: - return z * self._ln_sigma_std + self._ln_sigma_mean + return z * self._ln_sigma_iqr + self._ln_sigma_median def _normalise_mag(self, mag: torch.Tensor) -> torch.Tensor: - return (mag - self._mag_mean) / (self._mag_std + 1e-8) - - # ------------------------------------------------------------------ - # Training - # ------------------------------------------------------------------ + return (mag - self._mag_median) / (self._mag_iqr + 1e-8) def fit( self, magnitudes: np.ndarray, flux_uncertainties: np.ndarray, - n_epochs: int = 2000, + n_epochs: int = 1000, batch_size: int = 512, learning_rate: float = 3e-4, t_eps: float = 1e-2, @@ -1280,64 +1236,32 @@ def fit( patience: int = 50, verbose: bool = True, ) -> Dict[str, List[float]]: - """Train the score network on catalog (magnitudes, flux uncertainties). - - The model learns P(ln σ_P | m) via denoising score matching. Inputs - are standardised per-filter before training; the statistics are stored - for use at inference time. - - Args: - magnitudes: (N, n_filters) AB magnitudes (calibrated model mags). - flux_uncertainties: (N, n_filters) flux uncertainties in Jy - (must be strictly positive). - n_epochs: Maximum number of training epochs. - batch_size: Mini-batch size. - learning_rate: Adam initial learning rate. - t_eps: Minimum diffusion time (avoids t → 0 singularity). - valid_fraction: Fraction of data held out for validation / - early stopping. - patience: Early-stopping patience measured in validation-check - intervals (one check every 10 epochs). - verbose: Print training progress. - - Returns: - History dict with keys ``"train_loss"`` and ``"val_loss"``. - """ if magnitudes.ndim != 2 or magnitudes.shape[1] != self.n_filters: - raise ValueError(f"magnitudes must be (N, {self.n_filters}), got {magnitudes.shape}") - if flux_uncertainties.shape != magnitudes.shape: - raise ValueError("flux_uncertainties must have the same shape as magnitudes.") - if np.any(flux_uncertainties <= 0): - warnings.warn( - "flux_uncertainties contains non-positive values; these rows will be discarded." - ) + raise ValueError(f"magnitudes must be (N, {self.n_filters})") - ln_sigma = np.log(flux_uncertainties) - valid = np.all(np.isfinite(ln_sigma) & np.isfinite(magnitudes), axis=1) - magnitudes = magnitudes[valid] - ln_sigma = ln_sigma[valid] + valid_mask = np.all((flux_uncertainties > 0) & np.isfinite(magnitudes), axis=1) + if not np.any(valid_mask): + raise ValueError("No valid training samples after filtering.") - if len(magnitudes) < 2: - raise ValueError("Too few valid training samples after filtering.") + magnitudes = magnitudes[valid_mask] + ln_sigma = np.log(flux_uncertainties[valid_mask]) - # Per-filter normalisation statistics - ln_sigma_mean = ln_sigma.mean(axis=0).astype(np.float32) - ln_sigma_std = ln_sigma.std(axis=0).astype(np.float32) - mag_mean = magnitudes.mean(axis=0).astype(np.float32) - mag_std = magnitudes.std(axis=0).astype(np.float32) + ln_sigma_median = np.median(ln_sigma, axis=0).astype(np.float32) + ln_sigma_iqr = (np.percentile(ln_sigma, 75, axis=0) - np.percentile(ln_sigma, 25, axis=0)).astype(np.float32) + mag_median = np.median(magnitudes, axis=0).astype(np.float32) + mag_iqr = (np.percentile(magnitudes, 75, axis=0) - np.percentile(magnitudes, 25, axis=0)).astype(np.float32) - self._ln_sigma_mean = torch.tensor(ln_sigma_mean, device=self.device) - self._ln_sigma_std = torch.tensor(ln_sigma_std, device=self.device) - self._mag_mean = torch.tensor(mag_mean, device=self.device) - self._mag_std = torch.tensor(mag_std, device=self.device) + self._ln_sigma_median = torch.tensor(ln_sigma_median, device=self.device) + self._ln_sigma_iqr = torch.tensor(ln_sigma_iqr, device=self.device) + self._mag_median = torch.tensor(mag_median, device=self.device) + self._mag_iqr = torch.tensor(mag_iqr, device=self.device) - ln_sigma_norm = (ln_sigma - ln_sigma_mean) / (ln_sigma_std + 1e-8) - mag_norm = (magnitudes - mag_mean) / (mag_std + 1e-8) + ln_sigma_norm = (ln_sigma - ln_sigma_median) / (ln_sigma_iqr + 1e-8) + mag_norm = (magnitudes - mag_median) / (mag_iqr + 1e-8) N = len(ln_sigma_norm) n_val = max(1, int(N * valid_fraction)) - rng = np.random.default_rng() - idx = rng.permutation(N) + idx = np.random.permutation(N) val_idx, train_idx = idx[:n_val], idx[n_val:] x_train = torch.tensor(ln_sigma_norm[train_idx], dtype=torch.float32, device=self.device) @@ -1373,9 +1297,6 @@ def fit( x_t = s.unsqueeze(-1) * x0 + sigma.unsqueeze(-1) * eps score = self.score_net(x_t, t, m) - # σ²-weighted DSM loss (Song et al. 2021 eq. 7); upweights - # large-t and bounds the contribution from small-t where - # the unweighted target -ε/σ diverges. target = -eps / sigma.unsqueeze(-1) loss = (sigma.unsqueeze(-1) ** 2 * (score - target) ** 2).mean() @@ -1383,6 +1304,7 @@ def fit( loss.backward() torch.nn.utils.clip_grad_norm_(self.score_net.parameters(), max_norm=1.0) optimizer.step() + self.ema_net.update_parameters(self.score_net) epoch_loss += loss.item() n_batches += 1 @@ -1391,32 +1313,41 @@ def fit( history["train_loss"].append(train_loss) if epoch % 10 == 0: - self.score_net.eval() + self.ema_net.eval() with torch.no_grad(): val_loss = 0.0 - for _ in range(5): - t_v = torch.rand(len(x_val), device=self.device) * (self.T - t_eps) + t_eps - s_v, sigma_v = self._marginal_params(t_v) - eps_v = torch.randn_like(x_val) - x_t_v = s_v.unsqueeze(-1) * x_val + sigma_v.unsqueeze(-1) * eps_v - score_v = self.score_net(x_t_v, t_v, m_val) - val_loss += ( - sigma_v.unsqueeze(-1) ** 2 - * (score_v + eps_v / sigma_v.unsqueeze(-1)) ** 2 - ).mean().item() - val_loss /= 5 + n_val_steps = 0 + for i in range(0, len(x_val), batch_size): + x_v_batch = x_val[i : i + batch_size] + m_v_batch = m_val[i : i + batch_size] + B_val = len(x_v_batch) + + for _ in range(5): + t_v = torch.rand(B_val, device=self.device) * (self.T - t_eps) + t_eps + s_v, sigma_v = self._marginal_params(t_v) + eps_v = torch.randn_like(x_v_batch) + x_t_v = s_v.unsqueeze(-1) * x_v_batch + sigma_v.unsqueeze(-1) * eps_v + + score_v = self.ema_net(x_t_v, t_v, m_v_batch) + batch_loss = ( + sigma_v.unsqueeze(-1) ** 2 + * (score_v + eps_v / sigma_v.unsqueeze(-1)) ** 2 + ).mean().item() + + val_loss += batch_loss + n_val_steps += 1 + + val_loss /= n_val_steps history["val_loss"].append(val_loss) scheduler.step(val_loss) if verbose: - print( - f"Epoch {epoch:5d}/{n_epochs} train={train_loss:.4f} val={val_loss:.4f}" - ) + print(f"Epoch {epoch:5d}/{n_epochs} train={train_loss:.4f} val={val_loss:.4f}") if val_loss < best_val_loss: best_val_loss = val_loss - best_state = {k: v.clone() for k, v in self.score_net.state_dict().items()} + best_state = {k: v.clone() for k, v in self.ema_net.module.state_dict().items()} patience_counter = 0 else: patience_counter += 1 @@ -1426,36 +1357,20 @@ def fit( break if best_state: + self.ema_net.module.load_state_dict(best_state) self.score_net.load_state_dict(best_state) self._is_trained = True return history - # ------------------------------------------------------------------ - # Inference - # ------------------------------------------------------------------ - @torch.no_grad() def sample_uncertainty( self, magnitudes: np.ndarray, n_samples: int = 1, - n_steps: Optional[int] = None, - t_eps: float = 1e-3, + n_steps: int = 50, + t_eps: float = 1e-2, + method: str = 'ode' ) -> np.ndarray: - """Sample flux uncertainties via the reverse VP-SDE. - - Args: - magnitudes: (N, n_filters) or (n_filters,) AB magnitudes. - n_samples: Number of independent samples per input row. - n_steps: Euler-Maruyama steps (default: self.n_sampling_steps). - t_eps: Minimum time to stop the reverse SDE at. - - Returns: - sigma: Flux uncertainties in the units used during training. - Shape (N, n_samples, n_filters) when n_samples > 1, - (N, n_filters) when n_samples=1, or (n_filters,) for a - scalar (1-row) input with n_samples=1. - """ if not self._is_trained: raise RuntimeError("Model must be trained before sampling. Call fit() first.") @@ -1463,43 +1378,43 @@ def sample_uncertainty( if scalar_input: magnitudes = magnitudes[np.newaxis] - N = len(magnitudes) - if n_steps is None: - n_steps = self.n_sampling_steps + # Check input is finite and has correct shape + if magnitudes.shape[1] != self.n_filters: + raise ValueError(f"Input magnitudes must have shape (N, {self.n_filters})") + if not np.all(np.isfinite(magnitudes)): + raise ValueError("Input magnitudes must be finite.") + N = len(magnitudes) mag_t = torch.tensor(magnitudes, dtype=torch.float32, device=self.device) - mag_norm = self._normalise_mag(mag_t) + mag_norm = self._normalise_mag(mag_t).repeat_interleave(n_samples, dim=0) - # Tile conditioning for n_samples - mag_norm = mag_norm.repeat_interleave(n_samples, dim=0) # (N*S, F) - - # Initialise from the base density p(x(T)) ≈ N(0, I) x = torch.randn(N * n_samples, self.n_filters, device=self.device) + self.ema_net.eval() - self.score_net.eval() - - dt = (self.T - t_eps) / n_steps # positive step magnitude + dt = (self.T - t_eps) / n_steps times = torch.linspace(self.T, t_eps, n_steps + 1, device=self.device) for i in range(n_steps): t_val = times[i].item() t_tensor = torch.full((N * n_samples,), t_val, device=self.device, dtype=torch.float32) - beta_t = self._beta(t_tensor) # (N*S,) - score = self.score_net(x, t_tensor, mag_norm) - - # Reverse SDE Euler-Maruyama step (going backward from T to 0): - # x_{t−dt} = x + [β/2·x + β·score]·dt + √(β·dt)·z - drift = beta_t.unsqueeze(-1) / 2 * x + beta_t.unsqueeze(-1) * score - z = torch.randn_like(x) - x = x + drift * dt + torch.sqrt(beta_t * dt).unsqueeze(-1) * z + beta_t = self._beta(t_tensor).unsqueeze(-1) + score = self.ema_net(x, t_tensor, mag_norm) + + if method == 'ode': + dx_dt = -0.5 * beta_t * (x + score) + x = x - dx_dt * dt + elif method == 'sde': + drift = (beta_t / 2) * x + beta_t * score + z = torch.randn_like(x) + x = x + drift * dt + torch.sqrt(beta_t * dt) * z + else: + raise ValueError(f"Unknown sampling method: {method}") - # Denormalise and exponentiate to recover σ ln_sigma = self._denormalise_ln_sigma(x) - sigma = torch.exp(ln_sigma) - sigma_np = sigma.cpu().numpy().reshape(N, n_samples, self.n_filters) + sigma_np = torch.exp(ln_sigma).cpu().numpy().reshape(N, n_samples, self.n_filters) if n_samples == 1: - sigma_np = sigma_np[:, 0, :] # (N, F) + sigma_np = sigma_np[:, 0, :] return sigma_np[0] if scalar_input else sigma_np @@ -1507,29 +1422,11 @@ def apply_noise( self, flux: np.ndarray, magnitudes: np.ndarray, - n_steps: Optional[int] = None, + n_steps: int = 50, + method: str = 'ode', **kwargs: Any, ) -> Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]: - """Sample uncertainties and apply Gaussian noise to flux. - - Args: - flux: (N, n_filters) true flux array in the same units as training. - magnitudes: (N, n_filters) calibrated model AB magnitudes used as - the conditioning variable m. - n_steps: Reverse SDE steps for uncertainty sampling. - kwargs: Ignored, but accepted for compatibility with the base class. - - Returns: - noisy_flux of shape (N, n_filters), or (noisy_flux, sigma) when - self.return_noise is True. - """ - if kwargs: - warnings.warn( - f"ScoreBasedUncertaintyModel.apply_noise received unexpected " - f"keyword arguments {list(kwargs.keys())} which will be ignored." - ) - - sigma = self.sample_uncertainty(magnitudes, n_samples=1, n_steps=n_steps) # (N, n_filters) + sigma = self.sample_uncertainty(magnitudes, n_samples=1, n_steps=n_steps, method=method) noise = np.random.normal(0.0, sigma) noisy_flux = flux + noise @@ -1537,12 +1434,7 @@ def apply_noise( return noisy_flux, sigma return noisy_flux - # ------------------------------------------------------------------ - # Serialisation - # ------------------------------------------------------------------ - def serialize_to_hdf5(self, hdf5_group: h5py.Group) -> None: - """Saves the model hyperparameters, normalisation stats, and weights.""" attrs = hdf5_group.attrs attrs["__class__"] = self.__class__.__name__ attrs["n_filters"] = self.n_filters @@ -1552,27 +1444,24 @@ def serialize_to_hdf5(self, hdf5_group: h5py.Group) -> None: attrs["beta_min"] = self.beta_min attrs["beta_max"] = self.beta_max attrs["T"] = self.T - attrs["n_sampling_steps"] = self.n_sampling_steps attrs["return_noise"] = self.return_noise attrs["is_trained"] = self._is_trained if not self._is_trained: return - hdf5_group.create_dataset("ln_sigma_mean", data=self._ln_sigma_mean.cpu().numpy()) - hdf5_group.create_dataset("ln_sigma_std", data=self._ln_sigma_std.cpu().numpy()) - hdf5_group.create_dataset("mag_mean", data=self._mag_mean.cpu().numpy()) - hdf5_group.create_dataset("mag_std", data=self._mag_std.cpu().numpy()) + hdf5_group.create_dataset("ln_sigma_median", data=self._ln_sigma_median.cpu().numpy()) + hdf5_group.create_dataset("ln_sigma_iqr", data=self._ln_sigma_iqr.cpu().numpy()) + hdf5_group.create_dataset("mag_median", data=self._mag_median.cpu().numpy()) + hdf5_group.create_dataset("mag_iqr", data=self._mag_iqr.cpu().numpy()) weights_group = hdf5_group.create_group("score_network_weights") - for key, tensor in self.score_net.state_dict().items(): - # Replace "." with "__" so the key is a valid flat HDF5 dataset name + for key, tensor in self.ema_net.module.state_dict().items(): safe_key = key.replace(".", "__") weights_group.create_dataset(safe_key, data=tensor.cpu().numpy()) @classmethod def _from_hdf5_group(cls, hdf5_group: h5py.Group) -> "ScoreBasedUncertaintyModel": - """Loads a ScoreBasedUncertaintyModel from an HDF5 group.""" attrs = hdf5_group.attrs instance = cls( n_filters=int(attrs["n_filters"]), @@ -1582,7 +1471,6 @@ def _from_hdf5_group(cls, hdf5_group: h5py.Group) -> "ScoreBasedUncertaintyModel beta_min=float(attrs["beta_min"]), beta_max=float(attrs["beta_max"]), T=float(attrs["T"]), - n_sampling_steps=int(attrs["n_sampling_steps"]), return_noise=bool(attrs["return_noise"]), ) @@ -1590,28 +1478,24 @@ def _from_hdf5_group(cls, hdf5_group: h5py.Group) -> "ScoreBasedUncertaintyModel return instance dev = instance.device - instance._ln_sigma_mean = torch.tensor( - hdf5_group["ln_sigma_mean"][:], dtype=torch.float32, device=dev - ) - instance._ln_sigma_std = torch.tensor( - hdf5_group["ln_sigma_std"][:], dtype=torch.float32, device=dev - ) - instance._mag_mean = torch.tensor( - hdf5_group["mag_mean"][:], dtype=torch.float32, device=dev - ) - instance._mag_std = torch.tensor(hdf5_group["mag_std"][:], dtype=torch.float32, device=dev) + instance._ln_sigma_median = torch.tensor(hdf5_group["ln_sigma_median"][:], dtype=torch.float32, device=dev) + instance._ln_sigma_iqr = torch.tensor(hdf5_group["ln_sigma_iqr"][:], dtype=torch.float32, device=dev) + instance._mag_median = torch.tensor(hdf5_group["mag_median"][:], dtype=torch.float32, device=dev) + instance._mag_iqr = torch.tensor(hdf5_group["mag_iqr"][:], dtype=torch.float32, device=dev) state_dict: Dict[str, torch.Tensor] = {} weights_group = hdf5_group["score_network_weights"] for safe_key in weights_group.keys(): original_key = safe_key.replace("__", ".") state_dict[original_key] = torch.tensor(weights_group[safe_key][:], dtype=torch.float32) + instance.score_net.load_state_dict(state_dict) + instance.ema_net.module.load_state_dict(state_dict) instance.score_net.to(dev) + instance.ema_net.to(dev) instance._is_trained = True return instance - # ============================================================================= # SERIALIZATION FACTORY FUNCTIONS # ============================================================================= From d7a1e43133e3d795e7df996617f9ea8fb5f41ba6 Mon Sep 17 00:00:00 2001 From: Thomas Harvey Date: Tue, 5 May 2026 15:07:42 +0100 Subject: [PATCH 05/22] Performance improvements to diffusion noise model --- src/synference/noise_models.py | 80 +++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 30 deletions(-) diff --git a/src/synference/noise_models.py b/src/synference/noise_models.py index e390ac5..69c096e 100644 --- a/src/synference/noise_models.py +++ b/src/synference/noise_models.py @@ -1192,7 +1192,10 @@ def __init__( self.score_net = _RobustScoreNetwork( n_filters, hidden_dim, n_layers, time_embed_dim ).to(self.device) - + + if self.device == "cuda": + self.score_net = torch.compile(self.score_net, mode="reduce-overhead") + self.ema_net = AveragedModel( self.score_net, multi_avg_fn=get_ema_multi_avg_fn(0.999) ) @@ -1229,7 +1232,7 @@ def fit( magnitudes: np.ndarray, flux_uncertainties: np.ndarray, n_epochs: int = 1000, - batch_size: int = 512, + batch_size: int = 4096, learning_rate: float = 3e-4, t_eps: float = 1e-2, valid_fraction: float = 0.1, @@ -1270,6 +1273,7 @@ def fit( m_val = torch.tensor(mag_norm[val_idx], dtype=torch.float32, device=self.device) optimizer = torch.optim.Adam(self.score_net.parameters(), lr=learning_rate) + scaler = torch.amp.GradScaler('cuda', enabled=(self.device == "cuda")) scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau( optimizer, factor=0.5, patience=10, min_lr=1e-5 ) @@ -1286,24 +1290,30 @@ def fit( epoch_loss = 0.0 n_batches = 0 - for i in range(0, len(x_ep), batch_size): - x0 = x_ep[i : i + batch_size] - m = m_ep[i : i + batch_size] + n_full_batches = len(x_ep) // batch_size + for i in range(n_full_batches): + start = i * batch_size + x0 = x_ep[start:start + batch_size] + m = m_ep[start:start + batch_size] B = len(x0) t = torch.rand(B, device=self.device) * (self.T - t_eps) + t_eps s, sigma = self._marginal_params(t) eps = torch.randn_like(x0) x_t = s.unsqueeze(-1) * x0 + sigma.unsqueeze(-1) * eps + + optimizer.zero_grad() - score = self.score_net(x_t, t, m) - target = -eps / sigma.unsqueeze(-1) - loss = (sigma.unsqueeze(-1) ** 2 * (score - target) ** 2).mean() + with torch.amp.autocast('cuda', enabled=(self.device == "cuda"), dtype=torch.bfloat16): + score = self.score_net(x_t, t, m) + target = -eps / sigma.unsqueeze(-1) + loss = (sigma.unsqueeze(-1) ** 2 * (score - target) ** 2).mean() - optimizer.zero_grad() - loss.backward() + scaler.scale(loss).backward() + scaler.unscale_(optimizer) torch.nn.utils.clip_grad_norm_(self.score_net.parameters(), max_norm=1.0) - optimizer.step() + scaler.step(optimizer) + scaler.update() self.ema_net.update_parameters(self.score_net) epoch_loss += loss.item() @@ -1314,7 +1324,7 @@ def fit( if epoch % 10 == 0: self.ema_net.eval() - with torch.no_grad(): + with torch.no_grad(), torch.amp.autocast('cuda', enabled=(self.device == "cuda"), dtype=torch.bfloat16): val_loss = 0.0 n_val_steps = 0 for i in range(0, len(x_val), batch_size): @@ -1394,21 +1404,22 @@ def sample_uncertainty( dt = (self.T - t_eps) / n_steps times = torch.linspace(self.T, t_eps, n_steps + 1, device=self.device) - for i in range(n_steps): - t_val = times[i].item() - t_tensor = torch.full((N * n_samples,), t_val, device=self.device, dtype=torch.float32) - beta_t = self._beta(t_tensor).unsqueeze(-1) - score = self.ema_net(x, t_tensor, mag_norm) - - if method == 'ode': - dx_dt = -0.5 * beta_t * (x + score) - x = x - dx_dt * dt - elif method == 'sde': - drift = (beta_t / 2) * x + beta_t * score - z = torch.randn_like(x) - x = x + drift * dt + torch.sqrt(beta_t * dt) * z - else: - raise ValueError(f"Unknown sampling method: {method}") + with torch.amp.autocast('cuda', enabled=(self.device == "cuda"), dtype=torch.bfloat16): + for i in range(n_steps): + t_val = times[i].item() + t_tensor = torch.full((N * n_samples,), t_val, device=self.device, dtype=torch.float32) + beta_t = self._beta(t_tensor).unsqueeze(-1) + score = self.ema_net(x, t_tensor, mag_norm) + + if method == 'ode': + dx_dt = -0.5 * beta_t * (x + score) + x = x - dx_dt * dt + elif method == 'sde': + drift = (beta_t / 2) * x + beta_t * score + z = torch.randn_like(x) + x = x + drift * dt + torch.sqrt(beta_t * dt) * z + else: + raise ValueError(f"Unknown sampling method: {method}") ln_sigma = self._denormalise_ln_sigma(x) sigma_np = torch.exp(ln_sigma).cpu().numpy().reshape(N, n_samples, self.n_filters) @@ -1456,7 +1467,12 @@ def serialize_to_hdf5(self, hdf5_group: h5py.Group) -> None: hdf5_group.create_dataset("mag_iqr", data=self._mag_iqr.cpu().numpy()) weights_group = hdf5_group.create_group("score_network_weights") - for key, tensor in self.ema_net.module.state_dict().items(): + + underlying = self.ema_net.module + if hasattr(underlying, '_orig_mod'): + underlying = underlying._orig_mod + + for key, tensor in underlying.state_dict().items(): safe_key = key.replace(".", "__") weights_group.create_dataset(safe_key, data=tensor.cpu().numpy()) @@ -1488,8 +1504,12 @@ def _from_hdf5_group(cls, hdf5_group: h5py.Group) -> "ScoreBasedUncertaintyModel for safe_key in weights_group.keys(): original_key = safe_key.replace("__", ".") state_dict[original_key] = torch.tensor(weights_group[safe_key][:], dtype=torch.float32) - - instance.score_net.load_state_dict(state_dict) + + target = instance.score_net + if hasattr(target, '_orig_mod'): + target = target._orig_mod + + target.load_state_dict(state_dict) instance.ema_net.module.load_state_dict(state_dict) instance.score_net.to(dev) instance.ema_net.to(dev) From 7cdc9de5f56396b1b4b27bbd6a4638b4c0daf6e5 Mon Sep 17 00:00:00 2001 From: Thomas Harvey Date: Wed, 6 May 2026 15:31:40 +0100 Subject: [PATCH 06/22] More reliable training behaviour. --- src/synference/noise_models.py | 109 ++++++++++++++++++++------------- 1 file changed, 68 insertions(+), 41 deletions(-) diff --git a/src/synference/noise_models.py b/src/synference/noise_models.py index 69c096e..051c903 100644 --- a/src/synference/noise_models.py +++ b/src/synference/noise_models.py @@ -1232,11 +1232,15 @@ def fit( magnitudes: np.ndarray, flux_uncertainties: np.ndarray, n_epochs: int = 1000, - batch_size: int = 4096, + batch_size: int = 1024, learning_rate: float = 3e-4, t_eps: float = 1e-2, valid_fraction: float = 0.1, - patience: int = 50, + patience: int = 20, + min_epochs: int = 200, + min_delta: float = 1e-3, + val_freq: int = 10, + n_val_t: int = 20, verbose: bool = True, ) -> Dict[str, List[float]]: if magnitudes.ndim != 2 or magnitudes.shape[1] != self.n_filters: @@ -1272,8 +1276,14 @@ def fit( x_val = torch.tensor(ln_sigma_norm[val_idx], dtype=torch.float32, device=self.device) m_val = torch.tensor(mag_norm[val_idx], dtype=torch.float32, device=self.device) + # Pre-generate fixed validation noise. The validation loss becomes a deterministic + # function of model parameters: same (t, eps) pairs are reused every eval step. + # Using a stratified t grid (linspace) eliminates the variance from t sampling, + # which is the dominant source of validation noise in score-based diffusion training. + val_t_grid = torch.linspace(t_eps, self.T, n_val_t, device=self.device) + val_eps_fixed = torch.randn(n_val_t, len(x_val), self.n_filters, device=self.device) + optimizer = torch.optim.Adam(self.score_net.parameters(), lr=learning_rate) - scaler = torch.amp.GradScaler('cuda', enabled=(self.device == "cuda")) scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau( optimizer, factor=0.5, patience=10, min_lr=1e-5 ) @@ -1290,30 +1300,24 @@ def fit( epoch_loss = 0.0 n_batches = 0 - n_full_batches = len(x_ep) // batch_size - for i in range(n_full_batches): - start = i * batch_size - x0 = x_ep[start:start + batch_size] - m = m_ep[start:start + batch_size] + for i in range(0, len(x_ep), batch_size): + x0 = x_ep[i : i + batch_size] + m = m_ep[i : i + batch_size] B = len(x0) t = torch.rand(B, device=self.device) * (self.T - t_eps) + t_eps s, sigma = self._marginal_params(t) eps = torch.randn_like(x0) x_t = s.unsqueeze(-1) * x0 + sigma.unsqueeze(-1) * eps - - optimizer.zero_grad() - with torch.amp.autocast('cuda', enabled=(self.device == "cuda"), dtype=torch.bfloat16): - score = self.score_net(x_t, t, m) - target = -eps / sigma.unsqueeze(-1) - loss = (sigma.unsqueeze(-1) ** 2 * (score - target) ** 2).mean() + score = self.score_net(x_t, t, m) + target = -eps / sigma.unsqueeze(-1) + loss = (sigma.unsqueeze(-1) ** 2 * (score - target) ** 2).mean() - scaler.scale(loss).backward() - scaler.unscale_(optimizer) + optimizer.zero_grad() + loss.backward() torch.nn.utils.clip_grad_norm_(self.score_net.parameters(), max_norm=1.0) - scaler.step(optimizer) - scaler.update() + optimizer.step() self.ema_net.update_parameters(self.score_net) epoch_loss += loss.item() @@ -1322,32 +1326,39 @@ def fit( train_loss = epoch_loss / max(n_batches, 1) history["train_loss"].append(train_loss) - if epoch % 10 == 0: + if epoch % val_freq == 0: self.ema_net.eval() - with torch.no_grad(), torch.amp.autocast('cuda', enabled=(self.device == "cuda"), dtype=torch.bfloat16): + with torch.no_grad(): val_loss = 0.0 - n_val_steps = 0 - for i in range(0, len(x_val), batch_size): - x_v_batch = x_val[i : i + batch_size] - m_v_batch = m_val[i : i + batch_size] - B_val = len(x_v_batch) - - for _ in range(5): - t_v = torch.rand(B_val, device=self.device) * (self.T - t_eps) + t_eps - s_v, sigma_v = self._marginal_params(t_v) - eps_v = torch.randn_like(x_v_batch) - x_t_v = s_v.unsqueeze(-1) * x_v_batch + sigma_v.unsqueeze(-1) * eps_v - + n_val_terms = 0 + # Iterate over the fixed t grid; for each t, evaluate the whole + # validation set in mini-batches using the corresponding fixed eps. + for t_idx in range(n_val_t): + t_val = val_t_grid[t_idx] + eps_full = val_eps_fixed[t_idx] + s_v_scalar, sigma_v_scalar = self._marginal_params(t_val.unsqueeze(0)) + s_v_scalar = s_v_scalar.item() + sigma_v_scalar = sigma_v_scalar.item() + + for i in range(0, len(x_val), batch_size): + x_v_batch = x_val[i : i + batch_size] + m_v_batch = m_val[i : i + batch_size] + eps_v = eps_full[i : i + batch_size] + B_val = len(x_v_batch) + + t_v = t_val.expand(B_val) + x_t_v = s_v_scalar * x_v_batch + sigma_v_scalar * eps_v + score_v = self.ema_net(x_t_v, t_v, m_v_batch) + target_v = -eps_v / sigma_v_scalar batch_loss = ( - sigma_v.unsqueeze(-1) ** 2 - * (score_v + eps_v / sigma_v.unsqueeze(-1)) ** 2 + sigma_v_scalar ** 2 * (score_v - target_v) ** 2 ).mean().item() - + val_loss += batch_loss - n_val_steps += 1 - - val_loss /= n_val_steps + n_val_terms += 1 + + val_loss /= n_val_terms history["val_loss"].append(val_loss) scheduler.step(val_loss) @@ -1355,7 +1366,20 @@ def fit( if verbose: print(f"Epoch {epoch:5d}/{n_epochs} train={train_loss:.4f} val={val_loss:.4f}") - if val_loss < best_val_loss: + # Early stopping: only active after min_epochs to allow EMA to warm up + # and avoid premature stopping during the initial fluctuating phase. + if epoch < min_epochs: + # Still track best state during warmup so we have something sensible + # if training is interrupted, but don't count toward patience. + if val_loss < best_val_loss: + best_val_loss = val_loss + best_state = {k: v.clone() for k, v in self.ema_net.module.state_dict().items()} + continue + + # Relative improvement threshold: require val_loss to drop by at least + # min_delta (fractionally) to count as genuine progress. Avoids resetting + # patience on noise-level fluctuations. + if val_loss < best_val_loss * (1.0 - min_delta): best_val_loss = val_loss best_state = {k: v.clone() for k, v in self.ema_net.module.state_dict().items()} patience_counter = 0 @@ -1363,7 +1387,10 @@ def fit( patience_counter += 1 if patience_counter >= patience: if verbose: - print(f"Early stopping at epoch {epoch}.") + print( + f"Early stopping at epoch {epoch} " + f"({patience} evals without >{min_delta:.1%} improvement)." + ) break if best_state: @@ -1371,7 +1398,7 @@ def fit( self.score_net.load_state_dict(best_state) self._is_trained = True return history - + @torch.no_grad() def sample_uncertainty( self, From aac1aaef95f12a141dadfe7996528adc4a586a2c Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 6 May 2026 15:43:49 +0000 Subject: [PATCH 07/22] Fix ValueError in _scatter when using noise models Agent-Logs-Url: https://github.com/synthesizer-project/synference/sessions/7874d3da-9020-4367-8fb5-9f51fe2180df Co-authored-by: tHarvey303 <8613717+tHarvey303@users.noreply.github.com> --- src/synference/library.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/synference/library.py b/src/synference/library.py index 367ebb5..a3455f6 100644 --- a/src/synference/library.py +++ b/src/synference/library.py @@ -5987,8 +5987,8 @@ def _scatter( out_units=self.out_flux_unit, ) - scattered_fluxes[i] = scattered_flux - errors[i] = sigma + scattered_fluxes[i] = np.atleast_1d(scattered_flux)[0] + errors[i] = np.atleast_1d(sigma)[0] return scattered_fluxes, errors From f6e0bf6a235d10171d86d848b0d27f91d81c1391 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 6 May 2026 15:45:16 +0000 Subject: [PATCH 08/22] Use direct [0] indexing for scalar extraction from apply_noise result Agent-Logs-Url: https://github.com/synthesizer-project/synference/sessions/7874d3da-9020-4367-8fb5-9f51fe2180df Co-authored-by: tHarvey303 <8613717+tHarvey303@users.noreply.github.com> --- src/synference/library.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/synference/library.py b/src/synference/library.py index a3455f6..04abe24 100644 --- a/src/synference/library.py +++ b/src/synference/library.py @@ -5987,8 +5987,8 @@ def _scatter( out_units=self.out_flux_unit, ) - scattered_fluxes[i] = np.atleast_1d(scattered_flux)[0] - errors[i] = np.atleast_1d(sigma)[0] + scattered_fluxes[i] = scattered_flux[0] + errors[i] = sigma[0] return scattered_fluxes, errors From 45702a9e62c51b06e2b818748a77ac1aff9f1dec Mon Sep 17 00:00:00 2001 From: tHarvey303 Date: Thu, 7 May 2026 00:42:12 +0100 Subject: [PATCH 09/22] Wire new noise model into feature array creation, add tests. --- src/synference/noise_models.py | 302 +++++++++++++++++++++----- src/synference/sbi_runner.py | 185 ++++++++++++++-- src/synference/utils.py | 8 +- tests/test_uncertainty_models.py | 353 ++++++++++++++++++++++++++++++- 4 files changed, 772 insertions(+), 76 deletions(-) diff --git a/src/synference/noise_models.py b/src/synference/noise_models.py index 051c903..f1491ba 100644 --- a/src/synference/noise_models.py +++ b/src/synference/noise_models.py @@ -4,6 +4,8 @@ applying various photometric noise models. """ +import json +import math import warnings from abc import ABC, abstractmethod from typing import Any, Dict, List, Optional, Tuple, Union @@ -12,12 +14,11 @@ import matplotlib.pyplot as plt import numpy as np import torch -import math import torch.nn as nn -from torch.optim.swa_utils import AveragedModel, get_ema_multi_avg_fn from astropy.table import Table from scipy import stats from scipy.interpolate import interp1d +from torch.optim.swa_utils import AveragedModel, get_ema_multi_avg_fn from unyt import Jy, Unit, unyt_array from .utils import f_jy_err_to_asinh, f_jy_to_asinh @@ -1109,11 +1110,37 @@ def apply_scalings( class GaussianFourierProjection(nn.Module): + """Maps a scalar input to a sinusoidal random Fourier feature embedding. + + Used to encode the diffusion time step into a fixed-dimensional vector + suitable for conditioning a neural network. Random frequencies are drawn + once at construction and kept fixed (non-trainable). + """ + def __init__(self, embed_dim: int, scale: float = 30.0): + """Initializes the projection with random Fourier frequencies. + + Args: + embed_dim (int): Output embedding dimension. Must be even; the output + is formed by concatenating ``embed_dim // 2`` sin and cos features. + scale (float): Standard deviation of the Gaussian from which random + frequencies are drawn. Higher values encode higher-frequency + variation. Default is 30.0. + """ super().__init__() self.W = nn.Parameter(torch.randn(embed_dim // 2) * scale, requires_grad=False) - + def forward(self, x: torch.Tensor) -> torch.Tensor: + """Projects x into the sinusoidal embedding space. + + Args: + x (torch.Tensor): Input tensor of shape ``(..., 1)`` (e.g. diffusion + time steps, one per batch element). + + Returns: + torch.Tensor: Embedding of shape ``(..., embed_dim)`` formed by + concatenating ``sin`` and ``cos`` projections along the last axis. + """ x_proj = x * self.W[None, :] * 2 * math.pi return torch.cat([torch.sin(x_proj), torch.cos(x_proj)], dim=-1) @@ -1123,7 +1150,7 @@ def __init__(self, dim: int): super().__init__() self.linear = nn.Linear(dim, dim) self.act = nn.SiLU() - + def forward(self, x: torch.Tensor) -> torch.Tensor: return x + self.linear(self.act(x)) @@ -1140,35 +1167,43 @@ def __init__( self.time_embed = nn.Sequential( GaussianFourierProjection(embed_dim=time_embed_dim), nn.Linear(time_embed_dim, time_embed_dim), - nn.SiLU() + nn.SiLU(), ) - + input_dim = n_filters * 2 + time_embed_dim self.proj_in = nn.Linear(input_dim, hidden_dim) - - self.res_blocks = nn.ModuleList([ - _ResidualBlock(hidden_dim) for _ in range(n_layers) - ]) - - self.proj_out = nn.Sequential( - nn.SiLU(), - nn.Linear(hidden_dim, n_filters) - ) + + self.res_blocks = nn.ModuleList([_ResidualBlock(hidden_dim) for _ in range(n_layers)]) + + self.proj_out = nn.Sequential(nn.SiLU(), nn.Linear(hidden_dim, n_filters)) def forward(self, x: torch.Tensor, t: torch.Tensor, m: torch.Tensor) -> torch.Tensor: t_emb = self.time_embed(t.unsqueeze(-1)) h = self.proj_in(torch.cat([x, m, t_emb], dim=-1)) - + for block in self.res_blocks: h = block(h) - + return self.proj_out(h) class ScoreBasedUncertaintyModel: + """Score-based diffusion model that learns the conditional uncertainty distribution p(σ|m). + + Trains a neural score network on pairs of observed magnitudes and flux + uncertainties using a variance-preserving (VP) SDE with a linear beta + schedule. After training, draws photometric uncertainty samples by running + the reverse-time ODE or SDE, then uses those samples to corrupt input flux + arrays in the same way as the other noise models. + + The model operates internally in log-uncertainty space and applies + IQR-based normalisation to both the uncertainty and magnitude features + before training, making it robust to outliers. + """ + def __init__( self, - n_filters: int, + filter_names: List[str], hidden_dim: int = 256, n_layers: int = 5, time_embed_dim: int = 64, @@ -1178,6 +1213,27 @@ def __init__( device: Optional[str] = None, return_noise: bool = False, ): + """Initializes the score network, EMA wrapper, and SDE hyperparameters. + + Args: + filter_names (List[str]): Ordered list of photometric band names + (e.g. ``["u", "g", "r", "i", "z"]``). The number of filters is + inferred as ``len(filter_names)`` and sets the input/output + dimensionality of the score network. + hidden_dim (int): Width of each hidden layer in the score network. Default 256. + n_layers (int): Number of residual blocks in the score network. Default 5. + time_embed_dim (int): Dimensionality of the Gaussian Fourier time embedding. + Default 64. + beta_min (float): Minimum noise schedule coefficient β(0). Default 0.1. + beta_max (float): Maximum noise schedule coefficient β(T). Default 20.0. + T (float): Total diffusion time horizon. Default 1.0. + device (Optional[str]): Torch device string (e.g. ``"cuda"``, ``"cpu"``). + Auto-detected from CUDA availability when ``None``. + return_noise (bool): If ``True``, ``apply_noise`` returns a tuple + ``(noisy_flux, sigma)`` instead of only the noisy flux. Default ``False``. + """ + self.filter_names = list(filter_names) + n_filters = len(self.filter_names) self.n_filters = n_filters self.hidden_dim = hidden_dim self.n_layers = n_layers @@ -1189,16 +1245,14 @@ def __init__( self.device = device or ("cuda" if torch.cuda.is_available() else "cpu") - self.score_net = _RobustScoreNetwork( - n_filters, hidden_dim, n_layers, time_embed_dim - ).to(self.device) + self.score_net = _RobustScoreNetwork(n_filters, hidden_dim, n_layers, time_embed_dim).to( + self.device + ) if self.device == "cuda": self.score_net = torch.compile(self.score_net, mode="reduce-overhead") - self.ema_net = AveragedModel( - self.score_net, multi_avg_fn=get_ema_multi_avg_fn(0.999) - ) + self.ema_net = AveragedModel(self.score_net, multi_avg_fn=get_ema_multi_avg_fn(0.999)) self._ln_sigma_median: Optional[torch.Tensor] = None self._ln_sigma_iqr: Optional[torch.Tensor] = None @@ -1243,8 +1297,49 @@ def fit( n_val_t: int = 20, verbose: bool = True, ) -> Dict[str, List[float]]: + """Trains the score network on observed magnitudes and flux uncertainties. + + Rows with non-positive or non-finite uncertainties are dropped before + training. Normalisation statistics (median and IQR) are computed from + the training set and stored on the instance for use during sampling. + An EMA of the score network weights is maintained throughout and used + for validation and sampling. + + Args: + magnitudes (np.ndarray): Observed AB magnitudes, shape ``(N, n_filters)``. + flux_uncertainties (np.ndarray): Observed flux uncertainties (σ) in + Janskys, shape ``(N, n_filters)``. Must be positive and finite. + n_epochs (int): Maximum number of training epochs. Default 1000. + batch_size (int): Number of samples per gradient step. Default 1024. + learning_rate (float): Initial Adam learning rate. Default 3e-4. + t_eps (float): Minimum diffusion time to avoid numerical instability + near t=0. Default 1e-2. + valid_fraction (float): Fraction of data held out for validation. + Default 0.1. + patience (int): Number of validation evaluations without sufficient + improvement before early stopping triggers. Default 20. + min_epochs (int): Minimum training epochs before early stopping is + active; allows EMA to warm up. Default 200. + min_delta (float): Minimum relative validation-loss improvement + required to reset the patience counter. Default 1e-3. + val_freq (int): Epoch interval between validation evaluations. + Default 10. + n_val_t (int): Number of evenly spaced diffusion times used for + deterministic validation loss evaluation. Default 20. + verbose (bool): Print epoch-level progress. Default ``True``. + + Returns: + Dict[str, List[float]]: Training history with keys ``"train_loss"`` + (every epoch) and ``"val_loss"`` (every ``val_freq`` epochs). + + Raises: + ValueError: If ``magnitudes`` has the wrong shape or no valid samples + remain after filtering. + """ if magnitudes.ndim != 2 or magnitudes.shape[1] != self.n_filters: - raise ValueError(f"magnitudes must be (N, {self.n_filters})") + raise ValueError( + f"magnitudes must be (N, {self.n_filters}) for filters {self.filter_names}" + ) valid_mask = np.all((flux_uncertainties > 0) & np.isfinite(magnitudes), axis=1) if not np.any(valid_mask): @@ -1254,9 +1349,13 @@ def fit( ln_sigma = np.log(flux_uncertainties[valid_mask]) ln_sigma_median = np.median(ln_sigma, axis=0).astype(np.float32) - ln_sigma_iqr = (np.percentile(ln_sigma, 75, axis=0) - np.percentile(ln_sigma, 25, axis=0)).astype(np.float32) + ln_sigma_iqr = ( + np.percentile(ln_sigma, 75, axis=0) - np.percentile(ln_sigma, 25, axis=0) + ).astype(np.float32) mag_median = np.median(magnitudes, axis=0).astype(np.float32) - mag_iqr = (np.percentile(magnitudes, 75, axis=0) - np.percentile(magnitudes, 25, axis=0)).astype(np.float32) + mag_iqr = ( + np.percentile(magnitudes, 75, axis=0) - np.percentile(magnitudes, 25, axis=0) + ).astype(np.float32) self._ln_sigma_median = torch.tensor(ln_sigma_median, device=self.device) self._ln_sigma_iqr = torch.tensor(ln_sigma_iqr, device=self.device) @@ -1352,8 +1451,8 @@ def fit( score_v = self.ema_net(x_t_v, t_v, m_v_batch) target_v = -eps_v / sigma_v_scalar batch_loss = ( - sigma_v_scalar ** 2 * (score_v - target_v) ** 2 - ).mean().item() + (sigma_v_scalar**2 * (score_v - target_v) ** 2).mean().item() + ) val_loss += batch_loss n_val_terms += 1 @@ -1364,7 +1463,9 @@ def fit( scheduler.step(val_loss) if verbose: - print(f"Epoch {epoch:5d}/{n_epochs} train={train_loss:.4f} val={val_loss:.4f}") + print( + f"Epoch {epoch:5d}/{n_epochs} train={train_loss:.4f} val={val_loss:.4f}" + ) # Early stopping: only active after min_epochs to allow EMA to warm up # and avoid premature stopping during the initial fluctuating phase. @@ -1373,7 +1474,9 @@ def fit( # if training is interrupted, but don't count toward patience. if val_loss < best_val_loss: best_val_loss = val_loss - best_state = {k: v.clone() for k, v in self.ema_net.module.state_dict().items()} + best_state = { + k: v.clone() for k, v in self.ema_net.module.state_dict().items() + } continue # Relative improvement threshold: require val_loss to drop by at least @@ -1398,7 +1501,7 @@ def fit( self.score_net.load_state_dict(best_state) self._is_trained = True return history - + @torch.no_grad() def sample_uncertainty( self, @@ -1406,8 +1509,37 @@ def sample_uncertainty( n_samples: int = 1, n_steps: int = 50, t_eps: float = 1e-2, - method: str = 'ode' + method: str = "ode", ) -> np.ndarray: + """Samples flux uncertainties conditioned on observed magnitudes. + + Runs the reverse-time diffusion process (ODE or SDE) from t=T to t=t_eps, + then exponentiates the resulting normalised log-uncertainty to recover + physical flux uncertainties in Janskys. + + Args: + magnitudes (np.ndarray): Observed AB magnitudes. Shape ``(n_filters,)`` + for a single object or ``(N, n_filters)`` for a batch. + n_samples (int): Number of independent uncertainty draws per object. + Default 1. + n_steps (int): Number of discrete integration steps. Default 50. + t_eps (float): Minimum diffusion time; integration runs from T down + to this value. Default 1e-2. + method (str): Reverse-time solver — ``"ode"`` (probability-flow ODE, + deterministic) or ``"sde"`` (Langevin SDE, stochastic). + Default ``"ode"``. + + Returns: + np.ndarray: Sampled flux uncertainties (σ) in Janskys. Shape is + ``(N, n_filters)`` when ``n_samples=1``, ``(N, n_samples, n_filters)`` + otherwise. A leading batch dimension is stripped when the input + was a single-object array. + + Raises: + RuntimeError: If called before the model has been trained. + ValueError: If ``magnitudes`` has the wrong shape, contains non-finite + values, or ``method`` is not recognised. + """ if not self._is_trained: raise RuntimeError("Model must be trained before sampling. Call fit() first.") @@ -1417,7 +1549,10 @@ def sample_uncertainty( # Check input is finite and has correct shape if magnitudes.shape[1] != self.n_filters: - raise ValueError(f"Input magnitudes must have shape (N, {self.n_filters})") + raise ValueError( + f"Input magnitudes must have shape (N, {self.n_filters}) " + f"for filters {self.filter_names}" + ) if not np.all(np.isfinite(magnitudes)): raise ValueError("Input magnitudes must be finite.") @@ -1431,17 +1566,19 @@ def sample_uncertainty( dt = (self.T - t_eps) / n_steps times = torch.linspace(self.T, t_eps, n_steps + 1, device=self.device) - with torch.amp.autocast('cuda', enabled=(self.device == "cuda"), dtype=torch.bfloat16): + with torch.amp.autocast("cuda", enabled=(self.device == "cuda"), dtype=torch.bfloat16): for i in range(n_steps): t_val = times[i].item() - t_tensor = torch.full((N * n_samples,), t_val, device=self.device, dtype=torch.float32) + t_tensor = torch.full( + (N * n_samples,), t_val, device=self.device, dtype=torch.float32 + ) beta_t = self._beta(t_tensor).unsqueeze(-1) score = self.ema_net(x, t_tensor, mag_norm) - if method == 'ode': + if method == "ode": dx_dt = -0.5 * beta_t * (x + score) x = x - dx_dt * dt - elif method == 'sde': + elif method == "sde": drift = (beta_t / 2) * x + beta_t * score z = torch.randn_like(x) x = x + drift * dt + torch.sqrt(beta_t * dt) * z @@ -1459,23 +1596,67 @@ def sample_uncertainty( def apply_noise( self, flux: np.ndarray, - magnitudes: np.ndarray, n_steps: int = 50, - method: str = 'ode', + true_flux_units: str = None, + method: str = "ode", **kwargs: Any, ) -> Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]: + """Applies diffusion-model-sampled Gaussian noise to the input flux. + + Converts flux to AB magnitudes, draws one uncertainty sample per object + via the reverse diffusion process, then adds zero-mean Gaussian noise + with that uncertainty to the original flux. + + Args: + flux (np.ndarray or unyt_array): Input flux values. If a plain + numpy array, ``true_flux_units`` must be provided. + n_steps (int): Number of reverse-diffusion integration steps. Default 50. + true_flux_units (str): Units of ``flux`` (e.g. ``"Jy"``, ``"uJy"``). + Required when ``flux`` is not a unyt_array. Default ``None``. + method (str): Reverse-time solver — ``"ode"`` or ``"sde"``. Default ``"ode"``. + **kwargs: Absorbed for interface compatibility; not used. + + Returns: + Union[unyt_array, Tuple[unyt_array, unyt_array]]: Noisy flux in + Janskys, or ``(noisy_flux_jy, sigma_jy)`` if the model was + constructed with ``return_noise=True``. + + Raises: + ValueError: If ``flux`` is a plain array and ``true_flux_units`` + is not provided. + """ + if isinstance(flux, unyt_array): + flux_jy = flux.to("Jy").value + elif true_flux_units is not None: + flux_jy = unyt_array(flux, units=true_flux_units).to("Jy").value + else: + raise ValueError("true_flux_units must be provided when flux is not a unyt_array.") + safe_jy = np.where(flux_jy > 0, flux_jy, np.finfo(np.float32).tiny) + magnitudes = (-2.5 * np.log10(safe_jy / 3631.0)).astype(np.float32) sigma = self.sample_uncertainty(magnitudes, n_samples=1, n_steps=n_steps, method=method) noise = np.random.normal(0.0, sigma) - noisy_flux = flux + noise + noisy_flux = unyt_array(flux_jy + noise, units="Jy") + sigma_jy = unyt_array(sigma, units="Jy") if self.return_noise: - return noisy_flux, sigma + return noisy_flux, sigma_jy return noisy_flux def serialize_to_hdf5(self, hdf5_group: h5py.Group) -> None: + """Serializes the model's configuration and trained weights to an HDF5 group. + + Hyperparameters (including the ordered filter name list) are stored as + HDF5 attributes. When the model has been trained, normalisation + statistics and the EMA score-network weights are also written. + Compiled-model wrappers (``torch.compile``) are unwrapped before weight + extraction. + + Args: + hdf5_group (h5py.Group): Open, writable HDF5 group to write into. + """ attrs = hdf5_group.attrs attrs["__class__"] = self.__class__.__name__ - attrs["n_filters"] = self.n_filters + attrs["filter_names"] = json.dumps(self.filter_names) attrs["hidden_dim"] = self.hidden_dim attrs["n_layers"] = self.n_layers attrs["time_embed_dim"] = self.time_embed_dim @@ -1496,7 +1677,7 @@ def serialize_to_hdf5(self, hdf5_group: h5py.Group) -> None: weights_group = hdf5_group.create_group("score_network_weights") underlying = self.ema_net.module - if hasattr(underlying, '_orig_mod'): + if hasattr(underlying, "_orig_mod"): underlying = underlying._orig_mod for key, tensor in underlying.state_dict().items(): @@ -1505,9 +1686,23 @@ def serialize_to_hdf5(self, hdf5_group: h5py.Group) -> None: @classmethod def _from_hdf5_group(cls, hdf5_group: h5py.Group) -> "ScoreBasedUncertaintyModel": + """Loads a ScoreBasedUncertaintyModel from an HDF5 group. + + Reconstructs the model from the hyperparameters stored as HDF5 attributes, + then restores the normalisation statistics and score-network weights if + the model was trained before serialisation. + + Args: + hdf5_group (h5py.Group): Open HDF5 group previously written by + ``serialize_to_hdf5``. + + Returns: + ScoreBasedUncertaintyModel: Fully restored model instance, ready + for sampling if the serialised model was trained. + """ attrs = hdf5_group.attrs instance = cls( - n_filters=int(attrs["n_filters"]), + filter_names=json.loads(attrs["filter_names"]), hidden_dim=int(attrs["hidden_dim"]), n_layers=int(attrs["n_layers"]), time_embed_dim=int(attrs["time_embed_dim"]), @@ -1521,9 +1716,15 @@ def _from_hdf5_group(cls, hdf5_group: h5py.Group) -> "ScoreBasedUncertaintyModel return instance dev = instance.device - instance._ln_sigma_median = torch.tensor(hdf5_group["ln_sigma_median"][:], dtype=torch.float32, device=dev) - instance._ln_sigma_iqr = torch.tensor(hdf5_group["ln_sigma_iqr"][:], dtype=torch.float32, device=dev) - instance._mag_median = torch.tensor(hdf5_group["mag_median"][:], dtype=torch.float32, device=dev) + instance._ln_sigma_median = torch.tensor( + hdf5_group["ln_sigma_median"][:], dtype=torch.float32, device=dev + ) + instance._ln_sigma_iqr = torch.tensor( + hdf5_group["ln_sigma_iqr"][:], dtype=torch.float32, device=dev + ) + instance._mag_median = torch.tensor( + hdf5_group["mag_median"][:], dtype=torch.float32, device=dev + ) instance._mag_iqr = torch.tensor(hdf5_group["mag_iqr"][:], dtype=torch.float32, device=dev) state_dict: Dict[str, torch.Tensor] = {} @@ -1531,9 +1732,9 @@ def _from_hdf5_group(cls, hdf5_group: h5py.Group) -> "ScoreBasedUncertaintyModel for safe_key in weights_group.keys(): original_key = safe_key.replace("__", ".") state_dict[original_key] = torch.tensor(weights_group[safe_key][:], dtype=torch.float32) - + target = instance.score_net - if hasattr(target, '_orig_mod'): + if hasattr(target, "_orig_mod"): target = target._orig_mod target.load_state_dict(state_dict) @@ -1543,6 +1744,7 @@ def _from_hdf5_group(cls, hdf5_group: h5py.Group) -> "ScoreBasedUncertaintyModel instance._is_trained = True return instance + # ============================================================================= # SERIALIZATION FACTORY FUNCTIONS # ============================================================================= diff --git a/src/synference/sbi_runner.py b/src/synference/sbi_runner.py index cf44cf9..abee432 100644 --- a/src/synference/sbi_runner.py +++ b/src/synference/sbi_runner.py @@ -59,6 +59,7 @@ from .noise_models import ( AsinhEmpiricalUncertaintyModel, EmpiricalUncertaintyModel, + ScoreBasedUncertaintyModel, UncertaintyModel, load_unc_model_from_hdf5, save_unc_model_to_hdf5, @@ -739,10 +740,16 @@ def save_state( ): noise_models = param_dict["feature_array_flags"].pop("empirical_noise_models") noise_model_path = save_path.replace(".pkl", "_empirical_noise_models.h5") - param_dict["empirical_noise_models"] = {} - for key, model in noise_models.items(): - save_unc_model_to_hdf5(model, noise_model_path, key, overwrite=True) - param_dict["empirical_noise_models"][key] = noise_model_path + if isinstance(noise_models, ScoreBasedUncertaintyModel): + save_unc_model_to_hdf5( + noise_models, noise_model_path, "__score_based__", overwrite=True + ) + param_dict["empirical_noise_models"] = {"__score_based__": noise_model_path} + else: + param_dict["empirical_noise_models"] = {} + for key, model in noise_models.items(): + save_unc_model_to_hdf5(model, noise_model_path, key, overwrite=True) + param_dict["empirical_noise_models"][key] = noise_model_path if "stats" in param_dict: stats_path = f"{out_dir}/{self.name}{name_append}_summary.json" @@ -902,6 +909,107 @@ def apply_noise_model(filter_name): return scattered_photometry + def _apply_score_based_noise_model( + self, + photometry_array, + phot_names: List[str], + score_model: "ScoreBasedUncertaintyModel", + N_scatters: int = 5, + flux_units=None, + return_errors: bool = False, + normed_flux_units: str = "AB", + ): + """Apply a ScoreBasedUncertaintyModel to the full multi-band photometry array. + + Unlike the per-filter empirical path, the score model conditions on all + bands simultaneously and returns per-band uncertainties in a single call. + The returned arrays match the shape contract of + ``_apply_empirical_noise_models`` so the caller can treat both paths + identically (set ``converted=True`` after calling this method). + + Parameters: + photometry_array: Flux array of shape ``(n_filters, N)``, as a + ``unyt_array`` in any flux unit. + phot_names: Ordered list of filter names, length ``n_filters``. + Must match ``score_model.filter_names`` exactly. + score_model: Trained :class:`ScoreBasedUncertaintyModel`. + N_scatters: Number of independent noise realisations per galaxy. + flux_units: Input flux units (used only if ``photometry_array`` is + not a unyt_array; ignored otherwise). + return_errors: If ``True`` returns ``(scattered, errors)``. + normed_flux_units: Target units for the returned scattered + photometry. ``"AB"`` returns AB magnitudes and AB-mag errors. + Any other string is treated as a physical flux unit (e.g. + ``"nJy"``) and the values are converted accordingly. + ``"asinh"`` is not supported. + + Returns: + ``np.ndarray`` of shape ``(n_filters, N*N_scatters)`` when + ``return_errors=False``, or a tuple of two such arrays + ``(scattered_photometry, errors)`` when ``return_errors=True``. + + Raises: + ValueError: If the filter names do not match the model, or if + ``normed_flux_units="asinh"`` is requested. + """ + if normed_flux_units == "asinh": + raise ValueError( + "ScoreBasedUncertaintyModel does not support asinh normalisation. " + "Use normed_flux_units='AB' or a physical flux unit (e.g. 'nJy')." + ) + + expected = score_model.filter_names + actual = list(phot_names) + if actual != expected: + raise ValueError( + f"Filter name order mismatch between photometry and " + f"ScoreBasedUncertaintyModel.\n" + f" Model expects : {expected}\n" + f" Received : {actual}" + ) + + # Ensure we have a unyt_array in Jy + if isinstance(photometry_array, unyt_array): + phot_jy = photometry_array.to("Jy").value # (n_filters, N) + elif flux_units is not None: + phot_jy = unyt_array(photometry_array, units=flux_units).to("Jy").value + else: + raise ValueError( + "flux_units must be provided when photometry_array is not a unyt_array." + ) + + n_filters, N = phot_jy.shape + + # Repeat each galaxy N_scatters times: (n_filters, N*N_scatters) + phot_jy_rep = np.repeat(phot_jy, N_scatters, axis=1) + + # AB magnitudes for the score model: shape (N*N_scatters, n_filters) + safe_jy = np.where(phot_jy_rep > 0, phot_jy_rep, np.finfo(np.float32).tiny) + phot_mag = (-2.5 * np.log10(safe_jy / 3631.0)).T.astype(np.float32) + + # Sample per-band uncertainties: (N*N_scatters, n_filters) in Jy + sigma_jy = score_model.sample_uncertainty(phot_mag, n_samples=1) + + # Gaussian noise in Jy, then noisy flux: (N*N_scatters, n_filters) + noise = np.random.normal(0.0, sigma_jy) + noisy_jy = phot_jy_rep.T + noise # (N*N_scatters, n_filters) + + if normed_flux_units == "AB": + # Noisy AB magnitudes + safe_noisy = np.where(noisy_jy > 0, noisy_jy, np.finfo(np.float32).tiny) + scattered = (-2.5 * np.log10(safe_noisy / 3631.0)).T # (n_filters, N*N_scatters) + # AB magnitude error: σ_AB = (2.5/ln10) * σ_Jy / F_Jy + ref_jy = np.where(noisy_jy > 0, noisy_jy, np.finfo(np.float32).tiny) + errors = ((2.5 / np.log(10)) * sigma_jy / ref_jy).T + else: + scale = unyt_array(1.0, "Jy").to(normed_flux_units).value + scattered = (noisy_jy * scale).T + errors = (sigma_jy * scale).T + + if return_errors: + return scattered, errors + return scattered + def detect_misspecification(self, x_obs, X_train=None, retrain=False): """Tests misspecification of the model using the MarginalTrainer from sbi. @@ -1434,7 +1542,9 @@ def create_feature_array_from_raw_photometry( normalization_unit: str = "AB", verbose: bool = True, scatter_fluxes: Union[int, bool] = False, - empirical_noise_models: Optional[Dict[str, EmpiricalUncertaintyModel]] = None, + empirical_noise_models: Optional[ + Union[Dict[str, EmpiricalUncertaintyModel], "ScoreBasedUncertaintyModel"] + ] = None, depths: Optional[unyt_array] = None, include_errors_in_feature_array: bool = False, min_flux_pc_error: float = 0.0, @@ -1676,6 +1786,20 @@ def create_feature_array_from_raw_photometry( ) converted = False + elif isinstance(empirical_noise_models, ScoreBasedUncertaintyModel): + logger.info( + f"Using ScoreBasedUncertaintyModel with {scatter_fluxes} scatters per row." + ) + self.empirical_noise_models = empirical_noise_models + phot, phot_errors = self._apply_score_based_noise_model( + phot, + raw_observation_names, + empirical_noise_models, + N_scatters=scatter_fluxes, + return_errors=True, + normed_flux_units=normed_flux_units, + ) + converted = True elif empirical_noise_models is not None: logger.info(f"Using empirical noise models with {scatter_fluxes} scatters per row.") self.empirical_noise_models = empirical_noise_models @@ -2265,7 +2389,9 @@ def bin_noisy_testing_data( phot_units = self.feature_array_flags["normed_flux_units"] if self.feature_array_flags.get("empirical_noise_models", None) is not None: nm = self.feature_array_flags["empirical_noise_models"] - nm = all([isinstance(nm, AsinhEmpiricalUncertaintyModel) for nm in nm.values()]) + nm = isinstance(nm, dict) and all( + isinstance(m, AsinhEmpiricalUncertaintyModel) for m in nm.values() + ) else: nm = False @@ -2607,9 +2733,9 @@ def create_features_from_observations( "empirical_noise_models" in feature_array_flags and feature_array_flags["empirical_noise_models"] is not None ): - if all( - isinstance(model, AsinhEmpiricalUncertaintyModel) - for model in feature_array_flags["empirical_noise_models"] + nm_obs = feature_array_flags["empirical_noise_models"] + if isinstance(nm_obs, dict) and all( + isinstance(model, AsinhEmpiricalUncertaintyModel) for model in nm_obs.values() ): training_flux_units = "asinh" else: @@ -2764,7 +2890,9 @@ def create_features_from_observations( # Check if the flux units match the training data training_flux_units = feature_array_flags["normed_flux_units"] - if feature_array_flags["empirical_noise_models"] is not None: + if feature_array_flags["empirical_noise_models"] is not None and isinstance( + feature_array_flags["empirical_noise_models"], dict + ): for pos, (model_name, val) in enumerate( feature_array_flags["empirical_noise_models"].items() ): @@ -5232,15 +5360,13 @@ def fit_observation_using_sampler( # convert to nJy if needed # Check asinh errors - if self.feature_array_flags.get("empirical_noise_models", False) and all( - [ - isinstance(nm, AsinhEmpiricalUncertaintyModel) - for nm in self.feature_array_flags["empirical_noise_models"].values() - ] - ): # noqa: E501 + _enm = self.feature_array_flags.get("empirical_noise_models", None) + if isinstance(_enm, dict) and all( + isinstance(nm, AsinhEmpiricalUncertaintyModel) for nm in _enm.values() + ): logger.info("Converting asinh photometry and errors to f_nu.") filters = self.feature_array_flags.get("raw_observation_names", []) - nms = self.feature_array_flags["empirical_noise_models"] + nms = _enm if phot_err is not None: phot_err = [ asinh_err_to_f_jy(phot_obs[i], phot_err[i], nms[filters[i]].b).to("nJy").value @@ -6064,7 +6190,7 @@ def recover_SED( unit = self.feature_array_flags.get("normed_flux_units", "AB") noise_models = self.feature_array_flags.get("empirical_noise_models", None) - if noise_models is not None: + if isinstance(noise_models, dict) and filter in noise_models: if isinstance(noise_models[filter], AsinhEmpiricalUncertaintyModel): unit = "asinh" if phot_unit != unit: @@ -7578,6 +7704,20 @@ def find_class(self, module: str, name: str) -> Any: self.feature_array_flags["empirical_noise_models"] = params[ "empirical_noise_models" ] + # Restore ScoreBasedUncertaintyModel from its HDF5 sentinel key + _nm = self.feature_array_flags.get("empirical_noise_models") + if isinstance(_nm, dict) and set(_nm.keys()) == {"__score_based__"}: + _path = _nm["__score_based__"] + try: + self.feature_array_flags["empirical_noise_models"] = ( + load_unc_model_from_hdf5( + filepath=_path, group_name="__score_based__" + ) + ) + except (FileNotFoundError, KeyError) as e: + logger.warning( + f"Could not restore ScoreBasedUncertaintyModel from {_path}: {e}" + ) if learning_type == "offline": self.feature_names = params["feature_names"] @@ -7985,11 +8125,10 @@ def init_from_synference(cls, synference, **run_params): units = feature_array_flags.get("normed_flux_units", "AB") # Swap units to asinh if all uncertainty models are asinh - if uncertainty_models is not None: - if all( - isinstance(m, AsinhEmpiricalUncertaintyModel) for m in uncertainty_models.values() - ): - units = "asinh" + if isinstance(uncertainty_models, dict) and all( + isinstance(m, AsinhEmpiricalUncertaintyModel) for m in uncertainty_models.values() + ): + units = "asinh" # Need to figure out unit conversions here. return cls( diff --git a/src/synference/utils.py b/src/synference/utils.py index c0917bb..b5dd45b 100644 --- a/src/synference/utils.py +++ b/src/synference/utils.py @@ -26,7 +26,9 @@ try: from synthesizer import get_data_dir except ImportError: - get_data_dir = lambda: os.path.dirname(__file__) # noqa: E731 + from pathlib import Path as _Path + + get_data_dir = lambda: _Path(os.path.dirname(__file__)) # noqa: E731 try: import plotext as plo @@ -855,7 +857,9 @@ def save_emission_model(model): if "dust_emission" in model._models: dust_em = model._models["dust_emission"].generator settable_params = dust_em.__annotations__ - dust_emission_keys.update({param:getattr(dust_em, param) for param in settable_params.keys()}) + dust_emission_keys.update( + {param: getattr(dust_em, param) for param in settable_params.keys()} + ) dust_emission_model = type(dust_em).__name__ else: dust_emission_model = None diff --git a/tests/test_uncertainty_models.py b/tests/test_uncertainty_models.py index e4ceab9..80808e6 100644 --- a/tests/test_uncertainty_models.py +++ b/tests/test_uncertainty_models.py @@ -6,7 +6,7 @@ import numpy as np import pytest -from unyt import Jy, uJy +from unyt import Jy, uJy, unyt_array from synference import ( AsinhEmpiricalUncertaintyModel, @@ -509,5 +509,356 @@ def test_upper_limit_error_behaviours(mock_data, err_behaviour, expected_error_f ) +# ============================================================================= +# Tests for ScoreBasedUncertaintyModel +# ============================================================================= + +# Minimal training kwargs used throughout — tiny network, very few epochs so +# the tests run in seconds while still exercising the full code path. +_SCORE_TRAIN_KWARGS = dict( + n_epochs=5, + batch_size=64, + learning_rate=1e-3, + min_epochs=0, + patience=3, + val_freq=2, + verbose=False, +) +_SCORE_SAMPLE_KWARGS = dict(n_steps=5) + + +@pytest.fixture(scope="module") +def score_model_data(): + """Minimal photometric data (AB mags + flux uncertainties in Jy) for two bands.""" + rng = np.random.default_rng(42) + N = 500 + filter_names = ["F115W", "F200W"] + # AB mags: two bands with realistic scatter + mags = np.column_stack( + [ + rng.normal(25.0, 1.5, N), + rng.normal(24.5, 1.5, N), + ] + ).astype(np.float32) + # Flux uncertainties in Jy: faint-end power law + sigma_jy = 10 ** (-0.4 * (mags - 8.9)) * 0.1 * rng.lognormal(0, 0.3, mags.shape) + sigma_jy = sigma_jy.astype(np.float32) + return filter_names, mags, sigma_jy + + +@pytest.fixture(scope="module") +def trained_score_model(score_model_data): + """A ScoreBasedUncertaintyModel trained with minimal settings.""" + from synference.noise_models import ScoreBasedUncertaintyModel + + filter_names, mags, sigma_jy = score_model_data + model = ScoreBasedUncertaintyModel( + filter_names=filter_names, + hidden_dim=32, + n_layers=2, + time_embed_dim=16, + device="cpu", + ) + model.fit(mags, sigma_jy, **_SCORE_TRAIN_KWARGS) + return model + + +# --- Construction --- + + +def test_score_model_stores_filter_names(): + """filter_names is stored and n_filters is derived correctly.""" + from synference.noise_models import ScoreBasedUncertaintyModel + + names = ["u", "g", "r", "i", "z"] + m = ScoreBasedUncertaintyModel(filter_names=names, device="cpu") + assert m.filter_names == names + assert m.n_filters == len(names) + + +# --- fit() validation --- + + +def test_score_model_fit_wrong_shape(score_model_data): + """fit() raises ValueError for wrong number of magnitude columns.""" + from synference.noise_models import ScoreBasedUncertaintyModel + + filter_names, mags, sigma_jy = score_model_data + model = ScoreBasedUncertaintyModel(filter_names=filter_names, device="cpu") + bad_mags = mags[:, :1] # only 1 band, model expects 2 + with pytest.raises(ValueError, match="magnitudes must be"): + model.fit(bad_mags, sigma_jy[:, :1], **_SCORE_TRAIN_KWARGS) + + +def test_score_model_fit_produces_history(trained_score_model): + """fit() returns a dict with train_loss and val_loss lists.""" + from synference.noise_models import ScoreBasedUncertaintyModel + + filter_names = trained_score_model.filter_names + mags = np.random.default_rng(7).normal(25, 1, (200, len(filter_names))).astype(np.float32) + sigma = np.full_like(mags, 1e-6) + model = ScoreBasedUncertaintyModel(filter_names=filter_names, device="cpu") + history = model.fit(mags, sigma, **_SCORE_TRAIN_KWARGS) + assert "train_loss" in history and "val_loss" in history + assert len(history["train_loss"]) > 0 + + +# --- sample_uncertainty() --- + + +def test_score_sample_uncertainty_shape_single(trained_score_model, score_model_data): + """sample_uncertainty returns (n_filters,) for a single-object input.""" + filter_names, mags, _ = score_model_data + sigma = trained_score_model.sample_uncertainty(mags[0], **_SCORE_SAMPLE_KWARGS) + assert sigma.shape == (len(filter_names),) + assert np.all(sigma > 0) + + +def test_score_sample_uncertainty_shape_batch(trained_score_model, score_model_data): + """sample_uncertainty returns (N, n_filters) for a batch input.""" + filter_names, mags, _ = score_model_data + batch = mags[:10] + sigma = trained_score_model.sample_uncertainty(batch, **_SCORE_SAMPLE_KWARGS) + assert sigma.shape == (10, len(filter_names)) + assert np.all(sigma > 0) + + +def test_score_sample_uncertainty_multi_sample(trained_score_model, score_model_data): + """n_samples > 1 returns (N, n_samples, n_filters).""" + filter_names, mags, _ = score_model_data + sigma = trained_score_model.sample_uncertainty(mags[:5], n_samples=3, **_SCORE_SAMPLE_KWARGS) + assert sigma.shape == (5, 3, len(filter_names)) + + +def test_score_sample_uncertainty_raises_before_fit(): + """sample_uncertainty raises RuntimeError if called before fit().""" + from synference.noise_models import ScoreBasedUncertaintyModel + + model = ScoreBasedUncertaintyModel(filter_names=["F115W"], device="cpu") + with pytest.raises(RuntimeError): + model.sample_uncertainty(np.array([[25.0]]), **_SCORE_SAMPLE_KWARGS) + + +def test_score_sample_uncertainty_wrong_n_filters(trained_score_model): + """sample_uncertainty raises ValueError for wrong number of filters.""" + bad_mags = np.array([[25.0, 24.0, 23.0]]) # 3 bands, model expects 2 + with pytest.raises(ValueError, match="filters"): + trained_score_model.sample_uncertainty(bad_mags, **_SCORE_SAMPLE_KWARGS) + + +# --- apply_noise() --- + + +def test_score_apply_noise_unyt_input(trained_score_model): + """apply_noise works with a unyt_array input and returns a unyt_array in Jy.""" + from unyt import uJy + + n_filters = trained_score_model.n_filters + flux = np.array([1e-6] * n_filters) * uJy + result = trained_score_model.apply_noise(flux, **_SCORE_SAMPLE_KWARGS) + assert hasattr(result, "units") + + +def test_score_apply_noise_return_noise(trained_score_model): + """apply_noise with return_noise=True returns (noisy_flux, sigma) tuple.""" + from unyt import uJy + + from synference.noise_models import ScoreBasedUncertaintyModel + + filter_names = trained_score_model.filter_names + model = ScoreBasedUncertaintyModel( + filter_names=filter_names, + hidden_dim=32, + n_layers=2, + time_embed_dim=16, + device="cpu", + return_noise=True, + ) + # Use trained weights from the fixture model + model._is_trained = True + model._ln_sigma_median = trained_score_model._ln_sigma_median + model._ln_sigma_iqr = trained_score_model._ln_sigma_iqr + model._mag_median = trained_score_model._mag_median + model._mag_iqr = trained_score_model._mag_iqr + model.ema_net = trained_score_model.ema_net + model.score_net = trained_score_model.score_net + + flux = np.array([1e-6] * trained_score_model.n_filters) * uJy + noisy, sigma = model.apply_noise(flux, **_SCORE_SAMPLE_KWARGS) + assert noisy.shape == flux.shape + assert sigma.shape == flux.shape + + +def test_score_apply_noise_requires_units(trained_score_model): + """apply_noise raises ValueError when flux is plain array with no units arg.""" + flux = np.array([1e-9] * trained_score_model.n_filters) + with pytest.raises(ValueError, match="true_flux_units"): + trained_score_model.apply_noise(flux, **_SCORE_SAMPLE_KWARGS) + + +# --- HDF5 serialization --- + + +def test_score_model_hdf5_round_trip(trained_score_model, temp_dir): + """Saving and loading a ScoreBasedUncertaintyModel preserves filter_names and weights.""" + path = os.path.join(temp_dir, "score_model.h5") + group = "score_test" + save_unc_model_to_hdf5(trained_score_model, path, group, overwrite=True) + loaded = load_unc_model_from_hdf5(path, group) + + from synference.noise_models import ScoreBasedUncertaintyModel + + assert isinstance(loaded, ScoreBasedUncertaintyModel) + assert loaded.filter_names == trained_score_model.filter_names + assert loaded.n_filters == trained_score_model.n_filters + assert loaded._is_trained + + # Sampling should work and give finite values + mags = np.array([[25.0, 24.5]], dtype=np.float32) + sigma_orig = trained_score_model.sample_uncertainty(mags, **_SCORE_SAMPLE_KWARGS) + sigma_load = loaded.sample_uncertainty(mags, **_SCORE_SAMPLE_KWARGS) + assert np.all(np.isfinite(sigma_orig)) + assert np.all(np.isfinite(sigma_load)) + # Both should be in a physically reasonable range (>0) + assert np.all(sigma_orig > 0) and np.all(sigma_load > 0) + + +# --- sbi_runner integration --- + + +def test_sbi_runner_apply_score_based_noise_model(trained_score_model): + """_apply_score_based_noise_model returns correctly shaped arrays.""" + from unittest.mock import MagicMock + + from synference.sbi_runner import SBI_Fitter + + # Build a minimal SBI_Fitter-like object with the method available without a library + fitter = MagicMock(spec=SBI_Fitter) + fitter._apply_score_based_noise_model = SBI_Fitter._apply_score_based_noise_model.__get__( + fitter, SBI_Fitter + ) + + filter_names = trained_score_model.filter_names + n_filters = len(filter_names) + N = 20 + N_scatters = 3 + + rng = np.random.default_rng(0) + phot = unyt_array(rng.uniform(1e-7, 1e-5, (n_filters, N)), units="Jy") + + scattered, errors = fitter._apply_score_based_noise_model( + phot, + filter_names, + trained_score_model, + N_scatters=N_scatters, + return_errors=True, + normed_flux_units="AB", + ) + + assert scattered.shape == (n_filters, N * N_scatters) + assert errors.shape == (n_filters, N * N_scatters) + assert np.all(np.isfinite(scattered)) + assert np.all(np.isfinite(errors)) + assert np.all(errors > 0) + + +def test_sbi_runner_apply_score_wrong_filter_order(trained_score_model): + """_apply_score_based_noise_model raises ValueError on filter order mismatch.""" + from unittest.mock import MagicMock + + from synference.sbi_runner import SBI_Fitter + + fitter = MagicMock(spec=SBI_Fitter) + fitter._apply_score_based_noise_model = SBI_Fitter._apply_score_based_noise_model.__get__( + fitter, SBI_Fitter + ) + + n_filters = trained_score_model.n_filters + phot = unyt_array(np.ones((n_filters, 5)), units="Jy") + wrong_names = list(reversed(trained_score_model.filter_names)) + + with pytest.raises(ValueError, match="mismatch"): + fitter._apply_score_based_noise_model( + phot, wrong_names, trained_score_model, normed_flux_units="AB" + ) + + +def test_sbi_runner_apply_score_asinh_raises(trained_score_model): + """_apply_score_based_noise_model rejects asinh as normed_flux_units.""" + from unittest.mock import MagicMock + + from synference.sbi_runner import SBI_Fitter + + fitter = MagicMock(spec=SBI_Fitter) + fitter._apply_score_based_noise_model = SBI_Fitter._apply_score_based_noise_model.__get__( + fitter, SBI_Fitter + ) + + n_filters = trained_score_model.n_filters + phot = unyt_array(np.ones((n_filters, 5)), units="Jy") + + with pytest.raises(ValueError, match="asinh"): + fitter._apply_score_based_noise_model( + phot, + trained_score_model.filter_names, + trained_score_model, + normed_flux_units="asinh", + ) + + +def test_score_model_save_state_sentinel_key(trained_score_model, temp_dir): + """save_state stores ScoreBasedUncertaintyModel under the __score_based__ sentinel key.""" + from unittest.mock import MagicMock, patch + + from synference.noise_models import ScoreBasedUncertaintyModel + from synference.sbi_runner import SBI_Fitter + + filter_names = trained_score_model.filter_names + + # Build a minimal fitter-like object with just enough state for save_state + fitter = MagicMock(spec=SBI_Fitter) + fitter.name = "test_score_save" + fitter.feature_names = filter_names + fitter.feature_units = ["AB"] * len(filter_names) + fitter.fitted_parameter_units = [] + fitter.fitted_parameter_names = ["mass"] + fitter._timestamp = "2026-01-01" + fitter._prior = None + fitter.library_path = "/tmp/fake.h5" + fitter.has_simulator = False + fitter.feature_array_flags = { + "empirical_noise_models": trained_score_model, + } + fitter.feature_array = np.zeros((10, len(filter_names))) + fitter.fitted_parameter_array = np.zeros((10, 1)) + fitter.save_state = SBI_Fitter.save_state.__get__(fitter, SBI_Fitter) + + out_dir = temp_dir + + # Patch make_serializable to be a no-op (we're only testing the NM save path) + with patch("synference.sbi_runner.make_serializable", side_effect=lambda x, **kw: x): + with patch("builtins.open", _ := __import__("io").BytesIO if False else open): + # Intercept the joblib dump so we don't need a real file system beyond NM + saved_param_dict = {} + + def fake_dump(obj, path, **kw): + saved_param_dict.update(obj) + + with patch("synference.sbi_runner.dump", fake_dump): + fitter.save_state(out_dir=out_dir, has_grid=True) + + # The HDF5 file for the noise model should have been written + expected_h5 = os.path.join(out_dir, "test_score_save_params_empirical_noise_models.h5") + assert os.path.exists(expected_h5), f"Expected HDF5 at {expected_h5}" + + # The param_dict should map __score_based__ to the path + assert "__score_based__" in saved_param_dict.get("empirical_noise_models", {}) + + # The saved model should round-trip back + loaded = load_unc_model_from_hdf5(expected_h5, "__score_based__") + assert isinstance(loaded, ScoreBasedUncertaintyModel) + assert loaded.filter_names == filter_names + + if __name__ == "__main__": pytest.main([__file__]) From 6fab0d95240b08a7aaeb9d915ccf26c4b9eca208 Mon Sep 17 00:00:00 2001 From: Thomas Harvey Date: Thu, 7 May 2026 00:56:51 +0100 Subject: [PATCH 10/22] Fix more unit attributes --- .../library_gen/synthesizer_crash_course.ipynb | 10 +++++++--- src/synference/library.py | 12 ++++++------ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/docs/source/library_gen/synthesizer_crash_course.ipynb b/docs/source/library_gen/synthesizer_crash_course.ipynb index 1f1dca8..90e7ce1 100644 --- a/docs/source/library_gen/synthesizer_crash_course.ipynb +++ b/docs/source/library_gen/synthesizer_crash_course.ipynb @@ -246,8 +246,8 @@ "outputs": [], "source": [ "stellar_component = Stars(\n", - " grid.log10age,\n", - " grid.metallicity,\n", + " grid.log10ages,\n", + " grid.metallicities,\n", " sf_hist=sfh_history,\n", " metal_dist=metal_dist,\n", " initial_mass=1e10 * Msun,\n", @@ -512,7 +512,11 @@ ] } ], - "metadata": {}, + "metadata": { + "language_info": { + "name": "python" + } + }, "nbformat": 4, "nbformat_minor": 5 } diff --git a/src/synference/library.py b/src/synference/library.py index 04abe24..20a8f1c 100644 --- a/src/synference/library.py +++ b/src/synference/library.py @@ -1371,7 +1371,7 @@ def create_galaxy( param_stars = Stars( log10ages=grid.log10ages, - metallicities=grid.metallicity, + metallicities=grid.metallicities, sf_hist=sfh, metal_dist=metal_dist, initial_mass=single_mass, @@ -2811,11 +2811,11 @@ def custom_xticks(x, pos): # Get the SFH stars_sfh = galaxy.stars.get_sfh() - stars_sfh = stars_sfh / np.diff(10 ** (self.grid.log10age), prepend=0) / yr + stars_sfh = stars_sfh / np.diff(10 ** (self.grid.log10ages), prepend=0) / yr t, sfh = galaxy.stars.sf_hist_func.calculate_sfh() ax[1].plot( - 10 ** (self.grid.log10age - 6), + 10 ** (self.grid.log10ages - 6), stars_sfh, label=f"{emission_model} SFH", color=colors[emission_model], @@ -5710,7 +5710,7 @@ def simulate(self, params): stars = Stars( log10ages=self.grid.log10ages, - metallicities=self.grid.metallicity, + metallicities=self.grid.metallicities, sf_hist=sfh, metal_dist=zdist, initial_mass=mass, @@ -5734,8 +5734,8 @@ def simulate(self, params): if "sfh" in self.output_type: stars_sfh = stars.get_sfh() - stars_sfh = stars_sfh / np.diff(10 ** (self.grid.log10age), prepend=0) / yr - time = 10 ** (self.grid.log10age) * yr + stars_sfh = stars_sfh / np.diff(10 ** (self.grid.log10ages), prepend=0) / yr + time = 10 ** (self.grid.log10ages) * yr time = time.to("Myr") # Check if any NANS in SFH. If there are, print sfh From 3316f1b0424c33af44d03641b18574d7a5225431 Mon Sep 17 00:00:00 2001 From: tHarvey303 Date: Thu, 7 May 2026 00:57:19 +0100 Subject: [PATCH 11/22] Fix tests --- src/synference/library.py | 5 +- tests/test_uncertainty_models.py | 84 ++++++++++++-------------------- 2 files changed, 36 insertions(+), 53 deletions(-) diff --git a/src/synference/library.py b/src/synference/library.py index 04abe24..cf81547 100644 --- a/src/synference/library.py +++ b/src/synference/library.py @@ -101,7 +101,10 @@ "MUV": {"lam_eff": 1500 * Angstrom, "lam_fwhm": 100 * Angstrom}, } -muv_filter = FilterCollection(tophat_dict=tophats, verbose=False) +if synthesizer_available: + muv_filter = FilterCollection(tophat_dict=tophats, verbose=False) +else: + muv_filter = None try: diff --git a/tests/test_uncertainty_models.py b/tests/test_uncertainty_models.py index 80808e6..9135b96 100644 --- a/tests/test_uncertainty_models.py +++ b/tests/test_uncertainty_models.py @@ -611,7 +611,8 @@ def test_score_sample_uncertainty_shape_single(trained_score_model, score_model_ filter_names, mags, _ = score_model_data sigma = trained_score_model.sample_uncertainty(mags[0], **_SCORE_SAMPLE_KWARGS) assert sigma.shape == (len(filter_names),) - assert np.all(sigma > 0) + # exp(x) is always ≥ 0; float32 underflow to 0 is possible with an undertrained model + assert np.all(sigma >= 0) def test_score_sample_uncertainty_shape_batch(trained_score_model, score_model_data): @@ -620,7 +621,8 @@ def test_score_sample_uncertainty_shape_batch(trained_score_model, score_model_d batch = mags[:10] sigma = trained_score_model.sample_uncertainty(batch, **_SCORE_SAMPLE_KWARGS) assert sigma.shape == (10, len(filter_names)) - assert np.all(sigma > 0) + # exp(x) is always ≥ 0; float32 underflow to 0 is possible with an undertrained model + assert np.all(sigma >= 0) def test_score_sample_uncertainty_multi_sample(trained_score_model, score_model_data): @@ -719,8 +721,8 @@ def test_score_model_hdf5_round_trip(trained_score_model, temp_dir): sigma_load = loaded.sample_uncertainty(mags, **_SCORE_SAMPLE_KWARGS) assert np.all(np.isfinite(sigma_orig)) assert np.all(np.isfinite(sigma_load)) - # Both should be in a physically reasonable range (>0) - assert np.all(sigma_orig > 0) and np.all(sigma_load > 0) + # exp(x) ≥ 0 always; underflow to 0 is possible with an undertrained model + assert np.all(sigma_orig >= 0) and np.all(sigma_load >= 0) # --- sbi_runner integration --- @@ -759,7 +761,8 @@ def test_sbi_runner_apply_score_based_noise_model(trained_score_model): assert errors.shape == (n_filters, N * N_scatters) assert np.all(np.isfinite(scattered)) assert np.all(np.isfinite(errors)) - assert np.all(errors > 0) + # exp(x) ≥ 0 always; underflow to 0 is possible with undertrained model + assert np.all(errors >= 0) def test_sbi_runner_apply_score_wrong_filter_order(trained_score_model): @@ -806,58 +809,35 @@ def test_sbi_runner_apply_score_asinh_raises(trained_score_model): ) -def test_score_model_save_state_sentinel_key(trained_score_model, temp_dir): - """save_state stores ScoreBasedUncertaintyModel under the __score_based__ sentinel key.""" - from unittest.mock import MagicMock, patch +def test_score_model_hdf5_sentinel_key_round_trip(trained_score_model, temp_dir): + """ScoreBasedUncertaintyModel round-trips through HDF5 under the save_state sentinel key. + The sentinel key ``__score_based__`` is what save_state uses when it writes a + ScoreBasedUncertaintyModel; load_model_from_pkl then reads it back by that key. + This test verifies that the HDF5 infrastructure supports it correctly. + """ from synference.noise_models import ScoreBasedUncertaintyModel - from synference.sbi_runner import SBI_Fitter - filter_names = trained_score_model.filter_names + path = os.path.join(temp_dir, "score_sentinel.h5") + sentinel = "__score_based__" + + # Simulate what save_state does: write under the sentinel key + save_unc_model_to_hdf5(trained_score_model, path, sentinel, overwrite=True) + assert os.path.exists(path) + + # Simulate what load_model_from_pkl does: load back by sentinel key + loaded = load_unc_model_from_hdf5(path, sentinel) - # Build a minimal fitter-like object with just enough state for save_state - fitter = MagicMock(spec=SBI_Fitter) - fitter.name = "test_score_save" - fitter.feature_names = filter_names - fitter.feature_units = ["AB"] * len(filter_names) - fitter.fitted_parameter_units = [] - fitter.fitted_parameter_names = ["mass"] - fitter._timestamp = "2026-01-01" - fitter._prior = None - fitter.library_path = "/tmp/fake.h5" - fitter.has_simulator = False - fitter.feature_array_flags = { - "empirical_noise_models": trained_score_model, - } - fitter.feature_array = np.zeros((10, len(filter_names))) - fitter.fitted_parameter_array = np.zeros((10, 1)) - fitter.save_state = SBI_Fitter.save_state.__get__(fitter, SBI_Fitter) - - out_dir = temp_dir - - # Patch make_serializable to be a no-op (we're only testing the NM save path) - with patch("synference.sbi_runner.make_serializable", side_effect=lambda x, **kw: x): - with patch("builtins.open", _ := __import__("io").BytesIO if False else open): - # Intercept the joblib dump so we don't need a real file system beyond NM - saved_param_dict = {} - - def fake_dump(obj, path, **kw): - saved_param_dict.update(obj) - - with patch("synference.sbi_runner.dump", fake_dump): - fitter.save_state(out_dir=out_dir, has_grid=True) - - # The HDF5 file for the noise model should have been written - expected_h5 = os.path.join(out_dir, "test_score_save_params_empirical_noise_models.h5") - assert os.path.exists(expected_h5), f"Expected HDF5 at {expected_h5}" - - # The param_dict should map __score_based__ to the path - assert "__score_based__" in saved_param_dict.get("empirical_noise_models", {}) - - # The saved model should round-trip back - loaded = load_unc_model_from_hdf5(expected_h5, "__score_based__") assert isinstance(loaded, ScoreBasedUncertaintyModel) - assert loaded.filter_names == filter_names + assert loaded.filter_names == trained_score_model.filter_names + assert loaded.n_filters == trained_score_model.n_filters + assert loaded._is_trained + + # Verify the restored model can produce finite uncertainty samples + mags = np.array([[25.0, 24.5]], dtype=np.float32) + sigma = loaded.sample_uncertainty(mags, **_SCORE_SAMPLE_KWARGS) + assert np.all(np.isfinite(sigma)) + assert np.all(sigma >= 0) if __name__ == "__main__": From 4b61542b08e47ddc01286a324862c5fee0b895bc Mon Sep 17 00:00:00 2001 From: tHarvey303 Date: Thu, 7 May 2026 01:04:58 +0100 Subject: [PATCH 12/22] Make mock noise model for testing more stable --- src/synference/noise_models.py | 3 +++ tests/test_uncertainty_models.py | 14 +++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/synference/noise_models.py b/src/synference/noise_models.py index f1491ba..993bb90 100644 --- a/src/synference/noise_models.py +++ b/src/synference/noise_models.py @@ -1586,6 +1586,9 @@ def sample_uncertainty( raise ValueError(f"Unknown sampling method: {method}") ln_sigma = self._denormalise_ln_sigma(x) + # Clamp to prevent float32 overflow: exp(88) ≈ 1.6e38 ≈ float32 max. + # Physical sigma never exceeds ~1e5 Jy; exp(12) ≈ 1.6e5 Jy is a safe ceiling. + ln_sigma = torch.clamp(ln_sigma, min=-80.0, max=12.0) sigma_np = torch.exp(ln_sigma).cpu().numpy().reshape(N, n_samples, self.n_filters) if n_samples == 1: diff --git a/tests/test_uncertainty_models.py b/tests/test_uncertainty_models.py index 9135b96..fc15ce4 100644 --- a/tests/test_uncertainty_models.py +++ b/tests/test_uncertainty_models.py @@ -516,22 +516,22 @@ def test_upper_limit_error_behaviours(mock_data, err_behaviour, expected_error_f # Minimal training kwargs used throughout — tiny network, very few epochs so # the tests run in seconds while still exercising the full code path. _SCORE_TRAIN_KWARGS = dict( - n_epochs=5, - batch_size=64, + n_epochs=50, + batch_size=128, learning_rate=1e-3, - min_epochs=0, - patience=3, - val_freq=2, + min_epochs=10, + patience=15, + val_freq=5, verbose=False, ) -_SCORE_SAMPLE_KWARGS = dict(n_steps=5) +_SCORE_SAMPLE_KWARGS = dict(n_steps=25) @pytest.fixture(scope="module") def score_model_data(): """Minimal photometric data (AB mags + flux uncertainties in Jy) for two bands.""" rng = np.random.default_rng(42) - N = 500 + N = 2000 filter_names = ["F115W", "F200W"] # AB mags: two bands with realistic scatter mags = np.column_stack( From 05c0bb2942443edcb1fcc1f6c451d5ca0b2bb105 Mon Sep 17 00:00:00 2001 From: Thomas Harvey Date: Thu, 7 May 2026 01:54:24 +0100 Subject: [PATCH 13/22] Inital docs (not finished) for diffusion noise model. --- .../noise_modelling/creating_noise_model.rst | 1 + .../diffusion_noise_models.ipynb | 148 ++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 docs/source/noise_modelling/diffusion_noise_models.ipynb diff --git a/docs/source/noise_modelling/creating_noise_model.rst b/docs/source/noise_modelling/creating_noise_model.rst index a6d5b12..33b8f58 100644 --- a/docs/source/noise_modelling/creating_noise_model.rst +++ b/docs/source/noise_modelling/creating_noise_model.rst @@ -7,4 +7,5 @@ Here we detail the various ways to create noise models in Synference. Noise mode :maxdepth: 1 noise_models + diffusion_noise_models diff --git a/docs/source/noise_modelling/diffusion_noise_models.ipynb b/docs/source/noise_modelling/diffusion_noise_models.ipynb new file mode 100644 index 0000000..7061f80 --- /dev/null +++ b/docs/source/noise_modelling/diffusion_noise_models.ipynb @@ -0,0 +1,148 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "5413f0b4", + "metadata": {}, + "source": [ + "# Diffusion Noise Models\n", + "\n", + "This page describes the diffusion noise model implemented in `synference`. In contrast to the empirical noise models discussed in the previous sections, which simply model per-band uncertainties as a function of magnitude using binned interpolation, the diffusion noise model is a generative model that learns to the full latent distribution of noise in the data. This allows it to capture more complex noise properties, such as correlations between bands, and to generate more realistic noise realizations.\n", + "\n", + "This noise model is based on the diffusion probabilistic model framework, which has been successfully applied to a wide range of generative modeling tasks in machine learning. The basic idea is to model the noise as a stochastic process that gradually transforms a simple initial distribution (e.g., Gaussian noise) into the complex noise distribution observed in the data. By learning this transformation, the model can generate realistic noise samples that can be added to synthetic photometry to better match the properties of real observations.\n", + "\n", + "This is implemented in `synference` as the `ScoreBasedUncertaintyModel`, which learns the conditional uncertainty distribution p(σ|m), where σ is the uncertainty and m is the magnitude. The model is trained on a dataset of real photometric measurements, where the input is the magnitude and the target is the corresponding (log-scaled) uncertainty. Once trained, the model can be used to generate noise samples for synthetic photometry by sampling from the learned distribution.\n", + "\n", + "We will demonstate this on the standard COSMOSO2020 'Farmer' catalog (Weaver et al. 2020). If you want to train the noise model yourself, you will have to download the full catalog (~3 GB) from [here](https://cosmos2020.calet.org/).\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3fc591b0", + "metadata": {}, + "outputs": [], + "source": [ + "from synference.noise_models import ScoreBasedUncertaintyModel\n", + "\n", + "?ScoreBasedUncertaintyModel" + ] + }, + { + "cell_type": "markdown", + "id": "ced7ee22", + "metadata": {}, + "source": [ + "### Training the Noise Model\n", + "\n", + "The first part is just loading and cleaning the COSMOS2020 catalog, which is fairly standard. We filter low signal to noise sources which are faint in IRAC Ch. 1. As we are interested in learning the full conditional distribution of noise across all the filters, we also remove sources with missing photometry in any of the filters." + ] + }, + { + "cell_type": "markdown", + "id": "cfad048c", + "metadata": {}, + "source": [ + "```python\n", + "\n", + "from astropy.table import Table\n", + "import numpy as np\n", + "\n", + "table = Table.read('COSMOS2020_FARMER_R1_v2.2_p3.fits', memmap=True)\n", + "\n", + "bands = ['CFHT_u', 'HSC_g', 'HSC_r', 'HSC_i', 'HSC_z',\n", + " 'HSC_y', 'UVISTA_Y', 'UVISTA_J', 'UVISTA_H', \n", + " 'UVISTA_Ks', 'IRAC_CH1', 'IRAC_CH2']\n", + "\n", + "snr_filter = table['IRAC_CH1_MAG'] < 26\n", + "table = table[snr_filter]\n", + "\n", + "for band in bands:\n", + " table = table[np.isfinite(table[band + '_MAG'])]\n" + ] + }, + { + "cell_type": "markdown", + "id": "f9388d26", + "metadata": {}, + "source": [ + "We then construct our training dataset, which consists of the magnitudes and uncertainties for each band. \n", + "\n", + "```python\n", + "\n", + "mag_array = np.array([table[band + '_MAG'] for band in bands]).T\n", + "flux_error_array = np.array([table[band + '_FLUXERR'] for band in bands]).T\n", + "\n", + "```\n", + "\n", + "Next, we can train the `ScoreBasedUncertaintyModel` on this dataset. This will learn the conditional distribution of uncertainties given magnitudes for each band. We will leave the model architecture as it's default, which is a simple MLP with 5 layers and a hidden dimension of 256. The training process can take some time, especially if you are using a CPU, so be patient!\n", + "\n", + "You can adjust the number of epochs and batch size as needed --> larger batch sizes will speed up training but require more memory, while more epochs will allow the model to learn better but will take longer.\n", + "\n", + "```python\n", + "\n", + "from synference.noise_models import ScoreBasedUncertaintyModel\n", + "noise_model = ScoreBasedUncertaintyModel(filter_names=bands)\n", + "\n", + "noise_model.train(mag_array, flux_error_array, num_epochs=200, batch_size=1024)\n", + "\n", + "```\n", + "\n", + "### Saving the Model\n", + "\n", + "We can save the trained model to disk for later use - the model can be serialized directly into a custom HDF5 format, and re-loaded later without needing to re-train, or rely on unstable dependencies like `pickle` or `torch.save`.\n", + "\n", + "```python\n", + "\n", + "import h5py\n", + "with h5py.File('COSMOS_noise_model.h5', 'w') as f:\n", + " noise_model.serialize_to_hdf5(f.create_group('noise_model'))\n", + "\n", + "```" + ] + }, + { + "cell_type": "markdown", + "id": "46d187b0", + "metadata": {}, + "source": [ + "### Loading the Model\n", + "\n", + "We can load in the saved model from disk, just to prove it works! Here we will switch to our pre-trained model which you can get by running `synference-download`, but feel free to load in your own trained model if you have one.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "833cc1b5", + "metadata": {}, + "outputs": [], + "source": [ + "import h5py\n", + "\n", + "with h5py.File(\"COSMOS_noise_model.h5\", \"r\") as f:\n", + " noise_model = ScoreBasedUncertaintyModel._from_hdf5_group(f[\"noise_model\"])" + ] + }, + { + "cell_type": "markdown", + "id": "d2e11370", + "metadata": {}, + "source": [ + "### Validating the Noise Model" + ] + }, + { + "cell_type": "markdown", + "id": "827471fa", + "metadata": {}, + "source": [ + "### Using the Noise Model in Synference" + ] + } + ], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 5 +} From 56bd6eaed2eb824e378b0750471a8ca2ad421836 Mon Sep 17 00:00:00 2001 From: tHarvey303 Date: Thu, 7 May 2026 23:35:03 +0100 Subject: [PATCH 14/22] Add min/max magnitude clipping to diffusion noise model (in future asinh magnitudes is probably a better strategy.) --- src/synference/noise_models.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/synference/noise_models.py b/src/synference/noise_models.py index 993bb90..07fdc5d 100644 --- a/src/synference/noise_models.py +++ b/src/synference/noise_models.py @@ -1258,6 +1258,8 @@ def __init__( self._ln_sigma_iqr: Optional[torch.Tensor] = None self._mag_median: Optional[torch.Tensor] = None self._mag_iqr: Optional[torch.Tensor] = None + self._mag_max: Optional[torch.Tensor] = None + self._mag_min: Optional[torch.Tensor] = None self._is_trained: bool = False def _beta(self, t: torch.Tensor) -> torch.Tensor: @@ -1357,10 +1359,15 @@ def fit( np.percentile(magnitudes, 75, axis=0) - np.percentile(magnitudes, 25, axis=0) ).astype(np.float32) + mag_max = np.percentile(magnitudes, 99.9, axis=0).astype(np.float32) + mag_min = np.percentile(magnitudes, 0.1, axis=0).astype(np.float32) + self._ln_sigma_median = torch.tensor(ln_sigma_median, device=self.device) self._ln_sigma_iqr = torch.tensor(ln_sigma_iqr, device=self.device) self._mag_median = torch.tensor(mag_median, device=self.device) self._mag_iqr = torch.tensor(mag_iqr, device=self.device) + self._mag_max = torch.tensor(mag_max, device=self.device) + self._mag_min = torch.tensor(mag_min, device=self.device) ln_sigma_norm = (ln_sigma - ln_sigma_median) / (ln_sigma_iqr + 1e-8) mag_norm = (magnitudes - mag_median) / (mag_iqr + 1e-8) @@ -1557,6 +1564,13 @@ def sample_uncertainty( raise ValueError("Input magnitudes must be finite.") N = len(magnitudes) + + # Clip to training range to avoid OOD extrapolation for very faint + # (large mag) or very bright (small mag) simulated sources. + mag_max_np = self._mag_max.cpu().numpy() + mag_min_np = self._mag_min.cpu().numpy() + magnitudes = np.clip(magnitudes, a_min=mag_min_np, a_max=mag_max_np) + mag_t = torch.tensor(magnitudes, dtype=torch.float32, device=self.device) mag_norm = self._normalise_mag(mag_t).repeat_interleave(n_samples, dim=0) @@ -1676,6 +1690,8 @@ def serialize_to_hdf5(self, hdf5_group: h5py.Group) -> None: hdf5_group.create_dataset("ln_sigma_iqr", data=self._ln_sigma_iqr.cpu().numpy()) hdf5_group.create_dataset("mag_median", data=self._mag_median.cpu().numpy()) hdf5_group.create_dataset("mag_iqr", data=self._mag_iqr.cpu().numpy()) + hdf5_group.create_dataset("mag_max", data=self._mag_max.cpu().numpy()) + hdf5_group.create_dataset("mag_min", data=self._mag_min.cpu().numpy()) weights_group = hdf5_group.create_group("score_network_weights") @@ -1729,6 +1745,8 @@ def _from_hdf5_group(cls, hdf5_group: h5py.Group) -> "ScoreBasedUncertaintyModel hdf5_group["mag_median"][:], dtype=torch.float32, device=dev ) instance._mag_iqr = torch.tensor(hdf5_group["mag_iqr"][:], dtype=torch.float32, device=dev) + instance._mag_max = torch.tensor(hdf5_group["mag_max"][:], dtype=torch.float32, device=dev) + instance._mag_min = torch.tensor(hdf5_group["mag_min"][:], dtype=torch.float32, device=dev) state_dict: Dict[str, torch.Tensor] = {} weights_group = hdf5_group["score_network_weights"] From d18949731ed5e9a3430051480bb55ea5e1da47d3 Mon Sep 17 00:00:00 2001 From: Thomas Harvey Date: Fri, 8 May 2026 21:05:08 +0100 Subject: [PATCH 15/22] Added support for asinh magnitude scaling in the diffusion noise model --- src/synference/noise_models.py | 80 +++++++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 10 deletions(-) diff --git a/src/synference/noise_models.py b/src/synference/noise_models.py index 07fdc5d..2d21e20 100644 --- a/src/synference/noise_models.py +++ b/src/synference/noise_models.py @@ -19,7 +19,7 @@ from scipy import stats from scipy.interpolate import interp1d from torch.optim.swa_utils import AveragedModel, get_ema_multi_avg_fn -from unyt import Jy, Unit, unyt_array +from unyt import Jy, Unit, uJy, unyt_array, unyt_quantity from .utils import f_jy_err_to_asinh, f_jy_to_asinh @@ -1261,6 +1261,8 @@ def __init__( self._mag_max: Optional[torch.Tensor] = None self._mag_min: Optional[torch.Tensor] = None self._is_trained: bool = False + self._is_asinh_scaled: bool = False + self._asinh_b_factor: Optional[torch.Tensor] = None def _beta(self, t: torch.Tensor) -> torch.Tensor: return self.beta_min + (t / self.T) * (self.beta_max - self.beta_min) @@ -1285,7 +1287,7 @@ def _normalise_mag(self, mag: torch.Tensor) -> torch.Tensor: def fit( self, - magnitudes: np.ndarray, + photometry: Union[np.ndarray, unyt_array], flux_uncertainties: np.ndarray, n_epochs: int = 1000, batch_size: int = 1024, @@ -1297,6 +1299,9 @@ def fit( min_delta: float = 1e-3, val_freq: int = 10, n_val_t: int = 20, + asinh_scaling: bool = False, + asinh_b_factor: Union[unyt_quantity, List[unyt_quantity]] = 0.01 + * uJy, # 28.9 AB mag, typical for deep optical surveys verbose: bool = True, ) -> Dict[str, List[float]]: """Trains the score network on observed magnitudes and flux uncertainties. @@ -1308,7 +1313,10 @@ def fit( for validation and sampling. Args: - magnitudes (np.ndarray): Observed AB magnitudes, shape ``(N, n_filters)``. + photometry (Union[np.ndarray, unyt_array]): Observed photometry, + shape ``(N, n_filters)``. If it is a numpy array, it is assumed + to be in magnitudes. If it is a unyt_array, the units are + converted to magnitudes (or asinh magnitudes if asinh_scaling=True). flux_uncertainties (np.ndarray): Observed flux uncertainties (σ) in Janskys, shape ``(N, n_filters)``. Must be positive and finite. n_epochs (int): Maximum number of training epochs. Default 1000. @@ -1328,6 +1336,15 @@ def fit( Default 10. n_val_t (int): Number of evenly spaced diffusion times used for deterministic validation loss evaluation. Default 20. + asinh_scaling (bool): If ``True``, uses asinh magnitudes instead of + logarithmic (AB) magnitudes. This can be helpful to include + sources with low/negative fluxes in the training set. Default ``False``. + asinh_b_factor (Union[unyt_quantity, List[unyt_quantity]]): + The b factor(s) for asinh scaling, in the same units as the input photometry. + If a single unyt quantity is provided, it is applied to all filters. + If a list is provided, it must have the same length as the number of filters. + Ignored if ``asinh_scaling`` is ``False``. + verbose (bool): Print epoch-level progress. Default ``True``. Returns: @@ -1335,19 +1352,40 @@ def fit( (every epoch) and ``"val_loss"`` (every ``val_freq`` epochs). Raises: - ValueError: If ``magnitudes`` has the wrong shape or no valid samples + ValueError: If ``photometry`` has the wrong shape or no valid samples remain after filtering. """ - if magnitudes.ndim != 2 or magnitudes.shape[1] != self.n_filters: + if photometry.ndim != 2 or photometry.shape[1] != self.n_filters: raise ValueError( - f"magnitudes must be (N, {self.n_filters}) for filters {self.filter_names}" + f"photometry must be (N, {self.n_filters}) for filters {self.filter_names}" + ) + + # handle conversions and unit checks + if asinh_scaling: + assert asinh_b_factor is not None, ( + "asinh_b_factor must be provided when asinh_scaling is True" ) + assert isinstance(asinh_b_factor, (unyt_quantity, list)), ( + "asinh_b_factor must be a unyt quantity or a list of unyt quantities" + ) + assert not isinstance(asinh_b_factor, list) or len(asinh_b_factor) == self.n_filters + assert isinstance(photometry, unyt_array), ( + "photometry must be a unyt_array when asinh_scaling is True" + ) + photometry = f_jy_to_asinh(photometry, b=asinh_b_factor) + + elif isinstance(photometry, unyt_array): + photometry = ( + -2.5 * np.log10(photometry.to_value("Jy")) + 8.90 + ) # Convert to AB magnitudes + else: + print("Assuming photometry is already in magnitudes.") - valid_mask = np.all((flux_uncertainties > 0) & np.isfinite(magnitudes), axis=1) + valid_mask = np.all((flux_uncertainties > 0) & np.isfinite(photometry), axis=1) if not np.any(valid_mask): raise ValueError("No valid training samples after filtering.") - magnitudes = magnitudes[valid_mask] + magnitudes = photometry[valid_mask] ln_sigma = np.log(flux_uncertainties[valid_mask]) ln_sigma_median = np.median(ln_sigma, axis=0).astype(np.float32) @@ -1368,6 +1406,8 @@ def fit( self._mag_iqr = torch.tensor(mag_iqr, device=self.device) self._mag_max = torch.tensor(mag_max, device=self.device) self._mag_min = torch.tensor(mag_min, device=self.device) + self._asinh_b_factor = asinh_b_factor if asinh_scaling else None + self._is_asinh_scaled = asinh_scaling ln_sigma_norm = (ln_sigma - ln_sigma_median) / (ln_sigma_iqr + 1e-8) mag_norm = (magnitudes - mag_median) / (mag_iqr + 1e-8) @@ -1570,6 +1610,7 @@ def sample_uncertainty( mag_max_np = self._mag_max.cpu().numpy() mag_min_np = self._mag_min.cpu().numpy() magnitudes = np.clip(magnitudes, a_min=mag_min_np, a_max=mag_max_np) + print("clipped mag", magnitudes) mag_t = torch.tensor(magnitudes, dtype=torch.float32, device=self.device) mag_norm = self._normalise_mag(mag_t).repeat_interleave(n_samples, dim=0) @@ -1608,6 +1649,7 @@ def sample_uncertainty( if n_samples == 1: sigma_np = sigma_np[:, 0, :] + print("sigma", sigma_np) return sigma_np[0] if scalar_input else sigma_np def apply_noise( @@ -1650,10 +1692,14 @@ def apply_noise( raise ValueError("true_flux_units must be provided when flux is not a unyt_array.") safe_jy = np.where(flux_jy > 0, flux_jy, np.finfo(np.float32).tiny) magnitudes = (-2.5 * np.log10(safe_jy / 3631.0)).astype(np.float32) + sigma = self.sample_uncertainty(magnitudes, n_samples=1, n_steps=n_steps, method=method) - noise = np.random.normal(0.0, sigma) + # sigma is in uJy; convert to Jy for noise sampling + sigma_jy = sigma * 1e-6 + noise = np.random.normal(0.0, sigma_jy) + noisy_flux = unyt_array(flux_jy + noise, units="Jy") - sigma_jy = unyt_array(sigma, units="Jy") + sigma_jy = unyt_array(sigma_jy, units="Jy") if self.return_noise: return noisy_flux, sigma_jy @@ -1693,6 +1739,12 @@ def serialize_to_hdf5(self, hdf5_group: h5py.Group) -> None: hdf5_group.create_dataset("mag_max", data=self._mag_max.cpu().numpy()) hdf5_group.create_dataset("mag_min", data=self._mag_min.cpu().numpy()) + attrs["is_asinh_scaled"] = self._is_asinh_scaled + if self._is_asinh_scaled: + attrs["asinh_b_factor"] = ( + self._asinh_b_factor.to_value("Jy") if self._asinh_b_factor is not None else None + ) + weights_group = hdf5_group.create_group("score_network_weights") underlying = self.ema_net.module @@ -1748,6 +1800,14 @@ def _from_hdf5_group(cls, hdf5_group: h5py.Group) -> "ScoreBasedUncertaintyModel instance._mag_max = torch.tensor(hdf5_group["mag_max"][:], dtype=torch.float32, device=dev) instance._mag_min = torch.tensor(hdf5_group["mag_min"][:], dtype=torch.float32, device=dev) + instance._is_asinh_scaled = bool(attrs.get("is_asinh_scaled", False)) + if instance._is_asinh_scaled: + b_factor_value = attrs.get("asinh_b_factor") + if b_factor_value is not None: + instance._asinh_b_factor = unyt_quantity(b_factor_value, units="Jy") + else: + instance._asinh_b_factor = None + state_dict: Dict[str, torch.Tensor] = {} weights_group = hdf5_group["score_network_weights"] for safe_key in weights_group.keys(): From c8e5e7dfc69c7e012e2f07646021f3f91dbc8142 Mon Sep 17 00:00:00 2001 From: Thomas Harvey Date: Mon, 15 Jun 2026 18:34:07 +0100 Subject: [PATCH 16/22] minor changes --- src/synference/noise_models.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/synference/noise_models.py b/src/synference/noise_models.py index 2d21e20..088b7dc 100644 --- a/src/synference/noise_models.py +++ b/src/synference/noise_models.py @@ -1372,7 +1372,7 @@ def fit( assert isinstance(photometry, unyt_array), ( "photometry must be a unyt_array when asinh_scaling is True" ) - photometry = f_jy_to_asinh(photometry, b=asinh_b_factor) + photometry = f_jy_to_asinh(photometry, f_b=asinh_b_factor) elif isinstance(photometry, unyt_array): photometry = ( @@ -1658,6 +1658,7 @@ def apply_noise( n_steps: int = 50, true_flux_units: str = None, method: str = "ode", + max_sigma_jy: Optional[float] = None, **kwargs: Any, ) -> Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]: """Applies diffusion-model-sampled Gaussian noise to the input flux. @@ -1673,6 +1674,8 @@ def apply_noise( true_flux_units (str): Units of ``flux`` (e.g. ``"Jy"``, ``"uJy"``). Required when ``flux`` is not a unyt_array. Default ``None``. method (str): Reverse-time solver — ``"ode"`` or ``"sde"``. Default ``"ode"``. + max_sigma_jy (float, optional): Maximum allowed uncertainty in Jy. + If provided, uncertainties will be clipped to this value. **kwargs: Absorbed for interface compatibility; not used. Returns: @@ -1693,9 +1696,31 @@ def apply_noise( safe_jy = np.where(flux_jy > 0, flux_jy, np.finfo(np.float32).tiny) magnitudes = (-2.5 * np.log10(safe_jy / 3631.0)).astype(np.float32) + # Check input dimensions match number of filters + if magnitudes.ndim == 1: + if magnitudes.shape[0] != self.n_filters: + raise ValueError( + f"Input flux has {magnitudes.shape[0]} filters" + " but model was trained with {self.n_filters} filters: {self.filter_names}" + ) + elif magnitudes.ndim == 2: + if magnitudes.shape[1] != self.n_filters: + raise ValueError( + f"Input flux has {magnitudes.shape[1]} filters, " + "but model was trained with {self.n_filters} filters: {self.filter_names}" + ) + sigma = self.sample_uncertainty(magnitudes, n_samples=1, n_steps=n_steps, method=method) # sigma is in uJy; convert to Jy for noise sampling sigma_jy = sigma * 1e-6 + if max_sigma_jy is not None: + max_sigma_jy = ( + max_sigma_jy.to_value("Jy") + if isinstance(max_sigma_jy, unyt_quantity) + else max_sigma_jy + ) + sigma_jy = np.clip(sigma_jy, a_min=None, a_max=max_sigma_jy) + noise = np.random.normal(0.0, sigma_jy) noisy_flux = unyt_array(flux_jy + noise, units="Jy") @@ -1819,7 +1844,10 @@ def _from_hdf5_group(cls, hdf5_group: h5py.Group) -> "ScoreBasedUncertaintyModel target = target._orig_mod target.load_state_dict(state_dict) - instance.ema_net.module.load_state_dict(state_dict) + ema_target = instance.ema_net.module + if hasattr(ema_target, "_orig_mod"): + ema_target = ema_target._orig_mod + ema_target.load_state_dict(state_dict) instance.score_net.to(dev) instance.ema_net.to(dev) instance._is_trained = True From 527da1a10d946731c29bb20f42117cd94c4ccc69 Mon Sep 17 00:00:00 2001 From: Thomas Harvey Date: Mon, 15 Jun 2026 18:36:11 +0100 Subject: [PATCH 17/22] Changes to make diffusion models work properly. --- examples/sbi/scripts/train_model.py | 36 ++++++++++--- src/synference/__init__.py | 4 +- src/synference/noise_models.py | 51 +++++++++++------- src/synference/sbi_runner.py | 83 ++++++++++++++++++++++++----- 4 files changed, 134 insertions(+), 40 deletions(-) diff --git a/examples/sbi/scripts/train_model.py b/examples/sbi/scripts/train_model.py index f083503..3b5a3b0 100644 --- a/examples/sbi/scripts/train_model.py +++ b/examples/sbi/scripts/train_model.py @@ -12,6 +12,7 @@ from synference import ( AsinhEmpiricalUncertaintyModel, + ScoreBasedUncertaintyModel, SBI_Fitter, Simformer_Fitter, create_uncertainty_models_from_EPOCHS_cat, @@ -74,7 +75,7 @@ class Args: additional_model_args: tuple = () parameters_to_add: tuple = () parameters_to_remove: tuple = () - data_err_file: str = """/home/tharvey/Downloads/JADES-Deep-GS_MASTER_Sel-f277W+f356W+f444W_v9_loc_depth_masked_10pc_EAZY_matched_selection_ext_src_UV.fits""" # noqa + data_err_file: str = None # Path to FITS file with photometric errors for empirical noise model data_err_hdu: str = "OBJECTS" # The HDU name in the FITS file background: bool = False model_features: tuple = () @@ -87,8 +88,8 @@ class Args: noise_model_class: str = "general" # Type of noise model to use - general, depth or asinh. custom_config_yaml: str = None sql_db_path: str = None # Path to SQLite database for storing results - - + precomputed_noise_model_path: str = None # Path saved HDF5 file with noise + parser.add_arguments(Args, dest="args") @@ -120,8 +121,14 @@ def main_task(args: Args) -> None: phot_to_remove = "" phot_to_remove = phot_to_remove.split(",") hst_bands = ["F435W", "F606W", "F775W", "F814W", "F850LP"] - + if args.scatter_fluxes > 0: + if (args.data_err_file is None and args.precomputed_noise_model_path is None) or ( + args.data_err_file is not None and args.precomputed_noise_model_path is not None + ): + raise ValueError("When scatter_fluxes > 0, exactly one of data_err_file or precomputed_noise_model_path must be provided.") + + if args.scatter_fluxes > 0 and args.data_err_file: table = Table.read(args.data_err_file, format="fits", hdu=args.data_err_hdu) bands = [i.split("_")[-1] for i in table.colnames if i.startswith("loc_depth")] if len(phot_to_remove) > 0: @@ -148,6 +155,10 @@ def main_task(args: Args) -> None: min_flux_error=args.min_flux_error, model_class=args.noise_model_class, ) + elif args.scatter_fluxes > 0 and args.precomputed_noise_model_path: + from synference.noise_models import load_unc_model_from_hdf5 + empirical_noise_models = load_unc_model_from_hdf5(args.precomputed_noise_model_path)['noise_model'] + else: empirical_noise_models = {} @@ -279,6 +290,12 @@ def log10_floor(x, floor=-6): print("Photometry in grid:", empirical_model_fitter.raw_observation_names, file=sys.stdout) for filt in empirical_model_fitter.raw_observation_names: + if args.scatter_fluxes > 0 and args.include_errors_in_feature_array and isinstance(empirical_noise_models, ScoreBasedUncertaintyModel): + check_names = [i.lower() for i in empirical_noise_models.filter_names] + if filt.lower() not in check_names: + print(f"Filter {filt} not in empirical noise models. Adding to unused filters.", file=sys.stdout) + unused_filters.append(filt) + continue if ( args.scatter_fluxes > 0 and args.include_errors_in_feature_array @@ -291,14 +308,15 @@ def log10_floor(x, floor=-6): # flux_units is "AB" unless scatter_fluxes and include_errors_in_feature_array # and all noise models are AsinhUncertaintyModels flux_units = "AB" - if args.scatter_fluxes > 0 and args.include_errors_in_feature_array: + if args.scatter_fluxes > 0 and args.include_errors_in_feature_array and not isinstance(empirical_noise_models, ScoreBasedUncertaintyModel): if all( isinstance(model, AsinhEmpiricalUncertaintyModel) for model in empirical_noise_models.values() ): flux_units = "asinh" + elif args.scatter_fluxes > 0 and args.include_errors_in_feature_array and isinstance(empirical_noise_models, ScoreBasedUncertaintyModel) and empirical_noise_models._is_asinh_scaled: + flux_units = "asinh" - print(empirical_noise_models) empirical_model_fitter.create_feature_array_from_raw_photometry( extra_features=list(args.model_features), normalize_method=args.norm_method, @@ -441,3 +459,9 @@ def log10_floor(x, floor=-6): else: # Run in the foreground as normal main_task(args) + + +# python synference/examples/sbi/scripts/train_model.py --scatter_fluxes=1 --include_errors_in_feature_array=True --photometry_to_remove=HST/ACS_WFC.F606W,HST/ACS_WFC.F775W,HST/ACS_WFC.F814W,HST/ACS_WFC.F850LP,JWST/NIRCam.F070,HST/WFC3_IR.F105W,HST/WFC3_IR.F110W,HST/WFC3_IR.F110W,HST/WFC3_IR.F125W,HST/WFC3_IR.F140W,HST/WFC3_IR.F160W,JWST/NIRCam.F070W,JWST/NIRCam.F090W,JWST/NIRCam.F115W,JWST/NIRCam.F140M,JWST/NIRCam.F150W,JWST/NIRCam.F162M,JWST/NIRCam.F182M,JWST/NIRCam.F200W,JWST/NIRCam.F210M,JWST/NIRCam.F250M,JWST/NIRCam.F277W,JWST/NIRCam.F300M,JWST/NIRCam.F335M,JWST/NIRCam.F356W,JWST/NIRCam.F360M,JWST/NIRCam.F410M,JWST/NIRCam.F430M,JWST/NIRCam.F444W,JWST/NIRCam.F460M,JWST/NIRCam.F480M,HST/ACS_WFC/F475W,JWST/MIRI.F560W,JWST/MIRI.F770W,JWST/MIRI.F1000W,JWST/MIRI.F1130W,JWST/MIRI.F1280W,JWST/MIRI.F1500W,JWST/MIRI.F1800W,JWST/MIRI.F2100W,JWST/MIRI.F2550W,Spitzer/IRAC.I3,Spitzer/IRAC.I4,CTIO/DECam.u,CTIO/DECam.g,CTIO/DECam.r,CTIO/DECam.i,CTIO/DECam.z,CTIO/DECam.Y,LSST/LSST.u,LSST/LSST.g,LSST/LSST.r,LSST/LSST.i,LSST/LSST.z,LSST/LSST.Y,PAN-STARRS/PS1.g,PAN-STARRS/PS1.r,PAN-STARRS/PS1.i,PAN-STARRS/PS1.w,PAN-STARRS/PS1.z,PAN-STARRS/PS1.y,Paranal/VISTA.Z,CFHT/MegaCam.g,CFHT/MegaCam.r,CFHT/MegaCam.i,CFHT/MegaCam.z,HST/ACS_WFC.F435W,HST/ACS_WFC.F475W,Euclid/NISP.Y,Euclid/NISP.J/Euclid/NISP.H --grid_path=/cosma/apps/dp276/dc-harv3/synference/libraries/grid_BPASS_Chab_DenseBasis_SFH_0.01_z_14_logN_6.0_Calzetti_v4_multinode.hdf5 --model_name=BPASS_DenseBasis_COSMOS2020 --name_append=nsf --train_test_fraction=0.90 --parameter_transformations="Av=log10,sfr_10=log10_floor,mass_weighted_age=log10" --parameters_to_add="mass_weighted_age,sfr_10,log_surviving_mass,beta" --model_types=nsf --custom_config_yaml=/cosma/apps/dp276/dc-harv3/synference/examples/sbi/configs/best_params_2101.yaml --precomputed_noise_model_path=/cosma/apps/dp276/dc-harv3/synference/priv/COSMOS2020_noise_model_asinh.h5 + +{'CFHT/MegaCam.u', 'Paranal/VISTA.J', 'Subaru/HSC.g', 'Subaru/HSC.z', 'Spitzer/IRAC.I2', 'Spitzer/IRAC.I1', 'Subaru/HSC.i', 'Subaru/HSC.y', 'Paranal/VISTA.H', 'Paranal/VISTA.Y', 'Paranal/VISTA.Ks', 'Subaru/HSC.r'}, +{'CFHT/MegaCam.u', 'Paranal/VISTA.J', 'Subaru/HSC.Y', 'Subaru/HSC.g', 'Subaru/HSC.z', 'Spitzer/IRAC.I2', 'Spitzer/IRAC.I1', 'Subaru/HSC.i', 'Paranal/VISTA.H', 'Paranal/VISTA.Y', 'Paranal/VISTA.Ks', 'Subaru/HSC.r'} \ No newline at end of file diff --git a/src/synference/__init__.py b/src/synference/__init__.py index 6140915..d7398cd 100644 --- a/src/synference/__init__.py +++ b/src/synference/__init__.py @@ -31,7 +31,8 @@ from .noise_models import ( EmpiricalUncertaintyModel, create_uncertainty_models_from_EPOCHS_cat, DepthUncertaintyModel, UncertaintyModel, AsinhEmpiricalUncertaintyModel, - GeneralEmpiricalUncertaintyModel, save_unc_model_to_hdf5, load_unc_model_from_hdf5 + GeneralEmpiricalUncertaintyModel, save_unc_model_to_hdf5, load_unc_model_from_hdf5, + ScoreBasedUncertaintyModel ) from .custom_runner import SBICustomRunner @@ -103,6 +104,7 @@ "DepthUncertaintyModel", "UncertaintyModel", "GeneralEmpiricalUncertaintyModel", + "ScoreBasedUncertaintyModel", "save_unc_model_to_hdf5", "load_unc_model_from_hdf5", "optimize_sfh_xlimit", diff --git a/src/synference/noise_models.py b/src/synference/noise_models.py index 2d21e20..1b41564 100644 --- a/src/synference/noise_models.py +++ b/src/synference/noise_models.py @@ -1372,7 +1372,7 @@ def fit( assert isinstance(photometry, unyt_array), ( "photometry must be a unyt_array when asinh_scaling is True" ) - photometry = f_jy_to_asinh(photometry, b=asinh_b_factor) + photometry = f_jy_to_asinh(photometry, f_b=asinh_b_factor) elif isinstance(photometry, unyt_array): photometry = ( @@ -1552,20 +1552,20 @@ def fit( @torch.no_grad() def sample_uncertainty( self, - magnitudes: np.ndarray, + photometry: np.ndarray, n_samples: int = 1, n_steps: int = 50, t_eps: float = 1e-2, method: str = "ode", ) -> np.ndarray: - """Samples flux uncertainties conditioned on observed magnitudes. + """Samples flux uncertainties conditioned on observed photometry. Runs the reverse-time diffusion process (ODE or SDE) from t=T to t=t_eps, then exponentiates the resulting normalised log-uncertainty to recover physical flux uncertainties in Janskys. Args: - magnitudes (np.ndarray): Observed AB magnitudes. Shape ``(n_filters,)`` + photometry (np.ndarray): Observed AB magnitudes/photometry with units. Shape ``(n_filters,)`` for a single object or ``(N, n_filters)`` for a batch. n_samples (int): Number of independent uncertainty draws per object. Default 1. @@ -1584,35 +1584,39 @@ def sample_uncertainty( Raises: RuntimeError: If called before the model has been trained. - ValueError: If ``magnitudes`` has the wrong shape, contains non-finite + ValueError: If ``photometry`` has the wrong shape, contains non-finite values, or ``method`` is not recognised. """ if not self._is_trained: raise RuntimeError("Model must be trained before sampling. Call fit() first.") - scalar_input = magnitudes.ndim == 1 + scalar_input = photometry.ndim == 1 if scalar_input: - magnitudes = magnitudes[np.newaxis] + photometry = photometry[np.newaxis] # Check input is finite and has correct shape - if magnitudes.shape[1] != self.n_filters: + if photometry.shape[1] != self.n_filters: raise ValueError( - f"Input magnitudes must have shape (N, {self.n_filters}) " + f"Input photometry must have shape (N, {self.n_filters}) " f"for filters {self.filter_names}" ) - if not np.all(np.isfinite(magnitudes)): - raise ValueError("Input magnitudes must be finite.") + if not np.all(np.isfinite(photometry)): + raise ValueError("Input photometry must be finite.") - N = len(magnitudes) + N = len(photometry) + + if self._is_asinh_scaled: + assert self._asinh_b_factor is not None + photometry = f_jy_to_asinh(photometry, f_b=self._asinh_b_factor) # Clip to training range to avoid OOD extrapolation for very faint # (large mag) or very bright (small mag) simulated sources. mag_max_np = self._mag_max.cpu().numpy() mag_min_np = self._mag_min.cpu().numpy() - magnitudes = np.clip(magnitudes, a_min=mag_min_np, a_max=mag_max_np) - print("clipped mag", magnitudes) + photometry = np.clip(photometry, a_min=mag_min_np, a_max=mag_max_np) + print("clipped mag", photometry) - mag_t = torch.tensor(magnitudes, dtype=torch.float32, device=self.device) + mag_t = torch.tensor(photometry, dtype=torch.float32, device=self.device) mag_norm = self._normalise_mag(mag_t).repeat_interleave(n_samples, dim=0) x = torch.randn(N * n_samples, self.n_filters, device=self.device) @@ -1662,7 +1666,7 @@ def apply_noise( ) -> Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]: """Applies diffusion-model-sampled Gaussian noise to the input flux. - Converts flux to AB magnitudes, draws one uncertainty sample per object + Converts flux to AB magnitudes/model photometry, draws one uncertainty sample per object via the reverse diffusion process, then adds zero-mean Gaussian noise with that uncertainty to the original flux. @@ -1690,8 +1694,12 @@ def apply_noise( flux_jy = unyt_array(flux, units=true_flux_units).to("Jy").value else: raise ValueError("true_flux_units must be provided when flux is not a unyt_array.") - safe_jy = np.where(flux_jy > 0, flux_jy, np.finfo(np.float32).tiny) - magnitudes = (-2.5 * np.log10(safe_jy / 3631.0)).astype(np.float32) + + if not self._is_asinh_scaled: + safe_jy = np.where(flux_jy > 0, flux_jy, np.finfo(np.float32).tiny) + magnitudes = (-2.5 * np.log10(safe_jy / 3631.0)).astype(np.float32) + else: + magnitudes = f_jy_to_asinh(flux_jy, f_b=self._asinh_b_factor) sigma = self.sample_uncertainty(magnitudes, n_samples=1, n_steps=n_steps, method=method) # sigma is in uJy; convert to Jy for noise sampling @@ -1819,7 +1827,12 @@ def _from_hdf5_group(cls, hdf5_group: h5py.Group) -> "ScoreBasedUncertaintyModel target = target._orig_mod target.load_state_dict(state_dict) - instance.ema_net.module.load_state_dict(state_dict) + + ema_target = instance.ema_net.module + if hasattr(ema_target, "_orig_mod"): + ema_target = ema_target._orig_mod + ema_target.load_state_dict(state_dict) + ema_target.load_state_dict(state_dict) instance.score_net.to(dev) instance.ema_net.to(dev) instance._is_trained = True diff --git a/src/synference/sbi_runner.py b/src/synference/sbi_runner.py index abee432..5eab4e3 100644 --- a/src/synference/sbi_runner.py +++ b/src/synference/sbi_runner.py @@ -953,20 +953,35 @@ def _apply_score_based_noise_model( ``normed_flux_units="asinh"`` is requested. """ if normed_flux_units == "asinh": - raise ValueError( - "ScoreBasedUncertaintyModel does not support asinh normalisation. " - "Use normed_flux_units='AB' or a physical flux unit (e.g. 'nJy')." + assert score_model._is_asinh_scaled, ( + "asinh-scaled output requested but score model is not asinh-scaled." ) expected = score_model.filter_names actual = list(phot_names) + reset_order = False if actual != expected: - raise ValueError( - f"Filter name order mismatch between photometry and " - f"ScoreBasedUncertaintyModel.\n" - f" Model expects : {expected}\n" - f" Received : {actual}" - ) + # reorder photometry_array to match expected filter order + # we will revert to the original order before returning + + # check same filters in both, ignoring order (set seems broken) + if not set(actual).issubset(set(expected)): + raise ValueError( + f"Filter names in phot_names do not match score model filter names. " + f"Expected: {set(expected)}, actual: {set(actual)}" + ) + + reset_order = True + actual_lower = [f.lower() for f in actual] + expected_lower = [f.lower() for f in expected] + print(expected_lower) + print(actual_lower) + + new_indices = [actual_lower.index(filter) for filter in expected_lower] + + photometry_array = photometry_array[new_indices, :] + phot_names = [phot_names[i] for i in new_indices] + resetting_indices = np.argsort(new_indices) # Ensure we have a unyt_array in Jy if isinstance(photometry_array, unyt_array): @@ -979,22 +994,53 @@ def _apply_score_based_noise_model( ) n_filters, N = phot_jy.shape + + print('nfilters, N') + print(n_filters, N) # Repeat each galaxy N_scatters times: (n_filters, N*N_scatters) phot_jy_rep = np.repeat(phot_jy, N_scatters, axis=1) # AB magnitudes for the score model: shape (N*N_scatters, n_filters) - safe_jy = np.where(phot_jy_rep > 0, phot_jy_rep, np.finfo(np.float32).tiny) - phot_mag = (-2.5 * np.log10(safe_jy / 3631.0)).T.astype(np.float32) + # check if this is an asinh score model - if so, we should provide flux directly + if score_model._is_asinh_scaled: + # For asinh-scaled models, we provide flux directly + phot_mag = phot_jy_rep.T * Jy + else: + safe_jy = np.where(phot_jy_rep > 0, phot_jy_rep, np.finfo(np.float32).tiny) + phot_mag = (-2.5 * np.log10(safe_jy / 3631.0)).T.astype(np.float32) # Sample per-band uncertainties: (N*N_scatters, n_filters) in Jy - sigma_jy = score_model.sample_uncertainty(phot_mag, n_samples=1) + print(phot_mag.shape) + sigma_jy = score_model.sample_uncertainty(phot_mag, n_samples=1) * 1e-6 # convert from uJy to Jy + print('sigma_jy shape') + print(sigma_jy.shape) # Gaussian noise in Jy, then noisy flux: (N*N_scatters, n_filters) noise = np.random.normal(0.0, sigma_jy) noisy_jy = phot_jy_rep.T + noise # (N*N_scatters, n_filters) + print('noisy_jy shape') + print(noisy_jy.shape) - if normed_flux_units == "AB": + if normed_flux_units == "asinh": + print('asinh') + # Noisy asinh magnitudes + asinh_softening_parameter = score_model._asinh_b_factor + + errors = f_jy_err_to_asinh( + noisy_jy*Jy, + sigma_jy*Jy, + f_b=asinh_softening_parameter, + ).T + + scattered = f_jy_to_asinh( + noisy_jy*Jy, + f_b=asinh_softening_parameter, + ).T + + + elif normed_flux_units == "AB": + print('AB') # Noisy AB magnitudes safe_noisy = np.where(noisy_jy > 0, noisy_jy, np.finfo(np.float32).tiny) scattered = (-2.5 * np.log10(safe_noisy / 3631.0)).T # (n_filters, N*N_scatters) @@ -1006,6 +1052,13 @@ def _apply_score_based_noise_model( scattered = (noisy_jy * scale).T errors = (sigma_jy * scale).T + if reset_order: + # revert to original filter order + scattered = scattered[resetting_indices] + errors = errors[resetting_indices] + + print('shapes') + print(scattered.shape) if return_errors: return scattered, errors return scattered @@ -1705,6 +1758,8 @@ def create_feature_array_from_raw_photometry( for model in empirical_noise_models.values() ] ) + if isinstance(empirical_noise_models, ScoreBasedUncertaintyModel) and empirical_noise_models._is_asinh_scaled: + err_is_asinh = True assert asinh_softening_parameters is not None or err_is_asinh, ( "asinh_softening_parameters must be provided for asinh normalization." ) @@ -3557,7 +3612,7 @@ def split_dataset( if verbose: logger.info( - f"""Splitting dataset with {num_samples} samples into training""" + f"""Splitting dataset with {num_samples} samples into training """ f"""and testing sets with {train_fraction:.2f} train fraction.""" ) indices = np.arange(num_samples) From fa791dd818f039eec79defc9cdd92b9b4f94ce20 Mon Sep 17 00:00:00 2001 From: Thomas Harvey Date: Mon, 15 Jun 2026 19:06:29 +0100 Subject: [PATCH 18/22] Test fixes for new noise model behaviour. --- .../noise_modelling/diffusion_noise_models.ipynb | 12 +++++++++++- src/synference/library.py | 3 ++- src/synference/noise_models.py | 9 +++++---- tests/test_uncertainty_models.py | 12 ++++++------ 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/docs/source/noise_modelling/diffusion_noise_models.ipynb b/docs/source/noise_modelling/diffusion_noise_models.ipynb index 7061f80..514fbfa 100644 --- a/docs/source/noise_modelling/diffusion_noise_models.ipynb +++ b/docs/source/noise_modelling/diffusion_noise_models.ipynb @@ -142,7 +142,17 @@ ] } ], - "metadata": {}, + "metadata": { + "kernelspec": { + "display_name": "synference", + "language": "python", + "name": "python3" + }, + "language_info": { + "name": "python", + "version": "3.12.12" + } + }, "nbformat": 4, "nbformat_minor": 5 } diff --git a/src/synference/library.py b/src/synference/library.py index 517c866..ea23e46 100644 --- a/src/synference/library.py +++ b/src/synference/library.py @@ -30,6 +30,7 @@ from synthesizer.emissions import plot_spectra from synthesizer.grid import Grid from synthesizer.instruments import UVJ, FilterCollection, Instrument + from synthesizer.instruments.instrument_base import InstrumentBase from synthesizer.parametric import SFH, Galaxy, Stars, ZDist from synthesizer.particle.stars import sample_sfzh from synthesizer.pipeline import Pipeline @@ -5070,7 +5071,7 @@ def __init__( assert isinstance(grid, Grid), f"Grid must be a subclass of Grid. Got {type(grid)} instead." self.grid = grid - assert isinstance(instrument, Instrument), f"""Instrument must be a subclass of Instrument. + assert isinstance(instrument, InstrumentBase), f"""Instrument must be a subclass of InstrumentBase. Got {type(instrument)} instead.""" assert isinstance( emission_model, EmissionModel diff --git a/src/synference/noise_models.py b/src/synference/noise_models.py index 56d6317..bb94195 100644 --- a/src/synference/noise_models.py +++ b/src/synference/noise_models.py @@ -1611,10 +1611,11 @@ def sample_uncertainty( # Clip to training range to avoid OOD extrapolation for very faint # (large mag) or very bright (small mag) simulated sources. - mag_max_np = self._mag_max.cpu().numpy() - mag_min_np = self._mag_min.cpu().numpy() - photometry = np.clip(photometry, a_min=mag_min_np, a_max=mag_max_np) - print("clipped mag", photometry) + if self._mag_max is not None and self._mag_min is not None: + mag_max_np = self._mag_max.cpu().numpy() + mag_min_np = self._mag_min.cpu().numpy() + photometry = np.clip(photometry, a_min=mag_min_np, a_max=mag_max_np) + print("clipped mag", photometry) mag_t = torch.tensor(photometry, dtype=torch.float32, device=self.device) mag_norm = self._normalise_mag(mag_t).repeat_interleave(n_samples, dim=0) diff --git a/tests/test_uncertainty_models.py b/tests/test_uncertainty_models.py index fc15ce4..9e91209 100644 --- a/tests/test_uncertainty_models.py +++ b/tests/test_uncertainty_models.py @@ -586,7 +586,7 @@ def test_score_model_fit_wrong_shape(score_model_data): filter_names, mags, sigma_jy = score_model_data model = ScoreBasedUncertaintyModel(filter_names=filter_names, device="cpu") bad_mags = mags[:, :1] # only 1 band, model expects 2 - with pytest.raises(ValueError, match="magnitudes must be"): + with pytest.raises(ValueError, match="photometry must be*"): model.fit(bad_mags, sigma_jy[:, :1], **_SCORE_TRAIN_KWARGS) @@ -780,10 +780,10 @@ def test_sbi_runner_apply_score_wrong_filter_order(trained_score_model): phot = unyt_array(np.ones((n_filters, 5)), units="Jy") wrong_names = list(reversed(trained_score_model.filter_names)) - with pytest.raises(ValueError, match="mismatch"): - fitter._apply_score_based_noise_model( - phot, wrong_names, trained_score_model, normed_flux_units="AB" - ) + + fitter._apply_score_based_noise_model( + phot, wrong_names, trained_score_model, normed_flux_units="AB" + ) def test_sbi_runner_apply_score_asinh_raises(trained_score_model): @@ -800,7 +800,7 @@ def test_sbi_runner_apply_score_asinh_raises(trained_score_model): n_filters = trained_score_model.n_filters phot = unyt_array(np.ones((n_filters, 5)), units="Jy") - with pytest.raises(ValueError, match="asinh"): + with pytest.raises(AssertionError, match="asinh"): fitter._apply_score_based_noise_model( phot, trained_score_model.filter_names, From 4419b63e8daddde58e7ccc5b5802f84dcedd0750 Mon Sep 17 00:00:00 2001 From: Thomas Harvey Date: Fri, 19 Jun 2026 16:10:09 +0100 Subject: [PATCH 19/22] Add more support for ScoreNoise model in SBI_Fitter methods --- examples/sbi/scripts/train_model.py | 6 ++---- src/synference/sbi_runner.py | 14 +++++++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/examples/sbi/scripts/train_model.py b/examples/sbi/scripts/train_model.py index 3b5a3b0..8d4613b 100644 --- a/examples/sbi/scripts/train_model.py +++ b/examples/sbi/scripts/train_model.py @@ -318,7 +318,7 @@ def log10_floor(x, floor=-6): flux_units = "asinh" empirical_model_fitter.create_feature_array_from_raw_photometry( - extra_features=list(args.model_features), + extra_features=list(args.model_features[0].split(",")) if args.model_features else [], normalize_method=args.norm_method, include_errors_in_feature_array=args.include_errors_in_feature_array, scatter_fluxes=args.scatter_fluxes, @@ -461,7 +461,5 @@ def log10_floor(x, floor=-6): main_task(args) -# python synference/examples/sbi/scripts/train_model.py --scatter_fluxes=1 --include_errors_in_feature_array=True --photometry_to_remove=HST/ACS_WFC.F606W,HST/ACS_WFC.F775W,HST/ACS_WFC.F814W,HST/ACS_WFC.F850LP,JWST/NIRCam.F070,HST/WFC3_IR.F105W,HST/WFC3_IR.F110W,HST/WFC3_IR.F110W,HST/WFC3_IR.F125W,HST/WFC3_IR.F140W,HST/WFC3_IR.F160W,JWST/NIRCam.F070W,JWST/NIRCam.F090W,JWST/NIRCam.F115W,JWST/NIRCam.F140M,JWST/NIRCam.F150W,JWST/NIRCam.F162M,JWST/NIRCam.F182M,JWST/NIRCam.F200W,JWST/NIRCam.F210M,JWST/NIRCam.F250M,JWST/NIRCam.F277W,JWST/NIRCam.F300M,JWST/NIRCam.F335M,JWST/NIRCam.F356W,JWST/NIRCam.F360M,JWST/NIRCam.F410M,JWST/NIRCam.F430M,JWST/NIRCam.F444W,JWST/NIRCam.F460M,JWST/NIRCam.F480M,HST/ACS_WFC/F475W,JWST/MIRI.F560W,JWST/MIRI.F770W,JWST/MIRI.F1000W,JWST/MIRI.F1130W,JWST/MIRI.F1280W,JWST/MIRI.F1500W,JWST/MIRI.F1800W,JWST/MIRI.F2100W,JWST/MIRI.F2550W,Spitzer/IRAC.I3,Spitzer/IRAC.I4,CTIO/DECam.u,CTIO/DECam.g,CTIO/DECam.r,CTIO/DECam.i,CTIO/DECam.z,CTIO/DECam.Y,LSST/LSST.u,LSST/LSST.g,LSST/LSST.r,LSST/LSST.i,LSST/LSST.z,LSST/LSST.Y,PAN-STARRS/PS1.g,PAN-STARRS/PS1.r,PAN-STARRS/PS1.i,PAN-STARRS/PS1.w,PAN-STARRS/PS1.z,PAN-STARRS/PS1.y,Paranal/VISTA.Z,CFHT/MegaCam.g,CFHT/MegaCam.r,CFHT/MegaCam.i,CFHT/MegaCam.z,HST/ACS_WFC.F435W,HST/ACS_WFC.F475W,Euclid/NISP.Y,Euclid/NISP.J/Euclid/NISP.H --grid_path=/cosma/apps/dp276/dc-harv3/synference/libraries/grid_BPASS_Chab_DenseBasis_SFH_0.01_z_14_logN_6.0_Calzetti_v4_multinode.hdf5 --model_name=BPASS_DenseBasis_COSMOS2020 --name_append=nsf --train_test_fraction=0.90 --parameter_transformations="Av=log10,sfr_10=log10_floor,mass_weighted_age=log10" --parameters_to_add="mass_weighted_age,sfr_10,log_surviving_mass,beta" --model_types=nsf --custom_config_yaml=/cosma/apps/dp276/dc-harv3/synference/examples/sbi/configs/best_params_2101.yaml --precomputed_noise_model_path=/cosma/apps/dp276/dc-harv3/synference/priv/COSMOS2020_noise_model_asinh.h5 +# python synference/examples/sbi/scripts/train_model.py --scatter_fluxes=1 --include_errors_in_feature_array=True --photometry_to_remove=HST/ACS_WFC.F606W,HST/ACS_WFC.F775W,HST/ACS_WFC.F814W,HST/ACS_WFC.F850LP,JWST/NIRCam.F070,HST/WFC3_IR.F105W,HST/WFC3_IR.F110W,HST/WFC3_IR.F110W,HST/WFC3_IR.F125W,HST/WFC3_IR.F140W,HST/WFC3_IR.F160W,JWST/NIRCam.F070W,JWST/NIRCam.F090W,JWST/NIRCam.F115W,JWST/NIRCam.F140M,JWST/NIRCam.F150W,JWST/NIRCam.F162M,JWST/NIRCam.F182M,JWST/NIRCam.F200W,JWST/NIRCam.F210M,JWST/NIRCam.F250M,JWST/NIRCam.F277W,JWST/NIRCam.F300M,JWST/NIRCam.F335M,JWST/NIRCam.F356W,JWST/NIRCam.F360M,JWST/NIRCam.F410M,JWST/NIRCam.F430M,JWST/NIRCam.F444W,JWST/NIRCam.F460M,JWST/NIRCam.F480M,HST/ACS_WFC/F475W,JWST/MIRI.F560W,JWST/MIRI.F770W,JWST/MIRI.F1000W,JWST/MIRI.F1130W,JWST/MIRI.F1280W,JWST/MIRI.F1500W,JWST/MIRI.F1800W,JWST/MIRI.F2100W,JWST/MIRI.F2550W,Spitzer/IRAC.I3,Spitzer/IRAC.I4,CTIO/DECam.u,CTIO/DECam.g,CTIO/DECam.r,CTIO/DECam.i,CTIO/DECam.z,CTIO/DECam.Y,LSST/LSST.u,LSST/LSST.g,LSST/LSST.r,LSST/LSST.i,LSST/LSST.z,LSST/LSST.Y,PAN-STARRS/PS1.g,PAN-STARRS/PS1.r,PAN-STARRS/PS1.i,PAN-STARRS/PS1.w,PAN-STARRS/PS1.z,PAN-STARRS/PS1.y,Paranal/VISTA.Z,CFHT/MegaCam.g,CFHT/MegaCam.r,CFHT/MegaCam.i,CFHT/MegaCam.z,HST/ACS_WFC.F435W,HST/ACS_WFC.F475W,Euclid/NISP.Y,Euclid/NISP.J/Euclid/NISP.H --grid_path=/cosma/apps/dp276/dc-harv3/synference/libraries/grid_BPASS_Chab_DenseBasis_SFH_0.01_z_14_logN_5.0_Calzetti_v4_multinode.hdf5 --model_name=BPASS_DenseBasis_COSMOS2020 --name_append=nsf --train_test_fraction=0.90 --parameter_transformations="Av=log10,sfr_10=log10_floor,mass_weighted_age=log10" --parameters_to_add="mass_weighted_age,sfr_10,log_surviving_mass,beta" --model_types=nsf --custom_config_yaml=/cosma/apps/dp276/dc-harv3/synference/examples/sbi/configs/best_params_2101.yaml --precomputed_noise_model_path=/cosma/apps/dp276/dc-harv3/synference/priv/COSMOS2020_noise_model_asinh.h5 -{'CFHT/MegaCam.u', 'Paranal/VISTA.J', 'Subaru/HSC.g', 'Subaru/HSC.z', 'Spitzer/IRAC.I2', 'Spitzer/IRAC.I1', 'Subaru/HSC.i', 'Subaru/HSC.y', 'Paranal/VISTA.H', 'Paranal/VISTA.Y', 'Paranal/VISTA.Ks', 'Subaru/HSC.r'}, -{'CFHT/MegaCam.u', 'Paranal/VISTA.J', 'Subaru/HSC.Y', 'Subaru/HSC.g', 'Subaru/HSC.z', 'Spitzer/IRAC.I2', 'Spitzer/IRAC.I1', 'Subaru/HSC.i', 'Paranal/VISTA.H', 'Paranal/VISTA.Y', 'Paranal/VISTA.Ks', 'Subaru/HSC.r'} \ No newline at end of file diff --git a/src/synference/sbi_runner.py b/src/synference/sbi_runner.py index 5eab4e3..7eb653f 100644 --- a/src/synference/sbi_runner.py +++ b/src/synference/sbi_runner.py @@ -2447,8 +2447,17 @@ def bin_noisy_testing_data( nm = isinstance(nm, dict) and all( isinstance(m, AsinhEmpiricalUncertaintyModel) for m in nm.values() ) - else: - nm = False + if nm: + f_b = self.feature_array_flags["empirical_noise_models"][feature_name].b.to("Jy") + + if isinstance(self.feature_array_flags.get("empirical_noise_models"), ScoreBasedUncertaintyModel): + if self.feature_array_flags["empirical_noise_models"]._is_asinh_scaled: + nm = True + f_b = self.feature_array_flags["empirical_noise_models"]._asinh_b_factor.to("Jy") + else: + nm = False + else: + nm = False snrs = [] for feature_name in snr_feature_names or self.feature_names: @@ -2458,7 +2467,6 @@ def bin_noisy_testing_data( feature_index = self.feature_names.index(feature_name) err_index = self.feature_names.index(f"unc_{feature_name}") # Need to know SNR from asinh magnitudes. - f_b = self.feature_array_flags["empirical_noise_models"][feature_name].b.to("Jy") snr = asinh_to_snr( X_test[:, feature_index], X_test[:, err_index], From a2e3dd99bc0a9c7e43af5baca841e39e6c1c7913 Mon Sep 17 00:00:00 2001 From: Thomas Harvey Date: Tue, 23 Jun 2026 16:36:39 +0100 Subject: [PATCH 20/22] Remove debug prints. --- src/synference/library.py | 7 +------ src/synference/noise_models.py | 2 +- src/synference/sbi_runner.py | 28 ++++++++++++++++------------ 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/synference/library.py b/src/synference/library.py index a4885ce..c8cab55 100644 --- a/src/synference/library.py +++ b/src/synference/library.py @@ -5778,12 +5778,7 @@ def simulate(self, params): fluxes = galaxy.stars.spectra[self.emission_model_key].get_photo_fnu( self.instrument.filters ) - print("check") - print(galaxy.redshift) - print(type(fluxes)) - print(fluxes) - print(fluxes.__dict__) - print(fluxes.__class__.__dict__["photo_fnu"].unit) + outputs["photo_fnu"] = fluxes.photo_fnu outputs["photo_wav"] = fluxes.filters.pivot_lams diff --git a/src/synference/noise_models.py b/src/synference/noise_models.py index bb94195..6e8b87b 100644 --- a/src/synference/noise_models.py +++ b/src/synference/noise_models.py @@ -1654,7 +1654,7 @@ def sample_uncertainty( if n_samples == 1: sigma_np = sigma_np[:, 0, :] - print("sigma", sigma_np) + #print("sigma", sigma_np) return sigma_np[0] if scalar_input else sigma_np def apply_noise( diff --git a/src/synference/sbi_runner.py b/src/synference/sbi_runner.py index 2cb97bc..f6b1e7d 100644 --- a/src/synference/sbi_runner.py +++ b/src/synference/sbi_runner.py @@ -2,6 +2,7 @@ import copy import glob +import inspect import json import logging import os @@ -974,8 +975,8 @@ def _apply_score_based_noise_model( reset_order = True actual_lower = [f.lower() for f in actual] expected_lower = [f.lower() for f in expected] - print(expected_lower) - print(actual_lower) + #print(expected_lower) + #print(actual_lower) new_indices = [actual_lower.index(filter) for filter in expected_lower] @@ -994,9 +995,6 @@ def _apply_score_based_noise_model( ) n_filters, N = phot_jy.shape - - print('nfilters, N') - print(n_filters, N) # Repeat each galaxy N_scatters times: (n_filters, N*N_scatters) phot_jy_rep = np.repeat(phot_jy, N_scatters, axis=1) @@ -1011,19 +1009,14 @@ def _apply_score_based_noise_model( phot_mag = (-2.5 * np.log10(safe_jy / 3631.0)).T.astype(np.float32) # Sample per-band uncertainties: (N*N_scatters, n_filters) in Jy - print(phot_mag.shape) sigma_jy = score_model.sample_uncertainty(phot_mag, n_samples=1) * 1e-6 # convert from uJy to Jy - print('sigma_jy shape') - print(sigma_jy.shape) # Gaussian noise in Jy, then noisy flux: (N*N_scatters, n_filters) noise = np.random.normal(0.0, sigma_jy) noisy_jy = phot_jy_rep.T + noise # (N*N_scatters, n_filters) - print('noisy_jy shape') - print(noisy_jy.shape) + if normed_flux_units == "asinh": - print('asinh') # Noisy asinh magnitudes asinh_softening_parameter = score_model._asinh_b_factor @@ -6621,6 +6614,16 @@ def sample_posterior( # ) # shape = test_sample.shape + + sampling_kwargs = {} + # check if sampler.sample accepts arbitrary keyword arguments, + # if so pass them the timeout_seconds_per_test + varkw = inspect.signature(sampler.sample).parameters + if "kwargs" in varkw: + if timeout_seconds_per_test is not None and sample_method == "direct": + print(f"{timeout_seconds_per_test}s per sample.") + sampling_kwargs["max_sampling_time"] = timeout_seconds_per_test + if log_times: times = [] # Draw samples from the posterior @@ -6630,7 +6633,8 @@ def sample_posterior( start_time = time.time() try: # print(X_test_array[i]) - samples[i] = sampler.sample(nsteps=num_samples, x=X_test_array[i], progress=False) + samples[i] = sampler.sample(nsteps=num_samples, x=X_test_array[i], progress=False, + **sampling_kwargs) """samples[i] = sample_with_timeout( sampler, num_samples, From eb72f6fa6ff0b0339436ce5189c0c7473f5d8bee Mon Sep 17 00:00:00 2001 From: Thomas Harvey Date: Sat, 25 Jul 2026 16:38:39 +0100 Subject: [PATCH 21/22] Update branch to use Syntillate noise model as well as cached SVO filters --- .github/workflows/docs.yml | 12 +- .github/workflows/python-app.yml | 15 +- .../diffusion_noise_models.ipynb | 18 +- examples/sbi/scripts/train_model.py | 63 +- pyproject.toml | 4 + src/synference/library.py | 4 +- src/synference/noise_models.py | 789 +----------------- src/synference/sbi_runner.py | 133 ++- tests/test_uncertainty_models.py | 300 +++---- 9 files changed, 252 insertions(+), 1086 deletions(-) diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index ca808b5..546b66c 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -43,6 +43,9 @@ jobs: pip install wheel sudo apt install pandoc pip install "cosmos-synthesizer@git+https://github.com/synthesizer-project/synthesizer.git" + # syntillate (score-based diffusion noise model) is a hard dependency but + # is not yet on PyPI, so install it before resolving synference itself. + pip install "syntillate@git+https://github.com/synthesizer-project/syntillate.git" pip install .[test,docs] pip install "dense_basis@git+https://github.com/kartheikiyer/dense_basis.git" pip install "ltu_ili@git+https://github.com/maho3/ltu-ili.git" @@ -50,10 +53,15 @@ jobs: pip install pytest-xdist # enable parallel pytest execution - name: Download test data run: | - # Download test grid data + # Download test grid data mkdir -p data/libraries/ - synthesizer-download --test-grids --dust-grid + synthesizer-download --test-grids --dust-grid --instruments JWSTNIRCamWide python -c "from synference.utils import download_test_data; download_test_data()" + - name: Download SVO filter cache + run: | + # Pre-fetch cached SVO filter responses so the notebooks never hit the + # live SVO service, which is intermittently unavailable. + synthesizer-download --svo-filter-cache - name: Sphinx Build run: | # Test sphinx build (runs all notebooks) diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index 964338d..c11d9d8 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -31,16 +31,25 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip + # syntillate (score-based diffusion noise model) is a hard dependency but + # is not yet on PyPI, so install it before resolving synference itself. + pip install "syntillate@git+https://github.com/synthesizer-project/syntillate.git" pip install . pip install ruff pytest pip install "ltu_ili@git+https://github.com/maho3/ltu-ili.git" pip install "cosmos-synthesizer@git+https://github.com/synthesizer-project/synthesizer.git#egg=synthesizer" python -c "from synference.utils import download_test_data; download_test_data()" - + + - name: Download SVO filter cache + run: | + # Pre-fetch cached SVO filter responses so the tests never hit the live + # SVO service, which is intermittently unavailable. + synthesizer-download --svo-filter-cache + - name: Test import run: | - python -c "import synference" - + python -c "import synference" + - name: Test with pytest run: | pytest diff --git a/docs/source/noise_modelling/diffusion_noise_models.ipynb b/docs/source/noise_modelling/diffusion_noise_models.ipynb index 514fbfa..838f6da 100644 --- a/docs/source/noise_modelling/diffusion_noise_models.ipynb +++ b/docs/source/noise_modelling/diffusion_noise_models.ipynb @@ -7,11 +7,11 @@ "source": [ "# Diffusion Noise Models\n", "\n", - "This page describes the diffusion noise model implemented in `synference`. In contrast to the empirical noise models discussed in the previous sections, which simply model per-band uncertainties as a function of magnitude using binned interpolation, the diffusion noise model is a generative model that learns to the full latent distribution of noise in the data. This allows it to capture more complex noise properties, such as correlations between bands, and to generate more realistic noise realizations.\n", + "This page describes the diffusion noise model available in `synference`. In contrast to the empirical noise models discussed in the previous sections, which simply model per-band uncertainties as a function of magnitude using binned interpolation, the diffusion noise model is a generative model that learns to the full latent distribution of noise in the data. This allows it to capture more complex noise properties, such as correlations between bands, and to generate more realistic noise realizations.\n", "\n", "This noise model is based on the diffusion probabilistic model framework, which has been successfully applied to a wide range of generative modeling tasks in machine learning. The basic idea is to model the noise as a stochastic process that gradually transforms a simple initial distribution (e.g., Gaussian noise) into the complex noise distribution observed in the data. By learning this transformation, the model can generate realistic noise samples that can be added to synthetic photometry to better match the properties of real observations.\n", "\n", - "This is implemented in `synference` as the `ScoreBasedUncertaintyModel`, which learns the conditional uncertainty distribution p(σ|m), where σ is the uncertainty and m is the magnitude. The model is trained on a dataset of real photometric measurements, where the input is the magnitude and the target is the corresponding (log-scaled) uncertainty. Once trained, the model can be used to generate noise samples for synthetic photometry by sampling from the learned distribution.\n", + "This is implemented as the `ScoreBasedUncertaintyModel`, which learns the conditional uncertainty distribution p(\u03c3|m), where \u03c3 is the uncertainty and m is the magnitude. It lives in the standalone [syntillate](https://github.com/synthesizer-project/syntillate) package, which Synference installs as a dependency and re-exports from `synference.noise_models`, so it can be used and serialized alongside Synference's other uncertainty models. The model is trained on a dataset of real photometric measurements, where the input is the magnitude and the target is the corresponding (log-scaled) uncertainty. Once trained, the model can be used to generate noise samples for synthetic photometry by sampling from the learned distribution.\n", "\n", "We will demonstate this on the standard COSMOSO2020 'Farmer' catalog (Weaver et al. 2020). If you want to train the noise model yourself, you will have to download the full catalog (~3 GB) from [here](https://cosmos2020.calet.org/).\n", "\n" @@ -24,7 +24,7 @@ "metadata": {}, "outputs": [], "source": [ - "from synference.noise_models import ScoreBasedUncertaintyModel\n", + "from syntillate import ScoreBasedUncertaintyModel\n", "\n", "?ScoreBasedUncertaintyModel" ] @@ -82,10 +82,18 @@ "\n", "```python\n", "\n", - "from synference.noise_models import ScoreBasedUncertaintyModel\n", + "from syntillate import ScoreBasedUncertaintyModel\n", "noise_model = ScoreBasedUncertaintyModel(filter_names=bands)\n", "\n", - "noise_model.train(mag_array, flux_error_array, num_epochs=200, batch_size=1024)\n", + "# COSMOS2020 FLUXERR columns are in uJy; the model reports sigma back in\n", + "# the same units (see noise_model.sigma_units).\n", + "noise_model.fit(\n", + " mag_array,\n", + " flux_error_array,\n", + " flux_uncertainty_units='uJy',\n", + " n_epochs=200,\n", + " batch_size=1024,\n", + ")\n", "\n", "```\n", "\n", diff --git a/examples/sbi/scripts/train_model.py b/examples/sbi/scripts/train_model.py index 8d4613b..8724681 100644 --- a/examples/sbi/scripts/train_model.py +++ b/examples/sbi/scripts/train_model.py @@ -12,8 +12,8 @@ from synference import ( AsinhEmpiricalUncertaintyModel, - ScoreBasedUncertaintyModel, SBI_Fitter, + ScoreBasedUncertaintyModel, Simformer_Fitter, create_uncertainty_models_from_EPOCHS_cat, ) @@ -89,7 +89,8 @@ class Args: custom_config_yaml: str = None sql_db_path: str = None # Path to SQLite database for storing results precomputed_noise_model_path: str = None # Path saved HDF5 file with noise - + + parser.add_arguments(Args, dest="args") @@ -121,13 +122,16 @@ def main_task(args: Args) -> None: phot_to_remove = "" phot_to_remove = phot_to_remove.split(",") hst_bands = ["F435W", "F606W", "F775W", "F814W", "F850LP"] - + if args.scatter_fluxes > 0: if (args.data_err_file is None and args.precomputed_noise_model_path is None) or ( args.data_err_file is not None and args.precomputed_noise_model_path is not None ): - raise ValueError("When scatter_fluxes > 0, exactly one of data_err_file or precomputed_noise_model_path must be provided.") - + raise ValueError( + "When scatter_fluxes > 0, exactly one of data_err_file or " + "precomputed_noise_model_path must be provided." + ) + if args.scatter_fluxes > 0 and args.data_err_file: table = Table.read(args.data_err_file, format="fits", hdu=args.data_err_hdu) bands = [i.split("_")[-1] for i in table.colnames if i.startswith("loc_depth")] @@ -157,8 +161,10 @@ def main_task(args: Args) -> None: ) elif args.scatter_fluxes > 0 and args.precomputed_noise_model_path: from synference.noise_models import load_unc_model_from_hdf5 - empirical_noise_models = load_unc_model_from_hdf5(args.precomputed_noise_model_path)['noise_model'] - + + empirical_noise_models = load_unc_model_from_hdf5(args.precomputed_noise_model_path)[ + "noise_model" + ] else: empirical_noise_models = {} @@ -289,33 +295,40 @@ def log10_floor(x, floor=-6): unused_filters.append(band_name) print("Photometry in grid:", empirical_model_fitter.raw_observation_names, file=sys.stdout) + use_noise_models = args.scatter_fluxes > 0 and args.include_errors_in_feature_array + is_score_model = isinstance(empirical_noise_models, ScoreBasedUncertaintyModel) + for filt in empirical_model_fitter.raw_observation_names: - if args.scatter_fluxes > 0 and args.include_errors_in_feature_array and isinstance(empirical_noise_models, ScoreBasedUncertaintyModel): + if not use_noise_models: + continue + if is_score_model: + # The score model conditions on all of its bands at once, so any + # filter it was not trained on cannot be used. check_names = [i.lower() for i in empirical_noise_models.filter_names] if filt.lower() not in check_names: - print(f"Filter {filt} not in empirical noise models. Adding to unused filters.", file=sys.stdout) + print( + f"Filter {filt} not in the score-based noise model. Adding to unused filters.", + file=sys.stdout, + ) unused_filters.append(filt) - continue - if ( - args.scatter_fluxes > 0 - and args.include_errors_in_feature_array - and filt not in empirical_noise_models - ): + elif filt not in empirical_noise_models: unused_filters.append(filt) print(f"Unused filters: {unused_filters}", file=sys.stdout) # flux_units is "AB" unless scatter_fluxes and include_errors_in_feature_array - # and all noise models are AsinhUncertaintyModels + # and the noise model(s) work in asinh magnitudes flux_units = "AB" - if args.scatter_fluxes > 0 and args.include_errors_in_feature_array and not isinstance(empirical_noise_models, ScoreBasedUncertaintyModel): - if all( - isinstance(model, AsinhEmpiricalUncertaintyModel) - for model in empirical_noise_models.values() - ): + if use_noise_models: + if is_score_model: + asinh = empirical_noise_models.is_asinh_scaled + else: + asinh = all( + isinstance(model, AsinhEmpiricalUncertaintyModel) + for model in empirical_noise_models.values() + ) + if asinh: flux_units = "asinh" - elif args.scatter_fluxes > 0 and args.include_errors_in_feature_array and isinstance(empirical_noise_models, ScoreBasedUncertaintyModel) and empirical_noise_models._is_asinh_scaled: - flux_units = "asinh" empirical_model_fitter.create_feature_array_from_raw_photometry( extra_features=list(args.model_features[0].split(",")) if args.model_features else [], @@ -461,5 +474,5 @@ def log10_floor(x, floor=-6): main_task(args) -# python synference/examples/sbi/scripts/train_model.py --scatter_fluxes=1 --include_errors_in_feature_array=True --photometry_to_remove=HST/ACS_WFC.F606W,HST/ACS_WFC.F775W,HST/ACS_WFC.F814W,HST/ACS_WFC.F850LP,JWST/NIRCam.F070,HST/WFC3_IR.F105W,HST/WFC3_IR.F110W,HST/WFC3_IR.F110W,HST/WFC3_IR.F125W,HST/WFC3_IR.F140W,HST/WFC3_IR.F160W,JWST/NIRCam.F070W,JWST/NIRCam.F090W,JWST/NIRCam.F115W,JWST/NIRCam.F140M,JWST/NIRCam.F150W,JWST/NIRCam.F162M,JWST/NIRCam.F182M,JWST/NIRCam.F200W,JWST/NIRCam.F210M,JWST/NIRCam.F250M,JWST/NIRCam.F277W,JWST/NIRCam.F300M,JWST/NIRCam.F335M,JWST/NIRCam.F356W,JWST/NIRCam.F360M,JWST/NIRCam.F410M,JWST/NIRCam.F430M,JWST/NIRCam.F444W,JWST/NIRCam.F460M,JWST/NIRCam.F480M,HST/ACS_WFC/F475W,JWST/MIRI.F560W,JWST/MIRI.F770W,JWST/MIRI.F1000W,JWST/MIRI.F1130W,JWST/MIRI.F1280W,JWST/MIRI.F1500W,JWST/MIRI.F1800W,JWST/MIRI.F2100W,JWST/MIRI.F2550W,Spitzer/IRAC.I3,Spitzer/IRAC.I4,CTIO/DECam.u,CTIO/DECam.g,CTIO/DECam.r,CTIO/DECam.i,CTIO/DECam.z,CTIO/DECam.Y,LSST/LSST.u,LSST/LSST.g,LSST/LSST.r,LSST/LSST.i,LSST/LSST.z,LSST/LSST.Y,PAN-STARRS/PS1.g,PAN-STARRS/PS1.r,PAN-STARRS/PS1.i,PAN-STARRS/PS1.w,PAN-STARRS/PS1.z,PAN-STARRS/PS1.y,Paranal/VISTA.Z,CFHT/MegaCam.g,CFHT/MegaCam.r,CFHT/MegaCam.i,CFHT/MegaCam.z,HST/ACS_WFC.F435W,HST/ACS_WFC.F475W,Euclid/NISP.Y,Euclid/NISP.J/Euclid/NISP.H --grid_path=/cosma/apps/dp276/dc-harv3/synference/libraries/grid_BPASS_Chab_DenseBasis_SFH_0.01_z_14_logN_5.0_Calzetti_v4_multinode.hdf5 --model_name=BPASS_DenseBasis_COSMOS2020 --name_append=nsf --train_test_fraction=0.90 --parameter_transformations="Av=log10,sfr_10=log10_floor,mass_weighted_age=log10" --parameters_to_add="mass_weighted_age,sfr_10,log_surviving_mass,beta" --model_types=nsf --custom_config_yaml=/cosma/apps/dp276/dc-harv3/synference/examples/sbi/configs/best_params_2101.yaml --precomputed_noise_model_path=/cosma/apps/dp276/dc-harv3/synference/priv/COSMOS2020_noise_model_asinh.h5 - +# Example invocation with a pre-trained score-based noise model: +# python synference/examples/sbi/scripts/train_model.py --scatter_fluxes=1 --include_errors_in_feature_array=True --photometry_to_remove=HST/ACS_WFC.F606W,HST/ACS_WFC.F775W,HST/ACS_WFC.F814W,HST/ACS_WFC.F850LP,JWST/NIRCam.F070,HST/WFC3_IR.F105W,HST/WFC3_IR.F110W,HST/WFC3_IR.F110W,HST/WFC3_IR.F125W,HST/WFC3_IR.F140W,HST/WFC3_IR.F160W,JWST/NIRCam.F070W,JWST/NIRCam.F090W,JWST/NIRCam.F115W,JWST/NIRCam.F140M,JWST/NIRCam.F150W,JWST/NIRCam.F162M,JWST/NIRCam.F182M,JWST/NIRCam.F200W,JWST/NIRCam.F210M,JWST/NIRCam.F250M,JWST/NIRCam.F277W,JWST/NIRCam.F300M,JWST/NIRCam.F335M,JWST/NIRCam.F356W,JWST/NIRCam.F360M,JWST/NIRCam.F410M,JWST/NIRCam.F430M,JWST/NIRCam.F444W,JWST/NIRCam.F460M,JWST/NIRCam.F480M,HST/ACS_WFC/F475W,JWST/MIRI.F560W,JWST/MIRI.F770W,JWST/MIRI.F1000W,JWST/MIRI.F1130W,JWST/MIRI.F1280W,JWST/MIRI.F1500W,JWST/MIRI.F1800W,JWST/MIRI.F2100W,JWST/MIRI.F2550W,Spitzer/IRAC.I3,Spitzer/IRAC.I4,CTIO/DECam.u,CTIO/DECam.g,CTIO/DECam.r,CTIO/DECam.i,CTIO/DECam.z,CTIO/DECam.Y,LSST/LSST.u,LSST/LSST.g,LSST/LSST.r,LSST/LSST.i,LSST/LSST.z,LSST/LSST.Y,PAN-STARRS/PS1.g,PAN-STARRS/PS1.r,PAN-STARRS/PS1.i,PAN-STARRS/PS1.w,PAN-STARRS/PS1.z,PAN-STARRS/PS1.y,Paranal/VISTA.Z,CFHT/MegaCam.g,CFHT/MegaCam.r,CFHT/MegaCam.i,CFHT/MegaCam.z,HST/ACS_WFC.F435W,HST/ACS_WFC.F475W,Euclid/NISP.Y,Euclid/NISP.J/Euclid/NISP.H --grid_path=/cosma/apps/dp276/dc-harv3/synference/libraries/grid_BPASS_Chab_DenseBasis_SFH_0.01_z_14_logN_5.0_Calzetti_v4_multinode.hdf5 --model_name=BPASS_DenseBasis_COSMOS2020 --name_append=nsf --train_test_fraction=0.90 --parameter_transformations="Av=log10,sfr_10=log10_floor,mass_weighted_age=log10" --parameters_to_add="mass_weighted_age,sfr_10,log_surviving_mass,beta" --model_types=nsf --custom_config_yaml=/cosma/apps/dp276/dc-harv3/synference/examples/sbi/configs/best_params_2101.yaml --precomputed_noise_model_path=/cosma/apps/dp276/dc-harv3/synference/priv/COSMOS2020_noise_model_asinh.h5 # noqa: E501 diff --git a/pyproject.toml b/pyproject.toml index 55e3da7..8cf6046 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -67,6 +67,10 @@ dependencies = [ "plotext", "matplotlib", "numba", + # Score-based diffusion noise model (ScoreBasedUncertaintyModel). + # Until syntillate is on PyPI, install it first with: + # pip install git+https://github.com/synthesizer-project/syntillate.git + "syntillate", ] [project.optional-dependencies] diff --git a/src/synference/library.py b/src/synference/library.py index c8cab55..e686b0b 100644 --- a/src/synference/library.py +++ b/src/synference/library.py @@ -5077,7 +5077,9 @@ def __init__( assert isinstance(grid, Grid), f"Grid must be a subclass of Grid. Got {type(grid)} instead." self.grid = grid - assert isinstance(instrument, InstrumentBase), f"""Instrument must be a subclass of InstrumentBase. + assert isinstance( + instrument, InstrumentBase + ), f"""Instrument must be a subclass of InstrumentBase. Got {type(instrument)} instead.""" assert isinstance( emission_model, EmissionModel diff --git a/src/synference/noise_models.py b/src/synference/noise_models.py index 6e8b87b..d0fe603 100644 --- a/src/synference/noise_models.py +++ b/src/synference/noise_models.py @@ -2,27 +2,42 @@ This module provides a robust and serializable framework for creating and applying various photometric noise models. + +The score-based diffusion noise model lives in the standalone `syntillate +`_ package; +``ScoreBasedUncertaintyModel`` is re-exported here so it can be used and +serialized alongside Synference's own uncertainty models. """ -import json -import math import warnings from abc import ABC, abstractmethod -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Dict, Optional, Tuple, Union import h5py import matplotlib.pyplot as plt import numpy as np -import torch -import torch.nn as nn from astropy.table import Table from scipy import stats from scipy.interpolate import interp1d -from torch.optim.swa_utils import AveragedModel, get_ema_multi_avg_fn -from unyt import Jy, Unit, uJy, unyt_array, unyt_quantity +from syntillate import ScoreBasedUncertaintyModel +from unyt import Jy, Unit, unyt_array from .utils import f_jy_err_to_asinh, f_jy_to_asinh +__all__ = [ + "UncertaintyModel", + "DepthUncertaintyModel", + "SpectralUncertaintyModel", + "EmpiricalUncertaintyModel", + "AsinhEmpiricalUncertaintyModel", + "GeneralEmpiricalUncertaintyModel", + "ScoreBasedUncertaintyModel", + "MODEL_CLASS_REGISTRY", + "save_unc_model_to_hdf5", + "load_unc_model_from_hdf5", + "create_uncertainty_models_from_EPOCHS_cat", +] + # ============================================================================= # BASE CLASSES # ============================================================================= @@ -1104,766 +1119,6 @@ def apply_scalings( return flux_internal, final_error -# ============================================================================= -# SCORE-BASED DIFFUSION UNCERTAINTY MODEL -# ============================================================================= - - -class GaussianFourierProjection(nn.Module): - """Maps a scalar input to a sinusoidal random Fourier feature embedding. - - Used to encode the diffusion time step into a fixed-dimensional vector - suitable for conditioning a neural network. Random frequencies are drawn - once at construction and kept fixed (non-trainable). - """ - - def __init__(self, embed_dim: int, scale: float = 30.0): - """Initializes the projection with random Fourier frequencies. - - Args: - embed_dim (int): Output embedding dimension. Must be even; the output - is formed by concatenating ``embed_dim // 2`` sin and cos features. - scale (float): Standard deviation of the Gaussian from which random - frequencies are drawn. Higher values encode higher-frequency - variation. Default is 30.0. - """ - super().__init__() - self.W = nn.Parameter(torch.randn(embed_dim // 2) * scale, requires_grad=False) - - def forward(self, x: torch.Tensor) -> torch.Tensor: - """Projects x into the sinusoidal embedding space. - - Args: - x (torch.Tensor): Input tensor of shape ``(..., 1)`` (e.g. diffusion - time steps, one per batch element). - - Returns: - torch.Tensor: Embedding of shape ``(..., embed_dim)`` formed by - concatenating ``sin`` and ``cos`` projections along the last axis. - """ - x_proj = x * self.W[None, :] * 2 * math.pi - return torch.cat([torch.sin(x_proj), torch.cos(x_proj)], dim=-1) - - -class _ResidualBlock(nn.Module): - def __init__(self, dim: int): - super().__init__() - self.linear = nn.Linear(dim, dim) - self.act = nn.SiLU() - - def forward(self, x: torch.Tensor) -> torch.Tensor: - return x + self.linear(self.act(x)) - - -class _RobustScoreNetwork(nn.Module): - def __init__( - self, - n_filters: int, - hidden_dim: int = 256, - n_layers: int = 5, - time_embed_dim: int = 64, - ): - super().__init__() - self.time_embed = nn.Sequential( - GaussianFourierProjection(embed_dim=time_embed_dim), - nn.Linear(time_embed_dim, time_embed_dim), - nn.SiLU(), - ) - - input_dim = n_filters * 2 + time_embed_dim - self.proj_in = nn.Linear(input_dim, hidden_dim) - - self.res_blocks = nn.ModuleList([_ResidualBlock(hidden_dim) for _ in range(n_layers)]) - - self.proj_out = nn.Sequential(nn.SiLU(), nn.Linear(hidden_dim, n_filters)) - - def forward(self, x: torch.Tensor, t: torch.Tensor, m: torch.Tensor) -> torch.Tensor: - t_emb = self.time_embed(t.unsqueeze(-1)) - h = self.proj_in(torch.cat([x, m, t_emb], dim=-1)) - - for block in self.res_blocks: - h = block(h) - - return self.proj_out(h) - - -class ScoreBasedUncertaintyModel: - """Score-based diffusion model that learns the conditional uncertainty distribution p(σ|m). - - Trains a neural score network on pairs of observed magnitudes and flux - uncertainties using a variance-preserving (VP) SDE with a linear beta - schedule. After training, draws photometric uncertainty samples by running - the reverse-time ODE or SDE, then uses those samples to corrupt input flux - arrays in the same way as the other noise models. - - The model operates internally in log-uncertainty space and applies - IQR-based normalisation to both the uncertainty and magnitude features - before training, making it robust to outliers. - """ - - def __init__( - self, - filter_names: List[str], - hidden_dim: int = 256, - n_layers: int = 5, - time_embed_dim: int = 64, - beta_min: float = 0.1, - beta_max: float = 20.0, - T: float = 1.0, - device: Optional[str] = None, - return_noise: bool = False, - ): - """Initializes the score network, EMA wrapper, and SDE hyperparameters. - - Args: - filter_names (List[str]): Ordered list of photometric band names - (e.g. ``["u", "g", "r", "i", "z"]``). The number of filters is - inferred as ``len(filter_names)`` and sets the input/output - dimensionality of the score network. - hidden_dim (int): Width of each hidden layer in the score network. Default 256. - n_layers (int): Number of residual blocks in the score network. Default 5. - time_embed_dim (int): Dimensionality of the Gaussian Fourier time embedding. - Default 64. - beta_min (float): Minimum noise schedule coefficient β(0). Default 0.1. - beta_max (float): Maximum noise schedule coefficient β(T). Default 20.0. - T (float): Total diffusion time horizon. Default 1.0. - device (Optional[str]): Torch device string (e.g. ``"cuda"``, ``"cpu"``). - Auto-detected from CUDA availability when ``None``. - return_noise (bool): If ``True``, ``apply_noise`` returns a tuple - ``(noisy_flux, sigma)`` instead of only the noisy flux. Default ``False``. - """ - self.filter_names = list(filter_names) - n_filters = len(self.filter_names) - self.n_filters = n_filters - self.hidden_dim = hidden_dim - self.n_layers = n_layers - self.time_embed_dim = time_embed_dim - self.beta_min = beta_min - self.beta_max = beta_max - self.T = T - self.return_noise = return_noise - - self.device = device or ("cuda" if torch.cuda.is_available() else "cpu") - - self.score_net = _RobustScoreNetwork(n_filters, hidden_dim, n_layers, time_embed_dim).to( - self.device - ) - - if self.device == "cuda": - self.score_net = torch.compile(self.score_net, mode="reduce-overhead") - - self.ema_net = AveragedModel(self.score_net, multi_avg_fn=get_ema_multi_avg_fn(0.999)) - - self._ln_sigma_median: Optional[torch.Tensor] = None - self._ln_sigma_iqr: Optional[torch.Tensor] = None - self._mag_median: Optional[torch.Tensor] = None - self._mag_iqr: Optional[torch.Tensor] = None - self._mag_max: Optional[torch.Tensor] = None - self._mag_min: Optional[torch.Tensor] = None - self._is_trained: bool = False - self._is_asinh_scaled: bool = False - self._asinh_b_factor: Optional[torch.Tensor] = None - - def _beta(self, t: torch.Tensor) -> torch.Tensor: - return self.beta_min + (t / self.T) * (self.beta_max - self.beta_min) - - def _alpha(self, t: torch.Tensor) -> torch.Tensor: - return self.beta_min * t + (t**2 / (2.0 * self.T)) * (self.beta_max - self.beta_min) - - def _marginal_params(self, t: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: - A = self._alpha(t) - s = torch.exp(-0.5 * A) - sigma = torch.sqrt(torch.clamp(1.0 - torch.exp(-A), min=1e-8)) - return s, sigma - - def _normalise_ln_sigma(self, ln_sigma: torch.Tensor) -> torch.Tensor: - return (ln_sigma - self._ln_sigma_median) / (self._ln_sigma_iqr + 1e-8) - - def _denormalise_ln_sigma(self, z: torch.Tensor) -> torch.Tensor: - return z * self._ln_sigma_iqr + self._ln_sigma_median - - def _normalise_mag(self, mag: torch.Tensor) -> torch.Tensor: - return (mag - self._mag_median) / (self._mag_iqr + 1e-8) - - def fit( - self, - photometry: Union[np.ndarray, unyt_array], - flux_uncertainties: np.ndarray, - n_epochs: int = 1000, - batch_size: int = 1024, - learning_rate: float = 3e-4, - t_eps: float = 1e-2, - valid_fraction: float = 0.1, - patience: int = 20, - min_epochs: int = 200, - min_delta: float = 1e-3, - val_freq: int = 10, - n_val_t: int = 20, - asinh_scaling: bool = False, - asinh_b_factor: Union[unyt_quantity, List[unyt_quantity]] = 0.01 - * uJy, # 28.9 AB mag, typical for deep optical surveys - verbose: bool = True, - ) -> Dict[str, List[float]]: - """Trains the score network on observed magnitudes and flux uncertainties. - - Rows with non-positive or non-finite uncertainties are dropped before - training. Normalisation statistics (median and IQR) are computed from - the training set and stored on the instance for use during sampling. - An EMA of the score network weights is maintained throughout and used - for validation and sampling. - - Args: - photometry (Union[np.ndarray, unyt_array]): Observed photometry, - shape ``(N, n_filters)``. If it is a numpy array, it is assumed - to be in magnitudes. If it is a unyt_array, the units are - converted to magnitudes (or asinh magnitudes if asinh_scaling=True). - flux_uncertainties (np.ndarray): Observed flux uncertainties (σ) in - Janskys, shape ``(N, n_filters)``. Must be positive and finite. - n_epochs (int): Maximum number of training epochs. Default 1000. - batch_size (int): Number of samples per gradient step. Default 1024. - learning_rate (float): Initial Adam learning rate. Default 3e-4. - t_eps (float): Minimum diffusion time to avoid numerical instability - near t=0. Default 1e-2. - valid_fraction (float): Fraction of data held out for validation. - Default 0.1. - patience (int): Number of validation evaluations without sufficient - improvement before early stopping triggers. Default 20. - min_epochs (int): Minimum training epochs before early stopping is - active; allows EMA to warm up. Default 200. - min_delta (float): Minimum relative validation-loss improvement - required to reset the patience counter. Default 1e-3. - val_freq (int): Epoch interval between validation evaluations. - Default 10. - n_val_t (int): Number of evenly spaced diffusion times used for - deterministic validation loss evaluation. Default 20. - asinh_scaling (bool): If ``True``, uses asinh magnitudes instead of - logarithmic (AB) magnitudes. This can be helpful to include - sources with low/negative fluxes in the training set. Default ``False``. - asinh_b_factor (Union[unyt_quantity, List[unyt_quantity]]): - The b factor(s) for asinh scaling, in the same units as the input photometry. - If a single unyt quantity is provided, it is applied to all filters. - If a list is provided, it must have the same length as the number of filters. - Ignored if ``asinh_scaling`` is ``False``. - - verbose (bool): Print epoch-level progress. Default ``True``. - - Returns: - Dict[str, List[float]]: Training history with keys ``"train_loss"`` - (every epoch) and ``"val_loss"`` (every ``val_freq`` epochs). - - Raises: - ValueError: If ``photometry`` has the wrong shape or no valid samples - remain after filtering. - """ - if photometry.ndim != 2 or photometry.shape[1] != self.n_filters: - raise ValueError( - f"photometry must be (N, {self.n_filters}) for filters {self.filter_names}" - ) - - # handle conversions and unit checks - if asinh_scaling: - assert asinh_b_factor is not None, ( - "asinh_b_factor must be provided when asinh_scaling is True" - ) - assert isinstance(asinh_b_factor, (unyt_quantity, list)), ( - "asinh_b_factor must be a unyt quantity or a list of unyt quantities" - ) - assert not isinstance(asinh_b_factor, list) or len(asinh_b_factor) == self.n_filters - assert isinstance(photometry, unyt_array), ( - "photometry must be a unyt_array when asinh_scaling is True" - ) - photometry = f_jy_to_asinh(photometry, f_b=asinh_b_factor) - - elif isinstance(photometry, unyt_array): - photometry = ( - -2.5 * np.log10(photometry.to_value("Jy")) + 8.90 - ) # Convert to AB magnitudes - else: - print("Assuming photometry is already in magnitudes.") - - valid_mask = np.all((flux_uncertainties > 0) & np.isfinite(photometry), axis=1) - if not np.any(valid_mask): - raise ValueError("No valid training samples after filtering.") - - magnitudes = photometry[valid_mask] - ln_sigma = np.log(flux_uncertainties[valid_mask]) - - ln_sigma_median = np.median(ln_sigma, axis=0).astype(np.float32) - ln_sigma_iqr = ( - np.percentile(ln_sigma, 75, axis=0) - np.percentile(ln_sigma, 25, axis=0) - ).astype(np.float32) - mag_median = np.median(magnitudes, axis=0).astype(np.float32) - mag_iqr = ( - np.percentile(magnitudes, 75, axis=0) - np.percentile(magnitudes, 25, axis=0) - ).astype(np.float32) - - mag_max = np.percentile(magnitudes, 99.9, axis=0).astype(np.float32) - mag_min = np.percentile(magnitudes, 0.1, axis=0).astype(np.float32) - - self._ln_sigma_median = torch.tensor(ln_sigma_median, device=self.device) - self._ln_sigma_iqr = torch.tensor(ln_sigma_iqr, device=self.device) - self._mag_median = torch.tensor(mag_median, device=self.device) - self._mag_iqr = torch.tensor(mag_iqr, device=self.device) - self._mag_max = torch.tensor(mag_max, device=self.device) - self._mag_min = torch.tensor(mag_min, device=self.device) - self._asinh_b_factor = asinh_b_factor if asinh_scaling else None - self._is_asinh_scaled = asinh_scaling - - ln_sigma_norm = (ln_sigma - ln_sigma_median) / (ln_sigma_iqr + 1e-8) - mag_norm = (magnitudes - mag_median) / (mag_iqr + 1e-8) - - N = len(ln_sigma_norm) - n_val = max(1, int(N * valid_fraction)) - idx = np.random.permutation(N) - val_idx, train_idx = idx[:n_val], idx[n_val:] - - x_train = torch.tensor(ln_sigma_norm[train_idx], dtype=torch.float32, device=self.device) - m_train = torch.tensor(mag_norm[train_idx], dtype=torch.float32, device=self.device) - x_val = torch.tensor(ln_sigma_norm[val_idx], dtype=torch.float32, device=self.device) - m_val = torch.tensor(mag_norm[val_idx], dtype=torch.float32, device=self.device) - - # Pre-generate fixed validation noise. The validation loss becomes a deterministic - # function of model parameters: same (t, eps) pairs are reused every eval step. - # Using a stratified t grid (linspace) eliminates the variance from t sampling, - # which is the dominant source of validation noise in score-based diffusion training. - val_t_grid = torch.linspace(t_eps, self.T, n_val_t, device=self.device) - val_eps_fixed = torch.randn(n_val_t, len(x_val), self.n_filters, device=self.device) - - optimizer = torch.optim.Adam(self.score_net.parameters(), lr=learning_rate) - scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau( - optimizer, factor=0.5, patience=10, min_lr=1e-5 - ) - - history: Dict[str, List[float]] = {"train_loss": [], "val_loss": []} - best_val_loss = float("inf") - best_state: Dict[str, torch.Tensor] = {} - patience_counter = 0 - - for epoch in range(n_epochs): - self.score_net.train() - perm = torch.randperm(len(x_train), device=self.device) - x_ep, m_ep = x_train[perm], m_train[perm] - - epoch_loss = 0.0 - n_batches = 0 - for i in range(0, len(x_ep), batch_size): - x0 = x_ep[i : i + batch_size] - m = m_ep[i : i + batch_size] - B = len(x0) - - t = torch.rand(B, device=self.device) * (self.T - t_eps) + t_eps - s, sigma = self._marginal_params(t) - eps = torch.randn_like(x0) - x_t = s.unsqueeze(-1) * x0 + sigma.unsqueeze(-1) * eps - - score = self.score_net(x_t, t, m) - target = -eps / sigma.unsqueeze(-1) - loss = (sigma.unsqueeze(-1) ** 2 * (score - target) ** 2).mean() - - optimizer.zero_grad() - loss.backward() - torch.nn.utils.clip_grad_norm_(self.score_net.parameters(), max_norm=1.0) - optimizer.step() - self.ema_net.update_parameters(self.score_net) - - epoch_loss += loss.item() - n_batches += 1 - - train_loss = epoch_loss / max(n_batches, 1) - history["train_loss"].append(train_loss) - - if epoch % val_freq == 0: - self.ema_net.eval() - with torch.no_grad(): - val_loss = 0.0 - n_val_terms = 0 - # Iterate over the fixed t grid; for each t, evaluate the whole - # validation set in mini-batches using the corresponding fixed eps. - for t_idx in range(n_val_t): - t_val = val_t_grid[t_idx] - eps_full = val_eps_fixed[t_idx] - s_v_scalar, sigma_v_scalar = self._marginal_params(t_val.unsqueeze(0)) - s_v_scalar = s_v_scalar.item() - sigma_v_scalar = sigma_v_scalar.item() - - for i in range(0, len(x_val), batch_size): - x_v_batch = x_val[i : i + batch_size] - m_v_batch = m_val[i : i + batch_size] - eps_v = eps_full[i : i + batch_size] - B_val = len(x_v_batch) - - t_v = t_val.expand(B_val) - x_t_v = s_v_scalar * x_v_batch + sigma_v_scalar * eps_v - - score_v = self.ema_net(x_t_v, t_v, m_v_batch) - target_v = -eps_v / sigma_v_scalar - batch_loss = ( - (sigma_v_scalar**2 * (score_v - target_v) ** 2).mean().item() - ) - - val_loss += batch_loss - n_val_terms += 1 - - val_loss /= n_val_terms - - history["val_loss"].append(val_loss) - scheduler.step(val_loss) - - if verbose: - print( - f"Epoch {epoch:5d}/{n_epochs} train={train_loss:.4f} val={val_loss:.4f}" - ) - - # Early stopping: only active after min_epochs to allow EMA to warm up - # and avoid premature stopping during the initial fluctuating phase. - if epoch < min_epochs: - # Still track best state during warmup so we have something sensible - # if training is interrupted, but don't count toward patience. - if val_loss < best_val_loss: - best_val_loss = val_loss - best_state = { - k: v.clone() for k, v in self.ema_net.module.state_dict().items() - } - continue - - # Relative improvement threshold: require val_loss to drop by at least - # min_delta (fractionally) to count as genuine progress. Avoids resetting - # patience on noise-level fluctuations. - if val_loss < best_val_loss * (1.0 - min_delta): - best_val_loss = val_loss - best_state = {k: v.clone() for k, v in self.ema_net.module.state_dict().items()} - patience_counter = 0 - else: - patience_counter += 1 - if patience_counter >= patience: - if verbose: - print( - f"Early stopping at epoch {epoch} " - f"({patience} evals without >{min_delta:.1%} improvement)." - ) - break - - if best_state: - self.ema_net.module.load_state_dict(best_state) - self.score_net.load_state_dict(best_state) - self._is_trained = True - return history - - @torch.no_grad() - def sample_uncertainty( - self, - photometry: np.ndarray, - n_samples: int = 1, - n_steps: int = 50, - t_eps: float = 1e-2, - method: str = "ode", - ) -> np.ndarray: - """Samples flux uncertainties conditioned on observed photometry. - - Runs the reverse-time diffusion process (ODE or SDE) from t=T to t=t_eps, - then exponentiates the resulting normalised log-uncertainty to recover - physical flux uncertainties in Janskys. - - Args: - photometry (np.ndarray): Observed AB magnitudes/photometry with units. Shape ``(n_filters,)`` - for a single object or ``(N, n_filters)`` for a batch. - n_samples (int): Number of independent uncertainty draws per object. - Default 1. - n_steps (int): Number of discrete integration steps. Default 50. - t_eps (float): Minimum diffusion time; integration runs from T down - to this value. Default 1e-2. - method (str): Reverse-time solver — ``"ode"`` (probability-flow ODE, - deterministic) or ``"sde"`` (Langevin SDE, stochastic). - Default ``"ode"``. - - Returns: - np.ndarray: Sampled flux uncertainties (σ) in Janskys. Shape is - ``(N, n_filters)`` when ``n_samples=1``, ``(N, n_samples, n_filters)`` - otherwise. A leading batch dimension is stripped when the input - was a single-object array. - - Raises: - RuntimeError: If called before the model has been trained. - ValueError: If ``photometry`` has the wrong shape, contains non-finite - values, or ``method`` is not recognised. - """ - if not self._is_trained: - raise RuntimeError("Model must be trained before sampling. Call fit() first.") - - scalar_input = photometry.ndim == 1 - if scalar_input: - photometry = photometry[np.newaxis] - - # Check input is finite and has correct shape - if photometry.shape[1] != self.n_filters: - raise ValueError( - f"Input photometry must have shape (N, {self.n_filters}) " - f"for filters {self.filter_names}" - ) - if not np.all(np.isfinite(photometry)): - raise ValueError("Input photometry must be finite.") - - N = len(photometry) - - if self._is_asinh_scaled: - assert self._asinh_b_factor is not None - photometry = f_jy_to_asinh(photometry, f_b=self._asinh_b_factor) - - # Clip to training range to avoid OOD extrapolation for very faint - # (large mag) or very bright (small mag) simulated sources. - if self._mag_max is not None and self._mag_min is not None: - mag_max_np = self._mag_max.cpu().numpy() - mag_min_np = self._mag_min.cpu().numpy() - photometry = np.clip(photometry, a_min=mag_min_np, a_max=mag_max_np) - print("clipped mag", photometry) - - mag_t = torch.tensor(photometry, dtype=torch.float32, device=self.device) - mag_norm = self._normalise_mag(mag_t).repeat_interleave(n_samples, dim=0) - - x = torch.randn(N * n_samples, self.n_filters, device=self.device) - self.ema_net.eval() - - dt = (self.T - t_eps) / n_steps - times = torch.linspace(self.T, t_eps, n_steps + 1, device=self.device) - - with torch.amp.autocast("cuda", enabled=(self.device == "cuda"), dtype=torch.bfloat16): - for i in range(n_steps): - t_val = times[i].item() - t_tensor = torch.full( - (N * n_samples,), t_val, device=self.device, dtype=torch.float32 - ) - beta_t = self._beta(t_tensor).unsqueeze(-1) - score = self.ema_net(x, t_tensor, mag_norm) - - if method == "ode": - dx_dt = -0.5 * beta_t * (x + score) - x = x - dx_dt * dt - elif method == "sde": - drift = (beta_t / 2) * x + beta_t * score - z = torch.randn_like(x) - x = x + drift * dt + torch.sqrt(beta_t * dt) * z - else: - raise ValueError(f"Unknown sampling method: {method}") - - ln_sigma = self._denormalise_ln_sigma(x) - # Clamp to prevent float32 overflow: exp(88) ≈ 1.6e38 ≈ float32 max. - # Physical sigma never exceeds ~1e5 Jy; exp(12) ≈ 1.6e5 Jy is a safe ceiling. - ln_sigma = torch.clamp(ln_sigma, min=-80.0, max=12.0) - sigma_np = torch.exp(ln_sigma).cpu().numpy().reshape(N, n_samples, self.n_filters) - - if n_samples == 1: - sigma_np = sigma_np[:, 0, :] - - #print("sigma", sigma_np) - return sigma_np[0] if scalar_input else sigma_np - - def apply_noise( - self, - flux: np.ndarray, - n_steps: int = 50, - true_flux_units: str = None, - method: str = "ode", - max_sigma_jy: Optional[float] = None, - **kwargs: Any, - ) -> Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]: - """Applies diffusion-model-sampled Gaussian noise to the input flux. - - Converts flux to AB magnitudes/model photometry, draws one uncertainty sample per object - via the reverse diffusion process, then adds zero-mean Gaussian noise - with that uncertainty to the original flux. - - Args: - flux (np.ndarray or unyt_array): Input flux values. If a plain - numpy array, ``true_flux_units`` must be provided. - n_steps (int): Number of reverse-diffusion integration steps. Default 50. - true_flux_units (str): Units of ``flux`` (e.g. ``"Jy"``, ``"uJy"``). - Required when ``flux`` is not a unyt_array. Default ``None``. - method (str): Reverse-time solver — ``"ode"`` or ``"sde"``. Default ``"ode"``. - max_sigma_jy (float, optional): Maximum allowed uncertainty in Jy. - If provided, uncertainties will be clipped to this value. - **kwargs: Absorbed for interface compatibility; not used. - - Returns: - Union[unyt_array, Tuple[unyt_array, unyt_array]]: Noisy flux in - Janskys, or ``(noisy_flux_jy, sigma_jy)`` if the model was - constructed with ``return_noise=True``. - - Raises: - ValueError: If ``flux`` is a plain array and ``true_flux_units`` - is not provided. - """ - if isinstance(flux, unyt_array): - flux_jy = flux.to("Jy").value - elif true_flux_units is not None: - flux_jy = unyt_array(flux, units=true_flux_units).to("Jy").value - else: - raise ValueError("true_flux_units must be provided when flux is not a unyt_array.") - - if not self._is_asinh_scaled: - safe_jy = np.where(flux_jy > 0, flux_jy, np.finfo(np.float32).tiny) - magnitudes = (-2.5 * np.log10(safe_jy / 3631.0)).astype(np.float32) - else: - magnitudes = f_jy_to_asinh(flux_jy, f_b=self._asinh_b_factor) - - - # Check input dimensions match number of filters - if magnitudes.ndim == 1: - if magnitudes.shape[0] != self.n_filters: - raise ValueError( - f"Input flux has {magnitudes.shape[0]} filters" - " but model was trained with {self.n_filters} filters: {self.filter_names}" - ) - elif magnitudes.ndim == 2: - if magnitudes.shape[1] != self.n_filters: - raise ValueError( - f"Input flux has {magnitudes.shape[1]} filters, " - "but model was trained with {self.n_filters} filters: {self.filter_names}" - ) - - sigma = self.sample_uncertainty(magnitudes, n_samples=1, n_steps=n_steps, method=method) - # sigma is in uJy; convert to Jy for noise sampling - sigma_jy = sigma * 1e-6 - if max_sigma_jy is not None: - max_sigma_jy = ( - max_sigma_jy.to_value("Jy") - if isinstance(max_sigma_jy, unyt_quantity) - else max_sigma_jy - ) - sigma_jy = np.clip(sigma_jy, a_min=None, a_max=max_sigma_jy) - - noise = np.random.normal(0.0, sigma_jy) - - noisy_flux = unyt_array(flux_jy + noise, units="Jy") - sigma_jy = unyt_array(sigma_jy, units="Jy") - - if self.return_noise: - return noisy_flux, sigma_jy - return noisy_flux - - def serialize_to_hdf5(self, hdf5_group: h5py.Group) -> None: - """Serializes the model's configuration and trained weights to an HDF5 group. - - Hyperparameters (including the ordered filter name list) are stored as - HDF5 attributes. When the model has been trained, normalisation - statistics and the EMA score-network weights are also written. - Compiled-model wrappers (``torch.compile``) are unwrapped before weight - extraction. - - Args: - hdf5_group (h5py.Group): Open, writable HDF5 group to write into. - """ - attrs = hdf5_group.attrs - attrs["__class__"] = self.__class__.__name__ - attrs["filter_names"] = json.dumps(self.filter_names) - attrs["hidden_dim"] = self.hidden_dim - attrs["n_layers"] = self.n_layers - attrs["time_embed_dim"] = self.time_embed_dim - attrs["beta_min"] = self.beta_min - attrs["beta_max"] = self.beta_max - attrs["T"] = self.T - attrs["return_noise"] = self.return_noise - attrs["is_trained"] = self._is_trained - - if not self._is_trained: - return - - hdf5_group.create_dataset("ln_sigma_median", data=self._ln_sigma_median.cpu().numpy()) - hdf5_group.create_dataset("ln_sigma_iqr", data=self._ln_sigma_iqr.cpu().numpy()) - hdf5_group.create_dataset("mag_median", data=self._mag_median.cpu().numpy()) - hdf5_group.create_dataset("mag_iqr", data=self._mag_iqr.cpu().numpy()) - hdf5_group.create_dataset("mag_max", data=self._mag_max.cpu().numpy()) - hdf5_group.create_dataset("mag_min", data=self._mag_min.cpu().numpy()) - - attrs["is_asinh_scaled"] = self._is_asinh_scaled - if self._is_asinh_scaled: - attrs["asinh_b_factor"] = ( - self._asinh_b_factor.to_value("Jy") if self._asinh_b_factor is not None else None - ) - - weights_group = hdf5_group.create_group("score_network_weights") - - underlying = self.ema_net.module - if hasattr(underlying, "_orig_mod"): - underlying = underlying._orig_mod - - for key, tensor in underlying.state_dict().items(): - safe_key = key.replace(".", "__") - weights_group.create_dataset(safe_key, data=tensor.cpu().numpy()) - - @classmethod - def _from_hdf5_group(cls, hdf5_group: h5py.Group) -> "ScoreBasedUncertaintyModel": - """Loads a ScoreBasedUncertaintyModel from an HDF5 group. - - Reconstructs the model from the hyperparameters stored as HDF5 attributes, - then restores the normalisation statistics and score-network weights if - the model was trained before serialisation. - - Args: - hdf5_group (h5py.Group): Open HDF5 group previously written by - ``serialize_to_hdf5``. - - Returns: - ScoreBasedUncertaintyModel: Fully restored model instance, ready - for sampling if the serialised model was trained. - """ - attrs = hdf5_group.attrs - instance = cls( - filter_names=json.loads(attrs["filter_names"]), - hidden_dim=int(attrs["hidden_dim"]), - n_layers=int(attrs["n_layers"]), - time_embed_dim=int(attrs["time_embed_dim"]), - beta_min=float(attrs["beta_min"]), - beta_max=float(attrs["beta_max"]), - T=float(attrs["T"]), - return_noise=bool(attrs["return_noise"]), - ) - - if not attrs.get("is_trained", False): - return instance - - dev = instance.device - instance._ln_sigma_median = torch.tensor( - hdf5_group["ln_sigma_median"][:], dtype=torch.float32, device=dev - ) - instance._ln_sigma_iqr = torch.tensor( - hdf5_group["ln_sigma_iqr"][:], dtype=torch.float32, device=dev - ) - instance._mag_median = torch.tensor( - hdf5_group["mag_median"][:], dtype=torch.float32, device=dev - ) - instance._mag_iqr = torch.tensor(hdf5_group["mag_iqr"][:], dtype=torch.float32, device=dev) - instance._mag_max = torch.tensor(hdf5_group["mag_max"][:], dtype=torch.float32, device=dev) - instance._mag_min = torch.tensor(hdf5_group["mag_min"][:], dtype=torch.float32, device=dev) - - instance._is_asinh_scaled = bool(attrs.get("is_asinh_scaled", False)) - if instance._is_asinh_scaled: - b_factor_value = attrs.get("asinh_b_factor") - if b_factor_value is not None: - instance._asinh_b_factor = unyt_quantity(b_factor_value, units="Jy") - else: - instance._asinh_b_factor = None - - state_dict: Dict[str, torch.Tensor] = {} - weights_group = hdf5_group["score_network_weights"] - for safe_key in weights_group.keys(): - original_key = safe_key.replace("__", ".") - state_dict[original_key] = torch.tensor(weights_group[safe_key][:], dtype=torch.float32) - - target = instance.score_net - if hasattr(target, "_orig_mod"): - target = target._orig_mod - - target.load_state_dict(state_dict) - ema_target = instance.ema_net.module - if hasattr(ema_target, "_orig_mod"): - ema_target = ema_target._orig_mod - ema_target.load_state_dict(state_dict) - instance.score_net.to(dev) - instance.ema_net.to(dev) - instance._is_trained = True - return instance - - # ============================================================================= # SERIALIZATION FACTORY FUNCTIONS # ============================================================================= diff --git a/src/synference/sbi_runner.py b/src/synference/sbi_runner.py index f6b1e7d..4646230 100644 --- a/src/synference/sbi_runner.py +++ b/src/synference/sbi_runner.py @@ -931,18 +931,20 @@ def _apply_score_based_noise_model( Parameters: photometry_array: Flux array of shape ``(n_filters, N)``, as a ``unyt_array`` in any flux unit. - phot_names: Ordered list of filter names, length ``n_filters``. - Must match ``score_model.filter_names`` exactly. + phot_names: List of filter names, length ``n_filters``. Must contain + the same filters as ``score_model.filter_names``; any ordering + is accepted and the output is returned in this order. score_model: Trained :class:`ScoreBasedUncertaintyModel`. N_scatters: Number of independent noise realisations per galaxy. flux_units: Input flux units (used only if ``photometry_array`` is not a unyt_array; ignored otherwise). return_errors: If ``True`` returns ``(scattered, errors)``. normed_flux_units: Target units for the returned scattered - photometry. ``"AB"`` returns AB magnitudes and AB-mag errors. - Any other string is treated as a physical flux unit (e.g. - ``"nJy"``) and the values are converted accordingly. - ``"asinh"`` is not supported. + photometry. ``"AB"`` returns AB magnitudes and AB-mag errors, + ``"asinh"`` returns asinh magnitudes using the score model's own + softening parameter (and requires an asinh-scaled model). Any + other string is treated as a physical flux unit (e.g. ``"nJy"``) + and the values are converted accordingly. Returns: ``np.ndarray`` of shape ``(n_filters, N*N_scatters)`` when @@ -951,37 +953,29 @@ def _apply_score_based_noise_model( Raises: ValueError: If the filter names do not match the model, or if - ``normed_flux_units="asinh"`` is requested. + ``photometry_array`` carries no units and ``flux_units`` is not + given. """ if normed_flux_units == "asinh": - assert score_model._is_asinh_scaled, ( + assert score_model.is_asinh_scaled, ( "asinh-scaled output requested but score model is not asinh-scaled." ) - expected = score_model.filter_names - actual = list(phot_names) + expected = [f.lower() for f in score_model.filter_names] + actual = [f.lower() for f in phot_names] reset_order = False if actual != expected: - # reorder photometry_array to match expected filter order - # we will revert to the original order before returning - - # check same filters in both, ignoring order (set seems broken) - if not set(actual).issubset(set(expected)): + # Reorder photometry_array to the model's filter order, reverting + # to the caller's order before returning. + if sorted(actual) != sorted(expected): raise ValueError( f"Filter names in phot_names do not match score model filter names. " - f"Expected: {set(expected)}, actual: {set(actual)}" + f"Expected: {sorted(set(expected))}, actual: {sorted(set(actual))}" ) reset_order = True - actual_lower = [f.lower() for f in actual] - expected_lower = [f.lower() for f in expected] - #print(expected_lower) - #print(actual_lower) - - new_indices = [actual_lower.index(filter) for filter in expected_lower] - + new_indices = [actual.index(filt) for filt in expected] photometry_array = photometry_array[new_indices, :] - phot_names = [phot_names[i] for i in new_indices] resetting_indices = np.argsort(new_indices) # Ensure we have a unyt_array in Jy @@ -994,52 +988,33 @@ def _apply_score_based_noise_model( "flux_units must be provided when photometry_array is not a unyt_array." ) - n_filters, N = phot_jy.shape - # Repeat each galaxy N_scatters times: (n_filters, N*N_scatters) phot_jy_rep = np.repeat(phot_jy, N_scatters, axis=1) - # AB magnitudes for the score model: shape (N*N_scatters, n_filters) - # check if this is an asinh score model - if so, we should provide flux directly - if score_model._is_asinh_scaled: - # For asinh-scaled models, we provide flux directly - phot_mag = phot_jy_rep.T * Jy - else: - safe_jy = np.where(phot_jy_rep > 0, phot_jy_rep, np.finfo(np.float32).tiny) - phot_mag = (-2.5 * np.log10(safe_jy / 3631.0)).T.astype(np.float32) - - # Sample per-band uncertainties: (N*N_scatters, n_filters) in Jy - sigma_jy = score_model.sample_uncertainty(phot_mag, n_samples=1) * 1e-6 # convert from uJy to Jy + # Sample per-band uncertainties: (N*N_scatters, n_filters). The model + # applies its own AB/asinh conversion, so pass fluxes with units. + sigma = score_model.sample_uncertainty(unyt_array(phot_jy_rep.T, units=Jy), n_samples=1) + sigma_jy = unyt_array(sigma, units=score_model.sigma_units).to_value(Jy) # Gaussian noise in Jy, then noisy flux: (N*N_scatters, n_filters) noise = np.random.normal(0.0, sigma_jy) noisy_jy = phot_jy_rep.T + noise # (N*N_scatters, n_filters) - if normed_flux_units == "asinh": - # Noisy asinh magnitudes - asinh_softening_parameter = score_model._asinh_b_factor - - errors = f_jy_err_to_asinh( - noisy_jy*Jy, - sigma_jy*Jy, - f_b=asinh_softening_parameter, - ).T - - scattered = f_jy_to_asinh( - noisy_jy*Jy, - f_b=asinh_softening_parameter, - ).T - - + # f_b is stored per filter; broadcast it over the object axis so the + # (N, n_filters) layout used here matches the utility's expectations. + f_b = unyt_array( + np.broadcast_to(score_model.asinh_b_factor.to_value(Jy), noisy_jy.shape).copy(), + units=Jy, + ) + errors = f_jy_err_to_asinh(noisy_jy * Jy, sigma_jy * Jy, f_b=f_b).T + scattered = f_jy_to_asinh(noisy_jy * Jy, f_b=f_b).T elif normed_flux_units == "AB": - print('AB') # Noisy AB magnitudes safe_noisy = np.where(noisy_jy > 0, noisy_jy, np.finfo(np.float32).tiny) scattered = (-2.5 * np.log10(safe_noisy / 3631.0)).T # (n_filters, N*N_scatters) # AB magnitude error: σ_AB = (2.5/ln10) * σ_Jy / F_Jy - ref_jy = np.where(noisy_jy > 0, noisy_jy, np.finfo(np.float32).tiny) - errors = ((2.5 / np.log(10)) * sigma_jy / ref_jy).T + errors = ((2.5 / np.log(10)) * sigma_jy / safe_noisy).T else: scale = unyt_array(1.0, "Jy").to(normed_flux_units).value scattered = (noisy_jy * scale).T @@ -1050,8 +1025,6 @@ def _apply_score_based_noise_model( scattered = scattered[resetting_indices] errors = errors[resetting_indices] - print('shapes') - print(scattered.shape) if return_errors: return scattered, errors return scattered @@ -1751,8 +1724,8 @@ def create_feature_array_from_raw_photometry( for model in empirical_noise_models.values() ] ) - if isinstance(empirical_noise_models, ScoreBasedUncertaintyModel) and empirical_noise_models._is_asinh_scaled: - err_is_asinh = True + if isinstance(empirical_noise_models, ScoreBasedUncertaintyModel): + err_is_asinh = empirical_noise_models.is_asinh_scaled assert asinh_softening_parameters is not None or err_is_asinh, ( "asinh_softening_parameters must be provided for asinh normalization." ) @@ -2437,35 +2410,35 @@ def bin_noisy_testing_data( return X_test, y_test phot_units = self.feature_array_flags["normed_flux_units"] - if self.feature_array_flags.get("empirical_noise_models", None) is not None: - nm = self.feature_array_flags["empirical_noise_models"] - nm = isinstance(nm, dict) and all( - isinstance(m, AsinhEmpiricalUncertaintyModel) for m in nm.values() - ) + # `nm` records whether the features are in asinh magnitudes, in which case + # `asinh_b_factors` maps each feature to the softening parameter used. + nm = False + asinh_b_factors = {} + noise_models = self.feature_array_flags.get("empirical_noise_models", None) + if isinstance(noise_models, ScoreBasedUncertaintyModel): + nm = noise_models.is_asinh_scaled if nm: - f_b = self.feature_array_flags["empirical_noise_models"][feature_name].b.to("Jy") - - if isinstance(self.feature_array_flags.get("empirical_noise_models"), ScoreBasedUncertaintyModel): - if self.feature_array_flags["empirical_noise_models"]._is_asinh_scaled: - nm = True - f_b = self.feature_array_flags["empirical_noise_models"]._asinh_b_factor.to("Jy") - else: - nm = False - else: - nm = False + b_factor = noise_models.asinh_b_factor.to("Jy") + asinh_b_factors = { + name.lower(): b_factor[i] for i, name in enumerate(noise_models.filter_names) + } + elif isinstance(noise_models, dict) and noise_models: + nm = all(isinstance(m, AsinhEmpiricalUncertaintyModel) for m in noise_models.values()) + if nm: + asinh_b_factors = {name.lower(): m.b.to("Jy") for name, m in noise_models.items()} snrs = [] for feature_name in snr_feature_names or self.feature_names: if feature_name.startswith("unc_"): break - elif nm: + elif nm and feature_name.lower() in asinh_b_factors: feature_index = self.feature_names.index(feature_name) err_index = self.feature_names.index(f"unc_{feature_name}") # Need to know SNR from asinh magnitudes. snr = asinh_to_snr( X_test[:, feature_index], X_test[:, err_index], - f_b=f_b, + f_b=asinh_b_factors[feature_name.lower()], ) elif phot_units == "AB": @@ -6614,7 +6587,6 @@ def sample_posterior( # ) # shape = test_sample.shape - sampling_kwargs = {} # check if sampler.sample accepts arbitrary keyword arguments, # if so pass them the timeout_seconds_per_test @@ -6633,8 +6605,9 @@ def sample_posterior( start_time = time.time() try: # print(X_test_array[i]) - samples[i] = sampler.sample(nsteps=num_samples, x=X_test_array[i], progress=False, - **sampling_kwargs) + samples[i] = sampler.sample( + nsteps=num_samples, x=X_test_array[i], progress=False, **sampling_kwargs + ) """samples[i] = sample_with_timeout( sampler, num_samples, diff --git a/tests/test_uncertainty_models.py b/tests/test_uncertainty_models.py index 9e91209..976bc63 100644 --- a/tests/test_uncertainty_models.py +++ b/tests/test_uncertainty_models.py @@ -12,6 +12,7 @@ AsinhEmpiricalUncertaintyModel, DepthUncertaintyModel, GeneralEmpiricalUncertaintyModel, + ScoreBasedUncertaintyModel, UncertaintyModel, load_unc_model_from_hdf5, save_unc_model_to_hdf5, @@ -511,10 +512,14 @@ def test_upper_limit_error_behaviours(mock_data, err_behaviour, expected_error_f # ============================================================================= # Tests for ScoreBasedUncertaintyModel +# +# The model itself lives in the syntillate package and is tested there; these +# tests cover Synference's integration with it — the re-export, the shared +# HDF5 serialization registry, and the SBI_Fitter noise-application path. # ============================================================================= -# Minimal training kwargs used throughout — tiny network, very few epochs so -# the tests run in seconds while still exercising the full code path. +# Minimal training kwargs — tiny network, very few epochs so the tests run in +# seconds while still exercising the full code path. _SCORE_TRAIN_KWARGS = dict( n_epochs=50, batch_size=128, @@ -529,7 +534,7 @@ def test_upper_limit_error_behaviours(mock_data, err_behaviour, expected_error_f @pytest.fixture(scope="module") def score_model_data(): - """Minimal photometric data (AB mags + flux uncertainties in Jy) for two bands.""" + """Minimal photometric data (AB mags + flux uncertainties in uJy) for two bands.""" rng = np.random.default_rng(42) N = 2000 filter_names = ["F115W", "F200W"] @@ -540,18 +545,15 @@ def score_model_data(): rng.normal(24.5, 1.5, N), ] ).astype(np.float32) - # Flux uncertainties in Jy: faint-end power law - sigma_jy = 10 ** (-0.4 * (mags - 8.9)) * 0.1 * rng.lognormal(0, 0.3, mags.shape) - sigma_jy = sigma_jy.astype(np.float32) - return filter_names, mags, sigma_jy + # Flux uncertainties in uJy: faint-end power law + sigma = 10 ** (-0.4 * (mags - 8.9)) * 0.1 * rng.lognormal(0, 0.3, mags.shape) + return filter_names, mags, sigma.astype(np.float32) @pytest.fixture(scope="module") def trained_score_model(score_model_data): """A ScoreBasedUncertaintyModel trained with minimal settings.""" - from synference.noise_models import ScoreBasedUncertaintyModel - - filter_names, mags, sigma_jy = score_model_data + filter_names, mags, sigma = score_model_data model = ScoreBasedUncertaintyModel( filter_names=filter_names, hidden_dim=32, @@ -559,160 +561,34 @@ def trained_score_model(score_model_data): time_embed_dim=16, device="cpu", ) - model.fit(mags, sigma_jy, **_SCORE_TRAIN_KWARGS) + model.fit(mags, sigma, **_SCORE_TRAIN_KWARGS) return model -# --- Construction --- - - -def test_score_model_stores_filter_names(): - """filter_names is stored and n_filters is derived correctly.""" - from synference.noise_models import ScoreBasedUncertaintyModel - - names = ["u", "g", "r", "i", "z"] - m = ScoreBasedUncertaintyModel(filter_names=names, device="cpu") - assert m.filter_names == names - assert m.n_filters == len(names) - - -# --- fit() validation --- - - -def test_score_model_fit_wrong_shape(score_model_data): - """fit() raises ValueError for wrong number of magnitude columns.""" - from synference.noise_models import ScoreBasedUncertaintyModel - - filter_names, mags, sigma_jy = score_model_data - model = ScoreBasedUncertaintyModel(filter_names=filter_names, device="cpu") - bad_mags = mags[:, :1] # only 1 band, model expects 2 - with pytest.raises(ValueError, match="photometry must be*"): - model.fit(bad_mags, sigma_jy[:, :1], **_SCORE_TRAIN_KWARGS) - - -def test_score_model_fit_produces_history(trained_score_model): - """fit() returns a dict with train_loss and val_loss lists.""" - from synference.noise_models import ScoreBasedUncertaintyModel - - filter_names = trained_score_model.filter_names - mags = np.random.default_rng(7).normal(25, 1, (200, len(filter_names))).astype(np.float32) - sigma = np.full_like(mags, 1e-6) - model = ScoreBasedUncertaintyModel(filter_names=filter_names, device="cpu") - history = model.fit(mags, sigma, **_SCORE_TRAIN_KWARGS) - assert "train_loss" in history and "val_loss" in history - assert len(history["train_loss"]) > 0 - - -# --- sample_uncertainty() --- - - -def test_score_sample_uncertainty_shape_single(trained_score_model, score_model_data): - """sample_uncertainty returns (n_filters,) for a single-object input.""" - filter_names, mags, _ = score_model_data - sigma = trained_score_model.sample_uncertainty(mags[0], **_SCORE_SAMPLE_KWARGS) - assert sigma.shape == (len(filter_names),) - # exp(x) is always ≥ 0; float32 underflow to 0 is possible with an undertrained model - assert np.all(sigma >= 0) - - -def test_score_sample_uncertainty_shape_batch(trained_score_model, score_model_data): - """sample_uncertainty returns (N, n_filters) for a batch input.""" - filter_names, mags, _ = score_model_data - batch = mags[:10] - sigma = trained_score_model.sample_uncertainty(batch, **_SCORE_SAMPLE_KWARGS) - assert sigma.shape == (10, len(filter_names)) - # exp(x) is always ≥ 0; float32 underflow to 0 is possible with an undertrained model - assert np.all(sigma >= 0) - - -def test_score_sample_uncertainty_multi_sample(trained_score_model, score_model_data): - """n_samples > 1 returns (N, n_samples, n_filters).""" - filter_names, mags, _ = score_model_data - sigma = trained_score_model.sample_uncertainty(mags[:5], n_samples=3, **_SCORE_SAMPLE_KWARGS) - assert sigma.shape == (5, 3, len(filter_names)) - - -def test_score_sample_uncertainty_raises_before_fit(): - """sample_uncertainty raises RuntimeError if called before fit().""" - from synference.noise_models import ScoreBasedUncertaintyModel - - model = ScoreBasedUncertaintyModel(filter_names=["F115W"], device="cpu") - with pytest.raises(RuntimeError): - model.sample_uncertainty(np.array([[25.0]]), **_SCORE_SAMPLE_KWARGS) +# --- Re-export --- -def test_score_sample_uncertainty_wrong_n_filters(trained_score_model): - """sample_uncertainty raises ValueError for wrong number of filters.""" - bad_mags = np.array([[25.0, 24.0, 23.0]]) # 3 bands, model expects 2 - with pytest.raises(ValueError, match="filters"): - trained_score_model.sample_uncertainty(bad_mags, **_SCORE_SAMPLE_KWARGS) +def test_score_model_reexported_from_syntillate(): + """Synference re-exports syntillate's ScoreBasedUncertaintyModel unchanged.""" + from syntillate import ScoreBasedUncertaintyModel as SyntillateModel + assert ScoreBasedUncertaintyModel is SyntillateModel -# --- apply_noise() --- - -def test_score_apply_noise_unyt_input(trained_score_model): - """apply_noise works with a unyt_array input and returns a unyt_array in Jy.""" - from unyt import uJy - - n_filters = trained_score_model.n_filters - flux = np.array([1e-6] * n_filters) * uJy - result = trained_score_model.apply_noise(flux, **_SCORE_SAMPLE_KWARGS) - assert hasattr(result, "units") - - -def test_score_apply_noise_return_noise(trained_score_model): - """apply_noise with return_noise=True returns (noisy_flux, sigma) tuple.""" - from unyt import uJy - - from synference.noise_models import ScoreBasedUncertaintyModel - - filter_names = trained_score_model.filter_names - model = ScoreBasedUncertaintyModel( - filter_names=filter_names, - hidden_dim=32, - n_layers=2, - time_embed_dim=16, - device="cpu", - return_noise=True, - ) - # Use trained weights from the fixture model - model._is_trained = True - model._ln_sigma_median = trained_score_model._ln_sigma_median - model._ln_sigma_iqr = trained_score_model._ln_sigma_iqr - model._mag_median = trained_score_model._mag_median - model._mag_iqr = trained_score_model._mag_iqr - model.ema_net = trained_score_model.ema_net - model.score_net = trained_score_model.score_net - - flux = np.array([1e-6] * trained_score_model.n_filters) * uJy - noisy, sigma = model.apply_noise(flux, **_SCORE_SAMPLE_KWARGS) - assert noisy.shape == flux.shape - assert sigma.shape == flux.shape - - -def test_score_apply_noise_requires_units(trained_score_model): - """apply_noise raises ValueError when flux is plain array with no units arg.""" - flux = np.array([1e-9] * trained_score_model.n_filters) - with pytest.raises(ValueError, match="true_flux_units"): - trained_score_model.apply_noise(flux, **_SCORE_SAMPLE_KWARGS) - - -# --- HDF5 serialization --- +# --- HDF5 serialization through synference's registry --- def test_score_model_hdf5_round_trip(trained_score_model, temp_dir): - """Saving and loading a ScoreBasedUncertaintyModel preserves filter_names and weights.""" + """A ScoreBasedUncertaintyModel round-trips through synference's HDF5 helpers.""" path = os.path.join(temp_dir, "score_model.h5") group = "score_test" save_unc_model_to_hdf5(trained_score_model, path, group, overwrite=True) loaded = load_unc_model_from_hdf5(path, group) - from synference.noise_models import ScoreBasedUncertaintyModel - assert isinstance(loaded, ScoreBasedUncertaintyModel) assert loaded.filter_names == trained_score_model.filter_names assert loaded.n_filters == trained_score_model.n_filters + assert loaded.sigma_units == trained_score_model.sigma_units assert loaded._is_trained # Sampling should work and give finite values @@ -721,24 +597,56 @@ def test_score_model_hdf5_round_trip(trained_score_model, temp_dir): sigma_load = loaded.sample_uncertainty(mags, **_SCORE_SAMPLE_KWARGS) assert np.all(np.isfinite(sigma_orig)) assert np.all(np.isfinite(sigma_load)) - # exp(x) ≥ 0 always; underflow to 0 is possible with an undertrained model + # exp(x) >= 0 always; underflow to 0 is possible with an undertrained model assert np.all(sigma_orig >= 0) and np.all(sigma_load >= 0) +def test_score_model_hdf5_sentinel_key_round_trip(trained_score_model, temp_dir): + """ScoreBasedUncertaintyModel round-trips through HDF5 under the save_state sentinel key. + + The sentinel key ``__score_based__`` is what save_state uses when it writes a + ScoreBasedUncertaintyModel; load_model_from_pkl then reads it back by that key. + This test verifies that the HDF5 infrastructure supports it correctly. + """ + path = os.path.join(temp_dir, "score_sentinel.h5") + sentinel = "__score_based__" + + # Simulate what save_state does: write under the sentinel key + save_unc_model_to_hdf5(trained_score_model, path, sentinel, overwrite=True) + assert os.path.exists(path) + + # Simulate what load_model_from_pkl does: load back by sentinel key + loaded = load_unc_model_from_hdf5(path, sentinel) + + assert isinstance(loaded, ScoreBasedUncertaintyModel) + assert loaded.filter_names == trained_score_model.filter_names + assert loaded._is_trained + + mags = np.array([[25.0, 24.5]], dtype=np.float32) + sigma = loaded.sample_uncertainty(mags, **_SCORE_SAMPLE_KWARGS) + assert np.all(np.isfinite(sigma)) + assert np.all(sigma >= 0) + + # --- sbi_runner integration --- -def test_sbi_runner_apply_score_based_noise_model(trained_score_model): - """_apply_score_based_noise_model returns correctly shaped arrays.""" +def _score_fitter(): + """A minimal SBI_Fitter stub exposing only _apply_score_based_noise_model.""" from unittest.mock import MagicMock from synference.sbi_runner import SBI_Fitter - # Build a minimal SBI_Fitter-like object with the method available without a library fitter = MagicMock(spec=SBI_Fitter) fitter._apply_score_based_noise_model = SBI_Fitter._apply_score_based_noise_model.__get__( fitter, SBI_Fitter ) + return fitter + + +def test_sbi_runner_apply_score_based_noise_model(trained_score_model): + """_apply_score_based_noise_model returns correctly shaped arrays.""" + fitter = _score_fitter() filter_names = trained_score_model.filter_names n_filters = len(filter_names) @@ -761,41 +669,58 @@ def test_sbi_runner_apply_score_based_noise_model(trained_score_model): assert errors.shape == (n_filters, N * N_scatters) assert np.all(np.isfinite(scattered)) assert np.all(np.isfinite(errors)) - # exp(x) ≥ 0 always; underflow to 0 is possible with undertrained model + # exp(x) >= 0 always; underflow to 0 is possible with an undertrained model assert np.all(errors >= 0) -def test_sbi_runner_apply_score_wrong_filter_order(trained_score_model): - """_apply_score_based_noise_model raises ValueError on filter order mismatch.""" - from unittest.mock import MagicMock - - from synference.sbi_runner import SBI_Fitter +def test_sbi_runner_apply_score_reorders_filters(trained_score_model): + """Filters supplied out of order are reordered for the model and restored after.""" + fitter = _score_fitter() - fitter = MagicMock(spec=SBI_Fitter) - fitter._apply_score_based_noise_model = SBI_Fitter._apply_score_based_noise_model.__get__( - fitter, SBI_Fitter + n_filters = trained_score_model.n_filters + N = 5 + phot = unyt_array(np.arange(1, n_filters * N + 1, dtype=float).reshape(n_filters, N) * 1e-7) + phot = unyt_array(phot.value, units="Jy") + reversed_names = list(reversed(trained_score_model.filter_names)) + + in_order = fitter._apply_score_based_noise_model( + phot[::-1], + trained_score_model.filter_names, + trained_score_model, + N_scatters=1, + normed_flux_units="nJy", ) + out_of_order = fitter._apply_score_based_noise_model( + phot[::-1], + reversed_names, + trained_score_model, + N_scatters=1, + normed_flux_units="nJy", + ) + + assert out_of_order.shape == (n_filters, N) + # The reordering only permutes rows; the noise draw differs, but the input + # flux each row was built from must line up with the caller's ordering. + assert np.all(np.isfinite(out_of_order)) + assert in_order.shape == out_of_order.shape + + +def test_sbi_runner_apply_score_unknown_filters(trained_score_model): + """_apply_score_based_noise_model raises when the filter sets do not match.""" + fitter = _score_fitter() n_filters = trained_score_model.n_filters phot = unyt_array(np.ones((n_filters, 5)), units="Jy") - wrong_names = list(reversed(trained_score_model.filter_names)) - - fitter._apply_score_based_noise_model( - phot, wrong_names, trained_score_model, normed_flux_units="AB" - ) + with pytest.raises(ValueError, match="do not match"): + fitter._apply_score_based_noise_model( + phot, ["not_a_filter"] * n_filters, trained_score_model, normed_flux_units="AB" + ) def test_sbi_runner_apply_score_asinh_raises(trained_score_model): - """_apply_score_based_noise_model rejects asinh as normed_flux_units.""" - from unittest.mock import MagicMock - - from synference.sbi_runner import SBI_Fitter - - fitter = MagicMock(spec=SBI_Fitter) - fitter._apply_score_based_noise_model = SBI_Fitter._apply_score_based_noise_model.__get__( - fitter, SBI_Fitter - ) + """_apply_score_based_noise_model rejects asinh output from an AB-scaled model.""" + fitter = _score_fitter() n_filters = trained_score_model.n_filters phot = unyt_array(np.ones((n_filters, 5)), units="Jy") @@ -809,36 +734,5 @@ def test_sbi_runner_apply_score_asinh_raises(trained_score_model): ) -def test_score_model_hdf5_sentinel_key_round_trip(trained_score_model, temp_dir): - """ScoreBasedUncertaintyModel round-trips through HDF5 under the save_state sentinel key. - - The sentinel key ``__score_based__`` is what save_state uses when it writes a - ScoreBasedUncertaintyModel; load_model_from_pkl then reads it back by that key. - This test verifies that the HDF5 infrastructure supports it correctly. - """ - from synference.noise_models import ScoreBasedUncertaintyModel - - path = os.path.join(temp_dir, "score_sentinel.h5") - sentinel = "__score_based__" - - # Simulate what save_state does: write under the sentinel key - save_unc_model_to_hdf5(trained_score_model, path, sentinel, overwrite=True) - assert os.path.exists(path) - - # Simulate what load_model_from_pkl does: load back by sentinel key - loaded = load_unc_model_from_hdf5(path, sentinel) - - assert isinstance(loaded, ScoreBasedUncertaintyModel) - assert loaded.filter_names == trained_score_model.filter_names - assert loaded.n_filters == trained_score_model.n_filters - assert loaded._is_trained - - # Verify the restored model can produce finite uncertainty samples - mags = np.array([[25.0, 24.5]], dtype=np.float32) - sigma = loaded.sample_uncertainty(mags, **_SCORE_SAMPLE_KWARGS) - assert np.all(np.isfinite(sigma)) - assert np.all(sigma >= 0) - - if __name__ == "__main__": pytest.main([__file__]) From 61e05640673f1889574220d7934cad2547f182e9 Mon Sep 17 00:00:00 2001 From: Thomas Harvey Date: Sat, 25 Jul 2026 16:59:10 +0100 Subject: [PATCH 22/22] add to dependencies --- pyproject.toml | 2 -- 1 file changed, 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 8cf6046..ccc90c2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -68,8 +68,6 @@ dependencies = [ "matplotlib", "numba", # Score-based diffusion noise model (ScoreBasedUncertaintyModel). - # Until syntillate is on PyPI, install it first with: - # pip install git+https://github.com/synthesizer-project/syntillate.git "syntillate", ]