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
Threshold.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008, 2009, 2010 LSST Corporation.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <http://www.lsstcorp.org/LegalNotices/>.
21  */
22 #include <cmath>
23 #include <string>
24 #include <boost/format.hpp>
25 #include "lsst/pex/exceptions.h"
26 #include "lsst/pex/logging/Trace.h"
28 #include "lsst/afw/image/Image.h"
31 
32 namespace image = lsst::afw::image;
33 namespace math = lsst::afw::math;
34 namespace pexLogging = lsst::pex::logging;
35 
36 namespace lsst {
37 namespace afw {
38 namespace detection {
39 
41  if (typeStr.compare("bitmask") == 0) {
42  return Threshold::BITMASK;
43  } else if (typeStr.compare("value") == 0) {
44  return Threshold::VALUE;
45  } else if (typeStr.compare("stdev") == 0) {
46  return Threshold::STDEV;
47  } else if (typeStr.compare("variance") == 0) {
48  return Threshold::VARIANCE;
49  } else if (typeStr.compare("pixel_stdev") == 0) {
51  } else {
52  throw LSST_EXCEPT(
53  lsst::pex::exceptions::InvalidParameterError,
54  (boost::format("Unsupported Threshold type: %s") % typeStr).str()
55  );
56  }
57 }
58 
59 std::string Threshold::getTypeString(ThresholdType const & type) {
60  if (type == VALUE) {
61  return "value";
62  } else if (type == STDEV) {
63  return "stdev";
64  } else if (type == VARIANCE) {
65  return "variance";
66  } else {
67  throw LSST_EXCEPT(
68  lsst::pex::exceptions::InvalidParameterError,
69  (boost::format("Unsopported Threshold type: %d") % type).str()
70  );
71  }
72 }
73 
79 double Threshold::getValue(const double param) const {
80  switch (_type) {
81  case STDEV:
82  if (param <= 0) {
83  throw LSST_EXCEPT(
84  lsst::pex::exceptions::InvalidParameterError,
85  (boost::format("St. dev. must be > 0: %g") % param).str()
86  );
87  }
88  return _value*param;
89  case VALUE:
90  case BITMASK:
91  case PIXEL_STDEV:
92  return _value;
93  case VARIANCE:
94  if (param <= 0) {
95  throw LSST_EXCEPT(
96  lsst::pex::exceptions::InvalidParameterError,
97  (boost::format("Variance must be > 0: %g") % param).str()
98  );
99  }
100  return _value*std::sqrt(param);
101  default:
102  throw LSST_EXCEPT(
103  lsst::pex::exceptions::InvalidParameterError,
104  (boost::format("Unsupported type: %d") % _type).str()
105  );
106  }
107 }
108 
109 template<typename ImageT>
110 double Threshold::getValue(ImageT const& image) const {
111  double param = -1; // Parameter for getValue()
112  if (_type == STDEV ||
113  _type == VARIANCE) {
115  double const sd = stats.getValue(math::STDEVCLIP);
116 
117  pexLogging::TTrace<3>("afw.detection", "St. Dev = %g", sd);
118 
119  if (_type == VARIANCE) {
120  param = sd*sd;
121  } else {
122  param = sd;
123  }
124  }
125  return getValue(param);
126 }
127 
139  double const value,
140  std::string const typeStr,
141  bool const polarity
142 ) {
143  return Threshold(value, Threshold::parseTypeString(typeStr), polarity);
144 }
145 
146 
147 //
148 // Explicit instantiations
149 //
150 #define INSTANTIATE(TYPE) \
151 template double Threshold::getValue(image::TYPE<unsigned short> const&) const; \
152 template double Threshold::getValue(image::TYPE<int> const&) const; \
153 template double Threshold::getValue(image::TYPE<float> const&) const; \
154 template double Threshold::getValue(image::TYPE<double> const&) const; \
155 template double Threshold::getValue(image::TYPE<boost::uint64_t> const&) const;
156 
157 #ifndef DOXYGEN
158 INSTANTIATE(Image);
159 INSTANTIATE(MaskedImage);
160 #endif
161 
162 }}}
double getValue(Property const prop=NOTHING) const
Return the value of the desired property (if specified in the constructor)
Definition: Statistics.cc:1009
definition of the Trace messaging facilities
Use number of sigma given per-pixel s.d.
Definition: Threshold.h:52
A Threshold is used to pass a threshold value to detection algorithms.
Definition: Threshold.h:44
double _value
value of threshold, to be interpreted via _type
Definition: Threshold.h:100
ThresholdType _type
type of threshold
Definition: Threshold.h:101
estimate sample N-sigma clipped stdev (N set in StatisticsControl, default=3)
Definition: Statistics.h:73
Vector param
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
Use (pixels &amp; (given mask))
Definition: Threshold.h:49
static std::string getTypeString(ThresholdType const &type)
Definition: Threshold.cc:59
Use number of sigma given variance.
Definition: Threshold.h:51
Support for 2-D images.
Threshold createThreshold(const double value, const std::string type="value", const bool polarity=true)
Factory method for creating Threshold objects.
Definition: Threshold.cc:138
Represent a detection threshold.
double getValue(const double param=-1) const
Definition: Threshold.cc:79
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
ThresholdType
Types of threshold:
Definition: Threshold.h:47
Use number of sigma given s.d.
Definition: Threshold.h:50
Statistics makeStatistics(afwImage::Mask< afwImage::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl)
Specialization to handle Masks.
Definition: Statistics.cc:1082
Compute Image Statistics.
static ThresholdType parseTypeString(std::string const &typeStr)
Definition: Threshold.cc:40
Implementation of the Class MaskedImage.
#define INSTANTIATE(TYPE)
Definition: Threshold.cc:150
Include files required for standard LSST Exception handling.