jax-ml/jax · error · ValueError

coordinates must be a sequence of length input.ndim, but {}

Error message

coordinates must be a sequence of length input.ndim, but {} != {}

What it means

jax.scipy.ndimage.map_coordinates requires one coordinate array per input axis; the number of coordinate arrays must equal input.ndim. Mismatch means JAX cannot align coordinates to axes.

Source

Thrown at jax/_src/scipy/ndimage.py:80


def _linear_indices_and_weights(coordinate: Array) -> list[tuple[Array, ArrayLike]]:
  lower = jnp.floor(coordinate)
  upper_weight = coordinate - lower
  lower_weight = 1 - upper_weight
  index = lower.astype(np.int32)
  return [(index, lower_weight), (index + 1, upper_weight)]


@api.jit(static_argnums=(2, 3, 4))
def _map_coordinates(input: ArrayLike, coordinates: Sequence[ArrayLike],
                     order: int, mode: str, cval: ArrayLike) -> Array:
  input_arr = jnp.asarray(input)
  coordinate_arrs = [jnp.asarray(c) for c in coordinates]
  cval = jnp.asarray(cval, input_arr.dtype)

  if len(coordinates) != input_arr.ndim:
    raise ValueError('coordinates must be a sequence of length input.ndim, but '
                     '{} != {}'.format(len(coordinates), input_arr.ndim))

  index_fixer = _INDEX_FIXERS.get(mode)
  if index_fixer is None:
    raise NotImplementedError(
        'jax.scipy.ndimage.map_coordinates does not yet support mode {}. '
        'Currently supported modes are {}.'.format(mode, set(_INDEX_FIXERS)))

  if mode == 'constant':
    is_valid = lambda index, size: (0 <= index) & (index < size)
  else:
    is_valid = lambda index, size: True

  if order == 0:
    interp_fun = _nearest_indices_and_weights
  elif order == 1:
    interp_fun = _linear_indices_and_weights
  else:

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Pass a sequence/tuple of ndim arrays: coordinates=(rows, cols) for 2D
  2. If you have a stacked (ndim, N) array, unpack it: coordinates=tuple(coords)
  3. Verify input.ndim matches len(coordinates) before calling

Example fix

# before
coords = jnp.stack([ys, xs])  # shape (2, N)
out = ndimage.map_coordinates(img, coords)
# after
out = ndimage.map_coordinates(img, (ys, xs))
Defensive patterns

Strategy: validation

Validate before calling

coords = tuple(coords) if hasattr(coords, 'shape') and coords.ndim and coords.shape[0] == input.ndim else coords
assert len(coords) == jnp.asarray(input).ndim

Type guard

def coords_match(coords, x) -> bool: return len(list(coords)) == jnp.asarray(x).ndim

Prevention

When it happens

Trigger: Passing [x] for a 2D image, or (row, col, batch) coordinates for a 2D input; passing a single array of shape (2, N) instead of a 2-sequence of (N,) arrays.

Common situations: Using np.stack([xs, ys]) as coordinates (one array) instead of (xs, ys); migrating from scipy where a single coordinates array of shape (ndim, N) is accepted.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/1dc5efefa471658c. Report an issue: GitHub.