Issue with Convexpolygon.contains method in insertFakes.py

I am using LSST pipeline v21.0.0. In insertFakes.py the fakeCatalog that needs to be inserted is trimmed based on the following code in the function trimFakeCat:

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

        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
        wcs : `lsst.afw.geom.SkyWcs`
                    WCS to use to add fake sources

        Returns
        -------
        fakeCat : `pandas.core.frame.DataFrame`
                    The original fakeCat trimmed to the area of the image
        """

        bbox = Box2D(image.getBBox())
        corners = bbox.getCorners()

        skyCorners = wcs.pixelToSky(corners)
        region = ConvexPolygon([s.getVector() for s in skyCorners])

        def trim(row):
            coord = SpherePoint(row[self.config.raColName], row[self.config.decColName], radians)
            return region.contains(coord.getVector())

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

I have found that this particular code sometimes misses objects near the edge of the CCD (see the image below).

Screenshot from 2021-08-13 23-48-04

Here the convexPolygon region is shown based on the ds9 regions checking the corners as computed by the above code. The green circle seems clearly inside this polygon, but is returned as False by region.contains(coord.getVector())

Is this expected behaviour?

If I compute the x, y pixel values using a simple xp, yp = wcs.skyToPixelArray(ra, dec) and find the bounding box using

bbox = butler.get("calexp_bbox", dataId)
xmax, ymax = bbox.getMax()

I find that xp lies between [0, xmax ] and yp lies between [0, ymax].

This is confusing, and was leading to some of my fake insertions not being put in the catalog. Has anyone experienced this before?

I now see that there is a change in the newest version of insertFakes.py which implies that this problem with bounding box trimming is now fixed in the newest version of the pipeline. As mentioned in my original post, it is indeed done by comparing xp, yp to xmax, ymax.

The solution: Use latest version of the pipeline. :slight_smile: