35 #include "Eigen/Eigenvalues"
40 namespace afwMath = lsst::afw::math;
46 template <
typename ImageT>
51 _constantWeight(constantWeight),
52 _eigenValues(std::vector<double>()),
55 template <
typename ImageT>
57 if (_imageList.empty()) {
58 _dimensions = img->getDimensions();
60 if (getDimensions() != img->getDimensions()) {
61 throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
62 (boost::format(
"Dimension mismatch: saw %dx%d; expected %dx%d") %
63 img->getWidth() % img->getHeight() % _dimensions.getX() % _dimensions.getY())
69 throw LSST_EXCEPT(lsst::pex::exceptions::OutOfRangeError,
"Flux may not be zero");
72 _imageList.push_back(img);
73 _fluxList.push_back(flux);
76 template <
typename ImageT>
81 template <
typename ImageT>
83 if (_imageList.empty()) {
84 throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
"You haven't provided any images");
87 std::shared_ptr<ImageT> mean(
new ImageT(getDimensions()));
88 *mean =
static_cast<typename ImageT::Pixel
>(0);
90 for (
typename ImageList::const_iterator ptr = _imageList.begin(),
end = _imageList.end(); ptr !=
end;
94 *mean /= _imageList.size();
105 template <
typename T>
106 struct SortEvalueDecreasing
107 :
public std::binary_function<std::pair<T, int> const&, std::pair<T, int> const&, bool> {
108 bool operator()(std::pair<T, int>
const& a, std::pair<T, int>
const& b) {
109 return a.first > b.first;
114 template <
typename ImageT>
116 int const nImage = _imageList.size();
119 throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
"No images provided for PCA analysis");
125 _eigenImages.clear();
126 _eigenImages.push_back(std::shared_ptr<ImageT>(
new ImageT(*_imageList[0],
true)));
128 _eigenValues.clear();
129 _eigenValues.push_back(1.0);
136 Eigen::MatrixXd R(nImage, nImage);
139 for (
int i = 0; i != nImage; ++i) {
141 double const flux_i = getFlux(i);
144 for (
int j = i; j != nImage; ++j) {
146 double const flux_j = getFlux(j);
149 if (_constantWeight) {
150 dot /= flux_i * flux_j;
152 R(i, j) = R(j, i) = dot / nImage;
156 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> eVecValues(R);
157 Eigen::MatrixXd
const& Q = eVecValues.eigenvectors();
158 Eigen::VectorXd
const& lambda = eVecValues.eigenvalues();
163 std::vector<std::pair<double, int> > lambdaAndIndex;
164 lambdaAndIndex.reserve(nImage);
166 for (
int i = 0; i != nImage; ++i) {
167 lambdaAndIndex.push_back(std::make_pair(lambda(i), i));
169 std::sort(lambdaAndIndex.begin(), lambdaAndIndex.end(), SortEvalueDecreasing<double>());
173 _eigenValues.clear();
174 _eigenValues.reserve(nImage);
175 for (
int i = 0; i != nImage; ++i) {
176 _eigenValues.push_back(lambdaAndIndex[i].
first);
183 _eigenImages.clear();
184 _eigenImages.reserve(ncomp < nImage ? ncomp : nImage);
186 for (
int i = 0; i < ncomp; ++i) {
191 int const ii = lambdaAndIndex[i].second;
193 std::shared_ptr<ImageT> eImage(
new ImageT(_dimensions));
194 *eImage =
static_cast<typename ImageT::Pixel
>(0);
196 for (
int j = 0; j != nImage; ++j) {
197 int const jj = lambdaAndIndex[j].second;
198 double const weight = Q(jj, ii) * (_constantWeight ? flux_bar / getFlux(jj) : 1);
199 eImage->scaledPlus(weight, *_imageList[jj]);
201 _eigenImages.push_back(eImage);
211 template <
typename MaskedImageT>
212 std::shared_ptr<typename MaskedImageT::Image> fitEigenImagesToImage(
215 MaskedImageT
const&
image
217 typedef typename MaskedImageT::Image ImageT;
220 throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
"You must have at least one eigen image");
221 }
else if (nEigen > static_cast<int>(eigenImages.size())) {
222 throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
223 (boost::format(
"You only have %d eigen images (you asked for %d)") %
224 eigenImages.size() % nEigen)
231 Eigen::MatrixXd A(nEigen, nEigen);
232 Eigen::VectorXd b(nEigen);
234 for (
int i = 0; i != nEigen; ++i) {
235 b(i) =
innerProduct(*eigenImages[i]->getImage(), *image.getImage());
237 for (
int j = i; j != nEigen; ++j) {
238 A(i, j) = A(j, i) =
innerProduct(*eigenImages[i]->getImage(), *eigenImages[j]->getImage());
241 Eigen::VectorXd
x(nEigen);
244 x(0) = b(0) / A(0, 0);
246 x = A.jacobiSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(b);
251 std::shared_ptr<ImageT> bestFitImage = std::make_shared<ImageT>(eigenImages[0]->
getDimensions());
253 for (
int i = 0; i != nEigen; ++i) {
254 bestFitImage->scaledPlus(
x[i], *eigenImages[i]->getImage());
260 template <
typename ImageT>
261 double do_updateBadPixels(detail::basic_tag
const&,
typename ImagePca<ImageT>::ImageList
const&,
262 std::vector<double>
const&,
typename ImagePca<ImageT>::ImageList
const&,
263 unsigned long,
int const) {
267 template <
typename ImageT>
268 double do_updateBadPixels(detail::MaskedImage_tag
const&,
269 typename ImagePca<ImageT>::ImageList
const& imageList,
270 std::vector<double>
const& fluxes,
271 typename ImagePca<ImageT>::ImageList
const& eigenImages,
275 int const nImage = imageList.size();
277 throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
278 "Please provide at least one Image for me to update");
281 int const height = dimensions.getY();
283 double maxChange = 0.0;
286 typename ImageT::Image mean(dimensions);
289 for (
int i = 0; i != nImage; ++i) {
290 double const flux_i = fluxes[i];
292 for (
int y = 0; y != height; ++y) {
293 typename ImageT::const_x_iterator iptr = imageList[i]->row_begin(y);
295 for (
typename ImageT::Image::x_iterator mptr = mean.row_begin(y),
end = mean.row_end(y);
296 mptr !=
end; ++mptr, ++iptr, ++wptr) {
297 if (!(iptr.mask() &
mask) && iptr.variance() > 0.0) {
298 typename ImageT::Image::Pixel value = iptr.image() / flux_i;
299 float const var = iptr.variance() / (flux_i * flux_i);
300 float const ivar = 1.0 / var;
302 if (std::isfinite(value * ivar)) {
303 *mptr += value * ivar;
313 for (
int y = 0; y != height; ++y) {
315 for (
typename ImageT::Image::x_iterator mptr = mean.row_begin(y),
end = mean.row_end(y);
316 mptr !=
end; ++mptr, ++wptr) {
327 for (
int i = 0; i != nImage; ++i) {
328 double const flux_i = fluxes[i];
330 for (
int y = 0; y != height; ++y) {
331 typename ImageT::x_iterator iptr = imageList[i]->row_begin(y);
332 for (
typename ImageT::Image::x_iterator mptr = mean.row_begin(y),
end = mean.row_end(y);
333 mptr !=
end; ++mptr, ++iptr) {
334 if ((iptr.mask() &
mask)) {
335 double const delta = ::fabs(flux_i * (*mptr) - iptr.image());
336 if (delta > maxChange) {
339 iptr.image() = flux_i * (*mptr);
345 if (ncomp > static_cast<int>(eigenImages.size())) {
346 throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
347 (boost::format(
"You only have %d eigen images (you asked for %d)") %
348 eigenImages.size() % ncomp)
352 for (
int i = 0; i != nImage; ++i) {
353 std::shared_ptr<typename ImageT::Image> fitted =
354 fitEigenImagesToImage(eigenImages, ncomp, *imageList[i]);
356 for (
int y = 0; y != height; ++y) {
357 typename ImageT::x_iterator iptr = imageList[i]->row_begin(y);
358 for (
typename ImageT::Image::const_x_iterator fptr = fitted->row_begin(y),
359 end = fitted->row_end(y);
360 fptr !=
end; ++fptr, ++iptr) {
361 if (iptr.mask() &
mask) {
362 double const delta = fabs(static_cast<double>(*fptr) - iptr.image());
363 if (delta > maxChange) {
367 iptr.image() = *fptr;
377 template <
typename ImageT>
379 return do_updateBadPixels<ImageT>(
typename ImageT::image_category(), _imageList, _fluxList, _eigenImages,
384 template <
typename T,
typename U>
386 IsSame(T
const&, U
const&) {}
387 bool operator()() {
return false; }
390 template <
typename T>
391 struct IsSame<T, T> {
392 IsSame(T
const& im1, T
const& im2) :
_same(im1.row_begin(0) == im2.row_begin(0)) {}
393 bool operator()() {
return _same; }
400 template <
typename Image1T,
typename Image2T>
401 bool imagesAreIdentical(Image1T
const& im1, Image2T
const& im2) {
402 return IsSame<Image1T, Image2T>(im1, im2)();
405 template <
typename Image1T,
typename Image2T>
406 double innerProduct(Image1T
const& lhs, Image2T
const& rhs,
int border) {
407 if (lhs.getWidth() <= 2 * border || lhs.getHeight() <= 2 * border) {
408 throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
409 (boost::format(
"All image pixels are in the border of width %d: %dx%d") % border %
410 lhs.getWidth() % lhs.getHeight())
418 if (imagesAreIdentical(lhs, rhs)) {
419 for (
int y = border; y != lhs.getHeight() - border; ++y) {
420 for (
typename Image1T::const_x_iterator lptr = lhs.row_begin(y) + border,
421 lend = lhs.row_end(y) - border;
422 lptr != lend; ++lptr) {
423 typename Image1T::Pixel val = *lptr;
424 if (std::isfinite(val)) {
430 if (lhs.getDimensions() != rhs.getDimensions()) {
431 throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
432 (boost::format(
"Dimension mismatch: %dx%d v. %dx%d") % lhs.getWidth() %
433 lhs.getHeight() % rhs.getWidth() % rhs.getHeight())
437 for (
int y = border; y != lhs.getHeight() - border; ++y) {
438 typename Image2T::const_x_iterator rptr = rhs.row_begin(y) + border;
439 for (
typename Image1T::const_x_iterator lptr = lhs.row_begin(y) + border,
440 lend = lhs.row_end(y) - border;
441 lptr != lend; ++lptr, ++rptr) {
442 double const tmp = (*lptr) * (*rptr);
443 if (std::isfinite(tmp)) {
457 #define INSTANTIATE(T) \
458 template class ImagePca<Image<T> >; \
459 template double innerProduct(Image<T> const&, Image<T> const&, int); \
460 template class ImagePca<MaskedImage<T> >;
462 #define INSTANTIATE2(T, U) \
463 template double innerProduct(Image<T> const&, Image<U> const&, int); \
464 template double innerProduct(Image<U> const&, Image<T> const&, int);
472 INSTANTIATE2(
float,
double)
afw::table::PointKey< int > dimensions
double innerProduct(Image1T const &lhs, Image2T const &rhs, int const border=0)
Calculate the inner product of two images.
std::vector< std::shared_ptr< ImageT > > ImageList
ImageList getImageList() const
Return the list of images being analyzed.
Extent< int, 2 > Extent2I
std::shared_ptr< ImageT > getMean() const
Return the mean of the images in ImagePca's list.
geom::Extent2I const getDimensions() const
Return the dimension of the images being analyzed.
ImagePca(bool constantWeight=true)
ctor
void addImage(std::shared_ptr< ImageT > img, double flux=0.0)
Add an image to the set to be analyzed.
x_iterator row_begin(int y) const
Return an x_iterator to the start of the y'th row.
A class to represent a 2-dimensional array of pixels.
virtual double updateBadPixels(unsigned long mask, int const ncomp)
Update the bad pixels (i.e.
#define INSTANTIATE(FROMSYS, TOSYS)