LSST Applications g1653933729+34a971ddd9,g1a997c3884+34a971ddd9,g2160c40384+da0d0eec6b,g28da252d5a+cb6ff358b6,g2bbee38e9b+e5a1bc5b38,g2bc492864f+e5a1bc5b38,g2ca4be77d2+2af33ed832,g2cdde0e794+704103fe75,g3156d2b45e+6e87dc994a,g347aa1857d+e5a1bc5b38,g35bb328faa+34a971ddd9,g3a166c0a6a+e5a1bc5b38,g3e281a1b8c+8ec26ec694,g4005a62e65+ba0306790b,g414038480c+9f5be647b3,g41af890bb2+f34483f65b,g5065538af8+53fd6e9482,g5a0bb5165c+1b31a0decc,g717e5f8c0f+19a1ad098c,g80478fca09+1acdc24a0d,g8204df1d8d+19a1ad098c,g82479be7b0+44a04e1d61,g858d7b2824+19a1ad098c,g9125e01d80+34a971ddd9,ga5288a1d22+ad6d5da1a4,gae0086650b+34a971ddd9,gb58c049af0+ace264a4f2,gbd397ab92a+d5fd0218dc,gc28159a63d+e5a1bc5b38,gcf0d15dbbd+618cb38075,gd6b7c0dfd1+3388d854ad,gda6a2b7d83+618cb38075,gdaeeff99f8+7774323b41,ge2409df99d+8082276ac0,ge33fd446bb+19a1ad098c,ge79ae78c31+e5a1bc5b38,gf0baf85859+890af219f9,gf5289d68f6+701f7b396a,w.2024.37
LSST Data Management Base Package
Loading...
Searching...
No Matches
Psf.cc
Go to the documentation of this file.
1// -*- LSST-C++ -*-
2#include <limits>
3#include <cmath>
4#include <memory>
5
10
11namespace lsst {
12namespace afw {
13
16
17namespace detection {
18namespace detail {
19
20// Key for caching PSFs with lsst::cpputils::Cache
21//
22// We cache PSFs by their x,y position. Although there are placeholders
23// in the `Psf` class and here for `image::Color`, these are not used
24// in the cache because `image::Color` is not currently well-defined
25// or used.
29
31 : position(position_), color(color_) {}
32
33 bool operator==(PsfCacheKey const &other) const {
34 return position == other.position; // Currently don't care about color
35 }
36
37 friend std::ostream &operator<<(std::ostream &os, PsfCacheKey const &key) { return os << key.position; }
38};
39
40} // namespace detail
41} // namespace detection
42} // namespace afw
43} // namespace lsst
44
45namespace std {
46
47// Template specialisation for hashing PsfCacheKey
48//
49// We currently ignore the color, consistent with operator==.
50template <>
58
59} // namespace std
60
61namespace lsst {
62namespace afw {
63namespace detection {
64
65namespace {
66
67bool isPointNull(lsst::geom::Point2D const &p) { return std::isnan(p.getX()) && std::isnan(p.getY()); }
68
69} // namespace
70
71Psf::Psf(bool isFixed, std::size_t capacity) : _isFixed(isFixed) {
72 _imageCache = std::make_unique<PsfCache>(capacity);
73 _kernelImageCache = std::make_unique<PsfCache>(capacity);
74}
75
76Psf::~Psf() = default;
77
78Psf::Psf(Psf const &other) : Psf(other._isFixed, other.getCacheCapacity()) {}
79
80Psf::Psf(Psf &&other)
81 : _isFixed(other._isFixed),
82 _imageCache(std::move(other._imageCache)),
83 _kernelImageCache(std::move(other._kernelImageCache)) {}
84
86 lsst::geom::Point2D const &position,
87 std::string const &warpAlgorithm,
88 unsigned int warpBuffer) {
89 // "ir" : (integer, residual)
90 std::pair<int, double> const irX = image::positionToIndex(position.getX(), true);
91 std::pair<int, double> const irY = image::positionToIndex(position.getY(), true);
92
93 if (irX.second != 0.0 || irY.second != 0.0) {
94 im = math::offsetImage(*im, irX.second, irY.second, warpAlgorithm, warpBuffer);
95 }
96
97 im->setXY0(irX.first + im->getX0(), irY.first + im->getY0());
98 return im;
99}
100
102 ImageOwnerEnum owner) const {
103 if (isPointNull(position)) position = getAveragePosition();
104 if (color.isIndeterminate()) color = getAverageColor();
105 std::shared_ptr<Psf::Image> result = (*_imageCache)(
106 detail::PsfCacheKey(position, color),
107 [this](detail::PsfCacheKey const &key) { return doComputeImage(key.position, key.color); });
108 if (owner == COPY) {
109 result = std::make_shared<Image>(*result, true);
110 }
111 return result;
112}
113
115 ImageOwnerEnum owner) const {
116 if (_isFixed || isPointNull(position)) position = getAveragePosition();
117 if (_isFixed || color.isIndeterminate()) color = getAverageColor();
118 std::shared_ptr<Psf::Image> result = (*_kernelImageCache)(
119 detail::PsfCacheKey(position, color),
120 [this](detail::PsfCacheKey const &key) { return doComputeKernelImage(key.position, key.color); });
121 if (owner == COPY) {
122 result = std::make_shared<Image>(*result, true);
123 }
124 return result;
125}
126
128 if (isPointNull(position)) position = getAveragePosition();
129 if (color.isIndeterminate()) color = getAverageColor();
130 auto cached_image = _kernelImageCache->get(detail::PsfCacheKey(position, color));
131 if (cached_image.has_value()) {
132 return cached_image.value()->getBBox();
133 } else {
134 return doComputeBBox(position, color);
135 }
136}
137
139 if (isPointNull(position)) position = getAveragePosition();
140 if (color.isIndeterminate()) color = getAverageColor();
141 auto cached_image = _imageCache->get(detail::PsfCacheKey(position, color));
142 if (cached_image.has_value()) {
143 return cached_image.value()->getBBox();
144 } else {
145 return doComputeImageBBox(position, color);
146 }
147}
148
150 image::Color color) const {
151 if (isPointNull(position)) position = getAveragePosition();
152 if (color.isIndeterminate()) color = getAverageColor();
153 // FixedKernel ctor will deep copy image, so we can use INTERNAL.
155 return std::make_shared<math::FixedKernel>(*image);
156}
157
158double Psf::computePeak(lsst::geom::Point2D position, image::Color color) const {
159 if (isPointNull(position)) position = getAveragePosition();
160 if (color.isIndeterminate()) color = getAverageColor();
162 return (*image)(-image->getX0(), -image->getY0());
163}
164
165double Psf::computeApertureFlux(double radius, lsst::geom::Point2D position, image::Color color) const {
166 if (isPointNull(position)) position = getAveragePosition();
167 if (color.isIndeterminate()) color = getAverageColor();
168 return doComputeApertureFlux(radius, position, color);
169}
170
172 if (isPointNull(position)) position = getAveragePosition();
173 if (color.isIndeterminate()) color = getAverageColor();
174 return doComputeShape(position, color);
175}
176
178 image::Color const &color) const {
180 return recenterKernelImage(im, position);
181}
182
184 image::Color const& color) const {
186 return im->getBBox();
187}
188
190
191std::size_t Psf::getCacheCapacity() const { return _kernelImageCache->capacity(); }
192
194 _imageCache->reserve(capacity);
195 _kernelImageCache->reserve(capacity);
196}
197
198} // namespace detection
199} // namespace afw
200} // namespace lsst
py::object result
Definition _schema.cc:429
A polymorphic base class for representing an image's Point Spread Function.
Definition Psf.h:82
static std::shared_ptr< Image > recenterKernelImage(std::shared_ptr< Image > im, lsst::geom::Point2D const &position, std::string const &warpAlgorithm="lanczos5", unsigned int warpBuffer=5)
Helper function for Psf::doComputeImage(): converts a kernel image (centered at (0,...
Definition Psf.cc:85
lsst::geom::Box2I computeBBox(lsst::geom::Point2D position, image::Color color=image::Color()) const
Return the bounding box of the image returned by computeKernelImage()
Definition Psf.cc:127
virtual std::shared_ptr< Image > doComputeImage(lsst::geom::Point2D const &position, image::Color const &color) const
These virtual members are protected (rather than private) so that python-implemented derived classes ...
Definition Psf.cc:177
std::shared_ptr< Image > computeKernelImage(lsst::geom::Point2D position, image::Color color=image::Color(), ImageOwnerEnum owner=COPY) const
Return an Image of the PSF, in a form suitable for convolution.
Definition Psf.cc:114
virtual std::shared_ptr< Image > doComputeKernelImage(lsst::geom::Point2D const &position, image::Color const &color) const =0
These virtual member functions are private, not protected, because we only want derived classes to im...
std::size_t getCacheCapacity() const
Return the capacity of the caches.
Definition Psf.cc:191
double computeApertureFlux(double radius, lsst::geom::Point2D position, image::Color color=image::Color()) const
Compute the "flux" of the Psf model within a circular aperture of the given radius.
Definition Psf.cc:165
Psf(Psf const &)
Definition Psf.cc:78
std::shared_ptr< Image > computeImage(lsst::geom::Point2D position, image::Color color=image::Color(), ImageOwnerEnum owner=COPY) const
Return an Image of the PSF, in a form that can be compared directly with star images.
Definition Psf.cc:101
double computePeak(lsst::geom::Point2D position, image::Color color=image::Color()) const
Return the peak value of the PSF image.
Definition Psf.cc:158
image::Color getAverageColor() const
Return the average Color of the stars used to construct the Psf.
Definition Psf.h:245
virtual lsst::geom::Box2I doComputeImageBBox(lsst::geom::Point2D const &position, image::Color const &color) const
Definition Psf.cc:183
ImageOwnerEnum
Enum passed to computeImage and computeKernelImage to determine image ownership.
Definition Psf.h:92
@ COPY
The image will be copied before returning; caller will own it.
Definition Psf.h:93
@ INTERNAL
An internal image will be returned without copying.
Definition Psf.h:94
virtual lsst::geom::Box2I doComputeBBox(lsst::geom::Point2D const &position, image::Color const &color) const =0
void setCacheCapacity(std::size_t capacity)
Set the capacity of the caches.
Definition Psf.cc:193
std::shared_ptr< math::Kernel const > getLocalKernel(lsst::geom::Point2D position, image::Color color=image::Color()) const
Return a FixedKernel corresponding to the Psf image at the given point.
Definition Psf.cc:149
virtual double doComputeApertureFlux(double radius, lsst::geom::Point2D const &position, image::Color const &color) const =0
virtual lsst::geom::Point2D getAveragePosition() const
Return the average position of the stars used to construct the Psf.
Definition Psf.cc:189
virtual geom::ellipses::Quadrupole doComputeShape(lsst::geom::Point2D const &position, image::Color const &color) const =0
geom::ellipses::Quadrupole computeShape(lsst::geom::Point2D position, image::Color color=image::Color()) const
Compute the ellipse corresponding to the second moments of the Psf.
Definition Psf.cc:171
lsst::geom::Box2I computeImageBBox(lsst::geom::Point2D position, image::Color color=image::Color()) const
Return the bounding box of the image returned by computeImage()
Definition Psf.cc:138
An ellipse core with quadrupole moments as parameters.
Definition Quadrupole.h:47
Describe the colour of a source.
Definition Color.h:25
bool isIndeterminate() const noexcept
Whether the color is the special value that indicates that it is unspecified.
Definition Color.h:36
static std::shared_ptr< T > dynamicCast(std::shared_ptr< Persistable > const &ptr)
Dynamically cast a shared_ptr.
An integer coordinate rectangle.
Definition Box.h:55
T isnan(T... args)
int positionToIndex(double pos)
Convert image position to nearest integer index.
Definition ImageUtils.h:69
std::shared_ptr< ImageT > offsetImage(ImageT const &image, float dx, float dy, std::string const &algorithmName="lanczos5", unsigned int buffer=0)
Return an image offset by (dx, dy) using the specified algorithm.
Point< double, 2 > Point2D
Definition Point.h:324
STL namespace.
decltype(sizeof(void *)) size_t
Definition doctest.h:524
lsst::geom::Point2D const position
Definition Psf.cc:27
bool operator==(PsfCacheKey const &other) const
Definition Psf.cc:33
PsfCacheKey(lsst::geom::Point2D const &position_, image::Color color_=image::Color())
Definition Psf.cc:30
friend std::ostream & operator<<(std::ostream &os, PsfCacheKey const &key)
Definition Psf.cc:37
std::size_t operator()(lsst::afw::detection::detail::PsfCacheKey const &key) const noexcept
Definition Psf.cc:54