Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-28570: Take both width and height for cutouts #461

Merged
merged 2 commits into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 11 additions & 10 deletions python/lsst/pipe/tasks/calexpCutout.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class CalexpCutoutTaskConfig(pipeBase.PipelineTaskConfig,

class CalexpCutoutTask(pipeBase.PipelineTask):
"""Task for computing cutouts on a specific calexp given
positions and sizes of the stamps.
positions, xspans, and yspans of the stamps.
"""
ConfigClass = CalexpCutoutTaskConfig
_DefaultName = "calexpCutoutTask"
Expand All @@ -56,10 +56,9 @@ def run(self, in_table, calexp):
Parameters
----------
in_table : `astropy.QTable`
A table containing at least the following columns: position, size.
The position should be an `astropy.SkyCoord`. The size is
the size of the cutout in pixels. All cutouts are square in pixel
space.
A table containing at least the following columns: position, xspan
and yspan. The position should be an `astropy.SkyCoord`. The
xspan and yspan are the extent of the cutout in x and y in pixels.
calexp : `lsst.afw.image.ExposureF`
The calibrated exposure from which to extract cutouts

Expand All @@ -85,9 +84,10 @@ def run(self, in_table, calexp):
If the input catalog doesn't have the required columns,
a ValueError is raised
"""
if 'position' not in in_table.colnames or 'size' not in in_table.colnames:
cols = in_table.colnames
if 'position' not in cols or 'xspan' not in cols or 'yspan' not in cols:
raise ValueError('Required column missing from the input table. '
'Required columns are "position" and "size".'
'Required columns are "position", "xspan", and "yspan". '
f'The column names are: {in_table.colnames}')
max_idx = self.config.max_cutouts
cutout_list = []
Expand All @@ -104,10 +104,11 @@ def run(self, in_table, calexp):
pt = geom.SpherePoint(geom.Angle(ra, geom.degrees),
geom.Angle(dec, geom.degrees))
pix = wcs.skyToPixel(pt)
size = rec['size'].value
xspan = rec['xspan'].value
yspan = rec['yspan'].value
# Clamp to LL corner of the LL pixel and draw extent from there
box = geom.Box2I(geom.Point2I(int(pix.x-size/2), int(pix.y-size/2)),
geom.Extent2I(size, size))
box = geom.Box2I(geom.Point2I(int(pix.x-xspan/2), int(pix.y-yspan/2)),
geom.Extent2I(xspan, yspan))
if not mim.getBBox().contains(box):
if not self.config.skip_bad:
raise ValueError(f'Cutout bounding box is not completely contained in the image: {box}')
Expand Down
31 changes: 22 additions & 9 deletions tests/test_calexpCutout.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,38 @@ def make_data(bbox, wcs, border=100, ngood=13, nbad=7, nedge=3):

ident = []
pt = []
size = []
xspan = []
yspan = []
for i in range(ngood):
x = random.random()*(dx - 2*border) + border
y = random.random()*(dy - 2*border) + border
sphpt = wcs.pixelToSky(x, y)
pt.append(SkyCoord(sphpt.getRa().asDegrees(), sphpt.getDec().asDegrees(),
frame='icrs', unit=u.deg))
ident.append((i+1)*u.dimensionless_unscaled)
size.append(random.randint(13, 26)*u.dimensionless_unscaled)
data['good'] = QTable([ident, pt, size], names=['id', 'position', 'size'])
xspan.append(random.randint(13, 26)*u.dimensionless_unscaled)
yspan.append(random.randint(13, 26)*u.dimensionless_unscaled)
data['good'] = QTable([ident, pt, xspan, yspan], names=['id', 'position', 'xspan', 'yspan'])

ident = []
pt = []
size = []
xspan = []
yspan = []
for i in range(nbad):
x = random.random()*(dx - 2*border) + border
y = random.random()*(dy - 2*border) + border
sphpt = wcs.pixelToSky(-x, -y)
pt.append(SkyCoord(sphpt.getRa().asDegrees(), sphpt.getDec().asDegrees(),
frame='icrs', unit=u.deg))
ident.append((i+1)*u.dimensionless_unscaled)
size.append(random.randint(13, 26)*u.dimensionless_unscaled)
data['bad'] = QTable([ident, pt, size], names=['id', 'position', 'size'])
xspan.append(random.randint(13, 26)*u.dimensionless_unscaled)
yspan.append(random.randint(13, 26)*u.dimensionless_unscaled)
data['bad'] = QTable([ident, pt, xspan, yspan], names=['id', 'position', 'xspan', 'yspan'])

ident = []
pt = []
size = []
xspan = []
yspan = []
for i in range(nedge):
x_or_y = random.randint(0, 1)
if x_or_y:
Expand All @@ -87,8 +92,9 @@ def make_data(bbox, wcs, border=100, ngood=13, nbad=7, nedge=3):
pt.append(SkyCoord(sphpt.getRa().asDegrees(), sphpt.getDec().asDegrees(),
frame='icrs', unit=u.deg))
ident.append((i+1)*u.dimensionless_unscaled)
size.append(random.randint(13, 26)*u.dimensionless_unscaled)
data['edge'] = QTable([ident, pt, size], names=['id', 'position', 'size'])
xspan.append(random.randint(13, 26)*u.dimensionless_unscaled)
yspan.append(random.randint(13, 26)*u.dimensionless_unscaled)
data['edge'] = QTable([ident, pt, xspan, yspan], names=['id', 'position', 'xspan', 'yspan'])
return data


Expand All @@ -106,12 +112,19 @@ def testCalexpCutout(self):
task = CalexpCutoutTask(config=config)
result = task.run(self.data['good'], self.exp)
self.assertEqual(len(result.cutouts), len(self.data['good']))
indims = [(x, y) for x, y in zip(self.data['good']['xspan'], self.data['good']['yspan'])]
outdims = [tuple(el.stamp_im.getDimensions()) for el in result.cutouts]
self.assertEqual(indims, outdims)

# Test configuration of the max number of cutouts
config.max_cutouts = 4
task = CalexpCutoutTask(config=config)
result = task.run(self.data['good'], self.exp)
self.assertEqual(len(result.cutouts), task.config.max_cutouts)
indims = [(x, y) for x, y in zip(self.data['good']['xspan'][:config.max_cutouts],
self.data['good']['yspan'][:config.max_cutouts])]
outdims = [tuple(el.stamp_im.getDimensions()) for el in result.cutouts[:config.max_cutouts]]
self.assertEqual(indims, outdims)

def testEdge(self):
# Currently edge cutouts are handled the same way
Expand Down