Computing Nearest Neighbors of a Volume of Images


I have a 4D volume of image pixel data loaded into SciDB like so:
Image_Volume: < pixel: uint8 > [width=0:480,1000,0, height=0:320,1000,0, channel=0:2,1000,0, imagenum=0:100,1,0]

I need to compute the all-pairs nearest neighbors for all images across the imagenum dimension. Surely it seems like this is not possible natively in SciDB (using iquery) but rather should be done using SciDB-py wrapper by iterating over the individual images and computing the distance between the images (i’m using a simple sum of squared distance metric between each pixel location).

I can then assemble an [imagenum x imagenum] 2D array of image distances and run the k-nn UDO:

Anyone else have a better solution?