I am attempting to build a kd-tree with 3D coordinates that have arbitrary integers assigned to them. For example a tuple ([34534.65424, 10957.00023, -79.154323], 32).
However, I cant find out how to use scipy.spatial.cKDTree() with anything but a numpy array.
The identifier could even be a fourth field in the numpy array but the tree would have to be build based on the first three coordinates only.
Can somebody shed some light on this?
The purpose is to reference the kd-tree nodes back to a segment it belongs to for later comparisons.
Thank you!
Since the tree structure and also answers to queries are given in terms of indices, not values, you can simply keep your labels in a separate array with the same order.
Demo:
a = np.random.rand(100, 3)
t = cKDTree(a)
t.tree.indices
# array([13, 12, 18, 19, 20, 47, 54, 55, 83, 88, 82, 40, 31, 10, 45, 22, 15,
# 95, 76, 46, 94, 62, 5, 84, 74, 63, 80, 81, 59, 9, 34, 29, 71, 61,
# 73, 4, 85, 0, 16, 39, 35, 26, 53, 69, 77, 6, 36, 97, 57, 78, 90,
# 32, 38, 68, 65, 96, 3, 24, 66, 11, 86, 89, 91, 30, 67, 42, 28, 33,
# 7, 44, 8, 17, 60, 56, 41, 43, 92, 51, 58, 2, 93, 21, 14, 64, 70,
# 87, 72, 50, 25, 49, 98, 37, 99, 52, 1, 48, 75, 79, 23, 27])
t.query_pairs(0.1)
# {(20, 55), (48, 98), (6, 35), (38, 89), (98, 99), (26, 39), (45, 84), (26, 53), (37, 48), (48, 99), (15, 46), (36, 97), (14, 87), (42, 56), (37, 99), (5, 94), (71, 73), (0, 85), (70, 72), (22, 74), (37, 98)}