LSSTApplications  20.0.0
LSSTDataManagementBasePackage
imageStatistics.cc
// -*- LSST-C++ -*-
/*
* LSST Data Management System
* Copyright 2008, 2009, 2010 LSST Corporation.
*
* This product includes software developed by the
* LSST Project (http://www.lsst.org/).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the LSST License Statement and
* the GNU General Public License along with this program. If not,
* see <http://www.lsstcorp.org/LegalNotices/>.
*/
#include <cmath>
#include <iostream>
#include <limits>
#include <memory>
#include "lsst/geom.h"
namespace image = lsst::afw::image;
namespace math = lsst::afw::math;
typedef image::Image<float> ImageF;
typedef image::MaskedImage<float> MaskedImageF;
typedef math::Statistics ImgStat;
typedef math::MaskedVector<float> MaskedVectorF;
/*
* An example of how to use the Statistics class
*/
template <typename Image>
void printStats(Image &img, math::StatisticsControl const &sctrl) {
// initialize a Statistics object with any stats we might want
ImgStat stats = math::makeStatistics(
img,
sctrl);
// get various stats with getValue() and their errors with getError()
double const npoint = stats.getValue(math::NPOINT);
double const mean = stats.getValue(math::MEAN);
double const var = stats.getValue(math::VARIANCE);
double const dmean = stats.getError(math::MEAN);
double const sd = stats.getValue(math::STDEV);
double const min = stats.getValue(math::MIN);
double const max = stats.getValue(math::MAX);
double const meanclip = stats.getValue(math::MEANCLIP);
double const varclip = stats.getValue(math::VARIANCECLIP);
double const stdevclip = stats.getValue(math::STDEVCLIP);
double const median = stats.getValue(math::MEDIAN);
double const iqrange = stats.getValue(math::IQRANGE);
// output
std::cout << "N " << npoint << std::endl;
std::cout << "dmean " << dmean << std::endl;
std::cout << "mean: " << mean << std::endl;
std::cout << "meanclip: " << meanclip << std::endl;
std::cout << "var: " << var << std::endl;
std::cout << "varclip: " << varclip << std::endl;
std::cout << "stdev: " << sd << std::endl;
std::cout << "stdevclip: " << stdevclip << std::endl;
std::cout << "min: " << min << std::endl;
std::cout << "max: " << max << std::endl;
std::cout << "median: " << median << std::endl;
std::cout << "iqrange: " << iqrange << std::endl;
}
int main() {
// declare an image and a masked image
int const wid = 1024;
ImageF img(lsst::geom::Extent2I(wid, wid));
MaskedImageF mimg(img.getDimensions());
MaskedVectorF mv(wid * wid);
// fill it with some noise (Cauchy noise in this case)
for (int j = 0; j != img.getHeight(); ++j) {
int k = 0;
MaskedImageF::x_iterator mip = mimg.row_begin(j);
for (ImageF::x_iterator ip = img.row_begin(j); ip != img.row_end(j); ++ip) {
double const xUniform = M_PI * static_cast<ImageF::Pixel>(std::rand()) / RAND_MAX;
double xLorentz = xUniform; // tan(xUniform - M_PI/2.0);
// throw in the occassional nan ... 1% of the time
if (static_cast<double>(std::rand()) / RAND_MAX < 0.01) {
xLorentz = NAN;
}
*ip = xLorentz;
// mask the odd rows
// variance actually diverges for Cauchy noise ... but stats doesn't access this.
*mip = MaskedImageF::Pixel(xLorentz, (k % 2) ? 0x1 : 0x0, (k % 2) ? 1.0e99 : 1.0);
v.push_back(xLorentz);
++k;
++mip;
}
}
int j = 0;
for (MaskedVectorF::iterator mvp = mv.begin(); mvp != mv.end(); ++mvp) {
*mvp = MaskedVectorF::Pixel(v[j], (j % 2) ? 0x1 : 0x0, 10.0);
++j;
}
std::shared_ptr<std::vector<float> > vF = mv.getVector();
// make a statistics control object and override some of the default properties
math::StatisticsControl sctrl;
sctrl.setNumIter(3);
sctrl.setNumSigmaClip(5.0);
sctrl.setAndMask(0x1); // pixels with this mask bit set will be ignored.
sctrl.setNanSafe(true);
// ==================================================================
// Get stats for the Image, MaskedImage, and vector
std::cout << "image::Image" << std::endl;
printStats(img, sctrl);
std::cout << "image::MaskedImage" << std::endl;
printStats(mimg, sctrl);
std::cout << "std::vector" << std::endl;
printStats(v, sctrl);
std::cout << "image::MaskedVector" << std::endl;
printStats(mv, sctrl);
std::cout << "image::MaskedVector::getVector()" << std::endl;
printStats(*vF, sctrl);
// Now try the weighted statistics
sctrl.setWeighted(true);
sctrl.setAndMask(0x0);
std::cout << "image::MaskedImage (weighted)" << std::endl;
printStats(mimg, sctrl);
// Now try the specialization to get NPOINT and SUM (bitwise OR) for an image::Mask
math::Statistics mskstat = makeStatistics(*mimg.getMask(), (math::NPOINT | math::SUM), sctrl);
std::cout << "image::Mask" << std::endl;
std::cout << mskstat.getValue(math::NPOINT) << " " << mskstat.getValue(math::SUM) << std::endl;
return 0;
}
lsst::afw::math::IQRANGE
@ IQRANGE
estimate sample inter-quartile range
Definition: Statistics.h:71
lsst::afw::image
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Definition: imageAlgorithm.dox:1
std::shared_ptr
STL class.
MaskedVector.h
lsst::meas::modelfit::Pixel
float Pixel
Typedefs to be used for pixel values.
Definition: common.h:37
MaskedImage.h
std::vector< double >
geom.h
Image.h
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())
Handle a watered-down front-end to the constructor (no variance)
Definition: Statistics.h:354
lsst::afw::math::Statistics::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())
Handle a watered-down front-end to the constructor (no variance)
Definition: Statistics.h:354
lsst::afw::math::MEANCLIP
@ MEANCLIP
estimate sample N-sigma clipped mean (N set in StatisticsControl, default=3)
Definition: Statistics.h:72
lsst::afw::math::VARIANCECLIP
@ VARIANCECLIP
estimate sample N-sigma clipped variance (N set in StatisticsControl, default=3)
Definition: Statistics.h:74
lsst::afw::image::MaskedImage
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:73
lsst::afw::math::MEAN
@ MEAN
estimate sample mean
Definition: Statistics.h:67
image
afw::table::Key< afw::table::Array< ImagePixelT > > image
Definition: HeavyFootprint.cc:216
std::cout
lsst::afw::math::STDEVCLIP
@ STDEVCLIP
estimate sample N-sigma clipped stdev (N set in StatisticsControl, default=3)
Definition: Statistics.h:73
max
int max
Definition: BoundedField.cc:104
lsst::afw::math::ERRORS
@ ERRORS
Include errors of requested quantities.
Definition: Statistics.h:65
std::rand
T rand(T... args)
lsst::afw::math::NPOINT
@ NPOINT
number of sample points
Definition: Statistics.h:66
std::endl
T endl(T... args)
lsst::afw::math::STDEV
@ STDEV
estimate sample standard deviation
Definition: Statistics.h:68
Statistics.h
lsst::afw::math::MIN
@ MIN
estimate sample minimum
Definition: Statistics.h:76
min
int min
Definition: BoundedField.cc:103
lsst::afw::math
Definition: statistics.dox:6
lsst::afw::math::MAX
@ MAX
estimate sample maximum
Definition: Statistics.h:77
lsst::afw::math::VARIANCE
@ VARIANCE
estimate sample variance
Definition: Statistics.h:69
M_PI
#define M_PI
Definition: ListMatch.cc:31
lsst::afw::image::Image< float >
lsst::geom::Extent< int, 2 >
lsst::afw::math::SUM
@ SUM
find sum of pixels in the image
Definition: Statistics.h:78
lsst::afw::math::MEDIAN
@ MEDIAN
estimate sample median
Definition: Statistics.h:70
lsst.synpipe.compareModel.main
def main(root, visit, ccds, galType='sersic', output='outputs/')
Definition: compareModel.py:83