Skip to content

Commit

Permalink
Fix indexing bug in _match_psf_stars
Browse files Browse the repository at this point in the history
Improve test to catch the bug.
Rename variables in the method to make the components more obvious.
  • Loading branch information
parejkoj committed Feb 27, 2024
1 parent e2eec9b commit f65bc68
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
16 changes: 8 additions & 8 deletions python/lsst/pipe/tasks/calibrateImage.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,13 +717,13 @@ def _match_psf_stars(self, psf_stars, stars):
# Closest matches is a dict of psf_stars source ID to Match record
# (psf_stars source, sourceCat source, distance in pixels).
best = {}
for match0, match1, d in matches:
id0 = match0.getId()
match = best.get(id0)
for match_psf, match_stars, d in matches:
match = best.get(match_psf.getId())
if match is None or d <= match[2]:
best[id0] = (match0, match1, d)
best[match_psf.getId()] = (match_psf, match_stars, d)
matches = list(best.values())
ids = np.array([(match0.getId(), match1.getId()) for match0, match1, d in matches]).T
# We'll use this to construct index arrays into each catalog.
ids = np.array([(match_psf.getId(), match_stars.getId()) for match_psf, match_stars, d in matches]).T

# Check that no stars sources are listed twice; we already know
# that each match has a unique psf_stars id, due to using as the key
Expand All @@ -735,11 +735,11 @@ def _match_psf_stars(self, psf_stars, stars):
n_matches, n_unique)

# The indices of the IDs, so we can update the flag fields as arrays.
idx0 = np.searchsorted(psf_stars["id"], ids[0])
idx1 = np.searchsorted(stars["id"], ids[1])
idx_psf_stars = np.searchsorted(psf_stars["id"], ids[0])
idx_stars = np.searchsorted(stars["id"], ids[1])
for field in self.psf_fields:
result = np.zeros(len(stars), dtype=bool)
result[idx0] = psf_stars[field][idx1]
result[idx_stars] = psf_stars[field][idx_psf_stars]
stars[field] = result

def _fit_astrometry(self, exposure, stars):
Expand Down
13 changes: 11 additions & 2 deletions tests/test_calibrateImage.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,19 @@ def test_match_psf_stars(self):
self.assertEqual(stars["calib_psf_used"].sum(), 0)
self.assertEqual(stars["calib_psf_reserved"].sum(), 0)

# Reorder stars to be out of order with psf_stars (putting the sky
# sources in front); this tests that I get the indexing right.
stars.sort(stars.getCentroidSlot().getMeasKey().getX())
stars = stars.copy(deep=True)
# Re-number the ids: the matcher requires sorted ids: this is always
# true in the code itself, but we've permuted them by sorting on
# flux. We don't care what the actual ids themselves are here.
stars["id"] = np.arange(len(stars))

calibrate._match_psf_stars(psf_stars, stars)

# Sort in order of brightness; the psf stars are the 3 brightest, with
# two sky sources as the faintest.
# Check that the three brightest stars have the psf flags transfered
# from the psf_stars catalog by sorting in order of brightness.
stars.sort(stars.getPsfFluxSlot().getMeasKey())
# sort() above leaves the catalog non-contiguous.
stars = stars.copy(deep=True)
Expand Down

0 comments on commit f65bc68

Please sign in to comment.