LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
psf.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008-2013 LSST Corporation.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 
24 #include "pybind11/pybind11.h"
25 
26 #include "lsst/pex/config/python.h"
27 
30 
31 namespace py = pybind11;
32 using namespace pybind11::literals;
33 
34 namespace lsst {
35 namespace meas {
36 namespace modelfit {
37 namespace {
38 
39 void declareDoubleShapelet(py::module &mod) {
40  using Control = DoubleShapeletPsfApproxControl;
41  using Algorithm = DoubleShapeletPsfApproxAlgorithm;
42 
43  using PyControl = py::class_<Control, std::shared_ptr<Control>>;
44  using PyAlgorithm = py::class_<Algorithm, std::shared_ptr<Algorithm>, meas::base::SimpleAlgorithm>;
45 
46  PyControl clsControl(mod, "DoubleShapeletPsfApproxControl");
47  clsControl.def(py::init<>());
48  LSST_DECLARE_CONTROL_FIELD(clsControl, Control, innerOrder);
49  LSST_DECLARE_CONTROL_FIELD(clsControl, Control, outerOrder);
50  LSST_DECLARE_CONTROL_FIELD(clsControl, Control, radiusRatio);
51  LSST_DECLARE_CONTROL_FIELD(clsControl, Control, peakRatio);
52  LSST_DECLARE_CONTROL_FIELD(clsControl, Control, minRadius);
53  LSST_DECLARE_CONTROL_FIELD(clsControl, Control, minRadiusDiff);
54  LSST_DECLARE_CONTROL_FIELD(clsControl, Control, maxRadiusBoxFraction);
55  LSST_DECLARE_NESTED_CONTROL_FIELD(clsControl, Control, optimizer);
56 
57  PyAlgorithm clsAlgorithm(mod, "DoubleShapeletPsfApproxAlgorithm");
58  // wrap anonymous enum values as ints because we'll need to use them as ints
59  clsAlgorithm.attr("Control") = clsControl;
60  clsAlgorithm.attr("FAILURE") = py::cast(Algorithm::FAILURE);
61  clsAlgorithm.attr("INVALID_POINT_FOR_PSF") = py::cast(Algorithm::INVALID_POINT_FOR_PSF);
62  clsAlgorithm.attr("INVALID_MOMENTS") = py::cast(Algorithm::INVALID_MOMENTS);
63  clsAlgorithm.attr("MAX_ITERATIONS") = py::cast(Algorithm::MAX_ITERATIONS);
64 
65  clsAlgorithm.def(py::init<Control const &, std::string const &, afw::table::Schema &>(), "ctrl"_a,
66  "name"_a, "schema"_a);
67  clsAlgorithm.def_static("initializeResult", &Algorithm::initializeResult, "ctrl"_a);
68  clsAlgorithm.def_static("fitMoments", &Algorithm::fitMoments, "result"_a, "ctrl"_a, "psfImage"_a);
69  clsAlgorithm.def_static("makeObjective", &Algorithm::makeObjective, "moments"_a, "ctrl"_a, "psfImage"_a);
70  clsAlgorithm.def_static("fitProfile", &Algorithm::fitProfile, "result"_a, "ctrl"_a, "psfImage"_a);
71  clsAlgorithm.def_static("fitShapelets", &Algorithm::fitShapelets, "result"_a, "ctrl"_a, "psfImage"_a);
72  clsAlgorithm.def("measure", &Algorithm::measure, "measRecord"_a, "exposure"_a);
73  clsAlgorithm.def("fail", &Algorithm::fail, "measRecord"_a, "error"_a = nullptr);
74 }
75 
76 void declareGeneral(py::module &mod) {
77  using ComponentControl = GeneralPsfFitterComponentControl;
78  using Control = GeneralPsfFitterControl;
79  using Fitter = GeneralPsfFitter;
80  using Algorithm = GeneralPsfFitterAlgorithm;
81 
82  using PyComponentControl = py::class_<ComponentControl, std::shared_ptr<ComponentControl>>;
83  using PyControl = py::class_<Control, std::shared_ptr<Control>>;
84  using PyFitter = py::class_<Fitter, std::shared_ptr<Fitter>>;
85  using PyAlgorithm = py::class_<Algorithm, std::shared_ptr<Algorithm>, Fitter>;
86 
87  PyComponentControl clsComponentControl(mod, "GeneralPsfFitterComponentControl");
88  clsComponentControl.def(py::init<int, double>(), "order"_a = 0, "radius"_a = 1.0);
89  LSST_DECLARE_CONTROL_FIELD(clsComponentControl, ComponentControl, order);
90  LSST_DECLARE_CONTROL_FIELD(clsComponentControl, ComponentControl, positionPriorSigma);
91  LSST_DECLARE_CONTROL_FIELD(clsComponentControl, ComponentControl, ellipticityPriorSigma);
92  LSST_DECLARE_CONTROL_FIELD(clsComponentControl, ComponentControl, radiusFactor);
93  LSST_DECLARE_CONTROL_FIELD(clsComponentControl, ComponentControl, radiusPriorSigma);
94 
95  PyControl clsControl(mod, "GeneralPsfFitterControl");
96  clsControl.def(py::init<>());
97  LSST_DECLARE_NESTED_CONTROL_FIELD(clsControl, Control, inner);
98  LSST_DECLARE_NESTED_CONTROL_FIELD(clsControl, Control, primary);
99  LSST_DECLARE_NESTED_CONTROL_FIELD(clsControl, Control, wings);
100  LSST_DECLARE_NESTED_CONTROL_FIELD(clsControl, Control, outer);
101  LSST_DECLARE_NESTED_CONTROL_FIELD(clsControl, Control, optimizer);
102  LSST_DECLARE_CONTROL_FIELD(clsControl, Control, defaultNoiseSigma);
103 
104  PyFitter clsFitter(mod, "GeneralPsfFitter");
105  clsFitter.def(py::init<Control const &>(), "ctrl"_a);
106  clsFitter.def("addFields", &Fitter::addFields, "schema"_a, "prefix"_a);
107  clsFitter.def("getModel", &Fitter::getModel);
108  clsFitter.def("getPrior", &Fitter::getPrior);
109  clsFitter.def("adapt", &Fitter::adapt, "previousFit"_a, "previousModel"_a);
110  // We use lambdas here because the C++ signature has an optional int*
111  // argument not relevant for Python (which confuses pybind11).
112  clsFitter.def("apply", [](Fitter const &self, afw::image::Image<Pixel> const &image,
113  afw::geom::ellipses::Quadrupole const &moments,
114  Scalar noiseSigma) { return self.apply(image, moments, noiseSigma); },
115  "image"_a, "moments"_a, "noiseSigma"_a = -1);
116  clsFitter.def("apply", [](Fitter const &self, afw::image::Image<double> const &image,
117  afw::geom::ellipses::Quadrupole const &moments,
118  Scalar noiseSigma) { return self.apply(image, moments, noiseSigma); },
119  "image"_a, "moments"_a, "noiseSigma"_a = -1);
120  clsFitter.def("apply", [](Fitter const &self, afw::image::Image<Pixel> const &image,
121  shapelet::MultiShapeletFunction const &initial,
122  Scalar noiseSigma) { return self.apply(image, initial, noiseSigma); },
123  "image"_a, "initial"_a, "noiseSigma"_a = -1);
124  clsFitter.def("apply", [](Fitter const &self, afw::image::Image<double> const &image,
125  shapelet::MultiShapeletFunction const &initial,
126  Scalar noiseSigma) { return self.apply(image, initial, noiseSigma); },
127  "image"_a, "initial"_a, "noiseSigma"_a = -1);
128 
129  PyAlgorithm clsAlgorithm(mod, "GeneralPsfFitterAlgorithm");
130  clsAlgorithm.attr("Control") = clsControl;
131  clsAlgorithm.attr("FAILURE") = py::cast(Algorithm::FAILURE);
132  clsAlgorithm.attr("MAX_INNER_ITERATIONS") = py::cast(Algorithm::MAX_INNER_ITERATIONS);
133  clsAlgorithm.attr("MAX_OUTER_ITERATIONS") = py::cast(Algorithm::MAX_OUTER_ITERATIONS);
134  clsAlgorithm.attr("EXCEPTION") = py::cast(Algorithm::EXCEPTION);
135  clsAlgorithm.attr("CONTAINS_NAN") = py::cast(Algorithm::CONTAINS_NAN);
136  clsAlgorithm.def(py::init<Control const &, afw::table::Schema &, std::string const &>(), "ctrl"_a,
137  "schema"_a, "prefix"_a);
138  clsAlgorithm.def("getKey", &Algorithm::getKey);
139  clsAlgorithm.def("measure",
140  (void (Algorithm::*)(afw::table::SourceRecord &, afw::image::Image<double> const &,
141  shapelet::MultiShapeletFunction const &) const) &
143  "measRecord"_a, "image"_a, "initial"_a);
144  clsAlgorithm.def("measure",
145  (void (Algorithm::*)(afw::table::SourceRecord &, afw::image::Image<double> const &,
146  afw::geom::ellipses::Quadrupole const &) const) &
148  "measRecord"_a, "image"_a, "moments"_a);
149  clsAlgorithm.def("fail", &Algorithm::fail, "measRecord"_a, "error"_a = nullptr);
150 
151  // MultiShapeletPsfLikelihood intentionally not exposed to Python.
152 }
153 
154 PYBIND11_MODULE(psf, mod) {
155  py::module::import("lsst.afw.image");
156  py::module::import("lsst.afw.geom.ellipses");
157  py::module::import("lsst.meas.base");
158  py::module::import("lsst.shapelet");
159  py::module::import("lsst.meas.modelfit.optimizer");
160 
161  declareDoubleShapelet(mod);
162  declareGeneral(mod);
163 }
164 
165 }
166 }
167 }
168 } // namespace lsst::meas::modelfit::anonymous
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
def measure(mi, x, y, size, statistic, stats)
Definition: fringe.py:517
PYBIND11_MODULE(psf, mod)
Definition: psf.cc:37
double Scalar
Typedefs to be used for probability and parameter values.
Definition: common.h:44
A base class for image defects.
#define LSST_DECLARE_CONTROL_FIELD(WRAPPER, CLASS, NAME)
Macro used to wrap fields declared by LSST_CONTROL_FIELD using Pybind11.
Definition: python.h:50
#define LSST_DECLARE_NESTED_CONTROL_FIELD(WRAPPER, CLASS, NAME)
Macro used to wrap fields declared by LSST_NESTED_CONTROL_FIELD using Pybind11.
Definition: python.h:72
VectorQ moments
Definition: simpleShape.cc:151
Key< int > psf
Definition: Exposure.cc:65
table::Key< int > order