0

I am having different outputs on running same program again and again, in different environments(anaconda, python, 2.7, 3.5)

Can someone explain the behaviour of the traversal in quadtree.

I have used this library.

Take a look at code, and outputs.

from pyqtree import Index

class Dataset:
    def __init__(self,id,x,y):
        self.id = id
        self.bbox = (x,y,x,y)

spidex = Index(bbox=(0,0,80,80))

p = [[0]*4]*4
k = 0

i1 = i2 = 0
for i in range(10,81,20):
    for j in range(10,81,20):
        p[i1][i2] = Dataset(k,i,j)
        k += 1
        spidex.insert(p[i1][i2],(p[i1][i2]).bbox)
        i2 +=1
    i1+=1
    i2 = 0

o9 = (0,0,80,80)

matches = spidex.intersect(o9)

for i in matches:
    # print(str(i.id))
    print('p'+str(i.id)+' --> ('+str(i.bbox[0])+','+str(i.bbox[1])+')')

Outputs are different where I use first comment statement and second comment statement also.

Different outputs:

/usr/bin/python qt3.py
p6--> (30,50)
p10--> (50,50)
p3--> (10,70)
p11--> (50,70)
p14--> (70,50)
p7--> (30,70)
p0--> (10,10)
p13--> (70,30)
p4--> (30,10)
p8--> (50,10)
p1--> (10,30)
p5--> (30,30)
p12--> (70,10)
p15--> (70,70)
p9--> (50,30)
p2--> (10,50)

python2 qt3.py
p14 --> (70,50)
p6 --> (30,50)
p10 --> (50,50)
p3 --> (10,70)
p11 --> (50,70)
p7 --> (30,70)
p0 --> (10,10)
p13 --> (70,30)
p4 --> (30,10)
p15 --> (70,70)
p8 --> (50,10)
p1 --> (10,30)
p5 --> (30,30)
p12 --> (70,10)
p9 --> (50,30)
p2 --> (10,50)

There are more than two different outputs. Let me know if you are having deterministic behaviour. Kindly explain the behaviour otherwise.

Deepak Punjabi
  • 483
  • 1
  • 7
  • 17

1 Answers1

1

Both outputs are the same: only the order in which the matches are presented differ.

This is due to the way Pyqtree computes the intersection: it puts the results in a set, and sets are unordered.

(The docstring of intersect says:

Returns: - A list of inserted items whose bounding boxes intersect with the input bbox.

but it's really a set, not a list, that is used)

So, it's no surprise that the order changes with your Python version.
If you want a constant order, you should sort your matches.

Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
  • But I am using pyqtree intersection as merely to build an index in some logical manner. If I sort it, then it will be base on id, which is not what I want. I read that quadtrees are used in spatial indexing, isn't this how it is done? – Deepak Punjabi Apr 17 '17 at 17:57
  • If not on id, then you have to sort them on something else. So, you should define what the "logical manner" means for you, and sort accordingly, as there's no "natural" sorting order. – Thierry Lathuille Apr 17 '17 at 18:01