LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
ImageStatistics.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
35 #ifndef LSST_IP_DIFFIM_IMAGESTATISTICS_H
36 #define LSST_IP_DIFFIM_IMAGESTATISTICS_H
37 
38 #include <limits>
39 #include "boost/shared_ptr.hpp"
40 #include "lsst/afw/image.h"
41 #include "lsst/pex/policy/Policy.h"
42 #include "lsst/pex/logging/Trace.h"
43 #include "lsst/utils/ieee.h"
44 
45 namespace lsst {
46 namespace ip {
47 namespace diffim {
48 
57  template <typename PixelT>
59  public:
60  typedef boost::shared_ptr<ImageStatistics> Ptr;
62 
64  _xsum(0.), _x2sum(0.), _npix(0), _bpMask(0) {
65 
66  std::vector<std::string> detBadMaskPlanes = policy.getStringArray("badMaskPlanes");
67  for (std::vector<std::string>::iterator mi = detBadMaskPlanes.begin();
68  mi != detBadMaskPlanes.end(); ++mi){
69 
70  try {
72  } catch (pexExcept::Exception& e) {
73  lsst::pex::logging::TTrace<6>("lsst.ip.diffim.ImageStatistics",
74  "Cannot update bad bit mask with %s", (*mi).c_str());
75  lsst::pex::logging::TTrace<7>("lsst.ip.diffim.ImageStatistics",
76  e.what());
77  }
78  }
79  } ;
80  virtual ~ImageStatistics() {} ;
81 
82  // Clear the accumulators
83  void reset() { _xsum = _x2sum = 0.; _npix = 0;}
84 
85  // Work your magic
87  apply(image, -1);
88  }
89 
91  reset();
92  int y0, y1, x0, x1;
93  if (core == -1) {
94  y0 = 0;
95  y1 = image.getHeight();
96  x0 = 0;
97  x1 = image.getWidth();
98  }
99  else {
100  y0 = std::max(0, image.getHeight()/2 - core);
101  y1 = std::min(image.getHeight(), image.getHeight()/2 + core + 1);
102  x0 = std::max(0, image.getWidth()/2 - core);
103  x1 = std::min(image.getWidth(), image.getWidth()/2 + core + 1);
104  }
105 
106  for (int y = y0; y != y1; ++y) {
107  for (x_iterator ptr = image.x_at(x0, y), end = image.x_at(x1, y);
108  ptr != end; ++ptr) {
109  if (!((*ptr).mask() & _bpMask)) {
110  double const ivar = 1. / (*ptr).variance();
111  if (lsst::utils::lsst_isfinite(ivar)) {
112  _xsum += (*ptr).image() * sqrt(ivar);
113  _x2sum += (*ptr).image() * (*ptr).image() * ivar;
114  _npix += 1;
115  }
116  }
117  }
118  }
119  if ((!lsst::utils::lsst_isfinite(_xsum)) || (!lsst::utils::lsst_isfinite(_x2sum))) {
121  "Nan/Inf in ImageStatistics.apply");
122  }
123  }
124 
127 
128  // Mean of distribution
129  double getMean() const {
130  return (_npix > 0) ? _xsum/_npix : std::numeric_limits<double>::quiet_NaN();
131  }
132  // Variance of distribution
133  double getVariance() const {
134  return (_npix > 1) ? (_x2sum/_npix - _xsum/_npix * _xsum/_npix) * _npix/(_npix-1.) :
135  std::numeric_limits<double>::quiet_NaN();
136  }
137  // RMS
138  double getRms() const {
139  return sqrt(getVariance());
140  }
141  // Return the number of good pixels
142  int getNpix() const { return _npix; }
143 
144  // Return Sdqa rating
146  if ( fabs(getMean()) > policy.getDouble("maximumFootprintResidualMean") ) return false;
147  if ( getRms() > policy.getDouble("maximumFootprintResidualStd") ) return false;
148  return true;
149  }
150 
151  private:
152  double _xsum;
153  double _x2sum;
154  int _npix;
156  };
157 
158 
159 }}} // end of namespace lsst::ip::diffim
160 
161 
162 #endif
163 
164 
165 
int y
boost::uint16_t MaskPixel
StringArray getStringArray(const std::string &name) const
Definition: Policy.h:1023
int getWidth() const
Return the number of columns in the image.
Definition: MaskedImage.h:901
void apply(lsst::afw::image::MaskedImage< PixelT > const &image)
a container for holding hierarchical configuration data in memory.
Definition: Policy.h:169
definition of the Trace messaging facilities
lsst::afw::image::MaskPixel _bpMask
double getDouble(const std::string &name) const
Definition: Policy.h:617
void setBpMask(lsst::afw::image::MaskPixel bpMask)
int const x0
Definition: saturated.cc:45
lsst::afw::image::MaskedImage< PixelT >::x_iterator x_iterator
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
An iterator to the MaskedImage.
Definition: MaskedImage.h:109
x_iterator x_at(int x, int y) const
Return an x_iterator at the point (x, y)
Definition: MaskedImage.h:1006
An include file to include the header files for lsst::afw::image.
static MaskPixelT getPlaneBitMask(const std::vector< std::string > &names)
Return the bitmask corresponding to a vector of plane names OR&#39;d together.
Definition: Mask.cc:860
boost::shared_ptr< ImageStatistics > Ptr
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:77
Class to calculate difference image statistics.
virtual char const * what(void) const
Definition: Exception.cc:112
ImageStatistics(lsst::pex::policy::Policy const &policy)
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
bool evaluateQuality(lsst::pex::policy::Policy const &policy)
void apply(lsst::afw::image::MaskedImage< PixelT > const &image, int core)
int const y0
Definition: saturated.cc:45
int getHeight() const
Return the number of rows in the image.
Definition: MaskedImage.h:903
lsst::afw::image::MaskPixel getBpMask()