Source code for wcc_sim.render

"""Place PSF stamps on the detector grid and apply the ETC noise model."""

from dataclasses import dataclass

import numpy as np
from astropy import units as u
from wcc_etc.psfsim import saturation_mask_from_image_e

from .detectors import saturation_level_e
from .starflux import sky_and_dark_rates

#: float64 alive at the peak of render_subgrid per detector pixel of the stamp-padded
#: footprint (accumulated output, one phase result, oaconvolve blocks); measured
#: maximum 11.9 at k = 1 for stamps of 33-1025 px.
RENDER_FLOATS_PER_PADDED_PIXEL = 12


[docs] def bin_oversampled(stamp_os, oversample): n = stamp_os.shape[0] // oversample return ( stamp_os[: n * oversample, : n * oversample] .reshape(n, oversample, n, oversample) .sum(axis=(1, 3)) )
[docs] @dataclass(frozen=True) class StampBlend: """Everything about one stamp geometry that does not depend on the star. `w` is a cosine taper (1 in the core, 0 at r >= half-width) and `model` is the *diffraction* wing on the stamp grid; blending the stamp's outer annulus into the smooth model removes the Airy-ring texture that would otherwise end abruptly at the square stamp edge. `halo` is the measured scattered-light profile on the same grid, and `core_scale` the weight on the diffraction term -- both inert (0 and 1) when there is no scatter model, which reproduces the diffraction-only rendering exactly. """ w: np.ndarray model: np.ndarray halo: np.ndarray core_scale: float
[docs] def wing_blend(n_stamp, wing, blend_start=0.8): """Precompute the stamp->wing crossfade and halo for one stamp geometry.""" half = n_stamp // 2 yy, xx = np.mgrid[:n_stamp, :n_stamp] r = np.hypot(yy - half, xx - half) t = np.clip((r - blend_start * half) / ((1.0 - blend_start) * half), 0.0, 1.0) w = 0.5 * (1.0 + np.cos(np.pi * t)) # The scatter halo is added across the whole stamp, not crossfaded in at # the edge: it is a separate additive term of the PSF, present under the # core as well (where the core outruns it by ~1e9 and it does not matter). core = getattr(wing, "core", wing) halo_model = getattr(wing, "halo", None) return StampBlend( w=w, model=core.profile(np.maximum(r, 1.0)), halo=( np.zeros_like(r) if halo_model is None else halo_model.profile(r) ), core_scale=1.0 if halo_model is None else 1.0 - halo_model.frac_total, )
def _add_wing_halo(image, x0, y0, half, scaled_flux, wing, floor_e): """Add flux*profile(r) outside the square stamp footprint, in row chunks.""" ny, nx = image.shape r_out = wing.r_out(scaled_flux, floor_e) if r_out <= half: return r_out_i = int(np.ceil(r_out)) by_lo, by_hi = max(y0 - r_out_i, 0), min(y0 + r_out_i + 1, ny) bx_lo, bx_hi = max(x0 - r_out_i, 0), min(x0 + r_out_i + 1, nx) if by_lo >= by_hi or bx_lo >= bx_hi: return xx = np.arange(bx_lo, bx_hi, dtype=float)[None, :] - x0 for cy_lo in range(by_lo, by_hi, 1024): cy_hi = min(cy_lo + 1024, by_hi) yy = np.arange(cy_lo, cy_hi, dtype=float)[:, None] - y0 rr = np.hypot(yy, xx) m = (rr <= r_out) & ((np.abs(yy) > half) | (np.abs(xx) > half)) if not m.any(): continue halo = np.zeros(rr.shape, dtype=np.float32) halo[m] = scaled_flux * wing.profile(rr[m]) image[cy_lo:cy_hi, bx_lo:bx_hi] += halo
[docs] def unit_stamp(psf_os, sx, sy, oversample, wing=None, blend=None): """Detector-resolution PSF stamp shifted by (sx, sy) fine samples, unit flux scale. The oversampled PSF is rolled by (sy, sx) fine samples and binned to detector pixels. With a `wing` model the outer annulus is crossfaded into the smooth wing and the scattered-light halo is added (see wing_blend); the flux normalisation 1 / wing.flux_norm(half) is left to the caller. Shared by add_star (stars) and render_subgrid (images), so both draw the same stamp. """ stamp = bin_oversampled(np.roll(psf_os, (sy, sx), axis=(0, 1)), oversample) if wing is None: return stamp if blend is None: blend = wing_blend(psf_os.shape[0] // oversample, wing) return (blend.core_scale * (stamp * blend.w + blend.model * (1.0 - blend.w)) + blend.halo)
[docs] def add_star(image, psf_os, x, y, flux_e, oversample, wing=None, floor_e=None, blend=None): """Add one star at float pixel (x, y), sub-pixel placed, edge-clipped. Sub-pixel shift is a fine-grid np.roll (error <= 1/(2*oversample) px); rolled wrap-around energy is negligible because stamp edges are ~0. With a `wing` model (see wcc_sim.wings), the stamp's outer annulus is crossfaded into the smooth wing profile and the halo is continued on a circular footprint out to where flux_e * profile(r) < floor_e. Star flux is renormalized by `wing.flux_norm(half)` for all stars so photometry stays magnitude-independent; the sub-floor halo of faint stars is simply not drawn. A CombinedWing additionally carries the measured scattered-light halo, which is added across the stamp as well as beyond it. """ ny, nx = image.shape n_stamp = psf_os.shape[0] // oversample half = n_stamp // 2 x0, y0 = int(round(x)), int(round(y)) if x0 + half < 0 or x0 - half >= nx or y0 + half < 0 or y0 - half >= ny: return sx = int(round((x - x0) * oversample)) sy = int(round((y - y0) * oversample)) if wing is not None: if blend is None: blend = wing_blend(n_stamp, wing) flux_e = flux_e / wing.flux_norm(float(half)) stamp = flux_e * unit_stamp(psf_os, sx, sy, oversample, wing, blend) else: stamp = unit_stamp(psf_os, sx, sy, oversample) * flux_e y_lo, y_hi = y0 - half, y0 + half + 1 x_lo, x_hi = x0 - half, x0 + half + 1 iy_lo, ix_lo = max(y_lo, 0), max(x_lo, 0) iy_hi, ix_hi = min(y_hi, ny), min(x_hi, nx) image[iy_lo:iy_hi, ix_lo:ix_hi] += stamp[ iy_lo - y_lo : iy_hi - y_lo, ix_lo - x_lo : ix_hi - x_lo ] if wing is not None and floor_e is not None: _add_wing_halo(image, x0, y0, half, flux_e, wing, floor_e)
[docs] def render_subgrid(sub, psf_k, wing=None, blend=None): """PSF-convolve light on a detector-aligned sub-grid and bin it to detector pixels. `sub` is a resample.Subgrid (data per sub-pixel, k sub-pixels per detector pixel, origin y0, x0); `psf_k` is the PSF oversampled by the same k. Polyphase rendering: the sub-pixels at offset (p, q) inside their detector pixel all use the same shifted stamp (unit_stamp), so data[p::k, q::k] is convolved with it and the k*k results are summed. Identical to one add_star(floor_e=None) per sub-pixel, at detector resolution only. No halo is drawn beyond the stamp (as in render_extended). Returns a Subgrid with k = 1 whose origin is shifted by half a stamp (the convolution's border); total_in is passed through. """ from scipy.signal import oaconvolve from .resample import Subgrid k = int(sub.k) if psf_k.shape[0] % k or psf_k.shape[0] != psf_k.shape[1]: raise ValueError(f"PSF shape {psf_k.shape} is not square with a multiple of " f"k = {k} samples per side") n_stamp = psf_k.shape[0] // k if n_stamp % 2 == 0: raise ValueError(f"PSF stamp must be odd, got {n_stamp} px") half = n_stamp // 2 if wing is not None and blend is None: blend = wing_blend(n_stamp, wing) shift = (k - 1) // 2 out = None for p in range(k): for q in range(k): stamp = unit_stamp(psf_k, q - shift, p - shift, k, wing, blend) part = oaconvolve(np.asarray(sub.data[p::k, q::k], dtype=float), stamp, mode="full") out = part if out is None else out + part if wing is not None: out = out / wing.flux_norm(float(half)) return Subgrid(out, sub.y0 - half, sub.x0 - half, 1, sub.total_in)
[docs] def render_peak_bytes(box, k, stamp_npix): """Estimated peak memory [bytes] while render_subgrid runs; 0 if box is None. box : (y0, y1, x0, x1) footprint on the detector; ny, nx = its size [detector px] k : sub-grid factor; the resampled sub-grid (n_sub values) is kept during rendering stamp_npix : PSF stamp [px]; the rendered output covers the stamp-padded footprint (n_padded detector px) """ if box is None: return 0 y0, y1, x0, x1 = box ny, nx = y1 - y0, x1 - x0 n_sub = ny * nx * k * k # kept sub-grid n_padded = (ny + stamp_npix - 1) * (nx + stamp_npix - 1) # convolution output return 8 * (n_sub + RENDER_FLOATS_PER_PADDED_PIXEL * n_padded)
[docs] def render_scene(shape, xs, ys, fluxes_e, psf_os, oversample, wing=None, floor_e=None): """Sum of PSF stamps (e-) for all stars on a float32 (ny, nx) grid.""" image = np.zeros(shape, dtype=np.float32) blend = None if wing is not None: blend = wing_blend(psf_os.shape[0] // oversample, wing) for x, y, f in zip(np.atleast_1d(xs), np.atleast_1d(ys), np.atleast_1d(fluxes_e)): add_star(image, psf_os, float(x), float(y), float(f), oversample, wing=wing, floor_e=floor_e, blend=blend) return image
[docs] def star_saturated(satmask, x, y, radius=32): """True if any saturated pixel lies within `radius` px of (x, y). A window is used rather than the central pixel alone because the defocused PSFs are centrally depressed: a bright star can saturate its ring while its central pixel stays below full well. The 2-wave defocus PSF's bright ring extends to ~29 px from center. """ ny, nx = satmask.shape x0, y0 = int(round(x)), int(round(y)) y_lo, y_hi = max(y0 - radius, 0), min(y0 + radius + 1, ny) x_lo, x_hi = max(x0 - radius, 0), min(x0 + radius + 1, nx) if y_lo >= y_hi or x_lo >= x_hi: return False return bool(satmask[y_lo:y_hi, x_lo:x_hi].any())
[docs] def add_noise_and_digitize(image_sources_e, sim, exptime, n_reads, rng, add_noise=True, sky=None): """Apply sky+dark, per-frame saturation, Poisson + read noise, ADU conversion. ETC semantics (wcc_etc.simulation): read-noise variance scales with n_reads; saturation is evaluated on the per-frame expectation. `sky` [e-/s/pix, scalar or array broadcastable to the image] replaces the sky rate of `sim` (the grating sky differs between the two halves). """ n_reads = int(n_reads) if n_reads < 1: raise ValueError(f"n_reads must be >= 1, got {n_reads}") sensor = sim.sensor gain = float(sensor.gain.to(u.electron / u.ct).value) read_noise = float(sensor.read_noise.value) adc_max = float(sensor.adc_max.to(u.ct).value) bias = float(sensor.bias_level.to(u.ct).value) sim_sky, dark = sky_and_dark_rates(sim) if sky is None: sky = sim_sky image_clean = image_sources_e + np.asarray((sky + dark) * exptime, dtype=np.float32) per_frame = image_clean / n_reads satmask = saturation_mask_from_image_e(sensor, per_frame) cap_frame_e = saturation_level_e(sensor) expectation = np.minimum(image_clean, np.float32(n_reads * cap_frame_e)) if add_noise: image_e = rng.poisson(expectation.astype(np.float64)).astype(np.float32) image_e += rng.normal( 0.0, read_noise * np.sqrt(n_reads), size=image_e.shape ).astype(np.float32) else: # the delivered frame respects the well ceiling with or without noise image_e = expectation.astype(np.float32) image_adu = np.clip(image_e / gain + bias, 0.0, n_reads * adc_max).astype( np.float32 ) return { "image_clean": image_clean.astype(np.float32), "image_e": image_e, "image_adu": image_adu, "satmask": np.asarray(satmask, dtype=bool), }