lsst.afw  13.0-98-g5a654715e+2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
random.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/operators.h>
25 #include <pybind11/stl.h>
26 
27 #include "lsst/afw/image/Image.h"
28 #include "lsst/afw/math/Random.h"
29 
30 namespace py = pybind11;
31 
32 using namespace lsst::afw::math;
33 using namespace pybind11::literals;
34 
35 template <typename ImageT>
36 void declareRandomImage(py::module &mod) {
37  mod.def("randomUniformImage", (void (*)(ImageT *, Random &))randomUniformImage<ImageT>);
38  mod.def("randomUniformPosImage", (void (*)(ImageT *, Random &))randomUniformPosImage<ImageT>);
39  mod.def("randomUniformIntImage",
40  (void (*)(ImageT *, Random &, unsigned long))randomUniformIntImage<ImageT>);
41  mod.def("randomFlatImage",
42  (void (*)(ImageT *, Random &, double const, double const))randomFlatImage<ImageT>);
43  mod.def("randomGaussianImage", (void (*)(ImageT *, Random &))randomGaussianImage<ImageT>);
44  mod.def("randomChisqImage", (void (*)(ImageT *, Random &, double const))randomChisqImage<ImageT>);
45  mod.def("randomPoissonImage", (void (*)(ImageT *, Random &, double const))randomPoissonImage<ImageT>);
46 }
47 
48 PYBIND11_PLUGIN(_random) {
49  py::module mod("_random", "Python wrapper for afw _random library");
50 
51  py::class_<Random> clsRandom(mod, "Random");
52 
53  /* Member types and enums */
54  py::enum_<Random::Algorithm>(clsRandom, "Algorithm")
55  .value("MT19937", Random::Algorithm::MT19937)
56  .value("RANLXS0", Random::Algorithm::RANLXS0)
57  .value("RANLXS1", Random::Algorithm::RANLXS1)
58  .value("RANLXS2", Random::Algorithm::RANLXS2)
59  .value("RANLXD1", Random::Algorithm::RANLXD1)
60  .value("RANLXD2", Random::Algorithm::RANLXD2)
61  .value("RANLUX", Random::Algorithm::RANLUX)
62  .value("RANLUX389", Random::Algorithm::RANLUX389)
63  .value("CMRG", Random::Algorithm::CMRG)
64  .value("MRG", Random::Algorithm::MRG)
65  .value("TAUS", Random::Algorithm::TAUS)
66  .value("TAUS2", Random::Algorithm::TAUS2)
67  .value("GFSR4", Random::Algorithm::GFSR4)
68  .value("NUM_ALGORITHMS", Random::Algorithm::NUM_ALGORITHMS)
69  .export_values();
70 
71  /* Constructors */
72  clsRandom.def(py::init<Random::Algorithm, unsigned long>(), "algorithm"_a = Random::Algorithm::MT19937,
73  "seed"_a = 1);
74  clsRandom.def(py::init<std::string const &, unsigned long>(), "algorithm"_a, "seed"_a = 1);
75  clsRandom.def(py::init<std::shared_ptr<lsst::pex::policy::Policy> const>(), "policy"_a);
76 
77  /* Members */
78  clsRandom.def("deepCopy", &Random::deepCopy);
79  clsRandom.def("getAlgorithm", &Random::getAlgorithm);
80  clsRandom.def("getAlgorithmName", &Random::getAlgorithmName);
81  clsRandom.def_static("getAlgorithmNames", &Random::getAlgorithmNames);
82  clsRandom.def("getSeed", &Random::getSeed);
83  clsRandom.def("uniform", &Random::uniform);
84  clsRandom.def("uniformPos", &Random::uniformPos);
85  clsRandom.def("uniformInt", &Random::uniformInt);
86  clsRandom.def("flat", &Random::flat);
87  clsRandom.def("gaussian", &Random::gaussian);
88  clsRandom.def("chisq", &Random::chisq);
89  clsRandom.def("poisson", &Random::poisson);
90 
91  // getState and setState are special, their std::string cannot
92  // be converted to a Python string (needs to go to bytes instead)
93  // thus use the same solution as employed with Swig
94  clsRandom.def("getState", [](Random &self) -> py::object {
95  std::string state = self.getState();
96  return py::reinterpret_steal<py::object>(PyBytes_FromStringAndSize(state.data(), state.size()));
97  });
98  clsRandom.def("setState", [](Random &self, py::bytes const &state) { self.setState(state); });
99 
100  /* Module level */
101  declareRandomImage<lsst::afw::image::Image<double>>(mod);
102  declareRandomImage<lsst::afw::image::Image<float>>(mod);
103 
104  return mod.ptr();
105 }
double poisson(double const mu)
Returns a random variate from the poisson distribution with mean mu.
Definition: Random.cc:181
unsigned long uniformInt(unsigned long n)
Returns a uniformly distributed random integer from 0 to n-1.
Definition: Random.cc:166
unsigned long getSeed() const
Definition: Random.cc:158
Algorithm getAlgorithm() const
Definition: Random.cc:144
double chisq(double const nu)
Returns a random variate from the chi-squared distribution with nu degrees of freedom.
Definition: Random.cc:179
double const _a
Definition: RandomImage.cc:77
void declareRandomImage(py::module &mod)
Definition: random.cc:36
std::string getAlgorithmName() const
Definition: Random.cc:146
double flat(double const a, double const b)
Returns a random variate from the flat (uniform) distribution on [a, b).
Definition: Random.cc:175
double gaussian()
Returns a gaussian random variate with mean 0 and standard deviation 1
Definition: Random.cc:177
double uniformPos()
Returns a uniformly distributed random double precision floating point number from the generator...
Definition: Random.cc:164
double uniform()
Returns a uniformly distributed random double precision floating point number from the generator...
Definition: Random.cc:162
Random deepCopy() const
Creates a deep copy of this random number generator.
Definition: Random.cc:116
A class that can be used to generate sequences of random numbers according to a number of different a...
Definition: Random.h:62
PYBIND11_PLUGIN(_approximate)
Definition: approximate.cc:56
static std::vector< std::string > const & getAlgorithmNames()
Definition: Random.cc:148