lsst.meas.algorithms  13.0-13-gf5c99ad+4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CoaddPsf.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 /*
26  * Represent a PSF as for a Coadd based on the James Jee stacking
27  * algorithm which was extracted from Stackfit.
28  */
29 #include <cmath>
30 #include <sstream>
31 #include <iostream>
32 #include <numeric>
33 #include "boost/iterator/iterator_adaptor.hpp"
34 #include "boost/iterator/transform_iterator.hpp"
35 #include "ndarray/eigen.h"
36 #include "lsst/base.h"
37 #include "lsst/pex/exceptions.h"
38 #include "lsst/afw/image/ImageUtils.h"
39 #include "lsst/afw/math/Statistics.h"
41 #include "lsst/afw/table/io/OutputArchive.h"
42 #include "lsst/afw/table/io/InputArchive.h"
43 #include "lsst/afw/table/io/CatalogVector.h"
45 
46 namespace lsst {
47 namespace meas {
48 namespace algorithms {
49 
50 namespace {
51 
52 // Struct used to simplify calculations in computeAveragePosition; lets us use
53 // std::accumulate instead of explicit for loop.
54 struct AvgPosItem {
55  double wx; // weighted x position
56  double wy; // weighted y position
57  double w; // weight value
58 
59  explicit AvgPosItem(double wx_=0.0, double wy_=0.0, double w_=0.0) : wx(wx_), wy(wy_), w(w_) {}
60 
61  // return point, assuming this is a sum of many AvgPosItems
62  afw::geom::Point2D getPoint() const { return afw::geom::Point2D(wx/w, wy/w); }
63 
64  // comparison so we can sort by weights
65  bool operator<(AvgPosItem const & other) const {
66  return w < other.w;
67  }
68 
69  AvgPosItem & operator+=(AvgPosItem const & other) {
70  wx += other.wx;
71  wy += other.wy;
72  w += other.w;
73  return *this;
74  }
75 
76  AvgPosItem & operator-=(AvgPosItem const & other) {
77  wx -= other.wx;
78  wy -= other.wy;
79  w -= other.w;
80  return *this;
81  }
82 
83  friend AvgPosItem operator+(AvgPosItem a, AvgPosItem const & b) { return a += b; }
84 
85  friend AvgPosItem operator-(AvgPosItem a, AvgPosItem const & b) { return a -= b; }
86 };
87 
88 afw::geom::Point2D computeAveragePosition(
89  afw::table::ExposureCatalog const & catalog,
90  afw::image::Wcs const & coaddWcs,
91  afw::table::Key<double> weightKey
92 ) {
93  afw::table::Key<int> goodPixKey;
94  try {
95  goodPixKey = catalog.getSchema()["goodpix"];
96  } catch (pex::exceptions::NotFoundError &) {}
97  std::vector<AvgPosItem> items;
98  items.reserve(catalog.size());
99  for (afw::table::ExposureCatalog::const_iterator i = catalog.begin(); i != catalog.end(); ++i) {
100  afw::geom::Point2D p = coaddWcs.skyToPixel(
101  *i->getWcs()->pixelToSky(
102  i->getPsf()->getAveragePosition()
103  )
104  );
105  AvgPosItem item(p.getX(), p.getY(), i->get(weightKey));
106  if (goodPixKey.isValid()) {
107  item.w *= i->get(goodPixKey);
108  }
109  item.wx *= item.w;
110  item.wy *= item.w;
111  items.push_back(item);
112  }
113  // This is a bit pessimistic - we save and sort all the weights all the time,
114  // even though we'll only need them if the average position from all of them
115  // is invalid. But it makes for simpler code, and it's not that expensive
116  // computationally anyhow.
117  std::sort(items.begin(), items.end());
118  AvgPosItem result = std::accumulate(items.begin(), items.end(), AvgPosItem());
119  // If the position isn't valid (no input frames contain it), we remove frames
120  // from the average until it does.
121  for (
122  std::vector<AvgPosItem>::iterator iter = items.begin();
123  catalog.subsetContaining(result.getPoint(), coaddWcs, true).empty();
124  ++iter
125  ) {
126  if (iter == items.end()) {
127  // This should only happen if there are no inputs at all,
128  // or if constituent Psfs have a badly-behaved implementation
129  // of getAveragePosition().
130  throw LSST_EXCEPT(
131  pex::exceptions::RuntimeError,
132  "Could not find a valid average position for CoaddPsf"
133  );
134  }
135  result -= *iter;
136  }
137  return result.getPoint();
138 }
139 
140 } // anonymous
141 
143  afw::table::ExposureCatalog const & catalog,
144  afw::image::Wcs const & coaddWcs,
145  std::string const & weightFieldName,
146  std::string const & warpingKernelName,
147  int cacheSize
148 ) :
149  _coaddWcs(coaddWcs.clone()),
150  _warpingKernelName(warpingKernelName),
151  _warpingControl(std::make_shared<afw::math::WarpingControl>(warpingKernelName, "", cacheSize))
152 {
153  afw::table::SchemaMapper mapper(catalog.getSchema());
154  mapper.addMinimalSchema(afw::table::ExposureTable::makeMinimalSchema(), true);
155 
156  // copy the field "goodpix", if available, for computeAveragePosition to use
157  try {
158  afw::table::Key<int> goodPixKey = catalog.getSchema()["goodpix"]; // auto does not work
159  mapper.addMapping(goodPixKey, true);
160  } catch (pex::exceptions::NotFoundError &) {}
161 
162  // copy the field specified by weightFieldName to field "weight"
163  afw::table::Field<double> weightField = afw::table::Field<double>("weight", "Coadd weight");
164  afw::table::Key<double> weightKey = catalog.getSchema()[weightFieldName];
165  _weightKey = mapper.addMapping(weightKey, weightField);
166 
167  _catalog = afw::table::ExposureCatalog(mapper.getOutputSchema());
168  for (afw::table::ExposureCatalog::const_iterator i = catalog.begin(); i != catalog.end(); ++i) {
169  PTR(afw::table::ExposureRecord) record = _catalog.getTable()->makeRecord();
170  record->assign(*i, mapper);
171  _catalog.push_back(record);
172  }
173  _averagePosition = computeAveragePosition(_catalog, *_coaddWcs, _weightKey);
174 }
175 
176 PTR(afw::detection::Psf) CoaddPsf::clone() const {
177  return std::make_shared<CoaddPsf>(*this);
178 }
179 
180 
181 // Read all the images from the Image Vector and return the BBox in xy0 offset coordinates
182 
183 afw::geom::Box2I getOverallBBox(std::vector<PTR(afw::image::Image<double>)> const & imgVector) {
184 
185  afw::geom::Box2I bbox;
186  // Calculate the box which will contain them all
187  for (unsigned int i = 0; i < imgVector.size(); i ++) {
188  PTR(afw::image::Image<double>) componentImg = imgVector[i];
189  afw::geom::Box2I cBBox = componentImg->getBBox();
190  bbox.include(cBBox); // JFB: this works even on empty bboxes
191  }
192  return bbox;
193 }
194 
195 
196 // Read all the images from the Image Vector and add them to image
197 
199  PTR(afw::image::Image<double>) image,
200  std::vector<PTR(afw::image::Image<double>)> const & imgVector,
201  std::vector<double> const & weightVector
202 ) {
203  assert(imgVector.size() == weightVector.size());
204  for (unsigned int i = 0; i < imgVector.size(); i ++) {
205  PTR(afw::image::Image<double>) componentImg = imgVector[i];
206  double weight = weightVector[i];
207  double sum = componentImg->getArray().asEigen().sum();
208 
209  // Now get the portion of the component image which is appropriate to add
210  // If the default image size is used, the component is guaranteed to fit,
211  // but not if a size has been specified.
212  afw::geom::Box2I cBBox = componentImg->getBBox();
213  afw::geom::Box2I overlap(cBBox);
214  overlap.clip(image->getBBox());
215  // JFB: A subimage view of the image we want to add to, containing only the overlap region.
216  afw::image::Image<double> targetSubImage(*image, overlap);
217  // JFB: A subimage view of the image we want to add from, containing only the overlap region.
218  afw::image::Image<double> cSubImage(*componentImg, overlap);
219  targetSubImage.scaledPlus(weight/sum, cSubImage);
220  }
221 }
222 
223 
224 afw::geom::Box2I CoaddPsf::doComputeBBox(
225  afw::geom::Point2D const & ccdXY,
226  afw::image::Color const & color
227 ) const {
228  afw::table::ExposureCatalog subcat = _catalog.subsetContaining(ccdXY, *_coaddWcs, true);
229  if (subcat.empty()) {
230  throw LSST_EXCEPT(
231  pex::exceptions::InvalidParameterError,
232  (boost::format("Cannot compute BBox at point %s; no input images at that point.")
233  % ccdXY).str());
234  }
235 
236  afw::geom::Box2I ret;
237  for (auto const & exposureRecord : subcat) {
238  PTR(afw::geom::XYTransform) xytransform(
239  new afw::image::XYTransformFromWcsPair(_coaddWcs, exposureRecord.getWcs()));
240  WarpedPsf warpedPsf = WarpedPsf(exposureRecord.getPsf(), xytransform, _warpingControl);
241  afw::geom::Box2I componentBBox = warpedPsf.computeBBox(ccdXY, color);
242  ret.include(componentBBox);
243  }
244 
245  return ret;
246 }
247 
248 PTR(afw::detection::Psf::Image) CoaddPsf::doComputeKernelImage(
249  afw::geom::Point2D const & ccdXY,
250  afw::image::Color const & color
251 ) const {
252  // Get the subset of expoures which contain our coordinate within their validPolygons.
253  afw::table::ExposureCatalog subcat = _catalog.subsetContaining(ccdXY, *_coaddWcs, true);
254  if (subcat.empty()) {
255  throw LSST_EXCEPT(
256  pex::exceptions::InvalidParameterError,
257  (boost::format("Cannot compute CoaddPsf at point %s; no input images at that point.")
258  % ccdXY).str()
259  );
260  }
261  double weightSum = 0.0;
262 
263  // Read all the Psf images into a vector. The code is set up so that this can be done in chunks,
264  // with the image modified to accomodate
265  // However, we currently read all of the images.
266  std::vector<PTR(afw::image::Image<double>)> imgVector;
267  std::vector<double> weightVector;
268 
269  for (auto const & exposureRecord : subcat) {
270  PTR(afw::geom::XYTransform) xytransform(
271  new afw::image::XYTransformFromWcsPair(_coaddWcs, exposureRecord.getWcs())
272  );
273  WarpedPsf warpedPsf = WarpedPsf(exposureRecord.getPsf(), xytransform, _warpingControl);
274  PTR(afw::image::Image<double>) componentImg = warpedPsf.computeKernelImage(ccdXY, color);
275  imgVector.push_back(componentImg);
276  weightSum += exposureRecord.get(_weightKey);
277  weightVector.push_back(exposureRecord.get(_weightKey));
278  }
279 
280  afw::geom::Box2I bbox = getOverallBBox(imgVector);
281 
282  // create a zero image of the right size to sum into
283  PTR(afw::detection::Psf::Image) image = std::make_shared<afw::detection::Psf::Image>(bbox);
284  *image = 0.0;
285  addToImage(image, imgVector, weightVector);
286  *image /= weightSum;
287  return image;
288 }
289 
291  return _catalog.size();
292 }
293 
294 CONST_PTR(afw::detection::Psf) CoaddPsf::getPsf(int index) {
295  if (index < 0 || index > getComponentCount()) {
296  throw LSST_EXCEPT(pex::exceptions::RangeError, "index of CoaddPsf component out of range");
297  }
298  return _catalog[index].getPsf();
299 }
300 
301 CONST_PTR(afw::image::Wcs) CoaddPsf::getWcs(int index) {
302  if (index < 0 || index > getComponentCount()) {
303  throw LSST_EXCEPT(pex::exceptions::RangeError, "index of CoaddPsf component out of range");
304  }
305  return _catalog[index].getWcs();
306 }
307 
308 CONST_PTR(afw::geom::polygon::Polygon) CoaddPsf::getValidPolygon(int index) {
309  if (index < 0 || index > getComponentCount()) {
310  throw LSST_EXCEPT(pex::exceptions::RangeError, "index of CoaddPsf component out of range");
311  }
312  return _catalog[index].getValidPolygon();
313 }
314 
315 double CoaddPsf::getWeight(int index) {
316  if (index < 0 || index > getComponentCount()) {
317  throw LSST_EXCEPT(pex::exceptions::RangeError, "index of CoaddPsf component out of range");
318  }
319  return _catalog[index].get(_weightKey);
320 }
321 
322 afw::table::RecordId CoaddPsf::getId(int index) {
323  if (index < 0 || index > getComponentCount()) {
324  throw LSST_EXCEPT(pex::exceptions::RangeError, "index of CoaddPsf component out of range");
325  }
326  return _catalog[index].getId();
327 }
328 
329 afw::geom::Box2I CoaddPsf::getBBox(int index) {
330  if (index < 0 || index > getComponentCount()) {
331  throw LSST_EXCEPT(pex::exceptions::RangeError, "index of CoaddPsf component out of range");
332  }
333  return _catalog[index].getBBox();
334 }
335 
336 // ---------- Persistence -----------------------------------------------------------------------------------
337 
338 // For persistence of CoaddPsf, we have two catalogs: the first has just one record, and contains
339 // the archive ID of the coadd WCS, the size of the warping cache, the name of the warping kernel,
340 // and the average position. The latter is simply the ExposureCatalog.
341 
342 namespace {
343 
344 namespace tbl = afw::table;
345 
346 // Singleton class that manages the first persistence catalog's schema and keys
347 class CoaddPsfPersistenceHelper {
348 public:
349  tbl::Schema schema;
350  tbl::Key<int> coaddWcs;
351  tbl::Key<int> cacheSize;
352  tbl::PointKey<double> averagePosition;
353  tbl::Key<std::string> warpingKernelName;
354 
355  static CoaddPsfPersistenceHelper const & get() {
356  static CoaddPsfPersistenceHelper const instance;
357  return instance;
358  }
359 
360 private:
361  CoaddPsfPersistenceHelper() :
362  schema(),
363  coaddWcs(schema.addField<int>("coaddwcs", "archive ID of the coadd's WCS")),
364  cacheSize(schema.addField<int>("cachesize", "size of the warping cache")),
365  averagePosition(tbl::PointKey<double>::addFields(
366  schema, "avgpos", "PSF accessors default position", "pixel"
367  )),
368  warpingKernelName(schema.addField<std::string>("warpingkernelname", "warping kernel name", 32))
369  {
370  schema.getCitizen().markPersistent();
371  }
372 };
373 
374 } // anonymous
375 
376 class CoaddPsf::Factory : public tbl::io::PersistableFactory {
377 public:
378 
379  virtual PTR(tbl::io::Persistable)
380  read(InputArchive const & archive, CatalogVector const & catalogs) const {
381  if (catalogs.size() == 1u) {
382  // Old CoaddPsfs were saved in only one catalog, because we didn't
383  // save the warping parameters and average position, and we could
384  // save the coadd Wcs in a special final record.
385  return readV0(archive, catalogs);
386  }
387  LSST_ARCHIVE_ASSERT(catalogs.size() == 2u);
388  CoaddPsfPersistenceHelper const & keys1 = CoaddPsfPersistenceHelper::get();
389  LSST_ARCHIVE_ASSERT(catalogs.front().getSchema() == keys1.schema);
390  tbl::BaseRecord const & record1 = catalogs.front().front();
391  return PTR(CoaddPsf)(
392  new CoaddPsf(
393  tbl::ExposureCatalog::readFromArchive(archive, catalogs.back()),
394  archive.get<afw::image::Wcs>(record1.get(keys1.coaddWcs)),
395  record1.get(keys1.averagePosition),
396  record1.get(keys1.warpingKernelName),
397  record1.get(keys1.cacheSize)
398  )
399  );
400  }
401 
402 
403  // Backwards compatibility for files saved before meas_algorithms commit
404  // 53e61fae (7/10/2013). Prior to that change, the warping configuration
405  // and the average position were not saved at all, making it impossible to
406  // reconstruct the average position exactly, but it's better to
407  // approximate than to fail completely.
408  std::shared_ptr<tbl::io::Persistable>
409  readV0(InputArchive const & archive, CatalogVector const & catalogs) const {
410  auto internalCat = tbl::ExposureCatalog::readFromArchive(archive, catalogs.front());
411  // Coadd WCS is stored in a special last record.
412  auto coaddWcs = internalCat.back().getWcs();
413  internalCat.pop_back();
414  // Attempt to reconstruct the average position. We can't do this
415  // exactly, since the catalog we saved isn't the same one that was
416  // used to compute the original average position.
417  tbl::Key<double> weightKey;
418  try {
419  weightKey = internalCat.getSchema()["weight"];
420  } catch (pex::exceptions::NotFoundError &) {}
421  auto averagePos = computeAveragePosition(internalCat, *coaddWcs, weightKey);
422  return std::shared_ptr<CoaddPsf>(new CoaddPsf(internalCat, coaddWcs, averagePos));
423  }
424 
425  Factory(std::string const & name) : tbl::io::PersistableFactory(name) {}
426 
427 };
428 
429 namespace {
430 
431 std::string getCoaddPsfPersistenceName() { return "CoaddPsf"; }
432 
433 CoaddPsf::Factory registration(getCoaddPsfPersistenceName());
434 
435 } // anonymous
436 
437 std::string CoaddPsf::getPersistenceName() const { return getCoaddPsfPersistenceName(); }
438 
439 std::string CoaddPsf::getPythonModule() const { return "lsst.meas.algorithms"; }
440 
441 void CoaddPsf::write(OutputArchiveHandle & handle) const {
442  CoaddPsfPersistenceHelper const & keys1 = CoaddPsfPersistenceHelper::get();
443  tbl::BaseCatalog cat1 = handle.makeCatalog(keys1.schema);
444  PTR(tbl::BaseRecord) record1 = cat1.addNew();
445  record1->set(keys1.coaddWcs, handle.put(_coaddWcs));
446  record1->set(keys1.cacheSize, _warpingControl->getCacheSize());
447  record1->set(keys1.averagePosition, _averagePosition);
448  record1->set(keys1.warpingKernelName, _warpingKernelName);
449  handle.saveCatalog(cat1);
450  _catalog.writeToArchive(handle, false);
451 }
452 
454  afw::table::ExposureCatalog const & catalog,
455  PTR(afw::image::Wcs const) coaddWcs,
456  afw::geom::Point2D const & averagePosition,
457  std::string const & warpingKernelName,
458  int cacheSize
459 ) :
460  _catalog(catalog), _coaddWcs(coaddWcs), _weightKey(_catalog.getSchema()["weight"]),
461  _averagePosition(averagePosition), _warpingKernelName(warpingKernelName),
462  _warpingControl(new afw::math::WarpingControl(warpingKernelName, "", cacheSize))
463 {}
464 
465 }}} // namespace lsst::meas::algorithms
466 
467 
afw::geom::Box2I getOverallBBox(std::vector< boost::shared_ptr< afw::image::Image< double > >> const &imgVector)
Definition: CoaddPsf.cc:183
tbl::Key< double > weight
afw::geom::Box2I getBBox(int index)
Get the bounding box (in component image Pixel coordinates) of the component image at index...
Definition: CoaddPsf.cc:329
double getWeight(int index)
Get the weight of the component image at index.
Definition: CoaddPsf.cc:315
CoaddPsf(afw::table::ExposureCatalog const &catalog, afw::image::Wcs const &coaddWcs, std::string const &weightFieldName="weight", std::string const &warpingKernelName="lanczos3", int cacheSize=10000)
Main constructors for CoaddPsf.
Definition: CoaddPsf.cc:142
tbl::Key< int > cacheSize
Definition: CoaddPsf.cc:351
afw::table::RecordId getId(int index)
Get the exposure ID of the component image at index.
Definition: CoaddPsf.cc:322
Factory(std::string const &name)
Definition: CoaddPsf.cc:425
virtual std::string getPersistenceName() const
Definition: CoaddPsf.cc:437
double w
Definition: CoaddPsf.cc:57
CoaddPsf is the Psf derived to be used for non-PSF-matched Coadd images.
Definition: CoaddPsf.h:45
int getComponentCount() const
Return the number of component Psfs in this CoaddPsf.
Definition: CoaddPsf.cc:290
std::shared_ptr< tbl::io::Persistable > readV0(InputArchive const &archive, CatalogVector const &catalogs) const
Definition: CoaddPsf.cc:409
virtual boost::shared_ptr< tbl::io::Persistable > read(InputArchive const &archive, CatalogVector const &catalogs) const
Definition: CoaddPsf.cc:380
double wx
Definition: CoaddPsf.cc:55
tbl::Schema schema
tbl::Key< std::string > warpingKernelName
Definition: CoaddPsf.cc:353
void addToImage(boost::shared_ptr< afw::image::Image< double > > image, std::vector< boost::shared_ptr< afw::image::Image< double > >> const &imgVector, std::vector< double > const &weightVector)
Definition: CoaddPsf.cc:198
afw::table::Key< double > b
virtual afw::geom::Box2I doComputeBBox(afw::geom::Point2D const &position, afw::image::Color const &color) const
Definition: CoaddPsf.cc:224
virtual void write(OutputArchiveHandle &handle) const
Definition: CoaddPsf.cc:441
tbl::PointKey< double > averagePosition
Definition: CoaddPsf.cc:352
double wy
Definition: CoaddPsf.cc:56
virtual std::string getPythonModule() const
Definition: CoaddPsf.cc:439
A Psf class that maps an arbitrary Psf through a coordinate transformation.
Definition: WarpedPsf.h:49
tbl::Key< int > coaddWcs