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