Given a key for indexing an ndarray, return an equivalent key which is a tuple with length equal to the number of dimensions. The expansion is done by replacing all `Ellipsis` items with the right number of full slices and then padding the key with full slices so that it reaches the
(key, ndim)
| 229 | |
| 230 | |
| 231 | def expanded_indexer(key, ndim): |
| 232 | """Given a key for indexing an ndarray, return an equivalent key which is a |
| 233 | tuple with length equal to the number of dimensions. |
| 234 | |
| 235 | The expansion is done by replacing all `Ellipsis` items with the right |
| 236 | number of full slices and then padding the key with full slices so that it |
| 237 | reaches the appropriate dimensionality. |
| 238 | """ |
| 239 | if not isinstance(key, tuple): |
| 240 | # numpy treats non-tuple keys equivalent to tuples of length 1 |
| 241 | key = (key,) |
| 242 | new_key = [] |
| 243 | # handling Ellipsis right is a little tricky, see: |
| 244 | # https://numpy.org/doc/stable/reference/arrays.indexing.html#advanced-indexing |
| 245 | found_ellipsis = False |
| 246 | for k in key: |
| 247 | if k is Ellipsis: |
| 248 | if not found_ellipsis: |
| 249 | new_key.extend((ndim + 1 - len(key)) * [slice(None)]) |
| 250 | found_ellipsis = True |
| 251 | else: |
| 252 | new_key.append(slice(None)) |
| 253 | else: |
| 254 | new_key.append(k) |
| 255 | if len(new_key) > ndim: |
| 256 | raise IndexError("too many indices") |
| 257 | new_key.extend((ndim - len(new_key)) * [slice(None)]) |
| 258 | return tuple(new_key) |
| 259 | |
| 260 | |
| 261 | def normalize_slice(sl: slice, size: int) -> slice: |
no outgoing calls
no test coverage detected
searching dependent graphs…