LSST Applications g04e9c324dd+8c5ae1fdc5,g134cb467dc+b203dec576,g18429d2f64+358861cd2c,g199a45376c+0ba108daf9,g1fd858c14a+dd066899e3,g262e1987ae+ebfced1d55,g29ae962dfc+72fd90588e,g2cef7863aa+aef1011c0b,g35bb328faa+8c5ae1fdc5,g3fd5ace14f+b668f15bc5,g4595892280+3897dae354,g47891489e3+abcf9c3559,g4d44eb3520+fb4ddce128,g53246c7159+8c5ae1fdc5,g67b6fd64d1+abcf9c3559,g67fd3c3899+1f72b5a9f7,g74acd417e5+cb6b47f07b,g786e29fd12+668abc6043,g87389fa792+8856018cbb,g89139ef638+abcf9c3559,g8d7436a09f+bcf525d20c,g8ea07a8fe4+9f5ccc88ac,g90f42f885a+6054cc57f1,g97be763408+06f794da49,g9dd6db0277+1f72b5a9f7,ga681d05dcb+7e36ad54cd,gabf8522325+735880ea63,gac2eed3f23+abcf9c3559,gb89ab40317+abcf9c3559,gbf99507273+8c5ae1fdc5,gd8ff7fe66e+1f72b5a9f7,gdab6d2f7ff+cb6b47f07b,gdc713202bf+1f72b5a9f7,gdfd2d52018+8225f2b331,ge365c994fd+375fc21c71,ge410e46f29+abcf9c3559,geaed405ab2+562b3308c0,gf9a733ac38+8c5ae1fdc5,w.2025.35
LSST Data Management Base Package
Loading...
Searching...
No Matches
_spatialCell.cc
Go to the documentation of this file.
1/*
2 * LSST Data Management System
3 * Copyright 2008-2016 AURA/LSST.
4 *
5 * This product includes software developed by the
6 * LSST Project (http://www.lsst.org/).
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the LSST License Statement and
19 * the GNU General Public License along with this program. If not,
20 * see <https://www.lsstcorp.org/LegalNotices/>.
21 */
22
23#include <memory>
24#include <vector>
25
26#include <pybind11/pybind11.h>
28#include <pybind11/stl.h>
29
30#include "lsst/geom/Box.h"
31#include "lsst/afw/image.h"
33
34namespace py = pybind11;
35using namespace pybind11::literals;
36
37namespace lsst {
38namespace afw {
39namespace math {
40
41namespace {
42
43using CandidateList = std::vector<std::shared_ptr<SpatialCellCandidate>>;
44
45// Wrap SpatialCellCandidate (an abstract class so no constructor is wrapped)
46void declareSpatialCellCandidate(lsst::cpputils::python::WrapperCollection &wrappers) {
47 using PyClass = py::classh<SpatialCellCandidate>;
48 auto clsSpatialCellCandidate =
49 wrappers.wrapType(PyClass(wrappers.module, "SpatialCellCandidate"), [](auto &mod, auto &cls) {
50 cls.def("getXCenter", &SpatialCellCandidate::getXCenter);
51 cls.def("getYCenter", &SpatialCellCandidate::getYCenter);
52 cls.def("instantiate", &SpatialCellCandidate::instantiate);
53 cls.def("getCandidateRating", &SpatialCellCandidate::getCandidateRating);
54 cls.def("setCandidateRating", &SpatialCellCandidate::setCandidateRating);
55 cls.def("getId", &SpatialCellCandidate::getId);
56 cls.def("getStatus", &SpatialCellCandidate::getStatus);
57 cls.def("setStatus", &SpatialCellCandidate::setStatus);
58 cls.def("isBad", &SpatialCellCandidate::isBad);
59 });
60 wrappers.wrapType(py::enum_<SpatialCellCandidate::Status>(clsSpatialCellCandidate, "Status"),
61 [](auto &mod, auto &enm) {
62 enm.value("BAD", SpatialCellCandidate::Status::BAD);
63 enm.value("GOOD", SpatialCellCandidate::Status::GOOD);
64 enm.value("UNKNOWN", SpatialCellCandidate::Status::UNKNOWN);
65 enm.export_values();
66 });
67}
68
69// Wrap SpatialCellCandidateIterator
70void declareSpatialCellCandidateIterator(lsst::cpputils::python::WrapperCollection &wrappers) {
71 wrappers.wrapType(
72 py::classh<SpatialCellCandidateIterator>(wrappers.module, "SpatialCellCandidateIterator"),
73 [](auto &mod, auto &cls) {
74 cls.def("__incr__", &SpatialCellCandidateIterator::operator++, py::is_operator());
75 cls.def(
76 "__deref__",
77 [](SpatialCellCandidateIterator &it) -> std::shared_ptr<SpatialCellCandidate> {
78 return *it;
79 },
80 py::is_operator());
81 cls.def("__eq__", &SpatialCellCandidateIterator::operator==, py::is_operator());
82 cls.def("__ne__", &SpatialCellCandidateIterator::operator!=, py::is_operator());
83 cls.def("__sub__", &SpatialCellCandidateIterator::operator-, py::is_operator());
84 });
85}
86
87// Wrap SpatialCell
88void declareSpatialCell(lsst::cpputils::python::WrapperCollection &wrappers) {
89 wrappers.wrapType(
90 py::classh<SpatialCell>(wrappers.module, "SpatialCell"),
91 [](auto &mod, auto &cls) {
92 cls.def(py::init<std::string const &, lsst::geom::Box2I const &, CandidateList const &>(),
93 "label"_a, "bbox"_a = lsst::geom::Box2I(), "candidateList"_a = CandidateList());
94
95 cls.def("empty", &SpatialCell::empty);
96 cls.def("size", &SpatialCell::size);
97 cls.def("__len__", &SpatialCell::size);
98 cls.def("getLabel", &SpatialCell::getLabel);
99 cls.def("begin", (SpatialCellCandidateIterator(SpatialCell::*)()) & SpatialCell::begin);
100 cls.def("begin", (SpatialCellCandidateIterator(SpatialCell::*)(bool)) & SpatialCell::begin);
101 cls.def("end", (SpatialCellCandidateIterator(SpatialCell::*)()) & SpatialCell::end);
102 cls.def("end", (SpatialCellCandidateIterator(SpatialCell::*)(bool)) & SpatialCell::end);
103 cls.def("insertCandidate", &SpatialCell::insertCandidate);
104 cls.def("removeCandidate", &SpatialCell::removeCandidate);
105 cls.def("setIgnoreBad", &SpatialCell::setIgnoreBad, "ignoreBad"_a);
106 cls.def("getIgnoreBad", &SpatialCell::getIgnoreBad);
107 cls.def("getCandidateById", &SpatialCell::getCandidateById, "id"_a, "noThrow"_a = false);
108 cls.def("getLabel", &SpatialCell::getLabel);
109 cls.def("getBBox", &SpatialCell::getBBox);
110 cls.def("sortCandidates", &SpatialCell::sortCandidates);
111 cls.def("visitCandidates",
112 (void (SpatialCell::*)(CandidateVisitor *, int const, bool const, bool const)) &
113 SpatialCell::visitCandidates,
114 "visitor"_a, "nMaxPerCell"_a = -1, "ignoreExceptions"_a = false, "reset"_a = true);
115 cls.def("visitAllCandidates",
116 (void (SpatialCell::*)(CandidateVisitor *, bool const, bool const)) &
117 SpatialCell::visitAllCandidates,
118 "visitor"_a, "ignoreExceptions"_a = false, "reset"_a = true);
119 });
120}
121
122// Wrap SpatialCellSet
123void declareSpatialCellSet(lsst::cpputils::python::WrapperCollection &wrappers) {
124 wrappers.wrapType(
125 py::classh<SpatialCellSet>(wrappers.module, "SpatialCellSet"),
126 [](auto &mod, auto &cls) {
127 cls.def(py::init<lsst::geom::Box2I const &, int, int>(), "region"_a, "xSize"_a,
128 "ySize"_a = 0);
129
130 cls.def("getCellList", &SpatialCellSet::getCellList);
131 cls.def("getBBox", &SpatialCellSet::getBBox);
132 cls.def("insertCandidate", &SpatialCellSet::insertCandidate);
133 cls.def("sortCandidates", &SpatialCellSet::sortCandidates);
134 cls.def("visitCandidates",
135 (void (SpatialCellSet::*)(CandidateVisitor *, int const, bool const)) &
136 SpatialCellSet::visitCandidates,
137 "visitor"_a, "nMaxPerCell"_a = -1, "ignoreExceptions"_a = false);
138 cls.def("visitAllCandidates",
139 (void (SpatialCellSet::*)(CandidateVisitor *, bool const)) &
140 SpatialCellSet::visitAllCandidates,
141 "visitor"_a, "ignoreExceptions"_a = false);
142 cls.def("getCandidateById", &SpatialCellSet::getCandidateById, "id"_a, "noThrow"_a = false);
143 cls.def("setIgnoreBad", &SpatialCellSet::setIgnoreBad, "ignoreBad"_a);
144 });
145}
146
147// Wrap CandidateVisitor
148void declareCandidateVisitor(lsst::cpputils::python::WrapperCollection &wrappers) {
149 wrappers.wrapType(py::classh<CandidateVisitor>(wrappers.module,
150 "CandidateVisitor"),
151 [](auto &mod, auto &cls) {
152 cls.def(py::init<>());
153
154 cls.def("reset", &CandidateVisitor::reset);
155 cls.def("processCandidate", &CandidateVisitor::processCandidate);
156 });
157}
158
159void declareSpatialCellImageCandidate(lsst::cpputils::python::WrapperCollection &wrappers) {
160 wrappers.wrapType(py::classh<SpatialCellImageCandidate,
161 SpatialCellCandidate>(wrappers.module, "SpatialCellImageCandidate"),
162 [](auto &mod, auto &cls) {
163 cls.def_static("setWidth", &SpatialCellImageCandidate::setWidth, "width"_a);
164 cls.def_static("getWidth", &SpatialCellImageCandidate::getWidth);
165 cls.def_static("setHeight", &SpatialCellImageCandidate::setHeight, "height"_a);
166 cls.def_static("getHeight", &SpatialCellImageCandidate::getHeight);
167 cls.def("setChi2", &SpatialCellImageCandidate::setChi2, "chi2"_a);
168 cls.def("getChi2", &SpatialCellImageCandidate::getChi2);
169 });
170}
171
172void declareTestClasses(lsst::cpputils::python::WrapperCollection &wrappers) {
173 /*
174 * Test class for SpatialCellCandidate
175 */
176 class TestCandidate : public SpatialCellCandidate {
177 public:
178 TestCandidate(float const xCenter,
179 float const yCenter,
180 float const flux
181 )
182 : SpatialCellCandidate(xCenter, yCenter), _flux(flux) {}
183
185 double getCandidateRating() const override { return _flux; }
186 void setCandidateRating(double flux) override { _flux = flux; }
187
188 private:
189 double _flux;
190 };
191
193 class TestCandidateVisitor : public CandidateVisitor {
194 public:
195 TestCandidateVisitor() : CandidateVisitor() {}
196
197 // Called by SpatialCellSet::visitCandidates before visiting any Candidates
198 void reset() override { _n = 0; }
199
200 // Called by SpatialCellSet::visitCandidates for each Candidate
201 void processCandidate(SpatialCellCandidate *candidate) override { ++_n; }
202
203 int getN() const { return _n; }
204
205 private:
206 int _n{0}; // number of TestCandidates
207 };
208
209 class TestImageCandidate : public SpatialCellImageCandidate {
210 public:
211 using MaskedImageT = image::MaskedImage<float>;
212
213 TestImageCandidate(float const xCenter,
214 float const yCenter,
215 float const flux
216 )
217 : SpatialCellImageCandidate(xCenter, yCenter), _flux(flux) {}
218
220 double getCandidateRating() const override { return _flux; }
221
223 std::shared_ptr<MaskedImageT const> getMaskedImage() const {
224 if (!_image) {
225 _image = std::make_shared<MaskedImageT>(lsst::geom::ExtentI(getWidth(), getHeight()));
226 *_image->getImage() = _flux;
227 }
228 return _image;
229 }
230
231 private:
232 mutable std::shared_ptr<MaskedImageT> _image;
233 double _flux;
234 };
235
236 wrappers.wrapType(py::classh<TestCandidate, SpatialCellCandidate>(
237 wrappers.module, "TestCandidate"),
238 [](auto &mod, auto &cls) {
239 cls.def(py::init<float const, float const, float const>());
240 cls.def("getCandidateRating", &TestCandidate::getCandidateRating);
241 cls.def("setCandidateRating", &TestCandidate::setCandidateRating);
242 });
243 wrappers.wrapType(
244 py::classh<TestCandidateVisitor, CandidateVisitor>(
245 wrappers.module, "TestCandidateVisitor"),
246 [](auto &mod, auto &cls) {
247 cls.def(py::init<>());
248 cls.def("getN", &TestCandidateVisitor::getN);
249 });
250 wrappers.wrapType(
251 py::classh<TestImageCandidate, SpatialCellImageCandidate>(
252 wrappers.module, "TestImageCandidate"),
253 [](auto &mod, auto &cls) {
254 cls.def(py::init<float const, float const, float const>(), "xCenter"_a, "yCenter"_a,
255 "flux"_a);
256 cls.def("getCandidateRating", &TestImageCandidate::getCandidateRating);
257 cls.def("getMaskedImage", &TestImageCandidate::getMaskedImage);
258 });
259};
260} // namespace
262 declareSpatialCellCandidate(wrappers);
263 declareSpatialCellCandidateIterator(wrappers);
264 declareSpatialCell(wrappers);
265 declareSpatialCellSet(wrappers);
266 declareCandidateVisitor(wrappers);
267 declareSpatialCellImageCandidate(wrappers);
268 /* Test Members */
269 declareTestClasses(wrappers);
270}
271} // namespace math
272} // namespace afw
273} // namespace lsst
Base class for candidate objects in a SpatialCell.
Definition SpatialCell.h:70
Base class for candidate objects in a SpatialCell that are able to return an Image of some sort (e....
A helper class for subdividing pybind11 module across multiple translation units (i....
Definition python.h:242
PyType wrapType(PyType cls, ClassWrapperCallback function, bool setModuleName=true)
Add a type (class or enum) wrapper, deferring method and other attribute definitions until finish() i...
Definition python.h:391
pybind11::module module
The module object passed to the PYBIND11_MODULE block that contains this WrapperCollection.
Definition python.h:448
T make_shared(T... args)
void wrapSpatialCell(lsst::cpputils::python::WrapperCollection &)
py::classh< PixelAreaBoundedField, BoundedField > PyClass
Extent< int, 2 > ExtentI
Definition Extent.h:396