lsst.afw  13.0-39-g3deb52d
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
maskedImage.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008-2017 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 "pybind11/pybind11.h"
24 #include "pybind11/stl.h"
25 
26 #include "lsst/afw/fits.h"
28 
29 namespace py = pybind11;
30 using namespace pybind11::literals;
31 
32 namespace lsst {
33 namespace afw {
34 namespace image {
35 
36 namespace {
37 
38 template <typename ImagePixelT> // only the image type varies; mask and variance are fixed
39 using PyMaskedImage = py::class_<MaskedImage<ImagePixelT>, std::shared_ptr<MaskedImage<ImagePixelT>>,
40  daf::base::Persistable, daf::base::Citizen>;
41 
50 template <typename FromPixelT, typename ToPixelT>
51 void declareCastConstructor(PyMaskedImage<ToPixelT> &cls) {
52  cls.def(py::init<MaskedImage<FromPixelT> const &, bool const>(), "src"_a, "deep"_a);
53 }
54 
55 template <typename ImagePixelT>
56 PyMaskedImage<ImagePixelT> declareMaskedImage(py::module &mod, const std::string &suffix) {
57  using MI = MaskedImage<ImagePixelT>;
58 
59  PyMaskedImage<ImagePixelT> cls(mod, ("MaskedImage" + suffix).c_str());
60 
61  mod.def("makeMaskedImage", &makeMaskedImage<ImagePixelT, MaskPixel, VariancePixel>, "image"_a,
62  "mask"_a = nullptr, "variance"_a = nullptr);
63 
64  /* Member types and enums */
65 
66  /* Constructors */
67  cls.def(py::init<unsigned int, unsigned int, typename MI::MaskPlaneDict const &>(), "width"_a, "height"_a,
68  "planeDict"_a = typename MI::MaskPlaneDict());
69  cls.def(py::init<geom::Extent2I, typename MI::MaskPlaneDict const &>(), "dimensions"_a,
70  "planeDict"_a = typename MI::MaskPlaneDict());
71  cls.def(py::init<typename MI::ImagePtr, typename MI::MaskPtr, typename MI::VariancePtr>(), "image"_a,
72  "mask"_a = nullptr, "variance"_a = nullptr);
73  cls.def(py::init<geom::Box2I const &, typename MI::MaskPlaneDict const &>(), "bbox"_a,
74  "planeDict"_a = typename MI::MaskPlaneDict());
75  cls.def(py::init<std::string const &, std::shared_ptr<daf::base::PropertySet>, geom::Box2I const &,
76  ImageOrigin, bool, bool, std::shared_ptr<daf::base::PropertySet>,
77  std::shared_ptr<daf::base::PropertySet>, std::shared_ptr<daf::base::PropertySet>>(),
78  "fileName"_a, "metadata"_a = nullptr, "bbox"_a = geom::Box2I(), "origin"_a = PARENT,
79  "conformMasks"_a = false, "needAllHdus"_a = false, "imageMetadata"_a = nullptr,
80  "maskMetadata"_a = nullptr, "varianceMetadata"_a = nullptr);
81  cls.def(py::init<fits::MemFileManager &, std::shared_ptr<daf::base::PropertySet>, geom::Box2I const &,
82  ImageOrigin, bool, bool, std::shared_ptr<daf::base::PropertySet>,
83  std::shared_ptr<daf::base::PropertySet>, std::shared_ptr<daf::base::PropertySet>>(),
84  "manager"_a, "metadata"_a = nullptr, "bbox"_a = geom::Box2I(), "origin"_a = PARENT,
85  "conformMasks"_a = false, "needAllHdus"_a = false, "imageMetadata"_a = nullptr,
86  "maskMetadata"_a = nullptr, "varianceMetadata"_a = nullptr);
87  cls.def(py::init<MI const &, bool>(), "rhs"_a, "deep"_a = false);
88  cls.def(py::init<MI const &, geom::Box2I const &, ImageOrigin, bool>(), "rhs"_a, "bbox"_a,
89  "origin"_a = PARENT, "deep"_a = false);
90 
91  /* Operators */
92  cls.def("swap", &MI::swap);
93  cls.def("assign", &MI::assign, "rhs"_a, "bbox"_a = geom::Box2I(), "origin"_a = PARENT,
94  py::is_operator() // py::is_operator is a workaround for code in slicing.py
95  // that expects NotImplemented to be returned on failure.
96  );
97 
98  cls.def("__ilshift__", &MI::operator<<=);
99  cls.def("__iadd__", (MI & (MI::*)(ImagePixelT const)) & MI::operator+=);
100  cls.def("__iadd__", (MI & (MI::*)(MI const &)) & MI::operator+=);
101  cls.def("__iadd__", (MI & (MI::*)(Image<ImagePixelT> const &)) & MI::operator+=);
102  cls.def("__iadd__", (MI & (MI::*)(math::Function2<double> const &)) & MI::operator+=);
103  cls.def("scaledPlus", &MI::scaledPlus);
104  cls.def("__isub__", (MI & (MI::*)(ImagePixelT const)) & MI::operator-=);
105  cls.def("__isub__", (MI & (MI::*)(MI const &)) & MI::operator-=);
106  cls.def("__isub__", (MI & (MI::*)(Image<ImagePixelT> const &)) & MI::operator-=);
107  cls.def("__isub__", (MI & (MI::*)(math::Function2<double> const &)) & MI::operator-=);
108  cls.def("scaledMinus", &MI::scaledMinus);
109  cls.def("__imul__", (MI & (MI::*)(ImagePixelT const)) & MI::operator*=);
110  cls.def("__imul__", (MI & (MI::*)(MI const &)) & MI::operator*=);
111  cls.def("__imul__", (MI & (MI::*)(Image<ImagePixelT> const &)) & MI::operator*=);
112  cls.def("scaledMultiplies", &MI::scaledMultiplies);
113  cls.def("__itruediv__", (MI & (MI::*)(ImagePixelT const)) & MI::operator/=);
114  cls.def("__itruediv__", (MI & (MI::*)(MI const &)) & MI::operator/=);
115  cls.def("__itruediv__", (MI & (MI::*)(Image<ImagePixelT> const &)) & MI::operator/=);
116  cls.def("scaledDivides", &MI::scaledDivides);
117 
118  /* Members */
119  cls.def("writeFits", (void (MI::*)(std::string const &, std::shared_ptr<daf::base::PropertySet const>,
120  std::shared_ptr<daf::base::PropertySet const>,
121  std::shared_ptr<daf::base::PropertySet const>,
122  std::shared_ptr<daf::base::PropertySet const>) const) &
123  MI::writeFits,
124  "fileName"_a, "metadata"_a = std::shared_ptr<daf::base::PropertySet const>(),
125  "imageMetadata"_a = std::shared_ptr<daf::base::PropertySet const>(),
126  "maskMetadata"_a = std::shared_ptr<daf::base::PropertySet const>(),
127  "varianceMetadata"_a = std::shared_ptr<daf::base::PropertySet const>());
128  cls.def("writeFits", (void (MI::*)(fits::MemFileManager &, std::shared_ptr<daf::base::PropertySet const>,
129  std::shared_ptr<daf::base::PropertySet const>,
130  std::shared_ptr<daf::base::PropertySet const>,
131  std::shared_ptr<daf::base::PropertySet const>) const) &
132  MI::writeFits,
133  "manager"_a, "metadata"_a = std::shared_ptr<daf::base::PropertySet const>(),
134  "imageMetadata"_a = std::shared_ptr<daf::base::PropertySet const>(),
135  "maskMetadata"_a = std::shared_ptr<daf::base::PropertySet const>(),
136  "varianceMetadata"_a = std::shared_ptr<daf::base::PropertySet const>());
137  cls.def("writeFits", (void (MI::*)(fits::Fits &, std::shared_ptr<daf::base::PropertySet const>,
138  std::shared_ptr<daf::base::PropertySet const>,
139  std::shared_ptr<daf::base::PropertySet const>,
140  std::shared_ptr<daf::base::PropertySet const>) const) &
141  MI::writeFits,
142  "fitsfile"_a, "metadata"_a = std::shared_ptr<daf::base::PropertySet const>(),
143  "imageMetadata"_a = std::shared_ptr<daf::base::PropertySet const>(),
144  "maskMetadata"_a = std::shared_ptr<daf::base::PropertySet const>(),
145  "varianceMetadata"_a = std::shared_ptr<daf::base::PropertySet const>());
146 
147  cls.def_static("readFits", (MI(*)(std::string const &))MI::readFits, "filename"_a);
148  cls.def_static("readFits", (MI(*)(fits::MemFileManager &))MI::readFits, "manager"_a);
149  cls.def("getImage", &MI::getImage, "noThrow"_a = false);
150  cls.def("getMask", &MI::getMask, "noThrow"_a = false);
151  cls.def("getVariance", &MI::getVariance, "noThrow"_a = false);
152  cls.def("getWidth", &MI::getWidth);
153  cls.def("getHeight", &MI::getHeight);
154  cls.def("getDimensions", &MI::getDimensions);
155  cls.def("getBBox", &MI::getBBox, "origin"_a = PARENT);
156  cls.def("getX0", &MI::getX0);
157  cls.def("getY0", &MI::getY0);
158  cls.def("getXY0", &MI::getXY0);
159  cls.def("setXY0", (void (MI::*)(int const, int const)) & MI::setXY0, "x0"_a, "y0"_a);
160  cls.def("setXY0", (void (MI::*)(geom::Point2I const)) & MI::setXY0, "origin"_a);
161  cls.def("indexToPosition", &MI::indexToPosition);
162  cls.def("positionToIndex", &MI::positionToIndex);
163 
164  return cls;
165 }
166 
167 template <typename ImagePixelT> // addtional template types do not seem to be needed
168 void declareMakeMaskedImage(py::module &mod) {
169  mod.def("makeMaskedImage", makeMaskedImage<ImagePixelT, MaskPixel, VariancePixel>, "image"_a,
170  "mask"_a = nullptr, "variance"_a = nullptr);
171 }
172 
173 } // anonymous
174 
175 PYBIND11_PLUGIN(maskedImage) {
176  py::module mod("maskedImage");
177 
178  py::module::import("lsst.afw.image.image");
179  py::module::import("lsst.afw.image.mask");
180 
181  auto clsMaskedImageF = declareMaskedImage<float>(mod, "F");
182  auto clsMaskedImageD = declareMaskedImage<double>(mod, "D");
183  auto clsMaskedImageI = declareMaskedImage<int>(mod, "I");
184  auto clsMaskedImageU = declareMaskedImage<std::uint16_t>(mod, "U");
185  auto clsMaskedImageL = declareMaskedImage<std::uint64_t>(mod, "L");
186 
187  // Declare constructors for casting all exposure types to to float and double
188  // (the only two types of casts that Python supports)
189  declareCastConstructor<int, float>(clsMaskedImageF);
190  declareCastConstructor<int, double>(clsMaskedImageD);
191  declareCastConstructor<float, double>(clsMaskedImageD);
192  declareCastConstructor<double, float>(clsMaskedImageF);
193  declareCastConstructor<std::uint16_t, float>(clsMaskedImageF);
194  declareCastConstructor<std::uint16_t, double>(clsMaskedImageD);
195  declareCastConstructor<std::uint64_t, float>(clsMaskedImageF);
196  declareCastConstructor<std::uint64_t, double>(clsMaskedImageD);
197 
198  /* Module level */
199  declareMakeMaskedImage<int>(mod);
200  declareMakeMaskedImage<float>(mod);
201  declareMakeMaskedImage<double>(mod);
202  declareMakeMaskedImage<std::uint16_t>(mod);
203  declareMakeMaskedImage<std::uint64_t>(mod);
204 
205  return mod.ptr();
206 }
207 }
208 }
209 } // namspace lsst::afw::image
void scaledPlus(OutImageT &outImage, double c1, InImageT const &inImage1, double c2, InImageT const &inImage2)
Compute the scaled sum of two images.
double indexToPosition(double ind)
Convert image index to image position.
Definition: ImageUtils.h:55
void swap(ImageBase< PixelT > &a, ImageBase< PixelT > &b)
Definition: Image.cc:227
int positionToIndex(double pos)
Convert image position to nearest integer index.
Definition: ImageUtils.h:69
std::map< std::string, int > MaskPlaneDict
Definition: Mask.h:64
PYBIND11_PLUGIN(_cameraPoint)
Definition: cameraPoint.cc:35
double const _a
Definition: RandomImage.cc:77
afw::table::Key< afw::table::Array< ImagePixelT > > image
Point< int, 2 > Point2I
Definition: Point.h:270