[aligner,dataio] Export the results writing to a independant function
authorSimon Chabot <simon.chabot@logilab.fr>
Tue, 13 Nov 2012 16:42:57 +0100
changeset 126 752233f1a0b3
parent 125 07fe131b44d0
child 127 8f51d6d0c3be
[aligner,dataio] Export the results writing to a independant function
aligner.py
dataio.py
demo.py
--- a/aligner.py	Tue Nov 13 16:41:31 2012 +0100
+++ b/aligner.py	Tue Nov 13 16:42:57 2012 +0100
@@ -15,12 +15,12 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 
-from os.path import exists as fileexists
 
 from scipy.spatial import KDTree
 from scipy.sparse import lil_matrix
 
 from alignment.minhashing import Minlsh
+from alignment.dataio import write_results
 import alignment.matrix as m
 
 
@@ -211,21 +211,7 @@
 
     # Write file if asked
     if resultfile:
-        openmode = 'a' if fileexists(resultfile) else 'w'
-        with open(resultfile, openmode) as fobj:
-            if openmode == 'w':
-                fobj.write('aligned;targetted;distance\n')
-            for aligned in matched:
-                for target, dist in matched[aligned]:
-                    alignid = ralignset[aligned][0]
-                    targetid = rtargetset[target][0]
-                    fobj.write('%s;%s;%s\n' %
-                        (alignid.encode('utf-8') if isinstance(alignid, basestring)
-                                                 else alignid,
-                         targetid.encode('utf-8') if isinstance(targetid, basestring)
-                                                  else targetid,
-                         dist
-                         ))
+        write_results(matched, alignset, targetset, resultfile)
 
     return mat, matched
 
--- a/dataio.py	Tue Nov 13 16:41:31 2012 +0100
+++ b/dataio.py	Tue Nov 13 16:42:57 2012 +0100
@@ -15,6 +15,8 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 
+from os.path import exists as fileexists
+
 import csv
 
 
@@ -107,3 +109,23 @@
 
         result.append(data)
     return result
+
+def write_results(matched, alignset, targetset, resultfile):
+    """ Given a matched dictionnay, an alignset and a targetset to the
+        resultfile
+    """
+    openmode = 'a' if fileexists(resultfile) else 'w'
+    with open(resultfile, openmode) as fobj:
+        if openmode == 'w':
+            fobj.write('aligned;targetted;distance\n')
+        for aligned in matched:
+            for target, dist in matched[aligned]:
+                alignid = alignset[aligned][0]
+                targetid = targetset[target][0]
+                fobj.write('%s;%s;%s\n' %
+                    (alignid.encode('utf-8') if isinstance(alignid, basestring)
+                                             else alignid,
+                     targetid.encode('utf-8') if isinstance(targetid, basestring)
+                                              else targetid,
+                     dist
+                     ))
--- a/demo.py	Tue Nov 13 16:41:31 2012 +0100
+++ b/demo.py	Tue Nov 13 16:42:57 2012 +0100
@@ -6,7 +6,7 @@
 import alignment.distances as d
 import alignment.normalize as n
 from alignment.aligner import align, subalign, findneighbours
-from alignment.dataio import parsefile, sparqlquery
+from alignment.dataio import parsefile, sparqlquery, write_results
 
 DEMODIR = path.dirname(__file__)
 
@@ -113,13 +113,13 @@
     print "Start computation"
     for ind, (alignid, targetid) in enumerate(neighbours):
         print '%3d' % ind, len(alignid), 'x', len(targetid)
-        m, b = subalign(alignset,   # The dataset to align
-                        targetset,  # The target dataset
-                        alignid,
-                        targetid,
-                        0.3,
-                        treatments)
-        #XXX Write b
+        mat, matched = subalign(alignset,   # The dataset to align
+                                targetset,  # The target dataset
+                                alignid,
+                                targetid,
+                                0.3,
+                                treatments)
+        write_results(matched, alignset, targetset, 'demo2_results')
 
 if __name__ == '__main__':
     import sys