LSSTApplications  20.0.0
LSSTDataManagementBasePackage
statistics.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008-2016 AURA/LSST.
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 <https://www.lsstcorp.org/LegalNotices/>.
21  */
22 
23 #include <pybind11/pybind11.h>
24 #include <pybind11/stl.h>
25 
27 
28 namespace py = pybind11;
29 using namespace pybind11::literals;
30 
31 namespace lsst {
32 namespace afw {
33 namespace math {
34 
35 template <typename Pixel>
37  mod.def("makeStatistics",
38  (Statistics(*)(image::Image<Pixel> const &, image::Mask<image::MaskPixel> const &, int const,
39  StatisticsControl const &))makeStatistics<Pixel>,
40  "img"_a, "msk"_a, "flags"_a, "sctrl"_a = StatisticsControl());
41  mod.def("makeStatistics", (Statistics(*)(image::MaskedImage<Pixel> const &, int const,
42  StatisticsControl const &))makeStatistics<Pixel>,
43  "mimg"_a, "flags"_a, "sctrl"_a = StatisticsControl());
44  mod.def("makeStatistics",
45  (Statistics(*)(image::MaskedImage<Pixel> const &, image::Image<WeightPixel> const &, int const,
46  StatisticsControl const &))makeStatistics<Pixel>,
47  "mimg"_a, "weights"_a, "flags"_a, "sctrl"_a = StatisticsControl());
48  mod.def("makeStatistics",
49  (Statistics(*)(image::Mask<image::MaskPixel> const &, int const, StatisticsControl const &))
50  makeStatistics, // this is not a template, just a regular overload
51  "msk"_a,
52  "flags"_a, "sctrl"_a = StatisticsControl());
53  mod.def("makeStatistics", (Statistics(*)(image::Image<Pixel> const &, int const,
54  StatisticsControl const &))makeStatistics<Pixel>,
55  "img"_a, "flags"_a, "sctrl"_a = StatisticsControl());
56 }
57 
58 template <typename Pixel>
60  mod.def("makeStatistics", (Statistics(*)(std::vector<Pixel> const &, int const,
61  StatisticsControl const &))makeStatistics<Pixel>,
62  "v"_a, "flags"_a, "sctrl"_a = StatisticsControl());
63  mod.def("makeStatistics", (Statistics(*)(std::vector<Pixel> const &, std::vector<WeightPixel> const &,
64  int const, StatisticsControl const &))makeStatistics<Pixel>,
65  "v"_a, "vweights"_a, "flags"_a, "sctrl"_a = StatisticsControl());
66 }
67 
68 PYBIND11_MODULE(statistics, mod) {
69  /* Module level */
70  py::enum_<Property>(mod, "Property", py::arithmetic())
71  .value("NOTHING", Property::NOTHING)
72  .value("ERRORS", Property::ERRORS)
73  .value("NPOINT", Property::NPOINT)
74  .value("MEAN", Property::MEAN)
75  .value("STDEV", Property::STDEV)
76  .value("VARIANCE", Property::VARIANCE)
77  .value("MEDIAN", Property::MEDIAN)
78  .value("IQRANGE", Property::IQRANGE)
79  .value("MEANCLIP", Property::MEANCLIP)
80  .value("STDEVCLIP", Property::STDEVCLIP)
81  .value("VARIANCECLIP", Property::VARIANCECLIP)
82  .value("MIN", Property::MIN)
83  .value("MAX", Property::MAX)
84  .value("SUM", Property::SUM)
85  .value("MEANSQUARE", Property::MEANSQUARE)
86  .value("ORMASK", Property::ORMASK)
87  .value("NCLIPPED", Property::NCLIPPED)
88  .value("NMASKED", Property::NMASKED)
89  .export_values();
90 
91  mod.def("stringToStatisticsProperty", stringToStatisticsProperty);
92 
93  py::class_<StatisticsControl, std::shared_ptr<StatisticsControl>> clsStatisticsControl(
94  mod, "StatisticsControl");
95 
96  py::enum_<StatisticsControl::WeightsBoolean>(clsStatisticsControl, "WeightsBoolean")
97  .value("WEIGHTS_FALSE", StatisticsControl::WeightsBoolean::WEIGHTS_FALSE)
98  .value("WEIGHTS_TRUE", StatisticsControl::WeightsBoolean::WEIGHTS_TRUE)
99  .value("WEIGHTS_NONE", StatisticsControl::WeightsBoolean::WEIGHTS_NONE)
100  .export_values();
101 
102  clsStatisticsControl.def(py::init<double, int, lsst::afw::image::MaskPixel, bool,
104  "numSigmaClip"_a = 3.0, "numIter"_a = 3, "andMask"_a = 0x0, "isNanSafe"_a = true,
105  "useWeights"_a = StatisticsControl::WEIGHTS_NONE);
106 
107  clsStatisticsControl.def("getMaskPropagationThreshold", &StatisticsControl::getMaskPropagationThreshold);
108  clsStatisticsControl.def("setMaskPropagationThreshold", &StatisticsControl::setMaskPropagationThreshold);
109  clsStatisticsControl.def("getNumSigmaClip", &StatisticsControl::getNumSigmaClip);
110  clsStatisticsControl.def("getNumIter", &StatisticsControl::getNumIter);
111  clsStatisticsControl.def("getAndMask", &StatisticsControl::getAndMask);
112  clsStatisticsControl.def("getNoGoodPixelsMask", &StatisticsControl::getNoGoodPixelsMask);
113  clsStatisticsControl.def("getNanSafe", &StatisticsControl::getNanSafe);
114  clsStatisticsControl.def("getWeighted", &StatisticsControl::getWeighted);
115  clsStatisticsControl.def("getWeightedIsSet", &StatisticsControl::getWeightedIsSet);
116  clsStatisticsControl.def("getCalcErrorFromInputVariance",
117  &StatisticsControl::getCalcErrorFromInputVariance);
118  clsStatisticsControl.def("setNumSigmaClip", &StatisticsControl::setNumSigmaClip);
119  clsStatisticsControl.def("setNumIter", &StatisticsControl::setNumIter);
120  clsStatisticsControl.def("setAndMask", &StatisticsControl::setAndMask);
121  clsStatisticsControl.def("setNoGoodPixelsMask", &StatisticsControl::setNoGoodPixelsMask);
122  clsStatisticsControl.def("setNanSafe", &StatisticsControl::setNanSafe);
123  clsStatisticsControl.def("setWeighted", &StatisticsControl::setWeighted);
124  clsStatisticsControl.def("setCalcErrorFromInputVariance",
125  &StatisticsControl::setCalcErrorFromInputVariance);
126 
127  py::class_<Statistics> clsStatistics(mod, "Statistics");
128 
129  clsStatistics.def("getResult", &Statistics::getResult, "prop"_a = Property::NOTHING);
130  clsStatistics.def("getError", &Statistics::getError, "prop"_a = Property::NOTHING);
131  clsStatistics.def("getValue", &Statistics::getValue, "prop"_a = Property::NOTHING);
132  clsStatistics.def("getOrMask", &Statistics::getOrMask);
133 
134  declareStatistics<unsigned short>(mod);
135  declareStatistics<double>(mod);
136  declareStatistics<float>(mod);
137  declareStatistics<int>(mod);
138 
139  // Declare vector overloads separately to prevent casting errors
140  // that otherwise (mysteriously) occur when overloads are tried
141  // in order.
142  declareStatisticsVectorOverloads<unsigned short>(mod);
143  declareStatisticsVectorOverloads<double>(mod);
144  declareStatisticsVectorOverloads<float>(mod);
145  declareStatisticsVectorOverloads<int>(mod);
146 }
147 
148 } // math
149 } // afw
150 } // lsst
lsst::afw::math::IQRANGE
@ IQRANGE
estimate sample inter-quartile range
Definition: Statistics.h:71
lsst::afw::math::declareStatisticsVectorOverloads
void declareStatisticsVectorOverloads(py::module &mod)
Definition: statistics.cc:59
lsst::afw::math::declareStatistics
void declareStatistics(py::module &mod)
Definition: statistics.cc:36
lsst::afw::math::StatisticsControl::WeightsBoolean
WeightsBoolean
Definition: Statistics.h:95
lsst::afw::image::Mask
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:77
std::vector< Pixel >
lsst::afw
Definition: imageAlgorithm.dox:1
lsst::afw::math::stringToStatisticsProperty
Property stringToStatisticsProperty(std::string const property)
Conversion function to switch a string to a Property (see Statistics.h)
Definition: Statistics.cc:747
lsst::afw::math::makeStatistics
Statistics makeStatistics(lsst::afw::image::Image< Pixel > const &img, lsst::afw::image::Mask< image::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl=StatisticsControl())
Handle a watered-down front-end to the constructor (no variance)
Definition: Statistics.h:354
lsst::afw::math::MEANCLIP
@ MEANCLIP
estimate sample N-sigma clipped mean (N set in StatisticsControl, default=3)
Definition: Statistics.h:72
lsst::afw::math::VARIANCECLIP
@ VARIANCECLIP
estimate sample N-sigma clipped variance (N set in StatisticsControl, default=3)
Definition: Statistics.h:74
lsst::afw::image::MaskedImage
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:73
lsst::afw::math::MEAN
@ MEAN
estimate sample mean
Definition: Statistics.h:67
lsst::afw::math::STDEVCLIP
@ STDEVCLIP
estimate sample N-sigma clipped stdev (N set in StatisticsControl, default=3)
Definition: Statistics.h:73
lsst::afw::math::NCLIPPED
@ NCLIPPED
number of clipped points
Definition: Statistics.h:81
std::int32_t
lsst::afw::math::ORMASK
@ ORMASK
get the or-mask of all pixels used.
Definition: Statistics.h:80
lsst::afw::math::ERRORS
@ ERRORS
Include errors of requested quantities.
Definition: Statistics.h:65
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
lsst::afw::math::StatisticsControl
Pass parameters to a Statistics object.
Definition: Statistics.h:93
lsst::afw::math::MEANSQUARE
@ MEANSQUARE
find mean value of square of pixel values
Definition: Statistics.h:79
lsst::afw::math::NPOINT
@ NPOINT
number of sample points
Definition: Statistics.h:66
lsst::afw::math::PYBIND11_MODULE
PYBIND11_MODULE(statistics, mod)
Definition: statistics.cc:68
lsst::afw::math::STDEV
@ STDEV
estimate sample standard deviation
Definition: Statistics.h:68
Statistics.h
lsst::afw::math::MIN
@ MIN
estimate sample minimum
Definition: Statistics.h:76
lsst::afw::math::Statistics
Definition: Statistics.h:215
pybind11
Definition: _GenericMap.cc:40
lsst::afw::math::MAX
@ MAX
estimate sample maximum
Definition: Statistics.h:77
lsst::afw::math::VARIANCE
@ VARIANCE
estimate sample variance
Definition: Statistics.h:69
lsst::afw::math::NOTHING
@ NOTHING
We don't want anything.
Definition: Statistics.h:64
lsst::afw::image::Image< Pixel >
lsst::utils.tests.init
def init()
Definition: tests.py:58
lsst::afw::math::SUM
@ SUM
find sum of pixels in the image
Definition: Statistics.h:78
lsst::meas::modelfit.psf.psfContinued.module
module
Definition: psfContinued.py:42
lsst::afw::math::NMASKED
@ NMASKED
number of masked points
Definition: Statistics.h:82
lsst::afw::math::MEDIAN
@ MEDIAN
estimate sample median
Definition: Statistics.h:70