Skip to content

gen_indices

Index generation utilities for multi-dimensional array iteration.

This module provides generate(), which converts an index specification (int, slice, list, ndarray, or tuple) into an iterator over index tuples for a given array shape. It handles common NumPy-style indexing patterns including integer indexing, slicing, fancy indexing, and ellipsis expansion (Ellipsis is supported inside tuple specifications only).

generate(shape, idx)

Generate an iterator of index tuples for a given array shape.

Parameters:

Name Type Description Default
shape

The shape of the array to index - a sequence of dimension sizes.

required
idx

The index specification: - an int: selects that position in a 1-D shape; - a slice: selects the positions in its range; - a list (or other iterable): selects the given positions (1-D shapes only); - an ndarray: as list, for any shape depth; - a tuple: one specification per dimension, applied right-to-left; dimensions not covered by the tuple iterate over their full range. An Ellipsis entry expands to the full range of one dimension (numpy-like auto-fill of trailing dimensions is NOT supported: a tuple shorter than the shape raises TypeError).

required

Returns:

Type Description

An iterator of tuples - one per selected element - whose length

matches the shape depth. Each tuple holds one index per dimension.

Raises:

Type Description
AssertionError

If a 1-D-only index type (int, slice, list) is used with a multi-dimensional shape.

TypeError

If the index specification is not one of the supported types (a bare Ellipsis is not supported outside tuples), or if a tuple specification does not cover all the shape dimensions.

ValueError

If the index specification is invalid for the given shape.

Example
list(gen_indices.generate((3,), 1))  # [(1,)]
list(gen_indices.generate((3,), [0, 2]))  # [(0,), (2,)]
list(gen_indices.generate((2, 3), (Ellipsis, 1)))
# -> [(0, 1), (1, 1)] - full range on dim 0, fixed index 1 on dim 1.
Source code in py_misc_utils/gen_indices.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def generate(shape, idx):
  """Generate an iterator of index tuples for a given array shape.

  Args:
    shape: The shape of the array to index - a sequence of dimension
      sizes.
    idx: The index specification:
      - an int: selects that position in a 1-D shape;
      - a slice: selects the positions in its range;
      - a list (or other iterable): selects the given positions (1-D
        shapes only);
      - an ndarray: as list, for any shape depth;
      - a tuple: one specification per dimension, applied right-to-left;
        dimensions not covered by the tuple iterate over their full
        range. An Ellipsis entry expands to the full range of one
        dimension (numpy-like auto-fill of trailing dimensions is NOT
        supported: a tuple shorter than the shape raises TypeError).

  Returns:
    An iterator of tuples - one per selected element - whose length
    matches the shape depth. Each tuple holds one index per dimension.

  Raises:
    AssertionError: If a 1-D-only index type (int, slice, list) is used
      with a multi-dimensional shape.
    TypeError: If the index specification is not one of the supported
      types (a bare Ellipsis is not supported outside tuples), or if a
      tuple specification does not cover all the shape dimensions.
    ValueError: If the index specification is invalid for the given
      shape.

  Example:
    list(gen_indices.generate((3,), 1))  # [(1,)]
    list(gen_indices.generate((3,), [0, 2]))  # [(0,), (2,)]
    list(gen_indices.generate((2, 3), (Ellipsis, 1)))
    # -> [(0, 1), (1, 1)] - full range on dim 0, fixed index 1 on dim 1.
  """
  if isinstance(idx, int):
    assert len(shape) == 1, f'{shape}'

    gens = [(idx,)]
  elif isinstance(idx, slice):
    assert len(shape) == 1, f'{shape}'

    gens = [range(*idx.indices(shape[0]))]
  elif isinstance(idx, (list, np.ndarray)):
    assert len(shape) == 1, f'{shape}'

    gens = [idx]
  elif isinstance(idx, tuple):
    assert len(shape) >= len(idx), f'{shape} vs. {idx}'

    gens = [None] * len(shape)
    eidx = None
    for i, tidx in enumerate(idx):
      if isinstance(tidx, int):
        gens[i] = (tidx,)
      elif isinstance(tidx, slice):
        gens[i] = range(*tidx.indices(shape[i]))
      elif hasattr(tidx, '__iter__'):
        gens[i] = tidx
      elif isinstance(tidx, Ellipsis.__class__):
        eidx = i
        break

    if eidx is not None:
      for i in range(1, len(idx) - eidx):
        tidx = idx[-i]
        if isinstance(tidx, int):
          gens[-i] = (tidx,)
        elif isinstance(tidx, slice):
          gens[-i] = range(*tidx.indices(shape[-i]))
        elif hasattr(tidx, '__iter__'):
          gens[-i] = tidx
        elif isinstance(tidx, Ellipsis.__class__):
          raise ValueError(f'Wrong index {idx} for shape {shape}')

      for i in range(len(shape)):
        if gens[i] is None:
          gens[i] = range(shape[i])
  else:
    raise TypeError(f'Wrong index {idx} for shape {shape}')

  return itertools.product(*gens)