24 #include "pybind11/pybind11.h" 
   26 #include "ndarray/pybind11.h" 
   32 using namespace pybind11::literals;
 
   39 using PyCModelStageControl = py::class_<CModelStageControl, std::shared_ptr<CModelStageControl>>;
 
   40 using PyCModelControl = py::class_<CModelControl, std::shared_ptr<CModelControl>>;
 
   41 using PyCModelStageResult = py::class_<CModelStageResult, std::shared_ptr<CModelStageResult>>;
 
   42 using PyCModelResult = py::class_<CModelResult, std::shared_ptr<CModelResult>>;
 
   43 using PyCModelAlgorithm = py::class_<CModelAlgorithm, std::shared_ptr<CModelAlgorithm>>;
 
   45 static PyCModelStageControl declareCModelStageControl(
py::module &mod) {
 
   46     PyCModelStageControl 
cls(mod, 
"CModelStageControl");
 
   47     cls.def(py::init<>());
 
   48     cls.def(
"getProfile", &CModelStageControl::getProfile);
 
   49     cls.def(
"getModel", &CModelStageControl::getModel);
 
   50     cls.def(
"getPrior", &CModelStageControl::getPrior);
 
   66 static 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);
 
  100 static PyCModelStageResult declareCModelStageResult(
py::module &mod) {
 
  101     PyCModelStageResult 
cls(mod, 
"CModelStageResult");
 
  103     cls.def(py::init<>());
 
  105     cls.attr(
"FAILED") = py::cast(
int(CModelStageResult::FAILED));
 
  106     cls.attr(
"TR_SMALL") = py::cast(
int(CModelStageResult::TR_SMALL));
 
  107     cls.attr(
"MAX_ITERATIONS") = py::cast(
int(CModelStageResult::MAX_ITERATIONS));
 
  108     cls.attr(
"NUMERIC_ERROR") = py::cast(
int(CModelStageResult::NUMERIC_ERROR));
 
  109     cls.attr(
"BAD_REFERENCE") = py::cast(
int(CModelStageResult::BAD_REFERENCE));
 
  110     cls.attr(
"NO_FLUX") = py::cast(
int(CModelStageResult::NO_FLUX));
 
  111     cls.attr(
"N_FLAGS") = py::cast(
int(CModelStageResult::N_FLAGS));
 
  116     cls.def_readonly(
"model", &CModelStageResult::model);
 
  117     cls.def_readonly(
"prior", &CModelStageResult::prior);
 
  118     cls.def_readonly(
"objfunc", &CModelStageResult::objfunc);
 
  119     cls.def_readonly(
"likelihood", &CModelStageResult::likelihood);
 
  120     cls.def_readonly(
"instFlux", &CModelStageResult::instFlux);
 
  121     cls.def_readonly(
"instFluxErr", &CModelStageResult::instFluxErr);
 
  122     cls.def_readonly(
"instFluxInner", &CModelStageResult::instFluxInner);
 
  123     cls.def_readonly(
"objective", &CModelStageResult::objective);
 
  124     cls.def_readonly(
"time", &CModelStageResult::time);
 
  125     cls.def_readonly(
"ellipse", &CModelStageResult::ellipse);
 
  126     cls.def_readonly(
"nonlinear", &CModelStageResult::nonlinear);
 
  128     cls.def_readonly(
"fixed", &CModelStageResult::fixed);
 
  133     cls.def_property_readonly(
 
  135             [](CModelStageResult 
const &
self) { 
return BitSetView<CModelStageResult::N_FLAGS>(&
self.flags); },
 
  136             py::return_value_policy::reference_internal);
 
  141 static PyCModelResult declareCModelResult(
py::module &mod) {
 
  142     PyCModelResult 
cls(mod, 
"CModelResult");
 
  144     cls.def(py::init<>());
 
  146     cls.attr(
"FAILED") = py::cast(
int(CModelResult::FAILED));
 
  147     cls.attr(
"REGION_MAX_AREA") = py::cast(
int(CModelResult::REGION_MAX_AREA));
 
  148     cls.attr(
"REGION_MAX_BAD_PIXEL_FRACTION") = py::cast(
int(CModelResult::REGION_MAX_BAD_PIXEL_FRACTION));
 
  149     cls.attr(
"REGION_USED_FOOTPRINT_AREA") = py::cast(
int(CModelResult::REGION_USED_FOOTPRINT_AREA));
 
  150     cls.attr(
"REGION_USED_PSF_AREA") = py::cast(
int(CModelResult::REGION_USED_PSF_AREA));
 
  151     cls.attr(
"REGION_USED_INITIAL_ELLIPSE_MIN") =
 
  152             py::cast(
int(CModelResult::REGION_USED_INITIAL_ELLIPSE_MIN));
 
  153     cls.attr(
"REGION_USED_INITIAL_ELLIPSE_MAX") =
 
  154             py::cast(
int(CModelResult::REGION_USED_INITIAL_ELLIPSE_MAX));
 
  155     cls.attr(
"NO_FLUX") = py::cast(
int(CModelResult::NO_FLUX));
 
  160     cls.def_readonly(
"instFlux", &CModelResult::instFlux);
 
  161     cls.def_readonly(
"instFluxErr", &CModelResult::instFluxErr);
 
  162     cls.def_readonly(
"instFluxInner", &CModelResult::instFluxInner);
 
  163     cls.def_readonly(
"fracDev", &CModelResult::fracDev);
 
  164     cls.def_readonly(
"objective", &CModelResult::objective);
 
  165     cls.def_readonly(
"initial", &CModelResult::initial);
 
  166     cls.def_readonly(
"exp", &CModelResult::exp);
 
  167     cls.def_readonly(
"dev", &CModelResult::dev);
 
  168     cls.def_readonly(
"initialFitRegion", &CModelResult::initialFitRegion);
 
  169     cls.def_readonly(
"finalFitRegion", &CModelResult::finalFitRegion);
 
  170     cls.def_readonly(
"fitSysToMeasSys", &CModelResult::fitSysToMeasSys);
 
  175     cls.def_property_readonly(
 
  176             "flags", [](CModelResult 
const &
self) { 
return BitSetView<CModelResult::N_FLAGS>(&
self.flags); },
 
  177             py::return_value_policy::reference_internal);
 
  182 static 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);
 
  189     cls.def(
"getControl", &CModelAlgorithm::getControl);
 
  190     cls.def(
"apply", &CModelAlgorithm::apply, 
"exposure"_a, 
"psf"_a, 
"center"_a, 
"moments"_a,
 
  191             "approxFlux"_a = -1, 
"kronRadius"_a = -1, 
"footprintArea"_a = -1);
 
  192     cls.def(
"applyForced", &CModelAlgorithm::applyForced, 
"exposure"_a, 
"psf"_a, 
"center"_a, 
"reference"_a,
 
  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);
 
  203     cls.def(
"fail", &CModelAlgorithm::fail, 
"measRecord"_a, 
"error"_a);
 
  204     cls.def(
"writeResultToRecord", &CModelAlgorithm::writeResultToRecord, 
"result"_a, 
"record"_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;