LSSTApplications  16.0-1-gce273f5+20,16.0-10-g1758552,16.0-10-gc1446dd+21,16.0-12-g569485f+3,16.0-13-g5f20d24+1,16.0-13-g80874fd+2,16.0-13-gb122224+12,16.0-14-g08f9460+4,16.0-14-g8a3b804,16.0-14-ga5060d2,16.0-15-g77ef378+7,16.0-18-gdf247dd+2,16.0-18-ge18fa5b,16.0-18-ge4151178,16.0-2-g0febb12+16,16.0-2-g9d5294e+46,16.0-2-gc6e0ed0+5,16.0-23-ge8a9b866+3,16.0-3-g404ea43+13,16.0-3-gbc759ec+19,16.0-3-gcfd6c53+44,16.0-3-ge00e371,16.0-4-g03cf288+35,16.0-4-g13a27c5+21,16.0-4-g5f3a788+15,16.0-4-ga3eb747+5,16.0-5-g1991253+21,16.0-5-g1e9226d+3,16.0-5-g6a53317,16.0-5-g865efd9+23,16.0-5-gb3f8a4b+53,16.0-5-gd0f1235+10,16.0-52-gad2f36c79,16.0-7-g6043bfc+9,16.0-7-gd2eeba5+3,16.0-7-gde5bd64+3,16.0-8-g0e813a6+1,16.0-9-g52c50f7,16.0-9-g73415e6,master-g5768c874b9+5,w.2018.41
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 #include "lsst/afw/table/io/Persistable.cc"
12 
13 namespace lsst {
14 namespace afw {
15 
18 
19 namespace detection {
20 namespace detail {
21 
22 // Key for caching PSFs with lsst::utils::Cache
23 //
24 // We cache PSFs by their x,y position. Although there are placeholders
25 // in the `Psf` class and here for `image::Color`, these are not used
26 // in the cache because `image::Color` is not currently well-defined
27 // or used.
28 struct PsfCacheKey {
31 
33  : position(position_), color(color_) {}
34 
35  bool operator==(PsfCacheKey const &other) const {
36  return position == other.position; // Currently don't care about color
37  }
38 
39  friend std::ostream &operator<<(std::ostream &os, PsfCacheKey const &key) { return os << key.position; }
40 };
41 
42 } // namespace detail
43 } // namespace detection
44 } // namespace afw
45 } // namespace lsst
46 
47 namespace std {
48 
49 // Template specialisation for hashing PsfCacheKey
50 //
51 // We currently ignore the color.
52 template <>
55  std::size_t seed = 0;
56  return lsst::utils::hashCombine(seed, key.position.getX(), key.position.getY());
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) : daf::base::Citizen(typeid(this)), _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  : daf::base::Citizen(std::move(other)),
83  _isFixed(other._isFixed),
84  _imageCache(std::move(other._imageCache)),
85  _kernelImageCache(std::move(other._kernelImageCache)) {}
86 
88  lsst::geom::Point2D const &position,
89  std::string const &warpAlgorithm,
90  unsigned int warpBuffer) {
91  // "ir" : (integer, residual)
92  std::pair<int, double> const irX = image::positionToIndex(position.getX(), true);
93  std::pair<int, double> const irY = image::positionToIndex(position.getY(), true);
94 
95  if (irX.second != 0.0 || irY.second != 0.0) {
96  im = math::offsetImage(*im, irX.second, irY.second, warpAlgorithm, warpBuffer);
97  }
98 
99  im->setXY0(irX.first + im->getX0(), irY.first + im->getY0());
100  return im;
101 }
102 
104  ImageOwnerEnum owner) const {
105  if (isPointNull(position)) position = getAveragePosition();
106  if (color.isIndeterminate()) color = getAverageColor();
107  std::shared_ptr<Psf::Image> result = (*_imageCache)(
108  detail::PsfCacheKey(position, color),
109  [this](detail::PsfCacheKey const &key) { return doComputeImage(key.position, key.color); });
110  if (owner == COPY) {
111  result = std::make_shared<Image>(*result, true);
112  }
113  return result;
114 }
115 
117  ImageOwnerEnum owner) const {
118  if (_isFixed || isPointNull(position)) position = getAveragePosition();
119  if (_isFixed || color.isIndeterminate()) color = getAverageColor();
120  std::shared_ptr<Psf::Image> result = (*_kernelImageCache)(
121  detail::PsfCacheKey(position, color),
122  [this](detail::PsfCacheKey const &key) { return doComputeKernelImage(key.position, key.color); });
123  if (owner == COPY) {
124  result = std::make_shared<Image>(*result, true);
125  }
126  return result;
127 }
128 
130  if (isPointNull(position)) position = getAveragePosition();
131  if (color.isIndeterminate()) color = getAverageColor();
132  return doComputeBBox(position, color);
133 }
134 
136  image::Color color) const {
137  if (isPointNull(position)) position = getAveragePosition();
138  if (color.isIndeterminate()) color = getAverageColor();
139  // FixedKernel ctor will deep copy image, so we can use INTERNAL.
141  return std::make_shared<math::FixedKernel>(*image);
142 }
143 
145  if (isPointNull(position)) position = getAveragePosition();
146  if (color.isIndeterminate()) color = getAverageColor();
148  return (*image)(-image->getX0(), -image->getY0());
149 }
150 
151 double Psf::computeApertureFlux(double radius, lsst::geom::Point2D position, image::Color color) const {
152  if (isPointNull(position)) position = getAveragePosition();
153  if (color.isIndeterminate()) color = getAverageColor();
154  return doComputeApertureFlux(radius, position, color);
155 }
156 
158  if (isPointNull(position)) position = getAveragePosition();
159  if (color.isIndeterminate()) color = getAverageColor();
160  return doComputeShape(position, color);
161 }
162 
163 std::shared_ptr<Psf::Image> Psf::doComputeImage(lsst::geom::Point2D const &position,
164  image::Color const &color) const {
165  std::shared_ptr<Psf::Image> im = computeKernelImage(position, color, COPY);
166  return recenterKernelImage(im, position);
167 }
168 
170 
171 std::size_t Psf::getCacheCapacity() const { return _kernelImageCache->capacity(); }
172 
174  _imageCache->reserve(capacity);
175  _kernelImageCache->reserve(capacity);
176 }
177 
178 } // namespace detection
179 } // namespace afw
180 } // namespace lsst
An ellipse core with quadrupole moments as parameters.
Definition: Quadrupole.h:47
Psf(Psf const &)
Definition: Psf.cc:79
static std::shared_ptr< T > dynamicCast(std::shared_ptr< Persistable > const &ptr)
Dynamically cast a shared_ptr.
Definition: Persistable.cc:18
virtual lsst::geom::Point2D getAveragePosition() const
Return the average position of the stars used to construct the Psf.
Definition: Psf.cc:169
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:171
lsst::geom::Point2D const position
Definition: Psf.cc:29
std::size_t operator()(lsst::afw::detection::detail::PsfCacheKey const &key) const
Definition: Psf.cc:54
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:135
ImageOwnerEnum
Enum passed to computeImage and computeKernelImage to determine image ownership.
Definition: Psf.h:87
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:103
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:151
image::Color getAverageColor() const
Return the average Color of the stars used to construct the Psf.
Definition: Psf.h:226
Point< double, 2 > Point2D
Definition: Point.h:324
STL class.
friend std::ostream & operator<<(std::ostream &os, PsfCacheKey const &key)
Definition: Psf.cc:39
double computePeak(lsst::geom::Point2D position=makeNullPoint(), image::Color color=image::Color()) const
Return the peak value of the PSF image.
Definition: Psf.cc:144
A base class for image defects.
Definition: cameraGeom.dox:3
PsfCacheKey(lsst::geom::Point2D const &position_, image::Color color_=image::Color())
Definition: Psf.cc:32
bool operator==(PsfCacheKey const &other) const
Definition: Psf.cc:35
void setCacheCapacity(std::size_t capacity)
Set the capacity of the caches.
Definition: Psf.cc:173
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:87
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:45
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:116
T isnan(T... args)
ItemVariant const * other
Definition: Schema.cc:55
An internal image will be returned without copying.
Definition: Psf.h:89
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:75
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:129
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:88
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:157