[Matrix] Improvement of the matched() method
authorSimon Chabot <simon.chabot@logilab.fr>
Fri, 26 Oct 2012 09:49:07 +0200
changeset 51 212f03c7809c
parent 50 96d6d4a782ad
child 52 cf07e255bcd0
[Matrix] Improvement of the matched() method Don't run over all indices but ask directly to scipy for the wanted ones.
matrix.py
--- a/matrix.py	Fri Oct 26 09:54:16 2012 +0200
+++ b/matrix.py	Fri Oct 26 09:49:07 2012 +0200
@@ -116,13 +116,15 @@
         if normalized:
             cutoff *= self._maxdist
 
-        for i in xrange(self._matrix.shape[0]):
-            for j in xrange(self._matrix.shape[1]):
-                if self._matrix[i, j] <= cutoff:
-                    if normalized:
-                        match[i].append((j, self._matrix[i, j]/self._maxdist))
-                    else:
-                        match[i].append((j, self._matrix[i, j]))
+        ind = (self._matrix <= cutoff).nonzero()
+        indrow = ind[0].tolist()
+        indcol = ind[1].tolist()
+
+        for (i, j) in zip(indrow, indcol):
+            if normalized:
+                match[i].append((j, self._matrix[i, j]/self._maxdist))
+            else:
+                match[i].append((j, self._matrix[i, j]))
         return match
 
 def globalalignmentmatrix(items):