16 LOG_LOGGER _log =
LOG_GET(
"jointcal.ConstrainedPhotometryModel");
23 afw::geom::Box2D
const &focalPlaneBBox,
int visitOrder)
24 : _fittingChips(false), _fittingVisits(false) {
30 for (
auto const &
ccdImage : ccdImageList) {
33 auto visitPair = _visitMap.
find(visit);
34 auto chipPair = _chipMap.
find(chip);
37 if (chipPair == _chipMap.
end()) {
40 if (radius2 < minRadius2) {
42 constrainedChip = chip;
44 auto photoCalib =
ccdImage->getPhotoCalib();
47 std::make_shared<PhotometryTransfoSpatiallyInvariant>(photoCalib->getCalibrationMean());
48 _chipMap[chip] = std::make_unique<PhotometryMapping>(
std::move(chipTransfo));
51 if (visitPair == _visitMap.
end()) {
52 auto visitTransfo = std::make_shared<PhotometryTransfoChebyshev>(visitOrder, focalPlaneBBox);
53 _visitMap[visit] = std::make_unique<PhotometryMapping>(
std::move(visitTransfo));
58 _chipMap.
at(constrainedChip)->setFixed(
true);
63 for (
auto const &
ccdImage : ccdImageList) {
67 std::make_unique<ChipVisitPhotometryMapping>(_chipMap[chip], _visitMap[visit]));
69 LOGLS_INFO(_log,
"Got " << _chipMap.
size() <<
" chip mappings and " << _visitMap.
size()
70 <<
" visit mappings; holding chip " << constrainedChip <<
" fixed (" 72 LOGLS_DEBUG(_log,
"CcdImage map has " << _chipVisitMap.
size() <<
" mappings, with " 73 << _chipVisitMap.
bucket_count() <<
" buckets and a load factor of " 78 unsigned index = firstIndex;
79 if (whatToFit.
find(
"Model") == std::string::npos) {
80 LOGLS_WARN(_log,
"assignIndices was called and Model is *not* in whatToFit");
85 _fittingChips = (whatToFit.
find(
"ModelChip") != std::string::npos);
86 _fittingVisits = (whatToFit.
find(
"ModelVisit") != std::string::npos);
88 if ((!_fittingChips) && (!_fittingVisits)) {
89 _fittingChips = _fittingVisits =
true;
93 for (
auto &idMapping : _chipMap) {
94 auto mapping = idMapping.second.get();
96 if (mapping->isFixed())
continue;
97 mapping->setIndex(index);
98 index += mapping->getNpar();
101 if (_fittingVisits) {
102 for (
auto &idMapping : _visitMap) {
103 auto mapping = idMapping.second.get();
104 mapping->setIndex(index);
105 index += mapping->getNpar();
108 for (
auto &idMapping : _chipVisitMap) {
109 idMapping.second->setWhatToFit(_fittingChips, _fittingVisits);
116 for (
auto &idMapping : _chipMap) {
117 auto mapping = idMapping.second.get();
119 if (mapping->isFixed())
continue;
120 mapping->offsetParams(delta.segment(mapping->getIndex(), mapping->getNpar()));
123 if (_fittingVisits) {
124 for (
auto &idMapping : _visitMap) {
125 auto mapping = idMapping.second.get();
126 mapping->offsetParams(delta.segment(mapping->getIndex(), mapping->getNpar()));
132 double instFlux)
const {
133 auto mapping = findMapping(ccdImage);
134 return mapping->transform(measuredStar, instFlux);
138 double instFluxErr)
const {
139 auto mapping = findMapping(ccdImage);
140 return mapping->transformError(measuredStar, instFluxErr);
144 for (
auto &idMapping : _chipMap) {
145 idMapping.second.get()->freezeErrorTransform();
147 for (
auto &idMapping : _visitMap) {
148 idMapping.second.get()->freezeErrorTransform();
154 auto mapping = findMapping(ccdImage);
155 mapping->getMappingIndices(indices);
160 for (
auto &idMapping : _chipMap) {
161 total += idMapping.second->getNpar();
163 for (
auto &idMapping : _visitMap) {
164 total += idMapping.second->getNpar();
171 Eigen::VectorXd &derivatives)
const {
172 auto mapping = findMapping(ccdImage);
173 mapping->computeParameterDerivatives(measuredStar, measuredStar.
getInstFlux(), derivatives);
179 auto coeffs = transfo->getCoefficients();
181 ndarray::Array<double, 2, 2> chebyCoeffs = allocate(ndarray::makeVector(transfo->getNpar(), 4));
182 Eigen::VectorXd::Index k = 0;
183 auto order = transfo->getOrder();
184 for (ndarray::Size j = 0; j <= order; ++j) {
185 ndarray::Size
const iMax = order - j;
186 for (ndarray::Size i = 0; i <= iMax; ++i, ++k) {
187 chebyCoeffs[k][0] = coeffs[j][i];
188 chebyCoeffs[k][1] = 1;
189 chebyCoeffs[k][2] = i;
190 chebyCoeffs[k][3] = j;
201 auto ccdBBox = detector->getBBox();
205 assert(mapping !=
nullptr);
210 assert(visitTransfo !=
nullptr);
211 auto focalBBox = visitTransfo->getBBox();
214 auto coeff_f = toChebyMapCoeffs(
215 std::dynamic_pointer_cast<PhotometryTransfoChebyshev>(mapping->
getVisitMapping()->getTransfo()));
227 auto transform = pixToFocal->then(chebyTransform)->then(zoomTransform);
229 double mean = mapping->
getChipMapping()->getParameters()[0] * visitTransfo->mean();
230 auto boundedField = std::make_shared<afw::math::TransformBoundedField>(ccdBBox, *
transform);
231 return std::make_shared<afw::image::PhotoCalib>(mean, oldPhotoCalib->getCalibrationErr(), boundedField,
236 for (
auto &idMapping : _chipMap) {
237 idMapping.second->dump(stream);
241 for (
auto &idMapping : _visitMap) {
242 idMapping.second->dump(stream);
249 if (idMapping == _chipVisitMap.
end())
251 "ConstrainedPhotometryModel cannot find CcdImage " + ccdImage.
getName());
252 return idMapping->second.get();
#define LOGLS_WARN(logger, message)
Relates transfo(s) to their position in the fitting matrix and allows interaction with the transfo(s)...
std::string getName() const
Return the _name that identifies this ccdImage.
CameraSysPrefix const PIXELS
std::shared_ptr< PhotometryMapping > getChipMapping() const
T bucket_count(T... args)
ConstrainedPhotometryModel(CcdImageList const &ccdImageList, afw::geom::Box2D const &focalPlaneBBox, int visitOrder=7)
Construct a constrained photometry model.
double transform(CcdImage const &ccdImage, MeasuredStar const &measuredStar, double instFlux) const override
Return the on-sky transformed flux for measuredStar on ccdImage.
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...
std::shared_ptr< PhotometryMapping > getVisitMapping() const
int getTotalParameters() const override
Return the total number of parameters in this model.
std::shared_ptr< afw::image::PhotoCalib > getPhotoCalib() const
Return the exposure's photometric calibration.
#define LOGLS_DEBUG(logger, message)
Class for a simple mapping implementing a generic Gtransfo.
double getInstFlux() const
objects measured on actual images.
T dynamic_pointer_cast(T... args)
void computeParameterDerivatives(MeasuredStar const &measuredStar, CcdImage const &ccdImage, Eigen::VectorXd &derivatives) const override
Compute the parametric derivatives of this model.
#define LOGLS_INFO(logger, message)
void dump(std::ostream &stream=std::cout) const override
Dump the contents of the transfos, for debugging.
#define LSST_EXCEPT(type,...)
void offsetParams(Eigen::VectorXd const &delta) override
Offset the parameters by the provided amounts.
double transformError(CcdImage const &ccdImage, MeasuredStar const &measuredStar, double instFluxErr) const override
Return the on-sky transformed flux uncertainty for measuredStar on ccdImage.
nth-order 2d Chebyshev photometry transfo.
std::shared_ptr< afw::image::PhotoCalib > toPhotoCalib(CcdImage const &ccdImage) const override
Return the mapping of ccdImage represented as a PhotoCalib.
CcdImageKey getHashKey() const
std::shared_ptr< afw::cameraGeom::Detector > getDetector() const
Handler of an actual image from a single CCD.
CameraSys const FOCAL_PLANE
void freezeErrorTransform() override
Once this routine has been called, the error transform is not modified by offsetParams().
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.