AtsushiSakai/PythonRobotics · error · ValueError
Input array should only contain 0 and 1
Error message
Input array should only contain 0 and 1
What it means
Raised by compute_udf when the input boolean field contains values other than 0 and 1. The undirected distance transform (via the dt distance transform) requires a binary obstacle map, so any other value makes the result meaningless and is rejected up front.
Source
Thrown at Mapping/DistanceMap/distance_map.py:106
def compute_udf(obstacles):
"""
Compute the unsigned distance field (UDF) from a boolean field.
Parameters
----------
obstacles : array_like
A 2D boolean array where '1' represents obstacles and '0' represents free space.
Returns
-------
array_like
A 2D array of distances from the nearest obstacle, with the same dimensions as `bool_field`.
"""
edt = obstacles.copy()
if not np.all(np.isin(edt, [0, 1])):
raise ValueError("Input array should only contain 0 and 1")
edt = np.where(edt == 0, INF, edt)
edt = np.where(edt == 1, 0, edt)
for row in range(len(edt)):
dt(edt[row])
edt = edt.T
for row in range(len(edt)):
dt(edt[row])
edt = edt.T
return np.sqrt(edt)
def dt(d):
"""
Compute 1D distance transform under the squared Euclidean distance
Parameters
----------
d : array_likeView on GitHub (pinned to 1fe4fb980f)
Solutions
- Binarize the array before calling: np.where(field > threshold, 1, 0) or field.astype(bool).astype(int).
- Verify the data pipeline producing the obstacle map only emits 0/1.
- Add an assertion np.all(np.isin(field, [0, 1])) in your own code to catch bad input early.
Example fix
// before udf = compute_udf(occupancy) # occupancy holds 0..1 probabilities // after binary = (occupancy > 0.5).astype(int) udf = compute_udf(binary)
Defensive patterns
Strategy: validation
Validate before calling
import numpy as np assert np.all(np.isin(field, [0, 1])), 'obstacle field must be binary'
Type guard
def is_binary_field(a) -> bool:
import numpy as np
return isinstance(a, np.ndarray) and np.all(np.isin(a, [0, 1])) Prevention
- Binarize maps at the boundary of your pipeline with (arr > threshold).astype(int).
- Log np.unique(field) before calling distance-transform functions.
When it happens
Trigger: Calling compute_udf(obstacles) (or compute_sdf) with a float array, an occupancy grid containing probabilities (0..1), or an array with True/False plus other labels like 2 or 255.
Common situations: Feeding a grayscale map, a normalized occupancy probability grid, or a segmentation mask with multiple classes into a function that expects a strictly binary obstacle field.
Related errors
AI-assisted analysis of AtsushiSakai/PythonRobotics@1fe4fb980f (2026-08-28).
Data as JSON: /api/errors/90a5c11c9a438517.
Report an issue: GitHub.