LSSTApplications  15.0+21,16.0+1,16.0+3,16.0+4,16.0+8,16.0-1-g2115a9e+2,16.0-1-g4515a79+6,16.0-1-g5c6f5ee+4,16.0-1-g7bb14cc,16.0-1-g80120d7+4,16.0-1-g98efed3+4,16.0-1-gb7f560d+1,16.0-14-gb4f0cd2fa,16.0-2-g1ad129e+1,16.0-2-g2ed7261+1,16.0-2-g311bfd2,16.0-2-g568a347+3,16.0-2-g852da13+6,16.0-2-gd4c87cb+3,16.0-3-g099ede0,16.0-3-g150e024+3,16.0-3-g1f513a6,16.0-3-g958ce35,16.0-4-g08dccf71+4,16.0-4-g128aaef,16.0-4-g84f75fb+5,16.0-4-gcfd1396+4,16.0-4-gde8cee2,16.0-4-gdfb0d14+1,16.0-5-g7bc0afb+3,16.0-5-g86fb31a+3,16.0-6-g2dd73041+4,16.0-7-g95fb7bf,16.0-7-gc37dbc2+4,w.2018.28
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 
240  py::module mod("_spatialCell", "Python wrapper for afw _spatialCell library");
241 
242  wrapSpatialCellCandidate(mod);
243  wrapSpatialCellCandidateIterator(mod);
244  wrapSpatialCell(mod);
245  wrapSpatialCellSet(mod);
246  wrapCandidateVisitor(mod);
247  wrapSpatialCellImageCandidate(mod);
248 
249  /* Test Members */
250  wrapTestClasses(mod);
251 
252  return mod.ptr();
253 }
254 }
255 }
256 } // namespace lsst::afw::math
A base class for image defects.
Definition: cameraGeom.dox:3
PYBIND11_PLUGIN(_spatialCell)
Definition: spatialCell.cc:239
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:81
void wrapTestClasses(py::module &mod)
Definition: spatialCell.cc:156
STL class.
Base class for candidate objects in a SpatialCell that are able to return an Image of some sort (e...
Definition: SpatialCell.h:126
Extent< int, 2 > ExtentI
Definition: Extent.h:390
Base class for candidate objects in a SpatialCell.
Definition: SpatialCell.h:70
An integer coordinate rectangle.
Definition: Box.h:54
int end
Definition: BoundedField.cc:99