0

I am writing code to find out if a point 'npA' from the set 'pA' has a closest neighbour from a set 'pB' containing randomly generated points. (In the code I have a single point but in my application, the points would be randomly generated). My problem is that I am unable to view/access the resulting list from the cKDTree.query_ball_point result.

In my variable explorer the result displays the type as object and the value as ndarray object of numpy module. When I try to view/access this list i.e. result, a window pops up that says object arrays are currently not supported. I would like to know how I can view this list or convert it to an array that I can use for some analysis later on.

from scipy.spatial import cKDTree
import numpy as np

pA = np.array([[0.000,0.000],[0.300,0.000],[0.600,0.000],[0.000,0.300], [0.300,0.300],[0.600,0.300],[0.000,0.600],[0.300,0.600],[0.600,0.600]])

pB = np.array([[0.300,0.600]])
npA =[[pA[0,0],pA[0,1]]]
npA = np.array(npA)

tree = cKDTree(npA)
result = tree.query_ball_point(pB,0.100) #I wish to view the list stored in result
P.W
  • 26,289
  • 6
  • 39
  • 76
Joshwin Paul
  • 3
  • 1
  • 3

1 Answers1

1

I'm not sure if I understood your problem correctly, but just writing: print(result) and running your code from the command line gives: [list([])], and changing the value of distance from 0.1 (no points so close) to 1.0 gives [list([0L])], where 0 is the position of the nearest neighbour in your list.

BTW, the construction of the array npA seems strange to me. You could get the same result just writing: [pA[0]].

  • Let me elaborate on my problem, So basically I am moving a 2D template pA over a 2D set of data pB and I am measuring the closest distance of a single point npA of the template pA at each position in the data set pB. And then, I am compiling the matches. So I need the result for a match(i.e. the existence of a closest neighbour) for npA as either a 1 or a 0. I am not sure if I will be able to use the output 'List([])' for this. – Joshwin Paul Aug 15 '18 at 09:20
  • `if result == 'List([])': resbool = 0 else: resbool = 1` – Karol Frydrych Aug 16 '18 at 06:01
  • I tried your method but it returns an error 'ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()'. I have re written my code with your method, can you tell me where I am going wrong `result_ball = tree.query_ball_point(pB,accuracy) if result_ball == 'List([])': result[j] = 0 else: result[j] = 1`. I can repost it if it is not clear. – Joshwin Paul Aug 16 '18 at 16:39
  • I'm sorry, I haven't check it - it did not work for me either. But I have found the solution: you should change `result = tree.query_ball_point(pB,0.100)` to `result = tree.query_ball_point(pB[0],0.100)`, and then you can check your result `if result == []: resbool = 0 else: resbool = 1`. Now it should work. This works for my python.2.7.15 installed with Anaconda. – Karol Frydrych Aug 17 '18 at 08:43