lsst.afw  13.0-91-g2a3e2ee
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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>
27 #include <pybind11/stl.h>
28 
29 #include "lsst/afw/geom/Box.h"
31 
32 namespace py = pybind11;
33 using namespace pybind11::literals;
34 
35 namespace lsst {
36 namespace afw {
37 namespace math {
38 
39 namespace {
40 
41 using CandidateList = std::vector<std::shared_ptr<SpatialCellCandidate>>;
42 
43 // Wrap SpatialCellCandidate (an abstract class so no constructor is wrapped)
44 void wrapSpatialCellCandidate(py::module &mod) {
45  py::class_<SpatialCellCandidate, std::shared_ptr<SpatialCellCandidate>> cls(mod, "SpatialCellCandidate");
46 
47  py::enum_<SpatialCellCandidate::Status>(cls, "Status")
48  .value("BAD", SpatialCellCandidate::Status::BAD)
49  .value("GOOD", SpatialCellCandidate::Status::GOOD)
50  .value("UNKNOWN", SpatialCellCandidate::Status::UNKNOWN)
51  .export_values();
52 
53  cls.def("getXCenter", &SpatialCellCandidate::getXCenter);
54  cls.def("getYCenter", &SpatialCellCandidate::getYCenter);
55  cls.def("instantiate", &SpatialCellCandidate::instantiate);
56  cls.def("getCandidateRating", &SpatialCellCandidate::getCandidateRating);
57  cls.def("setCandidateRating", &SpatialCellCandidate::setCandidateRating);
58  cls.def("getId", &SpatialCellCandidate::getId);
59  cls.def("getStatus", &SpatialCellCandidate::getStatus);
60  cls.def("setStatus", &SpatialCellCandidate::setStatus);
61  cls.def("isBad", &SpatialCellCandidate::isBad);
62 }
63 
64 // Wrap SpatialCellCandidateIterator
65 void wrapSpatialCellCandidateIterator(py::module &mod) {
66  py::class_<SpatialCellCandidateIterator> cls(mod, "SpatialCellCandidateIterator");
67  cls.def("__incr__", &SpatialCellCandidateIterator::operator++, py::is_operator());
68  cls.def("__deref__",
69  [](SpatialCellCandidateIterator &it) -> std::shared_ptr<SpatialCellCandidate> { return *it; },
70  py::is_operator());
71  cls.def("__eq__", &SpatialCellCandidateIterator::operator==, py::is_operator());
72  cls.def("__ne__", &SpatialCellCandidateIterator::operator!=, py::is_operator());
73  cls.def("__sub__", &SpatialCellCandidateIterator::operator-, py::is_operator());
74 }
75 
76 // Wrap SpatialCell
77 void wrapSpatialCell(py::module &mod) {
78  py::class_<SpatialCell, std::shared_ptr<SpatialCell>> cls(mod, "SpatialCell");
79 
80  cls.def(py::init<std::string const &, geom::Box2I const &, CandidateList const &>(), "label"_a,
81  "bbox"_a = geom::Box2I(), "candidateList"_a = CandidateList());
82 
83  cls.def("empty", &SpatialCell::empty);
84  cls.def("size", &SpatialCell::size);
85  cls.def("__len__", &SpatialCell::size);
86  cls.def("getLabel", &SpatialCell::getLabel);
87  cls.def("begin", (SpatialCellCandidateIterator (SpatialCell::*)()) & SpatialCell::begin);
88  cls.def("begin", (SpatialCellCandidateIterator (SpatialCell::*)(bool)) & SpatialCell::begin);
89  cls.def("end", (SpatialCellCandidateIterator (SpatialCell::*)()) & SpatialCell::end);
90  cls.def("end", (SpatialCellCandidateIterator (SpatialCell::*)(bool)) & SpatialCell::end);
91  cls.def("insertCandidate", &SpatialCell::insertCandidate);
92  cls.def("removeCandidate", &SpatialCell::removeCandidate);
93  cls.def("setIgnoreBad", &SpatialCell::setIgnoreBad, "ignoreBad"_a);
94  cls.def("getIgnoreBad", &SpatialCell::getIgnoreBad);
95  cls.def("getCandidateById", &SpatialCell::getCandidateById, "id"_a, "noThrow"_a = false);
96  cls.def("getLabel", &SpatialCell::getLabel);
97  cls.def("getBBox", &SpatialCell::getBBox);
98  cls.def("sortCandidates", &SpatialCell::sortCandidates);
99  cls.def("visitCandidates",
100  (void (SpatialCell::*)(CandidateVisitor *, int const, bool const, bool const)) &
101  SpatialCell::visitCandidates,
102  "visitor"_a, "nMaxPerCell"_a = -1, "ignoreExceptions"_a = false, "reset"_a = true);
103  cls.def("visitAllCandidates", (void (SpatialCell::*)(CandidateVisitor *, bool const, bool const)) &
104  SpatialCell::visitAllCandidates,
105  "visitor"_a, "ignoreExceptions"_a = false, "reset"_a = true);
106 }
107 
108 // Wrap SpatialCellSet
109 void wrapSpatialCellSet(py::module &mod) {
110  py::class_<SpatialCellSet, std::shared_ptr<SpatialCellSet>> cls(mod, "SpatialCellSet");
111 
112  cls.def(py::init<geom::Box2I const &, int, int>(), "region"_a, "xSize"_a, "ySize"_a = 0);
113 
114  cls.def("getCellList", &SpatialCellSet::getCellList);
115  cls.def("getBBox", &SpatialCellSet::getBBox);
116  cls.def("insertCandidate", &SpatialCellSet::insertCandidate);
117  cls.def("sortCandidates", &SpatialCellSet::sortCandidates);
118  cls.def("visitCandidates", (void (SpatialCellSet::*)(CandidateVisitor *, int const, bool const)) &
119  SpatialCellSet::visitCandidates,
120  "visitor"_a, "nMaxPerCell"_a = -1, "ignoreExceptions"_a = false);
121  cls.def("visitAllCandidates",
122  (void (SpatialCellSet::*)(CandidateVisitor *, bool const)) & SpatialCellSet::visitAllCandidates,
123  "visitor"_a, "ignoreExceptions"_a = false);
124  cls.def("getCandidateById", &SpatialCellSet::getCandidateById, "id"_a, "noThrow"_a = false);
125  cls.def("setIgnoreBad", &SpatialCellSet::setIgnoreBad, "ignoreBad"_a);
126 }
127 
128 // Wrap CandidateVisitor
129 void wrapCandidateVisitor(py::module &mod) {
130  py::class_<CandidateVisitor, std::shared_ptr<CandidateVisitor>> cls(mod, "CandidateVisitor");
131 
132  cls.def(py::init<>());
133 
134  cls.def("reset", &CandidateVisitor::reset);
135  cls.def("processCandidate", &CandidateVisitor::processCandidate);
136 }
137 
138 // Wrap class SpatialCellImageCandidate (an abstract class, so no constructor is wrapped)
139 void wrapSpatialCellImageCandidate(py::module &mod) {
140  py::class_<SpatialCellImageCandidate, std::shared_ptr<SpatialCellImageCandidate>, SpatialCellCandidate>
141  cls(mod, "SpatialCellImageCandidate");
142 
143  cls.def_static("setWidth", &SpatialCellImageCandidate::setWidth, "width"_a);
144  cls.def_static("getWidth", &SpatialCellImageCandidate::getWidth);
145  cls.def_static("setHeight", &SpatialCellImageCandidate::setHeight, "height"_a);
146  cls.def_static("getHeight", &SpatialCellImageCandidate::getHeight);
147  cls.def("setChi2", &SpatialCellImageCandidate::setChi2, "chi2"_a);
148  cls.def("getChi2", &SpatialCellImageCandidate::getChi2);
149 }
150 
151 } // namespace lsst::afw::math::<anonymous>
152 
153 // PYBIND11_DECLARE_HOLDER_TYPE(MyType, std::shared_ptr<MyType>);
154 
155 void wrapTestClasses(py::module &mod) {
156  /*
157  * Test class for SpatialCellCandidate
158  */
159  class TestCandidate : public SpatialCellCandidate {
160  public:
161  TestCandidate(float const xCenter,
162  float const yCenter,
163  float const flux
164  )
165  : SpatialCellCandidate(xCenter, yCenter), _flux(flux) {}
166 
168  virtual double getCandidateRating() const { return _flux; }
169  virtual void setCandidateRating(double flux) { _flux = flux; }
170 
171  private:
172  double _flux;
173  };
174 
176  class TestCandidateVisitor : public CandidateVisitor {
177  public:
178  TestCandidateVisitor() : CandidateVisitor(), _n(0) {}
179 
180  // Called by SpatialCellSet::visitCandidates before visiting any Candidates
181  void reset() { _n = 0; }
182 
183  // Called by SpatialCellSet::visitCandidates for each Candidate
184  void processCandidate(SpatialCellCandidate *candidate) { ++_n; }
185 
186  int getN() const { return _n; }
187 
188  private:
189  int _n; // number of TestCandidates
190  };
191 
192  class TestImageCandidate : public SpatialCellImageCandidate {
193  public:
194  typedef image::MaskedImage<float> MaskedImageT;
195 
196  TestImageCandidate(float const xCenter,
197  float const yCenter,
198  float const flux
199  )
200  : SpatialCellImageCandidate(xCenter, yCenter), _flux(flux) {}
201 
203  double getCandidateRating() const { return _flux; }
204 
206  std::shared_ptr<MaskedImageT const> getMaskedImage() const {
207  if (!_image) {
208  _image = std::make_shared<MaskedImageT>(geom::ExtentI(getWidth(), getHeight()));
209  *_image->getImage() = _flux;
210  }
211  return _image;
212  }
213 
214  private:
215  mutable std::shared_ptr<MaskedImageT> _image;
216  double _flux;
217  };
218 
219  py::class_<TestCandidate, std::shared_ptr<TestCandidate>, SpatialCellCandidate> clsTestCandidate(
220  mod, ("TestCandidate"));
221  clsTestCandidate.def(py::init<float const, float const, float const>());
222  clsTestCandidate.def("getCandidateRating", &TestCandidate::getCandidateRating);
223  clsTestCandidate.def("setCandidateRating", &TestCandidate::setCandidateRating);
224 
225  py::class_<TestCandidateVisitor, std::shared_ptr<TestCandidateVisitor>, CandidateVisitor>
226  clsTestCandidateVisitor(mod, ("TestCandidateVisitor"));
227  clsTestCandidateVisitor.def(py::init<>());
228  clsTestCandidateVisitor.def("getN", &TestCandidateVisitor::getN);
229 
230  py::class_<TestImageCandidate, std::shared_ptr<TestImageCandidate>, SpatialCellImageCandidate>
231  clsTestImageCandidate(mod, "TestImageCandidate");
232  clsTestImageCandidate.def(py::init<float const, float const, float const>(), "xCenter"_a, "yCenter"_a,
233  "flux"_a);
234  clsTestImageCandidate.def("getCandidateRating", &TestImageCandidate::getCandidateRating);
235  clsTestImageCandidate.def("getMaskedImage", &TestImageCandidate::getMaskedImage);
236 };
237 
238 PYBIND11_PLUGIN(_spatialCell) {
239  py::module mod("_spatialCell", "Python wrapper for afw _spatialCell library");
240 
241  wrapSpatialCellCandidate(mod);
242  wrapSpatialCellCandidateIterator(mod);
243  wrapSpatialCell(mod);
244  wrapSpatialCellSet(mod);
245  wrapCandidateVisitor(mod);
246  wrapSpatialCellImageCandidate(mod);
247 
248  /* Test Members */
249  wrapTestClasses(mod);
250 
251  return mod.ptr();
252 }
253 }
254 }
255 } // namespace lsst::afw::math
unsigned long _n
Definition: RandomImage.cc:67
PYBIND11_PLUGIN(_cameraPoint)
Definition: cameraPoint.cc:35
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:81
double const _a
Definition: RandomImage.cc:77
void wrapTestClasses(py::module &mod)
Definition: spatialCell.cc:155
Extent< int, 2 > ExtentI
Definition: Extent.h:338
Base class for candidate objects in a SpatialCell that are able to return an Image of some sort (e...
Definition: SpatialCell.h:121
Base class for candidate objects in a SpatialCell.
Definition: SpatialCell.h:70
int end