LSST Applications  21.0.0-147-g0e635eb1+1acddb5be5,22.0.0+052faf71bd,22.0.0+1ea9a8b2b2,22.0.0+6312710a6c,22.0.0+729191ecac,22.0.0+7589c3a021,22.0.0+9f079a9461,22.0.1-1-g7d6de66+b8044ec9de,22.0.1-1-g87000a6+536b1ee016,22.0.1-1-g8e32f31+6312710a6c,22.0.1-10-gd060f87+016f7cdc03,22.0.1-12-g9c3108e+df145f6f68,22.0.1-16-g314fa6d+c825727ab8,22.0.1-19-g93a5c75+d23f2fb6d8,22.0.1-19-gb93eaa13+aab3ef7709,22.0.1-2-g8ef0a89+b8044ec9de,22.0.1-2-g92698f7+9f079a9461,22.0.1-2-ga9b0f51+052faf71bd,22.0.1-2-gac51dbf+052faf71bd,22.0.1-2-gb66926d+6312710a6c,22.0.1-2-gcb770ba+09e3807989,22.0.1-20-g32debb5+b8044ec9de,22.0.1-23-gc2439a9a+fb0756638e,22.0.1-3-g496fd5d+09117f784f,22.0.1-3-g59f966b+1e6ba2c031,22.0.1-3-g849a1b8+f8b568069f,22.0.1-3-gaaec9c0+c5c846a8b1,22.0.1-32-g5ddfab5d3+60ce4897b0,22.0.1-4-g037fbe1+64e601228d,22.0.1-4-g8623105+b8044ec9de,22.0.1-5-g096abc9+d18c45d440,22.0.1-5-g15c806e+57f5c03693,22.0.1-7-gba73697+57f5c03693,master-g6e05de7fdc+c1283a92b8,master-g72cdda8301+729191ecac,w.2021.39
LSST Data Management Base Package
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;
using MaskedImageF = image::MaskedImage<float>;
using ImgStat = math::Statistics;
using MaskedVectorF = math::MaskedVector<float>;
/*
* 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;
}
int min
int max
afw::table::Key< afw::table::Array< ImagePixelT > > image
#define M_PI
Definition: ListMatch.cc:31
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:51
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:73
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:359
T endl(T... args)
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
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:359
@ ERRORS
Include errors of requested quantities.
Definition: Statistics.h:64
@ VARIANCECLIP
estimate sample N-sigma clipped variance (N set in StatisticsControl, default=3)
Definition: Statistics.h:73
@ MIN
estimate sample minimum
Definition: Statistics.h:75
@ STDEV
estimate sample standard deviation
Definition: Statistics.h:67
@ STDEVCLIP
estimate sample N-sigma clipped stdev (N set in StatisticsControl, default=3)
Definition: Statistics.h:72
@ VARIANCE
estimate sample variance
Definition: Statistics.h:68
@ MEDIAN
estimate sample median
Definition: Statistics.h:69
@ MAX
estimate sample maximum
Definition: Statistics.h:76
@ SUM
find sum of pixels in the image
Definition: Statistics.h:77
@ IQRANGE
estimate sample inter-quartile range
Definition: Statistics.h:70
@ MEAN
estimate sample mean
Definition: Statistics.h:66
@ MEANCLIP
estimate sample N-sigma clipped mean (N set in StatisticsControl, default=3)
Definition: Statistics.h:71
@ NPOINT
number of sample points
Definition: Statistics.h:65
afw::image::Image< float > ImageF
Definition: VeresModel.cc:35
float Pixel
Typedefs to be used for pixel values.
Definition: common.h:37
T rand(T... args)