Skip to content

Commit

Permalink
Implement getDatabaseFilterLabel.
Browse files Browse the repository at this point in the history
  • Loading branch information
kfindeisen committed Oct 27, 2020
1 parent f9cb88b commit 2ee1984
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
7 changes: 7 additions & 0 deletions include/lsst/afw/image/FilterLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ class FilterLabel final : public table::io::PersistableFacade<FilterLabel>, publ
static Factory factory;
};

/**
* Remap special characters, etc. to "_" for database fields.
*
* @return The filter label in database-sanitized format.
*/
std::string getDatabaseFilterLabel(std::string const &filterLabel);

} // namespace image
} // namespace afw
} // namespace lsst
Expand Down
8 changes: 6 additions & 2 deletions python/lsst/afw/image/filterLabel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ PyFilterLabel declare(py::module& mod) {
return PyFilterLabel(mod, "FilterLabel", initDoc);
}

void define(PyFilterLabel& cls) {
void define(py::module& mod, PyFilterLabel& cls) {
table::io::python::addPersistableMethods(cls);

cls.def_static("fromBandPhysical", &FilterLabel::fromBandPhysical, "band"_a, "physical"_a);
Expand Down Expand Up @@ -112,6 +112,10 @@ void define(PyFilterLabel& cls) {
// Neither __copy__ nor __deepcopy__ default to each other
cls.def("__copy__", [](const FilterLabel& obj) { return obj.cloneStorable(); });
cls.def("__deepcopy__", [](const FilterLabel& obj, py::dict& memo) { return obj.cloneStorable(); });

// Free functions

mod.def("getDatabaseFilterLabel", &getDatabaseFilterLabel, "filterLabel"_a);
}

PYBIND11_MODULE(filterLabel, mod) {
Expand All @@ -122,7 +126,7 @@ PYBIND11_MODULE(filterLabel, mod) {
// then import dependencies used in method signatures
// none
// and now we can safely define methods and other attributes
define(cls);
define(mod, cls);
}

} // namespace
Expand Down
6 changes: 6 additions & 0 deletions src/image/FilterLabel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/

#include <memory>
#include <regex>

#include "lsst/utils/hashCombine.h"
#include "lsst/pex/exceptions.h"
Expand All @@ -41,6 +42,11 @@ namespace lsst {
namespace afw {
namespace image {

std::string getDatabaseFilterLabel(std::string const &filterLabel) {
static std::regex const unsafeCharacters("\\W"s);
return std::regex_replace(filterLabel, unsafeCharacters, "_"s);
}

namespace impl {
// Hack to allow unit tests to test states that, while legal, are
// not produced by (and should not be required of) standard factories.
Expand Down
9 changes: 8 additions & 1 deletion tests/test_filterLabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import lsst.utils.tests

from lsst.afw.image import FilterLabel
from lsst.afw.image import FilterLabel, getDatabaseFilterLabel


class FilterLabelTestCase(lsst.utils.tests.TestCase):
Expand Down Expand Up @@ -140,6 +140,13 @@ def testCopy(self):
copy2 = copy.deepcopy(label)
self._checkCopy(copy2, label)

def testDatabaseLabel(self):
self.assertEqual(getDatabaseFilterLabel("k"), "k")
self.assertEqual(getDatabaseFilterLabel("k-0234"), "k_0234")
self.assertEqual(getDatabaseFilterLabel("g decam 0101"), "g_decam_0101")
self.assertEqual(getDatabaseFilterLabel("speci@l band"), "speci_l_band")
self.assertEqual(getDatabaseFilterLabel("\"hacky, (42);"), "_hacky___42__")


class TestMemory(lsst.utils.tests.MemoryTestCase):
pass
Expand Down

0 comments on commit 2ee1984

Please sign in to comment.