Skip to content

Commit

Permalink
Add Psf::computeImageBBox and Psf::computeKernelBBox
Browse files Browse the repository at this point in the history
These are synonyms for Psf::computeImage::getBBox and
Psf::computeKernelImage::getBBox, but may be overridden
for speed by Psf subclasses.
  • Loading branch information
jmeyers314 committed Jun 14, 2021
1 parent f96c1f0 commit f3960e2
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 3 deletions.
27 changes: 24 additions & 3 deletions include/lsst/afw/detection/Psf.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,20 @@ class Psf : public afw::table::io::PersistableFacade<Psf>,
lsst::geom::Box2I computeBBox(lsst::geom::Point2D position = makeNullPoint(),
image::Color color = image::Color()) const;

/**
* Return the bounding box of the image returned by computeImage()
*/
lsst::geom::Box2I computeImageBBox(lsst::geom::Point2D position = makeNullPoint(),
image::Color color = image::Color()) const;

/**
* Alias for computeBBox
*/
lsst::geom::Box2I computeKernelBBox(lsst::geom::Point2D position = makeNullPoint(),
image::Color color = image::Color()) const {
return computeBBox(position, color);
}

/**
* Helper function for Psf::doComputeImage(): converts a kernel image (centered at (0,0) when xy0
* is taken into account) to an image centered at position when xy0 is taken into account.
Expand Down Expand Up @@ -290,13 +304,20 @@ class Psf : public afw::table::io::PersistableFacade<Psf>,
*/
explicit Psf(bool isFixed = false, std::size_t capacity = 100);

//@{
/**
* This virtual member is protected (rather than private) so that python-implemented derived
* classes may opt to use the default implementation. C++ derived classes may override this
* method, but should not call it.
* These virtual members are protected (rather than private) so that python-implemented derived
* classes may opt to use the default implementations. C++ derived classes may override these
* methods, but should not call them. Derived classes should call the corresponding compute*
* member functions instead so as to let the Psf base class handle caching properly.
*
* Derived classes are responsible for ensuring that returned images sum to one.
*/
virtual std::shared_ptr<Image> doComputeImage(lsst::geom::Point2D const& position,
image::Color const& color) const;
virtual lsst::geom::Box2I doComputeImageBBox(lsst::geom::Point2D const& position,
image::Color const& color) const;
//@}

private:
//@{
Expand Down
9 changes: 9 additions & 0 deletions include/lsst/afw/detection/python.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ class PsfTrampoline : public StorableHelper<Base> {
);
}

lsst::geom::Box2I doComputeImageBBox(
lsst::geom::Point2D const& position,
image::Color const& color
) const override {
PYBIND11_OVERLOAD_NAME(
lsst::geom::Box2I, Base, "_doComputeImageBBox", doComputeImageBBox, position, color
);
}

std::shared_ptr<Image> doComputeKernelImage(
lsst::geom::Point2D const& position,
image::Color const& color
Expand Down
4 changes: 4 additions & 0 deletions python/lsst/afw/detection/_psf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ void wrapPsf(utils::python::WrapperCollection& wrappers) {
"color"_a = image::Color());
cls.def("computeBBox", &Psf::computeBBox, "position"_a = NullPoint,
"color"_a = image::Color());
cls.def("computeImageBBox", &Psf::computeImageBBox, "position"_a = NullPoint,
"color"_a = image::Color());
cls.def("computeKernelBBox", &Psf::computeKernelBBox, "position"_a = NullPoint,
"color"_a = image::Color());
cls.def("getLocalKernel", &Psf::getLocalKernel, "position"_a = NullPoint,
"color"_a = image::Color());
cls.def("getAverageColor", &Psf::getAverageColor);
Expand Down
12 changes: 12 additions & 0 deletions src/detection/Psf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ lsst::geom::Box2I Psf::computeBBox(lsst::geom::Point2D position, image::Color co
return doComputeBBox(position, color);
}

lsst::geom::Box2I Psf::computeImageBBox(lsst::geom::Point2D position, image::Color color) const {
if (isPointNull(position)) position = getAveragePosition();
if (color.isIndeterminate()) color = getAverageColor();
return doComputeImageBBox(position, color);
}

std::shared_ptr<math::Kernel const> Psf::getLocalKernel(lsst::geom::Point2D position,
image::Color color) const {
if (isPointNull(position)) position = getAveragePosition();
Expand Down Expand Up @@ -165,6 +171,12 @@ std::shared_ptr<Psf::Image> Psf::doComputeImage(lsst::geom::Point2D const &posit
return recenterKernelImage(im, position);
}

lsst::geom::Box2I Psf::doComputeImageBBox(lsst::geom::Point2D const& position,
image::Color const& color) const {
std::shared_ptr<Psf::Image> im = computeImage(position, color, INTERNAL);
return im->getBBox();
}

lsst::geom::Point2D Psf::getAveragePosition() const { return lsst::geom::Point2D(); }

std::size_t Psf::getCacheCapacity() const { return _kernelImageCache->capacity(); }
Expand Down
3 changes: 3 additions & 0 deletions tests/test_gaussianPsf.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ def testBBox(self):
self.assertEqual(self.psf.computeKernelImage(lsst.geom.Point2D(0.0, 0.0)).getBBox(),
self.psf.computeBBox(lsst.geom.Point2D(0.0, 0.0)))

self.assertEqual(self.psf.computeImage().getBBox(),
self.psf.computeImageBBox())

def testResized(self):
for pad in [0, -10, 10]:
newLen = self.kernelSize - pad
Expand Down
16 changes: 16 additions & 0 deletions tests/test_psf_trampoline.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ def testBBox(self):
pgp.computeBBox(),
gp.computeBBox()
)
self.assertEqual(
pgp.computeKernelBBox(),
gp.computeKernelBBox(),
)
self.assertEqual(
pgp.computeImageBBox(),
gp.computeImageBBox(),
)
self.assertEqual(
pgp.computeImage().getBBox(),
pgp.computeImageBBox()
)
self.assertEqual(
pgp.computeKernelImage().getBBox(),
pgp.computeKernelBBox()
)

def testShape(self):
for pgp, gp in zip(self.pgps, self.gps):
Expand Down

0 comments on commit f3960e2

Please sign in to comment.