Skip to content

Instantly share code, notes, and snippets.

@kfindeisen
Forked from parejkoj/afwFilter.h
Last active September 24, 2020 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kfindeisen/1492210be6e19b86b1a529228e999a41 to your computer and use it in GitHub Desktop.
Save kfindeisen/1492210be6e19b86b1a529228e999a41 to your computer and use it in GitHub Desktop.
/**
* Pybind11 bindings for FilterLabel
*/
auto clsFilterLabel = wrappers.wrapType(
py::class_<FilterLabel, std::shared_ptr<FilterLabel>>(wrappers.module, "FilterLabel"),
[](auto & mod, auto & cls) {
cls.def(py::init([](py::object band, py::object physical) {
// Expand as we get more combinations of keywords
if (!band.is_none() && !physical.is_none()) {
return FilterLabel.fromBandPhysical(py::cast<std::string>(band), py::cast<std::string>(physical));
} else if (!band.is_none()) {
return FilterLabel.fromBand(py::cast<std::string>(band));
} else if (!physical.is_none()) {
return FilterLabel.fromPhysical(py::cast<std::string>(physical));
} else {
throw py::value_error("Need at least one of band, physical");
}
}), py::kw_only(), "band"_a=py::none, "physical"_a=py::none);
cls.def_property_readonly("bandLabel", &FilterLabel::getBandLabel);
cls.def("hasPhysicalLabel", &FilterLabel::hasPhysicalLabel);
cls.def_property_readonly("physicalLabel", &FilterLabel::getPhysicalLabel);
cls.def("__eq__", &FilterLabel::operator==, py::is_operator());
cls.def("__ne__", &FilterLabel::operator!=, py::is_operator());
}
);
/**
* A group of labels for a filter in an exposure or coadd.
*
* Details about the filter bandpass and transmission curve are stored in a TransmissionCurve object, which
* can be looked up with the label via `butler.get("transmission_filter", ...)`.
*/
class FilterLabel final : public typehandling::Storable {
// Single class because we don't know which labels will be around long-term,
// and specialized classes harder to phase out.
public:
// Use factories to be flexible w.r.t. future changes in filter system
static FilterLabel fromBandPhysical(std::string const& band, std::string const& physical);
static FilterLabel fromBand(std::string const& band);
static FilterLabel fromPhysical(std::string const& physical);
// Allow move and copy: this is a trivial object.
Filter(Filter const &) = default;
Filter(Filter &&) noexcept = default;
Filter &operator=(Filter const &) = default; // Only way to modify a FilterLabel
Filter &operator=(Filter &&) noexcept = default;
~Filter() noexcept = default;
std::string getBandLabel() const;
/// Return whether the filter label names a physical filter
bool hasPhysicalLabel() const noexcept;
/**
* Return the physical filter label.
*
* @returns The physical filter label.
* @throws pex::exceptions::RuntimeError Thrown if the label does not name a physical filter.
*/
std::string getPhysicalLabel() const;
/**
* Filter labels compare equal if their components are equal.
*
* @note This operation does not test whether two *filters* are the same.
* Two FilterLabels corresponding to identically-named filters on
* different instruments will compare equal.
*/
bool operator==(FilterLabel const &rhs) const noexcept {
return _band == rhs._band && _physical == rhs._physical;
}
bool operator!=(FilterLabel const &rhs) const noexcept {
return !(*this == rhs);
}
/// Return a hash of this object.
std::size_t hash_value() const noexcept override;
bool isPersistable() const noexcept override;
protected:
// Persistable support
std::string getPersistenceName() const override;
std::string getPythonModule() const override;
void write(OutputArchiveHandle &handle) const override;
private:
// Best implementation TBD
};
/**
* Remap special characters, etc. to "_" for database fields.
*
* @return The filter label in database-sanitized format.
*/
std::string getDatabaseLabel(std::string const& filterLabel);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment