{"record":{"id":"90a5c11c9a438517","repo":"AtsushiSakai/PythonRobotics","slug":"input-array-should-only-contain-0-and-1","errorCode":null,"errorMessage":"Input array should only contain 0 and 1","messagePattern":"Input array should only contain 0 and 1","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Mapping/DistanceMap/distance_map.py","lineNumber":106,"sourceCode":"\n\ndef compute_udf(obstacles):\n    \"\"\"\n    Compute the unsigned distance field (UDF) from a boolean field.\n\n    Parameters\n    ----------\n    obstacles : array_like\n        A 2D boolean array where '1' represents obstacles and '0' represents free space.\n\n    Returns\n    -------\n    array_like\n        A 2D array of distances from the nearest obstacle, with the same dimensions as `bool_field`.\n    \"\"\"\n    edt = obstacles.copy()\n    if not np.all(np.isin(edt, [0, 1])):\n        raise ValueError(\"Input array should only contain 0 and 1\")\n    edt = np.where(edt == 0, INF, edt)\n    edt = np.where(edt == 1, 0, edt)\n    for row in range(len(edt)):\n        dt(edt[row])\n    edt = edt.T\n    for row in range(len(edt)):\n        dt(edt[row])\n    edt = edt.T\n    return np.sqrt(edt)\n\n\ndef dt(d):\n    \"\"\"\n    Compute 1D distance transform under the squared Euclidean distance\n\n    Parameters\n    ----------\n    d : array_like","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/AtsushiSakai/PythonRobotics/blob/1fe4fb980f6a12fe21c3c33d2b4da97a52c9154d/Mapping/DistanceMap/distance_map.py#L88-L124","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nudf = compute_udf(occupancy)  # occupancy holds 0..1 probabilities\n\n// after\nbinary = (occupancy > 0.5).astype(int)\nudf = compute_udf(binary)","handlingStrategy":"validation","validationCode":"import numpy as np\nassert np.all(np.isin(field, [0, 1])), 'obstacle field must be binary'","typeGuard":"def is_binary_field(a) -> bool:\n    import numpy as np\n    return isinstance(a, np.ndarray) and np.all(np.isin(a, [0, 1]))","tryCatchPattern":null,"preventionTips":["Binarize maps at the boundary of your pipeline with (arr > threshold).astype(int).","Log np.unique(field) before calling distance-transform functions."],"tags":["numpy","distance-transform","input-validation"],"backgroundTag":"binary-array-validation-failed","analyzedSha":"1fe4fb980f6a12fe21c3c33d2b4da97a52c9154d","analyzedAt":"2026-08-28T13:23:33.733Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}