Skip to content

ENH: Suggest "ignore" mode for ravel_multi_index #15475

Open

Description

@lagru

I'd like to suggest a new mode (maybe named "ignore") which treats multi_index as index offsets rather than direct positional indices meaning: out-of-bounds indices would be calculated as normal indices, neither wrapped nor clipped and returned without raising an exception.

Over at scikit-image/scikit-image#4209 (comment) we would have liked to use np.ravel_multi_index for a use case which is not (yet) supported. The new mode would have allowed us to adjust the multi_index corresponding to a structuring element by its center index and simply pass the result into ravel_multi_index to compute the offsets in question.

I am willing to give this a try myself but first wanted to check in with you. I haven't looked to deeply at the code dealing with this yet but my current understanding is that it may be enough to simply add a case that ignores j < 0 || j >= m to this switch statement.

Activity

  1. lagru commented on Jan 31, 2020

    @lagru
    ContributorAuthor

    Okay, I got it to work for ravel_multi_index by simply adding a new value NPY_IGNORE to the NPY_CLIPMODE enum. Unfortunately that enum is used elsewhere as well (e.g. numpy.take) where indices are actually used for indexing and out-of-bounds indices can't simply be treated as normal ones.

    I'm not sure how to address this. Ideally the change would only affect ravel_multi_index. I doubt you'd be okay with adding another enum just for that function. The other option I see is to raise "clipmode not understood" for those functions if NPY_IGNORE is provided.

  2. eric-wieser commented on Jan 31, 2020

    @eric-wieser
    Member

    The other option I see is to raise "clipmode not understood" for those functions if NPY_IGNORE is provided.

    I think "forbidden" would be more accurate than "not understood", but this approach (and the overall idea) seems fine to me.

  3. madphysicist commented on Oct 16, 2022

    @madphysicist
    Contributor

    Another mode which is missing is normal "indexing" mode: a mixture of wrap and raise that wraps negative indices up to -N and raises on anything outside [-N, N). There should be no issue with this given that the shape is known. @eric-wieser If you feel that this is worth a separate issue, I would be happy to open one. "indexing" mode might be useful for other functions that use he NPY_CLIPMODE enum.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions