Skip to content

Change regionprops so that indices match labels #2755

Open

Description

@jni

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:

  1. change the API to return a dictionary of label: regionprops.
  2. add a keyword argument to the API to return said dictionary.
  3. add a regionprops_dict function to the API to return this.

Any opinions on this?

Activity

  1. JDWarner commented on Aug 13, 2017

    @JDWarner
    Contributor

    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.

  2. jni commented on Aug 14, 2017

    @jni
    MemberAuthor

    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.

  3. stefanv commented on Aug 14, 2017

    @stefanv
    Member

    Python 3.6++

    +1 on option (1), moving to the dictionary API.

  4. jni commented on Aug 28, 2017

    @jni
    MemberAuthor

    @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?

  5. stefanv commented on Aug 28, 2017

    @stefanv
    Member

    @jni Yes, agreed

  6. added this to the 0.15 milestone on Jan 15, 2018
  7. modified the milestones: 0.15, 0.16 on Apr 20, 2019
  8. locked and limited conversation to collaborators on Oct 18, 2021
  9. added
    📜 type: API
    on Mar 30, 2022
  10. unlocked this conversation on Mar 30, 2022
  11. stefanv commented on Aug 31, 2024

    @stefanv
    Member

    @jni Could you give some insight on why it is useful to index by label?

  12. jni commented on Aug 31, 2024

    @jni
    MemberAuthor

    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.

  13. jni commented on Aug 31, 2024

    @jni
    MemberAuthor

    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.

  14. jni commented on Aug 31, 2024

    @jni
    MemberAuthor

    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. 🤷

  15. stefanv commented on Aug 31, 2024

    @stefanv
    Member

    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.

  16. jni commented on Aug 31, 2024

    @jni
    MemberAuthor

    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.

  17. lagru commented on Sep 1, 2024

    @lagru
    Member

    @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. :)

  18. stefanv commented on Aug 16, 2025

    @stefanv
    Member

    If you need region by label, you can also do:

    area_by_label = {r.label: r for r in regions}
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ⏩ type: Enhancement📜 type: API

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions