LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
LSST Data Management Base Package
_maskedImage.cc
Go to the documentation of this file.
1 /*
2  * This file is part of afw.
3  *
4  * Developed for the LSST Data Management System.
5  * This product includes software developed by the LSST Project
6  * (https://www.lsst.org).
7  * See the COPYRIGHT file at the top-level directory of this distribution
8  * for details of code ownership.
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <https://www.gnu.org/licenses/>.
22  */
23 
24 #include "pybind11/pybind11.h"
25 #include "pybind11/stl.h"
26 #include "lsst/utils/python.h"
27 
28 #include "lsst/afw/fits.h"
30 
31 namespace py = pybind11;
32 using namespace pybind11::literals;
33 
34 namespace lsst {
35 namespace afw {
36 namespace image {
37 
38 namespace {
39 
40 template <typename ImagePixelT> // only the image type varies; mask and variance are fixed
41 using PyMaskedImage = py::class_<MaskedImage<ImagePixelT>, std::shared_ptr<MaskedImage<ImagePixelT>>>;
42 
51 template <typename FromPixelT, typename ToPixelT>
52 void declareCastConstructor(PyMaskedImage<ToPixelT> &cls) {
53  cls.def(py::init<MaskedImage<FromPixelT> const &, bool const>(), "src"_a, "deep"_a);
54 }
55 
56 template <typename ImagePixelT>
57 PyMaskedImage<ImagePixelT> declareMaskedImage(lsst::utils::python::WrapperCollection &wrappers,
58  const std::string &suffix) {
59  using MI = MaskedImage<ImagePixelT>;
60 
61  return wrappers.wrapType(
62  PyMaskedImage<ImagePixelT>(wrappers.module, ("MaskedImage" + suffix).c_str()),
63  [](auto &mod, auto &cls) {
64  mod.def("makeMaskedImage", &makeMaskedImage<ImagePixelT, MaskPixel, VariancePixel>, "image"_a,
65  "mask"_a = nullptr, "variance"_a = nullptr);
66 
67  /* Member types and enums */
68 
69  /* Constructors */
70  cls.def(py::init<unsigned int, unsigned int, typename MI::MaskPlaneDict const &>(), "width"_a,
71  "height"_a, "planeDict"_a = typename MI::MaskPlaneDict());
72  cls.def(py::init<lsst::geom::Extent2I, typename MI::MaskPlaneDict const &>(), "dimensions"_a,
73  "planeDict"_a = typename MI::MaskPlaneDict());
74  cls.def(py::init<typename MI::ImagePtr, typename MI::MaskPtr, typename MI::VariancePtr>(),
75  "image"_a, "mask"_a = nullptr, "variance"_a = nullptr);
76  cls.def(py::init<lsst::geom::Box2I const &, typename MI::MaskPlaneDict const &>(), "bbox"_a,
77  "planeDict"_a = typename MI::MaskPlaneDict());
78  cls.def(py::init<std::string const &, std::shared_ptr<daf::base::PropertySet>,
79  lsst::geom::Box2I const &, ImageOrigin, bool, bool,
80  std::shared_ptr<daf::base::PropertySet>,
81  std::shared_ptr<daf::base::PropertySet>,
82  std::shared_ptr<daf::base::PropertySet>, bool>(),
83  "fileName"_a, "metadata"_a = nullptr, "bbox"_a = lsst::geom::Box2I(),
84  "origin"_a = PARENT, "conformMasks"_a = false, "needAllHdus"_a = false,
85  "imageMetadata"_a = nullptr, "maskMetadata"_a = nullptr,
86  "varianceMetadata"_a = nullptr, "allowUnsafe"_a = false);
87  cls.def(py::init<fits::MemFileManager &, std::shared_ptr<daf::base::PropertySet>,
88  lsst::geom::Box2I const &, ImageOrigin, bool, bool,
89  std::shared_ptr<daf::base::PropertySet>,
90  std::shared_ptr<daf::base::PropertySet>,
91  std::shared_ptr<daf::base::PropertySet>, bool>(),
92  "manager"_a, "metadata"_a = nullptr, "bbox"_a = lsst::geom::Box2I(),
93  "origin"_a = PARENT, "conformMasks"_a = false, "needAllHdus"_a = false,
94  "imageMetadata"_a = nullptr, "maskMetadata"_a = nullptr,
95  "varianceMetadata"_a = nullptr, "allowUnsafe"_a = false);
96  cls.def(py::init<MI const &, bool>(), "rhs"_a, "deep"_a = false);
97  cls.def(py::init<MI const &, lsst::geom::Box2I const &, ImageOrigin, bool>(), "rhs"_a,
98  "bbox"_a, "origin"_a = PARENT, "deep"_a = false);
99 
100  /* Operators */
101  cls.def("swap", &MI::swap);
102  cls.def("assign", &MI::assign, "rhs"_a, "bbox"_a = lsst::geom::Box2I(), "origin"_a = PARENT,
103  py::is_operator() // py::is_operator is a workaround for code in slicing.py
104  // that expects NotImplemented to be returned on failure.
105  );
106 
107  cls.def("subset", &MI::subset, "bbox"_a, "origin"_a = PARENT);
108 
109  cls.def("__iadd__", (MI & (MI::*)(ImagePixelT const)) & MI::operator+=);
110  cls.def("__iadd__", (MI & (MI::*)(MI const &)) & MI::operator+=);
111  cls.def("__iadd__", (MI & (MI::*)(Image<ImagePixelT> const &)) & MI::operator+=);
112  cls.def("__iadd__", (MI & (MI::*)(math::Function2<double> const &)) & MI::operator+=);
113  cls.def("scaledPlus", &MI::scaledPlus);
114  cls.def("__isub__", (MI & (MI::*)(ImagePixelT const)) & MI::operator-=);
115  cls.def("__isub__", (MI & (MI::*)(MI const &)) & MI::operator-=);
116  cls.def("__isub__", (MI & (MI::*)(Image<ImagePixelT> const &)) & MI::operator-=);
117  cls.def("__isub__", (MI & (MI::*)(math::Function2<double> const &)) & MI::operator-=);
118  cls.def("scaledMinus", &MI::scaledMinus);
119  cls.def("__imul__", (MI & (MI::*)(ImagePixelT const)) & MI::operator*=);
120  cls.def("__imul__", (MI & (MI::*)(MI const &)) & MI::operator*=);
121  cls.def("__imul__", (MI & (MI::*)(Image<ImagePixelT> const &)) & MI::operator*=);
122  cls.def("scaledMultiplies", &MI::scaledMultiplies);
123  cls.def("__itruediv__", (MI & (MI::*)(ImagePixelT const)) & MI::operator/=);
124  cls.def("__itruediv__", (MI & (MI::*)(MI const &)) & MI::operator/=);
125  cls.def("__itruediv__", (MI & (MI::*)(Image<ImagePixelT> const &)) & MI::operator/=);
126  cls.def("scaledDivides", &MI::scaledDivides);
127 
128  /* Members */
129  cls.def("writeFits",
130  (void (MI::*)(std::string const &, std::shared_ptr<daf::base::PropertySet const>,
131  std::shared_ptr<daf::base::PropertySet const>,
132  std::shared_ptr<daf::base::PropertySet const>,
133  std::shared_ptr<daf::base::PropertySet const>) const) &
134  MI::writeFits,
135  "fileName"_a, "metadata"_a = std::shared_ptr<daf::base::PropertySet const>(),
136  "imageMetadata"_a = std::shared_ptr<daf::base::PropertySet const>(),
137  "maskMetadata"_a = std::shared_ptr<daf::base::PropertySet const>(),
138  "varianceMetadata"_a = std::shared_ptr<daf::base::PropertySet const>());
139  cls.def("writeFits",
140  (void (MI::*)(fits::MemFileManager &, std::shared_ptr<daf::base::PropertySet const>,
141  std::shared_ptr<daf::base::PropertySet const>,
142  std::shared_ptr<daf::base::PropertySet const>,
143  std::shared_ptr<daf::base::PropertySet const>) const) &
144  MI::writeFits,
145  "manager"_a, "metadata"_a = std::shared_ptr<daf::base::PropertySet const>(),
146  "imageMetadata"_a = std::shared_ptr<daf::base::PropertySet const>(),
147  "maskMetadata"_a = std::shared_ptr<daf::base::PropertySet const>(),
148  "varianceMetadata"_a = std::shared_ptr<daf::base::PropertySet const>());
149  cls.def("writeFits",
150  (void (MI::*)(fits::Fits &, std::shared_ptr<daf::base::PropertySet const>,
151  std::shared_ptr<daf::base::PropertySet const>,
152  std::shared_ptr<daf::base::PropertySet const>,
153  std::shared_ptr<daf::base::PropertySet const>) const) &
154  MI::writeFits,
155  "fitsfile"_a, "metadata"_a = std::shared_ptr<daf::base::PropertySet const>(),
156  "imageMetadata"_a = std::shared_ptr<daf::base::PropertySet const>(),
157  "maskMetadata"_a = std::shared_ptr<daf::base::PropertySet const>(),
158  "varianceMetadata"_a = std::shared_ptr<daf::base::PropertySet const>());
159 
160  cls.def(
161  "writeFits",
162  [](MI &self, std::string const &filename, fits::ImageWriteOptions const &imageOptions,
163  fits::ImageWriteOptions const &maskOptions,
164  fits::ImageWriteOptions const &varianceOptions,
165  std::shared_ptr<daf::base::PropertySet const> header) {
166  self.writeFits(filename, imageOptions, maskOptions, varianceOptions, header);
167  },
168  "filename"_a, "imageOptions"_a, "maskOptions"_a, "varianceOptions"_a,
170  cls.def(
171  "writeFits",
172  [](MI &self, fits::MemFileManager &manager,
173  fits::ImageWriteOptions const &imageOptions,
174  fits::ImageWriteOptions const &maskOptions,
175  fits::ImageWriteOptions const &varianceOptions,
177  self.writeFits(manager, imageOptions, maskOptions, varianceOptions, header);
178  },
179  "manager"_a, "imageOptions"_a, "maskOptions"_a, "varianceOptions"_a,
181  cls.def(
182  "writeFits",
183  [](MI &self, fits::Fits &fits, fits::ImageWriteOptions const &imageOptions,
184  fits::ImageWriteOptions const &maskOptions,
185  fits::ImageWriteOptions const &varianceOptions,
187  self.writeFits(fits, imageOptions, maskOptions, varianceOptions, header);
188  },
189  "fits"_a, "imageOptions"_a, "maskOptions"_a, "varianceOptions"_a,
191 
192  cls.def_static("readFits", (MI(*)(std::string const &))MI::readFits, "filename"_a);
193  cls.def_static("readFits", (MI(*)(fits::MemFileManager &))MI::readFits, "manager"_a);
194  cls.def("getImage", &MI::getImage);
195  cls.def("setImage", &MI::setImage);
196  cls.def_property("image", &MI::getImage, &MI::setImage);
197  cls.def("getMask", &MI::getMask);
198  cls.def("setMask", &MI::setMask);
199  cls.def_property("mask", &MI::getMask, &MI::setMask);
200  cls.def("getVariance", &MI::getVariance);
201  cls.def("setVariance", &MI::setVariance);
202  cls.def_property("variance", &MI::getVariance, &MI::setVariance);
203  cls.def("getWidth", &MI::getWidth);
204  cls.def("getHeight", &MI::getHeight);
205  cls.def("getDimensions", &MI::getDimensions);
206  cls.def("getBBox", &MI::getBBox, "origin"_a = PARENT);
207  cls.def("getX0", &MI::getX0);
208  cls.def("getY0", &MI::getY0);
209  cls.def("getXY0", &MI::getXY0);
210  cls.def("setXY0", (void (MI::*)(int const, int const)) & MI::setXY0, "x0"_a, "y0"_a);
211  cls.def("setXY0", (void (MI::*)(lsst::geom::Point2I const)) & MI::setXY0, "origin"_a);
212  cls.def("indexToPosition", &MI::indexToPosition);
213  cls.def("positionToIndex", &MI::positionToIndex);
214  });
215 }
216 
217 template <typename ImagePixelT> // addtional template types do not seem to be needed
218 void declareMakeMaskedImage(lsst::utils::python::WrapperCollection &wrappers) {
219  wrappers.wrap([](auto &mod) {
220  mod.def("makeMaskedImage", makeMaskedImage<ImagePixelT, MaskPixel, VariancePixel>, "image"_a,
221  "mask"_a = nullptr, "variance"_a = nullptr);
222  });
223 }
224 
225 template <typename ImagePixelT1, typename ImagePixelT2>
226 void declareImagesOverlap(lsst::utils::python::WrapperCollection &wrappers) {
227  // wrap both the Image and MaskedImage versions of imagesOverlap here, as wrapping
228  // the Image version in the Image wrapper results in it being invisible in lsst.afw.image
229  wrappers.wrap([](auto &mod) {
230  mod.def("imagesOverlap",
231  py::overload_cast<ImageBase<ImagePixelT1> const &, ImageBase<ImagePixelT2> const &>(
232  &imagesOverlap<ImagePixelT1, ImagePixelT2>),
233  "image1"_a, "image2"_a);
234 
235  mod.def("imagesOverlap",
236  py::overload_cast<MaskedImage<ImagePixelT1> const &, MaskedImage<ImagePixelT2> const &>(
237  &imagesOverlap<ImagePixelT1, ImagePixelT2>),
238  "image1"_a, "image2"_a);
239  });
240 }
241 
242 } // namespace
243 
244 PYBIND11_MODULE(_maskedImage, mod) {
245  lsst::utils::python::WrapperCollection wrappers(mod, "lsst.afw.image.maskedImage");
246  wrappers.addSignatureDependency("lsst.afw.image.image");
247  wrappers.addInheritanceDependency("lsst.daf.base");
248 
249  auto clsMaskedImageF = declareMaskedImage<float>(wrappers, "F");
250  auto clsMaskedImageD = declareMaskedImage<double>(wrappers, "D");
251  auto clsMaskedImageI = declareMaskedImage<int>(wrappers, "I");
252  auto clsMaskedImageU = declareMaskedImage<std::uint16_t>(wrappers, "U");
253  auto clsMaskedImageL = declareMaskedImage<std::uint64_t>(wrappers, "L");
254 
255  // Declare constructors for casting all exposure types to to float and double
256  // (the only two types of casts that Python supports)
257  declareCastConstructor<int, float>(clsMaskedImageF);
258  declareCastConstructor<int, double>(clsMaskedImageD);
259  declareCastConstructor<float, double>(clsMaskedImageD);
260  declareCastConstructor<double, float>(clsMaskedImageF);
261  declareCastConstructor<std::uint16_t, float>(clsMaskedImageF);
262  declareCastConstructor<std::uint16_t, double>(clsMaskedImageD);
263  declareCastConstructor<std::uint64_t, float>(clsMaskedImageF);
264  declareCastConstructor<std::uint64_t, double>(clsMaskedImageD);
265 
266  /* Module level */
267  declareMakeMaskedImage<int>(wrappers);
268  declareMakeMaskedImage<float>(wrappers);
269  declareMakeMaskedImage<double>(wrappers);
270  declareMakeMaskedImage<std::uint16_t>(wrappers);
271  declareMakeMaskedImage<std::uint64_t>(wrappers);
272 
273  declareImagesOverlap<int, int>(wrappers);
274  declareImagesOverlap<int, float>(wrappers);
275  declareImagesOverlap<int, double>(wrappers);
276  declareImagesOverlap<int, std::uint16_t>(wrappers);
277  declareImagesOverlap<int, std::uint64_t>(wrappers);
278 
279  declareImagesOverlap<float, int>(wrappers);
280  declareImagesOverlap<float, float>(wrappers);
281  declareImagesOverlap<float, double>(wrappers);
282  declareImagesOverlap<float, std::uint16_t>(wrappers);
283  declareImagesOverlap<float, std::uint64_t>(wrappers);
284 
285  declareImagesOverlap<double, int>(wrappers);
286  declareImagesOverlap<double, float>(wrappers);
287  declareImagesOverlap<double, double>(wrappers);
288  declareImagesOverlap<double, std::uint16_t>(wrappers);
289  declareImagesOverlap<double, std::uint64_t>(wrappers);
290 
291  declareImagesOverlap<std::uint16_t, int>(wrappers);
292  declareImagesOverlap<std::uint16_t, float>(wrappers);
293  declareImagesOverlap<std::uint16_t, double>(wrappers);
294  declareImagesOverlap<std::uint16_t, std::uint16_t>(wrappers);
295  declareImagesOverlap<std::uint16_t, std::uint64_t>(wrappers);
296 
297  declareImagesOverlap<std::uint64_t, int>(wrappers);
298  declareImagesOverlap<std::uint64_t, float>(wrappers);
299  declareImagesOverlap<std::uint64_t, double>(wrappers);
300  declareImagesOverlap<std::uint64_t, std::uint16_t>(wrappers);
301  declareImagesOverlap<std::uint64_t, std::uint64_t>(wrappers);
302  wrappers.finish();
303 }
304 } // namespace image
305 } // namespace afw
306 } // namespace lsst
Fits * fits
Definition: FitsWriter.cc:90
A helper class for subdividing pybind11 module across multiple translation units (i....
Definition: python.h:242
pybind11::module module
The module object passed to the PYBIND11_MODULE block that contains this WrapperCollection.
Definition: python.h:448
void finish()
Invoke all deferred wrapper-declaring callables.
Definition: python.h:435
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
void addInheritanceDependency(std::string const &name)
Indicate an external module that provides a base class for a subsequent addType call.
Definition: python.h:343
void wrap(WrapperCallback function)
Add a set of wrappers without defining a class.
Definition: python.h:369
void addSignatureDependency(std::string const &name)
Indicate an external module that provides a type used in function/method signatures.
Definition: python.h:357
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
double indexToPosition(double ind)
Convert image index to image position.
Definition: ImageUtils.h:55
int positionToIndex(double pos)
Convert image position to nearest integer index.
Definition: ImageUtils.h:69
PYBIND11_MODULE(_maskedImage, mod)
def init()
Definition: tests.py:59
A base class for image defects.