18 LOG_LOGGER _log =
LOG_GET(
"jointcal.ConstrainedPhotometryModel");
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");
32 _fittingChips = (whatToFit.
find(
"ModelChip") != std::string::npos);
33 _fittingVisits = (whatToFit.
find(
"ModelVisit") != std::string::npos);
35 if ((!_fittingChips) && (!_fittingVisits)) {
36 _fittingChips = _fittingVisits =
true;
41 auto mapping = idMapping.second.get();
43 if (mapping->isFixed())
continue;
44 mapping->setIndex(index);
45 index += mapping->getNpar();
50 auto mapping = idMapping.second.get();
51 mapping->setIndex(index);
52 index += mapping->getNpar();
56 idMapping.second->setWhatToFit(_fittingChips, _fittingVisits);
64 auto mapping = idMapping.second.get();
66 if (mapping->isFixed())
continue;
67 mapping->offsetParams(delta.segment(mapping->getIndex(), mapping->getNpar()));
72 auto mapping = idMapping.second.get();
73 mapping->offsetParams(delta.segment(mapping->getIndex(), mapping->getNpar()));
80 idMapping.second.get()->freezeErrorTransform();
83 idMapping.second.get()->freezeErrorTransform();
90 mapping->getMappingIndices(indices);
96 total += idMapping.second->getNpar();
99 total += idMapping.second->getNpar();
106 Eigen::VectorXd &derivatives)
const {
108 mapping->computeParameterDerivatives(measuredStar, measuredStar.
getInstFlux(), derivatives);
114 auto coeffs = transfo->getCoefficients();
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;
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;
134 idMapping.second->dump(stream);
139 idMapping.second->dump(stream);
148 "ConstrainedPhotometryModel cannot find CcdImage " + ccdImage.
getName());
149 return idMapping->second.get();
152 template <
class ChipTransfo,
class VisitTransfo,
class ChipVisitMapping>
154 afw::geom::Box2D
const &focalPlaneBBox,
int visitOrder) {
160 for (
auto const &
ccdImage : ccdImageList) {
170 if (radius2 < minRadius2) {
171 minRadius2 = radius2;
172 constrainedChip = chip;
174 auto photoCalib =
ccdImage->getPhotoCalib();
181 auto visitTransfo = std::make_unique<VisitTransfo>(visitOrder, focalPlaneBBox);
190 for (
auto const &
ccdImage : ccdImageList) {
197 <<
" visit mappings; holding chip " << constrainedChip <<
" fixed (" 213 return mapping->transform(measuredStar, measuredStar.
getInstFlux());
220 return mapping->transformError(measuredStar, measuredStar.
getInstFlux(), tempErr);
230 assert(mapping !=
nullptr);
235 assert(visitTransfo !=
nullptr);
236 auto focalBBox = visitTransfo->getBBox();
239 auto coeff_f = toChebyMapCoeffs(
240 std::dynamic_pointer_cast<PhotometryTransfoChebyshev>(mapping->
getVisitMapping()->getTransfo()));
252 auto transform = pixToFocal->then(chebyTransform)->then(zoomTransform);
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,
270 return mapping->transform(measuredStar, measuredStar.
getInstMag());
277 return mapping->transformError(measuredStar, measuredStar.
getInstFlux(), tempErr);
288 assert(mapping !=
nullptr);
293 assert(visitTransfo !=
nullptr);
294 auto focalBBox = visitTransfo->getBBox();
297 auto coeff_f = toChebyMapCoeffs(
298 std::dynamic_pointer_cast<PhotometryTransfoChebyshev>(mapping->
getVisitMapping()->getTransfo()));
305 using namespace std::string_literals;
307 ast::MathMap(1, 1, {
"y=pow(10.0,x/-2.5)"s}, {
"x=-2.5*log10(y)"s}));
315 auto transform = pixToFocal->then(chebyTransform)->then(logTransform)->then(zoomTransform);
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,
326 afw::geom::Box2D
const &, int);
329 CcdImageList
const &, afw::geom::Box2D
const &, int);
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.
CameraSysPrefix const PIXELS
std::shared_ptr< PhotometryMapping > getChipMapping() const
table::Key< int > detector
double getInstMag() const
T bucket_count(T... args)
nth-order 2d Chebyshev photometry transfo, times the input flux.
Photometric offset independent of position, defined as (fluxMag0)^-1.
table::Key< double > calibrationMean
#define LOGLS_INFO(logger, message)
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
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's photometric calibration.
Class for a simple mapping implementing a generic Gtransfo.
double getInstFlux() const
PhotometryMappingBase * findMapping(CcdImage const &ccdImage) const override
Return a pointer to the mapping associated with this ccdImage.
objects measured on actual images.
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...
void computeParameterDerivatives(MeasuredStar const &measuredStar, CcdImage const &ccdImage, Eigen::VectorXd &derivatives) const override
Compute the parametric derivatives of this model.
double transform(CcdImage const &ccdImage, MeasuredStar const &measuredStar) const override
Return the on-sky transformed flux for measuredStar on ccdImage.
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).
#define LOGLS_DEBUG(logger, message)
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
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
Handler of an actual image from a single CCD.
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
CameraSys const FOCAL_PLANE
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.