LSSTApplications  17.0+11,17.0+34,17.0+56,17.0+57,17.0+59,17.0+7,17.0-1-g377950a+33,17.0.1-1-g114240f+2,17.0.1-1-g4d4fbc4+28,17.0.1-1-g55520dc+49,17.0.1-1-g5f4ed7e+52,17.0.1-1-g6dd7d69+17,17.0.1-1-g8de6c91+11,17.0.1-1-gb9095d2+7,17.0.1-1-ge9fec5e+5,17.0.1-1-gf4e0155+55,17.0.1-1-gfc65f5f+50,17.0.1-1-gfc6fb1f+20,17.0.1-10-g87f9f3f+1,17.0.1-11-ge9de802+16,17.0.1-16-ga14f7d5c+4,17.0.1-17-gc79d625+1,17.0.1-17-gdae4c4a+8,17.0.1-2-g26618f5+29,17.0.1-2-g54f2ebc+9,17.0.1-2-gf403422+1,17.0.1-20-g2ca2f74+6,17.0.1-23-gf3eadeb7+1,17.0.1-3-g7e86b59+39,17.0.1-3-gb5ca14a,17.0.1-3-gd08d533+40,17.0.1-30-g596af8797,17.0.1-4-g59d126d+4,17.0.1-4-gc69c472+5,17.0.1-6-g5afd9b9+4,17.0.1-7-g35889ee+1,17.0.1-7-gc7c8782+18,17.0.1-9-gc4bbfb2+3,w.2019.22
LSSTDataManagementBasePackage
Background.h
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008-2015 AURA/LSST.
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 <https://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 #if !defined(LSST_AFW_MATH_BACKGROUND_H)
26 #define LSST_AFW_MATH_BACKGROUND_H
27 /*
28  * Estimate image backgrounds
29  */
30 #include <boost/preprocessor/seq.hpp>
31 #include <memory>
32 #include "lsst/daf/base/Citizen.h"
33 #include "lsst/pex/exceptions.h"
34 #include "lsst/geom/Box.h"
38 
39 namespace lsst {
40 namespace afw {
41 namespace math {
42 
43 //
44 // Remember to update stringToUndersampleStyle if you change this.
45 // If this happens often, we can play CPP games to put the definition in exactly one place, although swig
46 // may not be happy (so we could think m4 thoughts instead)
47 //
53 
58 public:
66  BackgroundControl(int const nxSample, int const nySample,
67  StatisticsControl const sctrl = StatisticsControl(), Property const prop = MEANCLIP,
69  : _style(Interpolate::AKIMA_SPLINE),
70  _nxSample(nxSample),
71  _nySample(nySample),
72  _undersampleStyle(THROW_EXCEPTION),
73  _sctrl(new StatisticsControl(sctrl)),
74  _prop(prop),
75  _actrl(new ApproximateControl(actrl)) {
76  if (nxSample <= 0 || nySample <= 0) {
78  str(boost::format("You must specify at least one point, not %dx%d") % nxSample %
79  nySample));
80  }
81  }
82 
92  BackgroundControl(int const nxSample, int const nySample, StatisticsControl const& sctrl,
93  std::string const& prop,
95  : _style(Interpolate::AKIMA_SPLINE),
96  _nxSample(nxSample),
97  _nySample(nySample),
98  _undersampleStyle(THROW_EXCEPTION),
99  _sctrl(new StatisticsControl(sctrl)),
100  _prop(stringToStatisticsProperty(prop)),
101  _actrl(new ApproximateControl(actrl)) {
102  if (nxSample <= 0 || nySample <= 0) {
104  str(boost::format("You must specify at least one point, not %dx%d") % nxSample %
105  nySample));
106  }
107  }
108  // And now the two old APIs (preserved for backward compatibility)
120  BackgroundControl(Interpolate::Style const style, int const nxSample = 10, int const nySample = 10,
121  UndersampleStyle const undersampleStyle = THROW_EXCEPTION,
122  StatisticsControl const sctrl = StatisticsControl(), Property const prop = MEANCLIP,
124 
125  )
126  : _style(style),
127  _nxSample(nxSample),
128  _nySample(nySample),
129  _undersampleStyle(undersampleStyle),
130  _sctrl(new StatisticsControl(sctrl)),
131  _prop(prop),
132  _actrl(new ApproximateControl(actrl)) {
133  if (nxSample <= 0 || nySample <= 0) {
135  str(boost::format("You must specify at least one point, not %dx%d") % nxSample %
136  nySample));
137  }
138  }
139 
153  BackgroundControl(std::string const& style, int const nxSample = 10, int const nySample = 10,
154  std::string const& undersampleStyle = "THROW_EXCEPTION",
155  StatisticsControl const sctrl = StatisticsControl(),
156  std::string const& prop = "MEANCLIP",
158  : _style(math::stringToInterpStyle(style)),
159  _nxSample(nxSample),
160  _nySample(nySample),
161  _undersampleStyle(math::stringToUndersampleStyle(undersampleStyle)),
162  _sctrl(new StatisticsControl(sctrl)),
163  _prop(stringToStatisticsProperty(prop)),
164  _actrl(new ApproximateControl(actrl)) {
165  if (nxSample <= 0 || nySample <= 0) {
167  str(boost::format("You must specify at least one point, not %dx%d") % nxSample %
168  nySample));
169  }
170  }
171 
172  BackgroundControl(BackgroundControl const&) = default;
174  BackgroundControl& operator=(BackgroundControl const&) = default;
176 
177  virtual ~BackgroundControl() = default;
178  void setNxSample(int nxSample) {
179  if (nxSample <= 0) {
181  str(boost::format("nxSample must be position, not %d") % nxSample));
182  }
183  _nxSample = nxSample;
184  }
185  void setNySample(int nySample) {
186  if (nySample <= 0) {
188  str(boost::format("nySample must be position, not %d") % nySample));
189  }
190  _nySample = nySample;
191  }
192 
193  void setInterpStyle(Interpolate::Style const style) { _style = style; }
194  // overload to take a string
195  void setInterpStyle(std::string const& style) { _style = math::stringToInterpStyle(style); }
196 
197  void setUndersampleStyle(UndersampleStyle const undersampleStyle) {
198  _undersampleStyle = undersampleStyle;
199  }
200  // overload to take a string
201  void setUndersampleStyle(std::string const& undersampleStyle) {
202  _undersampleStyle = math::stringToUndersampleStyle(undersampleStyle);
203  }
204 
205  int getNxSample() const { return _nxSample; }
206  int getNySample() const { return _nySample; }
208  if (_style < 0 || _style >= Interpolate::NUM_STYLES) {
210  str(boost::format("Style %d is invalid") % _style));
211  }
212  return _style;
213  }
214  UndersampleStyle getUndersampleStyle() const { return _undersampleStyle; }
217 
218  Property getStatisticsProperty() const { return _prop; }
219  void setStatisticsProperty(Property prop) { _prop = prop; }
221 
225 
226 private:
227  Interpolate::Style _style; // style of interpolation to use
228  int _nxSample; // number of grid squares to divide image into to sample in x
229  int _nySample; // number of grid squares to divide image into to sample in y
230  UndersampleStyle _undersampleStyle; // what to do when nx,ny are too small for the requested interp style
231  std::shared_ptr<StatisticsControl> _sctrl; // statistics control object
232  Property _prop; // statistics Property
233  std::shared_ptr<ApproximateControl> _actrl; // approximate control object
234 };
235 
240 protected:
254  template <typename ImageT>
255  explicit Background(ImageT const& img, BackgroundControl const& bgCtrl);
256 
267  explicit Background(lsst::geom::Box2I const imageBBox, int const nx, int const ny);
269  virtual ~Background() = default;
270 
271 public:
272  typedef float InternalPixelT;
273 
274  Background(Background const&) = delete;
275  Background(Background&&) = delete;
276  Background& operator=(Background const&) = delete;
277  Background& operator=(Background&&) = delete;
278 
280  virtual Background& operator+=(float const delta) = 0;
282  virtual Background& operator-=(float const delta) = 0;
290  template <typename PixelT>
292  Interpolate::Style const interpStyle,
293  UndersampleStyle const undersampleStyle = THROW_EXCEPTION) const {
294  return getImage<PixelT>(_imgBBox, interpStyle, undersampleStyle);
295  }
303  template <typename PixelT>
305  std::string const& interpStyle, std::string const& undersampleStyle = "THROW_EXCEPTION") const {
306  return getImage<PixelT>(math::stringToInterpStyle(interpStyle),
307  stringToUndersampleStyle(undersampleStyle));
308  }
314  template <typename PixelT>
316  lsst::geom::Box2I const& bbox, Interpolate::Style const interpStyle,
317  UndersampleStyle const undersampleStyle = THROW_EXCEPTION) const {
318  return _getImage(bbox, interpStyle, undersampleStyle, static_cast<PixelT>(0));
319  }
325  template <typename PixelT>
327  lsst::geom::Box2I const& bbox, std::string const& interpStyle,
328  std::string const& undersampleStyle = "THROW_EXCEPTION") const {
329  return _getImage(bbox, math::stringToInterpStyle(interpStyle),
330  stringToUndersampleStyle(undersampleStyle), static_cast<PixelT>(0));
331  }
332 
337  template <typename PixelT>
339  return getImage<PixelT>(_bctrl->getInterpStyle(), _bctrl->getUndersampleStyle());
340  }
346  Interpolate::Style getAsUsedInterpStyle() const { return _asUsedInterpStyle; }
350  UndersampleStyle getAsUsedUndersampleStyle() const { return _asUsedUndersampleStyle; }
358  ApproximateControl const& actrl,
359  UndersampleStyle const undersampleStyle = THROW_EXCEPTION) const {
360  InternalPixelT disambiguate = 0;
361  return _getApproximate(actrl, undersampleStyle, disambiguate);
362  }
366  lsst::geom::Box2I getImageBBox() const { return _imgBBox; }
367 
370 
371 protected:
376 
383  /*
384  * We want getImage to be present in the base class, but a templated virtual function
385  * is impossible. So we'll solve the dilemma with a hack: explicitly defined
386  * virtual functions for the image types we need
387  */
388 // We'll evaluate LSST_makeBackground_get{Approximation,Image} for each type in
389 // LSST_makeBackground_get{Approximation,Image}_types,
390 // setting v to the second arg (i.e. "= 0" for the first invocation). The first agument, m, is ignores
391 
392 // Desired types
393 #define LSST_makeBackground_getImage_types (Background::InternalPixelT)
394 #define LSST_makeBackground_getApproximate_types (Background::InternalPixelT)
395 #define LSST_makeBackground_getImage(m, v, T) \
396  virtual std::shared_ptr<lsst::afw::image::Image<T>> _getImage( \
397  lsst::geom::Box2I const& bbox, \
398  Interpolate::Style const interpStyle, /* Style of the interpolation */ \
399  UndersampleStyle const undersampleStyle = \
400  THROW_EXCEPTION, /* Behaviour if there are too few points */ \
401  T = 0 /* disambiguate */ \
402  ) const v;
403 
404 #define LSST_makeBackground_getApproximate(m, v, T) \
405  virtual std::shared_ptr<Approximate<T>> _getApproximate( \
406  ApproximateControl const& actrl, /* Approximation style */ \
407  UndersampleStyle const undersampleStyle = \
408  THROW_EXCEPTION, /* Behaviour if there are too few points */ \
409  T = 0 /* disambiguate */ \
410  ) const v;
411 
414 private:
419  void _setCenOrigSize(int const width, int const height, int const nxSample, int const nySample);
420 };
421 
444 class BackgroundMI : public Background {
445 public:
446  template <typename ImageT>
473  explicit BackgroundMI(ImageT const& img, BackgroundControl const& bgCtrl);
480  explicit BackgroundMI(lsst::geom::Box2I const imageDimensions,
481  image::MaskedImage<InternalPixelT> const& statsImage);
482 
483  BackgroundMI(BackgroundMI const&) = delete;
484  BackgroundMI(BackgroundMI&&) = delete;
485  BackgroundMI& operator=(BackgroundMI const&) = delete;
486  BackgroundMI& operator=(BackgroundMI&&) = delete;
487  ~BackgroundMI() override = default;
488 
494  BackgroundMI& operator+=(float const delta) override;
500  BackgroundMI& operator-=(float const delta) override;
501 
514  double getPixel(Interpolate::Style const style, int const x, int const y) const;
522  double getPixel(int const x, int const y) const { return getPixel(_bctrl->getInterpStyle(), x, y); }
527 
528 private:
530  _statsImage; // statistical properties for the grid of subimages
531  mutable std::vector<std::vector<double>> _gridColumns; // interpolated columns for the bicubic spline
532 
533  void _setGridColumns(Interpolate::Style const interpStyle, UndersampleStyle const undersampleStyle,
534  int const iX, std::vector<int> const& ypix) const;
535 
536 #if defined(LSST_makeBackground_getImage)
540 #undef LSST_makeBackground_getImage
541 #undef LSST_makeBackground_getApproximate
542 #endif
543  // Here's the worker function for _getImage (non-virtual; it's templated in BackgroundMI, not Background)
547  template <typename PixelT>
549  Interpolate::Style const interpStyle_,
550  UndersampleStyle const undersampleStyle) const;
551  // and for _getApproximate
552  template <typename PixelT>
553  std::shared_ptr<Approximate<PixelT>> doGetApproximate(ApproximateControl const& actrl,
554  UndersampleStyle const undersampleStyle) const;
555 };
561 template <typename ImageT>
563  return std::shared_ptr<Background>(new BackgroundMI(img, bgCtrl));
564 }
565 } // namespace math
566 } // namespace afw
567 } // namespace lsst
568 
569 #endif // LSST_AFW_MATH_BACKGROUND_H
float InternalPixelT
type used for any internal images, and returned by getApproximate
Definition: Background.h:272
std::vector< int > _xsize
x size of sub images
Definition: Background.h:381
UndersampleStyle stringToUndersampleStyle(std::string const &style)
Conversion function to switch a string to an UndersampleStyle.
Definition: Background.cc:119
Interpolate::Style getAsUsedInterpStyle() const
Return the Interpolate::Style that we actually used in the last call to getImage() ...
Definition: Background.h:346
std::vector< double > _xcen
x center pix coords of sub images
Definition: Background.h:377
Interpolate::Style stringToInterpStyle(std::string const &style)
Conversion function to switch a string to an Interpolate::Style.
Definition: Interpolate.cc:257
std::shared_ptr< lsst::afw::image::Image< PixelT > > getImage(std::string const &interpStyle, std::string const &undersampleStyle="THROW_EXCEPTION") const
Method to interpolate and return the background for entire image.
Definition: Background.h:304
std::vector< int > _yorig
y origin ...
Definition: Background.h:380
void setNySample(int nySample)
Definition: Background.h:185
Image< LhsPixelT > & operator+=(Image< LhsPixelT > &lhs, Image< RhsPixelT > const &rhs)
Add lhs to Image rhs (i.e. pixel-by-pixel addition) where types are different.
Definition: Image.cc:686
lsst::geom::Box2I _imgBBox
size and origin of input image
Definition: Background.h:372
Property getStatisticsProperty() const
Definition: Background.h:218
Reports attempts to exceed implementation-defined length limits for some classes. ...
Definition: Runtime.h:76
Interpolate::Style getInterpStyle() const
Definition: Background.h:207
std::shared_ptr< math::Approximate< InternalPixelT > > getApproximate(ApproximateControl const &actrl, UndersampleStyle const undersampleStyle=THROW_EXCEPTION) const
Method to return an approximation to the background.
Definition: Background.h:357
virtual ~BackgroundControl()=default
int y
Definition: SpanSet.cc:49
BackgroundControl(Interpolate::Style const style, int const nxSample=10, int const nySample=10, UndersampleStyle const undersampleStyle=THROW_EXCEPTION, StatisticsControl const sctrl=StatisticsControl(), Property const prop=MEANCLIP, ApproximateControl const actrl=ApproximateControl(ApproximateControl::UNKNOWN, 1))
Definition: Background.h:120
std::shared_ptr< lsst::afw::image::Image< PixelT > > getImage(lsst::geom::Box2I const &bbox, std::string const &interpStyle, std::string const &undersampleStyle="THROW_EXCEPTION") const
Definition: Background.h:326
std::shared_ptr< Background > makeBackground(ImageT const &img, BackgroundControl const &bgCtrl)
A convenience function that uses function overloading to make the correct type of Background...
Definition: Background.h:562
std::shared_ptr< ApproximateControl > getApproximateControl()
Definition: Background.h:223
lsst::afw::image::MaskedImage< InternalPixelT > getStatsImage() const
Return the image of statistical quantities extracted from the image.
Definition: Background.h:526
void setInterpStyle(Interpolate::Style const style)
Definition: Background.h:193
#define LSST_makeBackground_getApproximate_types
Definition: Background.h:394
std::vector< double > _ycen
y center ...
Definition: Background.h:378
BackgroundControl(int const nxSample, int const nySample, StatisticsControl const &sctrl, std::string const &prop, ApproximateControl const actrl=ApproximateControl(ApproximateControl::UNKNOWN, 1))
Overload constructor to handle string for statistical operator.
Definition: Background.h:92
void setStatisticsProperty(std::string prop)
Definition: Background.h:220
Pass parameters to a Background object.
Definition: Background.h:57
UndersampleStyle getAsUsedUndersampleStyle() const
Return the UndersampleStyle that we actually used in the last call to getImage()
Definition: Background.h:350
std::shared_ptr< lsst::afw::image::Image< PixelT > > getImage(lsst::geom::Box2I const &bbox, Interpolate::Style const interpStyle, UndersampleStyle const undersampleStyle=THROW_EXCEPTION) const
Definition: Background.h:315
A virtual base class to evaluate image background levels.
Definition: Background.h:239
STL class.
Property stringToStatisticsProperty(std::string const property)
Conversion function to switch a string to a Property (see Statistics.h)
Definition: Statistics.cc:747
std::shared_ptr< lsst::afw::image::Image< PixelT > > getImage() const
Method to interpolate and return the background for entire image.
Definition: Background.h:338
Control how to make an approximation.
Definition: Approximate.h:48
void setUndersampleStyle(std::string const &undersampleStyle)
Definition: Background.h:201
Pass parameters to a Statistics object.
Definition: Statistics.h:93
estimate sample N-sigma clipped mean (N set in StatisticsControl, default=3)
Definition: Statistics.h:72
Interpolate::Style _asUsedInterpStyle
the style we actually used
Definition: Background.h:374
A base class for image defects.
std::shared_ptr< BackgroundControl > getBackgroundControl()
Definition: Background.h:368
std::shared_ptr< StatisticsControl > getStatisticsControl()
Definition: Background.h:215
std::shared_ptr< ApproximateControl const > getApproximateControl() const
Definition: Background.h:224
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:168
void setUndersampleStyle(UndersampleStyle const undersampleStyle)
Definition: Background.h:197
table::Box2IKey bbox
Definition: Detector.cc:169
BackgroundControl & operator=(BackgroundControl const &)=default
double x
BackgroundControl(std::string const &style, int const nxSample=10, int const nySample=10, std::string const &undersampleStyle="THROW_EXCEPTION", StatisticsControl const sctrl=StatisticsControl(), std::string const &prop="MEANCLIP", ApproximateControl const actrl=ApproximateControl(ApproximateControl::UNKNOWN, 1))
Overload constructor to handle strings for both interp and undersample styles.
Definition: Background.h:153
#define LSST_makeBackground_getImage_types
Definition: Background.h:393
#define LSST_makeBackground_getImage(m, v, T)
Definition: Background.h:395
UndersampleStyle getUndersampleStyle() const
Definition: Background.h:214
BOOST_PP_SEQ_FOR_EACH(INSTANTIATE_COLUMNVIEW_SCALAR, _, BOOST_PP_TUPLE_TO_SEQ(AFW_TABLE_SCALAR_FIELD_TYPE_N, AFW_TABLE_SCALAR_FIELD_TYPE_TUPLE)) BOOST_PP_SEQ_FOR_EACH(INSTANTIATE_COLUMNVIEW_ARRAY
lsst::geom::Box2I getImageBBox() const
Return the input image&#39;s (PARENT) bounding box.
Definition: Background.h:366
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
BackgroundControl(int const nxSample, int const nySample, StatisticsControl const sctrl=StatisticsControl(), Property const prop=MEANCLIP, ApproximateControl const actrl=ApproximateControl(ApproximateControl::UNKNOWN, 1))
Definition: Background.h:66
double getPixel(int const x, int const y) const
Return the background value at a point.
Definition: Background.h:522
std::vector< int > _xorig
x origin pix coords of sub images
Definition: Background.h:379
Image< LhsPixelT > & operator-=(Image< LhsPixelT > &lhs, Image< RhsPixelT > const &rhs)
Subtract lhs from Image rhs (i.e. pixel-by-pixel subtraction) where types are different.
Definition: Image.cc:692
std::shared_ptr< BackgroundControl > _bctrl
control info set by user.
Definition: Background.h:373
std::vector< int > _ysize
y size ...
Definition: Background.h:382
UndersampleStyle _asUsedUndersampleStyle
the undersampleStyle we actually used
Definition: Background.h:375
Reports invalid arguments.
Definition: Runtime.h:66
void setInterpStyle(std::string const &style)
Definition: Background.h:195
std::shared_ptr< lsst::afw::image::Image< PixelT > > getImage(Interpolate::Style const interpStyle, UndersampleStyle const undersampleStyle=THROW_EXCEPTION) const
Method to interpolate and return the background for entire image.
Definition: Background.h:291
std::shared_ptr< StatisticsControl const > getStatisticsControl() const
Definition: Background.h:216
#define LSST_makeBackground_getApproximate(m, v, T)
Definition: Background.h:404
A class to evaluate image background levels.
Definition: Background.h:444
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:55
Property
control what is calculated
Definition: Statistics.h:63
void setStatisticsProperty(Property prop)
Definition: Background.h:219
An integer coordinate rectangle.
Definition: Box.h:54
std::shared_ptr< BackgroundControl const > getBackgroundControl() const
Definition: Background.h:369
void setNxSample(int nxSample)
Definition: Background.h:178
void setApproximateControl(std::shared_ptr< ApproximateControl > actrl)
Definition: Background.h:222