LSSTApplications  17.0+124,17.0+14,17.0+73,18.0.0+37,18.0.0+80,18.0.0-4-g68ffd23+4,18.1.0-1-g0001055+12,18.1.0-1-g03d53ef+5,18.1.0-1-g1349e88+55,18.1.0-1-g2505f39+44,18.1.0-1-g5315e5e+4,18.1.0-1-g5e4b7ea+14,18.1.0-1-g7e8fceb+4,18.1.0-1-g85f8cd4+48,18.1.0-1-g8ff0b9f+4,18.1.0-1-ga2c679d+1,18.1.0-1-gd55f500+35,18.1.0-10-gb58edde+2,18.1.0-11-g0997b02+4,18.1.0-13-gfe4edf0b+12,18.1.0-14-g259bd21+21,18.1.0-19-gdb69f3f+2,18.1.0-2-g5f9922c+24,18.1.0-2-gd3b74e5+11,18.1.0-2-gfbf3545+32,18.1.0-26-g728bddb4+5,18.1.0-27-g6ff7ca9+2,18.1.0-3-g52aa583+25,18.1.0-3-g8ea57af+9,18.1.0-3-gb69f684+42,18.1.0-3-gfcaddf3+6,18.1.0-32-gd8786685a,18.1.0-4-gf3f9b77+6,18.1.0-5-g1dd662b+2,18.1.0-5-g6dbcb01+41,18.1.0-6-gae77429+3,18.1.0-7-g9d75d83+9,18.1.0-7-gae09a6d+30,18.1.0-9-gc381ef5+4,w.2019.45
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/pex/exceptions.h"
33 #include "lsst/geom/Box.h"
37 
38 namespace lsst {
39 namespace afw {
40 namespace math {
41 
42 //
43 // Remember to update stringToUndersampleStyle if you change this.
44 // If this happens often, we can play CPP games to put the definition in exactly one place, although swig
45 // may not be happy (so we could think m4 thoughts instead)
46 //
52 
57 public:
65  BackgroundControl(int const nxSample, int const nySample,
66  StatisticsControl const sctrl = StatisticsControl(), Property const prop = MEANCLIP,
68  : _style(Interpolate::AKIMA_SPLINE),
69  _nxSample(nxSample),
70  _nySample(nySample),
71  _undersampleStyle(THROW_EXCEPTION),
72  _sctrl(new StatisticsControl(sctrl)),
73  _prop(prop),
74  _actrl(new ApproximateControl(actrl)) {
75  if (nxSample <= 0 || nySample <= 0) {
77  str(boost::format("You must specify at least one point, not %dx%d") % nxSample %
78  nySample));
79  }
80  }
81 
91  BackgroundControl(int const nxSample, int const nySample, StatisticsControl const& sctrl,
92  std::string const& prop,
94  : _style(Interpolate::AKIMA_SPLINE),
95  _nxSample(nxSample),
96  _nySample(nySample),
97  _undersampleStyle(THROW_EXCEPTION),
98  _sctrl(new StatisticsControl(sctrl)),
99  _prop(stringToStatisticsProperty(prop)),
100  _actrl(new ApproximateControl(actrl)) {
101  if (nxSample <= 0 || nySample <= 0) {
103  str(boost::format("You must specify at least one point, not %dx%d") % nxSample %
104  nySample));
105  }
106  }
107  // And now the two old APIs (preserved for backward compatibility)
119  BackgroundControl(Interpolate::Style const style, int const nxSample = 10, int const nySample = 10,
120  UndersampleStyle const undersampleStyle = THROW_EXCEPTION,
121  StatisticsControl const sctrl = StatisticsControl(), Property const prop = MEANCLIP,
123 
124  )
125  : _style(style),
126  _nxSample(nxSample),
127  _nySample(nySample),
128  _undersampleStyle(undersampleStyle),
129  _sctrl(new StatisticsControl(sctrl)),
130  _prop(prop),
131  _actrl(new ApproximateControl(actrl)) {
132  if (nxSample <= 0 || nySample <= 0) {
134  str(boost::format("You must specify at least one point, not %dx%d") % nxSample %
135  nySample));
136  }
137  }
138 
152  BackgroundControl(std::string const& style, int const nxSample = 10, int const nySample = 10,
153  std::string const& undersampleStyle = "THROW_EXCEPTION",
154  StatisticsControl const sctrl = StatisticsControl(),
155  std::string const& prop = "MEANCLIP",
157  : _style(math::stringToInterpStyle(style)),
158  _nxSample(nxSample),
159  _nySample(nySample),
160  _undersampleStyle(math::stringToUndersampleStyle(undersampleStyle)),
161  _sctrl(new StatisticsControl(sctrl)),
162  _prop(stringToStatisticsProperty(prop)),
163  _actrl(new ApproximateControl(actrl)) {
164  if (nxSample <= 0 || nySample <= 0) {
166  str(boost::format("You must specify at least one point, not %dx%d") % nxSample %
167  nySample));
168  }
169  }
170 
171  BackgroundControl(BackgroundControl const&) = default;
173  BackgroundControl& operator=(BackgroundControl const&) = default;
175 
176  virtual ~BackgroundControl() = default;
177  void setNxSample(int nxSample) {
178  if (nxSample <= 0) {
180  str(boost::format("nxSample must be position, not %d") % nxSample));
181  }
182  _nxSample = nxSample;
183  }
184  void setNySample(int nySample) {
185  if (nySample <= 0) {
187  str(boost::format("nySample must be position, not %d") % nySample));
188  }
189  _nySample = nySample;
190  }
191 
192  void setInterpStyle(Interpolate::Style const style) { _style = style; }
193  // overload to take a string
194  void setInterpStyle(std::string const& style) { _style = math::stringToInterpStyle(style); }
195 
196  void setUndersampleStyle(UndersampleStyle const undersampleStyle) {
197  _undersampleStyle = undersampleStyle;
198  }
199  // overload to take a string
200  void setUndersampleStyle(std::string const& undersampleStyle) {
201  _undersampleStyle = math::stringToUndersampleStyle(undersampleStyle);
202  }
203 
204  int getNxSample() const { return _nxSample; }
205  int getNySample() const { return _nySample; }
207  if (_style < 0 || _style >= Interpolate::NUM_STYLES) {
209  str(boost::format("Style %d is invalid") % _style));
210  }
211  return _style;
212  }
213  UndersampleStyle getUndersampleStyle() const { return _undersampleStyle; }
216 
217  Property getStatisticsProperty() const { return _prop; }
218  void setStatisticsProperty(Property prop) { _prop = prop; }
220 
224 
225 private:
226  Interpolate::Style _style; // style of interpolation to use
227  int _nxSample; // number of grid squares to divide image into to sample in x
228  int _nySample; // number of grid squares to divide image into to sample in y
229  UndersampleStyle _undersampleStyle; // what to do when nx,ny are too small for the requested interp style
230  std::shared_ptr<StatisticsControl> _sctrl; // statistics control object
231  Property _prop; // statistics Property
232  std::shared_ptr<ApproximateControl> _actrl; // approximate control object
233 };
234 
238 class Background {
239 protected:
253  template <typename ImageT>
254  explicit Background(ImageT const& img, BackgroundControl const& bgCtrl);
255 
266  explicit Background(lsst::geom::Box2I const imageBBox, int const nx, int const ny);
268  virtual ~Background() = default;
269 
270 public:
271  typedef float InternalPixelT;
272 
273  Background(Background const&) = delete;
274  Background(Background&&) = delete;
275  Background& operator=(Background const&) = delete;
276  Background& operator=(Background&&) = delete;
277 
279  virtual Background& operator+=(float const delta) = 0;
281  virtual Background& operator-=(float const delta) = 0;
289  template <typename PixelT>
291  Interpolate::Style const interpStyle,
292  UndersampleStyle const undersampleStyle = THROW_EXCEPTION) const {
293  return getImage<PixelT>(_imgBBox, interpStyle, undersampleStyle);
294  }
302  template <typename PixelT>
304  std::string const& interpStyle, std::string const& undersampleStyle = "THROW_EXCEPTION") const {
305  return getImage<PixelT>(math::stringToInterpStyle(interpStyle),
306  stringToUndersampleStyle(undersampleStyle));
307  }
313  template <typename PixelT>
315  lsst::geom::Box2I const& bbox, Interpolate::Style const interpStyle,
316  UndersampleStyle const undersampleStyle = THROW_EXCEPTION) const {
317  return _getImage(bbox, interpStyle, undersampleStyle, static_cast<PixelT>(0));
318  }
324  template <typename PixelT>
326  lsst::geom::Box2I const& bbox, std::string const& interpStyle,
327  std::string const& undersampleStyle = "THROW_EXCEPTION") const {
328  return _getImage(bbox, math::stringToInterpStyle(interpStyle),
329  stringToUndersampleStyle(undersampleStyle), static_cast<PixelT>(0));
330  }
331 
336  template <typename PixelT>
338  return getImage<PixelT>(_bctrl->getInterpStyle(), _bctrl->getUndersampleStyle());
339  }
345  Interpolate::Style getAsUsedInterpStyle() const { return _asUsedInterpStyle; }
349  UndersampleStyle getAsUsedUndersampleStyle() const { return _asUsedUndersampleStyle; }
357  ApproximateControl const& actrl,
358  UndersampleStyle const undersampleStyle = THROW_EXCEPTION) const {
359  InternalPixelT disambiguate = 0;
360  return _getApproximate(actrl, undersampleStyle, disambiguate);
361  }
365  lsst::geom::Box2I getImageBBox() const { return _imgBBox; }
366 
369 
370 protected:
375 
382  /*
383  * We want getImage to be present in the base class, but a templated virtual function
384  * is impossible. So we'll solve the dilemma with a hack: explicitly defined
385  * virtual functions for the image types we need
386  */
387 // We'll evaluate LSST_makeBackground_get{Approximation,Image} for each type in
388 // LSST_makeBackground_get{Approximation,Image}_types,
389 // setting v to the second arg (i.e. "= 0" for the first invocation). The first agument, m, is ignores
390 
391 // Desired types
392 #define LSST_makeBackground_getImage_types (Background::InternalPixelT)
393 #define LSST_makeBackground_getApproximate_types (Background::InternalPixelT)
394 #define LSST_makeBackground_getImage(m, v, T) \
395  virtual std::shared_ptr<lsst::afw::image::Image<T>> _getImage( \
396  lsst::geom::Box2I const& bbox, \
397  Interpolate::Style const interpStyle, /* Style of the interpolation */ \
398  UndersampleStyle const undersampleStyle = \
399  THROW_EXCEPTION, /* Behaviour if there are too few points */ \
400  T = 0 /* disambiguate */ \
401  ) const v;
402 
403 #define LSST_makeBackground_getApproximate(m, v, T) \
404  virtual std::shared_ptr<Approximate<T>> _getApproximate( \
405  ApproximateControl const& actrl, /* Approximation style */ \
406  UndersampleStyle const undersampleStyle = \
407  THROW_EXCEPTION, /* Behaviour if there are too few points */ \
408  T = 0 /* disambiguate */ \
409  ) const v;
410 
413 private:
418  void _setCenOrigSize(int const width, int const height, int const nxSample, int const nySample);
419 };
420 
443 class BackgroundMI : public Background {
444 public:
445  template <typename ImageT>
472  explicit BackgroundMI(ImageT const& img, BackgroundControl const& bgCtrl);
479  explicit BackgroundMI(lsst::geom::Box2I const imageDimensions,
480  image::MaskedImage<InternalPixelT> const& statsImage);
481 
482  BackgroundMI(BackgroundMI const&) = delete;
483  BackgroundMI(BackgroundMI&&) = delete;
484  BackgroundMI& operator=(BackgroundMI const&) = delete;
485  BackgroundMI& operator=(BackgroundMI&&) = delete;
486  ~BackgroundMI() override = default;
487 
493  BackgroundMI& operator+=(float const delta) override;
499  BackgroundMI& operator-=(float const delta) override;
500 
513  double getPixel(Interpolate::Style const style, int const x, int const y) const;
521  double getPixel(int const x, int const y) const { return getPixel(_bctrl->getInterpStyle(), x, y); }
526 
527 private:
529  _statsImage; // statistical properties for the grid of subimages
530  mutable std::vector<std::vector<double>> _gridColumns; // interpolated columns for the bicubic spline
531 
532  void _setGridColumns(Interpolate::Style const interpStyle, UndersampleStyle const undersampleStyle,
533  int const iX, std::vector<int> const& ypix) const;
534 
535 #if defined(LSST_makeBackground_getImage)
539 #undef LSST_makeBackground_getImage
540 #undef LSST_makeBackground_getApproximate
541 #endif
542  // Here's the worker function for _getImage (non-virtual; it's templated in BackgroundMI, not Background)
546  template <typename PixelT>
548  Interpolate::Style const interpStyle_,
549  UndersampleStyle const undersampleStyle) const;
550  // and for _getApproximate
551  template <typename PixelT>
552  std::shared_ptr<Approximate<PixelT>> doGetApproximate(ApproximateControl const& actrl,
553  UndersampleStyle const undersampleStyle) const;
554 };
560 template <typename ImageT>
562  return std::shared_ptr<Background>(new BackgroundMI(img, bgCtrl));
563 }
564 } // namespace math
565 } // namespace afw
566 } // namespace lsst
567 
568 #endif // LSST_AFW_MATH_BACKGROUND_H
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
float InternalPixelT
type used for any internal images, and returned by getApproximate
Definition: Background.h:271
std::vector< int > _xsize
x size of sub images
Definition: Background.h:380
UndersampleStyle stringToUndersampleStyle(std::string const &style)
Conversion function to switch a string to an UndersampleStyle.
Definition: Background.cc:117
Interpolate::Style getAsUsedInterpStyle() const
Return the Interpolate::Style that we actually used in the last call to getImage() ...
Definition: Background.h:345
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
std::vector< double > _xcen
x center pix coords of sub images
Definition: Background.h:376
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:303
std::vector< int > _yorig
y origin ...
Definition: Background.h:379
void setNySample(int nySample)
Definition: Background.h:184
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:671
lsst::geom::Box2I _imgBBox
size and origin of input image
Definition: Background.h:371
Property getStatisticsProperty() const
Definition: Background.h:217
Reports attempts to exceed implementation-defined length limits for some classes. ...
Definition: Runtime.h:76
Interpolate::Style getInterpStyle() const
Definition: Background.h:206
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:356
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:119
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:325
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:561
std::shared_ptr< ApproximateControl > getApproximateControl()
Definition: Background.h:222
lsst::afw::image::MaskedImage< InternalPixelT > getStatsImage() const
Return the image of statistical quantities extracted from the image.
Definition: Background.h:525
void setInterpStyle(Interpolate::Style const style)
Definition: Background.h:192
#define LSST_makeBackground_getApproximate_types
Definition: Background.h:393
std::vector< double > _ycen
y center ...
Definition: Background.h:377
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:91
void setStatisticsProperty(std::string prop)
Definition: Background.h:219
Pass parameters to a Background object.
Definition: Background.h:56
UndersampleStyle getAsUsedUndersampleStyle() const
Return the UndersampleStyle that we actually used in the last call to getImage()
Definition: Background.h:349
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:314
A virtual base class to evaluate image background levels.
Definition: Background.h:238
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:337
Control how to make an approximation.
Definition: Approximate.h:48
void setUndersampleStyle(std::string const &undersampleStyle)
Definition: Background.h:200
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:373
A base class for image defects.
std::shared_ptr< BackgroundControl > getBackgroundControl()
Definition: Background.h:367
std::shared_ptr< StatisticsControl > getStatisticsControl()
Definition: Background.h:214
std::shared_ptr< ApproximateControl const > getApproximateControl() const
Definition: Background.h:223
void setUndersampleStyle(UndersampleStyle const undersampleStyle)
Definition: Background.h:196
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:152
#define LSST_makeBackground_getImage_types
Definition: Background.h:392
#define LSST_makeBackground_getImage(m, v, T)
Definition: Background.h:394
UndersampleStyle getUndersampleStyle() const
Definition: Background.h:213
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:365
#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:65
double getPixel(int const x, int const y) const
Return the background value at a point.
Definition: Background.h:521
std::vector< int > _xorig
x origin pix coords of sub images
Definition: Background.h:378
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:677
std::shared_ptr< BackgroundControl > _bctrl
control info set by user.
Definition: Background.h:372
std::vector< int > _ysize
y size ...
Definition: Background.h:381
UndersampleStyle _asUsedUndersampleStyle
the undersampleStyle we actually used
Definition: Background.h:374
Reports invalid arguments.
Definition: Runtime.h:66
void setInterpStyle(std::string const &style)
Definition: Background.h:194
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:290
std::shared_ptr< StatisticsControl const > getStatisticsControl() const
Definition: Background.h:215
#define LSST_makeBackground_getApproximate(m, v, T)
Definition: Background.h:403
A class to evaluate image background levels.
Definition: Background.h:443
Property
control what is calculated
Definition: Statistics.h:63
void setStatisticsProperty(Property prop)
Definition: Background.h:218
An integer coordinate rectangle.
Definition: Box.h:55
std::shared_ptr< BackgroundControl const > getBackgroundControl() const
Definition: Background.h:368
void setNxSample(int nxSample)
Definition: Background.h:177
void setApproximateControl(std::shared_ptr< ApproximateControl > actrl)
Definition: Background.h:221