LSSTApplications  19.0.0-10-g920eed2,19.0.0-11-g48a0200+2,19.0.0-18-gfc4e62b+13,19.0.0-2-g3b2f90d+2,19.0.0-2-gd671419+5,19.0.0-20-g5a5a17ab+11,19.0.0-21-g2644856+13,19.0.0-23-g84eeccb+1,19.0.0-24-g878c510+1,19.0.0-25-g6c8df7140,19.0.0-25-gb330496+1,19.0.0-3-g2b32d65+5,19.0.0-3-g8227491+12,19.0.0-3-g9c54d0d+12,19.0.0-3-gca68e65+8,19.0.0-3-gcfc5f51+5,19.0.0-3-ge110943+11,19.0.0-3-ge74d124,19.0.0-3-gfe04aa6+13,19.0.0-30-g9c3fd16+1,19.0.0-4-g06f5963+5,19.0.0-4-g3d16501+13,19.0.0-4-g4a9c019+5,19.0.0-4-g5a8b323,19.0.0-4-g66397f0+1,19.0.0-4-g8278b9b+1,19.0.0-4-g8557e14,19.0.0-4-g8964aba+13,19.0.0-4-ge404a01+12,19.0.0-5-g40f3a5a,19.0.0-5-g4db63b3,19.0.0-5-gfb03ce7+13,19.0.0-6-gbaebbfb+12,19.0.0-61-gec4c6e08+1,19.0.0-7-g039c0b5+11,19.0.0-7-gbea9075+4,19.0.0-7-gc567de5+13,19.0.0-71-g41c0270,19.0.0-9-g2f02add+1,19.0.0-9-g463f923+12,w.2020.22
LSSTDataManagementBasePackage
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
LSST_DECLARE_NESTED_CONTROL_FIELD
#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
lsst::afw::image
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Definition: imageAlgorithm.dox:1
moments
VectorQ moments
Definition: simpleShape.cc:151
lsst::meas::extensions::psfex::PYBIND11_MODULE
PYBIND11_MODULE(psf, mod)
Definition: psf.cc:37
GeneralPsfFitter.h
psf
Key< int > psf
Definition: Exposure.cc:65
lsst::meas::modelfit::Scalar
double Scalar
Typedefs to be used for probability and parameter values.
Definition: common.h:44
LSST_DECLARE_CONTROL_FIELD
#define LSST_DECLARE_CONTROL_FIELD(WRAPPER, CLASS, NAME)
Macro used to wrap fields declared by LSST_CONTROL_FIELD using Pybind11.
Definition: python.h:50
python.h
DoubleShapeletPsfApprox.h
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
pybind11
Definition: _GenericMap.cc:40
lsst::ip::isr.fringe.measure
def measure(mi, x, y, size, statistic, stats)
Definition: fringe.py:506
lsst::ip::diffim.Control
Control
Definition: __init__.py:49
lsst::meas::modelfit.psf.psfContinued.module
module
Definition: psfContinued.py:42