LSST Applications g0f08755f38+82efc23009,g12f32b3c4e+e7bdf1200e,g1653933729+a8ce1bb630,g1a0ca8cf93+50eff2b06f,g28da252d5a+52db39f6a5,g2bbee38e9b+37c5a29d61,g2bc492864f+37c5a29d61,g2cdde0e794+c05ff076ad,g3156d2b45e+41e33cbcdc,g347aa1857d+37c5a29d61,g35bb328faa+a8ce1bb630,g3a166c0a6a+37c5a29d61,g3e281a1b8c+fb992f5633,g414038480c+7f03dfc1b0,g41af890bb2+11b950c980,g5fbc88fb19+17cd334064,g6b1c1869cb+12dd639c9a,g781aacb6e4+a8ce1bb630,g80478fca09+72e9651da0,g82479be7b0+04c31367b4,g858d7b2824+82efc23009,g9125e01d80+a8ce1bb630,g9726552aa6+8047e3811d,ga5288a1d22+e532dc0a0b,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc28159a63d+37c5a29d61,gcf0d15dbbd+2acd6d4d48,gd7358e8bfb+778a810b6e,gda3e153d99+82efc23009,gda6a2b7d83+2acd6d4d48,gdaeeff99f8+1711a396fd,ge2409df99d+6b12de1076,ge79ae78c31+37c5a29d61,gf0baf85859+d0a5978c5a,gf3967379c6+4954f8c433,gfb92a5be7c+82efc23009,gfec2e1e490+2aaed99252,w.2024.46
LSST Data Management Base Package
Loading...
Searching...
No Matches
MaskDict.cc
Go to the documentation of this file.
1/*
2 * Developed for the LSST Data Management System.
3 * This product includes software developed by the LSST Project
4 * (https://www.lsst.org).
5 * See the COPYRIGHT file at the top-level directory of this distribution
6 * for details of code ownership.
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 GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <mutex>
23#include <set>
24
25#include "boost/functional/hash.hpp"
27
28namespace lsst {
29namespace afw {
30namespace image {
31namespace detail {
32
33// A thread-safe singleton that manages MaskDict's global state.
35public:
36 static GlobalState &get() {
37 static GlobalState instance;
38 return instance;
39 }
40
43 if (!mpd.empty()) {
45 _allMaskDicts.insert(dict);
46 return dict;
47 }
48 return _defaultMaskDict;
49 }
50
54 _allMaskDicts.insert(result);
55 return result;
56 }
57
58 std::shared_ptr<MaskDict> getDefault() const noexcept { return _defaultMaskDict; }
59
60 void setDefault(std::shared_ptr<MaskDict> dict) { _defaultMaskDict = std::move(dict); }
61
64 _defaultMaskDict = copy(*_defaultMaskDict);
65 return _defaultMaskDict;
66 }
67
68 template <typename Functor>
69 void forEachMaskDict(Functor functor) {
71 _prune(); // guarantees dereference below is safe
72 for (auto const &ptr : _allMaskDicts) {
73 functor(*ptr.lock());
74 }
75 }
76
77private:
78 GlobalState() : _defaultMaskDict(new MaskDict()) {
79 _allMaskDicts.insert(_defaultMaskDict);
80 _defaultMaskDict->_addInitialMaskPlanes();
81 }
82
83 GlobalState(GlobalState const &) = delete;
84 GlobalState(GlobalState &&) = delete;
85
86 GlobalState &operator=(GlobalState const &) = delete;
87 GlobalState &operator=(GlobalState &&) = delete;
88
89 ~GlobalState() = default;
90
91 // Prune expired weak_ptrs from _allMaskDicts. Not thread safe; should
92 // only be called by routines that have already locked the mutex.
93 void _prune() {
94 for (auto iter = _allMaskDicts.begin(); iter != _allMaskDicts.end();) {
95 if (iter->expired()) {
96 iter = _allMaskDicts.erase(iter);
97 } else {
98 ++iter;
99 }
100 }
101 }
102
103 std::recursive_mutex _mutex; // guards _allMaskDicts and synchronizes updates to it and _defaultMaskDict
105 std::shared_ptr<MaskDict> _defaultMaskDict;
106};
107
111
113
115
117
118void MaskDict::addAllMasksPlane(std::string const &name, int bitId) {
119 GlobalState::get().forEachMaskDict([&name, bitId](MaskDict &dict) {
120 auto const found = std::find_if(dict.begin(), dict.end(),
121 [bitId](auto const &item) { return item.second == bitId; });
122 if (found == dict.end()) {
123 // is name already in use?
124 if (dict.find(name) == dict.end()) {
125 dict.add(name, bitId);
126 }
127 }
128 });
129}
130
131MaskDict::~MaskDict() noexcept = default;
132
133std::shared_ptr<MaskDict> MaskDict::clone() const { return GlobalState::get().copy(*this); }
134
136 if (empty()) {
137 return 0;
138 }
139
140 auto const maxIter = std::max_element(begin(), end(),
141 [](auto const &a, auto const &b) { return a.second < b.second; });
142 assert(maxIter != end());
143 int id = maxIter->second + 1; // The maskPlane to use if there are no gaps
144
145 for (int i = 0; i < id; ++i) {
146 // is i already used in this Mask?
147 auto const sameIter =
148 std::find_if(begin(), end(), [i](auto const &item) { return item.second == i; });
149 if (sameIter == end()) { // Not used; so we'll use it
150 return i;
151 }
152 }
153
154 return id;
155}
156
157int MaskDict::getMaskPlane(std::string const &name) const {
158 auto iter = find(name);
159 return (iter == end()) ? -1 : iter->second;
160}
161
162void MaskDict::print() const {
163 for (auto const &item : *this) {
164 std::cout << "Plane " << item.second << " -> " << item.first << std::endl;
165 }
166}
167
168bool MaskDict::operator==(MaskDict const &rhs) const {
169 return this == &rhs || (_hash == rhs._hash && _dict == rhs._dict);
170}
171
172void MaskDict::add(std::string const &name, int bitId) {
173 _dict[name] = bitId;
174 _hash = boost::hash<MaskPlaneDict>()(_dict);
175}
176
177void MaskDict::erase(std::string const &name) {
178 _dict.erase(name);
179 _hash = boost::hash<MaskPlaneDict>()(_dict);
180}
181
183 _dict.clear();
184 _hash = 0x0;
185}
186
187void MaskDict::_addInitialMaskPlanes() {
188 int i = -1;
189 _dict["BAD"] = ++i;
190 _dict["SAT"] = ++i;
191 _dict["INTRP"] = ++i;
192 _dict["CR"] = ++i;
193 _dict["EDGE"] = ++i;
194 _dict["DETECTED"] = ++i;
195 _dict["DETECTED_NEGATIVE"] = ++i;
196 _dict["SUSPECT"] = ++i;
197 _dict["NO_DATA"] = ++i;
198 _dict["VIGNETTED"] = ++i;
199 _hash = boost::hash<MaskPlaneDict>()(_dict);
200}
201
202MaskDict::MaskDict() : _dict(), _hash(0x0) {}
203
204MaskDict::MaskDict(MaskPlaneDict const &dict) : _dict(dict), _hash(boost::hash<MaskPlaneDict>()(_dict)) {}
205
206} // namespace detail
207} // namespace image
208} // namespace afw
209} // namespace lsst
py::object result
Definition _schema.cc:429
table::Key< std::string > name
Definition Amplifier.cc:116
table::Key< int > id
Definition Detector.cc:162
std::uint64_t * ptr
Definition RangeSet.cc:95
table::Key< int > b
std::shared_ptr< MaskDict > getDefault() const noexcept
Definition MaskDict.cc:58
std::shared_ptr< MaskDict > copyOrGetDefault(MaskPlaneDict const &mpd)
Definition MaskDict.cc:41
std::shared_ptr< MaskDict > copy(MaskDict const &dict)
Definition MaskDict.cc:51
void setDefault(std::shared_ptr< MaskDict > dict)
Definition MaskDict.cc:60
std::shared_ptr< MaskDict > detachDefault()
Definition MaskDict.cc:62
int getMaskPlane(std::string const &name) const
Definition MaskDict.cc:157
const_iterator begin() const noexcept
Definition MaskDict.h:131
static void addAllMasksPlane(std::string const &name, int bitId)
Definition MaskDict.cc:118
static std::shared_ptr< MaskDict > detachDefault()
Definition MaskDict.cc:116
const_iterator end() const noexcept
Definition MaskDict.h:132
static std::shared_ptr< MaskDict > getDefault()
Definition MaskDict.cc:112
static void setDefault(std::shared_ptr< MaskDict > dict)
Definition MaskDict.cc:114
bool operator==(MaskDict const &rhs) const
Definition MaskDict.cc:168
const_iterator find(std::string const &name) const
Definition MaskDict.h:135
void add(std::string const &name, int bitId)
Definition MaskDict.cc:172
static std::shared_ptr< MaskDict > copyOrGetDefault(MaskPlaneDict const &dict)
Definition MaskDict.cc:108
bool empty() const noexcept
Definition MaskDict.h:141
void erase(std::string const &name)
Definition MaskDict.cc:177
T clear(T... args)
T empty(T... args)
T endl(T... args)
T erase(T... args)
T find_if(T... args)
T max_element(T... args)
T move(T... args)
std::map< std::string, int > MaskPlaneDict
Definition Mask.h:58
STL namespace.