Skip to content

Inputs

Helpers for coercing user inputs (NumPy arrays, astropy CCDData, FITS HDUs) into the data/variance/mask layers the pipeline works with, and for synthesizing a variance map from a detector noise model.

as_layers

as_layers(
    obj,
    variance: NDArray | None = None,
    mask: NDArray | None = None,
) -> Layers

Coerce an image input to contiguous (data float32, variance, mask uint8).

Explicit variance / mask arguments override anything carried by the object. A boolean mask is interpreted as True == bad (-> kMaskBad).

Source code in python/delta/_inputs.py
def as_layers(
    obj,
    variance: NDArray | None = None,
    mask: NDArray | None = None,
) -> Layers:
    """Coerce an image input to contiguous (data float32, variance, mask uint8).

    Explicit ``variance`` / ``mask`` arguments override anything carried by the
    object. A boolean mask is interpreted as True == bad (-> kMaskBad).
    """
    data, v, m = _split(obj)
    if variance is not None:
        v = variance
    if mask is not None:
        m = mask

    data = np.ascontiguousarray(data, dtype=np.float32)
    if data.ndim != 2:
        raise ValueError(f"expected a 2-D image, got shape {data.shape}")

    if v is not None:
        v = np.ascontiguousarray(v, dtype=np.float32)
        if v.shape != data.shape:
            raise ValueError("variance shape does not match data")
    if m is not None:
        m = np.asarray(m)
        if m.dtype == bool:
            m = m.astype(np.uint8)  # True (bad) -> 1 == kMaskBad
        m = np.ascontiguousarray(m, dtype=np.uint8)
        if m.shape != data.shape:
            raise ValueError("mask shape does not match data")
    return data, v, m

synth_variance

synth_variance(
    data: NDArray[float32],
    gain: float,
    read_noise: float = 0.0,
) -> NDArray[float32]

Synthesize a per-pixel variance map from gain and read noise (SPEC §3.6).

In ADU, Var = max(data, 0) / gain + read_noise**2 (read_noise in ADU): the Poisson term from the source/sky plus the read-noise floor.

Source code in python/delta/_inputs.py
def synth_variance(
    data: NDArray[np.float32], gain: float, read_noise: float = 0.0
) -> NDArray[np.float32]:
    """Synthesize a per-pixel variance map from gain and read noise (SPEC §3.6).

    In ADU, Var = max(data, 0) / gain + read_noise**2 (read_noise in ADU): the
    Poisson term from the source/sky plus the read-noise floor.
    """
    if gain <= 0.0:
        raise ValueError("gain must be > 0")
    var = np.clip(data, 0.0, None) / gain + float(read_noise) ** 2
    return np.ascontiguousarray(var, dtype=np.float32)