LSSTApplications  16.0+42,16.0-1-gce273f5+8,16.0-10-g230e10e+1,16.0-11-g9fe0e56+17,16.0-11-gce733cf+17,16.0-12-g5ad1ebf+9,16.0-12-gc85596e+2,16.0-13-gde155d7+2,16.0-14-g9428de4d,16.0-14-gc1cf4a94+2,16.0-15-g8e16a51+14,16.0-2-g0febb12+7,16.0-2-g839ba83+32,16.0-2-g9d5294e+22,16.0-2-gab3db49+7,16.0-2-gf41ba6b+6,16.0-2-gf4e7cdd+5,16.0-3-g6923fb6+15,16.0-3-g8e51203+2,16.0-3-g9645794+6,16.0-3-gcfd6c53+20,16.0-35-g34c7dfe62+1,16.0-4-g03cf288+11,16.0-4-g32d12de,16.0-4-g5f3a788+7,16.0-4-g7690030+30,16.0-4-g8a0f11a+16,16.0-4-ga5d8928+16,16.0-5-g0da18be+7,16.0-5-g4940a70,16.0-5-g563880a+2,16.0-5-g7742071+2,16.0-5-gb3f8a4b+26,16.0-6-g3610b4f+5,16.0-6-gf0acd13+14,16.0-8-g4dec96c+7,16.0-8-gc315727+16,16.0-9-g1de645c+7,16.0-9-gcc4efb7+6,w.2018.36
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 namespace detection {
15 namespace detail {
16 
17 // Key for caching PSFs with lsst::utils::Cache
18 //
19 // We cache PSFs by their x,y position. Although there are placeholders
20 // in the `Psf` class and here for `image::Color`, these are not used
21 // in the cache because `image::Color` is not currently well-defined
22 // or used.
23 struct PsfCacheKey {
26 
28  : position(position_), color(color_) {}
29 
30  bool operator==(PsfCacheKey const &other) const {
31  return position == other.position; // Currently don't care about color
32  }
33 
34  friend std::ostream &operator<<(std::ostream &os, PsfCacheKey const &key) { return os << key.position; }
35 };
36 
37 } // namespace detail
38 } // namespace detection
39 } // namespace afw
40 } // namespace lsst
41 
42 namespace std {
43 
44 // Template specialisation for hashing PsfCacheKey
45 //
46 // We currently ignore the color.
47 template <>
48 struct hash<lsst::afw::detection::detail::PsfCacheKey> {
50  std::size_t seed = 0;
51  return lsst::utils::hashCombine(seed, key.position.getX(), key.position.getY());
52  }
53 };
54 
55 } // namespace std
56 
57 namespace lsst {
58 namespace afw {
59 namespace detection {
60 
61 namespace {
62 
63 bool isPointNull(lsst::geom::Point2D const &p) { return std::isnan(p.getX()) && std::isnan(p.getY()); }
64 
65 } // namespace
66 
67 Psf::Psf(bool isFixed, std::size_t capacity) : daf::base::Citizen(typeid(this)), _isFixed(isFixed) {
68  _imageCache = std::make_unique<PsfCache>(capacity);
69  _kernelImageCache = std::make_unique<PsfCache>(capacity);
70 }
71 
72 Psf::~Psf() = default;
73 
74 Psf::Psf(Psf const &other) : Psf(other._isFixed, other.getCacheCapacity()) {}
75 
77  : daf::base::Citizen(std::move(other)),
78  _isFixed(other._isFixed),
79  _imageCache(std::move(other._imageCache)),
80  _kernelImageCache(std::move(other._kernelImageCache)) {}
81 
83  lsst::geom::Point2D const &position,
84  std::string const &warpAlgorithm,
85  unsigned int warpBuffer) {
86  // "ir" : (integer, residual)
87  std::pair<int, double> const irX = image::positionToIndex(position.getX(), true);
88  std::pair<int, double> const irY = image::positionToIndex(position.getY(), true);
89 
90  if (irX.second != 0.0 || irY.second != 0.0) {
91  im = math::offsetImage(*im, irX.second, irY.second, warpAlgorithm, warpBuffer);
92  }
93 
94  im->setXY0(irX.first + im->getX0(), irY.first + im->getY0());
95  return im;
96 }
97 
99  ImageOwnerEnum owner) const {
100  if (isPointNull(position)) position = getAveragePosition();
101  if (color.isIndeterminate()) color = getAverageColor();
102  std::shared_ptr<Psf::Image> result = (*_imageCache)(
103  detail::PsfCacheKey(position, color),
104  [this](detail::PsfCacheKey const &key) { return doComputeImage(key.position, key.color); });
105  if (owner == COPY) {
106  result = std::make_shared<Image>(*result, true);
107  }
108  return result;
109 }
110 
112  ImageOwnerEnum owner) const {
113  if (_isFixed || isPointNull(position)) position = getAveragePosition();
114  if (_isFixed || color.isIndeterminate()) color = getAverageColor();
115  std::shared_ptr<Psf::Image> result = (*_kernelImageCache)(
116  detail::PsfCacheKey(position, color),
117  [this](detail::PsfCacheKey const &key) { return doComputeKernelImage(key.position, key.color); });
118  if (owner == COPY) {
119  result = std::make_shared<Image>(*result, true);
120  }
121  return result;
122 }
123 
125  if (isPointNull(position)) position = getAveragePosition();
126  if (color.isIndeterminate()) color = getAverageColor();
127  return doComputeBBox(position, color);
128 }
129 
131  image::Color color) const {
132  if (isPointNull(position)) position = getAveragePosition();
133  if (color.isIndeterminate()) color = getAverageColor();
134  // FixedKernel ctor will deep copy image, so we can use INTERNAL.
136  return std::make_shared<math::FixedKernel>(*image);
137 }
138 
140  if (isPointNull(position)) position = getAveragePosition();
141  if (color.isIndeterminate()) color = getAverageColor();
143  return (*image)(-image->getX0(), -image->getY0());
144 }
145 
146 double Psf::computeApertureFlux(double radius, lsst::geom::Point2D position, image::Color color) const {
147  if (isPointNull(position)) position = getAveragePosition();
148  if (color.isIndeterminate()) color = getAverageColor();
149  return doComputeApertureFlux(radius, position, color);
150 }
151 
153  if (isPointNull(position)) position = getAveragePosition();
154  if (color.isIndeterminate()) color = getAverageColor();
155  return doComputeShape(position, color);
156 }
157 
158 std::shared_ptr<Psf::Image> Psf::doComputeImage(lsst::geom::Point2D const &position,
159  image::Color const &color) const {
160  std::shared_ptr<Psf::Image> im = computeKernelImage(position, color, COPY);
161  return recenterKernelImage(im, position);
162 }
163 
165 
166 std::size_t Psf::getCacheCapacity() const { return _kernelImageCache->capacity(); }
167 
169  _imageCache->reserve(capacity);
170  _kernelImageCache->reserve(capacity);
171 }
172 
173 } // namespace detection
174 } // namespace afw
175 } // namespace lsst
An ellipse core with quadrupole moments as parameters.
Definition: Quadrupole.h:47
Psf(Psf const &)
Definition: Psf.cc:74
virtual lsst::geom::Point2D getAveragePosition() const
Return the average position of the stars used to construct the Psf.
Definition: Psf.cc:164
int positionToIndex(double pos)
Convert image position to nearest integer index.
Definition: ImageUtils.h:69
std::size_t getCacheCapacity() const
Return the capacity of the caches.
Definition: Psf.cc:166
lsst::geom::Point2D const position
Definition: Psf.cc:24
std::size_t operator()(lsst::afw::detection::detail::PsfCacheKey const &key) const
Definition: Psf.cc:49
py::object result
Definition: schema.cc:284
STL namespace.
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:130
ImageOwnerEnum
Enum passed to computeImage and computeKernelImage to determine image ownership.
Definition: Psf.h:90
bool isIndeterminate() const noexcept
Whether the color is the special value that indicates that it is unspecified.
Definition: Color.h:37
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:98
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:146
image::Color getAverageColor() const
Return the average Color of the stars used to construct the Psf.
Definition: Psf.h:229
Point< double, 2 > Point2D
Definition: Point.h:324
STL class.
friend std::ostream & operator<<(std::ostream &os, PsfCacheKey const &key)
Definition: Psf.cc:34
double computePeak(lsst::geom::Point2D position=makeNullPoint(), image::Color color=image::Color()) const
Return the peak value of the PSF image.
Definition: Psf.cc:139
A base class for image defects.
Definition: cameraGeom.dox:3
PsfCacheKey(lsst::geom::Point2D const &position_, image::Color color_=image::Color())
Definition: Psf.cc:27
bool operator==(PsfCacheKey const &other) const
Definition: Psf.cc:30
void setCacheCapacity(std::size_t capacity)
Set the capacity of the caches.
Definition: Psf.cc:168
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,0) when xy0 is taken into account) to an image centered at position when xy0 is taken into account.
Definition: Psf.cc:82
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
Citizen(const std::type_info &)
Definition: Citizen.cc:163
Key< U > key
Definition: Schema.cc:280
std::size_t hashCombine(std::size_t seed)
Combine hashes.
Definition: hashCombine.h:24
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:111
T isnan(T... args)
ItemVariant const * other
Definition: Schema.cc:55
An internal image will be returned without copying.
Definition: Psf.h:92
Describe the colour of a source.
Definition: Color.h:26
A polymorphic base class for representing an image&#39;s Point Spread Function.
Definition: Psf.h:77
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:124
An integer coordinate rectangle.
Definition: Box.h:54
STL class.
std::ostream * os
Definition: Schema.cc:737
The image will be copied before returning; caller will own it.
Definition: Psf.h:91
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:152