Skip to content

HSM: measure PSF/star shapes directly in sky coords (use_sky_coords)#812

Open
cailmdaley wants to merge 5 commits into
developfrom
feat/hsm-sky-coords
Open

HSM: measure PSF/star shapes directly in sky coords (use_sky_coords)#812
cailmdaley wants to merge 5 commits into
developfrom
feat/hsm-sky-coords

Conversation

@cailmdaley

Copy link
Copy Markdown
Contributor

Closes #775.

ShapePipe measured PSF/star HSM adaptive moments in the pixel frame, then rotated the ellipticities into sky coordinates in a separate post-processing step (scripts/python/convert_psf_pix2world.py, a WCS-Jacobian decomposition). galsim's FindAdaptiveMom now does this at the point of measurement via use_sky_coords=True (available since galsim 2.5; we pin ≥2.8) — per @aguinot's suggestion on #741. This PR moves the WCS knowledge into the HSM call and drops the redundant rotation.

What changed

  • Direct sky-frame measurement. Every PSF/star FindAdaptiveMom call site — PSFEx single-exposure and multi-epoch (psfex_interp.py), MCCD interpolation (mccd_interpolation_script.py, shapepipe_auxiliary_mccd.py) — now attaches the local WCS (linearised at the object's image position) to the stamp and passes use_sky_coords=True. Multi-epoch paths use the per-CCD WCS log already in scope; single-exposure paths read the WCS from the galcat's LDAC_IMHEAD header.
  • Rotation removed where redundant. convert_psf_pix2world.py no longer rotates the in-repo E1/E2/SIGMA_*_HSM shapes; it keeps its remaining jobs (position collation, MCCD CCD assignment, per-patch merge). CANFAR doc updated.
  • Exception kept deliberately: the MCCD fit-validation PSF_MOM_LIST/STAR_MOM_LIST columns come from the external mccd library, which still measures in the pixel frame — that one branch keeps its rotation (with a docstring caveat) until mccd itself adopts use_sky_coords.
  • Equivalence proven in-tree. New tests/module/test_hsm_sky_coords.py: a synthetic Gaussian stamp under a nontrivial local WCS (0.187″/pix, 28° rotation, shear), measured pixel-frame + old transform_shape math vs use_sky_coords=True — g1/g2/sigma agree to rtol 1e-6. All 5 tests pass in the develop container.

Notes for review

  • Sigma unit change (intended): moments_sigma now comes back in world units (arcsec) rather than pixels, so HSM_T_* = 2σ² is arcsec² — which is what the old pipeline produced after the rotation step, so end-of-pipeline catalogues are unchanged. Downstream consumers (merge_starcat, ngmix T-guess) checked: no pixel-scale assumption.
  • No double rotation: grep-verified nothing downstream applies a second Jacobian/getDecomposition to the HSM ellipticities; merge_starcat passes them through.
  • A subtle off-by-one (galsim's FITS 1-indexing vs astropy origin-0 positions) in the local-WCS evaluation point was caught in internal review and fixed (f153ea28).

An independent adversarial review of the full diff ran before opening this PR; both findings it raised (the external-mccd branch above, and the indexing fix) are addressed.

— Claude on behalf of Cail

🤖 Generated with Claude Code

https://claude.ai/code/session_01Gao7Pi5QDBgnTuagV7HT2w

cailmdaley and others added 5 commits July 12, 2026 23:34
Attach the local (Jacobian) WCS at each object position to the PSF/star
stamps and pass use_sky_coords=True to galsim FindAdaptiveMom, so PSFEx and
MCCD HSM ellipticities and sizes come out already in world coordinates. This
makes the downstream WCS-Jacobian shape rotation (convert_psf_pix2world)
redundant.

Threaded at every in-repo HSM PSF/star call site:
- psfex_interp: multi-epoch (per-CCD WCS log), single-exposure validation and
  classic (WCS from the galcat FITS_LDAC image header via _galcat_wcs)
- mccd_interpolation_script: multi-epoch (per-CCD WCS log)
- shapepipe_auxiliary_mccd: interpolation (WCS from galcat LDAC header)

local_wcs_list() builds the per-object local WCS: use_sky_coords evaluates the
stamp WCS at true_center, so it must be the full WCS linearised at the object's
image position, not at the stamp centre.

SIGMA_*_HSM (hence HSM_T_* = 2 sigma^2) is now in sky units (arcsec) rather
than pixels; this is the intended, physically correct size.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gao7Pi5QDBgnTuagV7HT2w
HSM ellipticities and sizes are measured directly in world coordinates
upstream, so the WCS-Jacobian shape rotation (transform_shape,
getDecomposition) is redundant. Remove it from both the PSFEx and MCCD
branches; the script keeps its remaining job of collating positions (X/Y/
RA/DEC), assigning the MCCD focal-plane CCD id, and merging per-exposure
catalogues into per-patch star catalogues. Drops the galsim dependency.

Caveat documented in the module docstring: the MCCD PSF_MOM_LIST/STAR_MOM_LIST
columns come from the external mccd fit-validation code, still measured in the
pixel frame; they are now passed through unrotated. Update the CANFAR doc to
describe the step as collation, not pix->world shape conversion.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gao7Pi5QDBgnTuagV7HT2w
Equivalence test for use_sky_coords=True: draw an elliptical Gaussian on a
stamp with a nontrivial local WCS (rotation + shear + 0.187 arcsec/pix scale,
positive determinant), and assert that measuring in the pixel frame then
applying the removed convert_psf_pix2world.transform_shape rotation (copied in
verbatim as reference) agrees with measuring directly in sky coordinates, to
numerical precision, for e1/e2 and sigma. A second test pins the deliberate
unit change: the sky-frame sigma is the pixel sigma times the WCS scale.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gao7Pi5QDBgnTuagV7HT2w
…ranch

The MCCD PSF_MOM_LIST/STAR_MOM_LIST columns come from the external mccd
fit-validation code (mccd.auxiliary_fun.mccd_validation), which still
measures HSM adaptive moments in the pixel frame. Stripping the WCS-Jacobian
rotation there (71d502f) would have handed MergeStarCatMCCD pixel-frame
ellipticities. Restore transform_shape + the per-CCD getDecomposition rotation
for the MCCD MOM_LIST branch only (and the galsim import it needs); the in-repo
PSFEx and MCCD-interpolation paths stay unrotated, as they now measure in world
coordinates upstream. Update the module docstring caveat: this branch keeps the
rotation until mccd itself adopts use_sky_coords.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gao7Pi5QDBgnTuagV7HT2w
local_wcs_list wraps galsim.AstropyWCS, which follows the 1-indexed FITS pixel
convention, but the multi-epoch paths fed it positions from all_world2pix(...,
0) (origin 0) — evaluating the local Jacobian one pixel off. Compute a separate
origin=1 position array (gal_pos_wcs) for local_wcs_list; the origin-0 gal_pos
stays for the PSF-model interpolation (interpsfex / interp_MCCD), which is its
only other use.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gao7Pi5QDBgnTuagV7HT2w
@martinkilbinger

Copy link
Copy Markdown
Contributor

We should revisit convert_psf_pix2world.py. In v2.0 we will no longer define patches. Renaming or re-scoping this script should reflect its main purpose if it is still needed: to merge the star catalogues. If pixel to WCS is needed for MCCD, this should just be implemented in the MCCD library, we can check in with @tobias-liaudat .

PSF/star ellipticities and sizes are now measured directly in sky coordinates
during PSF interpolation (galsim `FindAdaptiveMom(use_sky_coords=True)`), so
`convert_psf_pix2world.py` only collates and passes the shapes through.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update to reflect removal of the patch concept in v2.0

cailmdaley added a commit that referenced this pull request Jul 16, 2026
Martin (PR #812 review): the patch concept is removed in v2.0. For
-V v2.0 the handler reads summary/missing_job_32_all.txt and
exp_numbers.txt relative to the run root instead of iterating P1..P9;
-p restores an explicit P1..P<n> layout when needed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MqH9wjbJicVmaQdhZ4kBYr
@cailmdaley cailmdaley mentioned this pull request Jul 16, 2026
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HSM: measure PSF/star shapes directly in sky coords (use_sky_coords), drop the WCS re-transform

2 participants