Skip to content

Commit

Permalink
Update changes
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamJamieson committed Feb 12, 2025
1 parent d602a1c commit 97412fd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 119 deletions.
1 change: 1 addition & 0 deletions changes/310.general.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Move common parts of skymatch shared by both jwst and romancal into stcal.
126 changes: 14 additions & 112 deletions src/stcal/skymatch/skyimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@ def calc_bounding_polygon(self, stepsize=None):
of the image are included automatically. If `stepsize` is `None`,
bounding polygon will contain only vertices of the image.
Notes
-----
The bounding polygon is defined from corners of pixels whereas the pixel
coordinates refer to their centers and therefore the lower-left corner
is located at (-0.5, -0.5)
"""
ny, nx = self.image_shape

Expand Down Expand Up @@ -451,14 +456,6 @@ def set_builtin_skystat(self, skystat='median', lower=None, upper=None,
binwidth=binwidth
)

# TODO: due to a bug in the sphere package, see
# https://github.com/spacetelescope/sphere/issues/74
# intersections with polygons formed as union does not work.
# For this reason I re-implement 'calc_sky' below with
# a workaround for the bug.
# The original implementation (now called ``_calc_sky_orig``
# should replace current 'calc_sky' once the bug is fixed.
#
def calc_sky(self, overlap=None, delta=True):
"""
Compute sky background value.
Expand Down Expand Up @@ -494,7 +491,15 @@ def calc_sky(self, overlap=None, delta=True):
polyarea : float
Area (in srad) of the polygon that bounds data used to compute
sky statistics.
Notes
-----
Due to a bug in the sphere package, see
https://github.com/spacetelescope/sphere/issues/74
intersections with polygons formed as union does not work.
For this reason I re-implement 'calc_sky' below with a workaround for
the bug. The original implementation should be used when the bug is
fixed.
"""
if overlap is None:

Expand Down Expand Up @@ -582,109 +587,6 @@ def calc_sky(self, overlap=None, delta=True):

return skyval, npix, polyarea

# def _calc_sky_orig(self, overlap=None, delta=True):
# """
# Compute sky background value.
#
# Parameters
# ----------
# overlap : SkyImage, SkyGroup, SphericalPolygon, list of tuples, \
# None, optional
# Another `SkyImage`, `SkyGroup`,
# :py:class:`spherical_geometry.polygons.SphericalPolygon`, or
# a list of tuples of (RA, DEC) of vertices of a spherical
# polygon. This parameter is used to indicate that sky statistics
# should computed only in the region of intersection of *this*
# image with the polygon indicated by `overlap`. When `overlap` is
# `None`, sky statistics will be computed over the entire image.
#
# delta : bool, optional
# Should this function return absolute sky value or the difference
# between the computed value and the value of the sky stored in the
# `sky` property.
#
# Returns
# -------
# skyval : float, None
# Computed sky value (absolute or relative to the `sky` attribute).
# If there are no valid data to perform this computations (e.g.,
# because this image does not overlap with the image indicated by
# `overlap`), `skyval` will be set to `None`.
#
# npix : int
# Number of pixels used to compute sky statistics.
#
# polyarea : float
# Area (in srad) of the polygon that bounds data used to compute
# sky statistics.
#
# """
#
# if overlap is None:
#
# if self._mask is None:
# data = self.image
# else:
# data = self.image[self._mask.get_data()]
#
# polyarea = self.poly_area
#
# else:
# fill_mask = np.zeros(self.image_shape, dtype=bool)
#
# if isinstance(overlap, (SkyImage, SkyGroup, SphericalPolygon)):
# intersection = self.intersection(overlap)
# polyarea = np.fabs(intersection.area())
# radec = intersection.to_radec()
#
# else: # assume a list of (ra, dec) tuples:
# radec = []
# polyarea = 0.0
# for r, d in overlap:
# poly = SphericalPolygon.from_radec(r, d)
# polyarea1 = np.fabs(poly.area())
# if polyarea1 == 0.0 or len(r) < 4:
# continue
# polyarea += polyarea1
# radec.append(self.intersection(poly).to_radec())
#
# if polyarea == 0.0:
# return None, 0, 0.0
#
# for ra, dec in radec:
# if len(ra) < 4:
# continue
#
# # set pixels in 'fill_mask' that are inside a polygon
# # to True:
# x, y = self.wcs_inv(ra, dec)
# poly_vert = list(zip(*[x, y]))
#
# polygon = region.Polygon(True, poly_vert)
# fill_mask = polygon.scan(fill_mask)
#
# if self._mask is not None:
# fill_mask &= self._mask.get_data()
#
# data = self.image[fill_mask]
#
# if data.size < 1:
# return None, 0, 0.0
#
# # Calculate sky
# try:
#
# skyval, npix = self._skystat(data)
#
# except ValueError:
#
# return None, 0, 0.0
#
# if delta:
# skyval -= self._sky
#
# return skyval, npix, polyarea

def copy(self):
"""
Return a shallow copy of the `SkyImage` object.
Expand Down
13 changes: 6 additions & 7 deletions src/stcal/skymatch/skystatistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
__all__ = ['SkyStats']


class SkyStats():
class SkyStats:
"""
This is a superclass build on top of
:py:class:`stsci.imagestats.ImageStats`. Compared to
:py:class:`stsci.imagestats.ImageStats`, `SkyStats` has
"persistent settings" in the sense that object's parameters need to be
set once and these settings will be applied to all subsequent
computations on different data.
This class is built on top of :py:class:`stsci.imagestats.ImageStats`,
deligating its functionality to calls to the ``ImageStats`` object. Compared
to :py:class:`stsci.imagestats.ImageStats`, `SkyStats` has "persistent settings"
in the sense that object's parameters need to be set once and these settings
will be applied to all subsequent computations on different data.
"""

Expand Down

0 comments on commit 97412fd

Please sign in to comment.