Skip to content

Commit

Permalink
Only iterate over available region properties
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanv committed Nov 5, 2015
1 parent 856e199 commit 52594d0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
32 changes: 28 additions & 4 deletions skimage/measure/_regionprops.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
'WeightedNormalizedMoments': 'weighted_moments_normalized'
}

PROP_VALS = PROPS.values()
PROP_VALS = set(PROPS.values())


class _cached_property(object):
Expand Down Expand Up @@ -105,6 +105,9 @@ def __get__(self, obj, type=None):


class _RegionProperties(object):
"""Please refer to `skimage.measure.regionprops` for more information
on the available region properties.
"""

def __init__(self, slice, label, label_image, intensity_image,
cache_active):
Expand Down Expand Up @@ -301,7 +304,23 @@ def weighted_moments_normalized(self):
return _moments.moments_normalized(self.weighted_moments_central, 3)

def __iter__(self):
return iter(PROPS.values())
props = PROP_VALS

if self._intensity_image is None:
unavailable_props = ('intensity_image',
'max_intensity',
'mean_intensity',
'min_intensity',
'weighted_moments',
'weighted_moments_central',
'weighted_centroid',
'weighted_local_centroid',
'weighted_moments_hu',
'weighted_moments_normalized')

props = props.difference(unavailable_props)

return iter(sorted(props))

def __getitem__(self, key):
value = getattr(self, key, None)
Expand Down Expand Up @@ -457,7 +476,12 @@ def regionprops(label_image, intensity_image=None, cache=True):
wnu_ji = wmu_ji / wm_00^[(i+j)/2 + 1]
where `wm_00` is the zeroth spatial moment (intensity-weighted area).
where ``wm_00`` is the zeroth spatial moment (intensity-weighted area).
Each region also supports iteration, so that you can do::
for prop in region:
print(prop, region[prop])
References
----------
Expand All @@ -473,7 +497,7 @@ def regionprops(label_image, intensity_image=None, cache=True):
Examples
--------
>>> from skimage import data, util
>>> from skimage.morphology import label
>>> from skimage.measure import label
>>> img = util.img_as_ubyte(data.coins()) > 110
>>> label_img = label(img, connectivity=img.ndim)
>>> props = regionprops(label_img)
Expand Down
10 changes: 10 additions & 0 deletions skimage/measure/tests/test_regionprops.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,16 @@ def test_equals():
assert_equal(r1 != r3, True, "Different regionprops are equal")


def test_iterate_all_props():
region = regionprops(SAMPLE)[0]
p0 = {p: region[p] for p in region}

region = regionprops(SAMPLE, intensity_image=INTENSITY_SAMPLE)[0]
p1 = {p: region[p] for p in region}

assert len(p0) < len(p1)


if __name__ == "__main__":
from numpy.testing import run_module_suite
run_module_suite()

0 comments on commit 52594d0

Please sign in to comment.