LSSTApplications  21.0.0+75b29a8a7f,21.0.0+e70536a077,21.0.0-1-ga51b5d4+62c747d40b,21.0.0-11-ga6ea59e8e+47cba9fc36,21.0.0-2-g103fe59+914993bf7c,21.0.0-2-g1367e85+e2614ded12,21.0.0-2-g45278ab+e70536a077,21.0.0-2-g4bc9b9f+7b2b5f8678,21.0.0-2-g5242d73+e2614ded12,21.0.0-2-g54e2caa+6403186824,21.0.0-2-g7f82c8f+3ac4acbffc,21.0.0-2-g8dde007+04a6aea1af,21.0.0-2-g8f08a60+9402881886,21.0.0-2-ga326454+3ac4acbffc,21.0.0-2-ga63a54e+81dd751046,21.0.0-2-gc738bc1+5f65c6e7a9,21.0.0-2-gde069b7+26c92b3210,21.0.0-2-gecfae73+0993ddc9bd,21.0.0-2-gfc62afb+e2614ded12,21.0.0-21-gba890a8+5a4f502a26,21.0.0-23-g9966ff26+03098d1af8,21.0.0-3-g357aad2+8ad216c477,21.0.0-3-g4be5c26+e2614ded12,21.0.0-3-g6d51c4a+4d2fe0280d,21.0.0-3-g7d9da8d+75b29a8a7f,21.0.0-3-gaa929c8+522e0f12c2,21.0.0-3-ge02ed75+4d2fe0280d,21.0.0-4-g3300ddd+e70536a077,21.0.0-4-gc004bbf+eac6615e82,21.0.0-4-gccdca77+f94adcd104,21.0.0-4-gd1c1571+18b81799f9,21.0.0-5-g7b47fff+4d2fe0280d,21.0.0-5-gb155db7+d2632f662b,21.0.0-5-gdf36809+637e4641ee,21.0.0-6-g722ad07+28c848f42a,21.0.0-7-g959bb79+522e0f12c2,21.0.0-7-gfd72ab2+cf01990774,21.0.0-9-g87fb7b8d+e2ab11cdd6,w.2021.04
LSSTDataManagementBasePackage
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 
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  ImageOwnerEnum owner) const {
104  if (isPointNull(position)) position = getAveragePosition();
105  if (color.isIndeterminate()) color = getAverageColor();
106  std::shared_ptr<Psf::Image> result = (*_imageCache)(
107  detail::PsfCacheKey(position, color),
108  [this](detail::PsfCacheKey const &key) { return doComputeImage(key.position, key.color); });
109  if (owner == COPY) {
110  result = std::make_shared<Image>(*result, true);
111  }
112  return result;
113 }
114 
116  ImageOwnerEnum owner) const {
117  if (_isFixed || isPointNull(position)) position = getAveragePosition();
118  if (_isFixed || color.isIndeterminate()) color = getAverageColor();
119  std::shared_ptr<Psf::Image> result = (*_kernelImageCache)(
120  detail::PsfCacheKey(position, color),
121  [this](detail::PsfCacheKey const &key) { return doComputeKernelImage(key.position, key.color); });
122  if (owner == COPY) {
123  result = std::make_shared<Image>(*result, true);
124  }
125  return result;
126 }
127 
129  if (isPointNull(position)) position = getAveragePosition();
130  if (color.isIndeterminate()) color = getAverageColor();
131  return doComputeBBox(position, color);
132 }
133 
135  image::Color color) const {
136  if (isPointNull(position)) position = getAveragePosition();
137  if (color.isIndeterminate()) color = getAverageColor();
138  // FixedKernel ctor will deep copy image, so we can use INTERNAL.
140  return std::make_shared<math::FixedKernel>(*image);
141 }
142 
143 double Psf::computePeak(lsst::geom::Point2D position, image::Color color) const {
144  if (isPointNull(position)) position = getAveragePosition();
145  if (color.isIndeterminate()) color = getAverageColor();
147  return (*image)(-image->getX0(), -image->getY0());
148 }
149 
150 double Psf::computeApertureFlux(double radius, lsst::geom::Point2D position, image::Color color) const {
151  if (isPointNull(position)) position = getAveragePosition();
152  if (color.isIndeterminate()) color = getAverageColor();
153  return doComputeApertureFlux(radius, position, color);
154 }
155 
157  if (isPointNull(position)) position = getAveragePosition();
158  if (color.isIndeterminate()) color = getAverageColor();
159  return doComputeShape(position, color);
160 }
161 
163  image::Color const &color) const {
164  std::shared_ptr<Psf::Image> im = computeKernelImage(position, color, COPY);
165  return recenterKernelImage(im, position);
166 }
167 
169 
170 std::size_t Psf::getCacheCapacity() const { return _kernelImageCache->capacity(); }
171 
173  _imageCache->reserve(capacity);
174  _kernelImageCache->reserve(capacity);
175 }
176 
177 } // namespace detection
178 } // namespace afw
179 } // namespace lsst
lsst::afw::detection::Psf::Psf
Psf(Psf const &)
Definition: Psf.cc:79
lsst::afw::detection::Psf::getAveragePosition
virtual lsst::geom::Point2D getAveragePosition() const
Return the average position of the stars used to construct the Psf.
Definition: Psf.cc:168
lsst::afw::image
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Definition: imageAlgorithm.dox:1
lsst::afw::detection::detail::PsfCacheKey::position
lsst::geom::Point2D const position
Definition: Psf.cc:28
lsst::afw::detection::Psf::computeShape
geom::ellipses::Quadrupole computeShape(lsst::geom::Point2D position=makeNullPoint(), image::Color color=image::Color()) const
Compute the ellipse corresponding to the second moments of the Psf.
Definition: Psf.cc:156
offsetImage.h
Persistable.cc
Cache.h
std::string
STL class.
std::shared_ptr
STL class.
lsst::afw::detection::Psf::INTERNAL
@ INTERNAL
An internal image will be returned without copying.
Definition: Psf.h:88
Psf.h
lsst::afw::image::positionToIndex
int positionToIndex(double pos)
Convert image position to nearest integer index.
Definition: ImageUtils.h:69
std::pair
lsst::afw::detection::Psf::computePeak
double computePeak(lsst::geom::Point2D position=makeNullPoint(), image::Color color=image::Color()) const
Return the peak value of the PSF image.
Definition: Psf.cc:143
std::hash< lsst::afw::detection::detail::PsfCacheKey >
Definition: Psf.cc:52
lsst::afw
Definition: imageAlgorithm.dox:1
lsst::afw::detection::detail::PsfCacheKey::operator<<
friend std::ostream & operator<<(std::ostream &os, PsfCacheKey const &key)
Definition: Psf.cc:38
lsst::afw::image::Color::isIndeterminate
bool isIndeterminate() const noexcept
Whether the color is the special value that indicates that it is unspecified.
Definition: Color.h:37
lsst.pipe.tasks.processCcdWithFakes.radius
radius
Definition: processCcdWithFakes.py:347
lsst::geom::Point2D
Point< double, 2 > Point2D
Definition: Point.h:324
lsst::afw::detection::detail::PsfCacheKey::color
image::Color const color
Definition: Psf.cc:29
lsst::afw::detection::Psf::setCacheCapacity
void setCacheCapacity(std::size_t capacity)
Set the capacity of the caches.
Definition: Psf.cc:172
lsst::afw::detection::Psf::~Psf
~Psf() override
Definition: psfImpl.cc:125
lsst::afw::detection::Psf::getCacheCapacity
std::size_t getCacheCapacity() const
Return the capacity of the caches.
Definition: Psf.cc:170
std::isnan
T isnan(T... args)
std::hash< lsst::afw::detection::detail::PsfCacheKey >::operator()
std::size_t operator()(lsst::afw::detection::detail::PsfCacheKey const &key) const noexcept
Definition: Psf.cc:55
lsst::afw::detection::detail::PsfCacheKey::PsfCacheKey
PsfCacheKey(lsst::geom::Point2D const &position_, image::Color color_=image::Color())
Definition: Psf.cc:31
lsst::afw::detection::Psf::computeImage
std::shared_ptr< Image > computeImage(lsst::geom::Point2D position=makeNullPoint(), 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:102
std::ostream
STL class.
lsst::afw::detection::detail::PsfCacheKey::operator==
bool operator==(PsfCacheKey const &other) const
Definition: Psf.cc:34
other
ItemVariant const * other
Definition: Schema.cc:56
lsst::afw::detection::Psf::COPY
@ COPY
The image will be copied before returning; caller will own it.
Definition: Psf.h:87
result
py::object result
Definition: _schema.cc:429
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
lsst::afw::geom::ellipses::Quadrupole
An ellipse core with quadrupole moments as parameters.
Definition: Quadrupole.h:47
os
std::ostream * os
Definition: Schema.cc:746
lsst::afw::image::ImageBase::getX0
int getX0() const
Return the image's column-origin.
Definition: ImageBase.h:304
lsst::afw::detection::Psf::getAverageColor
image::Color getAverageColor() const
Return the average Color of the stars used to construct the Psf.
Definition: Psf.h:233
lsst::afw::detection::Psf::ImageOwnerEnum
ImageOwnerEnum
Enum passed to computeImage and computeKernelImage to determine image ownership.
Definition: Psf.h:86
lsst::afw::table::io::PersistableFacade::dynamicCast
static std::shared_ptr< T > dynamicCast(std::shared_ptr< Persistable > const &ptr)
Dynamically cast a shared_ptr.
Definition: Persistable.cc:18
lsst::afw::detection::Psf::computeBBox
lsst::geom::Box2I computeBBox(lsst::geom::Point2D position=makeNullPoint(), image::Color color=image::Color()) const
Return the bounding box of the image returned by computeKernelImage()
Definition: Psf.cc:128
std
STL namespace.
key
Key< U > key
Definition: Schema.cc:281
lsst::geom::Point< double, 2 >
lsst::afw::math::offsetImage
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
lsst::geom::Box2I
An integer coordinate rectangle.
Definition: Box.h:55
lsst::afw::detection::Psf::recenterKernelImage
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::afw::image::ImageBase::setXY0
void setXY0(lsst::geom::Point2I const origin)
Set the ImageBase's origin.
Definition: ImageBase.h:432
std::size_t
lsst::afw::image::ImageBase::getY0
int getY0() const
Return the image's row-origin.
Definition: ImageBase.h:312
lsst::afw::detection::Psf
A polymorphic base class for representing an image's Point Spread Function.
Definition: Psf.h:76
lsst::afw::detection::Psf::getLocalKernel
std::shared_ptr< math::Kernel const > getLocalKernel(lsst::geom::Point2D position=makeNullPoint(), image::Color color=image::Color()) const
Return a FixedKernel corresponding to the Psf image at the given point.
Definition: Psf.cc:134
lsst::afw::detection::Psf::computeApertureFlux
double computeApertureFlux(double radius, lsst::geom::Point2D position=makeNullPoint(), image::Color color=image::Color()) const
Compute the "flux" of the Psf model within a circular aperture of the given radius.
Definition: Psf.cc:150
lsst::afw::detection::detail::PsfCacheKey
Definition: Psf.cc:27
lsst::afw::detection::Psf::computeKernelImage
std::shared_ptr< Image > computeKernelImage(lsst::geom::Point2D position=makeNullPoint(), image::Color color=image::Color(), ImageOwnerEnum owner=COPY) const
Return an Image of the PSF, in a form suitable for convolution.
Definition: Psf.cc:115
lsst::afw::detection::Psf::doComputeImage
virtual std::shared_ptr< Image > doComputeImage(lsst::geom::Point2D const &position, image::Color const &color) const
This virtual member is protected (rather than private) so that python-implemented derived classes may...
Definition: Psf.cc:162
lsst::afw::image::Color
Describe the colour of a source.
Definition: Color.h:26
std::hash