lsst.jointcal  16.0-18-gdf247dd+1
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 {
18 LOG_LOGGER _log = LOG_GET("jointcal.ConstrainedPhotometryModel");
19 }
20 
21 namespace lsst {
22 namespace jointcal {
23 
24 unsigned ConstrainedPhotometryModel::assignIndices(std::string const &whatToFit, unsigned firstIndex) {
25  unsigned index = firstIndex;
26  if (whatToFit.find("Model") == std::string::npos) {
27  LOGLS_WARN(_log, "assignIndices was called and Model is *not* in whatToFit");
28  return index;
29  }
30 
31  // If we got here, "Model" is definitely in whatToFit.
32  _fittingChips = (whatToFit.find("ModelChip") != std::string::npos);
33  _fittingVisits = (whatToFit.find("ModelVisit") != std::string::npos);
34  // If nothing more than "Model" is specified, it means fit everything.
35  if ((!_fittingChips) && (!_fittingVisits)) {
36  _fittingChips = _fittingVisits = true;
37  }
38 
39  if (_fittingChips) {
40  for (auto &idMapping : _chipMap) {
41  auto mapping = idMapping.second.get();
42  // Don't assign indices for fixed parameters.
43  if (mapping->isFixed()) continue;
44  mapping->setIndex(index);
45  index += mapping->getNpar();
46  }
47  }
48  if (_fittingVisits) {
49  for (auto &idMapping : _visitMap) {
50  auto mapping = idMapping.second.get();
51  mapping->setIndex(index);
52  index += mapping->getNpar();
53  }
54  }
55  for (auto &idMapping : _chipVisitMap) {
56  idMapping.second->setWhatToFit(_fittingChips, _fittingVisits);
57  }
58  return index;
59 }
60 
61 void ConstrainedPhotometryModel::offsetParams(Eigen::VectorXd const &delta) {
62  if (_fittingChips) {
63  for (auto &idMapping : _chipMap) {
64  auto mapping = idMapping.second.get();
65  // Don't offset indices for fixed parameters.
66  if (mapping->isFixed()) continue;
67  mapping->offsetParams(delta.segment(mapping->getIndex(), mapping->getNpar()));
68  }
69  }
70  if (_fittingVisits) {
71  for (auto &idMapping : _visitMap) {
72  auto mapping = idMapping.second.get();
73  mapping->offsetParams(delta.segment(mapping->getIndex(), mapping->getNpar()));
74  }
75  }
76 }
77 
79  for (auto &idMapping : _chipMap) {
80  idMapping.second.get()->freezeErrorTransform();
81  }
82  for (auto &idMapping : _visitMap) {
83  idMapping.second.get()->freezeErrorTransform();
84  }
85 }
86 
88  std::vector<unsigned> &indices) const {
89  auto mapping = findMapping(ccdImage);
90  mapping->getMappingIndices(indices);
91 }
92 
94  int total = 0;
95  for (auto &idMapping : _chipMap) {
96  total += idMapping.second->getNpar();
97  }
98  for (auto &idMapping : _visitMap) {
99  total += idMapping.second->getNpar();
100  }
101  return total;
102 }
103 
105  CcdImage const &ccdImage,
106  Eigen::VectorXd &derivatives) const {
107  auto mapping = findMapping(ccdImage);
108  mapping->computeParameterDerivatives(measuredStar, measuredStar.getInstFlux(), derivatives);
109 }
110 
111 namespace {
112 // Convert photoTransfo's way of storing Chebyshev coefficients into the format wanted by ChebyMap.
113 ndarray::Array<double, 2, 2> toChebyMapCoeffs(std::shared_ptr<PhotometryTransfoChebyshev> transfo) {
114  auto coeffs = transfo->getCoefficients();
115  // 4 x nPar: ChebyMap wants rows that look like (a_ij, 1, i, j) for out += a_ij*T_i(x)*T_j(y)
116  ndarray::Array<double, 2, 2> chebyCoeffs = allocate(ndarray::makeVector(transfo->getNpar(), 4));
117  Eigen::VectorXd::Index k = 0;
118  auto order = transfo->getOrder();
119  for (ndarray::Size j = 0; j <= order; ++j) {
120  ndarray::Size const iMax = order - j; // to save re-computing `i+j <= order` every inner step.
121  for (ndarray::Size i = 0; i <= iMax; ++i, ++k) {
122  chebyCoeffs[k][0] = coeffs[j][i];
123  chebyCoeffs[k][1] = 1;
124  chebyCoeffs[k][2] = i;
125  chebyCoeffs[k][3] = j;
126  }
127  }
128  return chebyCoeffs;
129 }
130 } // namespace
131 
133  for (auto &idMapping : _chipMap) {
134  idMapping.second->dump(stream);
135  stream << std::endl;
136  }
137  stream << std::endl;
138  for (auto &idMapping : _visitMap) {
139  idMapping.second->dump(stream);
140  stream << std::endl;
141  }
142 }
143 
145  auto idMapping = _chipVisitMap.find(ccdImage.getHashKey());
146  if (idMapping == _chipVisitMap.end())
148  "ConstrainedPhotometryModel cannot find CcdImage " + ccdImage.getName());
149  return idMapping->second.get();
150 }
151 
152 template <class ChipTransfo, class VisitTransfo, class ChipVisitMapping>
154  afw::geom::Box2D const &focalPlaneBBox, int visitOrder) {
155  // keep track of which chip we want to constrain (the one closest to the middle of the focal plane)
156  double minRadius2 = std::numeric_limits<double>::infinity();
157  CcdIdType constrainedChip = -1;
158 
159  // First initialize all visit and ccd transfos, before we make the ccdImage mappings.
160  for (auto const &ccdImage : ccdImageList) {
161  auto visit = ccdImage->getVisit();
162  auto chip = ccdImage->getCcdId();
163  auto visitPair = _visitMap.find(visit);
164  auto chipPair = _chipMap.find(chip);
165 
166  // If the chip is not in the map, add it, otherwise continue.
167  if (chipPair == _chipMap.end()) {
168  auto center = ccdImage->getDetector()->getCenter(afw::cameraGeom::FOCAL_PLANE);
169  double radius2 = std::pow(center.getX(), 2) + std::pow(center.getY(), 2);
170  if (radius2 < minRadius2) {
171  minRadius2 = radius2;
172  constrainedChip = chip;
173  }
174  auto photoCalib = ccdImage->getPhotoCalib();
175  // Use the single-frame processing calibration from the PhotoCalib as the default.
176  auto chipTransfo = std::make_unique<ChipTransfo>(initialChipCalibration(photoCalib));
177  _chipMap[chip] = std::make_shared<PhotometryMapping>(std::move(chipTransfo));
178  }
179  // If the visit is not in the map, add it, otherwise continue.
180  if (visitPair == _visitMap.end()) {
181  auto visitTransfo = std::make_unique<VisitTransfo>(visitOrder, focalPlaneBBox);
182  _visitMap[visit] = std::make_shared<PhotometryMapping>(std::move(visitTransfo));
183  }
184  }
185 
186  // Fix one chip mapping, to remove the degeneracy from the system.
187  _chipMap.at(constrainedChip)->setFixed(true);
188 
189  // Now create the ccdImage mappings, which are combinations of the chip/visit mappings above.
190  for (auto const &ccdImage : ccdImageList) {
191  auto visit = ccdImage->getVisit();
192  auto chip = ccdImage->getCcdId();
193  _chipVisitMap.emplace(ccdImage->getHashKey(),
194  std::make_unique<ChipVisitMapping>(_chipMap[chip], _visitMap[visit]));
195  }
196  LOGLS_INFO(_log, "Got " << _chipMap.size() << " chip mappings and " << _visitMap.size()
197  << " visit mappings; holding chip " << constrainedChip << " fixed ("
198  << getTotalParameters() << " total parameters).");
199  LOGLS_DEBUG(_log, "CcdImage map has " << _chipVisitMap.size() << " mappings, with "
200  << _chipVisitMap.bucket_count() << " buckets and a load factor of "
202 }
203 
204 // ConstrainedFluxModel methods
205 
207  MeasuredStar const &measuredStar) const {
208  return transform(ccdImage, measuredStar) - measuredStar.getFittedStar()->getFlux();
209 }
210 
211 double ConstrainedFluxModel::transform(CcdImage const &ccdImage, MeasuredStar const &measuredStar) const {
212  auto mapping = findMapping(ccdImage);
213  return mapping->transform(measuredStar, measuredStar.getInstFlux());
214 }
215 
217  MeasuredStar const &measuredStar) const {
218  auto mapping = findMapping(ccdImage);
219  double tempErr = tweakFluxError(measuredStar);
220  return mapping->transformError(measuredStar, measuredStar.getInstFlux(), tempErr);
221 }
222 
224  auto oldPhotoCalib = ccdImage.getPhotoCalib();
225  auto detector = ccdImage.getDetector();
226  auto ccdBBox = detector->getBBox();
227  ChipVisitPhotometryMapping *mapping = dynamic_cast<ChipVisitPhotometryMapping *>(findMapping(ccdImage));
228  // There should be no way in which we can get to this point and not have a ChipVisitMapping,
229  // so blow up if we don't.
230  assert(mapping != nullptr);
231  auto pixToFocal = detector->getTransform(afw::cameraGeom::PIXELS, afw::cameraGeom::FOCAL_PLANE);
232  // We know it's a Chebyshev transfo because we created it as such, so blow up if it's not.
233  auto visitTransfo =
235  assert(visitTransfo != nullptr);
236  auto focalBBox = visitTransfo->getBBox();
237 
238  // Unravel our chebyshev coefficients to build an astshim::ChebyMap.
239  auto coeff_f = toChebyMapCoeffs(
240  std::dynamic_pointer_cast<PhotometryTransfoChebyshev>(mapping->getVisitMapping()->getTransfo()));
241  // Bounds are the bbox
242  std::vector<double> lowerBound = {focalBBox.getMinX(), focalBBox.getMinY()};
243  std::vector<double> upperBound = {focalBBox.getMaxX(), focalBBox.getMaxY()};
244 
245  afw::geom::TransformPoint2ToGeneric chebyTransform(ast::ChebyMap(coeff_f, 1, lowerBound, upperBound));
246 
247  // The chip part is easy: zoom map with the single value as the "zoom" factor.
249  ast::ZoomMap(1, mapping->getChipMapping()->getParameters()[0]));
250 
251  // Now stitch them all together.
252  auto transform = pixToFocal->then(chebyTransform)->then(zoomTransform);
253  // NOTE: TransformBoundedField does not yet implement mean(), so we have to compute it here.
254  double mean = mapping->getChipMapping()->getParameters()[0] * visitTransfo->mean();
255  auto boundedField = std::make_shared<afw::math::TransformBoundedField>(ccdBBox, *transform);
256  return std::make_shared<afw::image::PhotoCalib>(mean, oldPhotoCalib->getCalibrationErr(), boundedField,
257  false);
258 }
259 
260 // ConstrainedMagnitudeModel methods
261 
263  MeasuredStar const &measuredStar) const {
264  return transform(ccdImage, measuredStar) - measuredStar.getFittedStar()->getMag();
265 }
266 
268  MeasuredStar const &measuredStar) const {
269  auto mapping = findMapping(ccdImage);
270  return mapping->transform(measuredStar, measuredStar.getInstMag());
271 }
272 
274  MeasuredStar const &measuredStar) const {
275  auto mapping = findMapping(ccdImage);
276  double tempErr = tweakFluxError(measuredStar);
277  return mapping->transformError(measuredStar, measuredStar.getInstFlux(), tempErr);
278 }
279 
281  CcdImage const &ccdImage) const {
282  auto oldPhotoCalib = ccdImage.getPhotoCalib();
283  auto detector = ccdImage.getDetector();
284  auto ccdBBox = detector->getBBox();
285  ChipVisitPhotometryMapping *mapping = dynamic_cast<ChipVisitPhotometryMapping *>(findMapping(ccdImage));
286  // There should be no way in which we can get to this point and not have a ChipVisitMapping,
287  // so blow up if we don't.
288  assert(mapping != nullptr);
289  auto pixToFocal = detector->getTransform(afw::cameraGeom::PIXELS, afw::cameraGeom::FOCAL_PLANE);
290  // We know it's a Chebyshev transfo because we created it as such, so blow up if it's not.
291  auto visitTransfo =
293  assert(visitTransfo != nullptr);
294  auto focalBBox = visitTransfo->getBBox();
295 
296  // Unravel our chebyshev coefficients to build an astshim::ChebyMap.
297  auto coeff_f = toChebyMapCoeffs(
298  std::dynamic_pointer_cast<PhotometryTransfoChebyshev>(mapping->getVisitMapping()->getTransfo()));
299  // Bounds are the bbox
300  std::vector<double> lowerBound = {focalBBox.getMinX(), focalBBox.getMinY()};
301  std::vector<double> upperBound = {focalBBox.getMaxX(), focalBBox.getMaxY()};
302 
303  afw::geom::TransformPoint2ToGeneric chebyTransform(ast::ChebyMap(coeff_f, 1, lowerBound, upperBound));
304 
305  using namespace std::string_literals; // for operator""s to convert string literal->std::string
307  ast::MathMap(1, 1, {"y=pow(10.0,x/-2.5)"s}, {"x=-2.5*log10(y)"s}));
308 
309  // The chip part is easy: zoom map with the value (converted to a flux) as the "zoom" factor.
310  double calibrationMean = std::pow(10, mapping->getChipMapping()->getParameters()[0] / -2.5);
312  ast::ZoomMap(1, calibrationMean));
313 
314  // Now stitch them all together.
315  auto transform = pixToFocal->then(chebyTransform)->then(logTransform)->then(zoomTransform);
316  // NOTE: TransformBoundedField does not yet implement mean(), so we have to compute it here.
317  double mean = calibrationMean * visitTransfo->mean();
318  auto boundedField = std::make_shared<afw::math::TransformBoundedField>(ccdBBox, *transform);
319  return std::make_shared<afw::image::PhotoCalib>(mean, oldPhotoCalib->getCalibrationErr(), boundedField,
320  false);
321 }
322 
323 // explicit instantiation of templated function, so pybind11 can
326  afw::geom::Box2D const &, int);
329  CcdImageList const &, afw::geom::Box2D const &, int);
330 
331 } // namespace jointcal
332 } // 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
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.
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.
std::shared_ptr< FittedStar > getFittedStar() const
Definition: MeasuredStar.h:85
CameraSys const FOCAL_PLANE
STL class.
#define LOG_GET(logger)
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.