LSSTApplications  21.0.0+75b29a8a7f,21.0.0+e70536a077,21.0.0-1-ga51b5d4+62c747d40b,21.0.0-11-ga6ea59e8e+47cba9fc36,21.0.0-2-g103fe59+914993bf7c,21.0.0-2-g1367e85+e2614ded12,21.0.0-2-g45278ab+e70536a077,21.0.0-2-g4bc9b9f+7b2b5f8678,21.0.0-2-g5242d73+e2614ded12,21.0.0-2-g54e2caa+6403186824,21.0.0-2-g7f82c8f+3ac4acbffc,21.0.0-2-g8dde007+04a6aea1af,21.0.0-2-g8f08a60+9402881886,21.0.0-2-ga326454+3ac4acbffc,21.0.0-2-ga63a54e+81dd751046,21.0.0-2-gc738bc1+5f65c6e7a9,21.0.0-2-gde069b7+26c92b3210,21.0.0-2-gecfae73+0993ddc9bd,21.0.0-2-gfc62afb+e2614ded12,21.0.0-21-gba890a8+5a4f502a26,21.0.0-23-g9966ff26+03098d1af8,21.0.0-3-g357aad2+8ad216c477,21.0.0-3-g4be5c26+e2614ded12,21.0.0-3-g6d51c4a+4d2fe0280d,21.0.0-3-g7d9da8d+75b29a8a7f,21.0.0-3-gaa929c8+522e0f12c2,21.0.0-3-ge02ed75+4d2fe0280d,21.0.0-4-g3300ddd+e70536a077,21.0.0-4-gc004bbf+eac6615e82,21.0.0-4-gccdca77+f94adcd104,21.0.0-4-gd1c1571+18b81799f9,21.0.0-5-g7b47fff+4d2fe0280d,21.0.0-5-gb155db7+d2632f662b,21.0.0-5-gdf36809+637e4641ee,21.0.0-6-g722ad07+28c848f42a,21.0.0-7-g959bb79+522e0f12c2,21.0.0-7-gfd72ab2+cf01990774,21.0.0-9-g87fb7b8d+e2ab11cdd6,w.2021.04
LSSTDataManagementBasePackage
filterLabel.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 
26 #include <exception>
27 
28 #include "lsst/utils/python.h"
29 
33 
34 using namespace std::string_literals;
35 
36 namespace py = pybind11;
37 using namespace pybind11::literals;
38 
39 namespace lsst {
40 namespace afw {
41 namespace image {
42 namespace {
43 
44 using PyFilterLabel = py::class_<FilterLabel, std::shared_ptr<FilterLabel>, typehandling::Storable>;
45 
46 // Macro to convert an exception without defining a global handler for it
47 #define _DELEGATE_EXCEPTION(call, cpp_ex, py_ex) \
48  try { \
49  return call; \
50  } catch (cpp_ex const& e) { \
51  std::throw_with_nested(py_ex(e.what())); \
52  }
53 
54 PyFilterLabel declare(py::module& mod) {
55  // Include Python-only constructor in class doc, since it's what people should use
56  auto initDoc = R"delim(
57  Attributes
58  ----------
59  band : str, optional
60  The band associated with this label.
61  physical : str, optional
62  The physical filter associated with this label.
63  )delim";
64  return PyFilterLabel(mod, "FilterLabel", initDoc);
65 }
66 
67 void define(py::module& mod, PyFilterLabel& cls) {
69 
70  cls.def_static("fromBandPhysical", &FilterLabel::fromBandPhysical, "band"_a, "physical"_a);
71  cls.def_static("fromBand", &FilterLabel::fromBand, "band"_a);
72  cls.def_static("fromPhysical", &FilterLabel::fromPhysical, "physical"_a);
73 
74  // Keyword constructor
75  /* This is messy in C++, but it's hard to write a Python __init__ that delegates to a factory,
76  * and the pybind11 docs imply that this way is less prone to multiple-definition errors.
77  * In C++17, we should be able to replace py::object with std::optional<string>.
78  */
79  cls.def(py::init([](py::object band, py::object physical) {
80  try {
81  // Expand as we get more combinations of keywords
82  if (!band.is_none() && !physical.is_none()) {
83  return FilterLabel::fromBandPhysical(py::cast<std::string>(band),
84  py::cast<std::string>(physical));
85  } else if (!band.is_none()) {
86  return FilterLabel::fromBand(py::cast<std::string>(band));
87  } else if (!physical.is_none()) {
88  return FilterLabel::fromPhysical(py::cast<std::string>(physical));
89  } else {
90  throw py::value_error("Need at least one of band, physical");
91  }
92  } catch (py::cast_error const& e) {
93  // By default cast_error is wrapped as RuntimeError
94  std::throw_with_nested(py::type_error(e.what()));
95  }
96  }),
97  // TODO: use py::kw_only() in pybind11 2.6 or later (DM-27247)
98  "band"_a = py::none(), "physical"_a = py::none());
99 
100  cls.def("hasBandLabel", &FilterLabel::hasBandLabel);
101  cls.def_property_readonly("bandLabel", [](FilterLabel const& label) {
102  _DELEGATE_EXCEPTION(label.getBandLabel(), pex::exceptions::LogicError, std::runtime_error);
103  });
104  cls.def("hasPhysicalLabel", &FilterLabel::hasPhysicalLabel);
105  cls.def_property_readonly("physicalLabel", [](FilterLabel const& label) {
106  _DELEGATE_EXCEPTION(label.getPhysicalLabel(), pex::exceptions::LogicError, std::runtime_error);
107  });
108  cls.def("__eq__", &FilterLabel::operator==, py::is_operator());
109  cls.def("__ne__", &FilterLabel::operator!=, py::is_operator());
110 
111  cls.def("__repr__", &FilterLabel::toString);
112  // Neither __copy__ nor __deepcopy__ default to each other
113  cls.def("__copy__", [](const FilterLabel& obj) { return obj.cloneStorable(); });
114  cls.def("__deepcopy__", [](const FilterLabel& obj, py::dict& memo) { return obj.cloneStorable(); });
115 
116  // Free functions
117 
118  mod.def("getDatabaseFilterLabel", &getDatabaseFilterLabel, "filterLabel"_a);
119 }
120 
121 PYBIND11_MODULE(filterLabel, mod) {
122  // import inheritance dependencies
123  py::module::import("lsst.afw.typehandling");
124  // then declare classes
125  auto cls = declare(mod);
126  // then import dependencies used in method signatures
127  // none
128  // and now we can safely define methods and other attributes
129  define(mod, cls);
130 }
131 
132 } // namespace
133 } // namespace image
134 } // namespace afw
135 } // namespace lsst
lsst::afw::image
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Definition: imageAlgorithm.dox:1
lsst::afw::table::io::python::addPersistableMethods
void addPersistableMethods(pybind11::class_< Class, Args... > &cls)
Add table::io::Persistable and PersistableFacade methods to the pybind11 wrapper for a class.
Definition: python.h:55
lsst::afw
Definition: imageAlgorithm.dox:1
_DELEGATE_EXCEPTION
#define _DELEGATE_EXCEPTION(call, cpp_ex, py_ex)
Definition: filterLabel.cc:47
lsst::afw::geom.transform.transformContinued.cls
cls
Definition: transformContinued.py:33
lsst::afw::image::getDatabaseFilterLabel
std::string getDatabaseFilterLabel(std::string const &filterLabel)
Remap special characters, etc.
Definition: FilterLabel.cc:45
physical
OptionalString physical
Definition: FilterLabel.cc:214
std::throw_with_nested
T throw_with_nested(T... args)
std::runtime_error
STL class.
Storable.h
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
python.h
band
OptionalString band
Definition: FilterLabel.cc:213
pybind11
Definition: _GenericMap.cc:40
lsst.pex::exceptions.wrappers.declare
def declare(module, exception_name, base, wrapped_class)
Definition: wrappers.py:153
lsst::utils.tests.init
def init()
Definition: tests.py:59
lsst::meas::modelfit.psf.psfContinued.module
module
Definition: psfContinued.py:42
python.h
lsst::afw::cameraGeom::PYBIND11_MODULE
PYBIND11_MODULE(camera, mod)
Definition: camera.cc:133
FilterLabel.h