4 #include "lsst/log/Log.h" 7 #include "astshim/ChebyMap.h" 8 #include "lsst/afw/geom/Transform.h" 9 #include "lsst/afw/image/PhotoCalib.h" 10 #include "lsst/afw/math/TransformBoundedField.h" 16 LOG_LOGGER _log = LOG_GET(
"jointcal.ConstrainedPhotometryModel");
23 afw::geom::Box2D
const &focalPlaneBBox,
26 double minRadius2 = std::numeric_limits<double>::infinity();
30 for (
auto const &
ccdImage : ccdImageList) {
33 auto visitPair = _visitMap.find(visit);
34 auto chipPair = _chipMap.find(chip);
37 if (chipPair == _chipMap.end()) {
38 auto center =
ccdImage->getDetector()->getCenter(afw::cameraGeom::FOCAL_PLANE);
39 double radius2 = std::pow(center.getPoint().getX(), 2) + std::pow(center.getPoint().getY(), 2);
40 if (radius2 < minRadius2) {
42 constrainedChip = chip;
44 auto photoCalib =
ccdImage->getPhotoCalib();
47 std::make_shared<PhotometryTransfoSpatiallyInvariant>(photoCalib->getCalibrationMean());
49 std::unique_ptr<PhotometryMapping>(
new PhotometryMapping(std::move(chipTransfo)));
52 if (visitPair == _visitMap.end()) {
53 auto visitTransfo = std::make_shared<PhotometryTransfoChebyshev>(visitDegree, focalPlaneBBox);
55 std::unique_ptr<PhotometryMapping>(
new PhotometryMapping(std::move(visitTransfo)));
60 _chipMap.at(constrainedChip)->setFixed(
true);
63 _myMap.reserve(ccdImageList.size());
64 for (
auto const &
ccdImage : ccdImageList) {
67 _myMap.emplace(
ccdImage->getHashKey(),
68 std::unique_ptr<ChipVisitPhotometryMapping>(
71 LOGLS_INFO(_log,
"Got " << _chipMap.size() <<
" chip mappings and " << _visitMap.size()
72 <<
" visit mappings; holding chip " << constrainedChip <<
" fixed.");
73 LOGLS_DEBUG(_log,
"CcdImage map has " << _myMap.size() <<
" mappings, with " << _myMap.bucket_count()
74 <<
" buckets and a load factor of " << _myMap.load_factor());
79 unsigned index = firstIndex;
80 for (
auto &i : _chipMap) {
81 auto mapping = i.second.get();
83 if (mapping->isFixed())
continue;
84 mapping->setIndex(index);
85 index += mapping->getNpar();
87 for (
auto &i : _visitMap) {
88 auto mapping = i.second.get();
89 mapping->setIndex(index);
90 index += mapping->getNpar();
96 for (
auto &i : _chipMap) {
97 auto mapping = i.second.get();
99 if (mapping->isFixed())
continue;
100 mapping->offsetParams(delta.segment(mapping->getIndex(), mapping->getNpar()));
102 for (
auto &i : _visitMap) {
103 auto mapping = i.second.get();
104 mapping->offsetParams(delta.segment(mapping->getIndex(), mapping->getNpar()));
109 double instFlux)
const {
110 auto mapping = findMapping(ccdImage);
111 return mapping->transform(measuredStar, instFlux);
115 std::vector<unsigned> &indices)
const {
116 auto mapping = findMapping(ccdImage);
117 mapping->getMappingIndices(indices);
122 Eigen::VectorXd &derivatives)
const {
123 auto mapping = findMapping(ccdImage);
124 mapping->computeParameterDerivatives(measuredStar, measuredStar.
getInstFlux(), derivatives);
129 ndarray::Array<double, 2, 2> toChebyMapCoeffs(std::shared_ptr<PhotometryTransfoChebyshev> transfo) {
130 auto coeffs = transfo->getCoefficients();
132 ndarray::Array<double, 2, 2> chebyCoeffs = allocate(ndarray::makeVector(transfo->getNpar(), 4));
133 Eigen::VectorXd::Index k = 0;
134 auto degree = transfo->getDegree();
135 for (ndarray::Size j = 0; j <= degree; ++j) {
136 ndarray::Size
const iMax = degree - j;
137 for (ndarray::Size i = 0; i <= iMax; ++i, ++k) {
138 chebyCoeffs[k][0] = coeffs[j][i];
139 chebyCoeffs[k][1] = 1;
140 chebyCoeffs[k][2] = i;
141 chebyCoeffs[k][3] = j;
152 auto ccdBBox = detector->getBBox();
156 assert(mapping !=
nullptr);
157 auto pixToFocal = detector->getTransform(afw::cameraGeom::PIXELS, afw::cameraGeom::FOCAL_PLANE);
161 assert(visitTransfo !=
nullptr);
162 auto focalBBox = visitTransfo->getBBox();
165 auto coeff_f = toChebyMapCoeffs(
166 std::dynamic_pointer_cast<PhotometryTransfoChebyshev>(mapping->
getVisitMapping()->getTransfo()));
168 std::vector<double> lowerBound = {focalBBox.getMinX(), focalBBox.getMinY()};
169 std::vector<double> upperBound = {focalBBox.getMaxX(), focalBBox.getMaxY()};
170 ast::ChebyMap chebyMap(coeff_f, 1, lowerBound, upperBound);
173 ast::ZoomMap zoomMap(1, mapping->
getChipMapping()->getParameters()[0]);
177 afw::geom::TransformPoint2ToGeneric(pixToFocal->getFrameSet()->then(chebyMap).then(zoomMap));
179 double mean = mapping->
getChipMapping()->getParameters()[0] * visitTransfo->mean();
180 auto boundedField = std::make_shared<afw::math::TransformBoundedField>(ccdBBox,
transform);
181 return std::make_shared<afw::image::PhotoCalib>(mean, oldPhotoCalib->getCalibrationErr(), boundedField,
186 for (
auto &i : _chipMap) {
187 i.second->dump(stream);
191 for (
auto &i : _visitMap) {
192 i.second->dump(stream);
199 if (i == _myMap.end())
200 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
201 "ConstrainedPhotometryModel cannot find CcdImage " + ccdImage.
getName());
202 return i->second.get();
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.
std::shared_ptr< PhotometryMapping > getChipMapping() const
unsigned assignIndices(std::string const &whatToFit, unsigned firstIndex) override
Assign indices to parameters involved in mappings, starting at firstIndex.
std::shared_ptr< PhotometryMapping > getVisitMapping() const
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
objects measured on actual images.
void computeParameterDerivatives(MeasuredStar const &measuredStar, CcdImage const &ccdImage, Eigen::VectorXd &derivatives) const override
Compute the parametric derivatives of this model.
void dump(std::ostream &stream=std::cout) const override
Dump the contents of the transfos, for debugging.
void offsetParams(Eigen::VectorXd const &delta) override
Offset the parameters by the provided amounts.
double transform(CcdImage const &ccdImage, MeasuredStar const &star, double instFlux) const override
Return the on-sky transformed flux for measuredStar on ccdImage.
std::list< std::shared_ptr< CcdImage > > CcdImageList
nth-degree 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.
ConstrainedPhotometryModel(CcdImageList const &ccdImageList, afw::geom::Box2D const &focalPlaneBBox, int visitDegree=7)
Construct a constrained photometry model.
A mapping containing a single photometryTransfo.
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.