LSSTApplications  16.0-10-g0ee56ad+5,16.0-11-ga33d1f2+5,16.0-12-g3ef5c14+3,16.0-12-g71e5ef5+18,16.0-12-gbdf3636+3,16.0-13-g118c103+3,16.0-13-g8f68b0a+3,16.0-15-gbf5c1cb+4,16.0-16-gfd17674+3,16.0-17-g7c01f5c+3,16.0-18-g0a50484+1,16.0-20-ga20f992+8,16.0-21-g0e05fd4+6,16.0-21-g15e2d33+4,16.0-22-g62d8060+4,16.0-22-g847a80f+4,16.0-25-gf00d9b8+1,16.0-28-g3990c221+4,16.0-3-gf928089+3,16.0-32-g88a4f23+5,16.0-34-gd7987ad+3,16.0-37-gc7333cb+2,16.0-4-g10fc685+2,16.0-4-g18f3627+26,16.0-4-g5f3a788+26,16.0-5-gaf5c3d7+4,16.0-5-gcc1f4bb+1,16.0-6-g3b92700+4,16.0-6-g4412fcd+3,16.0-6-g7235603+4,16.0-69-g2562ce1b+2,16.0-8-g14ebd58+4,16.0-8-g2df868b+1,16.0-8-g4cec79c+6,16.0-8-gadf6c7a+1,16.0-8-gfc7ad86,16.0-82-g59ec2a54a+1,16.0-9-g5400cdc+2,16.0-9-ge6233d7+5,master-g2880f2d8cf+3,v17.0.rc1
LSSTDataManagementBasePackage
FootprintMerge.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008-2014 LSST Corporation.
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 <http://www.lsstcorp.org/LegalNotices/>.
21  */
22 #include <cstdint>
23 
24 #include "boost/bind.hpp"
25 
29 
30 namespace lsst {
31 namespace afw {
32 namespace detection {
33 
35 public:
36  typedef FootprintMergeList::KeyTuple KeyTuple;
38 
42  afw::table::SchemaMapper const &peakSchemaMapper, KeyTuple const &keys)
43  : _footprints(1, footprint), _source(sourceTable->makeRecord()) {
44  std::shared_ptr<Footprint> newFootprint = std::make_shared<Footprint>(*footprint);
45 
46  _source->set(keys.footprint, true);
47  // Replace all the Peaks in the merged Footprint with new ones that include the origin flags
48  newFootprint->getPeaks() = PeakCatalog(peakTable);
49  for (PeakCatalog::iterator iter = footprint->getPeaks().begin(); iter != footprint->getPeaks().end();
50  ++iter) {
51  std::shared_ptr<PeakRecord> newPeak = peakTable->copyRecord(*iter, peakSchemaMapper);
52  newPeak->set(keys.peak, true);
53  newFootprint->getPeaks().push_back(newPeak);
54  }
55  _source->setFootprint(newFootprint);
56  }
57 
58  ~FootprintMerge() = default;
59  FootprintMerge(FootprintMerge const &) = default;
60  FootprintMerge(FootprintMerge &&) = default;
61  FootprintMerge &operator=(FootprintMerge const &) = default;
63 
64  /*
65  * Does this Footprint overlap the merged Footprint.
66  *
67  * The current implementation just builds an image from the two Footprints and
68  * detects the number of peaks. This is not very efficient and will be changed
69  * within the Footprint class in the future.
70  */
71  bool overlaps(Footprint const &rhs) const {
72  return getMergedFootprint()->getSpans()->overlaps(*(rhs.getSpans()));
73  }
74 
75  /*
76  * Add this Footprint to the merge.
77  *
78  * If minNewPeakDist >= 0, it will add all peaks from the footprint to the merged Footprint
79  * that are greater than minNewPeakDist away from the closest peak in the existing list.
80  * If minNewPeakDist < 0, no peaks will be added from the footprint.
81  *
82  * If maxSamePeakDist >= 0, it will find the closest peak in the existing list for every peak
83  * in the footprint. If the closest peak is less than maxSamePeakDist, the peak will not
84  * be added and the closest peak will be flagged as detected by the filter defined in keys.
85  *
86  * If the footprint does not overlap it will do nothing.
87  */
89  KeyTuple const &keys, float minNewPeakDist = -1., float maxSamePeakDist = -1.) {
90  if (_addSpans(footprint)) {
91  _footprints.push_back(footprint);
92  _source->set(keys.footprint, true);
93  _addPeaks(footprint->getPeaks(), &peakSchemaMapper, &keys, minNewPeakDist, maxSamePeakDist, NULL);
94  }
95  }
96 
97  /*
98  * Merge an already-merged clump of Footprints into this
99  *
100  * If minNewPeakDist >= 0, it will add all peaks from the footprint to the merged Footprint
101  * that are greater than minNewPeakDist away from the closest peak in the existing list.
102  * If minNewPeakDist < 0, no peaks will be added from the footprint.
103  *
104  * If maxSamePeakDist >= 0, it will find the closest peak in the existing list for every peak
105  * in the footprint. If the closest peak is less than maxSamePeakDist, the peak will not
106  * be added to the list and the flags from the closest peak will be set to the OR of the two.
107  *
108  * If the FootprintMerge does not overlap it will do nothing.
109  */
110  void add(FootprintMerge const &other, FilterMap const &keys, float minNewPeakDist = -1.,
111  float maxSamePeakDist = -1.) {
112  if (_addSpans(other.getMergedFootprint())) {
113  _footprints.insert(_footprints.end(), other._footprints.begin(), other._footprints.end());
114  // Set source flags to the OR of the flags of the two inputs
115  for (FilterMap::const_iterator i = keys.begin(); i != keys.end(); ++i) {
116  afw::table::Key<afw::table::Flag> const &flagKey = i->second.footprint;
117  _source->set(flagKey, _source->get(flagKey) || other._source->get(flagKey));
118  }
119  _addPeaks(other.getMergedFootprint()->getPeaks(), NULL, NULL, minNewPeakDist, maxSamePeakDist,
120  &keys);
121  }
122  }
123 
124  // Get the bounding box of the merge
125  lsst::geom::Box2I getBBox() const { return getMergedFootprint()->getBBox(); }
126 
127  std::shared_ptr<Footprint> getMergedFootprint() const { return _source->getFootprint(); }
128 
130 
131 private:
132  // Implementation helper for add() methods; returns true if the Footprint actually overlapped
133  // and was merged, and false otherwise.
134  bool _addSpans(std::shared_ptr<Footprint> footprint) {
135  if (!getMergedFootprint()->getSpans()->overlaps(*(footprint->getSpans()))) return false;
136  getMergedFootprint()->setSpans(getMergedFootprint()->getSpans()->union_(*(footprint->getSpans())));
137  return true;
138  }
139 
140  /*
141  * Add new peaks to the list of peaks of the merged footprint.
142  * This function handles two different cases:
143  * - The peaks come from a single footprint. In this case, the peakSchemaMapper
144  * and keys should be defined so that it can create a new peak, copy the appropriate
145  * data, and set the peak flag defined in keys.
146  * - The peaks come from another FootprintMerge. In this case, filterMap should
147  * be defined so that the information from the other peaks can be propagated.
148  */
149  void _addPeaks(PeakCatalog const &otherPeaks, afw::table::SchemaMapper const *peakSchemaMapper,
150  KeyTuple const *keys, float minNewPeakDist, float maxSamePeakDist,
151  FilterMap const *filterMap) {
152  if (minNewPeakDist < 0 && maxSamePeakDist < 0) return;
153 
154  assert(peakSchemaMapper || filterMap);
155 
156  PeakCatalog &currentPeaks = getMergedFootprint()->getPeaks();
157  std::shared_ptr<PeakRecord> nearestPeak;
158  // Create new list of peaks
159  PeakCatalog newPeaks(currentPeaks.getTable());
160  float minNewPeakDist2 = minNewPeakDist * minNewPeakDist;
161  float maxSamePeakDist2 = maxSamePeakDist * maxSamePeakDist;
162  for (PeakCatalog::const_iterator otherIter = otherPeaks.begin(); otherIter != otherPeaks.end();
163  ++otherIter) {
164  float minDist2 = std::numeric_limits<float>::infinity();
165 
166  for (PeakCatalog::const_iterator currentIter = currentPeaks.begin();
167  currentIter != currentPeaks.end(); ++currentIter) {
168  float dist2 = otherIter->getI().distanceSquared(currentIter->getI());
169 
170  if (dist2 < minDist2) {
171  minDist2 = dist2;
172  nearestPeak = currentIter;
173  }
174  }
175 
176  if (minDist2 < maxSamePeakDist2 && nearestPeak && maxSamePeakDist > 0) {
177  if (peakSchemaMapper) {
178  nearestPeak->set(keys->peak, true);
179  } else {
180  for (FilterMap::const_iterator i = filterMap->begin(); i != filterMap->end(); ++i) {
181  afw::table::Key<afw::table::Flag> const &flagKey = i->second.peak;
182  nearestPeak->set(flagKey, nearestPeak->get(flagKey) || otherIter->get(flagKey));
183  }
184  }
185  } else if (minDist2 > minNewPeakDist2 && !(minNewPeakDist < 0)) {
186  if (peakSchemaMapper) {
187  std::shared_ptr<PeakRecord> newPeak = newPeaks.addNew();
188  newPeak->assign(*otherIter, *peakSchemaMapper);
189  newPeak->set(keys->peak, true);
190  } else {
191  newPeaks.push_back(otherIter);
192  }
193  }
194  }
195 
196  getMergedFootprint()->getPeaks().insert(getMergedFootprint()->getPeaks().end(), newPeaks.begin(),
197  newPeaks.end(),
198  true // deep-copy
199  );
200  }
201 
204 };
205 
207  std::vector<std::string> const &filterList,
208  afw::table::Schema const &initialPeakSchema)
209  : _peakSchemaMapper(initialPeakSchema) {
210  _initialize(sourceSchema, filterList);
211 }
212 
214  std::vector<std::string> const &filterList)
215  : _peakSchemaMapper(PeakTable::makeMinimalSchema()) {
216  _initialize(sourceSchema, filterList);
217 }
218 
224 
225 void FootprintMergeList::_initialize(afw::table::Schema &sourceSchema,
226  std::vector<std::string> const &filterList) {
227  _peakSchemaMapper.addMinimalSchema(_peakSchemaMapper.getInputSchema(), true);
228  // Add Flags for the filters
229  for (std::vector<std::string>::const_iterator iter = filterList.begin(); iter != filterList.end();
230  ++iter) {
231  KeyTuple &keys = _filterMap[*iter];
232  keys.footprint = sourceSchema.addField<afw::table::Flag>(
233  "merge_footprint_" + *iter,
234  "Detection footprint overlapped with a detection from filter " + *iter);
235  keys.peak = _peakSchemaMapper.editOutputSchema().addField<afw::table::Flag>(
236  "merge_peak_" + *iter, "Peak detected in filter " + *iter);
237  }
238  _peakTable = PeakTable::make(_peakSchemaMapper.getOutputSchema());
239 }
240 
242  afw::table::SourceCatalog const &inputCat, std::string const &filter,
243  float minNewPeakDist, bool doMerge, float maxSamePeakDist) {
244  FilterMap::const_iterator keyIter = _filterMap.find(filter);
245  if (keyIter == _filterMap.end()) {
247  (boost::format("Filter %s not in original list") % filter).str());
248  }
249 
250  // If list is empty don't check for any matches, just add all the objects
251  bool checkForMatches = (_mergeList.size() > 0);
252 
253  for (afw::table::SourceCatalog::const_iterator srcIter = inputCat.begin(); srcIter != inputCat.end();
254  ++srcIter) {
255  // Only consider unblended objects
256  if (srcIter->getParent() != 0) continue;
257 
258  std::shared_ptr<Footprint> foot = srcIter->getFootprint();
259 
260  // Empty pointer to account for the first match in the catalog. If there is more than one
261  // match, subsequent matches will be merged with this one
263 
264  if (checkForMatches) {
265  FootprintMergeVec::iterator iter = _mergeList.begin();
266  while (iter != _mergeList.end()) {
267  // Grow by one pixel to allow for touching
268  lsst::geom::Box2I box((**iter).getBBox());
269  box.grow(lsst::geom::Extent2I(1, 1));
270  if (box.overlaps(foot->getBBox()) && (**iter).overlaps(*foot)) {
271  if (!first) {
272  first = *iter;
273  // Add Footprint to existing merge and set flag for this band
274  if (doMerge) {
275  first->add(foot, _peakSchemaMapper, keyIter->second, minNewPeakDist,
276  maxSamePeakDist);
277  }
278  } else {
279  // Add merged Footprint to first
280  if (doMerge) {
281  first->add(**iter, _filterMap, minNewPeakDist, maxSamePeakDist);
282  iter = _mergeList.erase(iter);
283  continue;
284  }
285  }
286  }
287  ++iter;
288  }
289  }
290 
291  if (!first) {
292  _mergeList.push_back(std::make_shared<FootprintMerge>(foot, sourceTable, _peakTable,
293  _peakSchemaMapper, keyIter->second));
294  }
295  }
296 }
297 
299  // Now set the merged footprint as the footprint of the SourceRecord
300  for (FootprintMergeVec::iterator iter = _mergeList.begin(); iter != _mergeList.end(); ++iter) {
301  outputCat.push_back((**iter).getSource());
302  }
303 }
304 } // namespace detection
305 } // namespace afw
306 } // namespace lsst
Defines the fields and offsets for a table.
Definition: Schema.h:50
bool overlaps(Footprint const &rhs) const
FootprintMerge & operator=(FootprintMerge const &)=default
std::shared_ptr< afw::table::SourceRecord > getSource() const
void add(std::shared_ptr< Footprint > footprint, afw::table::SchemaMapper const &peakSchemaMapper, KeyTuple const &keys, float minNewPeakDist=-1., float maxSamePeakDist=-1.)
FootprintMerge(std::shared_ptr< Footprint > footprint, std::shared_ptr< afw::table::SourceTable > sourceTable, std::shared_ptr< PeakTable > peakTable, afw::table::SchemaMapper const &peakSchemaMapper, KeyTuple const &keys)
Schema const getInputSchema() const
Return the input schema (copy-on-write).
Definition: SchemaMapper.h:24
FootprintMergeList::FilterMap FilterMap
lsst::geom::Box2I getBBox() const
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:21
std::shared_ptr< Table > getTable() const
Return the table associated with the catalog.
Definition: Catalog.h:114
Table class for Peaks in Footprints.
Definition: Peak.h:92
Schema const getOutputSchema() const
Return the output schema (copy-on-write).
Definition: SchemaMapper.h:27
static std::shared_ptr< PeakTable > make(afw::table::Schema const &schema, bool forceNew=false)
Obtain a table that can be used to create records with given schema.
Definition: Peak.cc:100
T end(T... args)
STL class.
T push_back(T... args)
FootprintMergeList::KeyTuple KeyTuple
A base class for image defects.
afw::table::CatalogT< PeakRecord > PeakCatalog
Definition: Peak.h:234
iterator end()
Iterator access.
Definition: Catalog.h:397
Iterator class for CatalogT.
Definition: Catalog.h:38
T erase(T... args)
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:168
void getFinalSources(afw::table::SourceCatalog &outputCat)
Get SourceCatalog with entries that contain the final Footprint and SourceRecord for each entry...
Schema & editOutputSchema()
Return a reference to the output schema that allows it to be modified in place.
Definition: SchemaMapper.h:30
Reports errors in the logical structure of the program.
Definition: Runtime.h:46
T infinity(T... args)
Class to describe the properties of a detected object from an image.
Definition: Footprint.h:62
FootprintMergeList & operator=(FootprintMergeList const &)
std::shared_ptr< Footprint > getMergedFootprint() const
T get(T... args)
T find(T... args)
T size(T... args)
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
STL class.
void addMinimalSchema(Schema const &minimal, bool doMap=true)
Add the given minimal schema to the output schema.
T begin(T... args)
void addCatalog(std::shared_ptr< afw::table::SourceTable > sourceTable, afw::table::SourceCatalog const &inputCat, std::string const &filter, float minNewPeakDist=-1., bool doMerge=true, float maxSamePeakDist=-1.)
Add objects from a SourceCatalog in the specified filter.
FootprintMergeList(afw::table::Schema &sourceSchema, std::vector< std::string > const &filterList, afw::table::Schema const &initialPeakSchema)
Initialize the merge with a custom initial peak schema.
std::shared_ptr< geom::SpanSet > getSpans() const
Return a shared pointer to the SpanSet.
Definition: Footprint.h:117
ItemVariant const * other
Definition: Schema.cc:56
void add(FootprintMerge const &other, FilterMap const &keys, float minNewPeakDist=-1., float maxSamePeakDist=-1.)
iterator begin()
Iterator access.
Definition: Catalog.h:396
An integer coordinate rectangle.
Definition: Box.h:54
Key< T > addField(Field< T > const &field, bool doReplace=false)
Add a new field to the Schema, and return the associated Key.
Definition: Schema.cc:668
int end