lsst.jointcal  16.0-20-g17d57d5+3
ConstrainedPhotometryModel.cc
Go to the documentation of this file.
1 #include <map>
2 #include <limits>
3 #include <vector>
4 #include <string>
5 
6 #include "lsst/log/Log.h"
7 
8 #include "astshim.h"
9 #include "astshim/ChebyMap.h"
13 #include "lsst/jointcal/CcdImage.h"
16 
17 namespace lsst {
18 namespace jointcal {
19 
20 unsigned ConstrainedPhotometryModel::assignIndices(std::string const &whatToFit, unsigned firstIndex) {
21  unsigned index = firstIndex;
22  if (whatToFit.find("Model") == std::string::npos) {
23  LOGLS_WARN(_log, "assignIndices was called and Model is *not* in whatToFit");
24  return index;
25  }
26 
27  // If we got here, "Model" is definitely in whatToFit.
28  _fittingChips = (whatToFit.find("ModelChip") != std::string::npos);
29  _fittingVisits = (whatToFit.find("ModelVisit") != std::string::npos);
30  // If nothing more than "Model" is specified, it means fit everything.
31  if ((!_fittingChips) && (!_fittingVisits)) {
32  _fittingChips = _fittingVisits = true;
33  }
34 
35  if (_fittingChips) {
36  for (auto &idMapping : _chipMap) {
37  auto mapping = idMapping.second.get();
38  // Don't assign indices for fixed parameters.
39  if (mapping->isFixed()) continue;
40  mapping->setIndex(index);
41  index += mapping->getNpar();
42  }
43  }
44  if (_fittingVisits) {
45  for (auto &idMapping : _visitMap) {
46  auto mapping = idMapping.second.get();
47  mapping->setIndex(index);
48  index += mapping->getNpar();
49  }
50  }
51  for (auto &idMapping : _chipVisitMap) {
52  idMapping.second->setWhatToFit(_fittingChips, _fittingVisits);
53  }
54  return index;
55 }
56 
57 void ConstrainedPhotometryModel::offsetParams(Eigen::VectorXd const &delta) {
58  if (_fittingChips) {
59  for (auto &idMapping : _chipMap) {
60  auto mapping = idMapping.second.get();
61  // Don't offset indices for fixed parameters.
62  if (mapping->isFixed()) continue;
63  mapping->offsetParams(delta.segment(mapping->getIndex(), mapping->getNpar()));
64  }
65  }
66  if (_fittingVisits) {
67  for (auto &idMapping : _visitMap) {
68  auto mapping = idMapping.second.get();
69  mapping->offsetParams(delta.segment(mapping->getIndex(), mapping->getNpar()));
70  }
71  }
72 }
73 
75  for (auto &idMapping : _chipMap) {
76  idMapping.second.get()->freezeErrorTransform();
77  }
78  for (auto &idMapping : _visitMap) {
79  idMapping.second.get()->freezeErrorTransform();
80  }
81 }
82 
84  std::vector<unsigned> &indices) const {
85  auto mapping = findMapping(ccdImage);
86  mapping->getMappingIndices(indices);
87 }
88 
90  int total = 0;
91  for (auto &idMapping : _chipMap) {
92  total += idMapping.second->getNpar();
93  }
94  for (auto &idMapping : _visitMap) {
95  total += idMapping.second->getNpar();
96  }
97  return total;
98 }
99 
101  CcdImage const &ccdImage,
102  Eigen::VectorXd &derivatives) const {
103  auto mapping = findMapping(ccdImage);
104  mapping->computeParameterDerivatives(measuredStar, measuredStar.getInstFlux(), derivatives);
105 }
106 
107 namespace {
108 // Convert photoTransfo's way of storing Chebyshev coefficients into the format wanted by ChebyMap.
109 ndarray::Array<double, 2, 2> toChebyMapCoeffs(std::shared_ptr<PhotometryTransfoChebyshev> transfo) {
110  auto coeffs = transfo->getCoefficients();
111  // 4 x nPar: ChebyMap wants rows that look like (a_ij, 1, i, j) for out += a_ij*T_i(x)*T_j(y)
112  ndarray::Array<double, 2, 2> chebyCoeffs = allocate(ndarray::makeVector(transfo->getNpar(), 4));
113  Eigen::VectorXd::Index k = 0;
114  auto order = transfo->getOrder();
115  for (ndarray::Size j = 0; j <= order; ++j) {
116  ndarray::Size const iMax = order - j; // to save re-computing `i+j <= order` every inner step.
117  for (ndarray::Size i = 0; i <= iMax; ++i, ++k) {
118  chebyCoeffs[k][0] = coeffs[j][i];
119  chebyCoeffs[k][1] = 1;
120  chebyCoeffs[k][2] = i;
121  chebyCoeffs[k][3] = j;
122  }
123  }
124  return chebyCoeffs;
125 }
126 } // namespace
127 
129  for (auto &idMapping : _chipMap) {
130  idMapping.second->dump(stream);
131  stream << std::endl;
132  }
133  stream << std::endl;
134  for (auto &idMapping : _visitMap) {
135  idMapping.second->dump(stream);
136  stream << std::endl;
137  }
138 }
139 
141  auto idMapping = _chipVisitMap.find(ccdImage.getHashKey());
142  if (idMapping == _chipVisitMap.end())
144  "ConstrainedPhotometryModel cannot find CcdImage " + ccdImage.getName());
145  return idMapping->second.get();
146 }
147 
148 template <class ChipTransfo, class VisitTransfo, class ChipVisitMapping>
150  afw::geom::Box2D const &focalPlaneBBox, int visitOrder) {
151  // keep track of which chip we want to constrain (the one closest to the middle of the focal plane)
152  double minRadius2 = std::numeric_limits<double>::infinity();
153  CcdIdType constrainedChip = -1;
154 
155  // First initialize all visit and ccd transfos, before we make the ccdImage mappings.
156  for (auto const &ccdImage : ccdImageList) {
157  auto visit = ccdImage->getVisit();
158  auto chip = ccdImage->getCcdId();
159  auto visitPair = _visitMap.find(visit);
160  auto chipPair = _chipMap.find(chip);
161 
162  // If the chip is not in the map, add it, otherwise continue.
163  if (chipPair == _chipMap.end()) {
164  auto center = ccdImage->getDetector()->getCenter(afw::cameraGeom::FOCAL_PLANE);
165  double radius2 = std::pow(center.getX(), 2) + std::pow(center.getY(), 2);
166  if (radius2 < minRadius2) {
167  minRadius2 = radius2;
168  constrainedChip = chip;
169  }
170  auto photoCalib = ccdImage->getPhotoCalib();
171  // Use the single-frame processing calibration from the PhotoCalib as the default.
172  auto chipTransfo = std::make_unique<ChipTransfo>(initialChipCalibration(photoCalib));
173  _chipMap[chip] = std::make_shared<PhotometryMapping>(std::move(chipTransfo));
174  }
175  // If the visit is not in the map, add it, otherwise continue.
176  if (visitPair == _visitMap.end()) {
177  auto visitTransfo = std::make_unique<VisitTransfo>(visitOrder, focalPlaneBBox);
178  _visitMap[visit] = std::make_shared<PhotometryMapping>(std::move(visitTransfo));
179  }
180  }
181 
182  // Fix one chip mapping, to remove the degeneracy from the system.
183  _chipMap.at(constrainedChip)->setFixed(true);
184 
185  // Now create the ccdImage mappings, which are combinations of the chip/visit mappings above.
186  for (auto const &ccdImage : ccdImageList) {
187  auto visit = ccdImage->getVisit();
188  auto chip = ccdImage->getCcdId();
189  _chipVisitMap.emplace(ccdImage->getHashKey(),
190  std::make_unique<ChipVisitMapping>(_chipMap[chip], _visitMap[visit]));
191  }
192  LOGLS_INFO(_log, "Got " << _chipMap.size() << " chip mappings and " << _visitMap.size()
193  << " visit mappings; holding chip " << constrainedChip << " fixed ("
194  << getTotalParameters() << " total parameters).");
195  LOGLS_DEBUG(_log, "CcdImage map has " << _chipVisitMap.size() << " mappings, with "
196  << _chipVisitMap.bucket_count() << " buckets and a load factor of "
198 }
199 
200 // ConstrainedFluxModel methods
201 
203  MeasuredStar const &measuredStar) const {
204  return transform(ccdImage, measuredStar) - measuredStar.getFittedStar()->getFlux();
205 }
206 
207 double ConstrainedFluxModel::transform(CcdImage const &ccdImage, MeasuredStar const &measuredStar) const {
208  auto mapping = findMapping(ccdImage);
209  return mapping->transform(measuredStar, measuredStar.getInstFlux());
210 }
211 
213  MeasuredStar const &measuredStar) const {
214  auto mapping = findMapping(ccdImage);
215  double tempErr = tweakFluxError(measuredStar);
216  return mapping->transformError(measuredStar, measuredStar.getInstFlux(), tempErr);
217 }
218 
220  auto oldPhotoCalib = ccdImage.getPhotoCalib();
221  auto detector = ccdImage.getDetector();
222  auto ccdBBox = detector->getBBox();
223  ChipVisitPhotometryMapping *mapping = dynamic_cast<ChipVisitPhotometryMapping *>(findMapping(ccdImage));
224  // There should be no way in which we can get to this point and not have a ChipVisitMapping,
225  // so blow up if we don't.
226  assert(mapping != nullptr);
227  auto pixToFocal = detector->getTransform(afw::cameraGeom::PIXELS, afw::cameraGeom::FOCAL_PLANE);
228  // We know it's a Chebyshev transfo because we created it as such, so blow up if it's not.
229  auto visitTransfo =
231  assert(visitTransfo != nullptr);
232  auto focalBBox = visitTransfo->getBBox();
233 
234  // Unravel our chebyshev coefficients to build an astshim::ChebyMap.
235  auto coeff_f = toChebyMapCoeffs(
236  std::dynamic_pointer_cast<PhotometryTransfoChebyshev>(mapping->getVisitMapping()->getTransfo()));
237  // Bounds are the bbox
238  std::vector<double> lowerBound = {focalBBox.getMinX(), focalBBox.getMinY()};
239  std::vector<double> upperBound = {focalBBox.getMaxX(), focalBBox.getMaxY()};
240 
241  afw::geom::TransformPoint2ToGeneric chebyTransform(ast::ChebyMap(coeff_f, 1, lowerBound, upperBound));
242 
243  // The chip part is easy: zoom map with the single value as the "zoom" factor.
245  ast::ZoomMap(1, mapping->getChipMapping()->getParameters()[0]));
246 
247  // Now stitch them all together.
248  auto transform = pixToFocal->then(chebyTransform)->then(zoomTransform);
249  // NOTE: TransformBoundedField does not yet implement mean(), so we have to compute it here.
250  // TODO: restore this calculation as part of DM-16305
251  // double mean = mapping->getChipMapping()->getParameters()[0] * visitTransfo->mean(ccdBBoxInFocal);
252  auto boundedField = std::make_shared<afw::math::TransformBoundedField>(ccdBBox, *transform);
253  return std::make_shared<afw::image::PhotoCalib>(oldPhotoCalib->getCalibrationMean(),
254  oldPhotoCalib->getCalibrationErr(), boundedField, false);
255 }
256 
257 // ConstrainedMagnitudeModel methods
258 
260  MeasuredStar const &measuredStar) const {
261  return transform(ccdImage, measuredStar) - measuredStar.getFittedStar()->getMag();
262 }
263 
265  MeasuredStar const &measuredStar) const {
266  auto mapping = findMapping(ccdImage);
267  return mapping->transform(measuredStar, measuredStar.getInstMag());
268 }
269 
271  MeasuredStar const &measuredStar) const {
272  auto mapping = findMapping(ccdImage);
273  double tempErr = tweakFluxError(measuredStar);
274  return mapping->transformError(measuredStar, measuredStar.getInstFlux(), tempErr);
275 }
276 
278  CcdImage const &ccdImage) const {
279  auto oldPhotoCalib = ccdImage.getPhotoCalib();
280  auto detector = ccdImage.getDetector();
281  auto ccdBBox = detector->getBBox();
282  ChipVisitPhotometryMapping *mapping = dynamic_cast<ChipVisitPhotometryMapping *>(findMapping(ccdImage));
283  // There should be no way in which we can get to this point and not have a ChipVisitMapping,
284  // so blow up if we don't.
285  assert(mapping != nullptr);
286  auto pixToFocal = detector->getTransform(afw::cameraGeom::PIXELS, afw::cameraGeom::FOCAL_PLANE);
287  // We know it's a Chebyshev transfo because we created it as such, so blow up if it's not.
288  auto visitTransfo =
290  assert(visitTransfo != nullptr);
291  auto focalBBox = visitTransfo->getBBox();
292 
293  // Unravel our chebyshev coefficients to build an astshim::ChebyMap.
294  auto coeff_f = toChebyMapCoeffs(
295  std::dynamic_pointer_cast<PhotometryTransfoChebyshev>(mapping->getVisitMapping()->getTransfo()));
296  // Bounds are the bbox
297  std::vector<double> lowerBound = {focalBBox.getMinX(), focalBBox.getMinY()};
298  std::vector<double> upperBound = {focalBBox.getMaxX(), focalBBox.getMaxY()};
299 
300  afw::geom::TransformPoint2ToGeneric chebyTransform(ast::ChebyMap(coeff_f, 1, lowerBound, upperBound));
301 
302  using namespace std::string_literals; // for operator""s to convert string literal->std::string
304  ast::MathMap(1, 1, {"y=pow(10.0,x/-2.5)"s}, {"x=-2.5*log10(y)"s}));
305 
306  // The chip part is easy: zoom map with the value (converted to a flux) as the "zoom" factor.
307  double calibrationMean = std::pow(10, mapping->getChipMapping()->getParameters()[0] / -2.5);
309  ast::ZoomMap(1, calibrationMean));
310 
311  // Now stitch them all together.
312  auto transform = pixToFocal->then(chebyTransform)->then(logTransform)->then(zoomTransform);
313  // NOTE: TransformBoundedField does not yet implement mean(), so we have to compute it here.
314  // TODO: restore this calculation as part of DM-16305
315  // double mean = calibrationMean * visitTransfo->mean(ccdBBoxInFocal);
316  auto boundedField = std::make_shared<afw::math::TransformBoundedField>(ccdBBox, *transform);
317  return std::make_shared<afw::image::PhotoCalib>(oldPhotoCalib->getCalibrationMean(),
318  oldPhotoCalib->getCalibrationErr(), boundedField, false);
319 }
320 
321 // explicit instantiation of templated function, so pybind11 can
324  afw::geom::Box2D const &, int);
327  CcdImageList const &, afw::geom::Box2D const &, int);
328 
329 } // namespace jointcal
330 } // namespace lsst
Photometric offset independent of position, defined as -2.5 * log(flux / fluxMag0).
Relates transfo(s) to their position in the fitting matrix and allows interaction with the transfo(s)...
double transformError(CcdImage const &ccdImage, MeasuredStar const &measuredStar) const override
Return the on-sky transformed flux uncertainty for measuredStar on ccdImage.
std::string getName() const
Return the _name that identifies this ccdImage.
Definition: CcdImage.h:56
CameraSysPrefix const PIXELS
std::shared_ptr< PhotometryMapping > getChipMapping() const
table::Key< int > detector
T endl(T... args)
T bucket_count(T... args)
T end(T... args)
nth-order 2d Chebyshev photometry transfo, times the input flux.
T load_factor(T... args)
Photometric offset independent of position, defined as (fluxMag0)^-1.
table::Key< double > calibrationMean
#define LOGLS_INFO(logger, message)
STL class.
unsigned assignIndices(std::string const &whatToFit, unsigned firstIndex) override
Assign indices in the full matrix to the parameters being fit in the mappings, starting at firstIndex...
T at(T... args)
std::shared_ptr< PhotometryMapping > getVisitMapping() const
std::shared_ptr< afw::image::PhotoCalib > toPhotoCalib(CcdImage const &ccdImage) const override
Return the mapping of ccdImage represented as a PhotoCalib.
int getTotalParameters() const override
Return the total number of parameters in this model.
double computeResidual(CcdImage const &ccdImage, MeasuredStar const &measuredStar) const override
Compute the residual between the model applied to a star and its associated fittedStar.
std::shared_ptr< afw::image::PhotoCalib > getPhotoCalib() const
Return the exposure&#39;s photometric calibration.
Definition: CcdImage.h:137
Class for a simple mapping implementing a generic Gtransfo.
PhotometryMappingBase * findMapping(CcdImage const &ccdImage) const override
Return a pointer to the mapping associated with this ccdImage.
objects measured on actual images.
Definition: MeasuredStar.h:19
T dynamic_pointer_cast(T... args)
void initialize(CcdImageList const &ccdImageList, afw::geom::Box2D const &focalPlaneBBox, int visitOrder)
Initialize the chip, visit, and chipVisit mappings by creating appropriate transfos and mappings...
T infinity(T... args)
void computeParameterDerivatives(MeasuredStar const &measuredStar, CcdImage const &ccdImage, Eigen::VectorXd &derivatives) const override
Compute the parametric derivatives of this model.
T move(T... args)
double transform(CcdImage const &ccdImage, MeasuredStar const &measuredStar) const override
Return the on-sky transformed flux for measuredStar on ccdImage.
T find(T... args)
T size(T... args)
void dump(std::ostream &stream=std::cout) const override
Dump the contents of the transfos, for debugging.
#define LSST_EXCEPT(type,...)
double tweakFluxError(jointcal::MeasuredStar const &measuredStar) const
Add a fraction of the instrumental flux to the instrumental flux error, in quadrature.
LOG_LOGGER _log
lsst.logging instance, to be created by a subclass so that messages have consistent name...
void offsetParams(Eigen::VectorXd const &delta) override
Offset the parameters by the provided amounts (by -delta).
T pow(T... args)
#define LOGLS_DEBUG(logger, message)
T emplace(T... args)
double computeResidual(CcdImage const &ccdImage, MeasuredStar const &measuredStar) const override
Compute the residual between the model applied to a star and its associated fittedStar.
nth-order 2d Chebyshev photometry transfo.
CcdImageKey getHashKey() const
Definition: CcdImage.h:128
virtual double initialChipCalibration(std::shared_ptr< afw::image::PhotoCalib const > photoCalib)=0
Return the initial calibration to use from this photoCalib.
std::shared_ptr< afw::cameraGeom::Detector > getDetector() const
Definition: CcdImage.h:126
Handler of an actual image from a single CCD.
Definition: CcdImage.h:41
std::shared_ptr< afw::image::PhotoCalib > toPhotoCalib(CcdImage const &ccdImage) const override
Return the mapping of ccdImage represented as a PhotoCalib.
double transform(CcdImage const &ccdImage, MeasuredStar const &measuredStar) const override
Return the on-sky transformed flux for measuredStar on ccdImage.
nth-order 2d Chebyshev photometry transfo, plus the input flux.
std::shared_ptr< FittedStar > getFittedStar() const
Definition: MeasuredStar.h:85
CameraSys const FOCAL_PLANE
STL class.
void freezeErrorTransform() override
Once this routine has been called, the error transform is not modified by offsetParams().
virtual double transform(CcdImage const &ccdImage, MeasuredStar const &measuredStar) const =0
Return the on-sky transformed flux for measuredStar on ccdImage.
void getMappingIndices(CcdImage const &ccdImage, std::vector< unsigned > &indices) const override
Get how this set of parameters (of length Npar()) map into the "grand" fit.
A two-level photometric transform: one for the ccd and one for the visit.
#define LOGLS_WARN(logger, message)
double transformError(CcdImage const &ccdImage, MeasuredStar const &measuredStar) const override
Return the on-sky transformed flux uncertainty for measuredStar on ccdImage.