lsst.afw  22.0.1-31-gd62ef0f05+23bd69c089
Psf.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 #include <limits>
3 #include <typeinfo>
4 #include <cmath>
5 #include <memory>
6 
7 #include "lsst/utils/Cache.h"
11 
12 namespace lsst {
13 namespace afw {
14 
17 
18 namespace detection {
19 namespace detail {
20 
21 // Key for caching PSFs with lsst::utils::Cache
22 //
23 // We cache PSFs by their x,y position. Although there are placeholders
24 // in the `Psf` class and here for `image::Color`, these are not used
25 // in the cache because `image::Color` is not currently well-defined
26 // or used.
27 struct PsfCacheKey {
30 
32  : position(position_), color(color_) {}
33 
34  bool operator==(PsfCacheKey const &other) const {
35  return position == other.position; // Currently don't care about color
36  }
37 
38  friend std::ostream &operator<<(std::ostream &os, PsfCacheKey const &key) { return os << key.position; }
39 };
40 
41 } // namespace detail
42 } // namespace detection
43 } // namespace afw
44 } // namespace lsst
45 
46 namespace std {
47 
48 // Template specialisation for hashing PsfCacheKey
49 //
50 // We currently ignore the color, consistent with operator==.
51 template <>
56  return std::hash<lsst::geom::Point2D>()(key.position);
57  }
58 };
59 
60 } // namespace std
61 
62 namespace lsst {
63 namespace afw {
64 namespace detection {
65 
66 namespace {
67 
68 bool isPointNull(lsst::geom::Point2D const &p) { return std::isnan(p.getX()) && std::isnan(p.getY()); }
69 
70 } // namespace
71 
72 Psf::Psf(bool isFixed, std::size_t capacity) : _isFixed(isFixed) {
73  _imageCache = std::make_unique<PsfCache>(capacity);
74  _kernelImageCache = std::make_unique<PsfCache>(capacity);
75 }
76 
77 Psf::~Psf() = default;
78 
79 Psf::Psf(Psf const &other) : Psf(other._isFixed, other.getCacheCapacity()) {}
80 
81 Psf::Psf(Psf &&other)
82  : _isFixed(other._isFixed),
83  _imageCache(std::move(other._imageCache)),
84  _kernelImageCache(std::move(other._kernelImageCache)) {}
85 
87  lsst::geom::Point2D const &position,
88  std::string const &warpAlgorithm,
89  unsigned int warpBuffer) {
90  // "ir" : (integer, residual)
91  std::pair<int, double> const irX = image::positionToIndex(position.getX(), true);
92  std::pair<int, double> const irY = image::positionToIndex(position.getY(), true);
93 
94  if (irX.second != 0.0 || irY.second != 0.0) {
95  im = math::offsetImage(*im, irX.second, irY.second, warpAlgorithm, warpBuffer);
96  }
97 
98  im->setXY0(irX.first + im->getX0(), irY.first + im->getY0());
99  return im;
100 }
101 
103  return computeImage(makeNullPoint());
104 }
105 
107  ImageOwnerEnum owner) const {
108  if (isPointNull(position)) position = getAveragePosition();
109  if (color.isIndeterminate()) color = getAverageColor();
110  std::shared_ptr<Psf::Image> result = (*_imageCache)(
111  detail::PsfCacheKey(position, color),
112  [this](detail::PsfCacheKey const &key) { return doComputeImage(key.position, key.color); });
113  if (owner == COPY) {
114  result = std::make_shared<Image>(*result, true);
115  }
116  return result;
117 }
118 
120  return computeKernelImage(makeNullPoint());
121 }
122 
124  ImageOwnerEnum owner) const {
125  if (_isFixed || isPointNull(position)) position = getAveragePosition();
126  if (_isFixed || color.isIndeterminate()) color = getAverageColor();
127  std::shared_ptr<Psf::Image> result = (*_kernelImageCache)(
128  detail::PsfCacheKey(position, color),
129  [this](detail::PsfCacheKey const &key) { return doComputeKernelImage(key.position, key.color); });
130  if (owner == COPY) {
131  result = std::make_shared<Image>(*result, true);
132  }
133  return result;
134 }
135 
137  return computeBBox(makeNullPoint());
138 }
139 
141  if (isPointNull(position)) position = getAveragePosition();
142  if (color.isIndeterminate()) color = getAverageColor();
143  return doComputeBBox(position, color);
144 }
145 
147  return computeImageBBox(makeNullPoint());
148 }
149 
151  if (isPointNull(position)) position = getAveragePosition();
152  if (color.isIndeterminate()) color = getAverageColor();
153  return doComputeImageBBox(position, color);
154 }
155 
157  return getLocalKernel(makeNullPoint());
158 }
159 
161  image::Color color) const {
162  if (isPointNull(position)) position = getAveragePosition();
163  if (color.isIndeterminate()) color = getAverageColor();
164  // FixedKernel ctor will deep copy image, so we can use INTERNAL.
166  return std::make_shared<math::FixedKernel>(*image);
167 }
168 
169 double Psf::computePeak() const {
170  return computePeak(makeNullPoint());
171 }
172 
173 double Psf::computePeak(lsst::geom::Point2D position, image::Color color) const {
174  if (isPointNull(position)) position = getAveragePosition();
175  if (color.isIndeterminate()) color = getAverageColor();
177  return (*image)(-image->getX0(), -image->getY0());
178 }
179 
180 double Psf::computeApertureFlux(double radius) const {
181  return computeApertureFlux(radius, makeNullPoint());
182 }
183 
184 double Psf::computeApertureFlux(double radius, lsst::geom::Point2D position, image::Color color) const {
185  if (isPointNull(position)) position = getAveragePosition();
186  if (color.isIndeterminate()) color = getAverageColor();
187  return doComputeApertureFlux(radius, position, color);
188 }
189 
191  return computeShape(makeNullPoint());
192 }
193 
195  if (isPointNull(position)) position = getAveragePosition();
196  if (color.isIndeterminate()) color = getAverageColor();
197  return doComputeShape(position, color);
198 }
199 
201  image::Color const &color) const {
202  std::shared_ptr<Psf::Image> im = computeKernelImage(position, color, COPY);
203  return recenterKernelImage(im, position);
204 }
205 
207  image::Color const& color) const {
208  std::shared_ptr<Psf::Image> im = computeImage(position, color, INTERNAL);
209  return im->getBBox();
210 }
211 
213 
214 std::size_t Psf::getCacheCapacity() const { return _kernelImageCache->capacity(); }
215 
217  _imageCache->reserve(capacity);
218  _kernelImageCache->reserve(capacity);
219 }
220 
221 } // namespace detection
222 } // namespace afw
223 } // namespace lsst
std::ostream * os
Definition: Schema.cc:558
A polymorphic base class for representing an image's Point Spread Function.
Definition: Psf.h:76
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:86
lsst::geom::Box2I computeBBox() const
Definition: Psf.cc:136
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:200
lsst::geom::Box2I computeImageBBox() const
Definition: Psf.cc:146
std::size_t getCacheCapacity() const
Return the capacity of the caches.
Definition: Psf.cc:214
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...
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:184
std::shared_ptr< Image > computeImage() const
Definition: Psf.cc:102
std::shared_ptr< Image > computeKernelImage() const
Definition: Psf.cc:119
std::shared_ptr< math::Kernel const > getLocalKernel() const
Definition: Psf.cc:156
Psf(Psf const &)
Definition: Psf.cc:79
double computePeak() const
Definition: Psf.cc:169
image::Color getAverageColor() const
Return the average Color of the stars used to construct the Psf.
Definition: Psf.h:274
virtual lsst::geom::Box2I doComputeImageBBox(lsst::geom::Point2D const &position, image::Color const &color) const
Definition: Psf.cc:206
ImageOwnerEnum
Enum passed to computeImage and computeKernelImage to determine image ownership.
Definition: Psf.h:86
@ COPY
The image will be copied before returning; caller will own it.
Definition: Psf.h:87
@ INTERNAL
An internal image will be returned without copying.
Definition: Psf.h:88
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:216
geom::ellipses::Quadrupole computeShape() const
Definition: Psf.cc:190
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:212
virtual geom::ellipses::Quadrupole doComputeShape(lsst::geom::Point2D const &position, image::Color const &color) const =0
An ellipse core with quadrupole moments as parameters.
Definition: Quadrupole.h:47
Describe the colour of a source.
Definition: Color.h:26
bool isIndeterminate() const noexcept
Whether the color is the special value that indicates that it is unspecified.
Definition: Color.h:37
static std::shared_ptr< T > dynamicCast(std::shared_ptr< Persistable > const &ptr)
Dynamically cast a shared_ptr.
Definition: Persistable.cc:18
T isnan(T... args)
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
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.
Definition: offsetImage.cc:41
Point< double, 2 > Point2D
A base class for image defects.
STL namespace.
friend std::ostream & operator<<(std::ostream &os, PsfCacheKey const &key)
Definition: Psf.cc:38
lsst::geom::Point2D const position
Definition: Psf.cc:28
bool operator==(PsfCacheKey const &other) const
Definition: Psf.cc:34
PsfCacheKey(lsst::geom::Point2D const &position_, image::Color color_=image::Color())
Definition: Psf.cc:31
std::size_t operator()(lsst::afw::detection::detail::PsfCacheKey const &key) const noexcept
Definition: Psf.cc:55