Skip to content

Commit

Permalink
Merge branch 'tickets/DM-31388'
Browse files Browse the repository at this point in the history
  • Loading branch information
mrawls committed Mar 4, 2022
2 parents 84bf843 + 4b9ca29 commit e01ce96
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 22 deletions.
23 changes: 16 additions & 7 deletions python/lsst/pipe/tasks/insertFakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import galsim
from astropy.table import Table
import numpy as np
from astropy import units as u

import lsst.geom as geom
import lsst.afw.image as afwImage
Expand Down Expand Up @@ -1022,7 +1023,7 @@ def addPixCoords(self, fakeCat, image):
return fakeCat

def trimFakeCat(self, fakeCat, image):
"""Trim the fake cat to about the size of the input image.
"""Trim the fake cat to the size of the input image plus trimBuffer padding.
`fakeCat` must be processed with addPixCoords before using this method.
Expand All @@ -1038,17 +1039,25 @@ def trimFakeCat(self, fakeCat, image):
fakeCat : `pandas.core.frame.DataFrame`
The original fakeCat trimmed to the area of the image
"""
wideBbox = Box2D(image.getBBox()).dilatedBy(self.config.trimBuffer)

bbox = Box2D(image.getBBox()).dilatedBy(self.config.trimBuffer)
# prefilter in ra/dec to avoid cases where the wcs incorrectly maps
# input fakes which are really off the chip onto it.
ras = fakeCat[self.config.ra_col].values * u.rad
decs = fakeCat[self.config.dec_col].values * u.rad

isContainedRaDec = image.containsSkyCoords(ras, decs, padding=self.config.trimBuffer)

# also filter on the image BBox in pixel space
xs = fakeCat["x"].values
ys = fakeCat["y"].values

isContained = xs >= bbox.minX
isContained &= xs <= bbox.maxX
isContained &= ys >= bbox.minY
isContained &= ys <= bbox.maxY
isContainedXy = xs >= wideBbox.minX
isContainedXy &= xs <= wideBbox.maxX
isContainedXy &= ys >= wideBbox.minY
isContainedXy &= ys <= wideBbox.maxY

return fakeCat[isContained]
return fakeCat[isContainedRaDec & isContainedXy]

def mkFakeGalsimGalaxies(self, fakeCat, band, photoCalib, pixelScale, psf, image):
"""Make images of fake galaxies using GalSim.
Expand Down
68 changes: 53 additions & 15 deletions python/lsst/pipe/tasks/matchFakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import pandas as pd
from scipy.spatial import cKDTree

from lsst.geom import Box2D, radians, SpherePoint
from lsst.geom import Box2D
import lsst.pex.config as pexConfig
from lsst.pipe.base import PipelineTask, PipelineTaskConnections, Struct
import lsst.pipe.base.connectionTypes as connTypes
Expand Down Expand Up @@ -101,6 +101,13 @@ class MatchFakesConfig(
doc="Match visit to trim the fakeCat"
)

trimBuffer = pexConfig.Field(
doc="Size of the pixel buffer surrounding the image. Only those fake sources with a centroid"
"falling within the image+buffer region will be considered matches.",
dtype=int,
default=100,
)


class MatchFakesTask(PipelineTask):
"""Match a pre-existing catalog of fakes to a catalog of detections on
Expand Down Expand Up @@ -240,35 +247,66 @@ def getVisitMatchedFakeCat(self, fakeCat, exposure):

return fakeCat[selected]

def _trimFakeCat(self, fakeCat, image):
"""Trim the fake cat to about the size of the input image.
def _addPixCoords(self, fakeCat, image):

"""Add pixel coordinates to the catalog of fakes.
Parameters
----------
fakeCat : `pandas.core.frame.DataFrame`
The catalog of fake sources to be input
image : `lsst.afw.image.exposure.exposure.ExposureF`
The image into which the fake sources should be added
skyMap : `lsst.skymap.SkyMap`
SkyMap defining the tracts and patches the fakes are stored over.
Returns
-------
fakeCat : `pandas.core.frame.DataFrame`
"""
wcs = image.getWcs()
ras = fakeCat[self.config.ra_col].values
decs = fakeCat[self.config.dec_col].values
xs, ys = wcs.skyToPixelArray(ras, decs)
fakeCat["x"] = xs
fakeCat["y"] = ys

return fakeCat

def _trimFakeCat(self, fakeCat, image):
"""Trim the fake cat to the exact size of the input image.
Parameters
----------
fakeCat : `pandas.core.frame.DataFrame`
The catalog of fake sources that was input
image : `lsst.afw.image.exposure.exposure.ExposureF`
The image into which the fake sources were added
Returns
-------
fakeCats : `pandas.core.frame.DataFrame`
fakeCat : `pandas.core.frame.DataFrame`
The original fakeCat trimmed to the area of the image
"""
wcs = image.getWcs()

bbox = Box2D(image.getBBox())
# fakeCat must be processed with _addPixCoords before trimming
if ('x' not in fakeCat.columns) or ('y' not in fakeCat.columns):
fakeCat = self._addPixCoords(fakeCat, image)

# Prefilter in ra/dec to avoid cases where the wcs incorrectly maps
# input fakes which are really off the chip onto it.
ras = fakeCat[self.config.ra_col].values * u.rad
decs = fakeCat[self.config.dec_col].values * u.rad

def trim(row):
coord = SpherePoint(row[self.config.ra_col],
row[self.config.dec_col],
radians)
cent = wcs.skyToPixel(coord)
return bbox.contains(cent)
isContainedRaDec = image.containsSkyCoords(ras, decs, padding=0)

# now use the exact pixel BBox to filter to only fakes that were inserted
xs = fakeCat["x"].values
ys = fakeCat["y"].values

bbox = Box2D(image.getBBox())
isContainedXy = xs >= bbox.minX
isContainedXy &= xs <= bbox.maxX
isContainedXy &= ys >= bbox.minY
isContainedXy &= ys <= bbox.maxY

return fakeCat[fakeCat.apply(trim, axis=1)]
return fakeCat[isContainedRaDec & isContainedXy]

def _getVectors(self, ras, decs):
"""Convert ra dec to unit vectors on the sphere.
Expand Down

0 comments on commit e01ce96

Please sign in to comment.