Skip to content

Commit

Permalink
Filter catalog sources that are outside of the bounding box (#947)
Browse files Browse the repository at this point in the history
  • Loading branch information
schlafly authored Oct 26, 2023
2 parents 7381bfc + f9497a0 commit 34194f8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ resample
--------
- Implement resampling step. [#787]

tweakreg
--------

- Fix a bug due to which source catalog may contain sources
outside of the bounding box. [#947]


0.12.0 (2023-08-18)
===================
Expand Down
35 changes: 29 additions & 6 deletions romancal/tweakreg/tweakreg_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,6 @@ def process(self, input):
"Please either run SourceDetectionStep or provide a"
"custom source catalog."
)
# set meta.tweakreg_catalog
image_model.meta["tweakreg_catalog"] = catalog
# remove 4D numpy array from meta.source_detection
if is_tweakreg_catalog_present:
del image_model.meta.source_detection["tweakreg_catalog"]
Expand All @@ -214,16 +212,41 @@ def process(self, input):
"'xcentroid' and 'ycentroid'."
)

filename = image_model.meta.filename

# filter out sources outside the WCS bounding box
bb = image_model.meta.wcs.bounding_box
if bb is not None:
x = catalog["x"]
y = catalog["y"]
if bb is None:
r, d = image_model.meta.wcs(x, y)
mask = np.isfinite(r) & np.isfinite(d)
catalog = catalog[mask]

n_removed_src = np.sum(np.logical_not(mask))
if n_removed_src:
self.log.info(
f"Removed {n_removed_src} sources from {filename}'s "
"catalog whose image coordinates could not be "
"converted to world coordinates."
)
else:
# assume image coordinates of all sources within a bounding box
# can be converted to world coordinates.
((xmin, xmax), (ymin, ymax)) = bb
x = catalog["x"]
y = catalog["y"]
mask = (x > xmin) & (x < xmax) & (y > ymin) & (y < ymax)
catalog = catalog[mask]

filename = image_model.meta.filename
n_removed_src = np.sum(np.logical_not(mask))
if n_removed_src:
self.log.info(
f"Removed {n_removed_src} sources from {filename}'s "
"catalog that were outside of the bounding box."
)

# set meta.tweakreg_catalog
image_model.meta["tweakreg_catalog"] = catalog

nsources = len(catalog)
if nsources == 0:
self.log.warning(f"No sources found in {filename}.")
Expand Down

0 comments on commit 34194f8

Please sign in to comment.