From 925856a2f7f299595de37a3a2d782c4641dbe945 Mon Sep 17 00:00:00 2001 From: egnima <154389969+egnima@users.noreply.github.com> Date: Sun, 14 Apr 2024 13:55:39 -0500 Subject: [PATCH] Fix error caused by numpy sorting When sorting a numpy array, it crashes because the default numpy sort doesn't support a custom key function. This commit fixes that without directly importing numpy. --- kd_tree.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kd_tree.py b/kd_tree.py index 5b07fda..34b870b 100644 --- a/kd_tree.py +++ b/kd_tree.py @@ -39,7 +39,11 @@ def __init__(self, points, dim, dist_sq_func=None): def make(points, i=0): if len(points) > 1: - points.sort(key=lambda x: x[i]) + if type(points).__module__ == 'numpy' and type(points).__name__ == 'ndarray': + # Numpy-specific fix + points = points[points[:, i].argsort()] + else: + points.sort(key=lambda x: x[i]) i = (i + 1) % dim m = len(points) >> 1 return [make(points[:m], i), make(points[m + 1:], i),