Merge pull request #956 from vighneshbirodkar/issue945

Equality of regionprops
This commit is contained in:
Johannes Schönberger
2014-03-30 08:48:34 -04:00
2 changed files with 32 additions and 0 deletions
+16
View File
@@ -56,6 +56,8 @@ PROPS = {
'WeightedNormalizedMoments': 'weighted_moments_normalized'
}
PROP_VALS = PROPS.values()
class _cached_property(object):
"""Decorator to use a function as a cached property.
@@ -327,6 +329,20 @@ class _RegionProperties(MutableMapping):
category=DeprecationWarning)
return getattr(self, PROPS[key])
def __eq__(self, other):
if not isinstance(other, _RegionProperties):
return False
for key in PROP_VALS:
try:
#so that NaNs are equal
np.testing.assert_equal(getattr(self, key, None),
getattr(other, key, None))
except AssertionError:
return False
return True
def regionprops(label_image, properties=None,
intensity_image=None, cache=True):
+16
View File
@@ -371,6 +371,22 @@ def test_invalid():
assert_raises(AttributeError, get_intensity_image)
def test_equals():
arr = np.zeros((100, 100), dtype=np.int)
arr[0:25, 0:25] = 1
arr[50:99, 50:99] = 2
regions = regionprops(arr)
r1 = regions[0]
regions = regionprops(arr)
r2 = regions[0]
r3 = regions[1]
assert_equal(r1 == r2, True, "Same regionprops are not equal")
assert_equal(r1 != r3, True, "Different regionprops are equal")
if __name__ == "__main__":
from numpy.testing import run_module_suite
run_module_suite()