Skip to content

BUG: Inconsistent indexing with np.uint64 index arrays containing np.iinfo(np.uint64).max #23607

Closed

Description

@philipc2

Describe the issue:

When indexing another numpy array with a 64-bit unsigned integer array containing np.iinfo(np.uint64).max, it is treated as -1, wrapping around to the end of the array. However, when using a single np.iinfo(np.uint64).max index into another numpy array, it throws an OverflowError.

We were planning on using np.uint64 as our datatype for indexing arrays, with a fill_value of np.iinfo(np.uint64).max to ensure that users do not index another array using the fill value. We were expecting it to throw the following exception:

IndexError: index 18446744073709551615 is out of bounds for axis 0 with size 4

Reproduce the code example:

import numpy as np

INT_DTYPE = np.uint64
INT_FILL_VALUE = np.iinfo(INT_DTYPE).max

# data that we are indexing into
dummy_data = np.array([1, 2, 3, 4])
valid_indices = np.array([0, 1], dtype=INT_DTYPE)
invalid_indices = np.array([0, 1, INT_FILL_VALUE, INT_FILL_VALUE], dtype=INT_DTYPE)
invalid_index = INT_FILL_VALUE 
invalid_index_array = np.array(INT_FILL_VALUE, dtype=INT_DTYPE)

# this returns the expected array [1, 2]
dummy_data[valid_indices]

# this throws the error listed below
dummy_data[invalid_index]

# this also throws an error
dummy_data[invalid_index_array]

# does not throw an error, treats INT_FILL_VALUE as `-1` for indexing, returns [1, 2, 4, 4]
dummy_data[invalid_indices]

Error message:

OverflowError: Python int too large to convert to C long

Runtime information:

1.23.5
3.10.10 | packaged by conda-forge | (main, Mar 24 2023, 20:08:06) [GCC 11.3.0]

Context for the issue:

The uxarray Python package works with unstructured grid data, which includes geometries such as nodes, edges, and faces. Certain grids may contain faces that have a lower geometry (ex: triangle vs quadrilateral). We follow the UGRID conventions, which uses a fill value to indicate faces with smaller number of nodes than the arrays allow.

We chose to use the max of uintp as our fill value since we expected it to throw an error if you try to index using it, however this was not the case.

We have also considered using numpy masked arrays, but have not explored that route yet.

Activity

  1. changed the title [-]BUG: Inconsistent indexing when with `np.uint64` index arrays [/-] [+]BUG: Inconsistent indexing with `np.uint64` index arrays containing `np.iinfo(np.uint64).max`[/+] on Apr 18, 2023
  2. seberg commented on Apr 18, 2023

    @seberg
    Member

    Yeah, this is an old issue, because our indexing code is specialized for np.intp (which is the "correct" integer for indexing). Casting to that doesn't check out-of-bounds in NumPy, but refusing to cast in indexing operation would be no better.
    (Basically, maybe one day it would be a nicer project to either warn/error on such a cast or have a specialized cast that errors.)

    We were planning on using np.uint64 as our datatype for indexing arrays,

    You should use np.intp (not uintp!) consistently. Everything else will just incur unnecessary casts and overheads. This also makes it clear that np.iinfo(np.intp).min is a good "sentinel" value that must fail indexing (you can also use the max, but the minimal signed integer is a bit special anyway).

    We have also considered using numpy masked arrays, but have not explored that route yet.

    Masked arrays have their fair amount of issues unfortunately since they are far from first-class citizens in NumPy (and are too old to easily use improvements to NumPy core). If your use-case is simple enough without their use, we recommend not using them and at this point hope that a new replacement is created eventually.

  3. philipc2 commented on Apr 18, 2023

    @philipc2
    Author

    Yeah, this is an old issue, because our indexing code is specialized for np.intp (which is the "correct" integer for indexing). Casting to that doesn't check out-of-bounds in NumPy, but refusing to cast in indexing operation would be no better. (Basically, maybe one day it would be a nicer project to either warn/error on such a cast or have a specialized cast that errors.)

    We were planning on using np.uint64 as our datatype for indexing arrays,

    You should use np.intp (not uintp!) consistently. Everything else will just incur unnecessary casts and overheads. This also makes it clear that np.iinfo(np.intp).min is a good "sentinel" value that must fail indexing (you can also use the max, but the minimal signed integer is a bit special anyway).

    We have also considered using numpy masked arrays, but have not explored that route yet.

    Masked arrays have their fair amount of issues unfortunately since they are far from first-class citizens in NumPy (and are too old to easily use improvements to NumPy core). If your use-case is simple enough without their use, we recommend not using them and at this point hope that a new replacement is created eventually.

    This makes sense! Thank you for the clarification!

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

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions