You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Change regionprops so that indices match labels #2755
I would argue that the most common use case for regionprops is to measure the properties of a sequentially labelled image, starting from 0 (if there's a background) or 1 (if not). Currently, in both of these cases, regionprops returns a list where result[i] contains the region properties for label i+1.
In the case of nonsequential labels, this is even more confusing, because result[i] contains the region properties for the ith nonzero label, which could be anything.
I suggest one of the following:
change the API to return a dictionary of label: regionprops.
add a keyword argument to the API to return said dictionary.
add a regionprops_dict function to the API to return this.
Completely agree. This has always bothered me, too. The dictionary is almost certainly the right API choice. An OrderedDict might be the best choice, actually, to maintain the iteration order (in case that has been assumed in the current list paradigm).
I suspect trying to hack this in via options 2 or 3 would involve possibly rather significant performance hits compared to just doing it right to begin with (1). However, if backwards compatibility must be maintained, we could go ahead and change the underlying mechanics to return an [ordered] dictionary and then convert that back to the current behavior with a kwarg if desired.
Thanks for the suggestion of OrderedDict @JDWarner! I think this is the right approach: do the work of changing the internals to an ordered dict, cast that to list(values) to return current (deprecated) behaviour, then switch to ordered dict at some release.
@stefanv as an aside, the insertion order preservation from Python 3.6 is not yet a guarantee, though the buzz around PyCon on Twitter was that they would have no choice but to make it a guarantee because it was so popular. Nevertheless, I think this means we should stick to OrderedDict for this change?
I find it kind of natural? If I want the properties for label i I do props[i]. Most of my scripts using regionprops start by building the {props[i].label: props[i]} dictionary.
It's also worth noting that, because we ignore the 0 label, even in the case of sequential labels, you're stuck with props[i] -> the property for label i+1. This could be a source of confusion for many users and/or the source of subtle bugs.
On the other hand, I see that ~100% of my recent regionprops usage is regionprops_table, so I don't care about this as much as I used to. 😂 But the off-by-one stuff has always creeped me out. 🤷
Right, but I still want to understand why it is important to know the label. I am not sure when you use the label in processing. I'd imagine a pipeline like:
# Replace label by area
for r in regions:
label[r.label] = r.area
Although, usually I do something like:
areas = [r.area for r in regions]
print(f"Mean area is {areas.mean()}")
So, just curious in what context it is used by others, and happy to make changes to accommodate those scenarios.
I must also admit that when I went back through my older gh repos I found mostly usages of the form you suggested:
areas = [r.area for r in regions]
print(f"Mean area is {areas.mean()}")
So I'm in the +/-1 camp like you now. But I will definitely say I have been confused by these off-by-one issues in the past, and continue to be confused in other situations where one must go back and forth between the ID and the object, e.g. micromechman/defdap#102.
I've been caught out by the values in hrdic.Map.grains being off by one compared to indices in a hrdic.Map...
This is true for example in interactive data exploration examples, where you show a label image, and clicking on the image fetches properties of the label under the cursor.
@jni, what do you think about returning a set here? I feel like that might also be a very natural fit to communicate that there are no particular guarantees about the returned order of labels or that labels are contiguousness.
I'd also be +1 on using a dict, mapping label ids to RegionProperties. :)
Activity
JDWarner commentedon Aug 13, 2017
Completely agree. This has always bothered me, too. The dictionary is almost certainly the right API choice. An OrderedDict might be the best choice, actually, to maintain the iteration order (in case that has been assumed in the current list paradigm).
I suspect trying to hack this in via options 2 or 3 would involve possibly rather significant performance hits compared to just doing it right to begin with (1). However, if backwards compatibility must be maintained, we could go ahead and change the underlying mechanics to return an [ordered] dictionary and then convert that back to the current behavior with a kwarg if desired.
jni commentedon Aug 14, 2017
Thanks for the suggestion of OrderedDict @JDWarner! I think this is the right approach: do the work of changing the internals to an ordered dict, cast that to list(values) to return current (deprecated) behaviour, then switch to ordered dict at some release.
stefanv commentedon Aug 14, 2017
Python 3.6++
+1 on option (1), moving to the dictionary API.
jni commentedon Aug 28, 2017
@stefanv as an aside, the insertion order preservation from Python 3.6 is not yet a guarantee, though the buzz around PyCon on Twitter was that they would have no choice but to make it a guarantee because it was so popular. Nevertheless, I think this means we should stick to OrderedDict for this change?
stefanv commentedon Aug 28, 2017
@jni Yes, agreed
stefanv commentedon Aug 31, 2024
@jni Could you give some insight on why it is useful to index by label?
jni commentedon Aug 31, 2024
I find it kind of natural? If I want the properties for label i I do props[i]. Most of my scripts using regionprops start by building the {props[i].label: props[i]} dictionary.
jni commentedon Aug 31, 2024
It's also worth noting that, because we ignore the 0 label, even in the case of sequential labels, you're stuck with props[i] -> the property for label i+1. This could be a source of confusion for many users and/or the source of subtle bugs.
jni commentedon Aug 31, 2024
On the other hand, I see that ~100% of my recent regionprops usage is regionprops_table, so I don't care about this as much as I used to. 😂 But the off-by-one stuff has always creeped me out. 🤷
stefanv commentedon Aug 31, 2024
Right, but I still want to understand why it is important to know the label. I am not sure when you use the label in processing. I'd imagine a pipeline like:
Although, usually I do something like:
So, just curious in what context it is used by others, and happy to make changes to accommodate those scenarios.
jni commentedon Aug 31, 2024
I must also admit that when I went back through my older gh repos I found mostly usages of the form you suggested:
So I'm in the +/-1 camp like you now. But I will definitely say I have been confused by these off-by-one issues in the past, and continue to be confused in other situations where one must go back and forth between the ID and the object, e.g. micromechman/defdap#102.
This is true for example in interactive data exploration examples, where you show a label image, and clicking on the image fetches properties of the label under the cursor.
lagru commentedon Sep 1, 2024
@jni, what do you think about returning a
sethere? I feel like that might also be a very natural fit to communicate that there are no particular guarantees about the returned order of labels or that labels are contiguousness.I'd also be +1 on using a dict, mapping label ids to
RegionProperties. :)stefanv commentedon Aug 16, 2025
If you need region by label, you can also do: