# Buggy code
def _set_reach_dist(
    core_distances_,
    reachability_,
    predecessor_,
    point_index,
    processed,
    X,
    nbrs,
    metric,
    metric_params,
    p,
    max_eps,
):
    P = X[point_index : point_index + 1]
    # Assume that radius_neighbors is faster without distances
    # and we don't need all distances, nevertheless, this means
    # we may be doing some work twice.
    indices = nbrs.radius_neighbors(P, radius=max_eps, return_distance=False)[0]

    # Getting indices of neighbors that have not been processed
    unproc = np.compress(~np.take(processed, indices), indices)
    # Neighbors of current point are already processed.
    if not unproc.size:
        return

    # Only compute distances to unprocessed neighbors:
    if metric == "precomputed":
        dists = X[point_index, unproc]
    else:
        _params = dict() if metric_params is None else metric_params.copy()
        if metric == "minkowski" and "p" not in _params:
            # the same logic as neighbors, p is ignored if explicitly set
            # in the dict params
            _params["p"] = p
        dists = pairwise_distances(
            P, np.take(X, unproc, axis=0), metric=metric, n_jobs=None, **_params
        ).ravel()

    rdists = np.maximum(dists, core_distances_[point_index])
    improved = np.where(rdists < np.take(reachability_, unproc))
    reachability_[unproc[improved]] = rdists[improved]
    predecessor_[unproc[improved]] = point_index


# Traceback (AssertionError)
_____________________________ test_compare_to_ELKI _____________________________
[gw2] linux -- Python 3.9.1 /tmp/tmp.I1bENuyzQA/venv/bin/python

def test_compare_to_ELKI():
# Expected values, computed with (future) ELKI 0.7.5 using:
# java -jar elki.jar cli -dbc.in csv -dbc.filter FixedDBIDsFilter
#   -algorithm clustering.optics.OPTICSHeap -optics.minpts 5
# where the FixedDBIDsFilter gives 0-indexed ids.
r1 = [np.inf, 1.0574896366427478, 0.7587934993548423, 0.7290174038973836,
0.7290174038973836, 0.7290174038973836, 0.6861627576116127,
0.7587934993548423, 0.9280118450166668, 1.1748022534146194,
3.3355455741292257, 0.49618389254482587, 0.2552805046961355,
0.2552805046961355, 0.24944622248445714, 0.24944622248445714,
0.24944622248445714, 0.2552805046961355, 0.2552805046961355,
0.3086779122185853, 4.163024452756142, 1.623152630340929,
0.45315840475822655, 0.25468325192031926, 0.2254004358159971,
0.18765711877083036, 0.1821471333893275, 0.1821471333893275,
0.18765711877083036, 0.18765711877083036, 0.2240202988740153,
1.154337614548715, 1.342604473837069, 1.323308536402633,
0.8607514948648837, 0.27219111215810565, 0.13260875220533205,
0.13260875220533205, 0.09890587675958984, 0.09890587675958984,
0.13548790801634494, 0.1575483940837384, 0.17515137170530226,
0.17575920159442388, 0.27219111215810565, 0.6101447895405373,
1.3189208094864302, 1.323308536402633, 2.2509184159764577,
2.4517810628594527, 3.675977064404973, 3.8264795626020365,
2.9130735341510614, 2.9130735341510614, 2.9130735341510614,
2.9130735341510614, 2.8459300127258036, 2.8459300127258036,
2.8459300127258036, 3.0321982337972537]
o1 = [0, 3, 6, 4, 7, 8, 2, 9, 5, 1, 31, 30, 32, 34, 33, 38, 39, 35, 37, 36,
44, 21, 23, 24, 22, 25, 27, 29, 26, 28, 20, 40, 45, 46, 10, 15, 11,
13, 17, 19, 18, 12, 16, 14, 47, 49, 43, 48, 42, 41, 53, 57, 51, 52,
56, 59, 54, 55, 58, 50]
p1 = [-1, 0, 3, 6, 6, 6, 8, 3, 7, 5, 1, 31, 30, 30, 34, 34, 34, 32, 32, 37,
36, 44, 21, 23, 24, 22, 25, 25, 22, 22, 22, 21, 40, 45, 46, 10, 15,
15, 13, 13, 15, 11, 19, 15, 10, 47, 12, 45, 14, 43, 42, 53, 57, 57,
57, 57, 59, 59, 59, 58]

# Tests against known extraction array
# Does NOT work with metric='euclidean', because sklearn euclidean has
# worse numeric precision. 'minkowski' is slower but more accurate.
clust1 = OPTICS(min_samples=5).fit(X)

assert_array_equal(clust1.ordering_, np.array(o1))
E       AssertionError:
E       Arrays are not equal
E
E       Mismatched elements: 2 / 60 (3.33%)
E       Max absolute difference: 2
E       Max relative difference: 0.13333333
E        x: array([ 0,  3,  6,  4,  7,  8,  2,  9,  5,  1, 31, 30, 32, 34, 33, 38, 39,
E              35, 37, 36, 44, 21, 23, 24, 22, 25, 27, 29, 26, 28, 20, 40, 45, 46,
E              10, 17, 11, 13, 15, 19, 18, 12, 16, 14, 47, 49, 43, 48, 42, 41, 53,
E              57, 51, 52, 56, 59, 54, 55, 58, 50])
E        y: array([ 0,  3,  6,  4,  7,  8,  2,  9,  5,  1, 31, 30, 32, 34, 33, 38, 39,
E              35, 37, 36, 44, 21, 23, 24, 22, 25, 27, 29, 26, 28, 20, 40, 45, 46,
E              10, 15, 11, 13, 17, 19, 18, 12, 16, 14, 47, 49, 43, 48, 42, 41, 53,
E              57, 51, 52, 56, 59, 54, 55, 58, 50])

# Fixed code
def _set_reach_dist(
    core_distances_,
    reachability_,
    predecessor_,
    point_index,
    processed,
    X,
    nbrs,
    metric,
    metric_params,
    p,
    max_eps,
):
    P = X[point_index : point_index + 1]
    # Assume that radius_neighbors is faster without distances
    # and we don't need all distances, nevertheless, this means
    # we may be doing some work twice.
    indices = nbrs.radius_neighbors(P, radius=max_eps, return_distance=False)[0]

    # Getting indices of neighbors that have not been processed
    unproc = np.compress(~np.take(processed, indices), indices)
    # Neighbors of current point are already processed.
    if not unproc.size:
        return

    # Only compute distances to unprocessed neighbors:
    if metric == "precomputed":
        dists = X[point_index, unproc]
    else:
        _params = dict() if metric_params is None else metric_params.copy()
        if metric == "minkowski" and "p" not in _params:
            # the same logic as neighbors, p is ignored if explicitly set
            # in the dict params
            _params["p"] = p
        dists = pairwise_distances(
            P, np.take(X, unproc, axis=0), metric=metric, n_jobs=None, **_params
        ).ravel()

    rdists = np.maximum(dists, core_distances_[point_index])
    np.around(rdists, decimals=np.finfo(rdists.dtype).precision, out=rdists)
    improved = np.where(rdists < np.take(reachability_, unproc))
    reachability_[unproc[improved]] = rdists[improved]
    predecessor_[unproc[improved]] = point_index