24#include "pybind11/pybind11.h"
26#include "ndarray/pybind11.h"
32using namespace pybind11::literals;
39using PyCModelStageControl = py::class_<CModelStageControl, std::shared_ptr<CModelStageControl>>;
40using PyCModelControl = py::class_<CModelControl, std::shared_ptr<CModelControl>>;
41using PyCModelStageResult = py::class_<CModelStageResult, std::shared_ptr<CModelStageResult>>;
42using PyCModelResult = py::class_<CModelResult, std::shared_ptr<CModelResult>>;
43using PyCModelAlgorithm = py::class_<CModelAlgorithm, std::shared_ptr<CModelAlgorithm>>;
45static PyCModelStageControl declareCModelStageControl(
py::module &mod) {
46 PyCModelStageControl cls(mod,
"CModelStageControl");
47 cls.def(py::init<>());
66static PyCModelControl declareCModelControl(
py::module &mod) {
67 PyCModelControl cls(mod,
"CModelControl");
68 cls.def(py::init<>());
85 bool operator[](
int i)
const {
return (*_target)[i]; }
89 template <
typename PyParent>
90 static void declare(PyParent &parent) {
91 py::class_<BitSetView<N>> cls(parent, (
"BitSetView" +
std::to_string(N)).c_str());
92 cls.def(
"__getitem__", &BitSetView<N>::operator[]);
93 cls.def(
"__len__", &BitSetView<N>::size);
100static PyCModelStageResult declareCModelStageResult(
py::module &mod) {
101 PyCModelStageResult cls(mod,
"CModelStageResult");
103 cls.def(py::init<>());
133 cls.def_property_readonly(
135 [](CModelStageResult
const &
self) {
return BitSetView<CModelStageResult::N_FLAGS>(&
self.flags); },
136 py::return_value_policy::reference_internal);
141static PyCModelResult declareCModelResult(
py::module &mod) {
142 PyCModelResult cls(mod,
"CModelResult");
144 cls.def(py::init<>());
151 cls.attr(
"REGION_USED_INITIAL_ELLIPSE_MIN") =
153 cls.attr(
"REGION_USED_INITIAL_ELLIPSE_MAX") =
175 cls.def_property_readonly(
176 "flags", [](CModelResult
const &
self) {
return BitSetView<CModelResult::N_FLAGS>(&
self.flags); },
177 py::return_value_policy::reference_internal);
182static PyCModelAlgorithm declareCModelAlgorithm(
py::module &mod) {
183 PyCModelAlgorithm cls(mod,
"CModelAlgorithm");
184 cls.def(py::init<std::string const &, CModelControl const &, afw::table::Schema &>(),
"name"_a,
"ctrl"_a,
186 cls.def(py::init<std::string const &, CModelControl const &, afw::table::SchemaMapper &>(),
"name"_a,
187 "ctrl"_a,
"schemaMapper"_a);
188 cls.def(py::init<CModelControl const &>(),
"ctrl"_a);
191 "approxFlux"_a = -1,
"kronRadius"_a = -1,
"footprintArea"_a = -1);
193 "approxFlux"_a = -1);
194 cls.def(
"measure", (
void (CModelAlgorithm::*)(afw::table::SourceRecord &,
195 afw::image::Exposure<Pixel>
const &)
const) &
197 "measRecord"_a,
"exposure"_a);
199 (
void (CModelAlgorithm::*)(afw::table::SourceRecord &, afw::image::Exposure<Pixel>
const &,
200 afw::table::SourceRecord
const &)
const) &
202 "measRecord"_a,
"exposure"_a,
"refRecord"_a);
209 py::module::import(
"lsst.afw.geom.ellipses");
210 py::module::import(
"lsst.afw.detection");
211 py::module::import(
"lsst.meas.modelfit.model");
212 py::module::import(
"lsst.meas.modelfit.priors");
213 py::module::import(
"lsst.meas.modelfit.optimizer");
214 py::module::import(
"lsst.meas.modelfit.pixelFitRegion");
215 py::module::import(
"lsst.meas.modelfit.unitTransformedLikelihood");
217 declareCModelStageControl(mod);
218 auto clsControl = declareCModelControl(mod);
219 declareCModelStageResult(mod);
220 auto clsResult = declareCModelResult(mod);
221 auto clsAlgorithm = declareCModelAlgorithm(mod);
222 clsAlgorithm.attr(
"Control") = clsControl;
223 clsAlgorithm.attr(
"Result") = clsResult;
Key< Flag > const & target
void writeResultToRecord(Result const &result, afw::table::BaseRecord &record) const
Copy values from a Result struct to a BaseRecord object.
Control const & getControl() const
Return the control object the algorithm was constructed with.
Result applyForced(afw::image::Exposure< Pixel > const &exposure, shapelet::MultiShapeletFunction const &psf, geom::Point2D const ¢er, Result const &reference, Scalar approxFlux=-1) const
Run the CModel algorithm in forced mode on an image, supplying inputs directly and returning outputs ...
void fail(afw::table::SourceRecord &measRecord, meas::base::MeasurementError *error) const
Handle an exception thrown by one of the measure() methods, setting the appropriate flag in the given...
void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< Pixel > const &exposure) const
Run the CModel algorithm on an image, using a SourceRecord for inputs and outputs.
Result apply(afw::image::Exposure< Pixel > const &exposure, shapelet::MultiShapeletFunction const &psf, geom::Point2D const ¢er, afw::geom::ellipses::Quadrupole const &moments, Scalar approxFlux=-1, Scalar kronRadius=-1, int footprintArea=-1) const
Run the CModel algorithm on an image, supplying inputs directly and returning outputs in a Result.
PYBIND11_MODULE(_cameraGeom, mod)
def declare(module, exception_name, base, wrapped_class)
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.
#define LSST_DECLARE_NESTED_CONTROL_FIELD(WRAPPER, CLASS, NAME)
Macro used to wrap fields declared by LSST_NESTED_CONTROL_FIELD using Pybind11.
Scalar instFlux
Flux from the final linear fit.
Scalar objective
Objective value at the best-fit point (chisq/2)
Scalar fracDev
Fraction of flux from the final linear fit in the de Vaucouleur component (always between 0 and 1).
@ REGION_USED_INITIAL_ELLIPSE_MAX
Fit region implied by the best-fit ellipse of the initial was too large, so we used the configuration...
@ REGION_USED_PSF_AREA
Kron radius was unavailable or outside bounds, so the second-moment ellipse scaled to the PSF area wa...
@ FAILED
General failure flag for the linear fit flux; set if any other CModel flag is set,...
@ REGION_USED_INITIAL_ELLIPSE_MIN
Fit region implied by the best-fit ellipse of the initial was too small, so we used the configuration...
@ REGION_USED_FOOTPRINT_AREA
Kron radius was unavailable or outside bounds, so the second-moment ellipse scaled to the footprint a...
@ REGION_MAX_BAD_PIXEL_FRACTION
Set if we aborted early because the fit region had too many bad pixels.
@ NO_FLUX
No flux was measured.
@ REGION_MAX_AREA
Set if we aborted early because the fit region was too large.
Scalar instFluxErr
Flux uncertainty from the final linear fit.
afw::geom::ellipses::Quadrupole finalFitRegion
Pixels used in the exp, dev, and linear fits.
CModelStageResult dev
Results from the de Vaucouleur (Sersic n=4) fit.
CModelStageResult initial
Results from the initial approximate nonlinear fit that feeds the others.
CModelStageResult exp
Results from the exponential (Sersic n=1) fit.
Scalar instFluxInner
Flux measured strictly within the fit region (no extrapolation).
afw::geom::ellipses::Quadrupole initialFitRegion
Pixels used in the initial fit.
LocalUnitTransform fitSysToMeasSys
Transforms to the coordinate system where parameters are defined.
std::shared_ptr< Model > getModel() const
shapelet::RadialProfile const & getProfile() const
std::shared_ptr< Prior > getPrior() const
ndarray::Array< Scalar const, 1, 1 > fixed
Opaque fixed parameters in specialized units.
std::shared_ptr< OptimizerObjective > objfunc
Objective class used by the optimizer.
Scalar instFluxErr
Flux uncertainty from just this stage fit.
std::shared_ptr< Model > model
Model object that defines the parametrization (defined fully by Control struct)
Scalar time
Time spent in this fit in seconds.
std::shared_ptr< Prior > prior
Bayesian priors on the parameters (defined fully by Control struct)
@ TR_SMALL
Whether convergence was due to the optimizer trust region getting too small (not a failure!...
@ N_FLAGS
Non-flag counter to indicate the number of flags.
@ MAX_ITERATIONS
Whether the optimizer exceeded the maximum number of iterations.
@ NUMERIC_ERROR
Optimizer encountered a numerical error (something likely went to infinity).
@ BAD_REFERENCE
Reference fit failed, so forced fit will fail as well.
@ FAILED
General flag, indicating whether the flux for this stage can be trusted.
@ NO_FLUX
No flux was measured.
std::shared_ptr< UnitTransformedLikelihood > likelihood
Object used to evaluate models and compare to data.
Scalar objective
Value of the objective function at the best fit point: chisq/2 - ln(prior)
ndarray::Array< Scalar const, 1, 1 > nonlinear
Opaque nonlinear parameters in specialized units.
Scalar instFluxInner
Flux measured strictly within the fit region (no extrapolation).
Scalar instFlux
Flux measured from just this stage fit.
afw::geom::ellipses::Quadrupole ellipse
Best fit half-light ellipse in pixel coordinates.
ndarray::Array< Scalar const, 1, 1 > amplitudes
Opaque linear parameters in specialized units.