[aligner] Deal with possible singleton value for KDTree
authorVincent Michel <vincent.michel@logilab.fr>
Fri, 15 Feb 2013 10:13:36 +0100
changeset 219 50a3abe0c19a
parent 181 3c3ae9a236c7
child 220 3365a267b308
[aligner] Deal with possible singleton value for KDTree
aligner.py
dataio.py
--- a/aligner.py	Tue Dec 18 14:47:23 2012 +0100
+++ b/aligner.py	Fri Feb 15 10:13:36 2013 +0100
@@ -46,9 +46,16 @@
     """
     #If an element is None (missing), use instead the identity element.
     #The identity element is defined as the 0-vector
-    idelement = tuple([0 for _ in xrange(len(alignset[0][indexes[0]]))])
-    aligntree  = KDTree([elt[indexes[0]] or idelement for elt in alignset])
-    targettree = KDTree([elt[indexes[1]] or idelement for elt in targetset])
+    firstelement = alignset[0][indexes[0]]
+    idsize = len(firstelement) if isinstance(firstelement, (tuple, list)) else 1
+    idelement = (0,) * idsize
+    # KDTree is expecting a two-dimensional array
+    if idsize == 1:
+        aligntree  = KDTree([(elt[indexes[0]],) or idelement for elt in alignset])
+        targettree = KDTree([(elt[indexes[1]],) or idelement for elt in targetset])
+    else:
+        aligntree  = KDTree([elt[indexes[0]] or idelement for elt in alignset])
+        targettree = KDTree([elt[indexes[1]] or idelement for elt in targetset])
     extraneighbours = aligntree.query_ball_tree(targettree, threshold)
     neighbours = []
     for ind in xrange(len(alignset)):
--- a/dataio.py	Tue Dec 18 14:47:23 2012 +0100
+++ b/dataio.py	Fri Feb 15 10:13:36 2013 +0100
@@ -77,11 +77,11 @@
         if not indexes:
             data = [autocasted(raw[label]['value']) for label in labels]
         else:
-            for ind in indexes:
+            for il, ind in enumerate(indexes):
                 if isinstance(ind, tuple):
                     data.append(tuple([autocasted(raw[labels[i]]['value']) for i in ind]))
                 else:
-                    data.append(autocasted(raw[labels[ind]]['value']))
+                    data.append(autocasted(raw[labels[il]]['value']))
         results.append(data)
     return results