Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 45 additions & 13 deletions astra.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,8 @@ analyses:
decisions:
[star_selection_box, psf_train_validation_split,
psfex_candidate_vetting, psf_modelling_software,
psf_model_complexity, psf_acceptance_thresholds]
psf_model_complexity, psf_acceptance_thresholds,
psf_stamp_rendering]
decisions:
star_selection_box:
label: Stellar-locus selection, magnitude window and FWHM window scaled to the locus
Expand Down Expand Up @@ -1183,24 +1184,24 @@ analyses:
persistence and star-catalogue merging read MCCD products.
insights: [farrens22_two_psf_methods]
psf_model_complexity:
label: PSFEx pixel basis with degree-2 spatial variation per CCD
label: PSFEx pixel basis with degree-2 spatial variation per CCD (1.0 default, 0.7 named Run-2 config)
rationale: >-
A pixel basis with polynomial variation in XWIN/YWIN, fit per CCD
at native sampling. PSF_ACCURACY is the fractional accuracy PSFEx
assumes for PSF pixel values; per the PSFEx documentation it enters
the fit weights, and so how closely the model follows bright stars.
Model flexibility trades overfitting against PSF leakage, the
dominant additive systematic in cosmic shear. A more flexible model
must be supported by the surviving training stars and checked on
held-out residuals under the acceptance gate; successful optimisation
alone does not establish a usable PSF.
Stock values; no rationale recorded.
A pixel basis with polynomial variation in XWIN/YWIN, fit per CCD.
The committed default (default.psfex: PSF_SAMPLING = 1, PSF_SIZE = 51)
preserves Run 1 reproducibility; a named Run-2 config
(default_0p7.psfex: PSF_SAMPLING = 0.7, PSF_SIZE = 73, selected via
psfex_config in the workflow run config) oversamples the model while
spanning the same 51-pixel native stamp. Unit sampling leaves a -0.77%
centred size bias at 0.55-0.65" seeing and a -3.9e-3 g1 residual below
0.5" seeing (shapepipe#989); 0.5-pixel sampling degrades PSF recovery
on sparse high-latitude CCDs; 0.7 sits at an empirical zero-crossing
of the centred-render size bias across seeings and star counts.
Values:
BASIS_TYPE = PIXEL;
PSF_ACCURACY = 0.01;
BASIS_NUMBER = 20;
PSFVAR_DEGREES = 2;
PSF_SAMPLING = 1;
PSF_SAMPLING = 1 (default.psfex) or 0.7 (default_0p7.psfex);
PSFVAR_KEYS = XWIN_IMAGE,YWIN_IMAGE;
PSFVAR_GROUPS = 1,1;
MEF_TYPE = INDEPENDENT.
Expand Down Expand Up @@ -1253,6 +1254,37 @@ analyses:
excluded: true
excluded_reason: >-
CFIS CCDs are smaller than DECam's; the floor is survey-specific.
psf_stamp_rendering:
label: PSF models rendered as native-pixel stamps, centred or at the compared stamp's phase
rationale: >-
psfex_interp resamples the PSFEx model onto the native pixel grid
with PSFEx's own model-to-vignet mapping (separable Lanczos-3,
unit-sum weights), so validation HSM, the galaxy_psf store and
ngmix receive native-pixel effective-PSF stamps whatever
PSF_SAMPLING is; at unit sampling with centred stamps the model grid
is passed through unchanged. Each stamp is either centred or drawn
at the sub-pixel phase of the stamp it is compared with: the star's
SExtractor VIGNET (cut around the rounded X_IMAGE pixel) in
validation, each epoch's galaxy stamp (the vignetmaker OFFSET) on
the multi-epoch path. On a 0.50 arcsec exposure (shapepipe#989)
phase matching moves the validation star-minus-PSF g1 residual from
-4.0e-3 to between -1.0e-3 and -2.7e-3 at native sampling, and from
-1.3e-3 to consistent with zero with a 0.5-sampled model. On the
galaxy path, a phase-matched PSF makes metacal deconvolution exact
for compact galaxies and leaves the deconvolved galaxy at the stamp
centre, a sub-pixel offset from the ngmix centroid prior (sigma
1 px); its effect on shear is unmeasured.
Values:
config_exp_psfex.ini#PSFEX_INTERP_RUNNER.STAMP_PHASE = False;
config_tile_PiViVi_psfex.ini#PSFEX_INTERP_RUNNER.STAMP_PHASE = False.
default: centred
options:
centred:
label: PSF centred on the native stamp on both paths (validates the exact galaxy-path PSF)
validation_phase_galaxy_centred:
label: Validation PSFs at star VIGNET phase (PSFEx diagnostic); galaxy PSFs centred
stamp_phase:
label: PSF at the sub-pixel phase of the star VIGNET or galaxy stamp
prior_insights:
des_psf_blacklist_local:
claim: >-
Expand Down
183 changes: 177 additions & 6 deletions src/shapepipe/modules/psfex_interp_package/psfex_interp.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,117 @@
FILE_NOT_FOUND = "File_not_found"


def _lanczos3(x):
"""Lanczos-3 kernel, as PSFEx's ``INTERPF`` (``vignet.h``)."""
return np.where(np.abs(x) < 3, np.sinc(x) * np.sinc(x / 3), 0.0)


def _resample_matrix(n_model, n_native, sampling, offset):
"""Separable Lanczos-3 resampling matrix, native rows x model columns.

Native pixel ``j`` sits at model coordinate
``n_model // 2 + (j - n_native // 2 - offset) / sampling``; each row holds
the Lanczos-3 weights of the model pixels around it, normalised to unit
sum as PSFEx's ``vignet_resample`` does.
"""
native = np.arange(n_native) - n_native // 2 - offset
model_pos = n_model // 2 + native / sampling
weights = _lanczos3(model_pos[:, None] - np.arange(n_model)[None, :])
return weights / weights.sum(axis=1, keepdims=True)


def native_psf_stamps(psfs, sampling, offsets=None):
"""Native-Pixel PSF Stamps.

Render PSFEx model stamps on the native pixel grid.

@sc [decision:star_selection_psf.psf_stamp_rendering]
A PSFEx model (``PSF_MASK`` combined over its basis) is a grid of the
effective (pixel-convolved) PSF, ``sampling`` native pixels apart,
centred on grid index ``n // 2``, with values in native-pixel units: the
grid sums to about ``1 / sampling**2``. PSFEx fits that grid by resampling
it onto each training star's vignet (``psf_refine``, ``vignet_resample``),
so the image of a star the model predicts is that resampling. This
function applies it with PSFEx's kernel and conventions: separable
Lanczos-3, weights normalised to unit sum per output pixel, the model
centre ``n_model // 2`` placed on the native stamp centre
``n_native // 2`` shifted by ``offset``. A native stamp therefore carries
the model's flux in native pixels (about 1), and no pixel response is
applied: the model already is the effective PSF.

The native stamp is ``n_native x n_native`` with ``n_native`` the odd
integer ``int(n_model * sampling) | 1``: 51 for a 51-pixel model at unit
sampling and for a 101-pixel model at sampling 0.5.

At ``sampling == 1`` with no offsets the model grid already is the native
stamp and is returned unchanged.

Parameters
----------
psfs : numpy.ndarray
Model-grid stamps, ``(N, n_model, n_model)``.
sampling : float
Model pixel size in native pixels (the ``.psf`` ``PSF_SAMP``).
offsets : numpy.ndarray, optional
``(N, 2)`` sub-pixel ``(dx, dy)`` of each PSF centre from the native
stamp centre, in native pixels. ``None`` centres every stamp.

Returns
-------
numpy.ndarray
Native stamps, ``(N, n_native, n_native)``.

"""
if sampling == 1 and offsets is None:
return psfs

n_model = psfs.shape[-1]
n_native = int(n_model * sampling) | 1
if offsets is None:
offsets = np.zeros((len(psfs), 2))

stamps = np.empty((len(psfs), n_native, n_native))
for idx, (psf, (dx, dy)) in enumerate(zip(psfs, offsets)):
kx = _resample_matrix(n_model, n_native, sampling, dx)
ky = _resample_matrix(n_model, n_native, sampling, dy)
stamps[idx] = ky @ psf @ kx.T
return stamps


def sextractor_vignet_offsets(pos, barycentre):
"""SExtractor VIGNET Offsets.

Sub-pixel offset of each position from the centre of its SExtractor
``VIGNET``.

SExtractor cuts ``VIGNET`` around the 0-indexed pixel
``int(mx + 0.49999)``, where ``mx = X_IMAGE - 1`` is the isophotal
barycentre, and places that pixel at stamp index ``w // 2``. The
windowed centroid ``XWIN_IMAGE`` can round to a different pixel than
``X_IMAGE``, so the offset is measured from the ``X_IMAGE`` pixel.
Measured on SExtractor 2.25 VIGNETs (shapepipe#989): this convention
places the HSM centroid of 268 bright stars within 0.03 px of
``XWIN_IMAGE``, while rounding ``XWIN_IMAGE`` misplaces 28 of them by a
whole pixel.

Parameters
----------
pos : numpy.ndarray
``(N, 2)`` 1-indexed image positions the PSF is evaluated at
(``XWIN_IMAGE``, ``YWIN_IMAGE``).
barycentre : numpy.ndarray
``(N, 2)`` 1-indexed ``X_IMAGE``, ``Y_IMAGE``.

Returns
-------
numpy.ndarray
``(N, 2)`` ``(dx, dy)`` offsets in pixels.

"""
centre_pixel = np.floor(barycentre - 1 + 0.49999)
return pos - 1 - centre_pixel


def local_wcs_list(wcs, positions):
"""Local WCS List.

Expand Down Expand Up @@ -263,6 +374,10 @@ class PSFExInterpolator(object):
Threshold of stars under which the PSF is not interpolated
thresh_chi2 : int
Threshold for chi squared
stamp_phase : bool
If ``True``, render each PSF at the sub-pixel phase of the stamp it is
compared with or fitted against, rather than centred; see
:meth:`_stamp_offsets` and :meth:`process_me`

"""

Expand All @@ -277,6 +392,7 @@ def __init__(
get_shapes=True,
star_thresh=20,
chi2_thresh=2,
stamp_phase=False,
):

# Path to PSFEx output file
Expand All @@ -303,6 +419,8 @@ def __init__(

self._chi2_thresh = chi2_thresh

self._stamp_phase = stamp_phase

# Logging
self._w_log = w_log

Expand Down Expand Up @@ -408,10 +526,12 @@ def _get_galaxy_positions(self):
raise KeyError(pos_param_err)
galcat.close()

def interpsfex(self, dotpsfpath, pos):
def interpsfex(self, dotpsfpath, pos, offsets=None):
"""Interpolate PSFEx.

Use PSFEx generated model to perform spatial PSF interpolation.
Use PSFEx generated model to perform spatial PSF interpolation, and
return the PSFs as native-pixel stamps whatever the model's sampling
(``PSF_SAMP``); see :func:`native_psf_stamps`.

@sc [decision:star_selection_psf.psf_acceptance_thresholds,label:gate] psf-gate-path-specific-output
Validation logs a rejected gate and skips writing validation output;
Expand All @@ -424,12 +544,14 @@ def interpsfex(self, dotpsfpath, pos):
Path to ``.psf`` file (PSFEx output)
pos : numpy.ndarray
Positions where the PSF model should be evaluated
offsets : numpy.ndarray, optional
``(N, 2)`` sub-pixel ``(dx, dy)`` of each PSF centre from its
stamp centre; ``None`` centres every stamp

Returns
-------
numpy.ndarray
Array of PSFs, each row is the PSF image at the corresponding position
requested
Array of native-pixel PSF stamps, one per requested position

"""
if not os.path.exists(dotpsfpath):
Expand All @@ -454,11 +576,13 @@ def interpsfex(self, dotpsfpath, pos):
return BAD_CHI2

PSF_basis = np.array(PSF_model.data)[0][0]
sampling = PSF_model.header["PSF_SAMP"]
try:
deg = PSF_model.header["POLDEG1"]
except KeyError:
# constant PSF model
return PSF_basis[0, :, :]
PSFs = np.repeat(PSF_basis[:1], len(pos), axis=0)
return native_psf_stamps(PSFs, sampling, offsets)

# scale coordinates
x_interp, x_scale = (
Expand Down Expand Up @@ -505,7 +629,7 @@ def interpsfex(self, dotpsfpath, pos):
]
)

return PSFs
return native_psf_stamps(PSFs, sampling, offsets)

def _interpolate(self):
"""Interpolate.
Expand All @@ -517,8 +641,35 @@ def _interpolate(self):
self.interp_PSFs = self.interpsfex(
self._dotpsf_path,
self.gal_pos,
self._stamp_offsets() if self._stamp_phase else None,
)

def _stamp_offsets(self):
"""Stamp Offsets.

Sub-pixel offsets of the catalogue positions from the centres of the
catalogue's SExtractor ``VIGNET`` stamps, which are what the PSF is
compared with on the single-exposure paths: validation measures star
``VIGNET`` and PSF with HSM side by side, each with the local WCS at
the star's ``XWIN_IMAGE``. Rendered at that offset, the PSF stamp is
the model's image of the star as sampled in its ``VIGNET``, which is
also how PSFEx maps its model onto a training star. The positions must
be 1-indexed image coordinates (``XWIN_IMAGE``, ``YWIN_IMAGE``); see
:func:`sextractor_vignet_offsets`.

Returns
-------
numpy.ndarray
``(N, 2)`` ``(dx, dy)`` offsets in pixels.

"""
galcat = file_io.FITSCatalogue(self._galcat_path, SEx_catalogue=True)
galcat.open()
data = galcat.get_data()
barycentre = np.column_stack((data["X_IMAGE"], data["Y_IMAGE"]))
galcat.close()
return sextractor_vignet_offsets(self.gal_pos, barycentre)

def _get_psfshapes(self, wcs_list=None):
"""Get PSF Shapes.

Expand Down Expand Up @@ -802,6 +953,25 @@ def process_me(self, dot_psf_dirs, dot_psf_pattern, f_wcs_path):

Process multi-epoc PSF interpolation.

With ``stamp_phase``, each epoch's PSF is rendered at the sub-pixel
offset of the object from the centre of that epoch's galaxy stamp.
The vignetmaker cuts the stamp around ``numpy.round`` of the object's
0-indexed exposure position, projected from the same
``POSITION_PARAMS`` through the same exposure WCS
(:func:`shapepipe.modules.vignetmaker_package.vignetmaker.get_stamps`),
so the offset is ``gal_pos - round(gal_pos)``: the vignette's
``OFFSET``, in ``(x, y)`` order. The PSF stamp is then the model's
image of a point source at the object's centroid, sampled on the
galaxy stamp's pixel grid. ngmix metacal turns both stamps into
``InterpolatedImage`` profiles centred on their stamp centres and
divides one by the other, so for a point-like galaxy the
deconvolution is exact at every sampling and the deconvolved galaxy
sits at the stamp centre, a sub-pixel distance from the ngmix
centroid prior, which is placed at the ``OFFSET``
(``centroid_source="wcs"``). With centred PSFs the deconvolved galaxy
sits at the ``OFFSET``, and the aliased high frequencies of an
undersampled PSF do not match the galaxy's.

Parameters
----------
dot_psf_dirs : list
Expand Down Expand Up @@ -951,6 +1121,7 @@ def _interpolate_me(self):
self.interp_PSFs = self.interpsfex(
dot_psf_path,
gal_pos,
gal_pos - np.round(gal_pos) if self._stamp_phase else None,
)

if (
Expand Down
6 changes: 6 additions & 0 deletions src/shapepipe/modules/psfex_interp_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def psfex_interp_runner(
get_shapes = config.getboolean(module_config_sec, "GET_SHAPES")
star_thresh = config.getint(module_config_sec, "STAR_THRESH")
chi2_thresh = config.getint(module_config_sec, "CHI2_THRESH")
stamp_phase = config.has_option(
module_config_sec, "STAMP_PHASE"
) and config.getboolean(module_config_sec, "STAMP_PHASE")

# Run in CLASSIC mode
if mode == "CLASSIC":
Expand All @@ -53,6 +56,7 @@ def psfex_interp_runner(
get_shapes,
star_thresh,
chi2_thresh,
stamp_phase,
)

# Process inputs
Expand Down Expand Up @@ -104,6 +108,7 @@ def psfex_interp_runner(
get_shapes,
star_thresh,
chi2_thresh,
stamp_phase,
)

# Process inputs multi-epoch
Expand All @@ -126,6 +131,7 @@ def psfex_interp_runner(
get_shapes,
star_thresh,
chi2_thresh,
stamp_phase,
)

# Process inputs validation
Expand Down
Loading
Loading