Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-37767: use pybind wrappers, merge into one pybind shared lib #106

Merged
merged 2 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 17 additions & 18 deletions python/lsst/meas/modelfit/SConscript
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
# -*- python -*-
from lsst.sconsUtils import scripts
scripts.BasicSConscript.pybind11(
['mixture',
'unitSystem',
'priors/priors',
'model',
'multiModel',
'likelihood',
'sampler',
'adaptiveImportanceSampler',
'optimizer/optimizer',
'psf/psf',
'pixelFitRegion/pixelFitRegion',
'truncatedGaussian',
'integrals',
'unitTransformedLikelihood',
'cmodel/cmodel',
],
addUnderscore=False
scripts.BasicSConscript.python(['_modelfitLib'], [
'_modelfitLib.cc',
'adaptiveImportanceSampler.cc',
'cmodel/cmodel.cc',
'integrals.cc',
'likelihood.cc',
'mixture.cc',
'model.cc',
'multiModel.cc',
'optimizer/optimizer.cc',
'pixelFitRegion/pixelFitRegion.cc',
'priors/priors.cc',
'psf/psf.cc',
'sampler.cc',
'truncatedGaussian.cc',
'unitSystem.cc',
'unitTransformedLikelihood.cc']
)
16 changes: 4 additions & 12 deletions python/lsst/meas/modelfit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,12 @@
#
from .version import *
from .common import *
from .mixture import *
from .unitSystem import *

from ._modelfitLib import *
from .priors import *
from .model import *
from .multiModel import *
from .likelihood import *
from .sampler import *
from .adaptiveImportanceSampler import *
from .optimizer import *
from .pixelFitRegion import *
from .psf import *
from .truncatedGaussian import *
from .unitTransformedLikelihood import *
from .pixelFitRegion import *
from .cmodel import *

# Match C++ namespace, without bothering with a new package for just one file.
from . import integrals as detail
from .detail import *
84 changes: 84 additions & 0 deletions python/lsst/meas/modelfit/_modelfitLib.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* This file is part of meas_modelfit.
*
* Developed for the LSST Data Management System.
* This product includes software developed by the LSST Project
* (https://www.lsst.org).
* See the COPYRIGHT file at the top-level directory of this distribution
* for details of code ownership.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "pybind11/pybind11.h"
#include "lsst/cpputils/python.h"

namespace py = pybind11;
using namespace pybind11::literals;
using lsst::cpputils::python::WrapperCollection;

namespace lsst {
namespace meas {
namespace modelfit {

void wrapAdaptiveImportanceSampler(lsst::cpputils::python::WrapperCollection &wrappers);
void wrapCmodel(lsst::cpputils::python::WrapperCollection &wrappers);
void wrapIntegrals(lsst::cpputils::python::WrapperCollection &wrappers);
void wrapLikelihood(lsst::cpputils::python::WrapperCollection &wrappers);
void wrapMixture(lsst::cpputils::python::WrapperCollection &wrappers);
void wrapModel(lsst::cpputils::python::WrapperCollection &wrappers);
void wrapMultiModel(lsst::cpputils::python::WrapperCollection &wrappers);
void wrapOptimizer(lsst::cpputils::python::WrapperCollection &wrappers);
void wrapPixelFitRegion(lsst::cpputils::python::WrapperCollection &wrappers);
void wrapPriors(lsst::cpputils::python::WrapperCollection &wrappers);
void wrapPsf(lsst::cpputils::python::WrapperCollection &wrappers);
void wrapSampler(lsst::cpputils::python::WrapperCollection &wrappers);
void wrapTruncatedGaussian(lsst::cpputils::python::WrapperCollection &wrappers);
void wrapUnitSystem(lsst::cpputils::python::WrapperCollection &wrappers);
void wrapUnitTransformedLikelihood(lsst::cpputils::python::WrapperCollection &wrappers);

PYBIND11_MODULE(_modelfitLib, mod) {
lsst::utils::python::WrapperCollection wrappers(mod, "lsst.meas.modelfit");

wrappers.addInheritanceDependency("lsst.meas.base");

wrappers.addSignatureDependency("lsst.afw.detection");
wrappers.addSignatureDependency("lsst.afw.image");
wrappers.addSignatureDependency("lsst.afw.math");
wrappers.addSignatureDependency("lsst.afw.geom.ellipses");
wrappers.addSignatureDependency("lsst.afw.table");
wrappers.addSignatureDependency("lsst.shapelet");

wrapPriors(wrappers);
wrapUnitSystem(wrappers);
wrapModel(wrappers);
wrapLikelihood(wrappers);
wrapMixture(wrappers);
wrapOptimizer(wrappers);
wrapSampler(wrappers);
wrapPixelFitRegion(wrappers);

wrapAdaptiveImportanceSampler(wrappers);
wrapCmodel(wrappers);
wrapIntegrals(wrappers);
wrapMultiModel(wrappers);
wrapPsf(wrappers);
wrapTruncatedGaussian(wrappers);
wrapUnitTransformedLikelihood(wrappers);
wrappers.finish();
}

}
}
}
51 changes: 23 additions & 28 deletions python/lsst/meas/modelfit/adaptiveImportanceSampler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/

#include "pybind11/pybind11.h"
#include "lsst/cpputils/python.h"

#include "lsst/pex/config/python.h"
#include "lsst/meas/modelfit/AdaptiveImportanceSampler.h"
Expand All @@ -35,40 +36,34 @@ using namespace pybind11::literals;
namespace lsst {
namespace meas {
namespace modelfit {
namespace {

using PyImportanceSamplerControl =
py::class_<ImportanceSamplerControl, std::shared_ptr<ImportanceSamplerControl>>;
using PyAdaptiveImportanceSampler =
py::class_<AdaptiveImportanceSampler, std::shared_ptr<AdaptiveImportanceSampler>, Sampler>;

PYBIND11_MODULE(adaptiveImportanceSampler, mod) {
py::module::import("lsst.afw.table");
py::module::import("lsst.afw.math");
py::module::import("lsst.meas.modelfit.sampler");
py::module::import("lsst.meas.modelfit.mixture");
void wrapAdaptiveImportanceSampler(lsst::cpputils::python::WrapperCollection &wrappers) {
wrappers.wrapType(PyImportanceSamplerControl(wrappers.module, "ImportanceSamplerControl"), [](auto &mod, auto &cls) {
cls.def(py::init<>());
LSST_DECLARE_CONTROL_FIELD(cls, ImportanceSamplerControl, nSamples);
LSST_DECLARE_CONTROL_FIELD(cls, ImportanceSamplerControl, nUpdateSteps);
LSST_DECLARE_CONTROL_FIELD(cls, ImportanceSamplerControl, tau1);
LSST_DECLARE_CONTROL_FIELD(cls, ImportanceSamplerControl, tau2);
LSST_DECLARE_CONTROL_FIELD(cls, ImportanceSamplerControl, targetPerplexity);
LSST_DECLARE_CONTROL_FIELD(cls, ImportanceSamplerControl, maxRepeat);

PyImportanceSamplerControl clsImportanceSamplerControl(mod, "ImportanceSamplerControl");
clsImportanceSamplerControl.def(py::init<>());
LSST_DECLARE_CONTROL_FIELD(clsImportanceSamplerControl, ImportanceSamplerControl, nSamples);
LSST_DECLARE_CONTROL_FIELD(clsImportanceSamplerControl, ImportanceSamplerControl, nUpdateSteps);
LSST_DECLARE_CONTROL_FIELD(clsImportanceSamplerControl, ImportanceSamplerControl, tau1);
LSST_DECLARE_CONTROL_FIELD(clsImportanceSamplerControl, ImportanceSamplerControl, tau2);
LSST_DECLARE_CONTROL_FIELD(clsImportanceSamplerControl, ImportanceSamplerControl, targetPerplexity);
LSST_DECLARE_CONTROL_FIELD(clsImportanceSamplerControl, ImportanceSamplerControl, maxRepeat);

PyAdaptiveImportanceSampler clsAdaptiveImportanceSampler(mod, "AdaptiveImportanceSampler");
clsAdaptiveImportanceSampler.def(py::init<afw::table::Schema &, std::shared_ptr<afw::math::Random>,
std::map<int, ImportanceSamplerControl> const &, bool>(),
"sampleSchema"_a, "rng"_a, "ctrls"_a, "doSaveIteration"_a = false);
// virtual run method already wrapped by Sampler base class
clsAdaptiveImportanceSampler.def("computeNormalizedPerplexity",
&AdaptiveImportanceSampler::computeNormalizedPerplexity);
clsAdaptiveImportanceSampler.def("computeEffectiveSampleSizeFraction",
&AdaptiveImportanceSampler::computeEffectiveSampleSizeFraction);
PyAdaptiveImportanceSampler clsAdaptiveImportanceSampler(mod, "AdaptiveImportanceSampler");
clsAdaptiveImportanceSampler.def(py::init<afw::table::Schema &, std::shared_ptr<afw::math::Random>,
std::map<int, ImportanceSamplerControl> const &, bool>(),
"sampleSchema"_a, "rng"_a, "ctrls"_a, "doSaveIteration"_a = false);
// virtual run method already wrapped by Sampler base class
clsAdaptiveImportanceSampler.def("computeNormalizedPerplexity",
&AdaptiveImportanceSampler::computeNormalizedPerplexity);
clsAdaptiveImportanceSampler.def("computeEffectiveSampleSizeFraction",
&AdaptiveImportanceSampler::computeEffectiveSampleSizeFraction);
});
}

}
}
}
} // namespace lsst::meas::modelfit::anonymous
} // namespace modelfit
} // namespace meas
} // namespace lsst
3 changes: 2 additions & 1 deletion python/lsst/meas/modelfit/cmodel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
# see <http://www.lsstcorp.org/LegalNotices/>.
#

from .cmodel import *
from .._modelfitLib import (CModelAlgorithm, CModelControl, CModelResult,
CModelStageControl, CModelStageResult)
from .cmodelContinued import *