LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
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 <cstdint>
23 #include <cmath>
24 #include <string>
25 #include <boost/format.hpp>
26 #include "lsst/pex/exceptions.h"
27 #include "lsst/log/Log.h"
29 #include "lsst/afw/image/Image.h"
32 
33 namespace image = lsst::afw::image;
34 namespace math = lsst::afw::math;
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  LOGL_DEBUG("afw.detection.threshold", "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<std::uint64_t> const&) const;
156 
157 #ifndef DOXYGEN
158 INSTANTIATE(Image);
159 INSTANTIATE(MaskedImage);
160 #endif
161 
162 }}}
double _value
value of threshold, to be interpreted via _type
Definition: Threshold.h:100
static std::string getTypeString(ThresholdType const &type)
Definition: Threshold.cc:59
double getValue(const double param=-1) const
return value of threshold, to be interpreted via type
Definition: Threshold.cc:79
Include files required for standard LSST Exception handling.
Use (pixels &amp; (given mask))
Definition: Threshold.h:49
A Threshold is used to pass a threshold value to detection algorithms.
Definition: Threshold.h:44
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
Definition: Log.h:513
A class to evaluate image statistics.
Definition: Statistics.h:212
Vector param
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
LSST DM logging module built on log4cxx.
Use number of sigma given s.d.
Definition: Threshold.h:50
estimate sample N-sigma clipped stdev (N set in StatisticsControl, default=3)
Definition: Statistics.h:73
double getValue(Property const prop=NOTHING) const
Return the value of the desired property (if specified in the constructor)
Definition: Statistics.cc:1034
ThresholdType _type
type of threshold
Definition: Threshold.h:101
Support for 2-D images.
Threshold createThreshold(double const value, std::string const typeStr, bool const polarity)
Factory method for creating Threshold objects.
Definition: Threshold.cc:138
Represent a detection threshold.
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
Definition: Exception.h:46
ThresholdType
Types of threshold:
Definition: Threshold.h:47
Use number of sigma given variance.
Definition: Threshold.h:51
Statistics makeStatistics(afwImage::Mask< afwImage::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl)
Specialization to handle Masks.
Definition: Statistics.cc:1107
Compute Image Statistics.
Implementation of the Class MaskedImage.
static ThresholdType parseTypeString(std::string const &typeStr)
Definition: Threshold.cc:40
Use number of sigma given per-pixel s.d.
Definition: Threshold.h:52
#define INSTANTIATE(TYPE)
Definition: Threshold.cc:150