Skip to content

Commit

Permalink
Add operator== and toString for CoaddBoundedField
Browse files Browse the repository at this point in the history
  • Loading branch information
parejkoj committed Apr 28, 2017
1 parent ae9ff96 commit 148a2b5
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/lsst/meas/algorithms/CoaddBoundedField.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ struct CoaddBoundedFieldElement {
) : field(field_), wcs(wcs_), validPolygon(validPolygon_),
weight(weight_) {}

/// Elements are equal if all their components are equal
bool operator==(CoaddBoundedFieldElement const& rhs) const {
return (field == rhs.field) && (wcs == rhs.wcs) && (rhs.validPolygon) && (weight == rhs.weight);
}
/// @copydoc operator==
bool operator!=(CoaddBoundedFieldElement const& rhs) const { return !(*this == rhs); };

PTR(afw::math::BoundedField) field;
PTR(afw::image::Wcs const) wcs;
PTR(afw::geom::polygon::Polygon const) validPolygon;
Expand Down Expand Up @@ -82,6 +89,9 @@ class CoaddBoundedField :

virtual PTR(afw::math::BoundedField) operator*(double const scale) const;

/// BoundedFields (of the same sublcass) are equal if their bounding boxes and parameters are equal.
virtual bool operator==(BoundedField const& rhs) const;

protected:

// See afw::table::io::Persistable::getPersistenceName
Expand All @@ -99,6 +109,12 @@ class CoaddBoundedField :
double _default; // when none of the elements contribute at a point, return this value
PTR(afw::image::Wcs const) _coaddWcs; // coordinate system this field is defined in
ElementVector _elements; // vector of constituent fields being coadded

virtual std::string toString() const {
std::ostringstream os;
os << "CoaddBoundedField with " << _elements.size() << " elements, default " << _default;
return os.str();
}
};

}}} // namespace lsst::meas::algorithms
Expand Down
2 changes: 2 additions & 0 deletions python/lsst/meas/algorithms/coaddBoundedField.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ PYBIND11_PLUGIN(coaddBoundedField) {
"bbox"_a, "coaddWcs"_a, "elements"_a, "default"_a);

/* Operators */
clsCoaddBoundedField.def("__eq__", &CoaddBoundedField::operator==, py::is_operator());
clsCoaddBoundedField.def("__ne__", &CoaddBoundedField::operator!=, py::is_operator());
clsCoaddBoundedField.def("__imul__", &CoaddBoundedField::operator*);

/* Members */
Expand Down
9 changes: 9 additions & 0 deletions src/CoaddBoundedField.cc
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,15 @@ PTR(afw::math::BoundedField) CoaddBoundedField::operator*(double const scale) co
throw LSST_EXCEPT(pex::exceptions::NotFoundError, "Scaling of CoaddBoundedField is not implemented");
}

bool CoaddBoundedField::operator==(BoundedField const& rhs) const {
auto rhsCasted = dynamic_cast<CoaddBoundedField const *>(&rhs);
if (!rhsCasted) return false;

return (getBBox() == rhsCasted->getBBox()) && (_default == rhsCasted->_default) &&
((*_coaddWcs) == (*rhsCasted->_coaddWcs)) && (_elements == rhsCasted->_elements);
}


}}} // namespace lsst::meas::algorithms


44 changes: 44 additions & 0 deletions tests/testCoaddBoundedField.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,50 @@ def testEvaluate(self):

self.assertLess(bad.sum(), 0.10*self.bbox.getArea())

def testEquality(self):
field1 = lsst.meas.algorithms.CoaddBoundedField(self.bbox, self.coaddWcs, self.elements, 0.0)
field2 = lsst.meas.algorithms.CoaddBoundedField(self.bbox, self.coaddWcs, self.elements, 0.0)
self.assertEqual(field1, field2)

bbox = lsst.afw.geom.Box2I(lsst.afw.geom.Point2I(-75, -75), lsst.afw.geom.Point2I(75, 75))
field3 = lsst.meas.algorithms.CoaddBoundedField(bbox, self.coaddWcs, self.elements, 0.0)
self.assertEqual(field1, field3)

coaddWcs = self.coaddWcs.clone()
field4 = lsst.meas.algorithms.CoaddBoundedField(self.bbox, coaddWcs, self.elements, 0.0)
self.assertEqual(field1, field4)

# NOTE: make a copy of the list; [:] to copy segfaults,
# copy.copy() doesn't behave nicely on py2 w/our pybind11 objects,
# and self.elements.copy() doesn't exist on py2.
elements = list(self.elements)
field5 = lsst.meas.algorithms.CoaddBoundedField(self.bbox, self.coaddWcs, elements, 0.0)
self.assertEqual(field1, field5)

# inequality tests below here
field6 = lsst.meas.algorithms.CoaddBoundedField(self.bbox, self.coaddWcs, self.elements, 3.0)
self.assertNotEqual(field1, field6)
field7 = lsst.meas.algorithms.CoaddBoundedField(self.bbox, self.coaddWcs, [], 0.0)
self.assertNotEqual(field1, field7)

bbox = lsst.afw.geom.Box2I(lsst.afw.geom.Point2I(-74, -75), lsst.afw.geom.Point2I(75, 75))
field8 = lsst.meas.algorithms.CoaddBoundedField(bbox, self.coaddWcs, self.elements, 0.0)
self.assertNotEqual(field1, field8)

crval = lsst.afw.coord.IcrsCoord(45.0*lsst.afw.geom.degrees, 45.0*lsst.afw.geom.degrees)
coaddWcs = self.makeRandomWcs(crval, maxOffset=2.0)
field9 = lsst.meas.algorithms.CoaddBoundedField(self.bbox, coaddWcs, self.elements, 0.0)
self.assertNotEqual(field1, field9)

elements = list(self.elements)
elements[2].weight = 1000000
field10 = lsst.meas.algorithms.CoaddBoundedField(self.bbox, self.coaddWcs, elements, 0.0)
self.assertNotEqual(field1, field10)
elements = list(self.elements)
elements.pop(0)
field11 = lsst.meas.algorithms.CoaddBoundedField(self.bbox, self.coaddWcs, elements, 0.0)
self.assertNotEqual(field1, field11)

def testPersistence(self):
"""Test that we can round-trip a CoaddBoundedField through FITS."""
filename = "testCoaddBoundedField.fits"
Expand Down

0 comments on commit 148a2b5

Please sign in to comment.