LSST Applications g0b6bd0c080+a72a5dd7e6,g1182afd7b4+2a019aa3bb,g17e5ecfddb+2b8207f7de,g1d67935e3f+06cf436103,g38293774b4+ac198e9f13,g396055baef+6a2097e274,g3b44f30a73+6611e0205b,g480783c3b1+98f8679e14,g48ccf36440+89c08d0516,g4b93dc025c+98f8679e14,g5c4744a4d9+a302e8c7f0,g613e996a0d+e1c447f2e0,g6c8d09e9e7+25247a063c,g7271f0639c+98f8679e14,g7a9cd813b8+124095ede6,g9d27549199+a302e8c7f0,ga1cf026fa3+ac198e9f13,ga32aa97882+7403ac30ac,ga786bb30fb+7a139211af,gaa63f70f4e+9994eb9896,gabf319e997+ade567573c,gba47b54d5d+94dc90c3ea,gbec6a3398f+06cf436103,gc6308e37c7+07dd123edb,gc655b1545f+ade567573c,gcc9029db3c+ab229f5caf,gd01420fc67+06cf436103,gd877ba84e5+06cf436103,gdb4cecd868+6f279b5b48,ge2d134c3d5+cc4dbb2e3f,ge448b5faa6+86d1ceac1d,gecc7e12556+98f8679e14,gf3ee170dca+25247a063c,gf4ac96e456+ade567573c,gf9f5ea5b4d+ac198e9f13,gff490e6085+8c2580be5c,w.2022.27
LSST Data Management Base Package
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
38namespace lsst {
39namespace afw {
40namespace 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
57public:
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 }
118 BackgroundControl(Interpolate::Style const style, int const nxSample = 10, int const nySample = 10,
119 UndersampleStyle const undersampleStyle = THROW_EXCEPTION,
120 StatisticsControl const sctrl = StatisticsControl(), Property const prop = MEANCLIP,
122
123 )
124 : _style(style),
125 _nxSample(nxSample),
126 _nySample(nySample),
127 _undersampleStyle(undersampleStyle),
128 _sctrl(new StatisticsControl(sctrl)),
129 _prop(prop),
130 _actrl(new ApproximateControl(actrl)) {
131 if (nxSample <= 0 || nySample <= 0) {
133 str(boost::format("You must specify at least one point, not %dx%d") % nxSample %
134 nySample));
135 }
136 }
137
149 BackgroundControl(std::string const& style, int const nxSample = 10, int const nySample = 10,
150 std::string const& undersampleStyle = "THROW_EXCEPTION",
151 StatisticsControl const sctrl = StatisticsControl(),
152 std::string const& prop = "MEANCLIP",
154 : _style(math::stringToInterpStyle(style)),
155 _nxSample(nxSample),
156 _nySample(nySample),
157 _undersampleStyle(math::stringToUndersampleStyle(undersampleStyle)),
158 _sctrl(new StatisticsControl(sctrl)),
159 _prop(stringToStatisticsProperty(prop)),
160 _actrl(new ApproximateControl(actrl)) {
161 if (nxSample <= 0 || nySample <= 0) {
163 str(boost::format("You must specify at least one point, not %dx%d") % nxSample %
164 nySample));
165 }
166 }
167
172
173 virtual ~BackgroundControl() = default;
174 void setNxSample(int nxSample) {
175 if (nxSample <= 0) {
177 str(boost::format("nxSample must be position, not %d") % nxSample));
178 }
179 _nxSample = nxSample;
180 }
181 void setNySample(int nySample) {
182 if (nySample <= 0) {
184 str(boost::format("nySample must be position, not %d") % nySample));
185 }
186 _nySample = nySample;
187 }
188
189 void setInterpStyle(Interpolate::Style const style) { _style = style; }
190 // overload to take a string
191 void setInterpStyle(std::string const& style) { _style = math::stringToInterpStyle(style); }
192
193 void setUndersampleStyle(UndersampleStyle const undersampleStyle) {
194 _undersampleStyle = undersampleStyle;
195 }
196 // overload to take a string
197 void setUndersampleStyle(std::string const& undersampleStyle) {
198 _undersampleStyle = math::stringToUndersampleStyle(undersampleStyle);
199 }
200
201 int getNxSample() const { return _nxSample; }
202 int getNySample() const { return _nySample; }
204 if (_style < 0 || _style >= Interpolate::NUM_STYLES) {
206 str(boost::format("Style %d is invalid") % _style));
207 }
208 return _style;
209 }
210 UndersampleStyle getUndersampleStyle() const { return _undersampleStyle; }
213
214 Property getStatisticsProperty() const { return _prop; }
215 void setStatisticsProperty(Property prop) { _prop = prop; }
217
221
222private:
223 Interpolate::Style _style; // style of interpolation to use
224 int _nxSample; // number of grid squares to divide image into to sample in x
225 int _nySample; // number of grid squares to divide image into to sample in y
226 UndersampleStyle _undersampleStyle; // what to do when nx,ny are too small for the requested interp style
227 std::shared_ptr<StatisticsControl> _sctrl; // statistics control object
228 Property _prop; // statistics Property
229 std::shared_ptr<ApproximateControl> _actrl; // approximate control object
230};
231
236protected:
246 template <typename ImageT>
247 explicit Background(ImageT const& img, BackgroundControl const& bgCtrl);
248
259 explicit Background(lsst::geom::Box2I const imageBBox, int const nx, int const ny);
260
261public:
263
264 Background(Background const&) = delete;
267 virtual ~Background() = default;
268 Background& operator=(Background const&) = delete;
270
272 virtual Background& operator+=(float const delta) = 0;
274 virtual Background& operator-=(float const delta) = 0;
282 template <typename PixelT>
284 Interpolate::Style const interpStyle,
285 UndersampleStyle const undersampleStyle = THROW_EXCEPTION) const {
286 return getImage<PixelT>(_imgBBox, interpStyle, undersampleStyle);
287 }
295 template <typename PixelT>
297 std::string const& interpStyle, std::string const& undersampleStyle = "THROW_EXCEPTION") const {
298 return getImage<PixelT>(math::stringToInterpStyle(interpStyle),
299 stringToUndersampleStyle(undersampleStyle));
300 }
306 template <typename PixelT>
308 lsst::geom::Box2I const& bbox, Interpolate::Style const interpStyle,
309 UndersampleStyle const undersampleStyle = THROW_EXCEPTION) const {
310 return _getImage(bbox, interpStyle, undersampleStyle, static_cast<PixelT>(0));
311 }
317 template <typename PixelT>
319 lsst::geom::Box2I const& bbox, std::string const& interpStyle,
320 std::string const& undersampleStyle = "THROW_EXCEPTION") const {
321 return _getImage(bbox, math::stringToInterpStyle(interpStyle),
322 stringToUndersampleStyle(undersampleStyle), static_cast<PixelT>(0));
323 }
324
328 template <typename PixelT>
330 return getImage<PixelT>(_bctrl->getInterpStyle(), _bctrl->getUndersampleStyle());
331 }
349 ApproximateControl const& actrl,
350 UndersampleStyle const undersampleStyle = THROW_EXCEPTION) const {
351 InternalPixelT disambiguate = 0;
352 return _getApproximate(actrl, undersampleStyle, disambiguate);
353 }
358
361
362protected:
367
374 /*
375 * We want getImage to be present in the base class, but a templated virtual function
376 * is impossible. So we'll solve the dilemma with a hack: explicitly defined
377 * virtual functions for the image types we need
378 */
379// We'll evaluate LSST_makeBackground_get{Approximation,Image} for each type in
380// LSST_makeBackground_get{Approximation,Image}_types,
381// setting v to the second arg (i.e. "= 0" for the first invocation). The first agument, m, is ignores
382
383// Desired types
384#define LSST_makeBackground_getImage_types (Background::InternalPixelT)
385#define LSST_makeBackground_getApproximate_types (Background::InternalPixelT)
386#define LSST_makeBackground_getImage(m, v, T) \
387 virtual std::shared_ptr<lsst::afw::image::Image<T>> _getImage( \
388 lsst::geom::Box2I const& bbox, \
389 Interpolate::Style const interpStyle, /* Style of the interpolation */ \
390 UndersampleStyle const undersampleStyle = \
391 THROW_EXCEPTION, /* Behaviour if there are too few points */ \
392 T = 0 /* disambiguate */ \
393 ) const v;
394
395#define LSST_makeBackground_getApproximate(m, v, T) \
396 virtual std::shared_ptr<Approximate<T>> _getApproximate( \
397 ApproximateControl const& actrl, /* Approximation style */ \
398 UndersampleStyle const undersampleStyle = \
399 THROW_EXCEPTION, /* Behaviour if there are too few points */ \
400 T = 0 /* disambiguate */ \
401 ) const v;
402
405private:
410 void _setCenOrigSize(int const width, int const height, int const nxSample, int const nySample);
411};
412
434class BackgroundMI : public Background {
435public:
436 template <typename ImageT>
458 explicit BackgroundMI(ImageT const& img, BackgroundControl const& bgCtrl);
465 explicit BackgroundMI(lsst::geom::Box2I const imageDimensions,
466 image::MaskedImage<InternalPixelT> const& statsImage);
467
468 BackgroundMI(BackgroundMI const&) = delete;
472 ~BackgroundMI() override = default;
473
479 BackgroundMI& operator+=(float const delta) override;
485 BackgroundMI& operator-=(float const delta) override;
486
491
492private:
494 _statsImage; // statistical properties for the grid of subimages
495 mutable std::vector<std::vector<double>> _gridColumns; // interpolated columns for the bicubic spline
496
497 void _setGridColumns(Interpolate::Style const interpStyle, UndersampleStyle const undersampleStyle,
498 int const iX, std::vector<int> const& ypix) const;
499
500#if defined(LSST_makeBackground_getImage)
502 BOOST_PP_SEQ_FOR_EACH(LSST_makeBackground_getApproximate, override,
504#undef LSST_makeBackground_getImage
505#undef LSST_makeBackground_getApproximate
506#endif
507 // Here's the worker function for _getImage (non-virtual; it's templated in BackgroundMI, not Background)
511 template <typename PixelT>
513 Interpolate::Style const interpStyle_,
514 UndersampleStyle const undersampleStyle) const;
515 // and for _getApproximate
516 template <typename PixelT>
517 std::shared_ptr<Approximate<PixelT>> doGetApproximate(ApproximateControl const& actrl,
518 UndersampleStyle const undersampleStyle) const;
519};
525template <typename ImageT>
527 return std::shared_ptr<Background>(new BackgroundMI(img, bgCtrl));
528}
529} // namespace math
530} // namespace afw
531} // namespace lsst
532
533#endif // LSST_AFW_MATH_BACKGROUND_H
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
#define LSST_makeBackground_getImage(m, v, T)
Definition: Background.h:386
#define LSST_makeBackground_getApproximate(m, v, T)
Definition: Background.h:395
#define LSST_makeBackground_getApproximate_types
Definition: Background.h:385
#define LSST_makeBackground_getImage_types
Definition: Background.h:384
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:73
Control how to make an approximation.
Definition: Approximate.h:48
Pass parameters to a Background object.
Definition: Background.h:56
void setNySample(int nySample)
Definition: Background.h:181
void setNxSample(int nxSample)
Definition: Background.h:174
void setApproximateControl(std::shared_ptr< ApproximateControl > actrl)
Definition: Background.h:218
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:149
std::shared_ptr< ApproximateControl const > getApproximateControl() const
Definition: Background.h:220
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))
Overload constructor to provide interp style.
Definition: Background.h:118
BackgroundControl(BackgroundControl const &)=default
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
std::shared_ptr< StatisticsControl > getStatisticsControl()
Definition: Background.h:211
virtual ~BackgroundControl()=default
BackgroundControl & operator=(BackgroundControl const &)=default
BackgroundControl(BackgroundControl &&)=default
std::shared_ptr< ApproximateControl > getApproximateControl()
Definition: Background.h:219
Interpolate::Style getInterpStyle() const
Definition: Background.h:203
BackgroundControl & operator=(BackgroundControl &&)=default
void setInterpStyle(Interpolate::Style const style)
Definition: Background.h:189
std::shared_ptr< StatisticsControl const > getStatisticsControl() const
Definition: Background.h:212
void setUndersampleStyle(UndersampleStyle const undersampleStyle)
Definition: Background.h:193
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(Property prop)
Definition: Background.h:215
void setStatisticsProperty(std::string prop)
Definition: Background.h:216
UndersampleStyle getUndersampleStyle() const
Definition: Background.h:210
void setUndersampleStyle(std::string const &undersampleStyle)
Definition: Background.h:197
Property getStatisticsProperty() const
Definition: Background.h:214
void setInterpStyle(std::string const &style)
Definition: Background.h:191
A virtual base class to evaluate image background levels.
Definition: Background.h:235
std::vector< double > _ycen
y center ...
Definition: Background.h:369
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:318
std::vector< double > _xcen
x center pix coords of sub images
Definition: Background.h:368
UndersampleStyle getAsUsedUndersampleStyle() const
Return the UndersampleStyle that we actually used in the last call to getImage()
Definition: Background.h:341
Background & operator=(Background &&)=delete
virtual Background & operator-=(float const delta)=0
Subtract a constant level from a background.
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:307
Interpolate::Style getAsUsedInterpStyle() const
Return the Interpolate::Style that we actually used in the last call to getImage()
Definition: Background.h:337
Background(Background &&)=delete
virtual Background & operator+=(float const delta)=0
Add a constant level to a background.
Background(Background const &)=delete
lsst::geom::Box2I getImageBBox() const
Return the input image's (PARENT) bounding box.
Definition: Background.h:357
Background(ImageT const &img, BackgroundControl const &bgCtrl)
Constructor for Background.
Definition: Background.cc:44
std::vector< int > _xsize
x size of sub images
Definition: Background.h:372
lsst::geom::Box2I _imgBBox
size and origin of input image
Definition: Background.h:363
std::vector< int > _xorig
x origin pix coords of sub images
Definition: Background.h:370
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:296
std::shared_ptr< BackgroundControl > getBackgroundControl()
Definition: Background.h:359
UndersampleStyle _asUsedUndersampleStyle
the undersampleStyle we actually used
Definition: Background.h:366
std::shared_ptr< lsst::afw::image::Image< PixelT > > getImage() const
Method to interpolate and return the background for entire image.
Definition: Background.h:329
std::vector< int > _ysize
y size ...
Definition: Background.h:373
Background & operator=(Background const &)=delete
virtual ~Background()=default
dtor
float InternalPixelT
type used for any internal images, and returned by getApproximate
Definition: Background.h:262
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:348
std::shared_ptr< BackgroundControl const > getBackgroundControl() const
Definition: Background.h:360
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:283
std::vector< int > _yorig
y origin ...
Definition: Background.h:371
Interpolate::Style _asUsedInterpStyle
the style we actually used
Definition: Background.h:365
std::shared_ptr< BackgroundControl > _bctrl
control info set by user.
Definition: Background.h:364
A class to evaluate image background levels.
Definition: Background.h:434
lsst::afw::image::MaskedImage< InternalPixelT > getStatsImage() const
Return the image of statistical quantities extracted from the image.
Definition: Background.h:490
BackgroundMI(BackgroundMI &&)=delete
BackgroundMI & operator=(BackgroundMI const &)=delete
~BackgroundMI() override=default
BackgroundMI & operator-=(float const delta) override
Subtract a scalar from the Background (equivalent to subtracting a constant from the original image)
BackgroundMI(BackgroundMI const &)=delete
BackgroundMI & operator+=(float const delta) override
Add a scalar to the Background (equivalent to adding a constant to the original image)
BackgroundMI & operator=(BackgroundMI &&)=delete
BackgroundMI(ImageT const &img, BackgroundControl const &bgCtrl)
Constructor for BackgroundMI.
Definition: BackgroundMI.cc:80
Pass parameters to a Statistics object.
Definition: Statistics.h:92
An integer coordinate rectangle.
Definition: Box.h:55
Reports invalid arguments.
Definition: Runtime.h:66
Reports attempts to exceed implementation-defined length limits for some classes.
Definition: Runtime.h:76
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:526
Property
control what is calculated
Definition: Statistics.h:62
@ MEANCLIP
estimate sample N-sigma clipped mean (N set in StatisticsControl, default=3)
Definition: Statistics.h:71
Property stringToStatisticsProperty(std::string const property)
Conversion function to switch a string to a Property (see Statistics.h)
Definition: Statistics.cc:738
Interpolate::Style stringToInterpStyle(std::string const &style)
Conversion function to switch a string to an Interpolate::Style.
Definition: Interpolate.cc:256
UndersampleStyle stringToUndersampleStyle(std::string const &style)
Conversion function to switch a string to an UndersampleStyle.
Definition: Background.cc:117
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
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
A base class for image defects.