LSSTApplications  18.0.0+106,18.0.0+50,19.0.0,19.0.0+1,19.0.0+10,19.0.0+11,19.0.0+13,19.0.0+17,19.0.0+2,19.0.0-1-g20d9b18+6,19.0.0-1-g425ff20,19.0.0-1-g5549ca4,19.0.0-1-g580fafe+6,19.0.0-1-g6fe20d0+1,19.0.0-1-g7011481+9,19.0.0-1-g8c57eb9+6,19.0.0-1-gb5175dc+11,19.0.0-1-gdc0e4a7+9,19.0.0-1-ge272bc4+6,19.0.0-1-ge3aa853,19.0.0-10-g448f008b,19.0.0-12-g6990b2c,19.0.0-2-g0d9f9cd+11,19.0.0-2-g3d9e4fb2+11,19.0.0-2-g5037de4,19.0.0-2-gb96a1c4+3,19.0.0-2-gd955cfd+15,19.0.0-3-g2d13df8,19.0.0-3-g6f3c7dc,19.0.0-4-g725f80e+11,19.0.0-4-ga671dab3b+1,19.0.0-4-gad373c5+3,19.0.0-5-ga2acb9c+2,19.0.0-5-gfe96e6c+2,w.2020.01
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  [[deprecated(
120  "Omit `style` parameter, and pass it to `Background::getImage` instead. To be removed after "
121  "20.0.0.")]] // DM-22276
123  Interpolate::Style const style, int const nxSample = 10, int const nySample = 10,
124  UndersampleStyle const undersampleStyle = THROW_EXCEPTION,
125  StatisticsControl const sctrl = StatisticsControl(), Property const prop = MEANCLIP,
127 
128  )
129  : _style(style),
130  _nxSample(nxSample),
131  _nySample(nySample),
132  _undersampleStyle(undersampleStyle),
133  _sctrl(new StatisticsControl(sctrl)),
134  _prop(prop),
135  _actrl(new ApproximateControl(actrl)) {
136  if (nxSample <= 0 || nySample <= 0) {
138  str(boost::format("You must specify at least one point, not %dx%d") % nxSample %
139  nySample));
140  }
141  }
142 
156  [[deprecated(
157  "Omit `style` parameter, and pass it to `Background::getImage` instead. To be removed after "
158  "20.0.0.")]] // DM-22276
159  BackgroundControl(std::string const& style, int const nxSample = 10, int const nySample = 10,
160  std::string const& undersampleStyle = "THROW_EXCEPTION",
161  StatisticsControl const sctrl = StatisticsControl(),
162  std::string const& prop = "MEANCLIP",
164  1))
165  : _style(math::stringToInterpStyle(style)),
166  _nxSample(nxSample),
167  _nySample(nySample),
168  _undersampleStyle(math::stringToUndersampleStyle(undersampleStyle)),
169  _sctrl(new StatisticsControl(sctrl)),
170  _prop(stringToStatisticsProperty(prop)),
171  _actrl(new ApproximateControl(actrl)) {
172  if (nxSample <= 0 || nySample <= 0) {
174  str(boost::format("You must specify at least one point, not %dx%d") % nxSample %
175  nySample));
176  }
177  }
178 
179  BackgroundControl(BackgroundControl const&) = default;
181  BackgroundControl& operator=(BackgroundControl const&) = default;
183 
184  virtual ~BackgroundControl() = default;
185  void setNxSample(int nxSample) {
186  if (nxSample <= 0) {
188  str(boost::format("nxSample must be position, not %d") % nxSample));
189  }
190  _nxSample = nxSample;
191  }
192  void setNySample(int nySample) {
193  if (nySample <= 0) {
195  str(boost::format("nySample must be position, not %d") % nySample));
196  }
197  _nySample = nySample;
198  }
199 
200  [[deprecated(
201  "Replaced by passing style to `Background::getImage`. To be removed after 20.0.0.")]] // DM-22276
202  void
204  _style = style;
205  }
206  // overload to take a string
207  [[deprecated(
208  "Replaced by passing style to `Background::getImage`. To be removed after 20.0.0.")]] // DM-22276
209  void
210  setInterpStyle(std::string const& style) {
211  _style = math::stringToInterpStyle(style);
212  }
213 
214  void setUndersampleStyle(UndersampleStyle const undersampleStyle) {
215  _undersampleStyle = undersampleStyle;
216  }
217  // overload to take a string
218  void setUndersampleStyle(std::string const& undersampleStyle) {
219  _undersampleStyle = math::stringToUndersampleStyle(undersampleStyle);
220  }
221 
222  int getNxSample() const { return _nxSample; }
223  int getNySample() const { return _nySample; }
224  [[deprecated(
225  "Replaced by passing style to `Background::getImage`. To be removed after 20.0.0.")]] // DM-22276
227  getInterpStyle() const {
228  if (_style < 0 || _style >= Interpolate::NUM_STYLES) {
230  str(boost::format("Style %d is invalid") % _style));
231  }
232  return _style;
233  }
234  UndersampleStyle getUndersampleStyle() const { return _undersampleStyle; }
237 
238  Property getStatisticsProperty() const { return _prop; }
239  void setStatisticsProperty(Property prop) { _prop = prop; }
241 
245 
246 private:
247  // TODO: remove _style member in DM-22276
248  Interpolate::Style _style; // style of interpolation to use
249  int _nxSample; // number of grid squares to divide image into to sample in x
250  int _nySample; // number of grid squares to divide image into to sample in y
251  UndersampleStyle _undersampleStyle; // what to do when nx,ny are too small for the requested interp style
252  std::shared_ptr<StatisticsControl> _sctrl; // statistics control object
253  Property _prop; // statistics Property
254  std::shared_ptr<ApproximateControl> _actrl; // approximate control object
255 };
256 
260 class Background {
261 protected:
275  template <typename ImageT>
276  explicit Background(ImageT const& img, BackgroundControl const& bgCtrl);
277 
288  explicit Background(lsst::geom::Box2I const imageBBox, int const nx, int const ny);
290  virtual ~Background() = default;
291 
292 public:
293  typedef float InternalPixelT;
294 
295  Background(Background const&) = delete;
296  Background(Background&&) = delete;
297  Background& operator=(Background const&) = delete;
298  Background& operator=(Background&&) = delete;
299 
301  virtual Background& operator+=(float const delta) = 0;
303  virtual Background& operator-=(float const delta) = 0;
311  template <typename PixelT>
313  Interpolate::Style const interpStyle,
314  UndersampleStyle const undersampleStyle = THROW_EXCEPTION) const {
315  return getImage<PixelT>(_imgBBox, interpStyle, undersampleStyle);
316  }
324  template <typename PixelT>
326  std::string const& interpStyle, std::string const& undersampleStyle = "THROW_EXCEPTION") const {
327  return getImage<PixelT>(math::stringToInterpStyle(interpStyle),
328  stringToUndersampleStyle(undersampleStyle));
329  }
335  template <typename PixelT>
337  lsst::geom::Box2I const& bbox, Interpolate::Style const interpStyle,
338  UndersampleStyle const undersampleStyle = THROW_EXCEPTION) const {
339  return _getImage(bbox, interpStyle, undersampleStyle, static_cast<PixelT>(0));
340  }
346  template <typename PixelT>
348  lsst::geom::Box2I const& bbox, std::string const& interpStyle,
349  std::string const& undersampleStyle = "THROW_EXCEPTION") const {
350  return _getImage(bbox, math::stringToInterpStyle(interpStyle),
351  stringToUndersampleStyle(undersampleStyle), static_cast<PixelT>(0));
352  }
353 
358  template <typename PixelT>
359  [[deprecated(
360  "Call an overload with an `interpStyle` parameter. To be removed after 20.0.0.")]] // DM-22276
362  getImage() const {
363  return getImage<PixelT>(_bctrl->getInterpStyle(), _bctrl->getUndersampleStyle());
364  }
370  Interpolate::Style getAsUsedInterpStyle() const { return _asUsedInterpStyle; }
374  UndersampleStyle getAsUsedUndersampleStyle() const { return _asUsedUndersampleStyle; }
382  ApproximateControl const& actrl,
383  UndersampleStyle const undersampleStyle = THROW_EXCEPTION) const {
384  InternalPixelT disambiguate = 0;
385  return _getApproximate(actrl, undersampleStyle, disambiguate);
386  }
390  lsst::geom::Box2I getImageBBox() const { return _imgBBox; }
391 
394 
395 protected:
400 
407  /*
408  * We want getImage to be present in the base class, but a templated virtual function
409  * is impossible. So we'll solve the dilemma with a hack: explicitly defined
410  * virtual functions for the image types we need
411  */
412 // We'll evaluate LSST_makeBackground_get{Approximation,Image} for each type in
413 // LSST_makeBackground_get{Approximation,Image}_types,
414 // setting v to the second arg (i.e. "= 0" for the first invocation). The first agument, m, is ignores
415 
416 // Desired types
417 #define LSST_makeBackground_getImage_types (Background::InternalPixelT)
418 #define LSST_makeBackground_getApproximate_types (Background::InternalPixelT)
419 #define LSST_makeBackground_getImage(m, v, T) \
420  virtual std::shared_ptr<lsst::afw::image::Image<T>> _getImage( \
421  lsst::geom::Box2I const& bbox, \
422  Interpolate::Style const interpStyle, /* Style of the interpolation */ \
423  UndersampleStyle const undersampleStyle = \
424  THROW_EXCEPTION, /* Behaviour if there are too few points */ \
425  T = 0 /* disambiguate */ \
426  ) const v;
427 
428 #define LSST_makeBackground_getApproximate(m, v, T) \
429  virtual std::shared_ptr<Approximate<T>> _getApproximate( \
430  ApproximateControl const& actrl, /* Approximation style */ \
431  UndersampleStyle const undersampleStyle = \
432  THROW_EXCEPTION, /* Behaviour if there are too few points */ \
433  T = 0 /* disambiguate */ \
434  ) const v;
435 
438 private:
443  void _setCenOrigSize(int const width, int const height, int const nxSample, int const nySample);
444 };
445 
468 class BackgroundMI : public Background {
469 public:
470  template <typename ImageT>
497  explicit BackgroundMI(ImageT const& img, BackgroundControl const& bgCtrl);
504  explicit BackgroundMI(lsst::geom::Box2I const imageDimensions,
505  image::MaskedImage<InternalPixelT> const& statsImage);
506 
507  BackgroundMI(BackgroundMI const&) = delete;
508  BackgroundMI(BackgroundMI&&) = delete;
509  BackgroundMI& operator=(BackgroundMI const&) = delete;
510  BackgroundMI& operator=(BackgroundMI&&) = delete;
511  ~BackgroundMI() override = default;
512 
518  BackgroundMI& operator+=(float const delta) override;
524  BackgroundMI& operator-=(float const delta) override;
525 
538  [[deprecated("Use `getImage` instead. To be removed after 20.0.0.")]] // DM-22276
539  double
540  getPixel(Interpolate::Style const style, int const x, int const y) const;
548  [[deprecated("Use `getImage` instead. To be removed after 20.0.0.")]] // DM-22276
549  double
550  getPixel(int const x, int const y) const {
551  // I think this should be LOCAL because the original getPixel used 0-based indices
552  return getImage<float>(_bctrl->getInterpStyle(), _bctrl->getUndersampleStyle())
553  ->get(lsst::geom::Point2I(x, y), image::LOCAL);
554  }
559 
560 private:
562  _statsImage; // statistical properties for the grid of subimages
563  mutable std::vector<std::vector<double>> _gridColumns; // interpolated columns for the bicubic spline
564 
565  void _setGridColumns(Interpolate::Style const interpStyle, UndersampleStyle const undersampleStyle,
566  int const iX, std::vector<int> const& ypix) const;
567 
568 #if defined(LSST_makeBackground_getImage)
572 #undef LSST_makeBackground_getImage
573 #undef LSST_makeBackground_getApproximate
574 #endif
575  // Here's the worker function for _getImage (non-virtual; it's templated in BackgroundMI, not Background)
579  template <typename PixelT>
581  Interpolate::Style const interpStyle_,
582  UndersampleStyle const undersampleStyle) const;
583  // and for _getApproximate
584  template <typename PixelT>
585  std::shared_ptr<Approximate<PixelT>> doGetApproximate(ApproximateControl const& actrl,
586  UndersampleStyle const undersampleStyle) const;
587 };
593 template <typename ImageT>
595  return std::shared_ptr<Background>(new BackgroundMI(img, bgCtrl));
596 }
597 } // namespace math
598 } // namespace afw
599 } // namespace lsst
600 
601 #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:293
std::vector< int > _xsize
x size of sub images
Definition: Background.h:405
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:370
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:401
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:325
std::vector< int > _yorig
y origin ...
Definition: Background.h:404
void setNySample(int nySample)
Definition: Background.h:192
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:396
Property getStatisticsProperty() const
Definition: Background.h:238
Reports attempts to exceed implementation-defined length limits for some classes. ...
Definition: Runtime.h:76
Interpolate::Style getInterpStyle() const
Definition: Background.h:227
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:381
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:122
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:347
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:594
std::shared_ptr< ApproximateControl > getApproximateControl()
Definition: Background.h:243
lsst::afw::image::MaskedImage< InternalPixelT > getStatsImage() const
Return the image of statistical quantities extracted from the image.
Definition: Background.h:558
void setInterpStyle(Interpolate::Style const style)
Definition: Background.h:203
#define LSST_makeBackground_getApproximate_types
Definition: Background.h:418
std::vector< double > _ycen
y center ...
Definition: Background.h:402
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:240
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:374
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:336
A virtual base class to evaluate image background levels.
Definition: Background.h:260
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:362
Control how to make an approximation.
Definition: Approximate.h:48
void setUndersampleStyle(std::string const &undersampleStyle)
Definition: Background.h:218
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:398
A base class for image defects.
std::shared_ptr< BackgroundControl > getBackgroundControl()
Definition: Background.h:392
std::shared_ptr< StatisticsControl > getStatisticsControl()
Definition: Background.h:235
std::shared_ptr< ApproximateControl const > getApproximateControl() const
Definition: Background.h:244
void setUndersampleStyle(UndersampleStyle const undersampleStyle)
Definition: Background.h:214
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:159
#define LSST_makeBackground_getImage_types
Definition: Background.h:417
#define LSST_makeBackground_getImage(m, v, T)
Definition: Background.h:419
UndersampleStyle getUndersampleStyle() const
Definition: Background.h:234
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:390
#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:550
std::vector< int > _xorig
x origin pix coords of sub images
Definition: Background.h:403
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:397
std::vector< int > _ysize
y size ...
Definition: Background.h:406
UndersampleStyle _asUsedUndersampleStyle
the undersampleStyle we actually used
Definition: Background.h:399
Reports invalid arguments.
Definition: Runtime.h:66
void setInterpStyle(std::string const &style)
Definition: Background.h:210
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:312
std::shared_ptr< StatisticsControl const > getStatisticsControl() const
Definition: Background.h:236
#define LSST_makeBackground_getApproximate(m, v, T)
Definition: Background.h:428
A class to evaluate image background levels.
Definition: Background.h:468
Property
control what is calculated
Definition: Statistics.h:63
void setStatisticsProperty(Property prop)
Definition: Background.h:239
An integer coordinate rectangle.
Definition: Box.h:55
std::shared_ptr< BackgroundControl const > getBackgroundControl() const
Definition: Background.h:393
void setNxSample(int nxSample)
Definition: Background.h:185
void setApproximateControl(std::shared_ptr< ApproximateControl > actrl)
Definition: Background.h:242