lsst.ip.diffim
g174f8ebed5+0e497d1560
Toggle main menu visibility
Loading...
Searching...
No Matches
src
KernelPca.cc
Go to the documentation of this file.
1
// -*- lsst-c++ -*-
11
12
#include "
lsst/afw/math.h
"
13
#include "
lsst/afw/image.h
"
14
#include "
lsst/log/Log.h
"
15
#include "
lsst/pex/exceptions/Runtime.h
"
16
17
#include "
lsst/ip/diffim/KernelCandidate.h
"
18
#include "
lsst/ip/diffim/KernelPca.h
"
19
20
namespace
afwMath
=
lsst::afw::math
;
21
namespace
afwImage
=
lsst::afw::image
;
22
namespace
pexExcept
=
lsst::pex::exceptions
;
23
24
namespace
lsst
{
25
namespace
ip
{
26
namespace
diffim
{
27
namespace
detail
{
28
36
*
37
* @note Templated on the pixel types of the MaskedImages it will be
38
* visiting (typically float).
39
*
40
* @note Works in concert with a afwMath::SpatialCellSet and ip::Diffim
41
* KernelPca to create a Karhunen-Loeve basis from all the good
42
* KernelCandidates. This class adds the extra functionality to subtract
43
* off the mean kernel from all entries, which makes the resulting basis
44
* more compact. The user needs to manually add this mean image into the
45
* resulting basis list after imagePca.analyze() is called.
46
*
47
* @note KernelPca (and base class afwImage::ImagePca) weight objects of
48
* different brightness differently. However we don't necessarily want
49
* images with larger kernel sums to have more weight. Each kernel should
50
* have constant weight in the Pca. For simplicity we scale them to have
51
* the same kernel sum, 1.0, and send to ImagePca that the flux (weight) is
52
* 1.0.
53
*
54
*/
55
template
<
typename
PixelT>
56
KernelPcaVisitor<PixelT>::KernelPcaVisitor
(
57
std::shared_ptr
<
KernelPca<ImageT>
> imagePca
58
) :
59
afwMath
::
CandidateVisitor
(),
60
_imagePca(imagePca),
61
_mean()
62
{};
63
64
template
<
typename
PixelT>
65
lsst::afw::math::KernelList
KernelPcaVisitor<PixelT>::getEigenKernels
() {
66
afwMath::KernelList
kernelList;
67
68
std::vector<std::shared_ptr<ImageT>
> eigenImages = _imagePca->getEigenImages();
69
int
ncomp = eigenImages.
size
();
70
71
if
(_mean) {
72
kernelList.
push_back
(
std::shared_ptr<afwMath::Kernel>
(
73
new
afwMath::FixedKernel
(
74
afwImage::Image<afwMath::Kernel::Pixel>
75
(*_mean,
true
))));
76
}
77
for
(
int
i = 0; i < ncomp; i++) {
78
afwImage::Image<afwMath::Kernel::Pixel>
img =
79
afwImage::Image<afwMath::Kernel::Pixel>
(*eigenImages[i],
true
);
80
kernelList.
push_back
(
std::shared_ptr<afwMath::Kernel>
(
81
new
afwMath::FixedKernel
(img)
82
));
83
}
84
85
return
kernelList;
86
}
87
88
template
<
typename
PixelT>
89
void
KernelPcaVisitor<PixelT>::processCandidate
(
lsst::afw::math::SpatialCellCandidate
*candidate) {
90
91
KernelCandidate<PixelT>
*kCandidate =
dynamic_cast<
KernelCandidate<PixelT>
*
>
(candidate);
92
if
(kCandidate == NULL) {
93
throw
LSST_EXCEPT
(
pexExcept::LogicError
,
94
"Failed to cast SpatialCellCandidate to KernelCandidate"
);
95
}
96
LOGL_DEBUG
(
"TRACE5.ip.diffim.SetPcaImageVisitor.processCandidate"
,
97
"Processing candidate %d"
, kCandidate->
getId
());
98
99
try
{
100
/* Normalize to unit sum */
101
std::shared_ptr<ImageT>
kImage = kCandidate->
getKernelSolution
(
102
KernelCandidate<PixelT>::ORIG
)->makeKernelImage();
103
*kImage /= kCandidate->
getKernelSolution
(
104
KernelCandidate<PixelT>::ORIG
)->getKsum();
105
/* Tell imagePca they have the same weighting in the Pca */
106
_imagePca->addImage(kImage, 1.0);
107
}
catch
(
pexExcept::Exception
&e) {
108
return
;
109
}
110
}
111
112
template
<
typename
PixelT>
113
void
KernelPcaVisitor<PixelT>::subtractMean
() {
114
/*
115
If we don't subtract off the mean before we do the Pca, the
116
subsequent terms carry less of the power than if you do subtract
117
off the mean. Explicit example:
118
119
With mean subtraction:
120
DEBUG: Eigenvalue 0 : 0.010953 (0.373870 %)
121
DEBUG: Eigenvalue 1 : 0.007927 (0.270604 %)
122
DEBUG: Eigenvalue 2 : 0.001393 (0.047542 %)
123
DEBUG: Eigenvalue 3 : 0.001092 (0.037261 %)
124
DEBUG: Eigenvalue 4 : 0.000829 (0.028283 %)
125
126
Without mean subtraction:
127
DEBUG: Eigenvalue 0 : 0.168627 (0.876046 %)
128
DEBUG: Eigenvalue 1 : 0.007935 (0.041223 %)
129
DEBUG: Eigenvalue 2 : 0.006049 (0.031424 %)
130
DEBUG: Eigenvalue 3 : 0.001188 (0.006173 %)
131
DEBUG: Eigenvalue 4 : 0.001050 (0.005452 %)
132
133
After the first term above, which basically represents the mean,
134
the remaining terms carry less of the power than if you do
135
subtract off the mean. (0.041223/(1-0.876046) < 0.373870).
136
*/
137
LOGL_DEBUG
(
"TRACE5.ip.diffim.KernelPcaVisitor.subtractMean"
,
138
"Subtracting mean feature before Pca"
);
139
140
_mean = _imagePca->getMean();
141
KernelPca<ImageT>::ImageList
imageList = _imagePca->getImageList();
142
for
(
typename
KernelPca<ImageT>::ImageList::const_iterator ptr = imageList.
begin
(),
143
end = imageList.
end
(); ptr != end; ++ptr) {
144
**ptr -= *_mean;
145
}
146
}
147
162
template
<
typename
ImageT>
163
void
KernelPca<ImageT>::analyze
()
164
{
165
Super::analyze
();
166
167
typename
Super::ImageList
const
&eImageList = this->
getEigenImages
();
168
typename
Super::ImageList::const_iterator iter = eImageList.
begin
(), end = eImageList.
end
();
169
for
(
size_t
i = 0; iter != end; ++i, ++iter) {
170
std::shared_ptr<ImageT>
eImage = *iter;
171
172
/*
173
* Normalise eigenImages to have a maximum of 1.0. For n > 0 they
174
* (should) have mean == 0, so we can't use that to normalize
175
*/
176
afwMath::Statistics
stats =
afwMath::makeStatistics
(*eImage, (
afwMath::MIN
|
afwMath::MAX
));
177
double
const
min = stats.
getValue
(
afwMath::MIN
);
178
double
const
max = stats.
getValue
(
afwMath::MAX
);
179
180
double
const
extreme = (fabs(min) > max) ? min :max;
181
if
(extreme != 0.0) {
182
*eImage /= extreme;
183
}
184
}
185
}
186
187
188
typedef
float
PixelT
;
189
template
class
KernelPcaVisitor<PixelT>
;
190
template
class
KernelPca<afwImage::Image<afwMath::Kernel::Pixel>
>;
191
192
}}}}
// end of namespace lsst::ip::diffim::detail
Log.h
LOGL_DEBUG
#define LOGL_DEBUG(logger, message...)
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
image.h
KernelCandidate.h
Class used by SpatialModelCell for spatial Kernel fitting.
KernelPca.h
Declaration of KernelPca and KernelPcaVisitor.
Runtime.h
std::vector::begin
T begin(T... args)
lsst::afw::image::Image
lsst::afw::image::ImagePca< ImageT >::getEigenImages
ImageList const & getEigenImages() const
lsst::afw::image::ImagePca< ImageT >::ImageList
std::vector< std::shared_ptr< ImageT > > ImageList
lsst::afw::image::ImagePca< ImageT >::analyze
virtual void analyze()
lsst::afw::math::CandidateVisitor::CandidateVisitor
CandidateVisitor()=default
lsst::afw::math::FixedKernel
lsst::afw::math::SpatialCellCandidate
lsst::afw::math::SpatialCellImageCandidate::getId
int getId() const
lsst::afw::math::Statistics
lsst::afw::math::Statistics::getValue
double getValue(Property const prop=NOTHING) const
lsst::ip::diffim::KernelCandidate
Class stored in SpatialCells for spatial Kernel fitting.
Definition
KernelCandidate.h:39
lsst::ip::diffim::KernelCandidate::ORIG
@ ORIG
Definition
KernelCandidate.h:50
lsst::ip::diffim::KernelCandidate::getKernelSolution
std::shared_ptr< StaticKernelSolution< PixelT > > getKernelSolution(CandidateSwitch cand) const
Definition
KernelCandidate.cc:337
lsst::ip::diffim::detail::KernelPca
Overrides the analyze method of base class afwImage::ImagePca.
Definition
KernelPca.h:24
lsst::ip::diffim::detail::KernelPca::analyze
virtual void analyze()
Generate eigenimages that are normalised.
Definition
KernelPca.cc:163
lsst::ip::diffim::detail::KernelPcaVisitor
A class to run a PCA on all candidate kernels (represented as Images).
Definition
KernelPca.h:40
lsst::ip::diffim::detail::KernelPcaVisitor::subtractMean
void subtractMean()
Definition
KernelPca.cc:113
lsst::ip::diffim::detail::KernelPcaVisitor::processCandidate
void processCandidate(lsst::afw::math::SpatialCellCandidate *candidate)
Definition
KernelPca.cc:89
lsst::ip::diffim::detail::KernelPcaVisitor::getEigenKernels
lsst::afw::math::KernelList getEigenKernels()
Definition
KernelPca.cc:65
lsst::ip::diffim::detail::KernelPcaVisitor::KernelPcaVisitor
KernelPcaVisitor(std::shared_ptr< KernelPca< ImageT > > imagePca)
Definition
KernelPca.cc:56
lsst::pex::exceptions::Exception
lsst::pex::exceptions::LogicError
std::vector::end
T end(T... args)
math.h
lsst::afw::image
lsst::afw::math
lsst::afw::math::makeStatistics
Statistics makeStatistics(lsst::afw::image::Image< Pixel > const &img, lsst::afw::image::Mask< image::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl=StatisticsControl())
lsst::afw::math::MIN
MIN
lsst::afw::math::MAX
MAX
lsst::afw::math::KernelList
std::vector< std::shared_ptr< Kernel > > KernelList
lsst::ip::diffim::detail
Definition
AssessSpatialKernelVisitor.h:23
lsst::ip::diffim::detail::PixelT
float PixelT
Definition
AssessSpatialKernelVisitor.cc:208
lsst::ip::diffim
Definition
AssessSpatialKernelVisitor.h:22
lsst::ip
Definition
AssessSpatialKernelVisitor.h:21
lsst::pex::exceptions
lsst
std::vector::push_back
T push_back(T... args)
std::shared_ptr
std::vector::size
T size(T... args)
std::vector
Generated on
for lsst.ip.diffim by
1.17.0