From 7be31d3386739df1dba36c8f5507836ca75aae2b Mon Sep 17 00:00:00 2001
From: R <rlaz@cs.rit.edu>
Date: Wed, 9 Mar 2022 21:42:20 -0500
Subject: [PATCH] Reformatted python using 'black' pretty printer

---
 src/SmGrConfMatrix.py | 1437 ++++++++++++---------
 src/compareTools.py   |  115 +-
 src/compileLabels.py  |   88 +-
 src/confHists.py      |  430 ++++---
 src/evallg.py         |  375 +++---
 src/lg.py             | 2789 ++++++++++++++++++++++-------------------
 src/lg2NE.py          |    6 +-
 src/lg2OR.py          |    6 +-
 src/lg2dot.py         |  965 ++++++++------
 src/lg2txt.py         |   82 +-
 src/lgfilter.py       |  165 +--
 src/lgio.py           |  120 +-
 src/mergeLG.py        |   31 +-
 src/metricDist.py     |   88 +-
 src/mml.py            |   43 +-
 src/settings.py       |    4 +-
 src/smallGraph.py     |  597 +++++----
 src/statlgdb.py       |  139 +-
 src/sumDiff.py        |  524 ++++----
 src/sumMetric.py      | 1045 +++++++++------
 src/testNewSeg.py     |   82 +-
 src/testlg.py         |  636 +++++-----
 22 files changed, 5567 insertions(+), 4200 deletions(-)

diff --git a/src/SmGrConfMatrix.py b/src/SmGrConfMatrix.py
index df6800d..7bcf8bc 100644
--- a/src/SmGrConfMatrix.py
+++ b/src/SmGrConfMatrix.py
@@ -1,7 +1,7 @@
 #################################################################
 # SmGrConfMatrix.py
 #
-# Structure confusion histograms. 
+# Structure confusion histograms.
 #
 # Author: H. Mouchere, June 2013
 # Copyright (c) 2013-2014 Richard Zanibbi and Harold Mouchere
@@ -12,598 +12,907 @@ import os.path as osp
 from operator import itemgetter
 
 # Set graph size.
-GRAPH_SIZE=100
+GRAPH_SIZE = 100
+
 
 class SmDict(object):
-	"""This is not a real dictionnary but it is like a dictionnary using only 
+    """This is not a real dictionnary but it is like a dictionnary using only 
 	the == operator (without hash) 
 	A value can be associate to a smallGraph. 
 	For efficiency, use an object as a value to avoid call to set()
 	It uses the isomorphism to know if 2 smallGraphs are the same."""
-	
-	def __init__(self,*args):
-		self.myitems = []
-	
-	def set(self, sg, value):
-		for i in range(len(self.myitems)):
-			if(sg == self.myitems[i][0]):
-				self.myitems[i][1] = value
-				return
-		self.myitems.append((sg,value))
-		
-	def get(self, sg, defaultType = object):
-		"""find the corresponding key and if not found add it with the default value"""
-		for i in range(len(self.myitems)):
-			if(sg == self.myitems[i][0]):
-				return self.myitems[i][1]
-		self.myitems.append((sg, defaultType()))
-		return self.myitems[-1][1]
-		
-	def __contains__(self,sg):
-		for i in range(len(self.myitems)):
-			if(sg == self.myitems[i][0]):
-				return True
-		return False
-
-	def getIter(self):
-		for p in self.myitems:
-			yield p
-	def __str__(self):
-		res = "{"
-		for (k,v) in self.myitems:
-			res = res + "(" + str(k) + ":" + str(v) + "),"
-		return res[:-1] + "}"
-	def toHTML(self, limit = 0):
-		html = '<table border="1">\n'
-		sortedList = sorted(self.myitems, key=lambda t:t[1].get(), reverse=True)
-		for (g,v) in sortedList:
-			if (v.get() >  limit):
-					html = html + '<tr><td>\n' + g.toSVG(100) + '</td>\n'
-					html = html + '<td>'+str(v)+'</td></tr>\n'
-		return html + '</table>\n'
-		
+
+    def __init__(self, *args):
+        self.myitems = []
+
+    def set(self, sg, value):
+        for i in range(len(self.myitems)):
+            if sg == self.myitems[i][0]:
+                self.myitems[i][1] = value
+                return
+        self.myitems.append((sg, value))
+
+    def get(self, sg, defaultType=object):
+        """find the corresponding key and if not found add it with the default value"""
+        for i in range(len(self.myitems)):
+            if sg == self.myitems[i][0]:
+                return self.myitems[i][1]
+        self.myitems.append((sg, defaultType()))
+        return self.myitems[-1][1]
+
+    def __contains__(self, sg):
+        for i in range(len(self.myitems)):
+            if sg == self.myitems[i][0]:
+                return True
+        return False
+
+    def getIter(self):
+        for p in self.myitems:
+            yield p
+
+    def __str__(self):
+        res = "{"
+        for (k, v) in self.myitems:
+            res = res + "(" + str(k) + ":" + str(v) + "),"
+        return res[:-1] + "}"
+
+    def toHTML(self, limit=0):
+        html = '<table border="1">\n'
+        sortedList = sorted(self.myitems, key=lambda t: t[1].get(), reverse=True)
+        for (g, v) in sortedList:
+            if v.get() > limit:
+                html = html + "<tr><td>\n" + g.toSVG(100) + "</td>\n"
+                html = html + "<td>" + str(v) + "</td></tr>\n"
+        return html + "</table>\n"
+
 
 class Counter(object):
-	""" just a small counter but embedded in an object
+    """ just a small counter but embedded in an object
 	Designed to be used in smDict
 	It can save a list of object by adding it as param in the 
 	incr() function"""
-	
-	def __init__(self,*args):
-		self.value = 0
-		self.list = []
-		if(len(args)> 0):
-			self.value = int(args[0])
-			if (len(args) == 2):
-				self.list = args[1]
-	def incr(self,elem=None):
-		self.value = self.value +1
-		if elem != None:
-			self.list.append(elem)
-		else:
-			self.list = [ elem ]
-	def get(self):
-		return self.value
-	def set(self,v):
-		self.value = int(v)
-	def getList(self,unique = True):
-		if unique:
-			return list(set(self.list))
-		else:
-			return self.list
-	def add(self,c2):
-		return Counter(self.value + c2.value, self.list + c2.list)
-
-	def __add__(self,c2):
-		return Counter(self.value + c2.value, self.list + c2.list)
-
-	def __str__(self):
-		return str(self.value)
-	def __int__(self):
-		return self.value
-	
-	
-	
-class ConfMatrix(object):
 
-        def __init__(self,*args):
-                self.mat = SmDict()
+    def __init__(self, *args):
+        self.value = 0
+        self.list = []
+        if len(args) > 0:
+            self.value = int(args[0])
+            if len(args) == 2:
+                self.list = args[1]
+
+    def incr(self, elem=None):
+        self.value = self.value + 1
+        if elem != None:
+            self.list.append(elem)
+        else:
+            self.list = [elem]
+
+    def get(self):
+        return self.value
+
+    def set(self, v):
+        self.value = int(v)
+
+    def getList(self, unique=True):
+        if unique:
+            return list(set(self.list))
+        else:
+            return self.list
+
+    def add(self, c2):
+        return Counter(self.value + c2.value, self.list + c2.list)
+
+    def __add__(self, c2):
+        return Counter(self.value + c2.value, self.list + c2.list)
+
+    def __str__(self):
+        return str(self.value)
 
-        def size(self):
-                return len(self.mat.myitems)
+    def __int__(self):
+        return self.value
 
-        def incr(self, row, column, elem=None):
-                """ add 1 (one) to the counter indexed by row and column
+
+class ConfMatrix(object):
+    def __init__(self, *args):
+        self.mat = SmDict()
+
+    def size(self):
+        return len(self.mat.myitems)
+
+    def incr(self, row, column, elem=None):
+        """ add 1 (one) to the counter indexed by row and column
                 an object can be added in the attached list"""
-                self.mat.get(row, SmDict).get(column,Counter).incr(elem)
-        def __str__(self):
-                return str(self.mat)
-
-        def errorCount(self):
-                count = 0
-                for (obj,smDict) in self.mat.getIter():
-                        count += sum([v.get() for (_,v) in smDict.getIter()])
-                        #nbE = Counter()
-                        #for (_,c) in errmat.getIter():
-                        #	nbE = nbE + sum([v for (_,v) in c.getIter()], Counter())
-                        #count += nbE.get()
-                return count
-
-        def toHTMLfull(self, outputStream):
-                i = 0
-                allErr = SmDict()
-                outputStream.write(' <table border="1"><tr>')
-                outputStream.write('<td></td>')
-                for (rowG,col) in self.mat.getIter():
-                        for (g,_) in col.getIter():
-                                c = allErr.get(g,Counter)
-                                if c.get() == 0:
-                                        c.set(i)
-                                        i = i+1
-                                        outputStream.write( '<th>' + g.toSVG(100) + '</th>')
-                outputStream.write('</tr>')
-                nbE = len(allErr.myitems)
-                for (rowG,col) in self.mat.getIter():
-                        i = 0
-                        outputStream.write('<tr><th>\n' + rowG.toSVG(100) + '</th>')
-                        for (g,v) in col.getIter():
-                                c = allErr.get(g)
-                                for empty in range(i,c.get()):
-                                        outputStream.write('   <td>0</td>')
-                                outputStream.write('   <td>' + str(v) + '</td>')
-                                i = c.get()
-                        for empty in range(i,nbE-1):
-                                outputStream.write('   <td>0</td>')
-                        outputStream.write('</tr>\n')
-                outputStream.write('</table>\n<p>')
-
-        @staticmethod
-        def create_gtHTML(rowG, col, nbE, limit=0, viewerURL="", gt_file=None,
-                          parentObject=0, targetObject=0, arrow=True, 
-                          img_dir='', dotpdf_dir=''):
-                """ write in the output stream the HTML code for this matrix and
+        self.mat.get(row, SmDict).get(column, Counter).incr(elem)
+
+    def __str__(self):
+        return str(self.mat)
+
+    def errorCount(self):
+        count = 0
+        for (obj, smDict) in self.mat.getIter():
+            count += sum([v.get() for (_, v) in smDict.getIter()])
+            # nbE = Counter()
+            # for (_,c) in errmat.getIter():
+            # 	nbE = nbE + sum([v for (_,v) in c.getIter()], Counter())
+            # count += nbE.get()
+        return count
+
+    def toHTMLfull(self, outputStream):
+        i = 0
+        allErr = SmDict()
+        outputStream.write(' <table border="1"><tr>')
+        outputStream.write("<td></td>")
+        for (rowG, col) in self.mat.getIter():
+            for (g, _) in col.getIter():
+                c = allErr.get(g, Counter)
+                if c.get() == 0:
+                    c.set(i)
+                    i = i + 1
+                    outputStream.write("<th>" + g.toSVG(100) + "</th>")
+        outputStream.write("</tr>")
+        nbE = len(allErr.myitems)
+        for (rowG, col) in self.mat.getIter():
+            i = 0
+            outputStream.write("<tr><th>\n" + rowG.toSVG(100) + "</th>")
+            for (g, v) in col.getIter():
+                c = allErr.get(g)
+                for empty in range(i, c.get()):
+                    outputStream.write("   <td>0</td>")
+                outputStream.write("   <td>" + str(v) + "</td>")
+                i = c.get()
+            for empty in range(i, nbE - 1):
+                outputStream.write("   <td>0</td>")
+            outputStream.write("</tr>\n")
+        outputStream.write("</table>\n<p>")
+
+    @staticmethod
+    def create_gtHTML(
+        rowG,
+        col,
+        nbE,
+        limit=0,
+        viewerURL="",
+        gt_file=None,
+        parentObject=0,
+        targetObject=0,
+        arrow=True,
+        img_dir="",
+        dotpdf_dir="",
+    ):
+        """ write in the output stream the HTML code for this matrix and
                 use the ConfMatrix.toHTML to write the submatrices.
                 The list of files with error is prefixed with the param viewerURL
                 in the href attribute. """
-                htmlStream = open(osp.join("confHist_outputs", gt_file), 'w')
-                htmlStream.write('<meta charset="UTF-8">\n<html xmlns="http://www.w3.org/1999/xhtml">\n')  
-                htmlStream.write('<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>\n')
-                htmlStream.write('<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.0.0/FileSaver.min.js"></script>\n')
-                htmlStream.write('<head>\n')
-                htmlStream.write("<script src=\"../js/functions.js\"></script>\n")
-                htmlStream.write("<link rel=\"stylesheet\" href=\"../css/style.css\">\n")
-                htmlStream.write('</head>\n\n')
-                htmlStream.write("<font face=\"helvetica,arial,sans-serif\">")
-  
-                htmlStream.write("<h2>Primitive Error analysis and details</h2>")
-                htmlStream.write(' <table><tr>')
-                htmlStream.write('<td><h3>Primitive Target: </h3></td>')
-                htmlStream.write('<td valign="top">\n')
-                htmlStream.write(str(nbE) + " errors<br><br>")
-                htmlStream.write(rowG.toSVG(GRAPH_SIZE, arrow))
-                # htmlStream.write('</th><input type="checkbox" id="Obj"></th>')
-                htmlStream.write('</td></tr></table><br>')
-                htmlStream.write('<button type="button" font-size="12pt" id="savebutton">&nbsp;&nbsp;Save Selected Files&nbsp;&nbsp;</button>')
-                htmlStream.write('\n<hr>\n')
-	        # first count all error for each sub structure
-                graphSize=int(GRAPH_SIZE * 1.0)
-
-                htmlStream.write('<table valign="top">')
-                htmlStream.write('<tr><th><input type="checkbox" id="Obj' + str(parentObject) + 'Target' + \
-                                 str(targetObject) + '" name="fileList"></th><th>Errors</th></tr>\n')
+        htmlStream = open(osp.join("confHist_outputs", gt_file), "w")
+        htmlStream.write(
+            '<meta charset="UTF-8">\n<html xmlns="http://www.w3.org/1999/xhtml">\n'
+        )
+        htmlStream.write(
+            '<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>\n'
+        )
+        htmlStream.write(
+            '<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.0.0/FileSaver.min.js"></script>\n'
+        )
+        htmlStream.write("<head>\n")
+        htmlStream.write('<script src="../js/functions.js"></script>\n')
+        htmlStream.write('<link rel="stylesheet" href="../css/style.css">\n')
+        htmlStream.write("</head>\n\n")
+        htmlStream.write('<font face="helvetica,arial,sans-serif">')
+
+        htmlStream.write("<h2>Primitive Error analysis and details</h2>")
+        htmlStream.write(" <table><tr>")
+        htmlStream.write("<td><h3>Primitive Target: </h3></td>")
+        htmlStream.write('<td valign="top">\n')
+        htmlStream.write(str(nbE) + " errors<br><br>")
+        htmlStream.write(rowG.toSVG(GRAPH_SIZE, arrow))
+        # htmlStream.write('</th><input type="checkbox" id="Obj"></th>')
+        htmlStream.write("</td></tr></table><br>")
+        htmlStream.write(
+            '<button type="button" font-size="12pt" id="savebutton">&nbsp;&nbsp;Save Selected Files&nbsp;&nbsp;</button>'
+        )
+        htmlStream.write("\n<hr>\n")
+        # first count all error for each sub structure
+        graphSize = int(GRAPH_SIZE * 1.0)
+
+        htmlStream.write('<table valign="top">')
+        htmlStream.write(
+            '<tr><th><input type="checkbox" id="Obj'
+            + str(parentObject)
+            + "Target"
+            + str(targetObject)
+            + '" name="fileList"></th><th>Errors</th></tr>\n'
+        )
+        # ** Now show specific stroke-level confusions for target graph.
+        locHiddenErr = Counter()
+        arrow = False
+
+        # Critical: sort by frequency for specific confusion instances.
+        pairs = sorted(col.getIter(), key=lambda p: p[1].get(), reverse=True)
+        findex = 1
+        hindex = 1
+        for (g, v) in pairs:
+            if v.get() >= limit:
+                value = (" ").join(v.getList())
+                htmlStream.write(
+                    '<tr><th text-align="center" valign="top"><input type="checkbox" name="fileList" id="Obj'
+                    + str(parentObject)
+                    + "Target"
+                    + str(targetObject)
+                    + "Files"
+                    + str(findex)
+                    + '" class="fileCheck" value="'
+                    + value
+                    + '">'
+                    + str(findex)
+                    + "</th>"
+                )
+                htmlStream.write('<th valign="top">')
+                htmlStream.write(
+                    '<A NAME="#Obj'
+                    + str(parentObject)
+                    + "Target"
+                    + str(targetObject)
+                    + "Files"
+                    + str(findex)
+                    + '">'
+                    + str(v)
+                    + " errors</A><br><br>\n"
+                )
+                htmlStream.write(g.toSVG(graphSize, arrow))
+                htmlStream.write("</th>\n")
+                htmlStream.write('<td><table valign="top">\n')
+                htmlStream.write("<tr><th>Filename</th><th>Image</th><th>LG</th>\n")
+                for file in v.getList():
+                    htmlStream.write("</tr><tr>\n")
+                    htmlStream.write("<td>" + file + "</td>")
+                    imgfile = file[:-3] + ".PNG"
+                    dotfile = file[:-3] + ".pdf"
+                    htmlStream.write(
+                        '<td><img src="'
+                        + osp.join(img_dir, imgfile)
+                        + '" style="width:auto; height:40%;"></td>'
+                    )
+                    htmlStream.write(
+                        '<td><iframe src="'
+                        + osp.join(dotpdf_dir, dotfile)
+                        + '" style="width:1000px; height:275px;"></iframe></td>'
+                    )
+                htmlStream.write("</table></td></tr>")
+                hindex += 1
+            else:
+                # Tricky; this counter object __str__ gives its list
+                # size, but '+' concatenates entries.
+                locHiddenErr = locHiddenErr + v
+
+            findex += 1
+
+        # Write only one entry for 'other' errors. Do this
+        # Whenever a target/row is shown for completeness.
+        if len(locHiddenErr.getList()) > 0:
+            value = (" ").join(locHiddenErr.getList())
+            htmlStream.write(
+                '<tr><th valign="top">'
+                + '<input type="checkbox" id="Obj'
+                + str(parentObject)
+                + "Target"
+                + str(targetObject)
+                + "Files"
+                + str(findex)
+                + '" name="fileList" class="fileCheck" value="'
+                + value
+                + '">'
+                + str(hindex)
+                + "</th>"
+            )
+            htmlStream.write(
+                '<th valign="top">' + str(locHiddenErr) + " errors<br><br>"
+            )
+            htmlStream.write(
+                "<center><font size=2>Other<br>Errors</font></center></th></tr>"
+            )
+
+        # htmlStream.write('</table></td></tr>')
+        # End this entry.
+        htmlStream.write("</html>")
+        htmlStream.close()
+
+    def toHTML(
+        self,
+        outputStream,
+        limit=0,
+        targetLimit=1,
+        viewerURL="",
+        redn=[],
+        rede=[],
+        parentObject=0,
+        gt_file="",
+        fileList=None,
+        primitive=False,
+        img_dir="",
+        dotpdf_dir="",
+    ):
+        """ write in the output stream the HTML code for this matrix and
+                return a Counter object with
+                the number of unshown errors and the list of hidden elements
+                The list of files with errors is prefixed with the param viewerURL
+                in the href attribute."""
+
+        if primitive:
+            gt_fileList = osp.join("CH_" + fileList + "_gt_htmls")
+            # dotpdf_dir = "../../Results_" + fileList.split('_vs')[0] + "/errorGraphs/pdf"
+            # dotpdf_dir = "../../Results_" + fileList.split('_vs')[0] + "/errorGraphs/png"
+        arrow = True
+        hiddenErr = Counter()
+        sortedList = []
+        # first count all error for each sub structure
+        for (rowG, col) in self.mat.getIter():
+            nbE = sum([v for (_, v) in col.getIter()], Counter())
+            sortedList.append((rowG, col, nbE))
+
+        # Show each confused stroke-level graph target in decreasing order of frequency.
+        sortedList = sorted(sortedList, key=lambda t: t[2].get(), reverse=True)
+        tindex = 1
+        graphSize = int(GRAPH_SIZE * 1.0)
+        outputStream.write('<table valign="top">')
+        outputStream.write(
+            "<tr><th>"
+            + '<input type="checkbox" id="Obj'
+            + str(parentObject)
+            + '"></th><th>Targets</th></tr>\n'
+        )
+        for (rowG, col, nbE) in sortedList:
+            if int(nbE) >= limit:
+
+                outputStream.write(
+                    '<tr><th text-align="center" valign="top"><input type="checkbox" id="Obj'
+                    + str(parentObject)
+                    + "Target"
+                    + str(tindex)
+                    + '" name="fileList">'
+                )
+                outputStream.write(str(tindex) + "</th><th>")
+                if primitive:
+                    gt_file = osp.join(gt_fileList, "Primitive" + str(tindex) + ".html")
+                    ConfMatrix.create_gtHTML(
+                        rowG,
+                        col,
+                        nbE,
+                        limit=limit,
+                        viewerURL=viewerURL,
+                        gt_file=gt_file,
+                        parentObject=parentObject,
+                        targetObject=tindex,
+                        dotpdf_dir=dotpdf_dir,
+                        img_dir=img_dir,
+                    )
+                    outputStream.write(
+                        '<A HREF="'
+                        + gt_file
+                        + '">'
+                        + str(nbE)
+                        + " errors</A><br><br>\n"
+                    )
+                else:
+                    outputStream.write(
+                        '<A HREF="'
+                        + gt_file
+                        + "#Obj"
+                        + str(parentObject)
+                        + "Target"
+                        + str(tindex)
+                        + '">'
+                        + str(nbE)
+                        + " errors</A><br><br>\n"
+                    )
+                outputStream.write(rowG.toSVG(graphSize, arrow))
+                outputStream.write("\n</th>")
+
                 # ** Now show specific stroke-level confusions for target graph.
                 locHiddenErr = Counter()
                 arrow = False
 
                 # Critical: sort by frequency for specific confusion instances.
-                pairs = sorted(col.getIter(), key=lambda p:p[1].get(), reverse=True)
+                pairs = sorted(col.getIter(), key=lambda p: p[1].get(), reverse=True)
                 findex = 1
-                hindex = 1
-                for (g,v) in pairs:
-                        if v.get() >= limit:
-                                value = (" ").join(v.getList())
-                                htmlStream.write('<tr><th text-align="center" valign="top"><input type="checkbox" name="fileList" id="Obj' + str(parentObject) + 'Target' + str(targetObject) + 'Files' + str(findex) + '" class="fileCheck" value="' \
-                                                + value + '">' + str(findex) + '</th>')
-                                htmlStream.write('<th valign="top">')
-                                htmlStream.write('<A NAME=\"#Obj' + str(parentObject) + 'Target' + str(targetObject) +
-                                                 'Files' + str(findex) + '\">' + str(v) + ' errors</A><br><br>\n')
-                                htmlStream.write(g.toSVG(graphSize,arrow))
-                                htmlStream.write('</th>\n')
-                                htmlStream.write('<td><table valign="top">\n')
-                                htmlStream.write('<tr><th>Filename</th><th>Image</th><th>LG</th>\n')
-                                for file in v.getList():
-                                    htmlStream.write('</tr><tr>\n')
-                                    htmlStream.write('<td>' + file + '</td>')
-                                    imgfile = file[:-3] + '.PNG'
-                                    dotfile = file[:-3] + '.pdf'
-                                    htmlStream.write('<td><img src="' + osp.join(img_dir, imgfile) + '" style="width:auto; height:40%;"></td>')
-                                    htmlStream.write('<td><iframe src="' + osp.join(dotpdf_dir, dotfile) + '" style="width:1000px; height:275px;"></iframe></td>')
-                                htmlStream.write('</table></td></tr>')
-                                hindex += 1
-                        else:
-                                # Tricky; this counter object __str__ gives its list
-                                # size, but '+' concatenates entries.
-                                locHiddenErr = locHiddenErr + v
-                        
-                        findex += 1
-                
+                for (g, v) in pairs:
+                    if v.get() >= limit:
+                        value = (" ").join(v.getList())
+                        outputStream.write(
+                            '<td><input type="checkbox" name="fileList" id="Obj'
+                            + str(parentObject)
+                            + "Target"
+                            + str(tindex)
+                            + "Files"
+                            + str(findex)
+                            + '" class="fileCheck" value="'
+                            + value
+                            + '">'
+                        )
+                        outputStream.write(
+                            '<A HREF="'
+                            + gt_file
+                            + "#Obj"
+                            + str(parentObject)
+                            + "Target"
+                            + str(tindex)
+                            + "Files"
+                            + str(findex)
+                            + '">'
+                            + str(v)
+                            + " errors</A><br><br>\n"
+                        )
+                        outputStream.write(g.toSVG(graphSize, arrow))
+                        outputStream.write("</td>\n")
+                    else:
+                        # Tricky; this counter object __str__ gives its list
+                        # size, but '+' concatenates entries.
+                        locHiddenErr = locHiddenErr + v
+
+                    findex += 1
+
                 # Write only one entry for 'other' errors. Do this
                 # Whenever a target/row is shown for completeness.
                 if len(locHiddenErr.getList()) > 0:
-                        value = (" ").join(locHiddenErr.getList())
-                        htmlStream.write('<tr><th valign="top">' \
-                                        + '<input type="checkbox" id="Obj' + str(parentObject) + 'Target' + str(targetObject) + 'Files' + str(findex) + '" name="fileList" class="fileCheck" value="' \
-                                        + value + '">' + str(hindex) + '</th>')
-                        htmlStream.write('<th valign="top">' +str(locHiddenErr) + ' errors<br><br>')
-                        htmlStream.write('<center><font size=2>Other<br>Errors</font></center></th></tr>')
-
-                # htmlStream.write('</table></td></tr>')
-                # End this entry.
-                htmlStream.write('</html>')
-                htmlStream.close()
-
-        def toHTML(self, outputStream, limit = 0, targetLimit = 1, viewerURL="", 
-                   redn = [], rede = [], parentObject=0, gt_file='', fileList=None, 
-                   primitive=False, img_dir='', dotpdf_dir=''):
-                """ write in the output stream the HTML code for this matrix and
-                return a Counter object with
-                the number of unshown errors and the list of hidden elements
-                The list of files with errors is prefixed with the param viewerURL
-                in the href attribute."""
-                
-                if primitive:
-                    gt_fileList = osp.join("CH_" + fileList + "_gt_htmls")
-                    # dotpdf_dir = "../../Results_" + fileList.split('_vs')[0] + "/errorGraphs/pdf"
-                    # dotpdf_dir = "../../Results_" + fileList.split('_vs')[0] + "/errorGraphs/png"
-                arrow = True
-                hiddenErr = Counter()
-                sortedList = []
-# first count all error for each sub structure
-                for (rowG,col) in self.mat.getIter():
-                        nbE = sum([v for (_,v) in col.getIter()],Counter())
-                        sortedList.append((rowG,col,nbE))
-                
-                # Show each confused stroke-level graph target in decreasing order of frequency.
-                sortedList = sorted(sortedList, key=lambda t:t[2].get(), reverse=True)
-                tindex = 1
-                graphSize=int(GRAPH_SIZE * 1.0)
-                outputStream.write('<table valign="top">')
-                outputStream.write('<tr><th>' \
-                + '<input type="checkbox" id="Obj' + str(parentObject) + '"></th><th>Targets</th></tr>\n')
-                for (rowG,col,nbE) in sortedList:
-                        if int(nbE) >= limit:
-                                
-                                outputStream.write('<tr><th text-align="center" valign="top"><input type="checkbox" id="Obj' \
-                                                    + str(parentObject) + 'Target' + str(tindex) + '" name="fileList">')
-                                outputStream.write(str(tindex) + '</th><th>')
-                                if primitive:
-                                    gt_file = osp.join(gt_fileList, "Primitive" + str(tindex) + ".html")
-                                    ConfMatrix.create_gtHTML(rowG, col, nbE, limit=limit, viewerURL=viewerURL, 
-                                                             gt_file=gt_file, parentObject=parentObject, 
-                                                             targetObject=tindex, dotpdf_dir=dotpdf_dir,
-                                                             img_dir=img_dir)
-                                    outputStream.write('<A HREF=\"' + gt_file + '\">' + str(nbE) + ' errors</A><br><br>\n')
-                                else:
-                                    outputStream.write('<A HREF=\"' + gt_file + '#Obj' + str(parentObject) + 'Target' \
-                                                      + str(tindex) + '\">' + str(nbE) + ' errors</A><br><br>\n')
-                                outputStream.write(rowG.toSVG(graphSize,arrow))
-                                outputStream.write('\n</th>')
-                                
-                                # ** Now show specific stroke-level confusions for target graph.
-                                locHiddenErr = Counter()
-                                arrow = False
-
-                                # Critical: sort by frequency for specific confusion instances.
-                                pairs = sorted(col.getIter(), key=lambda p:p[1].get(), reverse=True)
-                                findex = 1
-                                for (g,v) in pairs:
-                                        if v.get() >= limit:
-                                                value = (" ").join(v.getList())
-                                                outputStream.write('<td><input type="checkbox" name="fileList" id="Obj' + str(parentObject) + 'Target' + str(tindex) + 'Files' + str(findex) + '" class="fileCheck" value="' \
-                                                                + value + '">')
-                                                outputStream.write('<A HREF=\"' + gt_file + '#Obj' + str(parentObject) + 'Target' + str(tindex) +
-                                                                   'Files' + str(findex) + '\">' + str(v) + ' errors</A><br><br>\n')
-                                                outputStream.write(g.toSVG(graphSize,arrow))
-                                                outputStream.write('</td>\n')
-                                        else:
-                                                # Tricky; this counter object __str__ gives its list
-                                                # size, but '+' concatenates entries.
-                                                locHiddenErr = locHiddenErr + v
-                                        
-                                        findex += 1
-                                
-                                # Write only one entry for 'other' errors. Do this
-                                # Whenever a target/row is shown for completeness.
-                                if len(locHiddenErr.getList()) > 0:
-                                        value = (" ").join(locHiddenErr.getList())
-                                        outputStream.write('<td valign="top">' \
-                                                        + '<input type="checkbox" id="Obj' + str(parentObject) + 'Target' + str(tindex) + 'Files' + str(findex) + '" name="fileList" class="fileCheck" value="' \
-                                                        + value + '">' + str(locHiddenErr) + ' errors<br><br>')
-                                        outputStream.write('<center><font size=2>Other<br>Errors</font></center></td>')
-
-                                outputStream.write('</tr>')
-                        
-                        elif int(str(nbE)) >= targetLimit:
-                                
-                                # This is for errors less frequent than the threshold
-                                # for different target stroke-level graphs.
-                                locHiddenErr = Counter()
-                                for (g,v) in col.getIter():
-                                        locHiddenErr = locHiddenErr + v
-
-                                # Show all ground truth target errors with counts, so that
-                                # object-level count is sum of confused stroke-level graphs.
-                                value = (" ").join(locHiddenErr.getList())
-                                outputStream.write('<tr><th valign="top"><input type="checkbox" id="Obj' + str(parentObject) + 'Target' + str(tindex) + '"> ' + str(tindex) + '</th>')
-                                outputStream.write('<th>' + str(locHiddenErr) + ' errors<br><br>\n')
-                                outputStream.write(rowG.toSVG(graphSize,arrow))
-                                outputStream.write('</th><td valign=\"top\"><input type="checkbox" class="fileCheck" id="Obj' + str(parentObject) + 'Target' + str(tindex) + 'Files1" value="' + value + '">' +
-                                        str(locHiddenErr) + ' errors<br><br>')
-                                outputStream.write('<center><font size=2>Other<br>Errors</font></center></td></tr>')
-                                
-                        else:
-                                hiddenErr = hiddenErr + nbE
-
-                        # Increment counter for stroke-level targets.
-                        tindex += 1
-
-# End this entry.
-                outputStream.write('</table>\n')
-                        
-                return hiddenErr
-		
+                    value = (" ").join(locHiddenErr.getList())
+                    outputStream.write(
+                        '<td valign="top">'
+                        + '<input type="checkbox" id="Obj'
+                        + str(parentObject)
+                        + "Target"
+                        + str(tindex)
+                        + "Files"
+                        + str(findex)
+                        + '" name="fileList" class="fileCheck" value="'
+                        + value
+                        + '">'
+                        + str(locHiddenErr)
+                        + " errors<br><br>"
+                    )
+                    outputStream.write(
+                        "<center><font size=2>Other<br>Errors</font></center></td>"
+                    )
+
+                outputStream.write("</tr>")
+
+            elif int(str(nbE)) >= targetLimit:
+
+                # This is for errors less frequent than the threshold
+                # for different target stroke-level graphs.
+                locHiddenErr = Counter()
+                for (g, v) in col.getIter():
+                    locHiddenErr = locHiddenErr + v
+
+                # Show all ground truth target errors with counts, so that
+                # object-level count is sum of confused stroke-level graphs.
+                value = (" ").join(locHiddenErr.getList())
+                outputStream.write(
+                    '<tr><th valign="top"><input type="checkbox" id="Obj'
+                    + str(parentObject)
+                    + "Target"
+                    + str(tindex)
+                    + '"> '
+                    + str(tindex)
+                    + "</th>"
+                )
+                outputStream.write("<th>" + str(locHiddenErr) + " errors<br><br>\n")
+                outputStream.write(rowG.toSVG(graphSize, arrow))
+                outputStream.write(
+                    '</th><td valign="top"><input type="checkbox" class="fileCheck" id="Obj'
+                    + str(parentObject)
+                    + "Target"
+                    + str(tindex)
+                    + 'Files1" value="'
+                    + value
+                    + '">'
+                    + str(locHiddenErr)
+                    + " errors<br><br>"
+                )
+                outputStream.write(
+                    "<center><font size=2>Other<br>Errors</font></center></td></tr>"
+                )
+
+            else:
+                hiddenErr = hiddenErr + nbE
+
+            # Increment counter for stroke-level targets.
+            tindex += 1
+
+        # End this entry.
+        outputStream.write("</table>\n")
+
+        return hiddenErr
+
 
 class ConfMatrixObject(object):
-	
-        def __init__(self,*args):
-                self.mat = SmDict()
-
-        def size(self):
-                return len(self.mat.myitems)
-
-        def incr(self, obj, row, column,elem=None):
-                self.mat.get(obj, ConfMatrix).incr(row, column,elem)
-
-        def __str__(self):
-                return str(self.mat)
-
-        def errorCount(self):
-                count = 0
-                for (obj,errmat) in self.mat.getIter():
-                        nbE = Counter()
-                        for (_,c) in errmat.mat.getIter():
-                                nbE = nbE + sum([v for (_,v) in c.getIter()], Counter())
-                        count += nbE.get()
-                return count
-
-        @staticmethod
-        def add_err_HTML(files, count, img_dir, dotpdf_dir, additional_file):
-                htmlStream = open(osp.join("confHist_outputs", additional_file), 'w')
-                htmlStream.write('<meta charset="UTF-8">\n<html xmlns="http://www.w3.org/1999/xhtml">\n') 
-                htmlStream.write('<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>\n')
-                htmlStream.write('<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.0.0/FileSaver.min.js"></script>\n')
-                htmlStream.write('<head>\n')
-                htmlStream.write("<script src=\"../js/functions.js\"></script>\n")
-                htmlStream.write("<link rel=\"stylesheet\" href=\"../css/style.css\">\n")
-                htmlStream.write('</head>\n\n')
-                htmlStream.write("<font face=\"helvetica,arial,sans-serif\">")
-                htmlStream.write("<h2>Additional Errors: " + count + "</h2>")
-                htmlStream.write('<table valign="top">\n')
-                htmlStream.write('<tr><th>Filename</th><th>Image</th><th>LG</th></tr>\n')
-                for file in files:
-                    htmlStream.write('<tr>\n')
-                    htmlStream.write('<td>' + file + '</td>')
-                    imgfile = file[:-3] + '.PNG'
-                    dotfile = file[:-3] + '.pdf'
-                    htmlStream.write('<td><img src="' + osp.join(img_dir, imgfile) + '" style="width:auto; height:40%;"></td>')
-                    htmlStream.write('<td><iframe src="' + osp.join(dotpdf_dir, dotfile) + '" style="width:1000px; height:275px;"></iframe></td></tr>')
-                htmlStream.write('</table>\n')
-                htmlStream.write('</html>')
-                htmlStream.close()
-
-
-        @staticmethod
-        def create_gtHTML(obj, errmat, nbE, limit=0, targetLimit =1,
-                          viewerURL="", redn=[], rede=[], gt_file=None,
-                          parentObject=0, arrow=True, img_dir='',
-                          dotpdf_dir=''):
-                htmlStream = open(osp.join("confHist_outputs", gt_file), 'w')
-                htmlStream.write('<meta charset="UTF-8">\n<html xmlns="http://www.w3.org/1999/xhtml">\n') 
-                htmlStream.write('<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>\n')
-                htmlStream.write('<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.0.0/FileSaver.min.js"></script>\n')
-                htmlStream.write('<head>\n')
-                htmlStream.write("<script src=\"../js/functions.js\"></script>\n")
-                htmlStream.write("<link rel=\"stylesheet\" href=\"../css/style.css\">\n")
-                htmlStream.write('</head>\n\n')
-                htmlStream.write("<font face=\"helvetica,arial,sans-serif\">")
-                htmlStream.write("<h2>Object Error analysis and details</h2>")
-                htmlStream.write(' <table><tr>')
-                htmlStream.write('<td><h3>Object Target: </h3></td>')
-                htmlStream.write('<td valign="top">\n')
-                htmlStream.write(str(nbE) + " errors<br><br>")
-                htmlStream.write(obj.toSVG(GRAPH_SIZE, arrow,'box'))
-                # htmlStream.write('</th><input type="checkbox" id="Obj"></th>')
-                htmlStream.write('</td></tr></table><br>')
-                htmlStream.write('<button type="button" font-size="12pt" id="savebutton">&nbsp;&nbsp;Save Selected Files&nbsp;&nbsp;</button>')
-                htmlStream.write('\n<hr>\n')
-                htmlStream.write('<table><tr><th><input type="checkbox" id="Obj' +
-                                 str(parentObject) +'"></th><th>Primitive \
-                                 Targets</th><th>Primitive level Errors</th>')
-                htmlStream.write('</tr>')
-  
-                hiddenErr = Counter()  
-                sortedList = []  
-	        # first count all error for each sub structure
-                for (rowG,col) in errmat.mat.getIter():
-                        nbE = sum([v for (_,v) in col.getIter()],Counter())
-                        sortedList.append((rowG,col,nbE))
-  
-# Show each confused stroke-level graph target in decreasing order of frequency.
-                sortedList = sorted(sortedList, key=lambda t:t[2].get(), reverse=True)
-                tindex = 1 
-                graphSize=int(GRAPH_SIZE * 1.0)
-# outputStream.write('<table valign="top">')
-                # htmlStream.write('<tr><th>')  \
-# + '<input type="checkbox" id="Obj' + str(parentObject) + '"></th><th>Targets</th></tr>\n')
-
-                for (rowG,col,nbE) in sortedList:
-                        if int(nbE) >= limit:
-                                htmlStream.write('<tr><th valign="top">' \
-                                            + str(tindex) + '</th><th>')
-                                htmlStream.write('<A NAME=\"#Obj' + str(parentObject) + 'Target' \
-                                                   + str(tindex) + '\">' + str(nbE) + ' errors</A><br><br>\n')
-                                htmlStream.write(rowG.toSVG(graphSize,arrow))
-                                htmlStream.write('\n</th>')
-                                
-                                htmlStream.write('<td><table valign="top">')
-                                htmlStream.write('<tr><th><input type="checkbox" id="Obj' + str(parentObject) + 'Target' + \
-                                                 str(tindex) + '" name="fileList"></th><th>Errors</th></tr>\n')
-                                # ** Now show specific stroke-level confusions for target graph.
-                                locHiddenErr = Counter()
-                                arrow = False
-
-                                # Critical: sort by frequency for specific confusion instances.
-                                pairs = sorted(col.getIter(), key=lambda p:p[1].get(), reverse=True)
-                                findex = 1
-                                hindex = 1
-                                for (g,v) in pairs:
-                                        if v.get() >= limit:
-                                                value = (" ").join(v.getList())
-                                                htmlStream.write('<tr><th text-align="center" valign="top"><input type="checkbox" name="fileList" id="Obj' + str(parentObject) + 'Target' + str(tindex) + 'Files' + str(findex) + '" class="fileCheck" value="' \
-                                                                + value + '">' + str(findex) + '</th>')
-                                                htmlStream.write('<th valign="top">')
-                                                htmlStream.write('<A NAME=\"#Obj' + str(parentObject) + 'Target' + str(tindex) +
-                                                                 'Files' + str(findex) + '\">' + str(v) + ' errors</A><br><br>\n')
-                                                htmlStream.write(g.toSVG(graphSize,arrow))
-                                                htmlStream.write('</th>\n')
-                                                htmlStream.write('<td><table valign="top">\n')
-                                                htmlStream.write('<tr><th>Filename</th><th>Image</th><th>LG</th>\n')
-                                                for file in v.getList():
-                                                    htmlStream.write('</tr><tr>\n')
-                                                    htmlStream.write('<td>' + file + '</td>')
-                                                    imgfile = file[:-3] + '.PNG'
-                                                    dotfile = file[:-3] + '.pdf'
-                                                    htmlStream.write('<td><img src="' + osp.join(img_dir, imgfile) + '" style="width:auto; height:40%;"></td>')
-                                                    htmlStream.write('<td><iframe src="' + osp.join(dotpdf_dir, dotfile) + '" style="width:1000px; height:275px;"></iframe></td>')
-                                                htmlStream.write('</table></td>')
-                                                hindex += 1
-                                        else:
-                                                # Tricky; this counter object __str__ gives its list
-                                                # size, but '+' concatenates entries.
-                                                locHiddenErr = locHiddenErr + v
-                                        
-                                        findex += 1
-                                
-                                htmlStream.write('</tr>')
-                                # Write only one entry for 'other' errors. Do this
-                                # Whenever a target/row is shown for completeness.
-                                if len(locHiddenErr.getList()) > 0:
-                                        value = (" ").join(locHiddenErr.getList())
-                                        htmlStream.write('<tr><th valign="top">' \
-                                                        + '<input type="checkbox" id="Obj' + str(parentObject) + 'Target' + str(tindex) + 'Files' + str(findex) + '" name="fileList" class="fileCheck" value="' \
-                                                        + value + '">' + str(hindex) + '</th>')
-                                        htmlStream.write('<th valign="top">' +str(locHiddenErr) + ' errors<br><br>')
-                                        htmlStream.write('<center><font size=2>Other<br>Errors</font></center></th></tr>')
-
-                        
-                        elif int(str(nbE)) >= targetLimit:
-                                
-                                htmlStream.write('<tr><th valign="top">' \
-                                            + str(tindex) + '</th>')
-                                # This is for errors less frequent than the threshold
-                                # for different target stroke-level graphs.
-                                locHiddenErr = Counter()
-                                for (g,v) in col.getIter():
-                                        locHiddenErr = locHiddenErr + v
-
-                                # Show all ground truth target errors with counts, so that
-                                # object-level count is sum of confused stroke-level graphs.
-                                value = (" ").join(locHiddenErr.getList())
-                                htmlStream.write('<th>' + str(nbE) + ' errors<br><br>\n')
-                                htmlStream.write(rowG.toSVG(graphSize,arrow))
-                                htmlStream.write('\n</th>')
-                                htmlStream.write('<td><table valign="top">')
-                                htmlStream.write('<tr><th><input type="checkbox" id="Obj' + str(parentObject) + 'Target' + \
-                                                 str(tindex) + '" name="fileList"></th><th>Errors</th></tr>\n')
-                                htmlStream.write('<tr><th valign=\"top\">' \
-                                                 + '<input type="checkbox" class="fileCheck" id="Obj' + str(parentObject) + 'Target' + str(tindex) + 'Files1" value="' + value + '">' +
-                                                 str(1) + '</th>')
-                                htmlStream.write('<th valign="top">' +str(locHiddenErr) + ' errors<br><br>')
-                                htmlStream.write('<center><font size=2>Other<br>Errors</font></center></th></tr>')
-                                
-                        else:
-                                hiddenErr = hiddenErr + nbE
-
-                        # Increment counter for stroke-level targets.
-                        tindex += 1
-                
-                        htmlStream.write('</table></td></tr>')
-                # End this entry.
-                htmlStream.write('</table><p><b>Additional errors:</b> ')
-                viewStr = ""
-                if(len(hiddenErr.getList())> 0):
-                        viewStr = '<a href="' + viewerURL+ (",").join(hiddenErr.getList())+'"> View </a>'
-                htmlStream.write(str(hiddenErr) + viewStr + '</p>')
-                htmlStream.write('</html>')
-                htmlStream.close()
-
-        def toHTML(self, outputStream, limit = 0, viewerURL="", redn = {}, fileList=None, 
-                   img_dir='', dotpdf_dir=''):
-                """ write in the output stream the HTML code for this matrix and
+    def __init__(self, *args):
+        self.mat = SmDict()
+
+    def size(self):
+        return len(self.mat.myitems)
+
+    def incr(self, obj, row, column, elem=None):
+        self.mat.get(obj, ConfMatrix).incr(row, column, elem)
+
+    def __str__(self):
+        return str(self.mat)
+
+    def errorCount(self):
+        count = 0
+        for (obj, errmat) in self.mat.getIter():
+            nbE = Counter()
+            for (_, c) in errmat.mat.getIter():
+                nbE = nbE + sum([v for (_, v) in c.getIter()], Counter())
+            count += nbE.get()
+        return count
+
+    @staticmethod
+    def add_err_HTML(files, count, img_dir, dotpdf_dir, additional_file):
+        htmlStream = open(osp.join("confHist_outputs", additional_file), "w")
+        htmlStream.write(
+            '<meta charset="UTF-8">\n<html xmlns="http://www.w3.org/1999/xhtml">\n'
+        )
+        htmlStream.write(
+            '<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>\n'
+        )
+        htmlStream.write(
+            '<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.0.0/FileSaver.min.js"></script>\n'
+        )
+        htmlStream.write("<head>\n")
+        htmlStream.write('<script src="../js/functions.js"></script>\n')
+        htmlStream.write('<link rel="stylesheet" href="../css/style.css">\n')
+        htmlStream.write("</head>\n\n")
+        htmlStream.write('<font face="helvetica,arial,sans-serif">')
+        htmlStream.write("<h2>Additional Errors: " + count + "</h2>")
+        htmlStream.write('<table valign="top">\n')
+        htmlStream.write("<tr><th>Filename</th><th>Image</th><th>LG</th></tr>\n")
+        for file in files:
+            htmlStream.write("<tr>\n")
+            htmlStream.write("<td>" + file + "</td>")
+            imgfile = file[:-3] + ".PNG"
+            dotfile = file[:-3] + ".pdf"
+            htmlStream.write(
+                '<td><img src="'
+                + osp.join(img_dir, imgfile)
+                + '" style="width:auto; height:40%;"></td>'
+            )
+            htmlStream.write(
+                '<td><iframe src="'
+                + osp.join(dotpdf_dir, dotfile)
+                + '" style="width:1000px; height:275px;"></iframe></td></tr>'
+            )
+        htmlStream.write("</table>\n")
+        htmlStream.write("</html>")
+        htmlStream.close()
+
+    @staticmethod
+    def create_gtHTML(
+        obj,
+        errmat,
+        nbE,
+        limit=0,
+        targetLimit=1,
+        viewerURL="",
+        redn=[],
+        rede=[],
+        gt_file=None,
+        parentObject=0,
+        arrow=True,
+        img_dir="",
+        dotpdf_dir="",
+    ):
+        htmlStream = open(osp.join("confHist_outputs", gt_file), "w")
+        htmlStream.write(
+            '<meta charset="UTF-8">\n<html xmlns="http://www.w3.org/1999/xhtml">\n'
+        )
+        htmlStream.write(
+            '<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>\n'
+        )
+        htmlStream.write(
+            '<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.0.0/FileSaver.min.js"></script>\n'
+        )
+        htmlStream.write("<head>\n")
+        htmlStream.write('<script src="../js/functions.js"></script>\n')
+        htmlStream.write('<link rel="stylesheet" href="../css/style.css">\n')
+        htmlStream.write("</head>\n\n")
+        htmlStream.write('<font face="helvetica,arial,sans-serif">')
+        htmlStream.write("<h2>Object Error analysis and details</h2>")
+        htmlStream.write(" <table><tr>")
+        htmlStream.write("<td><h3>Object Target: </h3></td>")
+        htmlStream.write('<td valign="top">\n')
+        htmlStream.write(str(nbE) + " errors<br><br>")
+        htmlStream.write(obj.toSVG(GRAPH_SIZE, arrow, "box"))
+        # htmlStream.write('</th><input type="checkbox" id="Obj"></th>')
+        htmlStream.write("</td></tr></table><br>")
+        htmlStream.write(
+            '<button type="button" font-size="12pt" id="savebutton">&nbsp;&nbsp;Save Selected Files&nbsp;&nbsp;</button>'
+        )
+        htmlStream.write("\n<hr>\n")
+        htmlStream.write(
+            '<table><tr><th><input type="checkbox" id="Obj'
+            + str(parentObject)
+            + '"></th><th>Primitive \
+                                 Targets</th><th>Primitive level Errors</th>'
+        )
+        htmlStream.write("</tr>")
+
+        hiddenErr = Counter()
+        sortedList = []
+        # first count all error for each sub structure
+        for (rowG, col) in errmat.mat.getIter():
+            nbE = sum([v for (_, v) in col.getIter()], Counter())
+            sortedList.append((rowG, col, nbE))
+
+        # Show each confused stroke-level graph target in decreasing order of frequency.
+        sortedList = sorted(sortedList, key=lambda t: t[2].get(), reverse=True)
+        tindex = 1
+        graphSize = int(GRAPH_SIZE * 1.0)
+        # outputStream.write('<table valign="top">')
+        # htmlStream.write('<tr><th>')  \
+        # + '<input type="checkbox" id="Obj' + str(parentObject) + '"></th><th>Targets</th></tr>\n')
+
+        for (rowG, col, nbE) in sortedList:
+            if int(nbE) >= limit:
+                htmlStream.write('<tr><th valign="top">' + str(tindex) + "</th><th>")
+                htmlStream.write(
+                    '<A NAME="#Obj'
+                    + str(parentObject)
+                    + "Target"
+                    + str(tindex)
+                    + '">'
+                    + str(nbE)
+                    + " errors</A><br><br>\n"
+                )
+                htmlStream.write(rowG.toSVG(graphSize, arrow))
+                htmlStream.write("\n</th>")
+
+                htmlStream.write('<td><table valign="top">')
+                htmlStream.write(
+                    '<tr><th><input type="checkbox" id="Obj'
+                    + str(parentObject)
+                    + "Target"
+                    + str(tindex)
+                    + '" name="fileList"></th><th>Errors</th></tr>\n'
+                )
+                # ** Now show specific stroke-level confusions for target graph.
+                locHiddenErr = Counter()
+                arrow = False
+
+                # Critical: sort by frequency for specific confusion instances.
+                pairs = sorted(col.getIter(), key=lambda p: p[1].get(), reverse=True)
+                findex = 1
+                hindex = 1
+                for (g, v) in pairs:
+                    if v.get() >= limit:
+                        value = (" ").join(v.getList())
+                        htmlStream.write(
+                            '<tr><th text-align="center" valign="top"><input type="checkbox" name="fileList" id="Obj'
+                            + str(parentObject)
+                            + "Target"
+                            + str(tindex)
+                            + "Files"
+                            + str(findex)
+                            + '" class="fileCheck" value="'
+                            + value
+                            + '">'
+                            + str(findex)
+                            + "</th>"
+                        )
+                        htmlStream.write('<th valign="top">')
+                        htmlStream.write(
+                            '<A NAME="#Obj'
+                            + str(parentObject)
+                            + "Target"
+                            + str(tindex)
+                            + "Files"
+                            + str(findex)
+                            + '">'
+                            + str(v)
+                            + " errors</A><br><br>\n"
+                        )
+                        htmlStream.write(g.toSVG(graphSize, arrow))
+                        htmlStream.write("</th>\n")
+                        htmlStream.write('<td><table valign="top">\n')
+                        htmlStream.write(
+                            "<tr><th>Filename</th><th>Image</th><th>LG</th>\n"
+                        )
+                        for file in v.getList():
+                            htmlStream.write("</tr><tr>\n")
+                            htmlStream.write("<td>" + file + "</td>")
+                            imgfile = file[:-3] + ".PNG"
+                            dotfile = file[:-3] + ".pdf"
+                            htmlStream.write(
+                                '<td><img src="'
+                                + osp.join(img_dir, imgfile)
+                                + '" style="width:auto; height:40%;"></td>'
+                            )
+                            htmlStream.write(
+                                '<td><iframe src="'
+                                + osp.join(dotpdf_dir, dotfile)
+                                + '" style="width:1000px; height:275px;"></iframe></td>'
+                            )
+                        htmlStream.write("</table></td>")
+                        hindex += 1
+                    else:
+                        # Tricky; this counter object __str__ gives its list
+                        # size, but '+' concatenates entries.
+                        locHiddenErr = locHiddenErr + v
+
+                    findex += 1
+
+                htmlStream.write("</tr>")
+                # Write only one entry for 'other' errors. Do this
+                # Whenever a target/row is shown for completeness.
+                if len(locHiddenErr.getList()) > 0:
+                    value = (" ").join(locHiddenErr.getList())
+                    htmlStream.write(
+                        '<tr><th valign="top">'
+                        + '<input type="checkbox" id="Obj'
+                        + str(parentObject)
+                        + "Target"
+                        + str(tindex)
+                        + "Files"
+                        + str(findex)
+                        + '" name="fileList" class="fileCheck" value="'
+                        + value
+                        + '">'
+                        + str(hindex)
+                        + "</th>"
+                    )
+                    htmlStream.write(
+                        '<th valign="top">' + str(locHiddenErr) + " errors<br><br>"
+                    )
+                    htmlStream.write(
+                        "<center><font size=2>Other<br>Errors</font></center></th></tr>"
+                    )
+
+            elif int(str(nbE)) >= targetLimit:
+
+                htmlStream.write('<tr><th valign="top">' + str(tindex) + "</th>")
+                # This is for errors less frequent than the threshold
+                # for different target stroke-level graphs.
+                locHiddenErr = Counter()
+                for (g, v) in col.getIter():
+                    locHiddenErr = locHiddenErr + v
+
+                # Show all ground truth target errors with counts, so that
+                # object-level count is sum of confused stroke-level graphs.
+                value = (" ").join(locHiddenErr.getList())
+                htmlStream.write("<th>" + str(nbE) + " errors<br><br>\n")
+                htmlStream.write(rowG.toSVG(graphSize, arrow))
+                htmlStream.write("\n</th>")
+                htmlStream.write('<td><table valign="top">')
+                htmlStream.write(
+                    '<tr><th><input type="checkbox" id="Obj'
+                    + str(parentObject)
+                    + "Target"
+                    + str(tindex)
+                    + '" name="fileList"></th><th>Errors</th></tr>\n'
+                )
+                htmlStream.write(
+                    '<tr><th valign="top">'
+                    + '<input type="checkbox" class="fileCheck" id="Obj'
+                    + str(parentObject)
+                    + "Target"
+                    + str(tindex)
+                    + 'Files1" value="'
+                    + value
+                    + '">'
+                    + str(1)
+                    + "</th>"
+                )
+                htmlStream.write(
+                    '<th valign="top">' + str(locHiddenErr) + " errors<br><br>"
+                )
+                htmlStream.write(
+                    "<center><font size=2>Other<br>Errors</font></center></th></tr>"
+                )
+
+            else:
+                hiddenErr = hiddenErr + nbE
+
+            # Increment counter for stroke-level targets.
+            tindex += 1
+
+            htmlStream.write("</table></td></tr>")
+        # End this entry.
+        htmlStream.write("</table><p><b>Additional errors:</b> ")
+        viewStr = ""
+        if len(hiddenErr.getList()) > 0:
+            viewStr = (
+                '<a href="'
+                + viewerURL
+                + (",").join(hiddenErr.getList())
+                + '"> View </a>'
+            )
+        htmlStream.write(str(hiddenErr) + viewStr + "</p>")
+        htmlStream.write("</html>")
+        htmlStream.close()
+
+    def toHTML(
+        self,
+        outputStream,
+        limit=0,
+        viewerURL="",
+        redn={},
+        fileList=None,
+        img_dir="",
+        dotpdf_dir="",
+    ):
+        """ write in the output stream the HTML code for this matrix and
                 use the ConfMatrix.toHTML to write the submatrices.
                 The list of files with error is prefixed with the param viewerURL
                 in the href attribute. """
-                outputStream.write(' <table><tr>')
-                outputStream.write('<th><input type="checkbox" id="Obj"></th><th>Object Targets</th><th>Primitive Targets and Errors</th>')
-                outputStream.write('</tr>')
-                arrow = True
-                hiddenErr = Counter()
-                sortedList = []
-                # first count all errors for each object (over the full sub matrix)
-                for (obj,errmat) in self.mat.getIter():
-                        nbE = Counter()
-                        for (_,c) in errmat.mat.getIter():
-                                nbE = nbE + sum([v for (_,v) in c.getIter()], Counter())
-                        sortedList.append((obj,errmat,nbE))
-                sortedList = sorted(sortedList, key=lambda t:t[2].get(), reverse=True)
-
-                # Entries for object graphs ('main' rows)
-                gt_fileList = osp.join("CH_" + fileList + "_gt_htmls")
-                # dotpdf_dir = "../../Results_" + fileList.split('_vs')[0] + "/errorGraphs/png"
-                # dotpdf_dir = "../../Results_" + fileList.split('_vs')[0] + "/errorGraphs/pdf"
-                if not osp.exists(os.path.join("confHist_outputs", gt_fileList)):
-                    os.makedirs(os.path.join("confHist_outputs", gt_fileList))
-                oindex = 1
-                graphSize=GRAPH_SIZE
-                for (obj,errmat,nbE) in sortedList:
-                        if int(nbE) >= limit:
-                                gt_file = osp.join(gt_fileList, 'Obj_' + str(oindex) + '.html')
-                                ConfMatrixObject.create_gtHTML(obj, errmat, nbE, limit, viewerURL=viewerURL, 
-                                                               gt_file=gt_file, parentObject=oindex, 
-                                                               dotpdf_dir=dotpdf_dir, img_dir=img_dir)
-                                outputStream.write('<tr><th valign="top">' \
-                                                + str(oindex) + '</th>')
-                                outputStream.write('<th valign="top">\n')
-                                outputStream.write('<A HREF=\"' + gt_file +'\">' + str(nbE) + " errors<br><br></A>")
-                                outputStream.write(obj.toSVG(graphSize,arrow,'box'))
-                                outputStream.write('</th><td>')
-                                arrow = False
-                                # Treat 'object-level' matrix different: show all missed targets.
-                                hiddenErr = hiddenErr + errmat.toHTML(outputStream,limit,viewerURL=viewerURL,parentObject=oindex, gt_file=gt_file)
-                                outputStream.write('</td></tr>\n')
-                        else:
-                                hiddenErr = hiddenErr + nbE
-                        
-                        # Increment count for object targets
-                        oindex += 1
-                
-                outputStream.write('</table><p><b>Additional errors:</b> ')
-                viewStr = ""
-                if(len(hiddenErr.getList())> 0):
-                        additional_file = osp.join(gt_fileList, 'Additional_errors.html')
-                        ConfMatrixObject.add_err_HTML(hiddenErr.getList(), str(hiddenErr), 
-                                                      img_dir, dotpdf_dir, additional_file)
-                        viewStr = '<a href=' + additional_file + '> View </a>'
-                outputStream.write(str(hiddenErr) + viewStr + '</p>')
-                
-
+        outputStream.write(" <table><tr>")
+        outputStream.write(
+            '<th><input type="checkbox" id="Obj"></th><th>Object Targets</th><th>Primitive Targets and Errors</th>'
+        )
+        outputStream.write("</tr>")
+        arrow = True
+        hiddenErr = Counter()
+        sortedList = []
+        # first count all errors for each object (over the full sub matrix)
+        for (obj, errmat) in self.mat.getIter():
+            nbE = Counter()
+            for (_, c) in errmat.mat.getIter():
+                nbE = nbE + sum([v for (_, v) in c.getIter()], Counter())
+            sortedList.append((obj, errmat, nbE))
+        sortedList = sorted(sortedList, key=lambda t: t[2].get(), reverse=True)
+
+        # Entries for object graphs ('main' rows)
+        gt_fileList = osp.join("CH_" + fileList + "_gt_htmls")
+        # dotpdf_dir = "../../Results_" + fileList.split('_vs')[0] + "/errorGraphs/png"
+        # dotpdf_dir = "../../Results_" + fileList.split('_vs')[0] + "/errorGraphs/pdf"
+        if not osp.exists(os.path.join("confHist_outputs", gt_fileList)):
+            os.makedirs(os.path.join("confHist_outputs", gt_fileList))
+        oindex = 1
+        graphSize = GRAPH_SIZE
+        for (obj, errmat, nbE) in sortedList:
+            if int(nbE) >= limit:
+                gt_file = osp.join(gt_fileList, "Obj_" + str(oindex) + ".html")
+                ConfMatrixObject.create_gtHTML(
+                    obj,
+                    errmat,
+                    nbE,
+                    limit,
+                    viewerURL=viewerURL,
+                    gt_file=gt_file,
+                    parentObject=oindex,
+                    dotpdf_dir=dotpdf_dir,
+                    img_dir=img_dir,
+                )
+                outputStream.write('<tr><th valign="top">' + str(oindex) + "</th>")
+                outputStream.write('<th valign="top">\n')
+                outputStream.write(
+                    '<A HREF="' + gt_file + '">' + str(nbE) + " errors<br><br></A>"
+                )
+                outputStream.write(obj.toSVG(graphSize, arrow, "box"))
+                outputStream.write("</th><td>")
+                arrow = False
+                # Treat 'object-level' matrix different: show all missed targets.
+                hiddenErr = hiddenErr + errmat.toHTML(
+                    outputStream,
+                    limit,
+                    viewerURL=viewerURL,
+                    parentObject=oindex,
+                    gt_file=gt_file,
+                )
+                outputStream.write("</td></tr>\n")
+            else:
+                hiddenErr = hiddenErr + nbE
+
+            # Increment count for object targets
+            oindex += 1
+
+        outputStream.write("</table><p><b>Additional errors:</b> ")
+        viewStr = ""
+        if len(hiddenErr.getList()) > 0:
+            additional_file = osp.join(gt_fileList, "Additional_errors.html")
+            ConfMatrixObject.add_err_HTML(
+                hiddenErr.getList(),
+                str(hiddenErr),
+                img_dir,
+                dotpdf_dir,
+                additional_file,
+            )
+            viewStr = "<a href=" + additional_file + "> View </a>"
+        outputStream.write(str(hiddenErr) + viewStr + "</p>")
diff --git a/src/compareTools.py b/src/compareTools.py
index a0ccb77..9348c97 100644
--- a/src/compareTools.py
+++ b/src/compareTools.py
@@ -8,68 +8,77 @@
 # Copyright (c) 2013-2014 Richard Zanibbi and Harold Mouchere
 ################################################################
 
-def generateListErr(ab,ba):
-	listErr = []
-	if len(ab) == 0:
-		ab = ['_']
-	if len(ba) == 0:
-		ba = ['_']
-	for c1 in ab:
-		for c2 in ba:
-			listErr.append((c1,c2))
-	return listErr
+
+def generateListErr(ab, ba):
+    listErr = []
+    if len(ab) == 0:
+        ab = ["_"]
+    if len(ba) == 0:
+        ba = ["_"]
+    for c1 in ab:
+        for c2 in ba:
+            listErr.append((c1, c2))
+    return listErr
+
 
 def defaultMetric(labelList1, labelList2):
-	#new way but with 1 label per node
-	diff =  set(labelList1) ^ (set(labelList2)) # symetric diff
-	if len(diff) == 0:
-		return (0,[])
-	else:
-		ab = diff&set(labelList1)
-		ba = diff&set(labelList2)
-		cost = max(len(ab),len(ba) )
-		return (cost,generateListErr(ab,ba))
-
-
-synonym = {'X':'x','\\times':'x', 'P':'p', 'O':'o','C':'c', '\\prime':'COMMA'}
+    # new way but with 1 label per node
+    diff = set(labelList1) ^ (set(labelList2))  # symetric diff
+    if len(diff) == 0:
+        return (0, [])
+    else:
+        ab = diff & set(labelList1)
+        ba = diff & set(labelList2)
+        cost = max(len(ab), len(ba))
+        return (cost, generateListErr(ab, ba))
+
+
+synonym = {"X": "x", "\\times": "x", "P": "p", "O": "o", "C": "c", "\\prime": "COMMA"}
+
+
 def synonymMetric(labelList1, labelList2):
-	def replace(x):
-		if x in list(synonym):
-			return synonym[x]
-		else:
-			return x
-	a = map(replace, labelList1)
-	b = map(replace, labelList2)
-	diff =  set(a) ^ (set(b)) # symetric diff
-	if len(diff) == 0:
-		return (0,[])
-	else:
-		ab = diff&set(a)
-		ba = diff&set(b)
-		cost = max(len(ab),len(ba) )
-		return (cost,generateListErr(ab,ba))
+    def replace(x):
+        if x in list(synonym):
+            return synonym[x]
+        else:
+            return x
+
+    a = map(replace, labelList1)
+    b = map(replace, labelList2)
+    diff = set(a) ^ (set(b))  # symetric diff
+    if len(diff) == 0:
+        return (0, [])
+    else:
+        ab = diff & set(a)
+        ba = diff & set(b)
+        cost = max(len(ab), len(ba))
+        return (cost, generateListErr(ab, ba))
+
 
 ignoredLabelSet = set([])
 selectedLabelSet = set([])
+
+
 def filteredMetric(labelList1, labelList2):
-	labelS1 = set(labelList1) - ignoredLabelSet # removing the ignored labels
-	labelS2 = set(labelList2) - ignoredLabelSet # removing the ignored labels
-	if len(selectedLabelSet) > 0:
-		labelS1 &= selectedLabelSet # keep only the selected labels
-		labelS2 &= selectedLabelSet # keep only the selected labels
-	return defaultMetric(labelS1,labelS2)
+    labelS1 = set(labelList1) - ignoredLabelSet  # removing the ignored labels
+    labelS2 = set(labelList2) - ignoredLabelSet  # removing the ignored labels
+    if len(selectedLabelSet) > 0:
+        labelS1 &= selectedLabelSet  # keep only the selected labels
+        labelS2 &= selectedLabelSet  # keep only the selected labels
+    return defaultMetric(labelS1, labelS2)
+
 
 # no error if at least one symbol is OK
 def intersectMetric(labelList1, labelList2):
-#new way but with 1 label per node
-	inter =  set(labelList1) & (set(labelList2)) # symetric diff
-	if len(inter) > 0:
-		return (0,[])
-	else:
-		ab = set(labelList1)-inter
-		ba = set(labelList2)-inter
-		return (1,generateListErr(ab,ba))
-	
-	
+    # new way but with 1 label per node
+    inter = set(labelList1) & (set(labelList2))  # symetric diff
+    if len(inter) > 0:
+        return (0, [])
+    else:
+        ab = set(labelList1) - inter
+        ba = set(labelList2) - inter
+        return (1, generateListErr(ab, ba))
+
+
 cmpNodes = defaultMetric
 cmpEdges = defaultMetric
diff --git a/src/compileLabels.py b/src/compileLabels.py
index 639263b..8ec78f9 100644
--- a/src/compileLabels.py
+++ b/src/compileLabels.py
@@ -1,7 +1,7 @@
 ################################################################
 # compileLabels.py
 #
-# Reads in a list of .lg files, and returns a file contining 
+# Reads in a list of .lg files, and returns a file contining
 # node and edge labels used in the files.
 #
 # Author: H. Mouchere, June 2012
@@ -13,45 +13,47 @@ from lgeval.src.lg import *
 import os
 import time
 
-def main( fileName ):
-	try:
-		fileReader = csv.reader(open(fileName))
-	except:
-		sys.stderr.write('  !! IO Error (cannot open): ' + fileName)
-		sys.exit(1)
-
-	nodeLabels = set()
-	edgeLabels = set()
-	for row in fileReader:
-		# Skip blank lines.
-		if len(row) == 0:
-			continue
-
-		nextFile = row[0].strip()
-		
-		lg = Lg( nextFile )
-
-		# Collect all labels from node and edge label sets.
-		for node in list(lg.nlabels):
-			for label in list(lg.nlabels[ node ]):
-				nodeLabels.add( label )
-
-		for edge in list(lg.elabels):
-			for label in list(lg.elabels[ edge ]):
-				edgeLabels.add( label )
-
-	outString = "NODE LABELS:"
-	for label in sorted( list(nodeLabels )):
-		outString += "\n" + label
-	
-	outString += "\n\nEDGE LABELS:"
-	for label in sorted( list(edgeLabels)):
-		outString += "\n" + label
-	
-	print( outString )
-
-if len( sys.argv ) < 2:
-	print("Usage: [[python]] compileLabels.py lgFileList")
-	sys.exit(0)
-
-main( sys.argv[1] )
+
+def main(fileName):
+    try:
+        fileReader = csv.reader(open(fileName))
+    except:
+        sys.stderr.write("  !! IO Error (cannot open): " + fileName)
+        sys.exit(1)
+
+    nodeLabels = set()
+    edgeLabels = set()
+    for row in fileReader:
+        # Skip blank lines.
+        if len(row) == 0:
+            continue
+
+        nextFile = row[0].strip()
+
+        lg = Lg(nextFile)
+
+        # Collect all labels from node and edge label sets.
+        for node in list(lg.nlabels):
+            for label in list(lg.nlabels[node]):
+                nodeLabels.add(label)
+
+        for edge in list(lg.elabels):
+            for label in list(lg.elabels[edge]):
+                edgeLabels.add(label)
+
+    outString = "NODE LABELS:"
+    for label in sorted(list(nodeLabels)):
+        outString += "\n" + label
+
+    outString += "\n\nEDGE LABELS:"
+    for label in sorted(list(edgeLabels)):
+        outString += "\n" + label
+
+    print(outString)
+
+
+if len(sys.argv) < 2:
+    print("Usage: [[python]] compileLabels.py lgFileList")
+    sys.exit(0)
+
+main(sys.argv[1])
diff --git a/src/confHists.py b/src/confHists.py
index d9bed13..993c4d8 100644
--- a/src/confHists.py
+++ b/src/confHists.py
@@ -19,61 +19,85 @@ from lgeval.src.lgio import *
 import lgeval.src.SmGrConfMatrix as SmGrConfMatrix
 import lgeval.src.compareTools as compareTools
 
-def main(fileList, minCount=1, confMat=False, confMatObj=True, 
-         subgraphSize=1, img_dir='', dotpdf_dir=''):
-    fileReader = csv.reader(open(fileList), delimiter=' ')
+
+def main(
+    fileList,
+    minCount=1,
+    confMat=False,
+    confMatObj=True,
+    subgraphSize=1,
+    img_dir="",
+    dotpdf_dir="",
+):
+    fileReader = csv.reader(open(fileList), delimiter=" ")
     htmlStream = None
-    
+
     matrix = SmGrConfMatrix.ConfMatrix()
     matrixObj = SmGrConfMatrix.ConfMatrixObject()
     pdf_count = 0
     if os.path.exists(dotpdf_dir):
-        pdf_count = len(glob(os.path.join(dotpdf_dir, '*.pdf')))
+        pdf_count = len(glob(os.path.join(dotpdf_dir, "*.pdf")))
     if pdf_count == 0:
         dotpdf_dir = "confHist_outputs/dotpdfs"
-        print("\nlg2dot comparison output pdfs not found. Generating pdfs at {} ...\n".format(dotpdf_dir))
+        print(
+            "\nlg2dot comparison output pdfs not found. Generating pdfs at {} ...\n".format(
+                dotpdf_dir
+            )
+        )
         if not os.path.exists(dotpdf_dir):
             os.makedirs(dotpdf_dir)
     for row in fileReader:
         # Skip comments and empty lines.
         if not row == [] and not row[0].strip()[0] == "#":
-            #print(row)
-            lgfile1 = row[0].strip() # remove leading/trailing whitespace
+            # print(row)
+            lgfile1 = row[0].strip()  # remove leading/trailing whitespace
             lgfile2 = row[1].strip()
             # Here lg1 is input; lg2 is ground truth/comparison
             lg1 = Lg(lgfile1)
             lg2 = Lg(lgfile2)
             out = lg1.compare(lg2)
-            
+
             nodeClassErr = set()
             edgeErr = set()
             if confMat or confMatObj:
-                for (n,_,_) in out[1] :
+                for (n, _, _) in out[1]:
                     nodeClassErr.add(n)
-                for (e,_,_) in out[2] :
+                for (e, _, _) in out[2]:
                     edgeErr.add(e)
-        
+
             (head, tail) = os.path.split(lgfile1)
             (base, _) = os.path.splitext(tail)
             fileName = base + ".lg"
             if pdf_count == 0:
-                os.system("python $LgEvalDir/src/lg2dot.py " + lgfile1 + " " + lgfile2 + " >" + os.path.join(dotpdf_dir, base + ".dot"))
-                os.system("dot -Tpdf " + os.path.join(dotpdf_dir, base + ".dot") + " -o " + os.path.join(dotpdf_dir, base + ".pdf"))
+                os.system(
+                    "python $LgEvalDir/src/lg2dot.py "
+                    + lgfile1
+                    + " "
+                    + lgfile2
+                    + " >"
+                    + os.path.join(dotpdf_dir, base + ".dot")
+                )
+                os.system(
+                    "dot -Tpdf "
+                    + os.path.join(dotpdf_dir, base + ".dot")
+                    + " -o "
+                    + os.path.join(dotpdf_dir, base + ".pdf")
+                )
             if confMat:
                 # Subgraphs of 2 or 3 primitives.
-                for (gt,er) in lg1.compareSubStruct(lg2,[subgraphSize]):
+                for (gt, er) in lg1.compareSubStruct(lg2, [subgraphSize]):
                     er.rednodes = set(list(er.nodes)) & nodeClassErr
                     er.rededges = set(list(er.edges)) & edgeErr
-                    matrix.incr(gt,er,fileName)
+                    matrix.incr(gt, er, fileName)
             if confMatObj:
                 # Object subgraphs of 2 objects.
-                for (obj,gt,er) in lg1.compareSegmentsStruct(lg2,[subgraphSize]):
+                for (obj, gt, er) in lg1.compareSegmentsStruct(lg2, [subgraphSize]):
                     er.rednodes = set(list(er.nodes)) & nodeClassErr
                     er.rededges = set(list(er.edges)) & edgeErr
-                    matrixObj.incr(obj,gt,er,fileName)
-                    
+                    matrixObj.incr(obj, gt, er, fileName)
+
     htmlStream = None
-    
+
     objTargets = matrixObj.size()
     primTargets = matrix.size()
 
@@ -81,163 +105,261 @@ def main(fileList, minCount=1, confMat=False, confMatObj=True,
     fileList_head, fileList_tail = os.path.split(fileList)
     if not fileList_head:
         fileList_head = "confHist_outputs"
-    fileList_tail = fileList_tail + '__size_' + str(subgraphSize) + '_min_' + str(minCount) 
-    print("\nGenerating HTML at " + os.path.join(fileList_head, "CH_" + fileList_tail + ".html"))
-    htmlStream = open(os.path.join(fileList_head, 'CH_' + fileList_tail + '.html'),'w')
-    htmlStream.write('<meta charset="UTF-8">\n<html xmlns="http://www.w3.org/1999/xhtml">\n')
-    htmlStream.write('<head>\n')
+    fileList_tail = (
+        fileList_tail + "__size_" + str(subgraphSize) + "_min_" + str(minCount)
+    )
+    print(
+        "\nGenerating HTML at "
+        + os.path.join(fileList_head, "CH_" + fileList_tail + ".html")
+    )
+    htmlStream = open(os.path.join(fileList_head, "CH_" + fileList_tail + ".html"), "w")
+    htmlStream.write(
+        '<meta charset="UTF-8">\n<html xmlns="http://www.w3.org/1999/xhtml">\n'
+    )
+    htmlStream.write("<head>\n")
 
     # Code to support 'select all' checkboxes
     # Essentially registering callback functions to checkbox click events.
     # Make sure to include JQuery (using version 2.1.1 for now)
-    htmlStream.write('<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>\n')
-    htmlStream.write('<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.0.0/FileSaver.min.js"></script>\n')
-    
-    # (Excuse the messs..) create callbacks for checkbox events, save button 
+    htmlStream.write(
+        '<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>\n'
+    )
+    htmlStream.write(
+        '<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.0.0/FileSaver.min.js"></script>\n'
+    )
+
+    # (Excuse the messs..) create callbacks for checkbox events, save button
     # which saves the unique list of selected files in sorted order.
     # This was a slow, painful way to do this - perhaps an 'include' would be better.
     JS_DIR = os.path.join(fileList_head, "js")
     if not os.path.exists(JS_DIR):
         os.makedirs(JS_DIR)
-    jsStream = open(os.path.join(JS_DIR, "functions.js"), 'w')
+    jsStream = open(os.path.join(JS_DIR, "functions.js"), "w")
     jsStream.write(
-    '$(function(){ \n' +
-    '    $("#savebutton").click(function () { \n' +
-    '       var output = ""; \n' +
-    '       var selections = $(".fileCheck:checked"); \n' +
-    '       var fileString = ""; \n' +
-    '       for (i=0; i < selections.length; i++) { \n' +
-    '            fileString = selections[i].value.concat(" ").concat(fileString); \n' +
-    '                                console.log(fileString); \n' +
-    '        } \n' +
-    '                   fileList = fileString.split(" "); \n' +
-    '       fileList.sort(); \n' +
-    '       var uniqueSels = []; \n' +
-    '       var last = ""; \n' +
-    '       for (i=0; i < fileList.length; i++) { \n' +
-    '         if (fileList[i] != last) { \n' +
-    '            uniqueSels.push( fileList[i]); \n' +
-    '            last = fileList[i]; \n' +
-    '         } \n' +
-    '        } \n' +
-    '       output = uniqueSels.join("\\n"); \n' +
-    '       var blob = new Blob([ output ], {type: "text/plain;charset=utf-8"}); \n' +
-    '       saveAs(blob, "selectedFiles.txt"); \n' +
-    '    }); \n' +
-    '    $(":checkbox").click(function () { \n' +
-    '                        var current = this.id; \n' +
-    '                        var re = new RegExp(this.id + "[0-9]","i"); \n' +
-    '                        var elementsCommonIdPrefix = $("[id^=" + this.id + "]").filter(function() { \n' +
-    '                                return current == "Obj" || ! this.id.match(re); }); \n' +
-    '                        console.log(this.id + " Matched: " + elementsCommonIdPrefix.length); \n' +
-    '                        var parentId = this.id.match(/[a-zA-Z]+[0-9]+[a-zA-Z]+[0-9]+/); \n' +
-    '                        var grandparentId = this.id.match(/[a-zA-Z]+[0-9]+/); \n' +
-    '                        if ( ! this.checked ) { \n' +
-    '                            elementsCommonIdPrefix.prop("checked",false); \n' +
-    '                            if (parentId != null ) \n' +
-    '                                $("[id=" + parentId + "]").prop("checked", false); \n' +
-    '                            if (grandparentId != null) \n' +
-    '                                    $("[id=" + grandparentId + "]").prop("checked", false); \n' +
-    '                            if ($("[id=Obj]") != null) \n' +
-    '                                    $("[id=Obj]").prop("checked", false); \n' +
-    '            } else { \n' +
-    '                            elementsCommonIdPrefix.prop("checked", true); \n' +
-    '                            var pDescendents = $("[id^=" + parentId + "]"); \n' +
-    '                        var pDescChecked = $("[id^=" + parentId + "]:checked"); \n' +
-    '                            if (parentId != null && pDescendents.length == pDescChecked.length + 1 ) \n' +
-    '                                    $("[id=" + parentId + "]").prop("checked", true); \n' +
-    '                            var gDescendents = $("[id^=" + grandparentId + "]"); \n' +
-    '                                var gDescChecked = $("[id^=" + grandparentId + "]:checked"); \n' +
-    '                                if (grandparentId != null && gDescendents.length == gDescChecked.length + 1 ) \n' +
-    '                                    $("[id=" + grandparentId + "]").prop("checked", true); \n' +
-    '                                var aDescendents = $("[id^=Obj]"); \n' +
-    '                                var aDescChecked = $("[id^=Obj]:checked"); \n' +
-    '                                if ($("[id=Obj]") != null && aDescendents.length == aDescChecked.length + 1 ) \n' +
-    '                                    $("[id=Obj]").prop("checked", true); \n' +
-    '                        } \n' +
-    '                }); \n' +
-    '}); \n')
+        "$(function(){ \n"
+        + '    $("#savebutton").click(function () { \n'
+        + '       var output = ""; \n'
+        + '       var selections = $(".fileCheck:checked"); \n'
+        + '       var fileString = ""; \n'
+        + "       for (i=0; i < selections.length; i++) { \n"
+        + '            fileString = selections[i].value.concat(" ").concat(fileString); \n'
+        + "                                console.log(fileString); \n"
+        + "        } \n"
+        + '                   fileList = fileString.split(" "); \n'
+        + "       fileList.sort(); \n"
+        + "       var uniqueSels = []; \n"
+        + '       var last = ""; \n'
+        + "       for (i=0; i < fileList.length; i++) { \n"
+        + "         if (fileList[i] != last) { \n"
+        + "            uniqueSels.push( fileList[i]); \n"
+        + "            last = fileList[i]; \n"
+        + "         } \n"
+        + "        } \n"
+        + '       output = uniqueSels.join("\\n"); \n'
+        + '       var blob = new Blob([ output ], {type: "text/plain;charset=utf-8"}); \n'
+        + '       saveAs(blob, "selectedFiles.txt"); \n'
+        + "    }); \n"
+        + '    $(":checkbox").click(function () { \n'
+        + "                        var current = this.id; \n"
+        + '                        var re = new RegExp(this.id + "[0-9]","i"); \n'
+        + '                        var elementsCommonIdPrefix = $("[id^=" + this.id + "]").filter(function() { \n'
+        + '                                return current == "Obj" || ! this.id.match(re); }); \n'
+        + '                        console.log(this.id + " Matched: " + elementsCommonIdPrefix.length); \n'
+        + "                        var parentId = this.id.match(/[a-zA-Z]+[0-9]+[a-zA-Z]+[0-9]+/); \n"
+        + "                        var grandparentId = this.id.match(/[a-zA-Z]+[0-9]+/); \n"
+        + "                        if ( ! this.checked ) { \n"
+        + '                            elementsCommonIdPrefix.prop("checked",false); \n'
+        + "                            if (parentId != null ) \n"
+        + '                                $("[id=" + parentId + "]").prop("checked", false); \n'
+        + "                            if (grandparentId != null) \n"
+        + '                                    $("[id=" + grandparentId + "]").prop("checked", false); \n'
+        + '                            if ($("[id=Obj]") != null) \n'
+        + '                                    $("[id=Obj]").prop("checked", false); \n'
+        + "            } else { \n"
+        + '                            elementsCommonIdPrefix.prop("checked", true); \n'
+        + '                            var pDescendents = $("[id^=" + parentId + "]"); \n'
+        + '                        var pDescChecked = $("[id^=" + parentId + "]:checked"); \n'
+        + "                            if (parentId != null && pDescendents.length == pDescChecked.length + 1 ) \n"
+        + '                                    $("[id=" + parentId + "]").prop("checked", true); \n'
+        + '                            var gDescendents = $("[id^=" + grandparentId + "]"); \n'
+        + '                                var gDescChecked = $("[id^=" + grandparentId + "]:checked"); \n'
+        + "                                if (grandparentId != null && gDescendents.length == gDescChecked.length + 1 ) \n"
+        + '                                    $("[id=" + grandparentId + "]").prop("checked", true); \n'
+        + '                                var aDescendents = $("[id^=Obj]"); \n'
+        + '                                var aDescChecked = $("[id^=Obj]:checked"); \n'
+        + '                                if ($("[id=Obj]") != null && aDescendents.length == aDescChecked.length + 1 ) \n'
+        + '                                    $("[id=Obj]").prop("checked", true); \n'
+        + "                        } \n"
+        + "                }); \n"
+        + "}); \n"
+    )
     jsStream.close()
-    
+
     # Style
     CSS_DIR = os.path.join(fileList_head, "css")
     if not os.path.exists(CSS_DIR):
         os.makedirs(CSS_DIR)
-    cssStream = open(os.path.join(CSS_DIR, "style.css"), 'w')
+    cssStream = open(os.path.join(CSS_DIR, "style.css"), "w")
     cssStream.write('<style type="text/css">\n')
-    cssStream.write('svg { overflow: visible; }\n')
-    cssStream.write('p { line-height: 125%; }\n')
-    cssStream.write('li { line-height: 125%; }\n')
-    cssStream.write('button { font-size: 12pt; }\n')
-    cssStream.write('td { font-size: 10pt; align: left; text-align: left; border: 1px solid lightgray; padding: 5px; }\n')
-    cssStream.write('th { font-size: 10pt; font-weight: normal; border: 1px solid lightgray; padding: 10px; background-color: lavender; text-align: left }\n')
-    cssStream.write('tr { padding: 4px; }\n')
-    cssStream.write('table { border-collapse:collapse;}\n')
-    cssStream.write('</style>')
+    cssStream.write("svg { overflow: visible; }\n")
+    cssStream.write("p { line-height: 125%; }\n")
+    cssStream.write("li { line-height: 125%; }\n")
+    cssStream.write("button { font-size: 12pt; }\n")
+    cssStream.write(
+        "td { font-size: 10pt; align: left; text-align: left; border: 1px solid lightgray; padding: 5px; }\n"
+    )
+    cssStream.write(
+        "th { font-size: 10pt; font-weight: normal; border: 1px solid lightgray; padding: 10px; background-color: lavender; text-align: left }\n"
+    )
+    cssStream.write("tr { padding: 4px; }\n")
+    cssStream.write("table { border-collapse:collapse;}\n")
+    cssStream.write("</style>")
     cssStream.close()
 
-    htmlStream.write("<script src=\"js/functions.js\"></script>\n")
-    htmlStream.write("<link rel=\"stylesheet\" href=\"css/style.css\">\n")
-    htmlStream.write("</head>\n\n<font face=\"helvetica,arial,sans-serif\">")
+    htmlStream.write('<script src="js/functions.js"></script>\n')
+    htmlStream.write('<link rel="stylesheet" href="css/style.css">\n')
+    htmlStream.write('</head>\n\n<font face="helvetica,arial,sans-serif">')
     htmlStream.write("<h2>LgEval Structure Confusion Histograms</h2>")
     htmlStream.write(time.strftime("%c"))
-    htmlStream.write('<p><b>'+ fileList_tail + '</b><br>')
-    htmlStream.write('<b>Subgraphs:</b> ' + str(subgraphSize) + ' node(s)<br>')
-    htmlStream.write('<br>')
-    htmlStream.write('<p><b>Note:</b> Only primitive-level graph confusions occurring at least '+str(minCount)+' times appear below.<br><Note:</b><b>Note:</b> Individual primitive errors may appear in multiple error graphs (e.g. due to segmentation errors).</p>')
-    htmlStream.write('<UL>')
-
-    if (confMatObj):
-            htmlStream.write('<LI><A HREF=\"#Obj\">Object histograms</A> (' + str(objTargets) + ' incorrect targets; ' + str(matrixObj.errorCount()) + ' errors)')
-    if (confMat):
-            htmlStream.write('<LI><A HREF=\"#Prim\">Primitive histograms</A> (' + str(primTargets) + ' incorrect targets; ' + str(matrix.errorCount()) + ' errors)')
-    htmlStream.write('</UL>')
-    
-    htmlStream.write('<button type="button" font-size="12pt" id="savebutton">&nbsp;&nbsp;Save Selected Files&nbsp;&nbsp;</button>')
-    htmlStream.write('\n<hr>\n')
-    
+    htmlStream.write("<p><b>" + fileList_tail + "</b><br>")
+    htmlStream.write("<b>Subgraphs:</b> " + str(subgraphSize) + " node(s)<br>")
+    htmlStream.write("<br>")
+    htmlStream.write(
+        "<p><b>Note:</b> Only primitive-level graph confusions occurring at least "
+        + str(minCount)
+        + " times appear below.<br><Note:</b><b>Note:</b> Individual primitive errors may appear in multiple error graphs (e.g. due to segmentation errors).</p>"
+    )
+    htmlStream.write("<UL>")
+
     if confMatObj:
-            htmlStream.write('<h2><A NAME=\"#Obj\">Object Confusion Histograms</A></h2>')
-            htmlStream.write('<p>\n')
-            htmlStream.write('Object structures recognized incorrectly are shown at left, sorted by decreasing frequency. ' + str(objTargets) + ' incorrect targets, ' + str(matrixObj.errorCount()) + ' errors.')
-            htmlStream.write('</p>\n')
-            matrixObj.toHTML(htmlStream,minCount,"", fileList=fileList_tail, 
-                             img_dir=img_dir, dotpdf_dir=os.path.join('../..', dotpdf_dir))
-    
+        htmlStream.write(
+            '<LI><A HREF="#Obj">Object histograms</A> ('
+            + str(objTargets)
+            + " incorrect targets; "
+            + str(matrixObj.errorCount())
+            + " errors)"
+        )
     if confMat:
-            htmlStream.write("<hr>\n")
-            htmlStream.write('<h2><A NAME=\"Prim\">Primitive Confusion Histograms</A></h2>')
-            htmlStream.write('<p>Primitive structure recognizes incorrectly are shown at left, sorted by decreasing frequency. ' + str(primTargets) + ' incorrect targets, ' + str(matrix.errorCount()) + ' errors.</p>')
-
-            # Enforce the given limit for all reported errors for primitives.
-            matrix.toHTML(htmlStream,minCount,minCount,"", primitive=True, fileList=fileList_tail,
-                          img_dir=img_dir, dotpdf_dir=os.path.join('../..', dotpdf_dir))
-    
-            
-    htmlStream.write('</html>')
+        htmlStream.write(
+            '<LI><A HREF="#Prim">Primitive histograms</A> ('
+            + str(primTargets)
+            + " incorrect targets; "
+            + str(matrix.errorCount())
+            + " errors)"
+        )
+    htmlStream.write("</UL>")
+
+    htmlStream.write(
+        '<button type="button" font-size="12pt" id="savebutton">&nbsp;&nbsp;Save Selected Files&nbsp;&nbsp;</button>'
+    )
+    htmlStream.write("\n<hr>\n")
+
+    if confMatObj:
+        htmlStream.write('<h2><A NAME="#Obj">Object Confusion Histograms</A></h2>')
+        htmlStream.write("<p>\n")
+        htmlStream.write(
+            "Object structures recognized incorrectly are shown at left, sorted by decreasing frequency. "
+            + str(objTargets)
+            + " incorrect targets, "
+            + str(matrixObj.errorCount())
+            + " errors."
+        )
+        htmlStream.write("</p>\n")
+        matrixObj.toHTML(
+            htmlStream,
+            minCount,
+            "",
+            fileList=fileList_tail,
+            img_dir=img_dir,
+            dotpdf_dir=os.path.join("../..", dotpdf_dir),
+        )
+
+    if confMat:
+        htmlStream.write("<hr>\n")
+        htmlStream.write('<h2><A NAME="Prim">Primitive Confusion Histograms</A></h2>')
+        htmlStream.write(
+            "<p>Primitive structure recognizes incorrectly are shown at left, sorted by decreasing frequency. "
+            + str(primTargets)
+            + " incorrect targets, "
+            + str(matrix.errorCount())
+            + " errors.</p>"
+        )
+
+        # Enforce the given limit for all reported errors for primitives.
+        matrix.toHTML(
+            htmlStream,
+            minCount,
+            minCount,
+            "",
+            primitive=True,
+            fileList=fileList_tail,
+            img_dir=img_dir,
+            dotpdf_dir=os.path.join("../..", dotpdf_dir),
+        )
+
+    htmlStream.write("</html>")
     htmlStream.close()
-	
+
+
 def parse_args():
     parser = argparse.ArgumentParser(description="ConfHist Arguments")
-    parser.add_argument("--fileList", type=str, required=True, 
-                        help="The file containing list of lg file pairs to evaluate")
-    parser.add_argument("-gs", "--graphSize", type=int, required=True, 
-                        help="The number of objects/primitives in targets to analyze")
-    parser.add_argument("-m", "--minCount", type=int, default=1,
-                        help="The minimum number of times an error should occur")
-    parser.add_argument("-s", "--strokes", type=int, default=0,
-                        help="Flag whether to construct stroke (primitive) confusion histograms")
-    parser.add_argument("-i", "--lgimgDir", type=str, 
-                        help="The directory containing the expression images of the lg files")
-    parser.add_argument("-p", "--dotpdfDir", type=str, 
-                        help="The directory containing the lg2dot comparison pdf outputs")
-    parser.add_argument("-sp", "--split", type=int, default=0,
-                        help="Flag whether to construct stroke (primitive) confusion histograms")
-    parser.add_argument("-f", "--filter", type=int, default=1,
-                        help="Flag whether to construct stroke (primitive) confusion histograms")
+    parser.add_argument(
+        "--fileList",
+        type=str,
+        required=True,
+        help="The file containing list of lg file pairs to evaluate",
+    )
+    parser.add_argument(
+        "-gs",
+        "--graphSize",
+        type=int,
+        required=True,
+        help="The number of objects/primitives in targets to analyze",
+    )
+    parser.add_argument(
+        "-m",
+        "--minCount",
+        type=int,
+        default=1,
+        help="The minimum number of times an error should occur",
+    )
+    parser.add_argument(
+        "-s",
+        "--strokes",
+        type=int,
+        default=0,
+        help="Flag whether to construct stroke (primitive) confusion histograms",
+    )
+    parser.add_argument(
+        "-i",
+        "--lgimgDir",
+        type=str,
+        help="The directory containing the expression images of the lg files",
+    )
+    parser.add_argument(
+        "-p",
+        "--dotpdfDir",
+        type=str,
+        help="The directory containing the lg2dot comparison pdf outputs",
+    )
+    parser.add_argument(
+        "-sp",
+        "--split",
+        type=int,
+        default=0,
+        help="Flag whether to construct stroke (primitive) confusion histograms",
+    )
+    parser.add_argument(
+        "-f",
+        "--filter",
+        type=int,
+        default=1,
+        help="Flag whether to construct stroke (primitive) confusion histograms",
+    )
     args = parser.parse_args()
     return args
 
+
 if __name__ == "__main__":
     args = parse_args()
     # print(args)
@@ -245,5 +367,11 @@ if __name__ == "__main__":
     # img_dir = '../../../../data/infty/IMG'   # infty_contour...
     # img_dir = '../../../Data/Expressions/IMG'   # lpga_rf...
     img_dir = os.path.join("../..", args.lgimgDir)
-    main(args.fileList, minCount=args.minCount, confMat=bool(args.strokes), 
-         subgraphSize=args.graphSize, img_dir=img_dir, dotpdf_dir=args.dotpdfDir)
+    main(
+        args.fileList,
+        minCount=args.minCount,
+        confMat=bool(args.strokes),
+        subgraphSize=args.graphSize,
+        img_dir=img_dir,
+        dotpdf_dir=args.dotpdfDir,
+    )
diff --git a/src/evallg.py b/src/evallg.py
index dcf45ce..65d2088 100644
--- a/src/evallg.py
+++ b/src/evallg.py
@@ -20,191 +20,206 @@ import lgeval.src.SmGrConfMatrix as SmGrConfMatrix
 import lgeval.src.compareTools as compareTools
 
 # for RIT web service :
-#INKMLVIEWER = "inkml_viewer/index.xhtml?path=../testdata/&files="
-#local :
+# INKMLVIEWER = "inkml_viewer/index.xhtml?path=../testdata/&files="
+# local :
 INKMLVIEWER = "http://www.cs.rit.edu/~rlaz/inkml_viewer/index.xhtml?path=http://www.cs.rit.edu/~rlaz/testdata/&files="
 MINERRTOSHOW = 3
 
+
 def runBatch(fileName, defaultFileOrder, confMat, confMatObj):
-	"""Compile metrics for pairs of files provided in a CSV
+    """Compile metrics for pairs of files provided in a CSV
 	file. Store metrics and errors in separate files."""
-	fileReader = csv.reader(open(fileName))
-	metricStream = open(fileName + '.m','w')
-	diffStream = open(fileName + '.diff','w')
-
-	htmlStream = None
-	matrix = None
-	matrixObj = None
-	if confMat:
-		matrix = SmGrConfMatrix.ConfMatrix()
-		if confMatObj:
-			matrixObj = SmGrConfMatrix.ConfMatrixObject()
-
-	for row in fileReader:
-		# Skip comments and empty lines.
-		if not row == [] and not row[0].strip()[0] == "#":
-			lgfile1 = row[0].strip() # remove leading/trailing whitespace
-			lgfile2 = row[1].strip()
-			if not defaultFileOrder:
-				temp = lgfile2
-				lgfile2 = lgfile1
-				lgfile1 = temp
-			print ("Test: "+lgfile1+" vs. "+lgfile2);
-			toShow = lgfile1
-			if len(row)> 2:
-				toShow = row[2].strip()
-			# Here lg1 is the output, and lg2 the ground truth.
-			lg1 = Lg(lgfile1)
-			lg2 = Lg(lgfile2)
-			out = lg1.compare(lg2)
-
-			metricStream.write('*M,' + lgfile1 + ',' + lgfile2 + '\n')
-			writeMetrics(out, metricStream)
-			diffStream.write('DIFF,' + lgfile1 + ',' + lgfile2 + '\n')
-			writeDiff(out[1], out[3], out[2], diffStream)
-			
-			nodeClassErr = set()
-			edgeErr = set()
-			if confMat or confMatObj:
-				for (n,_,_) in out[1] :
-					nodeClassErr.add(n)
-				for (e,_,_) in out[2] :
-					edgeErr.add(e)
-			
-			if confMat:
-				for (gt,er) in lg1.compareSubStruct(lg2,[2,3]):
-					er.rednodes = set(list(er.nodes)) & nodeClassErr
-					er.rededges = set(list(er.edges)) & edgeErr
-					matrix.incr(gt,er,toShow)
-			if confMatObj:
-				for (obj,gt,er) in lg1.compareSegmentsStruct(lg2,[2]):
-					er.rednodes = set(list(er.nodes)) & nodeClassErr
-					er.rededges = set(list(er.edges)) & edgeErr
-					matrixObj.incr(obj,gt,er,toShow)
-                        
-		htmlStream = None
-	if confMat or confMatObj:
-		htmlStream = open(fileName + '.html','w')
-		htmlStream.write('<html xmlns="http://www.w3.org/1999/xhtml">')
-		htmlStream.write('<h1> File :'+fileName+'</h1>')
-		htmlStream.write('<p>Only errors with at least '+str(MINERRTOSHOW)+' occurrences appear</p>')
-	if confMat:
-		htmlStream.write('<h2> Substructure Confusion Matrix </h2>')
-		matrix.toHTML(htmlStream,MINERRTOSHOW,INKMLVIEWER)
-	if confMatObj:
-		htmlStream.write('<h2> Substructure Confusion Matrix at Object level </h2>')
-		matrixObj.toHTML(htmlStream,MINERRTOSHOW,INKMLVIEWER)
-	if confMat or confMatObj:
-		htmlStream.write('</html>')
-		htmlStream.close()
-		
-	metricStream.close()
-	diffStream.close()
-		
+    fileReader = csv.reader(open(fileName))
+    metricStream = open(fileName + ".m", "w")
+    diffStream = open(fileName + ".diff", "w")
+
+    htmlStream = None
+    matrix = None
+    matrixObj = None
+    if confMat:
+        matrix = SmGrConfMatrix.ConfMatrix()
+        if confMatObj:
+            matrixObj = SmGrConfMatrix.ConfMatrixObject()
+
+    for row in fileReader:
+        # Skip comments and empty lines.
+        if not row == [] and not row[0].strip()[0] == "#":
+            lgfile1 = row[0].strip()  # remove leading/trailing whitespace
+            lgfile2 = row[1].strip()
+            if not defaultFileOrder:
+                temp = lgfile2
+                lgfile2 = lgfile1
+                lgfile1 = temp
+            print("Test: " + lgfile1 + " vs. " + lgfile2)
+            toShow = lgfile1
+            if len(row) > 2:
+                toShow = row[2].strip()
+            # Here lg1 is the output, and lg2 the ground truth.
+            lg1 = Lg(lgfile1)
+            lg2 = Lg(lgfile2)
+            out = lg1.compare(lg2)
+
+            metricStream.write("*M," + lgfile1 + "," + lgfile2 + "\n")
+            writeMetrics(out, metricStream)
+            diffStream.write("DIFF," + lgfile1 + "," + lgfile2 + "\n")
+            writeDiff(out[1], out[3], out[2], diffStream)
+
+            nodeClassErr = set()
+            edgeErr = set()
+            if confMat or confMatObj:
+                for (n, _, _) in out[1]:
+                    nodeClassErr.add(n)
+                for (e, _, _) in out[2]:
+                    edgeErr.add(e)
+
+            if confMat:
+                for (gt, er) in lg1.compareSubStruct(lg2, [2, 3]):
+                    er.rednodes = set(list(er.nodes)) & nodeClassErr
+                    er.rededges = set(list(er.edges)) & edgeErr
+                    matrix.incr(gt, er, toShow)
+            if confMatObj:
+                for (obj, gt, er) in lg1.compareSegmentsStruct(lg2, [2]):
+                    er.rednodes = set(list(er.nodes)) & nodeClassErr
+                    er.rededges = set(list(er.edges)) & edgeErr
+                    matrixObj.incr(obj, gt, er, toShow)
+
+        htmlStream = None
+    if confMat or confMatObj:
+        htmlStream = open(fileName + ".html", "w")
+        htmlStream.write('<html xmlns="http://www.w3.org/1999/xhtml">')
+        htmlStream.write("<h1> File :" + fileName + "</h1>")
+        htmlStream.write(
+            "<p>Only errors with at least "
+            + str(MINERRTOSHOW)
+            + " occurrences appear</p>"
+        )
+    if confMat:
+        htmlStream.write("<h2> Substructure Confusion Matrix </h2>")
+        matrix.toHTML(htmlStream, MINERRTOSHOW, INKMLVIEWER)
+    if confMatObj:
+        htmlStream.write("<h2> Substructure Confusion Matrix at Object level </h2>")
+        matrixObj.toHTML(htmlStream, MINERRTOSHOW, INKMLVIEWER)
+    if confMat or confMatObj:
+        htmlStream.write("</html>")
+        htmlStream.close()
+
+    metricStream.close()
+    diffStream.close()
+
+
 def main():
-	if len(sys.argv) < 3:
-		print("Usage: [[python]] evallg.py <file1.lg> <file2.lg> [diff/*]  [INTER]")
-		print("   OR  [[python]] evallg.py <file1.lg> <file2.lg> MATRIX fileout")
-		print("   OR  [[python]] evallg.py [batch] <filepair_list> [GT-FIRST] [MAT] [MATOBJ] [INTER]")
-		print("")
-		print("    For the first usage, return error metrics and differences")
-		print("    for  label graphs in file1.lg and file2.lg.")
-		print("    A third argument will return just differences ('diff')")
-		print("    or just metrics (any other string). ")
-		print("    If MATRIX option is used, 4 evaluations are done with the ")
-		print("    different matrix label filters and output in the fileout[ABCD].m]")
-		print("")
-		print("    For the second usage, a file is provided containing pairs of")
-		print("    label graph files, one per line (e.g. 'file1, GTruth').")
-		print("    A third optional column contains the file name which should be")
-		print("    linked to the InkML viewer.")
-		print("")
-		print("    A CSV file containing metrics for all comparisons is written")
-		print("    to \"filepair_list.m\", and differences are written to a file")
-		print("    \"filepair_list.diff\". By default ground truth is listed")
-		print("    second on each line of the batch file; GT-FIRST as third argument")
-		print("    will result in the first element of each line being treated")
-		print("    as ground truth - this does not affect metrics (.m), but does")
-		print("    affect difference (.diff) output.")
-		print("")
-		print("    The MAT or MATOBJ option will create a HTML file with confusion Matrix")
-		print("    between substructures.")
-		print("    MAT will produce the subtructure at stroke level.")
-		print("    MATOBJ will produce the subtructure at object level.")
-		print("     (in both cases, the size of substructure is 2 or 3 nodes,")
-		print("      in both cases, only errors with at least 3 occurrences appear)")
-		sys.exit(0)
-
-	showErrors = True
-	showMetrics = True
-	
-	if "INTER" in sys.argv:
-		compareTools.cmpNodes = compareTools.intersectMetric;
-		compareTools.cmpEdges = compareTools.intersectMetric;
-	
-	if sys.argv[1] == "batch":
-		# If requested, swap arguments.
-		defaultFileOrder = True
-		confMat = False
-		confMatObj = False
-		if len(sys.argv) > 3 and "GT-FIRST" in sys.argv:
-			print(">> Treating 1st column as ground truth.")
-			defaultFileOrder = False
-		if len(sys.argv) > 3 and "MAT" in sys.argv:
-			print(">> Compute the confusion matrix at primitive level.")
-			confMat = True
-		if len(sys.argv) > 3 and "MATOBJ" in sys.argv:
-			print(">> Compute the confusion matrix at object level.")
-			confMatObj = True
-		runBatch(sys.argv[2], defaultFileOrder, confMat, confMatObj)
-
-	else:
-		# Running for a pair of files: require default order of arguments.
-		fileName1 = sys.argv[1]
-		fileName2 = sys.argv[2]
-		if len(sys.argv) > 4 and  sys.argv[3] == 'MATRIX':
-			fileOut = sys.argv[4]
-			#print ("MODE MATRIX : " + fileOut)
-			todo = {'Mat':set(['*M']),'Col':set(['*C']),'Row':set(['*R']),'Cell':set(['*Cell'])}
-			compareTools.cmpNodes = compareTools.filteredMetric
-			compareTools.cmpEdges = compareTools.filteredMetric
-			for (n,s) in todo.items():
-				compareTools.selectedLabelSet = s
-				n1 = Lg(fileName1)
-				n2 = Lg(fileName2)
-				out	= n1.compare(n2)
-				outStream = open(fileOut+n+".m", 'w')
-				writeMetrics(out, outStream)
-				outStream.close()
-			compareTools.selectedLabelSet = set([])
-			compareTools.ignoredLabelSet = set(['*M','*C','*R','*Cell'])			
-			n1 = Lg(fileName1)
-			n2 = Lg(fileName2)
-			out	= n1.compare(n2)
-			outStream = open(fileOut+"Symb.m", 'w')
-			writeMetrics(out, outStream)
-			
-		else:
-			
-			if 'diff' in sys.argv:
-				showMetrics = False
-			elif 'm' in sys.argv:
-				showErrors = False
-			n1 = Lg(fileName1)
-			n2 = Lg(fileName2)
-			
-			if "INTER" in sys.argv:
-				n1.labelMissingEdges()
-				n2.labelMissingEdges()
-			# print n1.csv()
-			# print n2.csv()
-				
-			out = n1.compare(n2)
-
-			if showMetrics:
-				writeMetrics(out, sys.stdout)
-			if showErrors:
-				writeDiff(out[1],out[3],out[2], sys.stdout)
+    if len(sys.argv) < 3:
+        print("Usage: [[python]] evallg.py <file1.lg> <file2.lg> [diff/*]  [INTER]")
+        print("   OR  [[python]] evallg.py <file1.lg> <file2.lg> MATRIX fileout")
+        print(
+            "   OR  [[python]] evallg.py [batch] <filepair_list> [GT-FIRST] [MAT] [MATOBJ] [INTER]"
+        )
+        print("")
+        print("    For the first usage, return error metrics and differences")
+        print("    for  label graphs in file1.lg and file2.lg.")
+        print("    A third argument will return just differences ('diff')")
+        print("    or just metrics (any other string). ")
+        print("    If MATRIX option is used, 4 evaluations are done with the ")
+        print("    different matrix label filters and output in the fileout[ABCD].m]")
+        print("")
+        print("    For the second usage, a file is provided containing pairs of")
+        print("    label graph files, one per line (e.g. 'file1, GTruth').")
+        print("    A third optional column contains the file name which should be")
+        print("    linked to the InkML viewer.")
+        print("")
+        print("    A CSV file containing metrics for all comparisons is written")
+        print('    to "filepair_list.m", and differences are written to a file')
+        print('    "filepair_list.diff". By default ground truth is listed')
+        print("    second on each line of the batch file; GT-FIRST as third argument")
+        print("    will result in the first element of each line being treated")
+        print("    as ground truth - this does not affect metrics (.m), but does")
+        print("    affect difference (.diff) output.")
+        print("")
+        print(
+            "    The MAT or MATOBJ option will create a HTML file with confusion Matrix"
+        )
+        print("    between substructures.")
+        print("    MAT will produce the subtructure at stroke level.")
+        print("    MATOBJ will produce the subtructure at object level.")
+        print("     (in both cases, the size of substructure is 2 or 3 nodes,")
+        print("      in both cases, only errors with at least 3 occurrences appear)")
+        sys.exit(0)
 
-main()
+    showErrors = True
+    showMetrics = True
 
+    if "INTER" in sys.argv:
+        compareTools.cmpNodes = compareTools.intersectMetric
+        compareTools.cmpEdges = compareTools.intersectMetric
+
+    if sys.argv[1] == "batch":
+        # If requested, swap arguments.
+        defaultFileOrder = True
+        confMat = False
+        confMatObj = False
+        if len(sys.argv) > 3 and "GT-FIRST" in sys.argv:
+            print(">> Treating 1st column as ground truth.")
+            defaultFileOrder = False
+        if len(sys.argv) > 3 and "MAT" in sys.argv:
+            print(">> Compute the confusion matrix at primitive level.")
+            confMat = True
+        if len(sys.argv) > 3 and "MATOBJ" in sys.argv:
+            print(">> Compute the confusion matrix at object level.")
+            confMatObj = True
+        runBatch(sys.argv[2], defaultFileOrder, confMat, confMatObj)
+
+    else:
+        # Running for a pair of files: require default order of arguments.
+        fileName1 = sys.argv[1]
+        fileName2 = sys.argv[2]
+        if len(sys.argv) > 4 and sys.argv[3] == "MATRIX":
+            fileOut = sys.argv[4]
+            # print ("MODE MATRIX : " + fileOut)
+            todo = {
+                "Mat": set(["*M"]),
+                "Col": set(["*C"]),
+                "Row": set(["*R"]),
+                "Cell": set(["*Cell"]),
+            }
+            compareTools.cmpNodes = compareTools.filteredMetric
+            compareTools.cmpEdges = compareTools.filteredMetric
+            for (n, s) in todo.items():
+                compareTools.selectedLabelSet = s
+                n1 = Lg(fileName1)
+                n2 = Lg(fileName2)
+                out = n1.compare(n2)
+                outStream = open(fileOut + n + ".m", "w")
+                writeMetrics(out, outStream)
+                outStream.close()
+            compareTools.selectedLabelSet = set([])
+            compareTools.ignoredLabelSet = set(["*M", "*C", "*R", "*Cell"])
+            n1 = Lg(fileName1)
+            n2 = Lg(fileName2)
+            out = n1.compare(n2)
+            outStream = open(fileOut + "Symb.m", "w")
+            writeMetrics(out, outStream)
+
+        else:
+
+            if "diff" in sys.argv:
+                showMetrics = False
+            elif "m" in sys.argv:
+                showErrors = False
+            n1 = Lg(fileName1)
+            n2 = Lg(fileName2)
+
+            if "INTER" in sys.argv:
+                n1.labelMissingEdges()
+                n2.labelMissingEdges()
+            # print n1.csv()
+            # print n2.csv()
+
+            out = n1.compare(n2)
+
+            if showMetrics:
+                writeMetrics(out, sys.stdout)
+            if showErrors:
+                writeDiff(out[1], out[3], out[2], sys.stdout)
+
+
+main()
diff --git a/src/lg.py b/src/lg.py
index 4fafe4b..314bf13 100644
--- a/src/lg.py
+++ b/src/lg.py
@@ -15,19 +15,31 @@ from collections import OrderedDict
 from lgeval.src.smallGraph import SmallGraph
 from lgeval.src.compareTools import cmpNodes, cmpEdges
 
+
 class Lg(object):
     """Class for bipartite graphs where the two node sets are identical, and
     multiple node and edge labels are permited. The graph and individual nodes
     and edges have associated values (e.g. weights/probabilities)."""
 
-# Define graph data elements ('data members' for an object in the class)
-    __slots__ = ('file','gweight','nlabels','elabels','error','absentNodes',\
-                    'absentEdges','hiddenEdges', 'cmpNodes', 'cmpEdges','stringInput')
-
-##################################
-# Constructors (in __init__)
-##################################
-    def __init__(self,*args): 
+    # Define graph data elements ('data members' for an object in the class)
+    __slots__ = (
+        "file",
+        "gweight",
+        "nlabels",
+        "elabels",
+        "error",
+        "absentNodes",
+        "absentEdges",
+        "hiddenEdges",
+        "cmpNodes",
+        "cmpEdges",
+        "stringInput",
+    )
+
+    ##################################
+    # Constructors (in __init__)
+    ##################################
+    def __init__(self, *args):
         """Graph data is read from a CSV file or provided node and edge label
         dictionaries.  If invalid entries are found, the error flag is set to
         true, and graph input continues.  In .lg files, blank lines are
@@ -45,17 +57,16 @@ class Lg(object):
         self.cmpEdges = cmpEdges
         self.stringInput = False
 
-        
         fileName = None
         nodeLabels = {}
         edgeLabels = {}
-        
+
         validAsteriskEdges = set()
         invalidAsteriskNodes = set()
 
         if len(args) == 1:
             fileName = args[0]
-            self.file = fileName # DEBUG: add filename for debugging purposes.
+            self.file = fileName  # DEBUG: add filename for debugging purposes.
         elif len(args) == 2:
             nodeLabels = args[0]
             edgeLabels = args[1]
@@ -72,19 +83,26 @@ class Lg(object):
                 for label in list(nodeLabels[nid]):
                     if not isinstance(nid, str):
                         label = str(label)
-            
+
                     # Weights need to be floats.
-                    if not isinstance( nodeLabels[nid][label], float ):
+                    if not isinstance(nodeLabels[nid][label], float):
                         self.error = True
-                        sys.stderr.write('  !! Invalid weight for node ' + nid + ', label \"' \
-                                            + label +"\": " + str(nodeLabels[nid][label]) + "\n")
-                    newdict[ label ] = nodeLabels[nid][label]
+                        sys.stderr.write(
+                            "  !! Invalid weight for node "
+                            + nid
+                            + ', label "'
+                            + label
+                            + '": '
+                            + str(nodeLabels[nid][label])
+                            + "\n"
+                        )
+                    newdict[label] = nodeLabels[nid][label]
                 self.nlabels[nid] = newdict
 
             # WARNING: self-edges are not detected if edge labels used
             # for initialization.
             for eid in list(edgeLabels):
-                if not isinstance(eid[0], str) or not isinstance(eid[1],str):
+                if not isinstance(eid[0], str) or not isinstance(eid[1], str):
                     eid[0] = str(eid[0])
                     eid[1] = str(eid[1])
 
@@ -92,11 +110,18 @@ class Lg(object):
                 for label in list(edgeLabels[eid]):
                     if not isinstance(label, str):
                         label = str(label)
-                    if not isinstance( edgeLabels[eid][label], float ):
+                    if not isinstance(edgeLabels[eid][label], float):
                         self.error = True
-                        sys.stderr.write('  !! Invalid weight for edge ' + str(eid) + ', label \"' \
-                                            + label +"\": " + str(edgeLabels[eid][label]) + "\n")
-                    newdict[ label ] = edgeLabels[eid][label]
+                        sys.stderr.write(
+                            "  !! Invalid weight for edge "
+                            + str(eid)
+                            + ', label "'
+                            + label
+                            + '": '
+                            + str(edgeLabels[eid][label])
+                            + "\n"
+                        )
+                    newdict[label] = edgeLabels[eid][label]
 
                 self.elabels[eid] = newdict
         else:
@@ -109,8 +134,8 @@ class Lg(object):
 
             fileReader = None
             # If passed a StringIO object, read CSV from the string.
-            if type(fileName) == type( StringIO('') ):
-                fileReader = csv.reader( fileName )
+            if type(fileName) == type(StringIO("")):
+                fileReader = csv.reader(fileName)
                 self.stringInput = True
             else:
                 # Otherwise, read from a file.
@@ -119,235 +144,304 @@ class Lg(object):
                 except:
                     # Create an empty graph if a file cannot be found.
                     # Set the error flag.
-                    sys.stderr.write( str(sys.exc_info()[0]) )
-                    sys.stderr.write('  !! IO Error (cannot open): ' + fileName + '\n')
+                    sys.stderr.write(str(sys.exc_info()[0]))
+                    sys.stderr.write("  !! IO Error (cannot open): " + fileName + "\n")
                     self.error = True
                     return
 
             # Read in data
             objectDict = dict([])
             for row in fileReader:
-                    # Skip blank lines.
-                    if len(row) == 0 or len(row) == 1 and row[0].strip() == '':
-                            continue
-
-                    entryType = row[0].strip()
-                    if entryType == 'N':
-                            if len(row) < MIN_NODE_ENTRY_LENGTH:
-                                    sys.stderr.write(' !! Invalid node entry length: ' +str(len(row))+\
-                                                    '\n\t' + str(row) + '\n')
-                                    self.error = True
+                # Skip blank lines.
+                if len(row) == 0 or len(row) == 1 and row[0].strip() == "":
+                    continue
+
+                entryType = row[0].strip()
+                if entryType == "N":
+                    if len(row) < MIN_NODE_ENTRY_LENGTH:
+                        sys.stderr.write(
+                            " !! Invalid node entry length: "
+                            + str(len(row))
+                            + "\n\t"
+                            + str(row)
+                            + "\n"
+                        )
+                        self.error = True
+                    else:
+                        nid = row[1].strip()  # remove leading/trailing whitespace
+                        if nid in list(self.nlabels):
+                            nlabelDict = self.nlabels[nid]
+                            nlabel = row[2].strip()
+                            # if nlabel in nlabelDict:
+                            # # Note possible error.
+                            # sys.stderr.write(' !! Repeated node label entry ('\
+                            # + self.file + '): ' \
+                            # + '\n\t' + str(row) + '\n')
+                            # self.error = True
+                            # Add (or replace) entry for the label.
+                            nlabelDict[nlabel] = float(row[3])
+                        else:
+                            # New primitive; create new dictionary for
+                            # provided label (row[2]) and value (row[3])
+                            nid = row[1].strip()
+                            nlabel = row[2].strip()
+
+                            # Feb. 2013 - allow no weight to be provided.
+                            if len(row) > MIN_NODE_ENTRY_LENGTH:
+                                self.nlabels[nid] = {nlabel: float(row[3])}
                             else:
-                                    nid = row[1].strip() # remove leading/trailing whitespace
-                                    if nid in list(self.nlabels):
-                                            nlabelDict = self.nlabels[ nid ]
-                                            nlabel = row[2].strip()
-                                            # if nlabel in nlabelDict:
-                                                    # # Note possible error.
-                                                    # sys.stderr.write(' !! Repeated node label entry ('\
-                                                                    # + self.file + '): ' \
-                                                                    # + '\n\t' + str(row) + '\n')
-                                                    # self.error = True
-                                            # Add (or replace) entry for the label.
-                                            nlabelDict[ nlabel ] = float(row[3])
-                                    else:
-                                            # New primitive; create new dictionary for 
-                                            # provided label (row[2]) and value (row[3])
-                                            nid = row[1].strip()
-                                            nlabel = row[2].strip()
-
-                                            # Feb. 2013 - allow no weight to be provided.
-                                            if len(row) > MIN_NODE_ENTRY_LENGTH:
-                                                    self.nlabels[ nid ] = { nlabel : float(row[3]) }
-                                            else:
-                                                    self.nlabels[ nid ] = { nlabel : 1.0 }
+                                self.nlabels[nid] = {nlabel: 1.0}
+
+                elif entryType == "E":
+                    if len(row) < MIN_EDGE_ENTRY_LENGTH:
+                        sys.stderr.write(
+                            " !! Invalid edge entry length: "
+                            + str(len(row))
+                            + "\n\t"
+                            + str(row)
+                            + "\n"
+                        )
+                        self.error = True
+                    else:
+                        primPair = (row[1].strip(), row[2].strip())
+                        # self to self edge = error
+                        if primPair[0] == primPair[1]:
+                            sys.stderr.write(
+                                "  !! Invalid self-edge ("
+                                + self.file
+                                + "):\n\t"
+                                + str(row)
+                                + "\n"
+                            )
+                            self.error = True
+                            nid = primPair[0]
+                            if nid in list(self.nlabels):
+                                nlabelDict = self.nlabels[nid]
+                                nlabel = row[3].strip()
+                                # if nlabel in nlabelDict:
+                                # # Note possible error.
+                                # sys.stderr.write(' !! Repeated node label entry ('\
+                                # + self.file + '): ' \
+                                # + '\n\t' + str(row) + '\n')
+                            # Add (or replace) entry for the label.
+                            nlabelDict[nlabel] = float(row[4])
+
+                        # an edge already existing, add a new label
+                        elif primPair in list(self.elabels):
+                            elabelDict = self.elabels[primPair]
+                            elabel = row[3].strip()
+                            # if elabel in elabelDict:
+                            # # Note possible error.
+                            # sys.stderr.write(' !! Repeated edge label entry (' \
+                            # + self.file + '):\n\t' + str(row) + '\n')
+                            # self.error = True
+                            if elabel == "*":
+                                # if using old fashion segmentation label, convert it by finding the (only) node label
+                                if (
+                                    primPair[0] in self.nlabels
+                                    and primPair[1] in self.nlabels
+                                    and self.nlabels[primPair[0]]
+                                    == self.nlabels[primPair[1]]
+                                ):
+                                    elabel = list(self.nlabels[primPair[0]])[0]
+
+                                    validAsteriskEdges.add(primPair)
+
+                                else:
+                                    sys.stderr.write(
+                                        " !! * edge used with ambiguous node labels ("
+                                        + str(self.nlabels[primPair[0]])
+                                        + " vs. "
+                                        + str(self.nlabels[primtPair[1]])
+                                        + ") in "
+                                        + self.file
+                                        + "):\n\t"
+                                        + ", ".join(row)
+                                        + "\n"
+                                    )
+
+                                    # RZ: Oct. 14 - cheap and dirty correction.
+                                    elabel = "MergeError"
+                                    self.nlabels[primPair[0]] = {elabel: 1.0}
+                                    self.nlabels[primPair[1]] = {elabel: 1.0}
+                                    self.error = True
+
+                                    invalidAsteriskNodes.add(primPair[0])
+                                    invalidAsteriskNodes.add(primPair[1])
 
-                    elif entryType == 'E':
-                            if len(row) < MIN_EDGE_ENTRY_LENGTH:
-                                    sys.stderr.write(' !! Invalid edge entry length: ' +str(len(row))+\
-                                                    '\n\t' + str(row) + '\n')
+                            # Add (or replace) entry for the label.
+                            # Feb. 2013 - allow no weight.
+                            if len(row) > MIN_EDGE_ENTRY_LENGTH:
+                                elabelDict[elabel] = float(row[4])
+                            else:
+                                elabelDict[elabel] = 1.0
+                        else:
+                            # Add new edge label entry for the new edge label
+                            # as a dictionary.
+                            primPair = (row[1].strip(), row[2].strip())
+                            elabel = row[3].strip()
+                            if elabel == "*":
+                                # if using old fashion segmentation label, convert it by finding the (only) node label
+                                if (
+                                    primPair[0] in self.nlabels
+                                    and primPair[1] in self.nlabels
+                                    and self.nlabels[primPair[0]]
+                                    == self.nlabels[primPair[1]]
+                                ):
+                                    elabel = list(self.nlabels[primPair[0]])[0]
+                                    validAsteriskEdges.add(primPair)
+
+                                else:
+                                    sys.stderr.write(
+                                        " !! * edge used with ambiguous node labels ("
+                                        + str(self.nlabels[primPair[0]])
+                                        + " vs. "
+                                        + str(self.nlabels[primPair[1]])
+                                        + ") in "
+                                        + self.file
+                                        + "):\n\t"
+                                        + ", ".join(row)
+                                        + "\n"
+                                    )
+
+                                    elabel = "MergeError"
+                                    self.nlabels[primPair[0]] = {elabel: 1.0}
+                                    self.nlabels[primPair[1]] = {elabel: 1.0}
                                     self.error = True
+
+                                    invalidAsteriskNodes.add(primPair[0])
+                                    invalidAsteriskNodes.add(primPair[1])
+
+                            self.elabels[primPair] = {elabel: float(row[4])}
+                elif entryType == "O":
+                    if len(row) < MIN_OBJECT_ENTRY_LENGTH:
+                        sys.stderr.write(
+                            " !! Invalid object entry length: "
+                            + str(len(row))
+                            + "\n\t"
+                            + str(row)
+                            + "\n"
+                        )
+                        self.error = True
+                    else:
+                        rawnodeList = row[4:]  # get all other item as node id
+                        oid = row[1].strip()
+                        nlabel = row[2].strip()
+                        nValue = float(row[3].strip())
+                        nodeList = []
+                        # add all nodes
+                        for n in rawnodeList:
+                            nid = n.strip()
+                            nodeList.append(nid)
+                            if nid in list(self.nlabels):
+                                nlabelDict = self.nlabels[nid]
+
+                                # Add (or replace) entry for the label.
+                                nlabelDict[nlabel] = nValue
                             else:
-                                    primPair = ( row[1].strip(), row[2].strip() )
-                                    #self to self edge = error
-                                    if primPair[0] == primPair[1]:
-                                            sys.stderr.write('  !! Invalid self-edge (' +
-                                                            self.file + '):\n\t' + str(row) + '\n')
-                                            self.error = True
-                                            nid = primPair[0]
-                                            if nid in list(self.nlabels):
-                                                    nlabelDict = self.nlabels[ nid ]
-                                                    nlabel = row[3].strip()
-                                                    # if nlabel in nlabelDict:
-                                                            # # Note possible error.
-                                                            # sys.stderr.write(' !! Repeated node label entry ('\
-                                                                    # + self.file + '): ' \
-                                                                    # + '\n\t' + str(row) + '\n')
-                                            # Add (or replace) entry for the label.
-                                            nlabelDict[ nlabel ] = float(row[4])
-
-                                    #an edge already existing, add a new label
-                                    elif primPair in list(self.elabels):
-                                            elabelDict = self.elabels[ primPair ]
-                                            elabel = row[3].strip()
-                                            # if elabel in elabelDict:
-                                                    # # Note possible error.
-                                                    # sys.stderr.write(' !! Repeated edge label entry (' \
-                                                                    # + self.file + '):\n\t' + str(row) + '\n')
-                                                    # self.error = True
-                                            if elabel == '*':
-                                                    # if using old fashion segmentation label, convert it by finding the (only) node label
-                                                    if primPair[0] in self.nlabels and primPair[1] in self.nlabels and \
-                                                    self.nlabels[ primPair[0]] == self.nlabels[ primPair[1]]:
-                                                            elabel =  list(self.nlabels[ primPair[0]])[0]
-                                                            
-                                                            validAsteriskEdges.add( primPair )
-
-                                                    else:
-                                                            sys.stderr.write(' !! * edge used with ambiguous node labels (' \
-                                                                    + str(self.nlabels[ primPair[0]]) + ' vs. ' \
-                                                                    + str(self.nlabels[ primtPair[1]]) + ') in ' \
-                                                                    + self.file + '):\n\t' + ", ".join(row) + '\n')
-                                                            
-                                                            # RZ: Oct. 14 - cheap and dirty correction.
-                                                            elabel = 'MergeError'
-                                                            self.nlabels[ primPair[0] ] = { elabel : 1.0 }
-                                                            self.nlabels[ primPair[1] ] = { elabel : 1.0 }
-                                                            self.error = True
-
-                                                            invalidAsteriskNodes.add( primPair[0] )
-                                                            invalidAsteriskNodes.add( primPair[1] )
-            
-                                            # Add (or replace) entry for the label.
-                                            # Feb. 2013 - allow no weight.
-                                            if len(row) > MIN_EDGE_ENTRY_LENGTH:
-                                                    elabelDict[ elabel ] = float(row[4])
-                                            else:
-                                                    elabelDict[ elabel ] = 1.0
+                                # New primitive; create new dictionary for
+                                # provided label and value
+                                # Feb. 2013 - allow no weight to be provided.
+                                self.nlabels[nid] = {nlabel: nValue}
+                        # save the nodes of this object
+                        objectDict[oid] = nodeList
+                        # add all edges
+                        for nid1 in nodeList:
+                            # nid1 = n1.strip()
+                            for nid2 in nodeList:
+                                # nid2 = n2.strip()
+                                if nid1 != nid2:
+                                    primPair = (nid1, nid2)
+                                    elabel = nlabel
+                                    if primPair in list(self.elabels):
+                                        elabelDict = self.elabels[primPair]
+
+                                        # Add (or replace) entry for the label.
+                                        elabelDict[elabel] = nValue
                                     else:
+                                        # Add new edge label entry for the new edge label
+                                        # as a dictionary.
+                                        self.elabels[primPair] = {elabel: nValue}
+
+                elif entryType == "R" or entryType == "EO":
+                    if len(row) < MIN_OBJECT_EDGE_ENTRY_LENGTH:
+                        sys.stderr.write(
+                            " !! Invalid object entry length: "
+                            + str(len(row))
+                            + "\n\t"
+                            + str(row)
+                            + "\n"
+                        )
+                        self.error = True
+                    else:
+                        oid1 = row[1].strip()
+                        oid2 = row[2].strip()
+                        elabel = row[3].strip()
+                        eValue = float(row[4].strip())
+                        validRelationship = True
+
+                        if not oid1 in objectDict:
+                            sys.stderr.write(
+                                ' !! Invalid object id: "'
+                                + oid1
+                                + '" - IGNORING relationship:\n\t'
+                                + str(row)
+                                + "\n"
+                            )
+                            self.error = True
+                            validRelationship = False
+                        if not oid2 in objectDict:
+                            sys.stderr.write(
+                                ' !! Invalid object id: "'
+                                + oid2
+                                + '" - IGNORING relationship:\n\t'
+                                + str(row)
+                                + "\n"
+                            )
+                            self.error = True
+                            validRelationship = False
+                        if validRelationship:
+                            nodeList1 = objectDict[
+                                oid1
+                            ]  # get all other item as node id
+                            nodeList2 = objectDict[
+                                oid2
+                            ]  # get all other item as node id
+
+                            for nid1 in nodeList1:
+                                for nid2 in nodeList2:
+                                    if nid1 != nid2:
+                                        primPair = (nid1, nid2)
+                                        if primPair in list(self.elabels):
+                                            elabelDict = self.elabels[primPair]
+
+                                            # Add (or replace) entry for the label.
+                                            elabelDict[elabel] = eValue
+                                        else:
                                             # Add new edge label entry for the new edge label
-                                            # as a dictionary.
-                                            primPair = ( row[1].strip(), row[2].strip() )
-                                            elabel = row[3].strip()
-                                            if elabel == '*':
-                                                    # if using old fashion segmentation label, convert it by finding the (only) node label
-                                                    if primPair[0] in self.nlabels and primPair[1] in self.nlabels and \
-                                                    self.nlabels[ primPair[0]] == self.nlabels[ primPair[1]]:
-                                                            elabel = list(self.nlabels[ primPair[0]])[0]
-                                                            validAsteriskEdges.add( primPair )
-
-                                                    else:
-                                                            sys.stderr.write(' !! * edge used with ambiguous node labels (' \
-                                                                    + str(self.nlabels[ primPair[0]]) + ' vs. ' \
-                                                                    + str(self.nlabels[ primPair[1]]) + ') in ' \
-                                                                    + self.file + '):\n\t' + ", ".join(row) + '\n')
-                                                            
-                                                            elabel = 'MergeError'
-                                                            self.nlabels[ primPair[0] ] = { elabel : 1.0 }
-                                                            self.nlabels[ primPair[1] ] = { elabel : 1.0 }
-                                                            self.error = True
-
-                                                            invalidAsteriskNodes.add( primPair[0] )
-                                                            invalidAsteriskNodes.add( primPair[1] )
-
-                                            self.elabels[ primPair ] = { elabel : float(row[4]) }
-                    elif entryType == 'O':
-                            if len(row) < MIN_OBJECT_ENTRY_LENGTH:
-                                    sys.stderr.write(' !! Invalid object entry length: '+str(len(row))+\
-                                                    '\n\t' + str(row) + '\n')
-                                    self.error = True
-                            else:
-                                    rawnodeList = row[4:] # get all other item as node id
-                                    oid =  row[1].strip()
-                                    nlabel =  row[2].strip()
-                                    nValue =  float(row[3].strip())
-                                    nodeList = []
-                                # add all nodes
-                                    for n in rawnodeList:
-                                            nid = n.strip()
-                                            nodeList.append(nid)
-                                            if nid in list(self.nlabels):
-                                                    nlabelDict = self.nlabels[ nid ]
-                                                    
-                                                    # Add (or replace) entry for the label.
-                                                    nlabelDict[ nlabel ] = nValue
-                                            else:
-                                                    # New primitive; create new dictionary for 
-                                                    # provided label and value 	
-                                                    # Feb. 2013 - allow no weight to be provided.
-                                                    self.nlabels[ nid ] = { nlabel : nValue }
-                                    #save the nodes of this object
-                                    objectDict[oid] = nodeList
-                                    #add all edges
-                                    for nid1 in nodeList:
-                                            #nid1 = n1.strip()
-                                            for nid2 in nodeList:
-                                                    #nid2 = n2.strip()
-                                                    if nid1 != nid2:
-                                                            primPair = ( nid1, nid2 )
-                                                            elabel = nlabel 
-                                                            if primPair in list(self.elabels):
-                                                                    elabelDict = self.elabels[ primPair ]
-                                                                    
-                                                                    # Add (or replace) entry for the label.
-                                                                    elabelDict[ elabel ] = nValue
-                                                            else:
-                                                                    # Add new edge label entry for the new edge label
-                                                                    # as a dictionary.
-                                                                    self.elabels[ primPair ] = { elabel : nValue }
-
-                    elif entryType == 'R' or entryType == 'EO':
-                            if len(row) < MIN_OBJECT_EDGE_ENTRY_LENGTH:
-                                    sys.stderr.write(' !! Invalid object entry length: ' +str(len(row))+\
-                                                    '\n\t' + str(row) + '\n')
-                                    self.error = True
-                            else:
-                                    oid1 = row[1].strip()
-                                    oid2 = row[2].strip()
-                                    elabel = row[3].strip()
-                                    eValue = float(row[4].strip())
-                                    validRelationship = True
-
-                                    if not oid1 in objectDict:
-                                        sys.stderr.write(' !! Invalid object id: "' + oid1+\
-                                                        '" - IGNORING relationship:\n\t' + str(row) + '\n')
-                                        self.error = True
-                                        validRelationship = False
-                                    if not oid2 in objectDict:
-                                        sys.stderr.write(' !! Invalid object id: "' + oid2+\
-                                                        '" - IGNORING relationship:\n\t' + str(row) + '\n')
+                                            # as dictionary.
+                                            self.elabels[primPair] = {elabel: eValue}
+                                    else:
+                                        sys.stderr.write(
+                                            "  !! Invalid self-edge ("
+                                            + self.file
+                                            + "):\n\t"
+                                            + str(row)
+                                            + "\n"
+                                        )
                                         self.error = True
-                                        validRelationship = False
-                                    if validRelationship:
-                                        nodeList1 = objectDict[oid1] # get all other item as node id
-                                        nodeList2 = objectDict[oid2] # get all other item as node id
-
-                                        for nid1 in nodeList1:
-                                                for nid2 in nodeList2:
-                                                        if nid1 != nid2:
-                                                                primPair = ( nid1, nid2 )
-                                                                if primPair in list(self.elabels):
-                                                                        elabelDict = self.elabels[ primPair ]
-                                                                        
-                                                                        # Add (or replace) entry for the label.
-                                                                        elabelDict[ elabel ] = eValue
-                                                                else:
-                                                                        # Add new edge label entry for the new edge label
-                                                                        # as dictionary.
-                                                                        self.elabels[ primPair ] = { elabel : eValue }
-                                                        else:			
-                                                                sys.stderr.write('  !! Invalid self-edge (' +
-                                                                self.file + '):\n\t' + str(row) + '\n')
-                                                                self.error = True
-
-                    # DEBUG: complaints about empty lines here...
-                    elif len(entryType.strip()) > 0 and entryType.strip()[0] == '#':
-                        # Ignore lines with comments.
-                        pass
-                    else:
-                        sys.stderr.write('  !! Invalid graph entry type (expected N, E, O, R or EO): ' \
-                                        + str(row) + '\n')
-                        self.error = True
+
+                # DEBUG: complaints about empty lines here...
+                elif len(entryType.strip()) > 0 and entryType.strip()[0] == "#":
+                    # Ignore lines with comments.
+                    pass
+                else:
+                    sys.stderr.write(
+                        "  !! Invalid graph entry type (expected N, E, O, R or EO): "
+                        + str(row)
+                        + "\n"
+                    )
+                    self.error = True
 
         # Add any implicit nodes in edges explicitly to the hash table
         # containing nodes. The 'nolabel' label is '_'.
@@ -358,26 +452,26 @@ class Lg(object):
             nid2 = elabel[1]
 
             if not nid1 in list(self.nlabels):
-                    self.nlabels[ nid1 ] = { '_' : 1.0 }
-                    anodeList = anodeList + [ nid1 ]
-                    anonNode = True
+                self.nlabels[nid1] = {"_": 1.0}
+                anodeList = anodeList + [nid1]
+                anonNode = True
             if not nid2 in list(self.nlabels):
-                    self.nlabels[ nid2 ] = { '_' : 1.0 }
-                    anodeList = anodeList + [ nid2 ]
-                    anonNode = True
+                self.nlabels[nid2] = {"_": 1.0}
+                anodeList = anodeList + [nid2]
+                anonNode = True
         if anonNode:
-            sys.stderr.write('  ** Anonymous labels created for:\n\t' \
-                    + str(anodeList) + '\n')
-
+            sys.stderr.write(
+                "  ** Anonymous labels created for:\n\t" + str(anodeList) + "\n"
+            )
 
         # RZ Oct. 2014: add invalid merge edges and node labels where missing.
         #    This catches when a valid * edge is connected to an invalid one,
         #    relabeling the edge.
-        invalidAsteriskNodeList = sorted( list(invalidAsteriskNodes) )
+        invalidAsteriskNodeList = sorted(list(invalidAsteriskNodes))
         while len(invalidAsteriskNodeList) > 0:
             # Remove last element from the list.
             nextPrimId = invalidAsteriskNodeList.pop()
-            
+
             # Linear traversal for matches (a 'region growing' algorithm)
             # Add a traversal each time a new connected edge is found.
             # NOTE: this will not add edges missing in the input (e.g.
@@ -391,948 +485,1062 @@ class Lg(object):
 
                 if otherId != None:
                     if not otherId in invalidAsteriskNodes:
-                        invalidAsteriskNodes.add( otherId )
-                        invalidAsteriskNodeList.append( otherId )
+                        invalidAsteriskNodes.add(otherId)
+                        invalidAsteriskNodeList.append(otherId)
 
-                    self.nlabels[ otherId ] = { 'MergeError' : 1.0 }
-                    self.elabels[ (parent, child) ] = { 'MergeError' : 1.0 }
+                    self.nlabels[otherId] = {"MergeError": 1.0}
+                    self.elabels[(parent, child)] = {"MergeError": 1.0}
 
-##################################
-# String, CSV output
-##################################
+    ##################################
+    # String, CSV output
+    ##################################
     def __str__(self):
-            nlabelcount = 0
-            elabelcount = 0
-            for nid in list(self.nlabels):
-                    nlabelcount = nlabelcount + len(list(self.nlabels[nid]))
-            for eid in list(self.elabels):
-                    elabelcount = elabelcount + len(list(self.elabels[eid]))
-
-            return 'Nodes: ' + str(len(list(self.nlabels))) \
-                            + ' (labels: ' + str(nlabelcount) \
-                            + ')   Edges: ' + str(len(list(self.elabels))) \
-                            + ' (labels: ' + str(elabelcount) \
-                            + ')   Error: ' + str(self.error)
-
+        nlabelcount = 0
+        elabelcount = 0
+        for nid in list(self.nlabels):
+            nlabelcount = nlabelcount + len(list(self.nlabels[nid]))
+        for eid in list(self.elabels):
+            elabelcount = elabelcount + len(list(self.elabels[eid]))
+
+        return (
+            "Nodes: "
+            + str(len(list(self.nlabels)))
+            + " (labels: "
+            + str(nlabelcount)
+            + ")   Edges: "
+            + str(len(list(self.elabels)))
+            + " (labels: "
+            + str(elabelcount)
+            + ")   Error: "
+            + str(self.error)
+        )
 
     def csvObject(self):
-            """Construct CSV data file using object-relationship format. Currently 
+        """Construct CSV data file using object-relationship format. Currently 
             weight values are only placeholders (i.e. 1.0 is always used)."""
-            outputString = ""
-
-            (segmentPrimitiveMap, primitiveSegmentMap, rootSegments, \
-                            segmentEdges) = self.segmentGraph()
-
-            # Write the file name if file is used.
-            if self.stringInput == False:
-                outputString += "# " + os.path.split(self.file)[1]
-                outputString += "\n\n"
-
-            # Write number of objects and format information.
-            # Output object information.
-            outputString += "# " + str(len(list(segmentPrimitiveMap))) + " Objects"
-            outputString += "\n"
-            outputString += "# FORMAT: O, Object ID, Label, Weight, [ Primitive ID List ]"
-            outputString += "\n"
-
-            for objectId in sorted( list(segmentPrimitiveMap) ):
-                    for label in sorted(segmentPrimitiveMap[objectId][1]):
-                            outputString += "O, " + objectId + ", " + label + ", 1.0"
-                            for primitiveId in sorted( segmentPrimitiveMap[ objectId ][ 0 ] ):
-                                    outputString += ", " + primitiveId 
-                            outputString += "\n"
-
-            # Write number of relationships and format information.
-            # Write relationship information.
-            outputString += "\n"
-            outputString += "# " + str( len(list(segmentEdges)) ) + " Relationships (Pairs of Objects)"
-            outputString += "\n"
-            outputString += "# FORMAT: R, Object ID (parent), Object ID (child), Label, Weight" 
-            outputString += "\n"
-
-            for (parentObj, childObj) in sorted( list(segmentEdges) ):
-                    for relationship in sorted( list(segmentEdges[ (parentObj, childObj) ]) ):
-                            outputString += "R, " + parentObj + ", " + childObj + ", " 
-                            outputString += relationship + ", 1.0"
-                            outputString += "\n"
-
-            return outputString
-
+        outputString = ""
+
+        (
+            segmentPrimitiveMap,
+            primitiveSegmentMap,
+            rootSegments,
+            segmentEdges,
+        ) = self.segmentGraph()
+
+        # Write the file name if file is used.
+        if self.stringInput == False:
+            outputString += "# " + os.path.split(self.file)[1]
+            outputString += "\n\n"
+
+        # Write number of objects and format information.
+        # Output object information.
+        outputString += "# " + str(len(list(segmentPrimitiveMap))) + " Objects"
+        outputString += "\n"
+        outputString += "# FORMAT: O, Object ID, Label, Weight, [ Primitive ID List ]"
+        outputString += "\n"
+
+        for objectId in sorted(list(segmentPrimitiveMap)):
+            for label in sorted(segmentPrimitiveMap[objectId][1]):
+                outputString += "O, " + objectId + ", " + label + ", 1.0"
+                for primitiveId in sorted(segmentPrimitiveMap[objectId][0]):
+                    outputString += ", " + primitiveId
+                outputString += "\n"
+
+        # Write number of relationships and format information.
+        # Write relationship information.
+        outputString += "\n"
+        outputString += (
+            "# " + str(len(list(segmentEdges))) + " Relationships (Pairs of Objects)"
+        )
+        outputString += "\n"
+        outputString += (
+            "# FORMAT: R, Object ID (parent), Object ID (child), Label, Weight"
+        )
+        outputString += "\n"
+
+        for (parentObj, childObj) in sorted(list(segmentEdges)):
+            for relationship in sorted(list(segmentEdges[(parentObj, childObj)])):
+                outputString += "R, " + parentObj + ", " + childObj + ", "
+                outputString += relationship + ", 1.0"
+                outputString += "\n"
+
+        return outputString
 
     def csv(self, sort=True):
-            """Construct CSV data file representation as a string."""
-            # NOTE: currently the graph value is not being stored...
-            sstring = ''
-            nlist = []
-            elist = []
-            for nkey in list(self.nlabels):
-                    nodeLabels = self.nlabels[nkey]
-                    for nlabel in list(nodeLabels):
-                            nstring = 'N,' + nkey + ',' + nlabel + ',' + \
-                                            str(nodeLabels[nlabel]) + '\n'
-                            nlist = nlist + [ nstring ]
-
-            for npair in list(self.elabels):
-                    edgeLabels = self.elabels[npair]
-                    for elabel in list(edgeLabels):
-                            estring = 'E,' + npair[0] + ',' + npair[1] + ',' + elabel + ',' + \
-                                            str(edgeLabels[ elabel ]) + '\n'
-                            elist = elist + [ estring ]
-
-            if sort:
-                # Sort the node and edge strings lexicographically.
-                # NOTE: this means that '10' precedes '2' in the sorted ordering
-                nlist.sort()
-                elist.sort() 
-
-            if self.stringInput == False:
-                sstring += '# ' + os.path.split(self.file)[1] + '\n\n' 
-
-            sstring += '# ' + str(len(nlist)) + ' Nodes\n'
-            sstring += "# FORMAT: N, Primitive ID, Label, Weight\n"
-            for nstring in nlist:
-                    sstring = sstring + nstring
-            sstring += "\n"
-
-            sstring += '# ' + str(len(elist)) + ' Edges\n'
-            sstring += '# FORMAT: E, Primitive ID (parent), Primitive ID (child), Label, Weight\n'
-            for estring in elist:
-                    sstring = sstring + estring
-            
-            return sstring
-
-##################################
-# Construct segment-based graph
-# for current graph state
-##################################
+        """Construct CSV data file representation as a string."""
+        # NOTE: currently the graph value is not being stored...
+        sstring = ""
+        nlist = []
+        elist = []
+        for nkey in list(self.nlabels):
+            nodeLabels = self.nlabels[nkey]
+            for nlabel in list(nodeLabels):
+                nstring = (
+                    "N," + nkey + "," + nlabel + "," + str(nodeLabels[nlabel]) + "\n"
+                )
+                nlist = nlist + [nstring]
+
+        for npair in list(self.elabels):
+            edgeLabels = self.elabels[npair]
+            for elabel in list(edgeLabels):
+                estring = (
+                    "E,"
+                    + npair[0]
+                    + ","
+                    + npair[1]
+                    + ","
+                    + elabel
+                    + ","
+                    + str(edgeLabels[elabel])
+                    + "\n"
+                )
+                elist = elist + [estring]
+
+        if sort:
+            # Sort the node and edge strings lexicographically.
+            # NOTE: this means that '10' precedes '2' in the sorted ordering
+            nlist.sort()
+            elist.sort()
+
+        if self.stringInput == False:
+            sstring += "# " + os.path.split(self.file)[1] + "\n\n"
+
+        sstring += "# " + str(len(nlist)) + " Nodes\n"
+        sstring += "# FORMAT: N, Primitive ID, Label, Weight\n"
+        for nstring in nlist:
+            sstring = sstring + nstring
+        sstring += "\n"
+
+        sstring += "# " + str(len(elist)) + " Edges\n"
+        sstring += (
+            "# FORMAT: E, Primitive ID (parent), Primitive ID (child), Label, Weight\n"
+        )
+        for estring in elist:
+            sstring = sstring + estring
+
+        return sstring
+
+    ##################################
+    # Construct segment-based graph
+    # for current graph state
+    ##################################
     def segmentGraph(self):
-            """Return dictionaries from segments to strokes, strokes to segments,
+        """Return dictionaries from segments to strokes, strokes to segments,
             segments without parents, and edges labeled as segment (w. symbol label)."""
-            primitiveSegmentMap = {}
-            segmentPrimitiveMap = {}
-            #noparentSegments = []
-            segmentEdges = {}  # Edges between detected objects (segments)
-
-            self.hideUnlabeledEdges()
-
-            # Note: a segmentation edge in either direction merges a primitive pair.
-            primSets = {}
-            for node,labs in self.nlabels.items():
-                    primSets[node] = {}
-                    for l in labs:
-                            (cost,_)=self.cmpNodes([l],[])
-                            if(cost > 0):
-                                    primSets[node][l] = set([node])
-                    #if len(primSets[node]) == 0:
-                    #	primSets[node]['_'] = set([node]) #at least one empty label
-            for (n1, n2) in list(self.elabels):
-                    commonLabels = set(list(self.nlabels[n1])).intersection(list(self.nlabels[n2]),list(self.elabels[(n1,n2)]))
-                    for l in commonLabels:
-                            #check if this label is interesting or not => compare to 'nothing', if there is not error, it means it is not interesting
-                            (cost,_)=self.cmpNodes([l],[])
-                            if(cost > 0):
-                                    primSets[n1][l].add(n2)
-                                    primSets[n2][l].add(n1)
-
-            # NOTE: Segments can have multiple labels
-            # A primitive can belong to several different
-            # segments with different sets of primitives with different labels.
-            # but there is only one segment with the same label attached to each primitive.
-            i = 0
-            segmentList = []
-            rootSegments = set([])
-            
-            # For each label associated with each primitive, there is a possible object/segment
-            for primitive,segments in primSets.items():
-                    if not primitive in primitiveSegmentMap:
-                            primitiveSegmentMap[ primitive ] = {}
-                    for lab in list(segments):
-                            alreadySegmented = False
-                            for j in range(len(segmentList)):
-                                    if segments[lab] == segmentList[j]["prim"]:
-                                            if not primitive in primitiveSegmentMap:
-                                                    primitiveSegmentMap[ primitive ] = {}
-                                            primitiveSegmentMap[ primitive ][lab] = 'Obj' + str(j)
-                                            alreadySegmented = True
-                                            if lab not in segmentList[j]["label"]:
-                                                    segmentPrimitiveMap[  'Obj' + str(j) ][1].append(lab)
-                                                    segmentList[j]["label"].add(lab)
-                                            break
-
-                            if not alreadySegmented:
-                                    # Add the new segment.
-                                    newSegment = 'Obj' + str(i)
-                                    segmentList = segmentList + [ {"label":{lab},"prim":primSets[primitive][lab]} ]
-                                    segmentPrimitiveMap[ newSegment ] = (segments[lab],[lab])
-                                    primitiveSegmentMap[ primitive ][lab] = newSegment
-                                    rootSegments.add(newSegment)
-                                    i += 1
-
-            # Identify 'root' objects/segments (i.e. with no incoming edges),
-            # and edges between objects. **We skip segmentation edges.
-            for (n1, n2), elabs in self.elabels.items():
-                    segment1 = primitiveSegmentMap[n1]
-                    segment2 = primitiveSegmentMap[n2]
-                    
-                    #for all possible pair of segments with these two primitives, look for the effective relation labels
-                    possibleRelationLabels = set(list(elabs)).difference(list(self.nlabels[n1]),list(self.nlabels[n2]))
-                    if len(possibleRelationLabels) != 0:
-                            #for all pair of labels
-                            for l1,pset1 in segment1.items():
-                                    for l2, pset2 in segment2.items():
-                                            #if not in the same seg
-                                            if pset1 != pset2:
-                                                    #look for the label which is common for all primitive pair in the two segments
-                                                    theRelationLab = possibleRelationLabels
-                                                    for p1 in primSets[n1][l1]:
-                                                            for p2 in primSets[n2][l2]:
-                                                                    if(p1,p2) in self.elabels:
-                                                                            theRelationLab &= set(list(self.elabels[(p1,p2)]))
-                                                                    else:
-                                                                            theRelationLab = set([]) # it should be a clique !
-                                                                    if len(theRelationLab) == 0:
-                                                                            break
-                                                            if len(theRelationLab) == 0:
-                                                                    break
-                                                    # there is a common relation if theRelationLab is not empty
-                                                    if len(theRelationLab) != 0:
-                                                            #we can remove seg2 from the roots
-                                                            if pset2 in rootSegments:
-                                                                    rootSegments.remove(pset2)
-                                                            #print (str((n1, n2))+ " => " + str(( pset1,  pset2)) + "  = " + str(theRelationLab))
-                                                            for label in theRelationLab:
-                                                                    #check if this label is interesting or not => compare to 'nothing', if there is not error, it means it is not interesting
-                                                                    (cost,_)=self.cmpNodes([label],[])
-                                                                    if(cost > 0):
-                                                                            if ( pset1,  pset2) in segmentEdges:
-                                                                                    if label in segmentEdges[ ( pset1,  pset2) ]:
-                                                                                            # Sum weights for repeated labels
-                                                                                            segmentEdges[ ( pset1,  pset2)][label] += \
-                                                                                                            self.elabels[(n1,n2)][label]
-                                                                                    else:
-                                                                                            # Add unaltered weights for new edge labels
-                                                                                            segmentEdges[ ( pset1,  pset2) ][label] = \
-                                                                                                            self.elabels[(n1,n2)][label]
-                                                                            else:
-                                                                                    segmentEdges[ ( pset1, pset2) ] = {}
-                                                                                    segmentEdges[ ( pset1, pset2) ][label] = \
-                                                                                                    self.elabels[(n1,n2)][label]
-
-            self.restoreUnlabeledEdges()
-
-            return (segmentPrimitiveMap, primitiveSegmentMap, list(rootSegments), \
-                            segmentEdges)
-
-
-##################################
-# Metrics and Graph Differences
-##################################
+        primitiveSegmentMap = {}
+        segmentPrimitiveMap = {}
+        # noparentSegments = []
+        segmentEdges = {}  # Edges between detected objects (segments)
+
+        self.hideUnlabeledEdges()
+
+        # Note: a segmentation edge in either direction merges a primitive pair.
+        primSets = {}
+        for node, labs in self.nlabels.items():
+            primSets[node] = {}
+            for l in labs:
+                (cost, _) = self.cmpNodes([l], [])
+                if cost > 0:
+                    primSets[node][l] = set([node])
+            # if len(primSets[node]) == 0:
+            # 	primSets[node]['_'] = set([node]) #at least one empty label
+        for (n1, n2) in list(self.elabels):
+            commonLabels = set(list(self.nlabels[n1])).intersection(
+                list(self.nlabels[n2]), list(self.elabels[(n1, n2)])
+            )
+            for l in commonLabels:
+                # check if this label is interesting or not => compare to 'nothing', if there is not error, it means it is not interesting
+                (cost, _) = self.cmpNodes([l], [])
+                if cost > 0:
+                    primSets[n1][l].add(n2)
+                    primSets[n2][l].add(n1)
+
+        # NOTE: Segments can have multiple labels
+        # A primitive can belong to several different
+        # segments with different sets of primitives with different labels.
+        # but there is only one segment with the same label attached to each primitive.
+        i = 0
+        segmentList = []
+        rootSegments = set([])
+
+        # For each label associated with each primitive, there is a possible object/segment
+        for primitive, segments in primSets.items():
+            if not primitive in primitiveSegmentMap:
+                primitiveSegmentMap[primitive] = {}
+            for lab in list(segments):
+                alreadySegmented = False
+                for j in range(len(segmentList)):
+                    if segments[lab] == segmentList[j]["prim"]:
+                        if not primitive in primitiveSegmentMap:
+                            primitiveSegmentMap[primitive] = {}
+                        primitiveSegmentMap[primitive][lab] = "Obj" + str(j)
+                        alreadySegmented = True
+                        if lab not in segmentList[j]["label"]:
+                            segmentPrimitiveMap["Obj" + str(j)][1].append(lab)
+                            segmentList[j]["label"].add(lab)
+                        break
+
+                if not alreadySegmented:
+                    # Add the new segment.
+                    newSegment = "Obj" + str(i)
+                    segmentList = segmentList + [
+                        {"label": {lab}, "prim": primSets[primitive][lab]}
+                    ]
+                    segmentPrimitiveMap[newSegment] = (segments[lab], [lab])
+                    primitiveSegmentMap[primitive][lab] = newSegment
+                    rootSegments.add(newSegment)
+                    i += 1
+
+        # Identify 'root' objects/segments (i.e. with no incoming edges),
+        # and edges between objects. **We skip segmentation edges.
+        for (n1, n2), elabs in self.elabels.items():
+            segment1 = primitiveSegmentMap[n1]
+            segment2 = primitiveSegmentMap[n2]
+
+            # for all possible pair of segments with these two primitives, look for the effective relation labels
+            possibleRelationLabels = set(list(elabs)).difference(
+                list(self.nlabels[n1]), list(self.nlabels[n2])
+            )
+            if len(possibleRelationLabels) != 0:
+                # for all pair of labels
+                for l1, pset1 in segment1.items():
+                    for l2, pset2 in segment2.items():
+                        # if not in the same seg
+                        if pset1 != pset2:
+                            # look for the label which is common for all primitive pair in the two segments
+                            theRelationLab = possibleRelationLabels
+                            for p1 in primSets[n1][l1]:
+                                for p2 in primSets[n2][l2]:
+                                    if (p1, p2) in self.elabels:
+                                        theRelationLab &= set(
+                                            list(self.elabels[(p1, p2)])
+                                        )
+                                    else:
+                                        theRelationLab = set(
+                                            []
+                                        )  # it should be a clique !
+                                    if len(theRelationLab) == 0:
+                                        break
+                                if len(theRelationLab) == 0:
+                                    break
+                            # there is a common relation if theRelationLab is not empty
+                            if len(theRelationLab) != 0:
+                                # we can remove seg2 from the roots
+                                if pset2 in rootSegments:
+                                    rootSegments.remove(pset2)
+                                # print (str((n1, n2))+ " => " + str(( pset1,  pset2)) + "  = " + str(theRelationLab))
+                                for label in theRelationLab:
+                                    # check if this label is interesting or not => compare to 'nothing', if there is not error, it means it is not interesting
+                                    (cost, _) = self.cmpNodes([label], [])
+                                    if cost > 0:
+                                        if (pset1, pset2) in segmentEdges:
+                                            if label in segmentEdges[(pset1, pset2)]:
+                                                # Sum weights for repeated labels
+                                                segmentEdges[(pset1, pset2)][
+                                                    label
+                                                ] += self.elabels[(n1, n2)][label]
+                                            else:
+                                                # Add unaltered weights for new edge labels
+                                                segmentEdges[(pset1, pset2)][
+                                                    label
+                                                ] = self.elabels[(n1, n2)][label]
+                                        else:
+                                            segmentEdges[(pset1, pset2)] = {}
+                                            segmentEdges[(pset1, pset2)][
+                                                label
+                                            ] = self.elabels[(n1, n2)][label]
+
+        self.restoreUnlabeledEdges()
+
+        return (
+            segmentPrimitiveMap,
+            primitiveSegmentMap,
+            list(rootSegments),
+            segmentEdges,
+        )
+
+    ##################################
+    # Metrics and Graph Differences
+    ##################################
     def compareSegments(self, lg2):
-            """Compute the number of differing segments, and record disagreements.
+        """Compute the number of differing segments, and record disagreements.
             The primitives in each graph should be of the same number and names
             (identifiers). Nodes are merged that have identical (label,value)
             pairs on nodes and all incoming and outgoing edges."""
-            (sp1, ps1, _, sre1) = self.segmentGraph()
-            (sp2, ps2, _, sre2) = lg2.segmentGraph()
-
-            allNodes = set(list(ps1))
-            assert allNodes == set(list(ps2))
-
-            edgeDiffCount = 0
-            edgeDiffClassCount = 0
-            segDiffs = {}
-            correctSegments = set([])
-            correctSegmentsAndClass = set([])
-            undirDiffClassSet = set([])
-            
-            # List and count errors due to segmentation.
-            # Use cmpNodes to compare the labels of symbols.
-            # Idea : build the sub graph with the current primitive as center and only 
-            for primitive in list(ps1):
-                    edgeFromP1 = {}
-                    edgeFromP2 = {}
-                    for (lab1,seg1) in ps1[primitive].items():
-                            for p in sp1[seg1][0]:
-                                    # DEBUG (RZ): this is producing a primitive edge-level count:
-                                    # do not count segment edges that are undefined (e.g. in one direction,
-                                    # but not the other)
-                                    if p != primitive and (p,primitive) in list(self.elabels) and \
-                                                    lab1 in list(self.elabels[ (p,primitive) ]):
-                                            if p in edgeFromP1:
-                                                    edgeFromP1[p].append(lab1)
-                                            else:  
-                                                    edgeFromP1[p] = [lab1]
-
-                    for (lab2,seg2) in ps2[primitive].items():
-                            for p in sp2[seg2][0]:
-                                    # DEBUG (RZ) - see DEBUG comment above.
-                                    if p != primitive and (p,primitive) in list(lg2.elabels) and \
-                                                    lab2 in list(lg2.elabels[ (p, primitive) ]):
-                                            if p in edgeFromP2:
-                                                    edgeFromP2[p].append(lab2)
-                                            else:
-                                                    edgeFromP2[p] = [lab2]
-
-                    # Compute differences in edge labels with cmpNodes (as they are symbol labels)
-                    diff1 = set([])
-                    diff2 = set([])
-                    
-                    # first add differences for shared primitives
-                    commonPrim = set(list(edgeFromP1)).intersection(list(edgeFromP2))
-                    for p in commonPrim:
-                            (cost,diff) = self.cmpNodes(edgeFromP1[p], edgeFromP2[p])
-                            edgeDiffCount = edgeDiffCount + cost
-
-                            # RZ June 2015: Record edges that are specifically valid merges with disagreeing labels.
-                            #     Also record sets of undirected edges that disagree.
-                            for (l1,l2) in diff:
-                                    if l1 in list(self.nlabels[p]) and l2 in list(lg2.nlabels[p]):
-                                            edgeDiffClassCount += 1
-                                    
-                                    # RZ: we do not have a *segmentation* difference if corresponding segm.
-                                    #     edges have a label.
-                                    elif cost > 0:
-                                            diff1.add(p)
-                                            diff2.add(p)
-
-                                    if not (p, primitive) in undirDiffClassSet and not (primitive, p) in undirDiffClassSet:
-                                            undirDiffClassSet.add( (primitive, p) )
-
-                    #then add differences for primitives which are not in the other set
-                    for p in (set(list(edgeFromP1)) - commonPrim):
-                            (cost,diff) = self.cmpNodes(edgeFromP1[p], [])
-                            edgeDiffCount = edgeDiffCount + cost
-                            diff1.add(p)
-                                    
-                    for p in (set(list(edgeFromP2)) - commonPrim):
-                            (cost,diff) = self.cmpNodes(edgeFromP2[p], [])
-                            edgeDiffCount = edgeDiffCount + cost
-                            diff2.add(p)
-                                    
-
-                    # Only create an entry where there are disagreements.
-                    if len(diff1) + len(diff2) > 0:
-                            segDiffs[primitive] = ( diff1, diff2 )
-                    
-            # RZ: Oct. 2014 - replacing method used to evaluate segmentation. Also
-            #     add checks for segments in the target being disjoint.
-            #
-            # Objects are defined by a set of primitives, plus a label. 
-            # NOTE: This currently will support mutliple labels, but will lead to invalid
-            #   "Class/Det" values in 00_Summary.txt if there are multiple labels.
-            targets = {}
-
-            # RZ: Add mapping from primitive list to object ids for direct lookup.
-            targetObjIds = {}
-            matchedTargets = set()
-            for ObjID in list(sp2):
-                    # Skip absent nodes - they are not valid targets.
-                    if 'ABSENT' not in sp2[ ObjID ][ 1 ]:
-                            # Convert primitive set to a sorted tuple list.
-                            primitiveTupleList = tuple( sorted( list( sp2[ ObjID ][ 0 ] ) ) )
-                    
-                            # Store target label in targets dict, matches in matchedTargets dict (false init.)
-                            targets[ primitiveTupleList ] = sp2[ ObjID][1]
-                            targetObjIds[ primitiveTupleList ] = ObjID
-            
-            # Look for matches.
-            # Do *not* allow a primitive set to be matched more than once.
-            for ObjID in list(sp1):
-                    # HACK (RZ): DEBUG - was not checking whether matched objects were
-                    #               missing before absent nodes were added.
-                    if 'ABSENT' in sp1[ ObjID ][ 1 ]:
-                            continue
-
-                    primitiveTupleList = tuple( sorted( list(sp1[ObjID][ 0 ] )))
-                    if primitiveTupleList in list(targets) \
-                                    and not primitiveTupleList in matchedTargets:
-                            matchedTargets.add( primitiveTupleList )
-                            correctSegments.add( ObjID )
-                            
-                            # Obtain matching labels. Create list of correct (segmentId, label) pairs
-                            # for *all* matching labels.
-                            # DEBUG: empty lists were being matched! Added test for empty matches.
-                            # WARNING: Only guaranteed to work for single labels.
-                            outputLabels = set(sp1[ ObjID ][ 1 ])
-                            matchingLabels = list( outputLabels.intersection( targets[ primitiveTupleList ] ) )
-                            if len(matchingLabels) > 0:
-                                    ObjIDRepeats = [ObjID] * len(matchingLabels)
-                                    correctSegmentsAndClass.add( tuple( zip(ObjIDRepeats, list(matchingLabels))))
-
-            # Compute total number of object classifications (recognition targets)
-            nbSegmClass = 0
-            for (_,labs) in sp2.items():
-                    nbSegmClass += len(labs[1])
-
-            # Compute the specific 'object-level' graph edges that disagree, at the
-            # level of primitive-pairs. 
-            segRelErrors = 0
-            correctSegRels = 0
-            correctSegRelLocations = 0
-            primRelEdgeDiffs = {}
-            
-            # Iterate over object relationships in the output graph.
-            for thisPair in list(sre1):
-                    misLabeled = False
-                    falsePositive = False
-
-                    thisParentIds = set(sp1[ thisPair[0] ][0])
-                    thisChildIds = set(sp1[thisPair[1] ][0])
-                    
-                    # RZ (June 2015): Obtain names for correct segments in target graph (lg2)
-                    primitiveTupleListParent = tuple( sorted( list( thisParentIds )))
-                    primitiveTupleListChild =  tuple( sorted( list ( thisChildIds )))
-                    targetObjNameParent = None
-                    targetObjNameChild = None
-
-                    if primitiveTupleListParent in list(targetObjIds):
-                            targetObjNameParent = targetObjIds[ primitiveTupleListParent ]
-                    if primitiveTupleListChild in list(targetObjIds):
-                            targetObjNameChild = targetObjIds[ primitiveTupleListChild ]
-                    
-                    # Check whether the objects are correctly segmented by their object identifiers
-                    if not ( thisPair[0] in correctSegments and  thisPair[1] in correctSegments):
-                            # Avoid counting mis-segmented objects as having valid relationships
-                            falsePositive = True
-                    elif not ( targetObjNameParent, targetObjNameChild ) in list(sre2):
-                            # Check that there is an edge between these objects in the target graph.
-                            falsePositive = True
-                    else:
-                            # RZ (June, 2015): Compare labels directly on relation edges.
-                            # WARNING: This checks that *all* labels are identical. Fine for single labels.
-                            if not sorted( list(sre1[ thisPair ]) ) == \
-                                            sorted( list(sre2[ ( targetObjNameParent, targetObjNameChild )]) ):
-                                    misLabeled = True
-                                    
-                    # NOTE: assumes single labels on primitives.
-                    # primRelEdgeDiffs records which object pairs have incorrect labels.
-                    if falsePositive or misLabeled:
-                            self.error = True
-                            segRelErrors += 1
-                            primRelEdgeDiffs[ thisPair ] = [ ('Error',1.0) ]
-                    else:
-                            correctSegRels += 1
-                    
-                    # Count correct relationship structures/locations.
-                    if not falsePositive:
-                            correctSegRelLocations += 1
-
-            # Compute object counts *without* inserted absent nodes.
-            lg2.removeAbsent()
-            self.removeAbsent()
-
-            (sp2orig, ps2orig, _, sre2orig) = lg2.segmentGraph()
-            (sp1orig, ps1orig, _, sre1orig) = self.segmentGraph()
-            
-            nLg2Objs = len(list(sp2orig)) 
-            nLg1Objs = len(list(sp1orig)) 
-
-            # For input file, need to compare against all objects after including
-            # missing/additional absent nodes and edges.
-            nLg1ObjsWithAbsent = len(list(sp1))
-
-            lg2.addAbsent(self)
-            self.addAbsent(lg2)
-            
-            # RZ (Oct. 2014) Adding indicator variables for different correctness scenarios.
-            hasCorrectSegments = 1 if len(correctSegments) == nLg2Objs and \
-                            len(correctSegments) == nLg1ObjsWithAbsent else 0
-            hasCorrectSegmentsAndLabels = 1 if len(correctSegmentsAndClass) == nLg2Objs and \
-                            len(correctSegmentsAndClass) == nLg1ObjsWithAbsent else 0
-            
-            hasCorrectRelationLocations = 1 if correctSegRelLocations == len(list(sre1)) and \
-                            correctSegRelLocations == len(list(sre2)) else 0
-            hasCorrectRelationsAndLabels =  1 if correctSegRels == len(list(sre1)) and \
-                            correctSegRels == len(list(sre2)) else 0
-            
-            hasCorrectStructure = hasCorrectRelationLocations and hasCorrectSegments
-            
-            # Compile vector of (name, value) metric pairs.
-            metrics = [
-                    ("edgeDiffClassCount", edgeDiffClassCount),
-                    ("undirDiffClassCount", len(undirDiffClassSet)),
-                    
-                    ("nSeg", nLg2Objs),
-                    ("detectedSeg", nLg1Objs),
-                    ("dSegRelEdges", len(list(sre1))),
-                    ("CorrectSegments", len(correctSegments)),
-                ("CorrectSegmentsAndClass", len(correctSegmentsAndClass)),
-                    ("ClassError", nbSegmClass - len(correctSegmentsAndClass)), 
-                    ("CorrectSegRels",correctSegRels),
-                    ("CorrectSegRelLocations",correctSegRelLocations),
-                    ("SegRelErrors", segRelErrors),
-                    
-                    ("hasCorrectSegments", hasCorrectSegments),
-                    ("hasCorrectSegLab", hasCorrectSegmentsAndLabels), 
-                    ("hasCorrectRelationLocations", hasCorrectRelationLocations),
-                    ("hasCorrectRelLab", hasCorrectRelationsAndLabels),
-                    ("hasCorrectStructure", hasCorrectStructure) ]
-
-            # RZ: June 2015 - need to subtract misclassified edges from non-matching edges
-            # to obtain correct "Delta S" (D_S) Hamming distance for mismatched
-            # segmentation edges.
-            segEdgeMismatch = edgeDiffCount - edgeDiffClassCount
-
-            return (segEdgeMismatch, segDiffs, correctSegments, metrics, primRelEdgeDiffs)
+        (sp1, ps1, _, sre1) = self.segmentGraph()
+        (sp2, ps2, _, sre2) = lg2.segmentGraph()
+
+        allNodes = set(list(ps1))
+        assert allNodes == set(list(ps2))
+
+        edgeDiffCount = 0
+        edgeDiffClassCount = 0
+        segDiffs = {}
+        correctSegments = set([])
+        correctSegmentsAndClass = set([])
+        undirDiffClassSet = set([])
+
+        # List and count errors due to segmentation.
+        # Use cmpNodes to compare the labels of symbols.
+        # Idea : build the sub graph with the current primitive as center and only
+        for primitive in list(ps1):
+            edgeFromP1 = {}
+            edgeFromP2 = {}
+            for (lab1, seg1) in ps1[primitive].items():
+                for p in sp1[seg1][0]:
+                    # DEBUG (RZ): this is producing a primitive edge-level count:
+                    # do not count segment edges that are undefined (e.g. in one direction,
+                    # but not the other)
+                    if (
+                        p != primitive
+                        and (p, primitive) in list(self.elabels)
+                        and lab1 in list(self.elabels[(p, primitive)])
+                    ):
+                        if p in edgeFromP1:
+                            edgeFromP1[p].append(lab1)
+                        else:
+                            edgeFromP1[p] = [lab1]
+
+            for (lab2, seg2) in ps2[primitive].items():
+                for p in sp2[seg2][0]:
+                    # DEBUG (RZ) - see DEBUG comment above.
+                    if (
+                        p != primitive
+                        and (p, primitive) in list(lg2.elabels)
+                        and lab2 in list(lg2.elabels[(p, primitive)])
+                    ):
+                        if p in edgeFromP2:
+                            edgeFromP2[p].append(lab2)
+                        else:
+                            edgeFromP2[p] = [lab2]
+
+            # Compute differences in edge labels with cmpNodes (as they are symbol labels)
+            diff1 = set([])
+            diff2 = set([])
+
+            # first add differences for shared primitives
+            commonPrim = set(list(edgeFromP1)).intersection(list(edgeFromP2))
+            for p in commonPrim:
+                (cost, diff) = self.cmpNodes(edgeFromP1[p], edgeFromP2[p])
+                edgeDiffCount = edgeDiffCount + cost
+
+                # RZ June 2015: Record edges that are specifically valid merges with disagreeing labels.
+                #     Also record sets of undirected edges that disagree.
+                for (l1, l2) in diff:
+                    if l1 in list(self.nlabels[p]) and l2 in list(lg2.nlabels[p]):
+                        edgeDiffClassCount += 1
+
+                    # RZ: we do not have a *segmentation* difference if corresponding segm.
+                    #     edges have a label.
+                    elif cost > 0:
+                        diff1.add(p)
+                        diff2.add(p)
+
+                    if (
+                        not (p, primitive) in undirDiffClassSet
+                        and not (primitive, p) in undirDiffClassSet
+                    ):
+                        undirDiffClassSet.add((primitive, p))
+
+            # then add differences for primitives which are not in the other set
+            for p in set(list(edgeFromP1)) - commonPrim:
+                (cost, diff) = self.cmpNodes(edgeFromP1[p], [])
+                edgeDiffCount = edgeDiffCount + cost
+                diff1.add(p)
+
+            for p in set(list(edgeFromP2)) - commonPrim:
+                (cost, diff) = self.cmpNodes(edgeFromP2[p], [])
+                edgeDiffCount = edgeDiffCount + cost
+                diff2.add(p)
+
+            # Only create an entry where there are disagreements.
+            if len(diff1) + len(diff2) > 0:
+                segDiffs[primitive] = (diff1, diff2)
+
+        # RZ: Oct. 2014 - replacing method used to evaluate segmentation. Also
+        #     add checks for segments in the target being disjoint.
+        #
+        # Objects are defined by a set of primitives, plus a label.
+        # NOTE: This currently will support mutliple labels, but will lead to invalid
+        #   "Class/Det" values in 00_Summary.txt if there are multiple labels.
+        targets = {}
+
+        # RZ: Add mapping from primitive list to object ids for direct lookup.
+        targetObjIds = {}
+        matchedTargets = set()
+        for ObjID in list(sp2):
+            # Skip absent nodes - they are not valid targets.
+            if "ABSENT" not in sp2[ObjID][1]:
+                # Convert primitive set to a sorted tuple list.
+                primitiveTupleList = tuple(sorted(list(sp2[ObjID][0])))
+
+                # Store target label in targets dict, matches in matchedTargets dict (false init.)
+                targets[primitiveTupleList] = sp2[ObjID][1]
+                targetObjIds[primitiveTupleList] = ObjID
+
+        # Look for matches.
+        # Do *not* allow a primitive set to be matched more than once.
+        for ObjID in list(sp1):
+            # HACK (RZ): DEBUG - was not checking whether matched objects were
+            #               missing before absent nodes were added.
+            if "ABSENT" in sp1[ObjID][1]:
+                continue
+
+            primitiveTupleList = tuple(sorted(list(sp1[ObjID][0])))
+            if (
+                primitiveTupleList in list(targets)
+                and not primitiveTupleList in matchedTargets
+            ):
+                matchedTargets.add(primitiveTupleList)
+                correctSegments.add(ObjID)
+
+                # Obtain matching labels. Create list of correct (segmentId, label) pairs
+                # for *all* matching labels.
+                # DEBUG: empty lists were being matched! Added test for empty matches.
+                # WARNING: Only guaranteed to work for single labels.
+                outputLabels = set(sp1[ObjID][1])
+                matchingLabels = list(
+                    outputLabels.intersection(targets[primitiveTupleList])
+                )
+                if len(matchingLabels) > 0:
+                    ObjIDRepeats = [ObjID] * len(matchingLabels)
+                    correctSegmentsAndClass.add(
+                        tuple(zip(ObjIDRepeats, list(matchingLabels)))
+                    )
+
+        # Compute total number of object classifications (recognition targets)
+        nbSegmClass = 0
+        for (_, labs) in sp2.items():
+            nbSegmClass += len(labs[1])
+
+        # Compute the specific 'object-level' graph edges that disagree, at the
+        # level of primitive-pairs.
+        segRelErrors = 0
+        correctSegRels = 0
+        correctSegRelLocations = 0
+        primRelEdgeDiffs = {}
+
+        # Iterate over object relationships in the output graph.
+        for thisPair in list(sre1):
+            misLabeled = False
+            falsePositive = False
+
+            thisParentIds = set(sp1[thisPair[0]][0])
+            thisChildIds = set(sp1[thisPair[1]][0])
+
+            # RZ (June 2015): Obtain names for correct segments in target graph (lg2)
+            primitiveTupleListParent = tuple(sorted(list(thisParentIds)))
+            primitiveTupleListChild = tuple(sorted(list(thisChildIds)))
+            targetObjNameParent = None
+            targetObjNameChild = None
+
+            if primitiveTupleListParent in list(targetObjIds):
+                targetObjNameParent = targetObjIds[primitiveTupleListParent]
+            if primitiveTupleListChild in list(targetObjIds):
+                targetObjNameChild = targetObjIds[primitiveTupleListChild]
+
+            # Check whether the objects are correctly segmented by their object identifiers
+            if not (thisPair[0] in correctSegments and thisPair[1] in correctSegments):
+                # Avoid counting mis-segmented objects as having valid relationships
+                falsePositive = True
+            elif not (targetObjNameParent, targetObjNameChild) in list(sre2):
+                # Check that there is an edge between these objects in the target graph.
+                falsePositive = True
+            else:
+                # RZ (June, 2015): Compare labels directly on relation edges.
+                # WARNING: This checks that *all* labels are identical. Fine for single labels.
+                if not sorted(list(sre1[thisPair])) == sorted(
+                    list(sre2[(targetObjNameParent, targetObjNameChild)])
+                ):
+                    misLabeled = True
+
+            # NOTE: assumes single labels on primitives.
+            # primRelEdgeDiffs records which object pairs have incorrect labels.
+            if falsePositive or misLabeled:
+                self.error = True
+                segRelErrors += 1
+                primRelEdgeDiffs[thisPair] = [("Error", 1.0)]
+            else:
+                correctSegRels += 1
+
+            # Count correct relationship structures/locations.
+            if not falsePositive:
+                correctSegRelLocations += 1
+
+        # Compute object counts *without* inserted absent nodes.
+        lg2.removeAbsent()
+        self.removeAbsent()
+
+        (sp2orig, ps2orig, _, sre2orig) = lg2.segmentGraph()
+        (sp1orig, ps1orig, _, sre1orig) = self.segmentGraph()
+
+        nLg2Objs = len(list(sp2orig))
+        nLg1Objs = len(list(sp1orig))
+
+        # For input file, need to compare against all objects after including
+        # missing/additional absent nodes and edges.
+        nLg1ObjsWithAbsent = len(list(sp1))
+
+        lg2.addAbsent(self)
+        self.addAbsent(lg2)
+
+        # RZ (Oct. 2014) Adding indicator variables for different correctness scenarios.
+        hasCorrectSegments = (
+            1
+            if len(correctSegments) == nLg2Objs
+            and len(correctSegments) == nLg1ObjsWithAbsent
+            else 0
+        )
+        hasCorrectSegmentsAndLabels = (
+            1
+            if len(correctSegmentsAndClass) == nLg2Objs
+            and len(correctSegmentsAndClass) == nLg1ObjsWithAbsent
+            else 0
+        )
+
+        hasCorrectRelationLocations = (
+            1
+            if correctSegRelLocations == len(list(sre1))
+            and correctSegRelLocations == len(list(sre2))
+            else 0
+        )
+        hasCorrectRelationsAndLabels = (
+            1
+            if correctSegRels == len(list(sre1)) and correctSegRels == len(list(sre2))
+            else 0
+        )
+
+        hasCorrectStructure = hasCorrectRelationLocations and hasCorrectSegments
+
+        # Compile vector of (name, value) metric pairs.
+        metrics = [
+            ("edgeDiffClassCount", edgeDiffClassCount),
+            ("undirDiffClassCount", len(undirDiffClassSet)),
+            ("nSeg", nLg2Objs),
+            ("detectedSeg", nLg1Objs),
+            ("dSegRelEdges", len(list(sre1))),
+            ("CorrectSegments", len(correctSegments)),
+            ("CorrectSegmentsAndClass", len(correctSegmentsAndClass)),
+            ("ClassError", nbSegmClass - len(correctSegmentsAndClass)),
+            ("CorrectSegRels", correctSegRels),
+            ("CorrectSegRelLocations", correctSegRelLocations),
+            ("SegRelErrors", segRelErrors),
+            ("hasCorrectSegments", hasCorrectSegments),
+            ("hasCorrectSegLab", hasCorrectSegmentsAndLabels),
+            ("hasCorrectRelationLocations", hasCorrectRelationLocations),
+            ("hasCorrectRelLab", hasCorrectRelationsAndLabels),
+            ("hasCorrectStructure", hasCorrectStructure),
+        ]
+
+        # RZ: June 2015 - need to subtract misclassified edges from non-matching edges
+        # to obtain correct "Delta S" (D_S) Hamming distance for mismatched
+        # segmentation edges.
+        segEdgeMismatch = edgeDiffCount - edgeDiffClassCount
+
+        return (segEdgeMismatch, segDiffs, correctSegments, metrics, primRelEdgeDiffs)
 
     def compare(self, lg2):
-            """Returns: 1. a list of (metric,value) pairs,
+        """Returns: 1. a list of (metric,value) pairs,
             2. a list of (n1,n2) node disagreements, 3. (e1,e2) pairs
             for edge disagreements, 4. dictionary from primitives to
             disagreeing segment graph edges for (self, lg2). Node and 
             edge labels are compared using label sets without values, and
             *not* labels sorted by value."""
-            metrics  = []
-            nodeconflicts = []
-            edgeconflicts = []
-
-            # HM: use the union of all node labels instead of only lg2 ones
-            #     it changes the nlabelMismatch, nodeClassError and so D_C and all rates values
-            allNodes = set(list(lg2.nlabels)).union(list(self.nlabels))
-            numNodes = len(allNodes)
-            (sp2, ps2, _, sre2) = lg2.segmentGraph()
-            nSegRelEdges = len(sre2)
-
-            # Handle case of empty graphs, and missing primitives.
-            # SIDE EFFECT: 'ABSENT' nodes added to each graph.
-            self.matchAbsent(lg2)
-
-            # METRICS
-            # Node and edge labels are considered as sets.
-            nlabelMismatch = 0
-            numEdges = numNodes * (numNodes - 1)  # No self-edges.
-            numLabels = numNodes + numEdges
-            elabelMismatch = 0
-
-            # Mismatched nodes.
-            nodeClassError = set()
-            for nid in allNodes: #list(self.nlabels):
-                    (cost,errL) = self.cmpNodes(list(self.nlabels[nid]),list(lg2.nlabels[nid]))
-                    #if there is some error
-                    if cost > 0:
-                            # add mismatch
-                            nlabelMismatch = nlabelMismatch + cost
-                            # add errors in error list
-                            for (l1,l2) in errL:
-                                    nodeconflicts = nodeconflicts + [ (nid, [ (l1, 1.0) ], [(l2, 1.0)] ) ]
-                            # add node in error list
-                            nodeClassError = nodeClassError.union([nid])
-
-            # Two-sided comparison of *label sets* (look from absent edges in both
-            # graphs!) Must check whether edge exists; '_' represents a "NONE"
-            # label (no edge).
-
-            # Identify the set of nodes with disagreeing edges.
-            nodeEdgeError = set()
-            for (graph,oGraph) in [ (self,lg2), (lg2,self) ]:
-                    for npair in list(graph.elabels):
-                            if not npair in oGraph.elabels \
-                                            and (not graph.elabels[ npair ] == ['_']):
-                                    (cost,errL) = self.cmpEdges(list(graph.elabels[ npair ]),['_'])
-                                    elabelMismatch = elabelMismatch + cost
-
-                                    (a,b) = npair
-                                    
-                                    # Record nodes in invalid edge
-                                    nodeEdgeError.update([a,b])
-
-                                    # DEBUG: Need to indicate correctly *which* graph has the
-                                    # missing edge; this graph (1st) or the other (listed 2nd).
-                                    if graph == self:
-                                            for (l1,l2) in errL:
-                                                    edgeconflicts.append((npair, [ (l1, 1.0) ], [(l2, 1.0)] ) )
-                                            
-                                    else:
-                                            for (l1,l2) in errL:
-                                                    edgeconflicts.append((npair, [ (l2, 1.0) ], [(l1, 1.0)] ) )
-
-            # Obtain number of primitives with an error of any sort.
-            nodeError = nodeClassError.union(nodeEdgeError)
-
-            # One-sided comparison for common edges. Compared by cmpEdges
-            for npair in list(self.elabels):
-                    if npair in list(lg2.elabels):
-                            (cost,errL) = self.cmpEdges(list(self.elabels[npair]),list(lg2.elabels[npair]))
-                            if cost > 0:
-                                    elabelMismatch = elabelMismatch + cost
-                                    (a,b) = npair
-                                    
-                                    # Record nodes in invalid edge
-                                    nodeEdgeError.update([a,b])
-                                    for (l1,l2) in errL:
-                                            edgeconflicts.append((npair, [ (l1, 1.0) ], [(l2, 1.0)] ) )
-
-            # Now compute segmentation differences.
-            (segEdgeMismatch, segDiffs, correctSegs, segmentMetrics, segRelDiffs) \
-                            = self.compareSegments(lg2)
-
-            # UNDIRECTED/NODE PAIR METRICS
-            # Compute number of invalid nodePairs
-            badPairs = {}
-            for ((n1, n2), _, _) in edgeconflicts:
-                    if not (n2, n1) in badPairs:
-                            badPairs[(n1, n2)] = True
-            incorrectPairs = len(badPairs)
-
-            # Compute number of mis-segmented node pairs.
-            badSegPairs = set([])
-            for node in list(segDiffs):
-                    for other in segDiffs[node][0]:
-                            if node != other and (other, node) not in badSegPairs:
-                                    badSegPairs.add((node, other))
-                    for other in segDiffs[node][1]:
-                            if  node != other and (other, node)not in badSegPairs:
-                                    badSegPairs.add((node, other))
-            segPairErrors = len(badSegPairs)
-
-            # Compute performance metrics; avoid divisions by 0.
-            cerror = ("D_C", nlabelMismatch) 
-            lerror = ("D_L", elabelMismatch) 
-            serror = ("D_S", segEdgeMismatch) 
-            rerror = ("D_R", elabelMismatch - segEdgeMismatch)
-            aerror = ("D_B", nlabelMismatch + elabelMismatch) 
-
-            # DEBUG:
-            # Delta E BASE CASE: for a single node, which is absent in the other
-            # file, set label and segment edge mismatches to 1 (in order
-            # to obtain 1.0 as the error metric, i.e. total error).
-            if len(list(self.nlabels)) == 1 and \
-                            (len(self.absentNodes) > 0 or \
-                            len(lg2.absentNodes) > 0):
-                    elabelMismatch = 1
-                    segEdgeMismatch = 1
-            
-            errorVal = 0.0
-            if numEdges > 0:
-                    errorVal +=  math.sqrt(float(segEdgeMismatch) / numEdges) + \
-                                     math.sqrt(float(elabelMismatch) / numEdges)
-            if numNodes > 0:
-                    errorVal += float(nlabelMismatch) / numNodes
-            errorVal = errorVal / 3.0
-            eerror  = ("D_E(%)", errorVal)
-
-            # Compile metrics
-            metrics = metrics + [ aerror, cerror, lerror, rerror, serror,  \
-                            eerror, \
-                            ("nNodes",numNodes), ("nEdges", numEdges), \
-                            ("nSegRelEdges", nSegRelEdges), \
-                            ("dPairs",incorrectPairs),("segPairErrors",segPairErrors),
-                            ("nodeCorrect", numNodes - len(nodeError)) ]
-                            
-            metrics = metrics + segmentMetrics
-
-            return (metrics, nodeconflicts, edgeconflicts, segDiffs, correctSegs,\
-                            segRelDiffs)
-            
-##################################
-# Manipulation/'Mutation'
-##################################
+        metrics = []
+        nodeconflicts = []
+        edgeconflicts = []
+
+        # HM: use the union of all node labels instead of only lg2 ones
+        #     it changes the nlabelMismatch, nodeClassError and so D_C and all rates values
+        allNodes = set(list(lg2.nlabels)).union(list(self.nlabels))
+        numNodes = len(allNodes)
+        (sp2, ps2, _, sre2) = lg2.segmentGraph()
+        nSegRelEdges = len(sre2)
+
+        # Handle case of empty graphs, and missing primitives.
+        # SIDE EFFECT: 'ABSENT' nodes added to each graph.
+        self.matchAbsent(lg2)
+
+        # METRICS
+        # Node and edge labels are considered as sets.
+        nlabelMismatch = 0
+        numEdges = numNodes * (numNodes - 1)  # No self-edges.
+        numLabels = numNodes + numEdges
+        elabelMismatch = 0
+
+        # Mismatched nodes.
+        nodeClassError = set()
+        for nid in allNodes:  # list(self.nlabels):
+            (cost, errL) = self.cmpNodes(
+                list(self.nlabels[nid]), list(lg2.nlabels[nid])
+            )
+            # if there is some error
+            if cost > 0:
+                # add mismatch
+                nlabelMismatch = nlabelMismatch + cost
+                # add errors in error list
+                for (l1, l2) in errL:
+                    nodeconflicts = nodeconflicts + [(nid, [(l1, 1.0)], [(l2, 1.0)])]
+                # add node in error list
+                nodeClassError = nodeClassError.union([nid])
+
+        # Two-sided comparison of *label sets* (look from absent edges in both
+        # graphs!) Must check whether edge exists; '_' represents a "NONE"
+        # label (no edge).
+
+        # Identify the set of nodes with disagreeing edges.
+        nodeEdgeError = set()
+        for (graph, oGraph) in [(self, lg2), (lg2, self)]:
+            for npair in list(graph.elabels):
+                if not npair in oGraph.elabels and (not graph.elabels[npair] == ["_"]):
+                    (cost, errL) = self.cmpEdges(list(graph.elabels[npair]), ["_"])
+                    elabelMismatch = elabelMismatch + cost
+
+                    (a, b) = npair
+
+                    # Record nodes in invalid edge
+                    nodeEdgeError.update([a, b])
+
+                    # DEBUG: Need to indicate correctly *which* graph has the
+                    # missing edge; this graph (1st) or the other (listed 2nd).
+                    if graph == self:
+                        for (l1, l2) in errL:
+                            edgeconflicts.append((npair, [(l1, 1.0)], [(l2, 1.0)]))
+
+                    else:
+                        for (l1, l2) in errL:
+                            edgeconflicts.append((npair, [(l2, 1.0)], [(l1, 1.0)]))
+
+        # Obtain number of primitives with an error of any sort.
+        nodeError = nodeClassError.union(nodeEdgeError)
+
+        # One-sided comparison for common edges. Compared by cmpEdges
+        for npair in list(self.elabels):
+            if npair in list(lg2.elabels):
+                (cost, errL) = self.cmpEdges(
+                    list(self.elabels[npair]), list(lg2.elabels[npair])
+                )
+                if cost > 0:
+                    elabelMismatch = elabelMismatch + cost
+                    (a, b) = npair
+
+                    # Record nodes in invalid edge
+                    nodeEdgeError.update([a, b])
+                    for (l1, l2) in errL:
+                        edgeconflicts.append((npair, [(l1, 1.0)], [(l2, 1.0)]))
+
+        # Now compute segmentation differences.
+        (
+            segEdgeMismatch,
+            segDiffs,
+            correctSegs,
+            segmentMetrics,
+            segRelDiffs,
+        ) = self.compareSegments(lg2)
+
+        # UNDIRECTED/NODE PAIR METRICS
+        # Compute number of invalid nodePairs
+        badPairs = {}
+        for ((n1, n2), _, _) in edgeconflicts:
+            if not (n2, n1) in badPairs:
+                badPairs[(n1, n2)] = True
+        incorrectPairs = len(badPairs)
+
+        # Compute number of mis-segmented node pairs.
+        badSegPairs = set([])
+        for node in list(segDiffs):
+            for other in segDiffs[node][0]:
+                if node != other and (other, node) not in badSegPairs:
+                    badSegPairs.add((node, other))
+            for other in segDiffs[node][1]:
+                if node != other and (other, node) not in badSegPairs:
+                    badSegPairs.add((node, other))
+        segPairErrors = len(badSegPairs)
+
+        # Compute performance metrics; avoid divisions by 0.
+        cerror = ("D_C", nlabelMismatch)
+        lerror = ("D_L", elabelMismatch)
+        serror = ("D_S", segEdgeMismatch)
+        rerror = ("D_R", elabelMismatch - segEdgeMismatch)
+        aerror = ("D_B", nlabelMismatch + elabelMismatch)
+
+        # DEBUG:
+        # Delta E BASE CASE: for a single node, which is absent in the other
+        # file, set label and segment edge mismatches to 1 (in order
+        # to obtain 1.0 as the error metric, i.e. total error).
+        if len(list(self.nlabels)) == 1 and (
+            len(self.absentNodes) > 0 or len(lg2.absentNodes) > 0
+        ):
+            elabelMismatch = 1
+            segEdgeMismatch = 1
+
+        errorVal = 0.0
+        if numEdges > 0:
+            errorVal += math.sqrt(float(segEdgeMismatch) / numEdges) + math.sqrt(
+                float(elabelMismatch) / numEdges
+            )
+        if numNodes > 0:
+            errorVal += float(nlabelMismatch) / numNodes
+        errorVal = errorVal / 3.0
+        eerror = ("D_E(%)", errorVal)
+
+        # Compile metrics
+        metrics = metrics + [
+            aerror,
+            cerror,
+            lerror,
+            rerror,
+            serror,
+            eerror,
+            ("nNodes", numNodes),
+            ("nEdges", numEdges),
+            ("nSegRelEdges", nSegRelEdges),
+            ("dPairs", incorrectPairs),
+            ("segPairErrors", segPairErrors),
+            ("nodeCorrect", numNodes - len(nodeError)),
+        ]
+
+        metrics = metrics + segmentMetrics
+
+        return (
+            metrics,
+            nodeconflicts,
+            edgeconflicts,
+            segDiffs,
+            correctSegs,
+            segRelDiffs,
+        )
+
+    ##################################
+    # Manipulation/'Mutation'
+    ##################################
     def separateTreeEdges(self):
-            """Return a list of root nodes, and two lists of edges corresponding to 
+        """Return a list of root nodes, and two lists of edges corresponding to 
             tree/forest edges, and the remaining edges."""
 
-            # First, obtain segments; perform extraction on edges over segments.
-            (segmentPrimitiveMap, primitiveSegmentMap, noparentSegments, \
-                            segmentEdges) = self.segmentGraph()
-
-            # Collect parents and children for each node; identify root nodes.
-            # (NOTE: root nodes provided already as noparentSegments)
-            nodeParentMap = {}
-            nodeChildMap = {}
-            rootNodes = set(list(segmentPrimitiveMap))
-            for (parent, child) in segmentEdges:
-                    if not child in list(nodeParentMap):
-                            nodeParentMap[ child ] = [ parent ]
-                            rootNodes.remove( child )
-                    else:
-                            nodeParentMap[ child ] += [ parent ]
+        # First, obtain segments; perform extraction on edges over segments.
+        (
+            segmentPrimitiveMap,
+            primitiveSegmentMap,
+            noparentSegments,
+            segmentEdges,
+        ) = self.segmentGraph()
+
+        # Collect parents and children for each node; identify root nodes.
+        # (NOTE: root nodes provided already as noparentSegments)
+        nodeParentMap = {}
+        nodeChildMap = {}
+        rootNodes = set(list(segmentPrimitiveMap))
+        for (parent, child) in segmentEdges:
+            if not child in list(nodeParentMap):
+                nodeParentMap[child] = [parent]
+                rootNodes.remove(child)
+            else:
+                nodeParentMap[child] += [parent]
+
+            if not parent in list(nodeChildMap):
+                nodeChildMap[parent] = [child]
+            else:
+                nodeChildMap[parent] += [child]
 
-                    if not parent in list(nodeChildMap):
-                            nodeChildMap[ parent ] = [ child ]
+        # Separate non-tree edges, traversing from the root.
+        fringe = list(rootNodes)
+
+        # Filter non-tree edges.
+        nonTreeEdges = set([])
+        while len(fringe) > 0:
+            nextNode = fringe.pop(0)
+
+            # Skip leaf nodes.
+            if nextNode in list(nodeChildMap):
+                children = copy.deepcopy(nodeChildMap[nextNode])
+
+                for child in children:
+                    numChildParents = len(nodeParentMap[child])
+
+                    # Filter edges to children that have more than
+                    # one parent (i.e. other than nextNode)
+                    if numChildParents == 1:
+                        # Child in the tree found, put on fringe.
+                        fringe += [child]
                     else:
-                            nodeChildMap[ parent ] += [ child ]
-
-            # Separate non-tree edges, traversing from the root.
-            fringe = list(rootNodes)
-
-            # Filter non-tree edges.
-            nonTreeEdges = set([])
-            while len(fringe) > 0:
-                    nextNode = fringe.pop(0)
-
-                    # Skip leaf nodes.
-                    if nextNode in list(nodeChildMap):
-                            children = copy.deepcopy(nodeChildMap[ nextNode ])
-                            
-                            for child in children:
-                                    numChildParents = len( nodeParentMap[ child ] )
-
-                                    # Filter edges to children that have more than
-                                    # one parent (i.e. other than nextNode)
-                                    if numChildParents == 1:
-                                            # Child in the tree found, put on fringe.
-                                            fringe += [ child ]
-                                    else:
-                                            # Shift edge to non-tree status.
-                                            nonTreeEdges.add((nextNode, child))
+                        # Shift edge to non-tree status.
+                        nonTreeEdges.add((nextNode, child))
+
+                        nodeChildMap[nextNode].remove(child)
+                        nodeParentMap[child].remove(nextNode)
 
-                                            nodeChildMap[ nextNode ].remove(child)
-                                            nodeParentMap[ child ].remove(nextNode)
+        # Generate the tree edges from remaining child relationships.
+        treeEdges = []
+        for node in nodeChildMap:
+            for child in nodeChildMap[node]:
+                treeEdges += [(node, child)]
 
-            # Generate the tree edges from remaining child relationships.
-            treeEdges = []
-            for node in nodeChildMap:
-                    for child in nodeChildMap[ node ]:
-                            treeEdges += [ (node, child) ]
+        return (list(rootNodes), treeEdges, list(nonTreeEdges))
 
-            return (list(rootNodes), treeEdges, list(nonTreeEdges))
-                                    
     def removeAbsent(self):
-            """Remove any absent edges from both graphs, and empty the fields
+        """Remove any absent edges from both graphs, and empty the fields
             recording empty objects."""
-            for absEdge in self.absentEdges:
-                    del self.elabels[ absEdge ]
+        for absEdge in self.absentEdges:
+            del self.elabels[absEdge]
 
-            for absNode in self.absentNodes:
-                    del self.nlabels[ absNode ]
-            
-            self.absentNodes = set([])
-            self.absentEdges = set([])
+        for absNode in self.absentNodes:
+            del self.nlabels[absNode]
 
-    def addAbsent(self, lg2):
-            """Identify edges in other graph but not the current one."""
-            selfNodes = set(list(self.nlabels))
-            lg2Nodes = set(list(lg2.nlabels))
-            self.absentNodes = lg2Nodes.difference(selfNodes)
-
-            # WARN about absent nodes/edges; indicate that there is an error.
-            if len(self.absentNodes) > 0:
-                    sys.stderr.write('  !! Inserting ABSENT nodes for:\n      ' \
-                                    + self.file + ' vs.\n      ' + lg2.file + '\n      ' \
-                            + str(sorted(list(self.absentNodes))) + '\n')
-                    self.error = True
+        self.absentNodes = set([])
+        self.absentEdges = set([])
 
-            # Add "absent" nodes.
-            # NOTE: all edges to/from "absent" nodes are unlabeled.
-            for missingNode in self.absentNodes:
-                    self.nlabels[ missingNode ] = { 'ABSENT': 1.0 }
+    def addAbsent(self, lg2):
+        """Identify edges in other graph but not the current one."""
+        selfNodes = set(list(self.nlabels))
+        lg2Nodes = set(list(lg2.nlabels))
+        self.absentNodes = lg2Nodes.difference(selfNodes)
+
+        # WARN about absent nodes/edges; indicate that there is an error.
+        if len(self.absentNodes) > 0:
+            sys.stderr.write(
+                "  !! Inserting ABSENT nodes for:\n      "
+                + self.file
+                + " vs.\n      "
+                + lg2.file
+                + "\n      "
+                + str(sorted(list(self.absentNodes)))
+                + "\n"
+            )
+            self.error = True
+
+        # Add "absent" nodes.
+        # NOTE: all edges to/from "absent" nodes are unlabeled.
+        for missingNode in self.absentNodes:
+            self.nlabels[missingNode] = {"ABSENT": 1.0}
 
     def matchAbsent(self, lg2):
-            """Add all missing primitives and edges between this graph and
+        """Add all missing primitives and edges between this graph and
             the passed graph. **Modifies both the object and argument graph lg2."""
-            self.removeAbsent()
-            self.addAbsent(lg2)
-
-            lg2.removeAbsent()
-            lg2.addAbsent(self)
+        self.removeAbsent()
+        self.addAbsent(lg2)
 
+        lg2.removeAbsent()
+        lg2.addAbsent(self)
 
-##################################
-# Routines for missing/unlabeled 
-# edges.
-##################################
-# Returns NONE: modifies in-place.
+    ##################################
+    # Routines for missing/unlabeled
+    # edges.
+    ##################################
+    # Returns NONE: modifies in-place.
     def labelMissingEdges(self):
-            for node1 in list(self.nlabels):
-                    for node2 in list(self.nlabels):
-                            if not node1 == node2:
-                                    if not (node1, node2) in list(self.elabels):
-                                            self.elabels[(node1, node2)] = {'_' : 1.0 }
+        for node1 in list(self.nlabels):
+            for node2 in list(self.nlabels):
+                if not node1 == node2:
+                    if not (node1, node2) in list(self.elabels):
+                        self.elabels[(node1, node2)] = {"_": 1.0}
 
-# Returns NONE: modifies in-place.
+    # Returns NONE: modifies in-place.
     def hideUnlabeledEdges(self):
-            """Move all missing/unlabeled edges to the hiddenEdges field."""
-            # Move all edges labeled '_' to the hiddenEdges field.
-            for edge in list(self.elabels):
-                    if set( list(self.elabels[ edge ]) ) == \
-                                    set( [ '_' ] ):
-                            self.hiddenEdges[ edge ] = self.elabels[ edge ]
-                            del self.elabels[ edge ]
+        """Move all missing/unlabeled edges to the hiddenEdges field."""
+        # Move all edges labeled '_' to the hiddenEdges field.
+        for edge in list(self.elabels):
+            if set(list(self.elabels[edge])) == set(["_"]):
+                self.hiddenEdges[edge] = self.elabels[edge]
+                del self.elabels[edge]
 
     def restoreUnlabeledEdges(self):
-            """Move all edges in the hiddenEdges field back to the set of
+        """Move all edges in the hiddenEdges field back to the set of
             edges for the graph."""
-            for edge in list(self.hiddenEdges):
-                    self.elabels[ edge ] = self.hiddenEdges[ edge ]
-                    del self.hiddenEdges[ edge ]
-
-##################################
-# Merging graphs
-##################################
-# RETURNS None (modifies 'self' in-place.)
+        for edge in list(self.hiddenEdges):
+            self.elabels[edge] = self.hiddenEdges[edge]
+            del self.hiddenEdges[edge]
+
+    ##################################
+    # Merging graphs
+    ##################################
+    # RETURNS None (modifies 'self' in-place.)
     def merge(self, lg2, ncombfn, ecombfn):
-            """New node/edge labels are added from lg2 with common primitives. The
+        """New node/edge labels are added from lg2 with common primitives. The
        value for common node/edge labels updated using ncombfn and
        ecombfn respectiveley: each function is applied to current values to
        obtain the new value (i.e. v1' = fn(v1,v2))."""
 
-            # Deal with non-common primitives/nodes.
-            # DEBUG: make sure that all absent edges are treated as
-            # 'hard' decisions (i.e. label ('_',1.0))
-            self.matchAbsent(lg2)
-            #self.labelMissingEdges()
-
-            # Merge node and edgelabels.
-            mergeMaps(self.nlabels, self.gweight, lg2.nlabels, lg2.gweight, \
-                            ncombfn)
-            mergeMaps(self.elabels, self.gweight, lg2.elabels, lg2.gweight,\
-                            ecombfn)
-
-                            
-# RETURNS None: modifies in-place.
-    def addWeightedLabelValues(self,lg2):
-            """Merge two graphs, adding the values for each node/edge label."""
-            def addValues( v1, w1, v2, w2 ):
-                    return w1 * v1 + w2 * v2
-            self.merge(lg2, addValues, addValues)
-
-# RETURNS None: modifies in-place.
-# HM: Added for IJDAR CROHME draft; invoke to filter a graph.
-# (currenly not used by default).
+        # Deal with non-common primitives/nodes.
+        # DEBUG: make sure that all absent edges are treated as
+        # 'hard' decisions (i.e. label ('_',1.0))
+        self.matchAbsent(lg2)
+        # self.labelMissingEdges()
+
+        # Merge node and edgelabels.
+        mergeMaps(self.nlabels, self.gweight, lg2.nlabels, lg2.gweight, ncombfn)
+        mergeMaps(self.elabels, self.gweight, lg2.elabels, lg2.gweight, ecombfn)
+
+    # RETURNS None: modifies in-place.
+    def addWeightedLabelValues(self, lg2):
+        """Merge two graphs, adding the values for each node/edge label."""
+
+        def addValues(v1, w1, v2, w2):
+            return w1 * v1 + w2 * v2
+
+        self.merge(lg2, addValues, addValues)
+
+    # RETURNS None: modifies in-place.
+    # HM: Added for IJDAR CROHME draft; invoke to filter a graph.
+    # (currenly not used by default).
     def keepOnlyCorrectLab(self, gt):
-            """Keep only correct labels compared with the gt. Use the
+        """Keep only correct labels compared with the gt. Use the
             label ERROR_N and ERROR_E for node and edges errors. Use the 
             compareTools to compare the labels with ground truth."""
-            
-            allNodes = set(list(gt.nlabels)).union(list(self.nlabels))
-            self.matchAbsent(gt)
-
-            for nid in allNodes:
-                    (cost,_) = self.cmpNodes(list(self.nlabels[nid]),list(gt.nlabels[nid]))
-                    #if there is some error
-                    if cost > 0:
-                            self.nlabels[ nid ] = {'ERROR_N' : 1.0}
-                    else:
-                            self.nlabels[ nid ] = gt.nlabels[nid]
 
-            for (graph,oGraph) in [ (self,gt), (gt,self) ]:
-                    for npair in list(graph.elabels):
-                            cost = 0;
-                            if not npair in oGraph.elabels:
-                                    (cost,errL) = self.cmpEdges(list(graph.elabels[npair]),['_'])
-                            else:
-                                    (cost,errL) = self.cmpEdges(list(graph.elabels[npair]),list(oGraph.elabels[npair]))
-                            if cost > 0:
-                                    self.elabels[ npair ] = {'ERROR_E' : 1.0}
-                            else:
-                                    if npair in gt.elabels:
-                                            self.elabels[ npair ] = gt.elabels[npair]
-                                    else:
-                                            self.elabels[ npair ] =  {'_' : 1.0}
+        allNodes = set(list(gt.nlabels)).union(list(self.nlabels))
+        self.matchAbsent(gt)
+
+        for nid in allNodes:
+            (cost, _) = self.cmpNodes(list(self.nlabels[nid]), list(gt.nlabels[nid]))
+            # if there is some error
+            if cost > 0:
+                self.nlabels[nid] = {"ERROR_N": 1.0}
+            else:
+                self.nlabels[nid] = gt.nlabels[nid]
+
+        for (graph, oGraph) in [(self, gt), (gt, self)]:
+            for npair in list(graph.elabels):
+                cost = 0
+                if not npair in oGraph.elabels:
+                    (cost, errL) = self.cmpEdges(list(graph.elabels[npair]), ["_"])
+                else:
+                    (cost, errL) = self.cmpEdges(
+                        list(graph.elabels[npair]), list(oGraph.elabels[npair])
+                    )
+                if cost > 0:
+                    self.elabels[npair] = {"ERROR_E": 1.0}
+                else:
+                    if npair in gt.elabels:
+                        self.elabels[npair] = gt.elabels[npair]
+                    else:
+                        self.elabels[npair] = {"_": 1.0}
 
-# RETURNS None: modifies in-place.
+    # RETURNS None: modifies in-place.
     def selectMaxLabels(self):
-            """Filter for labels with maximum confidence. NOTE: this will
+        """Filter for labels with maximum confidence. NOTE: this will
             keep all maximum value labels found in each map, e.g. if two
             classifications have the same likelihood for a node."""
-            for object in list(self.nlabels):
-                    max = -1.0
-                    maxPairs = {}
-                    for (label, value) in self.nlabels[object].items():
-                            if value > max:
-                                    max = value
-                                    maxPairs = { label : value }
-                            elif value == max:
-                                    maxPairs[label] = value
-
-                    self.nlabels[ object ] = maxPairs
-
-            for edge in list(self.elabels):
-                    max = -1.0
-                    maxPairs = {}
-                    for (label, value) in self.elabels[edge].items():
-                            if value > max:
-                                    max = value
-                                    maxPairs = { label : value }
-                            elif value == max:
-                                    maxPairs[label] = value
-
-                    self.elabels[ edge ] = maxPairs
-
-# RETURNS NONE: modifies in-place.
+        for object in list(self.nlabels):
+            max = -1.0
+            maxPairs = {}
+            for (label, value) in self.nlabels[object].items():
+                if value > max:
+                    max = value
+                    maxPairs = {label: value}
+                elif value == max:
+                    maxPairs[label] = value
+
+            self.nlabels[object] = maxPairs
+
+        for edge in list(self.elabels):
+            max = -1.0
+            maxPairs = {}
+            for (label, value) in self.elabels[edge].items():
+                if value > max:
+                    max = value
+                    maxPairs = {label: value}
+                elif value == max:
+                    maxPairs[label] = value
+
+            self.elabels[edge] = maxPairs
+
+    # RETURNS NONE: modifies in-place.
     def invertValues(self):
-            """Substract all node and edge label values from 1.0, to 
+        """Substract all node and edge label values from 1.0, to 
             invert the values. Attempting to invert a value outside [0,1] will
             set the error flag on the object."""
-            for node in list(self.nlabels):
-                    for label in self.nlabels[ node ]:
-                            currentValue = self.nlabels[ node ][ label ] 
-                            if currentValue < 0.0 or currentValue > 1.0:
-                                    sys.stderr.write('\n  !! Attempted to invert node: ' \
-                                                    + node + ' label \"' \
-                                                    + label + '\" with value ' + str(currentValue) + '\n')
-                                    self.error = True
-                            else:
-                                    self.nlabels[ node ][ label ] = 1.0 - currentValue
-
-            for edge in list(self.elabels):
-                    for label in self.elabels[ edge ]:
-                            currentValue = self.elabels[ edge ][ label ]
-                            if currentValue < 0.0 or currentValue > 1.0:
-                                    sys.stderr.write('\n  !! Attempted to invert edge: ' + \
-                                                    str(edge) + ' label \"' \
-                                                    + label + '\" with value ' + str(currentValue) + '\n')
-                                    self.error = True
-                            else:
-                                    self.elabels[ edge ][ label ] = 1.0 - currentValue
+        for node in list(self.nlabels):
+            for label in self.nlabels[node]:
+                currentValue = self.nlabels[node][label]
+                if currentValue < 0.0 or currentValue > 1.0:
+                    sys.stderr.write(
+                        "\n  !! Attempted to invert node: "
+                        + node
+                        + ' label "'
+                        + label
+                        + '" with value '
+                        + str(currentValue)
+                        + "\n"
+                    )
+                    self.error = True
+                else:
+                    self.nlabels[node][label] = 1.0 - currentValue
+
+        for edge in list(self.elabels):
+            for label in self.elabels[edge]:
+                currentValue = self.elabels[edge][label]
+                if currentValue < 0.0 or currentValue > 1.0:
+                    sys.stderr.write(
+                        "\n  !! Attempted to invert edge: "
+                        + str(edge)
+                        + ' label "'
+                        + label
+                        + '" with value '
+                        + str(currentValue)
+                        + "\n"
+                    )
+                    self.error = True
+                else:
+                    self.elabels[edge][label] = 1.0 - currentValue
 
     def subStructIterator(self, nodeNumbers):
-            """ Return an iterator which gives all substructures with n nodes
+        """ Return an iterator which gives all substructures with n nodes
             n belonging to the list depths"""
-            if(isinstance(nodeNumbers, int)):
-                    nodeNumbers = [nodeNumbers]
-            subStruct = []
-            
-            # Init the substruct with isolated nodes
-            for n in list(self.nlabels):
-                    subStruct.append(set([n]))
-                    if 1 in nodeNumbers:
-                            yield SmallGraph([(n, "".join(list(self.nlabels[n])))], [])
-            
-            for d in range(2,max(nodeNumbers)+1):
-                    #add one node to each substructure
-                    newSubsS = set([])
-                    newSubsL = []
-                    for sub in subStruct:
-                            le = getEdgesToNeighbours(sub,list(self.elabels))
-                            for (f,to) in le:
-                                    new = sub.union([to])
-                                    lnew = list(new)
-                                    lnew.sort()
-                                    snew = ",".join(lnew)
-                                    
-                                    if(not snew in newSubsS):
-                                            newSubsS.add(snew)
-                                            newSubsL.append(new)
-                                            if d in nodeNumbers:
-                                                    yield self.getSubSmallGraph(new)
-                    
-                    # ??? BUG ???
-                    subStruct = newSubsL
-                    
+        if isinstance(nodeNumbers, int):
+            nodeNumbers = [nodeNumbers]
+        subStruct = []
+
+        # Init the substruct with isolated nodes
+        for n in list(self.nlabels):
+            subStruct.append(set([n]))
+            if 1 in nodeNumbers:
+                yield SmallGraph([(n, "".join(list(self.nlabels[n])))], [])
+
+        for d in range(2, max(nodeNumbers) + 1):
+            # add one node to each substructure
+            newSubsS = set([])
+            newSubsL = []
+            for sub in subStruct:
+                le = getEdgesToNeighbours(sub, list(self.elabels))
+                for (f, to) in le:
+                    new = sub.union([to])
+                    lnew = list(new)
+                    lnew.sort()
+                    snew = ",".join(lnew)
+
+                    if not snew in newSubsS:
+                        newSubsS.add(snew)
+                        newSubsL.append(new)
+                        if d in nodeNumbers:
+                            yield self.getSubSmallGraph(new)
+
+            # ??? BUG ???
+            subStruct = newSubsL
+
     def getSubSmallGraph(self, nodelist):
-            """Return the small graph with the primitives in nodelist and all edges 
+        """Return the small graph with the primitives in nodelist and all edges 
             between them. The used label is the merged list of labels from nodes/edges"""
-            sg = SmallGraph()
-            for n in nodelist:
-                    sg.nodes[n] = list(self.nlabels[n])
-            for e in getEdgesBetweenThem(nodelist,list(self.elabels)):
-                    sg.edges[e] = list(self.elabels[e])
-            return sg
-            
-# Compare the substructure
+        sg = SmallGraph()
+        for n in nodelist:
+            sg.nodes[n] = list(self.nlabels[n])
+        for e in getEdgesBetweenThem(nodelist, list(self.elabels)):
+            sg.edges[e] = list(self.elabels[e])
+        return sg
+
+    # Compare the substructure
     def compareSubStruct(self, olg, depths):
-            """Return the list of couple of substructure which disagree
+        """Return the list of couple of substructure which disagree
             the substructure from self are used as references"""
-            allerrors = []
-            for struc in olg.subStructIterator(depths):
-                            sg1 = self.getSubSmallGraph(list(struc.nodes))
-                            if(not (struc == sg1)):		
-                                    allerrors.append((struc,sg1))
-            return allerrors
-
-    def compareSegmentsStruct(self, lgGT,depths):
-            """Compute the number of differing segments, and record disagreements
+        allerrors = []
+        for struc in olg.subStructIterator(depths):
+            sg1 = self.getSubSmallGraph(list(struc.nodes))
+            if not (struc == sg1):
+                allerrors.append((struc, sg1))
+        return allerrors
+
+    def compareSegmentsStruct(self, lgGT, depths):
+        """Compute the number of differing segments, and record disagreements
             in a list. 
             The primitives in each subgraph should be of the same number and names
             (identifiers). Nodes are merged that have identical (label,value) pairs
@@ -1340,176 +1548,183 @@ class Lg(object):
             classification evaluation, the ground-truth should be lgGT.  The first
             key value of the matrix is the lgGT obj structure, which gives the
             structure of the corresponding primitives which is the key to get the
-            error structure in self.""" 
-            (sp1, ps1, _, sre1) = self.segmentGraph()
-            (spGT, psGT, _, sreGT) = lgGT.segmentGraph()
-
-            segDiffs = set()
-            correctSegments = set()
-            for primitive in list(psGT):
-                    # Make sure to skip primitives that were missing ('ABSENT'),
-                    # as in that case the graphs disagree on all non-identical node
-                    # pairs for this primitive, and captured in self.absentEdges.
-
-                    # RZ: Assuming one level of structure here; modifying for
-                    #     new data structures accomodating multiple structural levels.
-                    obj1Id = ps1[primitive][ list(ps1[primitive])[0] ]
-                    obj2Id = psGT[primitive][ list(psGT[primitive])[0] ]
-
-                    if not 'ABSENT' in self.nlabels[primitive] and \
-                                    not 'ABSENT' in lgGT.nlabels[primitive]:
-                            # Obtain sets of primitives sharing a segment for the current
-                            # primitive for both graphs.
-                            # Each of sp1/spGT are a map of ( {prim_set}, label ) pairs.
-
-                            segPrimSet1 = sp1[ obj1Id ][0]
-                            segPrimSet2 = spGT[ obj2Id ][0]
-                            
-                            # Only create an entry where there are disagreements.
-                            if segPrimSet1 != segPrimSet2:
-                                    segDiffs.add( ( obj2Id, obj1Id) )
-                            else:
-                                    correctSegments.add( obj2Id )
-                    
-                    # DEBUG: don't record differences for a single node.
-                    elif len(list(self.nlabels)) > 1:
-                            # If node was missing in this graph or the other, treat 
-                            # this graph as having a missing segment
-                            # do not count the segment in graph with 1 primitive
-                            segDiffs.add(( obj2Id, obj1Id ) )
-
-            # now check if the labels are identical
-            for seg in correctSegments:
-                    # Get label for the first primtives (all primitives have identical
-                    # labels in a segment).
-                    # DEBUG: use only the set of labels, not confidence values.
-                    firstPrim = list(spGT[seg][0])[0]
-                    (cost, diff) = self.cmpNodes(list(self.nlabels[ firstPrim ]),list(lgGT.nlabels[ firstPrim ]))
-
-                    segId1 = ps1[firstPrim][ list(ps1[ firstPrim ])[0] ]
-                    segId2 = psGT[firstPrim][ list(psGT[ firstPrim ])[0] ]
-                    
-                    if (0,[]) != (cost, diff):
-                            segDiffs.add(( segId2, segId1) )
-            allSegWithErr = set([p for (p,_) in segDiffs])
-            
-            # start to build the LG at the object level
-            # add nodes for object with the labels from the first prim
-            lgObj = Lg()
-            for (sid,lprim) in spGT.items():
-                    lgObj.nlabels[sid] = lgGT.nlabels[list(lprim[0])[0]]
-
-            # Compute the specific 'segment-level' graph edges that disagree, at the
-            # level of primitive-pairs. This means that invalid segmentations may
-            # still have valid layouts in some cases.
-            # Add also the edges in the smallGraph
-            segEdgeErr = set()
-            for thisPair in list(sreGT):
-                    # TODO : check if it is sp1[thisPair[0]] instead of sp1[thisPair[0]][0]
-                    thisParentIds = set(spGT[ thisPair[0] ][0])
-                    thisChildIds = set(spGT[thisPair[1] ][0])
-                    lgObj.elabels[thisPair] = lgGT.elabels[ (list(thisParentIds)[0], list(thisChildIds)[0])]
-                    
-                    # A 'correct' edge has the same label between all primitives
-                    # in the two segments.
-                    # NOTE: we are not checking the consitency of label in each graph
-                    #  ie if all labels from thisParentIds to thisChildIds in self are 
-                    # the same 
-                    for parentId in thisParentIds:
-                            for childId in thisChildIds:
-                                    # DEBUG: compare only label sets, not values.
-                                    if not (parentId, childId) in list(self.elabels) or \
-                                       (0,[]) != self.cmpEdges(list(self.elabels[ (parentId, childId) ]), list(lgGT.elabels[ (parentId, childId) ])):
-                                            segEdgeErr.add(thisPair)
-                                            continue
-            
-            listOfAllError = []
-            for smg in lgObj.subStructIterator(depths):
-                    #if one segment is in the segment error set
-                    showIt = False
-                    if len(set(list(smg.nodes)).intersection(allSegWithErr)) > 0:
-                            showIt = True
-                    for pair in list(smg.edges):
-                            if pair in segEdgeErr:
-                                    showIt = True
-                                    continue
-                    if showIt:
-                            #build the smg for the prim from lgGT
-                            allPrim = []
-                            for s in list(smg.nodes):
-                                    allPrim.extend(spGT[s][0])
-                            
-                            smgPrim1 = self.getSubSmallGraph(allPrim)
-                            smgPrimGT = lgGT.getSubSmallGraph(allPrim)
-                            listOfAllError.append((smg,smgPrimGT,smgPrim1))
-            
-            return listOfAllError 
+            error structure in self."""
+        (sp1, ps1, _, sre1) = self.segmentGraph()
+        (spGT, psGT, _, sreGT) = lgGT.segmentGraph()
+
+        segDiffs = set()
+        correctSegments = set()
+        for primitive in list(psGT):
+            # Make sure to skip primitives that were missing ('ABSENT'),
+            # as in that case the graphs disagree on all non-identical node
+            # pairs for this primitive, and captured in self.absentEdges.
+
+            # RZ: Assuming one level of structure here; modifying for
+            #     new data structures accomodating multiple structural levels.
+            obj1Id = ps1[primitive][list(ps1[primitive])[0]]
+            obj2Id = psGT[primitive][list(psGT[primitive])[0]]
+
+            if (
+                not "ABSENT" in self.nlabels[primitive]
+                and not "ABSENT" in lgGT.nlabels[primitive]
+            ):
+                # Obtain sets of primitives sharing a segment for the current
+                # primitive for both graphs.
+                # Each of sp1/spGT are a map of ( {prim_set}, label ) pairs.
+
+                segPrimSet1 = sp1[obj1Id][0]
+                segPrimSet2 = spGT[obj2Id][0]
+
+                # Only create an entry where there are disagreements.
+                if segPrimSet1 != segPrimSet2:
+                    segDiffs.add((obj2Id, obj1Id))
+                else:
+                    correctSegments.add(obj2Id)
+
+            # DEBUG: don't record differences for a single node.
+            elif len(list(self.nlabels)) > 1:
+                # If node was missing in this graph or the other, treat
+                # this graph as having a missing segment
+                # do not count the segment in graph with 1 primitive
+                segDiffs.add((obj2Id, obj1Id))
+
+        # now check if the labels are identical
+        for seg in correctSegments:
+            # Get label for the first primtives (all primitives have identical
+            # labels in a segment).
+            # DEBUG: use only the set of labels, not confidence values.
+            firstPrim = list(spGT[seg][0])[0]
+            (cost, diff) = self.cmpNodes(
+                list(self.nlabels[firstPrim]), list(lgGT.nlabels[firstPrim])
+            )
+
+            segId1 = ps1[firstPrim][list(ps1[firstPrim])[0]]
+            segId2 = psGT[firstPrim][list(psGT[firstPrim])[0]]
+
+            if (0, []) != (cost, diff):
+                segDiffs.add((segId2, segId1))
+        allSegWithErr = set([p for (p, _) in segDiffs])
+
+        # start to build the LG at the object level
+        # add nodes for object with the labels from the first prim
+        lgObj = Lg()
+        for (sid, lprim) in spGT.items():
+            lgObj.nlabels[sid] = lgGT.nlabels[list(lprim[0])[0]]
+
+        # Compute the specific 'segment-level' graph edges that disagree, at the
+        # level of primitive-pairs. This means that invalid segmentations may
+        # still have valid layouts in some cases.
+        # Add also the edges in the smallGraph
+        segEdgeErr = set()
+        for thisPair in list(sreGT):
+            # TODO : check if it is sp1[thisPair[0]] instead of sp1[thisPair[0]][0]
+            thisParentIds = set(spGT[thisPair[0]][0])
+            thisChildIds = set(spGT[thisPair[1]][0])
+            lgObj.elabels[thisPair] = lgGT.elabels[
+                (list(thisParentIds)[0], list(thisChildIds)[0])
+            ]
+
+            # A 'correct' edge has the same label between all primitives
+            # in the two segments.
+            # NOTE: we are not checking the consitency of label in each graph
+            #  ie if all labels from thisParentIds to thisChildIds in self are
+            # the same
+            for parentId in thisParentIds:
+                for childId in thisChildIds:
+                    # DEBUG: compare only label sets, not values.
+                    if not (parentId, childId) in list(self.elabels) or (
+                        0,
+                        [],
+                    ) != self.cmpEdges(
+                        list(self.elabels[(parentId, childId)]),
+                        list(lgGT.elabels[(parentId, childId)]),
+                    ):
+                        segEdgeErr.add(thisPair)
+                        continue
+
+        listOfAllError = []
+        for smg in lgObj.subStructIterator(depths):
+            # if one segment is in the segment error set
+            showIt = False
+            if len(set(list(smg.nodes)).intersection(allSegWithErr)) > 0:
+                showIt = True
+            for pair in list(smg.edges):
+                if pair in segEdgeErr:
+                    showIt = True
+                    continue
+            if showIt:
+                # build the smg for the prim from lgGT
+                allPrim = []
+                for s in list(smg.nodes):
+                    allPrim.extend(spGT[s][0])
+
+                smgPrim1 = self.getSubSmallGraph(allPrim)
+                smgPrimGT = lgGT.getSubSmallGraph(allPrim)
+                listOfAllError.append((smg, smgPrimGT, smgPrim1))
+
+        return listOfAllError
+
 
 ################################################################
 # Utility functions
 ################################################################
 def mergeLabelLists(llist1, weight1, llist2, weight2, combfn):
-	"""Combine values in two label lists according to the passed combfn
+    """Combine values in two label lists according to the passed combfn
 	function, and passed weights for each label list."""
-	# Combine values for each label in lg2 already in self.
-	allLabels = set(llist1.items())\
-			.union(set(llist2.items()))
-	# have to test whether labels exist
-	# in one or both list.
-	for (label, value) in allLabels:
-		if label in list(llist1) and \
-				label in list(llist2):
-			llist1[ label ] = \
-				combfn( llist1[label], weight1,\
-						llist2[label], weight2 )
-		elif label in list(llist2):
-			llist1[ label ] = \
-				weight2 * llist2[label]
-		else:
-			llist1[ label ] = \
-				weight1 * llist1[label]
+    # Combine values for each label in lg2 already in self.
+    allLabels = set(llist1.items()).union(set(llist2.items()))
+    # have to test whether labels exist
+    # in one or both list.
+    for (label, value) in allLabels:
+        if label in list(llist1) and label in list(llist2):
+            llist1[label] = combfn(llist1[label], weight1, llist2[label], weight2)
+        elif label in list(llist2):
+            llist1[label] = weight2 * llist2[label]
+        else:
+            llist1[label] = weight1 * llist1[label]
 
 
 def mergeMaps(map1, weight1, map2, weight2, combfn):
-	"""Combine values in two maps according to the passed combfn
+    """Combine values in two maps according to the passed combfn
 	function, and passed weights for each map."""
-	# Odds are good that there are built-in function for this
-	# operation.
-	objects1 = list(map1)
-	objects2 = list(map2)
-	allObjects = set(objects1).union(set(objects2))
-	for object in allObjects:
-		if object in objects1 and object in objects2:
-			# Combine values for each label in lg2 already in self.
-			mergeLabelLists(map1[object],weight1, map2[object], weight2, combfn )			
-		# DEBUG: no relationship ('missing') edges should
-		# be taken as certain (value 1.0 * weight) where not explicit.
-		elif object in objects2:
-			# Use copy to avoid aliasing problems.
-			# Use appropriate weight to update value.
-			map1[ object ] = copy.deepcopy( map2[ object ] )
-			for (label, value) in map1[object].items():
-				map1[object][label] = weight2 * value
-			map1[object]['_'] = weight1 
-		else:
-			# Only in current map: weight value appropriately.
-			for (label, value) in map1[object].items():
-				map1[object][label] = weight1 * value
-			map1[object]['_'] = weight2 
-
-
-def getEdgesToNeighbours(nodes,edges):
-	"""return all edges which are coming from one of the nodes to out of these nodes"""
-	neigb = set([])
-	for (n1,n2) in edges:
-		if (n1 in nodes and not n2 in nodes):
-			neigb.add((n1,n2))
-	return neigb
-
-def getEdgesBetweenThem(nodes,edges):
-	"""return all edges which are coming from one of the nodes to out of these nodes"""
-	edg = set([])
-	for (n1,n2) in edges:
-		if (n1 in nodes and n2 in nodes):
-			edg.add((n1,n2))
-	return edg
+    # Odds are good that there are built-in function for this
+    # operation.
+    objects1 = list(map1)
+    objects2 = list(map2)
+    allObjects = set(objects1).union(set(objects2))
+    for object in allObjects:
+        if object in objects1 and object in objects2:
+            # Combine values for each label in lg2 already in self.
+            mergeLabelLists(map1[object], weight1, map2[object], weight2, combfn)
+        # DEBUG: no relationship ('missing') edges should
+        # be taken as certain (value 1.0 * weight) where not explicit.
+        elif object in objects2:
+            # Use copy to avoid aliasing problems.
+            # Use appropriate weight to update value.
+            map1[object] = copy.deepcopy(map2[object])
+            for (label, value) in map1[object].items():
+                map1[object][label] = weight2 * value
+            map1[object]["_"] = weight1
+        else:
+            # Only in current map: weight value appropriately.
+            for (label, value) in map1[object].items():
+                map1[object][label] = weight1 * value
+            map1[object]["_"] = weight2
+
+
+def getEdgesToNeighbours(nodes, edges):
+    """return all edges which are coming from one of the nodes to out of these nodes"""
+    neigb = set([])
+    for (n1, n2) in edges:
+        if n1 in nodes and not n2 in nodes:
+            neigb.add((n1, n2))
+    return neigb
+
+
+def getEdgesBetweenThem(nodes, edges):
+    """return all edges which are coming from one of the nodes to out of these nodes"""
+    edg = set([])
+    for (n1, n2) in edges:
+        if n1 in nodes and n2 in nodes:
+            edg.add((n1, n2))
+    return edg
diff --git a/src/lg2NE.py b/src/lg2NE.py
index 9dcaa13..9463e8a 100644
--- a/src/lg2NE.py
+++ b/src/lg2NE.py
@@ -9,9 +9,11 @@
 import sys
 from lgeval.src.lg import *
 
+
 def main(fileName):
-	inputFile = Lg(fileName)
-	print(inputFile.csv())
+    inputFile = Lg(fileName)
+    print(inputFile.csv())
+
 
 fileName = sys.argv[1]
 main(fileName)
diff --git a/src/lg2OR.py b/src/lg2OR.py
index 8b3e6aa..89e7575 100644
--- a/src/lg2OR.py
+++ b/src/lg2OR.py
@@ -9,9 +9,11 @@
 import sys
 from lgeval.src.lg import *
 
+
 def main(fileName):
-	inputFile = Lg(fileName)
-	print(inputFile.csvObject())
+    inputFile = Lg(fileName)
+    print(inputFile.csvObject())
+
 
 fileName = sys.argv[1]
 main(fileName)
diff --git a/src/lg2dot.py b/src/lg2dot.py
index cfe4a64..818adaa 100644
--- a/src/lg2dot.py
+++ b/src/lg2dot.py
@@ -10,431 +10,606 @@ import sys
 from lgeval.src.lg import *
 
 # Default separator for label lists.
-DELIM=" "
+DELIM = " "
+
 
 def createLabelList(labelList, delimiter=DELIM):
-	"""Convenience - create a delimited list of labels."""
-	Labels = sorted(list(set(labelList)))
-	outString = ""
-	i=0
-	for label in Labels:
-		if i > 0:
-			outString += delimiter
-		outString += (str(label)).replace('\\','\\\\')
-		i += 1
-
-	# Make sure to use underscore to represent empty label lists.
-	if len(Labels) < 1:
-		outString = '_'
-	return(outString)
-
-def createSegPrimitivesLabel( segmentId, lg, segPrimMap, delimiter=DELIM):
-	"""Given a label graph and segment tuple (with nodeIds and
+    """Convenience - create a delimited list of labels."""
+    Labels = sorted(list(set(labelList)))
+    outString = ""
+    i = 0
+    for label in Labels:
+        if i > 0:
+            outString += delimiter
+        outString += (str(label)).replace("\\", "\\\\")
+        i += 1
+
+    # Make sure to use underscore to represent empty label lists.
+    if len(Labels) < 1:
+        outString = "_"
+    return outString
+
+
+def createSegPrimitivesLabel(segmentId, lg, segPrimMap, delimiter=DELIM):
+    """Given a label graph and segment tuple (with nodeIds and
 	segment label), return a delimited list for all labels
 	associated with nodes in the segment."""
-	(primIds, _) = segPrimMap[ segmentId ]
+    (primIds, _) = segPrimMap[segmentId]
+
+    #  Compile the label set for all primitives.
+    primLabelSet = set()
+    for idName in primIds:
+        if idName in list(lg.nlabels):
+            for nextLabel in list(lg.nlabels[idName]):
+                primLabelSet.add(nextLabel)
+        else:
+            primLabelSet.add("_")
 
-	#  Compile the label set for all primitives.
-	primLabelSet = set()
-	for idName in primIds:
-		if idName in list(lg.nlabels):
-			for nextLabel in list(lg.nlabels[ idName ]):
-				primLabelSet.add( nextLabel )
-		else:
-			primLabelSet.add('_')
+    return createLabelList(sorted(list(primLabelSet)), delimiter)
 
-	return( createLabelList( sorted(list(primLabelSet)), delimiter) )
 
-def createRelPrimitivesLabel( edge, lg, segPrimMap, delimiter=DELIM):
-	"""Create label list from set of all primitive labels associated
+def createRelPrimitivesLabel(edge, lg, segPrimMap, delimiter=DELIM):
+    """Create label list from set of all primitive labels associated
 	with a relationship between objects."""
-	(parentPrimIds, _) = segPrimMap[ edge[0] ]
-	(childPrimIds, _) = segPrimMap[ edge[1] ]
-
-	edgePrimLabelSet = set()
-	for pid in parentPrimIds:
-		for cid in childPrimIds:
-			if (pid, cid) in list(lg.elabels):
-				for label in list(lg.elabels[ (pid, cid) ]):
-					edgePrimLabelSet.add(label)
-			else:
-				edgePrimLabelSet.add('_')
-		
-	return( createLabelList( sorted(list(edgePrimLabelSet)), delimiter) )
+    (parentPrimIds, _) = segPrimMap[edge[0]]
+    (childPrimIds, _) = segPrimMap[edge[1]]
+
+    edgePrimLabelSet = set()
+    for pid in parentPrimIds:
+        for cid in childPrimIds:
+            if (pid, cid) in list(lg.elabels):
+                for label in list(lg.elabels[(pid, cid)]):
+                    edgePrimLabelSet.add(label)
+            else:
+                edgePrimLabelSet.add("_")
+
+    return createLabelList(sorted(list(edgePrimLabelSet)), delimiter)
+
 
 def getFirstElements(list):
-	"""Return first element for each tuple in a list."""
-	def first(x):
-		return(x[0])
-	return(map(first,list))
+    """Return first element for each tuple in a list."""
+
+    def first(x):
+        return x[0]
+
+    return map(first, list)
+
 
 def primitiveNodeString(lg, nodeDiffList):
-	dotString = "digraph lg {\n\trankdir=LR; ranksep=1.0;\n\tedge[fontsize=11,weight=1]; node[fontsize=13]; graph[concentrate=true,ordering=out];\n"
-	dotString = dotString + "\n\t/*  NODES (PRIMITIVES) */\n\t"
-	for primitive in sorted(list(list(lg.nlabels))):
-		Rlabel = createLabelList(list(lg.nlabels[primitive]))
-		color = 'blue'
-
-		# Find id's with disagreeing node labels.
-		Elabel = [] 
-		for (id,_,RlabelList) in nodeDiffList:
-			if id == primitive:
-				Elabel += getFirstElements(RlabelList)
-				color = 'red,penwidth=2.0,fontcolor=red' #,peripheries=2'
-
-		Estring = ""
-		if len(Elabel) > 0:
-			Estring = "\n( " + createLabelList(Elabel) + " )"
-		
-		dotString = dotString + primitive + " [label=\"" \
-				+  Rlabel + Estring + "\\n" + primitive \
-				+  "\", color=" + color + "];\n\t"
-
-	return dotString
+    dotString = "digraph lg {\n\trankdir=LR; ranksep=1.0;\n\tedge[fontsize=11,weight=1]; node[fontsize=13]; graph[concentrate=true,ordering=out];\n"
+    dotString = dotString + "\n\t/*  NODES (PRIMITIVES) */\n\t"
+    for primitive in sorted(list(list(lg.nlabels))):
+        Rlabel = createLabelList(list(lg.nlabels[primitive]))
+        color = "blue"
+
+        # Find id's with disagreeing node labels.
+        Elabel = []
+        for (id, _, RlabelList) in nodeDiffList:
+            if id == primitive:
+                Elabel += getFirstElements(RlabelList)
+                color = "red,penwidth=2.0,fontcolor=red"  # ,peripheries=2'
+
+        Estring = ""
+        if len(Elabel) > 0:
+            Estring = "\n( " + createLabelList(Elabel) + " )"
+
+        dotString = (
+            dotString
+            + primitive
+            + ' [label="'
+            + Rlabel
+            + Estring
+            + "\\n"
+            + primitive
+            + '", color='
+            + color
+            + "];\n\t"
+        )
+
+    return dotString
+
 
 def bipartiteNodeString(lg, nodeDiffList, lg2=None):
-	dotString = "digraph lg {\n\trankdir=LR; ranksep=1.0;\n\tedge[fontsize=11,weight=1]; node[fontsize=13]; graph[ordering=out];\n"
-	i = 0
-	dotString = dotString + "\n\t/*  NODES (PRIMITIVES) */"
-	for primitive in sorted(list(list(lg.nlabels))):
-		dotString = dotString + \
-				"\n\tsubgraph cluster" + str(i) + "{" \
-				+ "\n\t\trank=" + str(i+1) + "; color=white;\n\t\t"
-		Llabel = createLabelList(list(lg.nlabels[primitive]))
-		Rlabel = Llabel
-
-		if not lg2 == None:
-			Rlabel = createLabelList(list(lg2.nlabels[primitive]))
-		color = 'blue'
-		if not Llabel == Rlabel:
-			color = "red,penwidth=2.0,fontsize=14,fontcolor=red"
-		dotString = dotString + 'L' + primitive + " [label=\"" \
-				+  Llabel + "\\n" + primitive \
-				+  "\", color = " + color + "];\n\t\t"
-		dotString = dotString + 'R' + primitive + " [label=\"" \
-				+  Rlabel + "\\n" + primitive \
-				+  "\", color = blue" + "];\n\t\t"
-		dotString = dotString + "L" + primitive + " -> " \
-				+ "R" + primitive + " [style=invis, weight=1000]}\n"
-		i = i + 1
-	return dotString
+    dotString = "digraph lg {\n\trankdir=LR; ranksep=1.0;\n\tedge[fontsize=11,weight=1]; node[fontsize=13]; graph[ordering=out];\n"
+    i = 0
+    dotString = dotString + "\n\t/*  NODES (PRIMITIVES) */"
+    for primitive in sorted(list(list(lg.nlabels))):
+        dotString = (
+            dotString
+            + "\n\tsubgraph cluster"
+            + str(i)
+            + "{"
+            + "\n\t\trank="
+            + str(i + 1)
+            + "; color=white;\n\t\t"
+        )
+        Llabel = createLabelList(list(lg.nlabels[primitive]))
+        Rlabel = Llabel
+
+        if not lg2 == None:
+            Rlabel = createLabelList(list(lg2.nlabels[primitive]))
+        color = "blue"
+        if not Llabel == Rlabel:
+            color = "red,penwidth=2.0,fontsize=14,fontcolor=red"
+        dotString = (
+            dotString
+            + "L"
+            + primitive
+            + ' [label="'
+            + Llabel
+            + "\\n"
+            + primitive
+            + '", color = '
+            + color
+            + "];\n\t\t"
+        )
+        dotString = (
+            dotString
+            + "R"
+            + primitive
+            + ' [label="'
+            + Rlabel
+            + "\\n"
+            + primitive
+            + '", color = blue'
+            + "];\n\t\t"
+        )
+        dotString = (
+            dotString
+            + "L"
+            + primitive
+            + " -> "
+            + "R"
+            + primitive
+            + " [style=invis, weight=1000]}\n"
+        )
+        i = i + 1
+    return dotString
+
 
 def lgsegdot(lg, nodeDiffList, sdiffs, lg2=None):
-	"""Produce a .dot string representation of the segmentation graph
+    """Produce a .dot string representation of the segmentation graph
 	corresponding to a bipartite graph."""
-	
-	dotString = bipartiteNodeString(lg, nodeDiffList, lg2)
-	
-	# Compute segment information. We only need the maps (and diffs,
-	# if provided)
-	(segmentPrimitiveMap, primitiveSegmentMap, _, _) \
-			= lg.segmentGraph()
-
-	dotString = dotString + "\n\t/* EDGES (FOR COMMON OBJECTS) */\n"
-	for cid in sorted(list(list(primitiveSegmentMap))):
-		# HACK - this map has changed somehow.
-		csegment = primitiveSegmentMap[cid].values()[0]
-		commonIds = segmentPrimitiveMap[csegment][0] 
-		for neighbor in commonIds: 
-			# Prevent self-edges.
-			if not neighbor == cid:
-				color = ""
-				if cid in list(sdiffs):
-					errorEdges = sdiffs[cid]
-					# Color false positives red.
-					if neighbor in errorEdges[0] and neighbor not in errorEdges[1]:
-						color = ",color=red,penwidth=2.0"
-				dotString = dotString + "\tL" + cid + " -> " + "R" + neighbor \
-					+ " [dir=none" + color + "];\n"
-
-		# Show false negative as red dashed lines.
-		if cid in list(sdiffs):
-			errorEdges = sdiffs[cid]
-			for primitive in errorEdges[1]:
-				if not primitive == cid and primitive not in commonIds:
-					dotString = dotString + "\tL" + cid + " -> " + "R" + primitive \
-						+ " [dir=none,color=red,penwidth=2.0,style=dashed];\n"
-
-	dotString = dotString + "}"
-	return dotString
+
+    dotString = bipartiteNodeString(lg, nodeDiffList, lg2)
+
+    # Compute segment information. We only need the maps (and diffs,
+    # if provided)
+    (segmentPrimitiveMap, primitiveSegmentMap, _, _) = lg.segmentGraph()
+
+    dotString = dotString + "\n\t/* EDGES (FOR COMMON OBJECTS) */\n"
+    for cid in sorted(list(list(primitiveSegmentMap))):
+        # HACK - this map has changed somehow.
+        csegment = primitiveSegmentMap[cid].values()[0]
+        commonIds = segmentPrimitiveMap[csegment][0]
+        for neighbor in commonIds:
+            # Prevent self-edges.
+            if not neighbor == cid:
+                color = ""
+                if cid in list(sdiffs):
+                    errorEdges = sdiffs[cid]
+                    # Color false positives red.
+                    if neighbor in errorEdges[0] and neighbor not in errorEdges[1]:
+                        color = ",color=red,penwidth=2.0"
+                dotString = (
+                    dotString
+                    + "\tL"
+                    + cid
+                    + " -> "
+                    + "R"
+                    + neighbor
+                    + " [dir=none"
+                    + color
+                    + "];\n"
+                )
+
+        # Show false negative as red dashed lines.
+        if cid in list(sdiffs):
+            errorEdges = sdiffs[cid]
+            for primitive in errorEdges[1]:
+                if not primitive == cid and primitive not in commonIds:
+                    dotString = (
+                        dotString
+                        + "\tL"
+                        + cid
+                        + " -> "
+                        + "R"
+                        + primitive
+                        + " [dir=none,color=red,penwidth=2.0,style=dashed];\n"
+                    )
+
+    dotString = dotString + "}"
+    return dotString
+
 
 def lgPrimitiveDot(lg, nodeDiffList, edgeDiffList):
-	"""Produce a .dot string representation of a primitive graph."""
-	dotString = ""
-	dotString += primitiveNodeString(lg,nodeDiffList)
-	dotString += "\n\t/* EDGES (PRIMITIVE RELATIONSHIPS) */\n"
-	edges = list(lg.elabels)
-	
-	# Edges/Mislabeled edges from graph lg, as appropriate.
-	# Check for opportunities to create bidirectional edges.
-	seen = set()
-	for (parent, child) in sorted(list(edges)):
-		errorString = ""
-		labelString = createLabelList(lg.elabels[(parent,child)])
-
-		# Skip edges that have already been created.
-		if (parent, child) in seen:
-			continue
-	
-		bidirectional=""
-		# Check label in opposite direction.
-		if (child, parent) in list(edges):
-			oppositeLabelString = createLabelList(lg.elabels[(child,parent)])
-			if labelString == oppositeLabelString:
-				bidirectional="dir=both,"
-				seen.add((child,parent))
-
-		Elabel = []
-		for (pair,_,oelabel) in edgeDiffList:
-			# NOTE: specific to current implementation for missing edge repr.
-			if pair == (parent,child):
-				Elabel += getFirstElements(oelabel)
-				errorString = ",color=red,penwidth=2.0,fontsize=14,fontcolor=red"
-		
-		Estring = ""
-		if len(Elabel) > 0:
-			Estring = "\\n( " + createLabelList(Elabel) + " )"
-
-		dotString = dotString + "\t" + parent + " -> " + child \
-			+ " [" + bidirectional + "label=\"" + labelString + Estring + "\"" \
-			+ errorString + "];\n"
-
-
-	# Check for missing edges.
-	for ((parent,child), labels, oelabel) in edgeDiffList:
-		if labels == [('_',1.0)]:
-			errorString = ",color=red,penwidth=2.0,fontcolor=red"
-			otherLabel = createLabelList(getFirstElements(oelabel))
-			dotString = dotString + "\t" + parent + " -> " + child \
-					+ " [fontsize=14,label=\"_\\n( " + otherLabel + " )\"" + errorString + "];\n"
-
-	dotString = dotString + "}"
-	return(dotString)
+    """Produce a .dot string representation of a primitive graph."""
+    dotString = ""
+    dotString += primitiveNodeString(lg, nodeDiffList)
+    dotString += "\n\t/* EDGES (PRIMITIVE RELATIONSHIPS) */\n"
+    edges = list(lg.elabels)
+
+    # Edges/Mislabeled edges from graph lg, as appropriate.
+    # Check for opportunities to create bidirectional edges.
+    seen = set()
+    for (parent, child) in sorted(list(edges)):
+        errorString = ""
+        labelString = createLabelList(lg.elabels[(parent, child)])
+
+        # Skip edges that have already been created.
+        if (parent, child) in seen:
+            continue
+
+        bidirectional = ""
+        # Check label in opposite direction.
+        if (child, parent) in list(edges):
+            oppositeLabelString = createLabelList(lg.elabels[(child, parent)])
+            if labelString == oppositeLabelString:
+                bidirectional = "dir=both,"
+                seen.add((child, parent))
+
+        Elabel = []
+        for (pair, _, oelabel) in edgeDiffList:
+            # NOTE: specific to current implementation for missing edge repr.
+            if pair == (parent, child):
+                Elabel += getFirstElements(oelabel)
+                errorString = ",color=red,penwidth=2.0,fontsize=14,fontcolor=red"
+
+        Estring = ""
+        if len(Elabel) > 0:
+            Estring = "\\n( " + createLabelList(Elabel) + " )"
+
+        dotString = (
+            dotString
+            + "\t"
+            + parent
+            + " -> "
+            + child
+            + " ["
+            + bidirectional
+            + 'label="'
+            + labelString
+            + Estring
+            + '"'
+            + errorString
+            + "];\n"
+        )
+
+    # Check for missing edges.
+    for ((parent, child), labels, oelabel) in edgeDiffList:
+        if labels == [("_", 1.0)]:
+            errorString = ",color=red,penwidth=2.0,fontcolor=red"
+            otherLabel = createLabelList(getFirstElements(oelabel))
+            dotString = (
+                dotString
+                + "\t"
+                + parent
+                + " -> "
+                + child
+                + ' [fontsize=14,label="_\\n( '
+                + otherLabel
+                + ' )"'
+                + errorString
+                + "];\n"
+            )
+
+    dotString = dotString + "}"
+    return dotString
 
 
 def lgdot(lg, nodeDiffList, edgeDiffList, lg2=None):
-	"""Produce a .dot string representation for a bipartite graph."""
-	dotString = ""
-
-	dotString = dotString + bipartiteNodeString(lg,nodeDiffList,lg2)
-	dotString = dotString + "\n\t/*  EDGES (PRIMITIVE RELATIONSHIPS) */\n"
-	edges = list(lg.elabels)
-
-	# Edges/Mislabeled edges from graph lg, as appropriate.
-	for (parent, child) in sorted(list(edges)):
-		errorString = ""
-		otherLabel = []
-		for (pair,_,oelabel) in edgeDiffList:
-			# NOTE: specific to current implementation for missing edge repr.
-			if pair == (parent,child):
-				errorString = ",color=red,penwidth=2.0,fontsize=14,fontcolor=red"
-				otherLabel += getFirstElements(oelabel)  
-		otherString = ""
-		if len(otherLabel) > 0:
-			otherString = "\n( " + createLabelList(otherLabel) + " )"
-
-		labelString = createLabelList(lg.elabels[(parent,child)])
-		dotString = dotString + "\tL" + parent + " -> " + "R" + child \
-			+ " [label=\"" + labelString + otherString + "\"" \
-			+ errorString + "];\n"
-
-	# Check for missing edges.
-	for ((parent,child), labels, oelabel) in edgeDiffList:
-		if labels == [('_',1.0)]:
-			errorString = ",color=red,penwidth=2.0,fontcolor=red"
-			otherLabel = "\\n( " + createLabelList(getFirstElements(oelabel)) + " )"
-			dotString = dotString + "\tL" + parent + " -> " + "R" + child \
-				+ " [fontsize=14,label=\"_" + otherLabel + "\"" + errorString + "];\n"
-
-	dotString = dotString + "}"
-	return(dotString)
+    """Produce a .dot string representation for a bipartite graph."""
+    dotString = ""
+
+    dotString = dotString + bipartiteNodeString(lg, nodeDiffList, lg2)
+    dotString = dotString + "\n\t/*  EDGES (PRIMITIVE RELATIONSHIPS) */\n"
+    edges = list(lg.elabels)
+
+    # Edges/Mislabeled edges from graph lg, as appropriate.
+    for (parent, child) in sorted(list(edges)):
+        errorString = ""
+        otherLabel = []
+        for (pair, _, oelabel) in edgeDiffList:
+            # NOTE: specific to current implementation for missing edge repr.
+            if pair == (parent, child):
+                errorString = ",color=red,penwidth=2.0,fontsize=14,fontcolor=red"
+                otherLabel += getFirstElements(oelabel)
+        otherString = ""
+        if len(otherLabel) > 0:
+            otherString = "\n( " + createLabelList(otherLabel) + " )"
+
+        labelString = createLabelList(lg.elabels[(parent, child)])
+        dotString = (
+            dotString
+            + "\tL"
+            + parent
+            + " -> "
+            + "R"
+            + child
+            + ' [label="'
+            + labelString
+            + otherString
+            + '"'
+            + errorString
+            + "];\n"
+        )
+
+    # Check for missing edges.
+    for ((parent, child), labels, oelabel) in edgeDiffList:
+        if labels == [("_", 1.0)]:
+            errorString = ",color=red,penwidth=2.0,fontcolor=red"
+            otherLabel = "\\n( " + createLabelList(getFirstElements(oelabel)) + " )"
+            dotString = (
+                dotString
+                + "\tL"
+                + parent
+                + " -> "
+                + "R"
+                + child
+                + ' [fontsize=14,label="_'
+                + otherLabel
+                + '"'
+                + errorString
+                + "];\n"
+            )
+
+    dotString = dotString + "}"
+    return dotString
+
 
 def dagSegmentString(lg, lg2, segPrimMap, primSegMap, correctSegs):
-	dotString = "digraph dag {\n\trankdir=LR; ranksep=1.0;\n\tedge[fontsize=13,weight=1]; node[fontsize=13,shape=box]; graph[ordering=out];\n"
-
-	dotString = dotString + "\n\t/* NODES (OBJECTS) */\n\t"
-	for segmentId in sorted(list(list(segPrimMap))):
-		BadSegmentation = True
-		
-		# Search for this segment in the list of correct segments.
-		if segmentId in correctSegs:
-			BadSegmentation = False
-
-		# Get segment label,  all labels for primitives in the segment.
-		(segIds, slabel) = segPrimMap[ segmentId ]
-		otherLabel = createSegPrimitivesLabel(segmentId, lg2, segPrimMap) 
-
-		classError = ""
-		slabelString = createLabelList(slabel)
-		if not slabelString == otherLabel:
-			classError = '\\n(' + otherLabel + ')'
-
-		# Format segmentation errors differently than mislabelings,
-		# than correct segments.
-		if BadSegmentation:
-			color = 'red,peripheries=2,fontcolor=red'
-		elif len(classError) > 0:
-			color = 'red,fontcolor=red'
-		else:
-			color = 'blue'
-
-		dotString = dotString + segmentId + " [label=\"" \
-				+  slabelString + classError + "\\n" \
-				+  segmentId + "\\n"\
-				+  createLabelList(segIds," ") + "\", color = " + color + "];\n\t"
-	return dotString
-
-def dagSegmentRelString(lg, lg2, segPrimMap, treeEdges, otherEdges, segmentEdges,\
-		treeOnly, segRelDiffs ):
-	dotString = "\n\t/* EDGES (OBJECT RELATIONSHIPS)    */\n\t"
-
-	for edge in list(segRelDiffs):
-		formatting =",color=red,fontcolor=red,penwidth=3"
-	
-		if treeOnly and edge not in treeEdges:
-			continue 
-
-		# Produce edge labels.
-		parentPrim = list(segPrimMap[ edge[0] ][0])[0]
-		childPrim = list(segPrimMap[ edge[1] ][0])[0]
-		elabel = createLabelList(list(lg.elabels[ (parentPrim, childPrim) ]))
-
-		otherLabel = createRelPrimitivesLabel( edge, lg2, segPrimMap)
-
-		dotString += str(edge[0]) + ' -> ' + str(edge[1])
-		dotString += " [label=\"" + elabel  \
-				+ "\\n(" + otherLabel + ")\\n\""+ formatting + "]" + ';\n\t'
-
-	for edge in segmentEdges:
-		formatting = ""
-
-		# Skip incorrect edges; skip non-tree edges if instructed.
-		if edge in list(segRelDiffs) or \
-			(treeOnly and edge not in treeEdges):
-			continue
-
-		# Produce edge labels.
-		parentPrim = list(segPrimMap[ edge[0] ][0])[0]
-		childPrim = list(segPrimMap[ edge[1] ][0])[0]
-		elabel = createLabelList(list(lg.elabels[ (parentPrim, childPrim) ]))
-
-		dotString += str(edge[0]) + ' -> ' + str(edge[1])
-		dotString += " [label=\"" + str(elabel) + "\""+ formatting + "]" + ';\n\t'
-
-	dotString += '\n}'
-	return dotString
-	
+    dotString = "digraph dag {\n\trankdir=LR; ranksep=1.0;\n\tedge[fontsize=13,weight=1]; node[fontsize=13,shape=box]; graph[ordering=out];\n"
+
+    dotString = dotString + "\n\t/* NODES (OBJECTS) */\n\t"
+    for segmentId in sorted(list(list(segPrimMap))):
+        BadSegmentation = True
+
+        # Search for this segment in the list of correct segments.
+        if segmentId in correctSegs:
+            BadSegmentation = False
+
+        # Get segment label,  all labels for primitives in the segment.
+        (segIds, slabel) = segPrimMap[segmentId]
+        otherLabel = createSegPrimitivesLabel(segmentId, lg2, segPrimMap)
+
+        classError = ""
+        slabelString = createLabelList(slabel)
+        if not slabelString == otherLabel:
+            classError = "\\n(" + otherLabel + ")"
+
+        # Format segmentation errors differently than mislabelings,
+        # than correct segments.
+        if BadSegmentation:
+            color = "red,peripheries=2,fontcolor=red"
+        elif len(classError) > 0:
+            color = "red,fontcolor=red"
+        else:
+            color = "blue"
+
+        dotString = (
+            dotString
+            + segmentId
+            + ' [label="'
+            + slabelString
+            + classError
+            + "\\n"
+            + segmentId
+            + "\\n"
+            + createLabelList(segIds, " ")
+            + '", color = '
+            + color
+            + "];\n\t"
+        )
+    return dotString
+
+
+def dagSegmentRelString(
+    lg, lg2, segPrimMap, treeEdges, otherEdges, segmentEdges, treeOnly, segRelDiffs
+):
+    dotString = "\n\t/* EDGES (OBJECT RELATIONSHIPS)    */\n\t"
+
+    for edge in list(segRelDiffs):
+        formatting = ",color=red,fontcolor=red,penwidth=3"
+
+        if treeOnly and edge not in treeEdges:
+            continue
+
+        # Produce edge labels.
+        parentPrim = list(segPrimMap[edge[0]][0])[0]
+        childPrim = list(segPrimMap[edge[1]][0])[0]
+        elabel = createLabelList(list(lg.elabels[(parentPrim, childPrim)]))
+
+        otherLabel = createRelPrimitivesLabel(edge, lg2, segPrimMap)
+
+        dotString += str(edge[0]) + " -> " + str(edge[1])
+        dotString += (
+            ' [label="'
+            + elabel
+            + "\\n("
+            + otherLabel
+            + ')\\n"'
+            + formatting
+            + "]"
+            + ";\n\t"
+        )
+
+    for edge in segmentEdges:
+        formatting = ""
+
+        # Skip incorrect edges; skip non-tree edges if instructed.
+        if edge in list(segRelDiffs) or (treeOnly and edge not in treeEdges):
+            continue
+
+        # Produce edge labels.
+        parentPrim = list(segPrimMap[edge[0]][0])[0]
+        childPrim = list(segPrimMap[edge[1]][0])[0]
+        elabel = createLabelList(list(lg.elabels[(parentPrim, childPrim)]))
+
+        dotString += str(edge[0]) + " -> " + str(edge[1])
+        dotString += ' [label="' + str(elabel) + '"' + formatting + "]" + ";\n\t"
+
+    dotString += "\n}"
+    return dotString
+
 
 def lgDag(lg, lg2, treeOnly, correctSegs, segRelDiffs):
-	"""Directed graph over segments."""
-	(segmentPrimitiveMap, primitiveSegmentMap, noparentSegments, segmentEdges) = \
-				lg.segmentGraph()
-	(rootNodes, treeEdges, otherEdges) = lg.separateTreeEdges()
-	head = dagSegmentString(lg,lg2, segmentPrimitiveMap, primitiveSegmentMap,\
-			correctSegs)
-	rest = dagSegmentRelString(lg, lg2, segmentPrimitiveMap, treeEdges, otherEdges,\
-			segmentEdges, treeOnly, segRelDiffs)
-	return head + rest
+    """Directed graph over segments."""
+    (
+        segmentPrimitiveMap,
+        primitiveSegmentMap,
+        noparentSegments,
+        segmentEdges,
+    ) = lg.segmentGraph()
+    (rootNodes, treeEdges, otherEdges) = lg.separateTreeEdges()
+    head = dagSegmentString(
+        lg, lg2, segmentPrimitiveMap, primitiveSegmentMap, correctSegs
+    )
+    rest = dagSegmentRelString(
+        lg,
+        lg2,
+        segmentPrimitiveMap,
+        treeEdges,
+        otherEdges,
+        segmentEdges,
+        treeOnly,
+        segRelDiffs,
+    )
+    return head + rest
 
 
 def main():
-	if len(sys.argv) < 2:
-		print("Usage: [[python]] lg2dot.py <lg.csv> [lg2.csv] [ b | s | p | t ]")
-		print("")
-		print("    Produce a dot file containing either a:")
-		print("       1. (default) directed graph of relationships over objects, or a")
-		print("       2. (p) directed graph over primitives,")
-		print("       3. (b) bipartite graph over primitives,")
-		print("       4. (s) segmentation graph over primitives,")
-		print("       5. (t) tree of relationships over objects (assumes hierarchical structure).")
-		print("              (NOTE: all inherited relationships are removed from the graph)")
-		
-		print("")
-		print("    The object graphs (options d and t) only show objects and edges defined in")
-		print("    the first graph. To see all milabelings at the primitive level, use")
-		print("    the default or (s) options.")
-
-		print("\n    VISUALIZING DIFFERENCES BETWEEN GRAPHS\n")
-		print("    If two files are provided, both should use the same ")
-		print("    node (primitive) identifiers.")
-		print("")
-		print("    For two files, disagreements between the first and second graphs are shown")
-		print("    in red.")
-		print("\n    REPRESENTATION\n")
-		print("    Ovals are used for primitives, squares for objects, and squares with")
-		print("    doubled edges for objects appearing in lg.csv but not lg2.csv")
-		print("    (for segmentation errors). Labeling disagreements show lg1.csv")
-		print("    labels above labels from lg2.csv (shown in parentheses).")
-		print("    In the default and 'b' primitive graphs, objects are shown using")
-		print("    bidirectional edges between primitives in the same object, which")
-		print("    have the same label as the object.")
-		print("")
-		print("    NOTE: object-level plots ('d' and 't' options) assume one level of structure")
-		print("    for objects (i.e. each primitive belongs to one object).")
-		sys.exit(0)
-
-	fileName = sys.argv[1]
-	lg = Lg(fileName)
-
-	# Hide unlabeled edges.
-	lg.hideUnlabeledEdges()
-
-	if len(sys.argv) == 2:
-		# RZ: Modification: show the object graph.
-		# HACK: to get correct segments, compare graph with itself.
-		(_, nodeconflicts, edgeconflicts, segDiffs, correctSegs, \
-				segRelDiffs) = lg.compare(lg)
-		print( lgDag(lg, lg, False, correctSegs, segRelDiffs) )
-
-	elif len(sys.argv) == 3:
-		# Graph types for single graph - check second argument passed (sys.argv[2])
-		# - Bipartite graph (b)
-		# - DAG graph over segments (default)
-		# - Tree(s) over segments (t)
-		# - Segmentation graph over primitives (s)
-		# - Directed graph over primitives (p)
-
-		# HACK: to get correct segments, compare graph with itself.
-		(_, nodeconflicts, edgeconflicts, segDiffs, correctSegs, \
-				segRelDiffs) = lg.compare(lg)
-
-		if sys.argv[2] == 't':
-			print(lgDag(lg, lg, True, correctSegs, segRelDiffs))
-		elif sys.argv[2] == 's':
-			print( lgsegdot(lg, {}, {}) )
-		elif sys.argv[2] == 'b':
-			print(lgdot(lg, [], []))
-		elif sys.argv[2] == 'p':
-			print( lgPrimitiveDot(lg, nodeconflicts, edgeconflicts) )
-		else: 
-			# Difference DAG over objects.
-			comparisonFileName = sys.argv[2]
-			lg2 = Lg(comparisonFileName)
-			(_, nodeconflicts, edgeconflicts, segDiffs, correctSegs, \
-				segRelDiffs) = lg.compare(lg2)
-			print(lgDag(lg, lg2,  False, correctSegs, segRelDiffs))
-	
-
-	elif len(sys.argv) > 3:
-		# Compute graph difference.
-		comparisonFileName = sys.argv[2]
-		lg2 = Lg(comparisonFileName)
-		(_, nodeconflicts, edgeconflicts, segDiffs, correctSegs, \
-			segRelDiffs) = lg.compare(lg2)
-
-		# Produce the appropriate graph type.
-		if sys.argv[3] == 's':
-			print( lgsegdot(lg, nodeconflicts, segDiffs, lg2) )
-		elif (sys.argv[3] == 'd'):
-			print( lgDag(lg, lg2, False, correctSegs, segRelDiffs) )
-		elif (sys.argv[3] == 't'):
-			print( lgDag(lg, lg2, True, correctSegs, segRelDiffs) )
-		elif (sys.argv[3] == 'b'):
-			print( lgdot(lg, nodeconflicts, edgeconflicts, lg2) )
-		else:
-			print( lgPrimitiveDot(lg, nodeconflicts, edgeconflicts) )
+    if len(sys.argv) < 2:
+        print("Usage: [[python]] lg2dot.py <lg.csv> [lg2.csv] [ b | s | p | t ]")
+        print("")
+        print("    Produce a dot file containing either a:")
+        print("       1. (default) directed graph of relationships over objects, or a")
+        print("       2. (p) directed graph over primitives,")
+        print("       3. (b) bipartite graph over primitives,")
+        print("       4. (s) segmentation graph over primitives,")
+        print(
+            "       5. (t) tree of relationships over objects (assumes hierarchical structure)."
+        )
+        print(
+            "              (NOTE: all inherited relationships are removed from the graph)"
+        )
+
+        print("")
+        print(
+            "    The object graphs (options d and t) only show objects and edges defined in"
+        )
+        print("    the first graph. To see all milabelings at the primitive level, use")
+        print("    the default or (s) options.")
+
+        print("\n    VISUALIZING DIFFERENCES BETWEEN GRAPHS\n")
+        print("    If two files are provided, both should use the same ")
+        print("    node (primitive) identifiers.")
+        print("")
+        print(
+            "    For two files, disagreements between the first and second graphs are shown"
+        )
+        print("    in red.")
+        print("\n    REPRESENTATION\n")
+        print(
+            "    Ovals are used for primitives, squares for objects, and squares with"
+        )
+        print("    doubled edges for objects appearing in lg.csv but not lg2.csv")
+        print("    (for segmentation errors). Labeling disagreements show lg1.csv")
+        print("    labels above labels from lg2.csv (shown in parentheses).")
+        print("    In the default and 'b' primitive graphs, objects are shown using")
+        print("    bidirectional edges between primitives in the same object, which")
+        print("    have the same label as the object.")
+        print("")
+        print(
+            "    NOTE: object-level plots ('d' and 't' options) assume one level of structure"
+        )
+        print("    for objects (i.e. each primitive belongs to one object).")
+        sys.exit(0)
+
+    fileName = sys.argv[1]
+    lg = Lg(fileName)
+
+    # Hide unlabeled edges.
+    lg.hideUnlabeledEdges()
+
+    if len(sys.argv) == 2:
+        # RZ: Modification: show the object graph.
+        # HACK: to get correct segments, compare graph with itself.
+        (
+            _,
+            nodeconflicts,
+            edgeconflicts,
+            segDiffs,
+            correctSegs,
+            segRelDiffs,
+        ) = lg.compare(lg)
+        print(lgDag(lg, lg, False, correctSegs, segRelDiffs))
+
+    elif len(sys.argv) == 3:
+        # Graph types for single graph - check second argument passed (sys.argv[2])
+        # - Bipartite graph (b)
+        # - DAG graph over segments (default)
+        # - Tree(s) over segments (t)
+        # - Segmentation graph over primitives (s)
+        # - Directed graph over primitives (p)
+
+        # HACK: to get correct segments, compare graph with itself.
+        (
+            _,
+            nodeconflicts,
+            edgeconflicts,
+            segDiffs,
+            correctSegs,
+            segRelDiffs,
+        ) = lg.compare(lg)
+
+        if sys.argv[2] == "t":
+            print(lgDag(lg, lg, True, correctSegs, segRelDiffs))
+        elif sys.argv[2] == "s":
+            print(lgsegdot(lg, {}, {}))
+        elif sys.argv[2] == "b":
+            print(lgdot(lg, [], []))
+        elif sys.argv[2] == "p":
+            print(lgPrimitiveDot(lg, nodeconflicts, edgeconflicts))
+        else:
+            # Difference DAG over objects.
+            comparisonFileName = sys.argv[2]
+            lg2 = Lg(comparisonFileName)
+            (
+                _,
+                nodeconflicts,
+                edgeconflicts,
+                segDiffs,
+                correctSegs,
+                segRelDiffs,
+            ) = lg.compare(lg2)
+            print(lgDag(lg, lg2, False, correctSegs, segRelDiffs))
+
+    elif len(sys.argv) > 3:
+        # Compute graph difference.
+        comparisonFileName = sys.argv[2]
+        lg2 = Lg(comparisonFileName)
+        (
+            _,
+            nodeconflicts,
+            edgeconflicts,
+            segDiffs,
+            correctSegs,
+            segRelDiffs,
+        ) = lg.compare(lg2)
+
+        # Produce the appropriate graph type.
+        if sys.argv[3] == "s":
+            print(lgsegdot(lg, nodeconflicts, segDiffs, lg2))
+        elif sys.argv[3] == "d":
+            print(lgDag(lg, lg2, False, correctSegs, segRelDiffs))
+        elif sys.argv[3] == "t":
+            print(lgDag(lg, lg2, True, correctSegs, segRelDiffs))
+        elif sys.argv[3] == "b":
+            print(lgdot(lg, nodeconflicts, edgeconflicts, lg2))
+        else:
+            print(lgPrimitiveDot(lg, nodeconflicts, edgeconflicts))
+
 
 # RZ: Avoid invocation on import
 if __name__ == "__main__":
     main()
-
diff --git a/src/lg2txt.py b/src/lg2txt.py
index 96de3b3..ba51fc4 100644
--- a/src/lg2txt.py
+++ b/src/lg2txt.py
@@ -52,7 +52,7 @@ def readtranslateFile(mapFile):
 def readMapFile(fileName):
     """Read in symbol and structure mappings from a file."""
     try:
-        fileReader = csv.reader(open(fileName, encoding='utf8'))
+        fileReader = csv.reader(open(fileName, encoding="utf8"))
     except:
         sys.stderr.write("  !! IO Error (cannot open): " + fileName + "\n")
         return
@@ -112,7 +112,7 @@ def translateStructure(
     symbolMap,
     segId,
     nodeString,
-    warnings = True
+    warnings=True,
 ):
     """Generate a string for a given structure."""
     strString = ""
@@ -151,8 +151,13 @@ def translateStructure(
                 (childId, relation) = nodeRelationPairs[j]
                 if relation == nextRelation:
                     strString += translate(
-                        lg, childId, segPrimMap, edgeMap, symbolMap, 
-                        structureMap, warnings
+                        lg,
+                        childId,
+                        segPrimMap,
+                        edgeMap,
+                        symbolMap,
+                        structureMap,
+                        warnings,
                     )
                     match = True
                     break
@@ -179,8 +184,13 @@ def translateStructure(
                 (childId, relation) = nodeRelationPairs[j]
                 if relation == nextRelation:
                     strString += translate(
-                        lg, childId, segPrimMap, edgeMap, symbolMap, 
-                        structureMap, warnings
+                        lg,
+                        childId,
+                        segPrimMap,
+                        edgeMap,
+                        symbolMap,
+                        structureMap,
+                        warnings,
                     )
                     match = True
                     break
@@ -201,7 +211,15 @@ def translateStructure(
 
 
 def translateRelation(
-    lg, relation, nextChildId, structureMap, segPrimMap, edgeMap, symbolMap, nodeString, warnings=True
+    lg,
+    relation,
+    nextChildId,
+    structureMap,
+    segPrimMap,
+    edgeMap,
+    symbolMap,
+    nodeString,
+    warnings=True,
 ):
     """Translate an individual spatial relation."""
     relString = ""
@@ -211,7 +229,9 @@ def translateRelation(
         replacementTuple = structureMap[relation]
 
     else:
-        sys.stderr.write("  !! lg2txt Warning: Unknown relationship label " + relation + "\n")
+        sys.stderr.write(
+            "  !! lg2txt Warning: Unknown relationship label " + relation + "\n"
+        )
         sys.stderr.write(
             "  !!        Using relationship mapping: "
             + str(structureMap["REL_DEFAULT"])
@@ -230,8 +250,7 @@ def translateRelation(
             relString += nodeString
         elif nextEntry == "CHILD":
             relString += translate(
-                lg, nextChildId, segPrimMap, edgeMap, symbolMap, structureMap,
-                warnings
+                lg, nextChildId, segPrimMap, edgeMap, symbolMap, structureMap, warnings
             )
         else:
             relString += replacementTuple[i]
@@ -239,8 +258,7 @@ def translateRelation(
     return relString
 
 
-def translate(lg, segId, segPrimMap, edgeMap, symbolMap, structureMap,
-        warnings = True):
+def translate(lg, segId, segPrimMap, edgeMap, symbolMap, structureMap, warnings=True):
     """Recursively create output for an expression at the object level."""
     byValue = lambda pair: pair[1]
     byRel = lambda pair: pair[0]
@@ -266,7 +284,9 @@ def translate(lg, segId, segPrimMap, edgeMap, symbolMap, structureMap,
             .replace("_L_", label)
         )
         if warnings:
-            sys.stderr.write("  !! lg2txt Warning: Unknown object label " + label + "\n")
+            sys.stderr.write(
+                "  !! lg2txt Warning: Unknown object label " + label + "\n"
+            )
 
     if segId in edgeMap:
         # This node has children - lookup replacement based on sorted labels
@@ -317,7 +337,7 @@ def translate(lg, segId, segPrimMap, edgeMap, symbolMap, structureMap,
             symbolMap,
             segId,
             nodeString,
-            warnings
+            warnings,
         )
         if not strString == "":
             nodeString = strString
@@ -333,7 +353,7 @@ def translate(lg, segId, segPrimMap, edgeMap, symbolMap, structureMap,
                 symbolMap,
                 segId,
                 nodeString,
-                warnings
+                warnings,
             )
             if not strString == "":
                 nodeString = strString
@@ -347,7 +367,7 @@ def translate(lg, segId, segPrimMap, edgeMap, symbolMap, structureMap,
                         edgeMap,
                         symbolMap,
                         nodeString,
-                        warnings
+                        warnings,
                     )
 
                     # nodeString += translateRelation(lg, (relation, nextChildId),\
@@ -364,7 +384,7 @@ def translate(lg, segId, segPrimMap, edgeMap, symbolMap, structureMap,
                         edgeMap,
                         symbolMap,
                         nodeString,
-                        warnings
+                        warnings,
                     )
 
         # Lastly, generate string for adjacent symbols on the baseline.
@@ -379,7 +399,7 @@ def translate(lg, segId, segPrimMap, edgeMap, symbolMap, structureMap,
                 edgeMap,
                 symbolMap,
                 nodeString,
-                warnings
+                warnings,
             )
 
     return nodeString
@@ -455,9 +475,13 @@ def lg2mml(lg_file, mapFile, warnings=True):
         # 		symbolMap, structureMap))
         mml_out_raw.append(
             translate(
-                lg, root, segmentPrimitiveMap, treeEdgeMap, symbolMap, 
-                structureMap, 
-                warnings
+                lg,
+                root,
+                segmentPrimitiveMap,
+                treeEdgeMap,
+                symbolMap,
+                structureMap,
+                warnings,
             )
         )
     # mml_out = postprocess("\n".join(mml_out_raw))
@@ -470,7 +494,7 @@ def preprocess(filename, translateFile, translate=True):
     lg = Lg(filename)
     lg_NE_string = lg.csv(sort=False)
     if translate:
-        ( symbolsMap, relationsMap ) = readtranslateFile(translateFile)
+        (symbolsMap, relationsMap) = readtranslateFile(translateFile)
         edge_pattern = re.compile(r"^E,")
         node_pattern = re.compile(r"(^N,)|#")
         symbol_temp = relabel(lg_NE_string, symbolsMap, node_pattern)
@@ -479,7 +503,7 @@ def preprocess(filename, translateFile, translate=True):
         )
         lg_NE_string = symbol_temp + "\n" + edge_temp
 
-    if type(filename) == type( StringIO('')):
+    if type(filename) == type(StringIO("")):
         return lg_NE_string
     else:
         temp_file = "temp.lg"
@@ -496,17 +520,19 @@ def relabel(lg_NE_string, Map, pattern):
         temp = temp.replace("," + source_label + ",", "," + mapped_label + ",")
     return temp
 
-def get_MML( lgString, mapFile, translateFile, warnings=True):
+
+def get_MML(lgString, mapFile, translateFile, warnings=True):
     # RZ: add ability to use StringIO and avoid writing temp file.
 
     # Create StringIO object
-    stringIO = StringIO( lgString )
+    stringIO = StringIO(lgString)
 
-    intermString = preprocess( stringIO, translateFile )
-    intermStringIO = StringIO( intermString )
-    mml_out = lg2mml( intermStringIO, mapFile, warnings )
+    intermString = preprocess(stringIO, translateFile)
+    intermStringIO = StringIO(intermString)
+    mml_out = lg2mml(intermStringIO, mapFile, warnings)
     return mml_out
 
+
 def main(lg_file, mapFile, translateFile):
     temp_lg_file = preprocess(lg_file, translateFile)
     mml_out = lg2mml(temp_lg_file, mapFile)
diff --git a/src/lgfilter.py b/src/lgfilter.py
index 1b713dd..7fb3d4a 100644
--- a/src/lgfilter.py
+++ b/src/lgfilter.py
@@ -1,7 +1,7 @@
 ###########################################################################
 #
 # lgfilter.py
-#	- Warning: this assumes that edges and nodes have a single label.
+# 	- Warning: this assumes that edges and nodes have a single label.
 #
 # Author: R. Zanibbi, March 2013
 # Copyright (c) 2012-2014 Richard Zanibbi and Harold Mouchere
@@ -10,79 +10,98 @@
 import sys
 from lgeval.src.lg import *
 
+
 def main():
-	if (len(sys.argv) < 2):
-		print("Usage: python lgfilter.py <infile> [outfile]")
-		print("")
-		print("Removes non-tree edges in the graph (assuming a tree is present!)")
-		print("and produces an .lg file with objects and relationships defined")
-		print("at the bottom of the file.")
-		print("")
-		print("Warning: this assumes that each node and edge has a single label.")
-		return
-	
-	fileName = sys.argv[1]
-	lg = Lg(fileName)
-
-	(segmentPrimitiveMap, primitiveSegmentMap, noparentSegments, segmentEdges) = \
-			lg.segmentGraph()
-	(rootNodes, treeEdges, otherEdges) = lg.separateTreeEdges()
-
-	# Remove all edges that aren't from the set of treeEdges between segments.
-	removedNotes = ''
-	for (seg1, seg2) in otherEdges:
-		seg1prims = segmentPrimitiveMap[ seg1 ][0]
-		seg2prims = segmentPrimitiveMap[ seg2 ][0]
-
-		for prim1 in seg1prims:
-			for prim2 in seg2prims:
-				removedNotes += '#   Removed (' + str(prim1) + ',' + str(prim2) \
-						+ ') ' + str(lg.elabels[(prim1,prim2)]) + '\n'
-				del lg.elabels[(prim1,prim2)]
-	
-	topString = '# Created by lgfilter.py from ' + fileName + '\n' 
-	topString += removedNotes
-	topString += lg.csv() +'\n'
-
-	# Create a list of segments and relationships.
-	objectList = '# [ OBJECTS ]\n#   Format: [Label] prim1 ... primN\n'
-	for segment in list(segmentPrimitiveMap):
-		segString =''
-		segLabel = 'ERROR'
-		for stroke in segmentPrimitiveMap[segment][0]:
-			segString += str(stroke) + ' '
-			segLabel = str(list(lg.nlabels[stroke])[0])
-		objectList += '# ' + segLabel + ' ' + segString + '\n'
-
-	objectList += '# [ RELS ]\n#   Format:  [Label] parentPrim1 ... parentPrimN :: childPrim1 ... childPrimM\n'
-
-	for (seg1,seg2) in treeEdges:
-		seg1prims = segmentPrimitiveMap[ seg1 ][0]
-		seg2prims = segmentPrimitiveMap[ seg2 ][0]
-
-		list1 = ''
-		p1 = ''
-		for prim1 in seg1prims:
-			list1 += prim1 + ' '
-			p1 = prim1
-
-		list2 = ''
-		for prim2 in seg2prims:
-			list2 += prim2 + ' '
-			p2 = prim2
-
-		objectList += '# ' + str(list(lg.elabels[(p1,p2)])[0]) + ' ' + list1 + ':: ' + list2 + '\n'
-
-	# Generate output.
-	if (len(sys.argv) > 2):
-		# To file if output file name provided.
-		outFileHandle = open( sys.argv[2],'w')
-		outFileHandle.write(topString + objectList)
-		outFileHandle.close()
-	else:
-		# Otherwise to standard output.
-		print(topString + objectList)
+    if len(sys.argv) < 2:
+        print("Usage: python lgfilter.py <infile> [outfile]")
+        print("")
+        print("Removes non-tree edges in the graph (assuming a tree is present!)")
+        print("and produces an .lg file with objects and relationships defined")
+        print("at the bottom of the file.")
+        print("")
+        print("Warning: this assumes that each node and edge has a single label.")
+        return
 
+    fileName = sys.argv[1]
+    lg = Lg(fileName)
 
-main()
+    (
+        segmentPrimitiveMap,
+        primitiveSegmentMap,
+        noparentSegments,
+        segmentEdges,
+    ) = lg.segmentGraph()
+    (rootNodes, treeEdges, otherEdges) = lg.separateTreeEdges()
+
+    # Remove all edges that aren't from the set of treeEdges between segments.
+    removedNotes = ""
+    for (seg1, seg2) in otherEdges:
+        seg1prims = segmentPrimitiveMap[seg1][0]
+        seg2prims = segmentPrimitiveMap[seg2][0]
+
+        for prim1 in seg1prims:
+            for prim2 in seg2prims:
+                removedNotes += (
+                    "#   Removed ("
+                    + str(prim1)
+                    + ","
+                    + str(prim2)
+                    + ") "
+                    + str(lg.elabels[(prim1, prim2)])
+                    + "\n"
+                )
+                del lg.elabels[(prim1, prim2)]
+
+    topString = "# Created by lgfilter.py from " + fileName + "\n"
+    topString += removedNotes
+    topString += lg.csv() + "\n"
+
+    # Create a list of segments and relationships.
+    objectList = "# [ OBJECTS ]\n#   Format: [Label] prim1 ... primN\n"
+    for segment in list(segmentPrimitiveMap):
+        segString = ""
+        segLabel = "ERROR"
+        for stroke in segmentPrimitiveMap[segment][0]:
+            segString += str(stroke) + " "
+            segLabel = str(list(lg.nlabels[stroke])[0])
+        objectList += "# " + segLabel + " " + segString + "\n"
+
+    objectList += "# [ RELS ]\n#   Format:  [Label] parentPrim1 ... parentPrimN :: childPrim1 ... childPrimM\n"
 
+    for (seg1, seg2) in treeEdges:
+        seg1prims = segmentPrimitiveMap[seg1][0]
+        seg2prims = segmentPrimitiveMap[seg2][0]
+
+        list1 = ""
+        p1 = ""
+        for prim1 in seg1prims:
+            list1 += prim1 + " "
+            p1 = prim1
+
+        list2 = ""
+        for prim2 in seg2prims:
+            list2 += prim2 + " "
+            p2 = prim2
+
+        objectList += (
+            "# "
+            + str(list(lg.elabels[(p1, p2)])[0])
+            + " "
+            + list1
+            + ":: "
+            + list2
+            + "\n"
+        )
+
+    # Generate output.
+    if len(sys.argv) > 2:
+        # To file if output file name provided.
+        outFileHandle = open(sys.argv[2], "w")
+        outFileHandle.write(topString + objectList)
+        outFileHandle.close()
+    else:
+        # Otherwise to standard output.
+        print(topString + objectList)
+
+
+main()
diff --git a/src/lgio.py b/src/lgio.py
index d3cf09f..8348944 100644
--- a/src/lgio.py
+++ b/src/lgio.py
@@ -10,82 +10,84 @@ import sys
 import csv
 from lgeval.src.lg import *
 
+
 def writeDiff(ndiffs, sdiffs, ediffs, streamFile):
-	"""Write differences for label graphs and (implicit) trees
+    """Write differences for label graphs and (implicit) trees
 	to the passed stream."""
-	for ndiff in ndiffs:
-		streamFile.write('*N,' + ndiff[0] + ',')
-		for symbol in ndiff[1]:
-			streamFile.write(symbol[0] + ',')
-			streamFile.write(str(symbol[1]) + ',')
-		streamFile.write(':vs:,')
+    for ndiff in ndiffs:
+        streamFile.write("*N," + ndiff[0] + ",")
+        for symbol in ndiff[1]:
+            streamFile.write(symbol[0] + ",")
+            streamFile.write(str(symbol[1]) + ",")
+        streamFile.write(":vs:,")
 
-		for i in range(0,len(ndiff[2])):
-			streamFile.write(ndiff[2][i][0] + ',')
-			streamFile.write(str(ndiff[2][i][1]))
-			if i < len(ndiff[2][i]) - 2:
-				streamFile.write(',')
-		streamFile.write('\n')
+        for i in range(0, len(ndiff[2])):
+            streamFile.write(ndiff[2][i][0] + ",")
+            streamFile.write(str(ndiff[2][i][1]))
+            if i < len(ndiff[2][i]) - 2:
+                streamFile.write(",")
+        streamFile.write("\n")
 
-	# Edges
-	for ediff in ediffs:
-		streamFile.write('*E,' + ediff[0][0] + ',' + ediff[0][1] + ',')
-		for symbol in ediff[1]:
-			streamFile.write(symbol[0] + ',')
-			streamFile.write(str(symbol[1]) + ',')
-		streamFile.write(':vs:,')
+    # Edges
+    for ediff in ediffs:
+        streamFile.write("*E," + ediff[0][0] + "," + ediff[0][1] + ",")
+        for symbol in ediff[1]:
+            streamFile.write(symbol[0] + ",")
+            streamFile.write(str(symbol[1]) + ",")
+        streamFile.write(":vs:,")
 
-		for i in range(0,len(ediff[2])):
-			streamFile.write(str(ediff[2][i][0]) + ',')
-			streamFile.write(str(ediff[2][i][1]))
-			if i < len(ediff[2]) - 1:
-				streamFile.write(',')
-		streamFile.write('\n')
+        for i in range(0, len(ediff[2])):
+            streamFile.write(str(ediff[2][i][0]) + ",")
+            streamFile.write(str(ediff[2][i][1]))
+            if i < len(ediff[2]) - 1:
+                streamFile.write(",")
+        streamFile.write("\n")
 
-	# Segmentation graph edges. Simply construct all pairs,
-	# as disagreeing edges are common.
-	for sdiff in list(sdiffs):
-		primitiveEndSet = sdiffs[sdiff]
-		for end in primitiveEndSet[0]:
-			streamFile.write('*S,' + str(sdiff) + ',' + str(end) + '\n')
-		for end in primitiveEndSet[1]:
-			streamFile.write('*S,' + str(sdiff) + ',' + str(end) + '\n')
+    # Segmentation graph edges. Simply construct all pairs,
+    # as disagreeing edges are common.
+    for sdiff in list(sdiffs):
+        primitiveEndSet = sdiffs[sdiff]
+        for end in primitiveEndSet[0]:
+            streamFile.write("*S," + str(sdiff) + "," + str(end) + "\n")
+        for end in primitiveEndSet[1]:
+            streamFile.write("*S," + str(sdiff) + "," + str(end) + "\n")
 
 
 def writeCSVTuple(tuple, secondComma, streamFile):
-	for i in range(0,len(tuple) - 1):
-		streamFile.write(str(tuple[i]))
-		streamFile.write(',')
-	streamFile.write(str(tuple[len(tuple) - 1]))
-	if secondComma:
-		streamFile.write(',')
+    for i in range(0, len(tuple) - 1):
+        streamFile.write(str(tuple[i]))
+        streamFile.write(",")
+    streamFile.write(str(tuple[len(tuple) - 1]))
+    if secondComma:
+        streamFile.write(",")
+
 
 def writeMetrics(metrics, streamFile):
-	"""Computes numerical metrics (for node and edge labels, and segmentation
+    """Computes numerical metrics (for node and edge labels, and segmentation
 	graph edge disagreements), and writes them to standard output."""
-	numMetrics = len(metrics[0])
-	for i in range(0,numMetrics - 1):
-		writeCSVTuple(metrics[0][i],True, streamFile)
-	writeCSVTuple(metrics[0][numMetrics - 1],False, streamFile)
-	streamFile.write('\n')
+    numMetrics = len(metrics[0])
+    for i in range(0, numMetrics - 1):
+        writeCSVTuple(metrics[0][i], True, streamFile)
+    writeCSVTuple(metrics[0][numMetrics - 1], False, streamFile)
+    streamFile.write("\n")
+
 
 ################################################################
 # Input
 ################################################################
 def fileListToLgs(fileName):
-	"""Given a file containing a list of .lg files, return the
+    """Given a file containing a list of .lg files, return the
 	list of corresponding label graphs."""
-	file = open(fileName)
-	fileReader = csv.reader(file)
-
-	lgList = []
-	for row in fileReader:
-		# Skip comments and empty lines.
-		if not row == [] and not row[0].strip()[0] == "#":
-			lgfile1 = row[0].strip() # remove leading/trailing whitespace
-			lg1 = Lg(lgfile1)
-			lgList += [ lg1 ]
+    file = open(fileName)
+    fileReader = csv.reader(file)
 
-	file.close()
-	return lgList
+    lgList = []
+    for row in fileReader:
+        # Skip comments and empty lines.
+        if not row == [] and not row[0].strip()[0] == "#":
+            lgfile1 = row[0].strip()  # remove leading/trailing whitespace
+            lg1 = Lg(lgfile1)
+            lgList += [lg1]
 
+    file.close()
+    return lgList
diff --git a/src/mergeLG.py b/src/mergeLG.py
index 6a5486c..a6ce3b9 100644
--- a/src/mergeLG.py
+++ b/src/mergeLG.py
@@ -14,20 +14,19 @@ from lgeval.src.lg import *
 
 
 def main():
-	if len(sys.argv) < 3:
-		print("Usage: [[python]] mergeLG.py <file1.lg> <file2.lg> ... ")
-		print("reads in two or more .lg (CSV) files, merges")
-		print("them and prints the new LG graph on standard output.")
-		sys.exit(0)
-	
-	n1 = Lg(sys.argv[1])
-	print '#'+sys.argv[1]
-	for f in sys.argv[2:]:
-		print '#'+f
-		n2 = Lg(f)
-		n1.addWeightedLabelValues(n2)
-	print n1.csv()
-		
-main()
-		
+    if len(sys.argv) < 3:
+        print ("Usage: [[python]] mergeLG.py <file1.lg> <file2.lg> ... ")
+        print ("reads in two or more .lg (CSV) files, merges")
+        print ("them and prints the new LG graph on standard output.")
+        sys.exit(0)
+
+    n1 = Lg(sys.argv[1])
+    print "#" + sys.argv[1]
+    for f in sys.argv[2:]:
+        print "#" + f
+        n2 = Lg(f)
+        n1.addWeightedLabelValues(n2)
+    print n1.csv()
 
+
+main()
diff --git a/src/metricDist.py b/src/metricDist.py
index 7877dbb..2bd2769 100644
--- a/src/metricDist.py
+++ b/src/metricDist.py
@@ -1,7 +1,7 @@
 ################################################################
 # metricDist.py
 #
-# Filter to collect the distribution for a metric in a given 
+# Filter to collect the distribution for a metric in a given
 # .m (metrics) file.
 #
 # Author: R. Zanibbi, June 2012
@@ -11,54 +11,56 @@ import sys
 import csv
 import math
 
+
 def main():
-	if len(sys.argv) < 3:
-		print("Usage : [[python]] metricDist.py <metric_name> <file1.m> [sort]\n")
-		print("    Print all values for the passed metric name/field in")
-		print("    metric file file1.m to the standard output. By default")
-		print("    results are returned as (file,value) pairs in order of")
-		print("    appearance.")
-		print("")
-		print("    !NOTE! For metrics with special characters, e.g. 'DB(%)',")
-		print("    the metric name *must* be passed in double quotes.")
-		print("")
-		print("    Any third argument (e.g. 'sort') will result in sorting")
-		print("    the values before they are output.")
-		sys.exit(0)
+    if len(sys.argv) < 3:
+        print("Usage : [[python]] metricDist.py <metric_name> <file1.m> [sort]\n")
+        print("    Print all values for the passed metric name/field in")
+        print("    metric file file1.m to the standard output. By default")
+        print("    results are returned as (file,value) pairs in order of")
+        print("    appearance.")
+        print("")
+        print("    !NOTE! For metrics with special characters, e.g. 'DB(%)',")
+        print("    the metric name *must* be passed in double quotes.")
+        print("")
+        print("    Any third argument (e.g. 'sort') will result in sorting")
+        print("    the values before they are output.")
+        sys.exit(0)
+
+    # Read metric data from CSV file.
+    fileName = sys.argv[2]
+    fileOut = open("listStrokeN.txt", "w")
+    try:
+        fileReader = csv.reader(open(fileName))
+    except:
+        sys.stderr.write("  !! IO Error (cannot open): " + fileName)
+        sys.exit(0)
 
-	# Read metric data from CSV file.
-	fileName = sys.argv[2]
-	fileOut = open("listStrokeN.txt","w")
-	try:
-		fileReader = csv.reader(open(fileName))
-	except:
-		sys.stderr.write('  !! IO Error (cannot open): ' + fileName)
-		sys.exit(0)
+    # Collect all values for the given metric; associate these with files.
+    values = []
+    for row in fileReader:
+        # Skip blank lines.
+        if len(row) == 0:
+            continue
 
-	# Collect all values for the given metric; associate these with files.
-	values = []
-	for row in fileReader:
-		# Skip blank lines.
-		if len(row) == 0:
-			continue
+        entryType = row[0].strip()
+        if entryType == "*M":
+            file = row[1]
+            continue
 
-		entryType = row[0].strip()
-		if entryType == "*M":
-			file = row[1]
-			continue
+        for i in range(0, len(row), 2):
+            metricName = row[i].strip()
+            if metricName == sys.argv[1]:
+                values = values + [(file, float(row[i + 1].strip()))]
 
-		for i in range(0,len(row),2):
-			metricName = row[i].strip()
-			if metricName == sys.argv[1]:
-				values = values + [ (file, float(row[i+1].strip()) ) ]
+    # Sort if user requested this.
+    byValue = lambda pair: pair[1]  # define key for sort comparisons.
+    if len(sys.argv) > 3:
+        values = sorted(values, key=byValue)
 
-	# Sort if user requested this.
-	byValue = lambda pair: pair[1]  # define key for sort comparisons.
-	if len(sys.argv) > 3:
-		values = sorted(values, key=byValue)
+    # Print the values for the metric.
+    for val in values:
+        print(val[0] + ", " + str(val[1]))
 
-	# Print the values for the metric.
-	for val in values:
-		print(val[0] + ', ' + str(val[1]))
 
 main()
diff --git a/src/mml.py b/src/mml.py
index dc45079..eb49f2d 100644
--- a/src/mml.py
+++ b/src/mml.py
@@ -6,8 +6,13 @@ import os.path as osp
 
 from lgeval.src.lg import Lg
 import lgeval.src.lg2txt as lg2txt
-from lgeval.src.settings import LABELS_TRANS_CSV, MATHML_MAP_DIR, CROHMELIB_SRC_DIR, \
-                    CROHMELIB_GRAMMAR_DIR, MATHML_TXL_FILE
+from lgeval.src.settings import (
+    LABELS_TRANS_CSV,
+    MATHML_MAP_DIR,
+    CROHMELIB_SRC_DIR,
+    CROHMELIB_GRAMMAR_DIR,
+    MATHML_TXL_FILE,
+)
 
 
 def readMapFile(mapFile):
@@ -15,7 +20,7 @@ def readMapFile(mapFile):
     try:
         fileReader = csv.reader(open(mapFile))
     except:
-        sys.stderr.write('  !! IO Error (cannot open): ' + mapFile + '\n')
+        sys.stderr.write("  !! IO Error (cannot open): " + mapFile + "\n")
         return
 
     symbolsMap = {}
@@ -25,11 +30,11 @@ def readMapFile(mapFile):
         # Skip blank lines and comments.
         if len(row) == 0:
             continue
-        elif row[0].strip()[0] == '#':
+        elif row[0].strip()[0] == "#":
             continue
-        elif row[0].strip() == 'SYMBOLS':
+        elif row[0].strip() == "SYMBOLS":
             readingSymbols = True
-        elif row[0].strip() == 'RELATIONSHIPS':
+        elif row[0].strip() == "RELATIONSHIPS":
             readingSymbols = False
         else:
             if readingSymbols:
@@ -40,34 +45,38 @@ def readMapFile(mapFile):
 
 
 def get_mml(filename, translate=True):
-    temp_lg = osp.join(osp.split(filename)[0], "temp_" + osp.split(filename)[1] )
+    temp_lg = osp.join(osp.split(filename)[0], "temp_" + osp.split(filename)[1])
     inputFile = Lg(filename)
     lg_NE_string = inputFile.csv()
     if translate:
         symbolsMap, relationsMap = readMapFile(LABELS_TRANS_CSV)
-        edge_pattern = re.compile(r'^E,')
-        node_pattern = re.compile(r'(^N,)|#')
+        edge_pattern = re.compile(r"^E,")
+        node_pattern = re.compile(r"(^N,)|#")
         symbol_temp = relabel(lg_NE_string, symbolsMap, node_pattern)
-        edge_temp = relabel(lg_NE_string, dict(**symbolsMap, **relationsMap), 
-                            edge_pattern)
+        edge_temp = relabel(
+            lg_NE_string, dict(**symbolsMap, **relationsMap), edge_pattern
+        )
         lg_NE_string = symbol_temp + "\n" + edge_temp
     print("\n" + temp_lg)
-    with open(temp_lg, 'w') as f:
+    with open(temp_lg, "w") as f:
         f.writelines(lg_NE_string)
-    
+
     try:
-        mml_out= lg2txt.main(temp_lg, MATHML_MAP_DIR)
+        mml_out = lg2txt.main(temp_lg, MATHML_MAP_DIR)
     except:
         mml_out = ""
     return mml_out
-        
+
+
 def relabel(lg_NE_string, Map, pattern):
-    temp = '\n'.join([line for line in lg_NE_string.split('\n') 
-                       if re.match(pattern, line)])
+    temp = "\n".join(
+        [line for line in lg_NE_string.split("\n") if re.match(pattern, line)]
+    )
     for source_label, mapped_label in Map.items():
         temp = temp.replace("," + source_label + ",", "," + mapped_label + ",")
     return temp
 
+
 if __name__ == "__main__":
     filename = sys.argv[1]
     get_mml(filename)
diff --git a/src/settings.py b/src/settings.py
index 7f064c5..8cabe51 100644
--- a/src/settings.py
+++ b/src/settings.py
@@ -1,8 +1,8 @@
 import os
 import os.path as osp
 
-LG_EVAL_DIR = os.environ.get('LgEvalDir')
-CROHMELIB_DIR = os.environ.get('CROHMELibDir')
+LG_EVAL_DIR = os.environ.get("LgEvalDir")
+CROHMELIB_DIR = os.environ.get("CROHMELibDir")
 
 LABELS_TRANS_CSV = osp.join(LG_EVAL_DIR, "translate", "infty_to_crohme.csv")
 MATHML_MAP_DIR = osp.join(LG_EVAL_DIR, "translate", "mathMLMap.csv")
diff --git a/src/smallGraph.py b/src/smallGraph.py
index 808b6a4..aaa3052 100644
--- a/src/smallGraph.py
+++ b/src/smallGraph.py
@@ -1,7 +1,7 @@
 ################################################################
 # smallGraph.py
 #
-# Simpler graph class for use with structure confusion 
+# Simpler graph class for use with structure confusion
 # histograms.
 #
 # Author: Harold Mouchere
@@ -22,261 +22,384 @@ from math import sqrt
 
 from lgeval.src.compareTools import cmpNodes, cmpEdges
 
+
 class SmallGraph(object):
-	"""Class for small graphs. The individual nodes
+    """Class for small graphs. The individual nodes
 	and edges have one associated label. 
 	Only for small graphes because algorithms are not optimized.
 	see igraph or graph_tool module for bigger graph"""
 
-	# Define graph data elements ('data members' for an object in the class)
-	__slots__ = ('nodes','edges', 'rednodes', 'rededges')
+    # Define graph data elements ('data members' for an object in the class)
+    __slots__ = ("nodes", "edges", "rednodes", "rededges")
 
-	##################################
-	# Constructors (in __init__)
-	##################################
-	def __init__(self,*args): 
-		""" init the small graph with 2 lists:
+    ##################################
+    # Constructors (in __init__)
+    ##################################
+    def __init__(self, *args):
+        """ init the small graph with 2 lists:
 			- list of nodes (id, label)
 			- list of edges (id,id, label)
 		"""
-		self.nodes = {}
-		self.edges = {}
-		self.rednodes = set()
-		self.rededges = set()
-		if(len(args) == 2 and isinstance(args[0],list) and isinstance(args[1],list)):
-			for (i,l) in args[0]:
-				self.nodes[i] = l
-			for (i1,i2,l) in args[1]:
-				self.edges[(i1,i2)] = l
-	
-	def printLG(self):
-		for k in list(self.nodes):
-			print ("N,"+str(k)+","+(",".join(self.nodes[k])) + ",1.0")
-		for (e1,e2) in list(self.edges):
-			print ("E,"+str(e1)+","+str(e2)+","+(",".join(self.edges[(e1,e2)])) + ",1.0")
-	
-	def __str__(self):
-		"""returns a string with nodes and edges. Format:
+        self.nodes = {}
+        self.edges = {}
+        self.rednodes = set()
+        self.rededges = set()
+        if len(args) == 2 and isinstance(args[0], list) and isinstance(args[1], list):
+            for (i, l) in args[0]:
+                self.nodes[i] = l
+            for (i1, i2, l) in args[1]:
+                self.edges[(i1, i2)] = l
+
+    def printLG(self):
+        for k in list(self.nodes):
+            print("N," + str(k) + "," + (",".join(self.nodes[k])) + ",1.0")
+        for (e1, e2) in list(self.edges):
+            print(
+                "E,"
+                + str(e1)
+                + ","
+                + str(e2)
+                + ","
+                + (",".join(self.edges[(e1, e2)]))
+                + ",1.0"
+            )
+
+    def __str__(self):
+        """returns a string with nodes and edges. Format:
 			nbNodes,id1,lab1,id2,lab2... ,nbedges,from1,to1,label1, from2, to2, label2,..."""
-		out = str(len(list(self.nodes)))
-		for k in list(self.nodes):
-			out = out + ","+str(k)+","+str(self.nodes[k])
-		out = out + ","+str(len(list(self.edges)))
-		for (e1,e2) in list(self.edges):
-			out = out + ","+str(e1)+","+str(e2)+","+str(self.edges[(e1,e2)])
-		return out
-		
-	def fromStr(self, inStr):
-		tab = inStr.split(',')
-		nnode = int(tab[0])
-		i = nnode*2+1
-		for n in range(1,i,2):
-			self.nodes[str(tab[n])] = str(tab[n+1])
-		nedg = int(tab[i])
-		for n in range(i+1, i+1+nedg*3,3 ):
-			a = str(tab[n])
-			b = str(tab[n+1])			
-			self.edges[(a,b)] = str(tab[n+2])
-	def iso(self,osg):
-		"""true if the two graphs are isomorphisms"""
-		if(len(list(self.nodes)) != len(list(osg.nodes))):# or \ # problem with '_' edges which exist but should be ignored
-			#len(list(self.edges)) != len(list(osg.edges))):
-				return False
-		myLabels = list(self.nodes.values()) + list(self.edges.values()) + ['_'] # add no edge label
-		hisLabels = list(osg.nodes.values()) + list(osg.edges.values()) + ['_'] # add no edge label
-		#myLabels.sort()
-		#hisLabels.sort()
-		#if(myLabels != hisLabels):
-		#print myLabels
-		myLabelsFlat = [item for sublist in myLabels for item in sublist]
-		hisLabelsFlat = [item for sublist in hisLabels for item in sublist]
-		if cmpNodes(myLabelsFlat, hisLabelsFlat) != (0,[]):
-			return False
-		#So they seems to be isomorphims (same label counts)
-		#let's try all permutation of nodes
-		for m in itertools.permutations(list(self.nodes)):
-			if self.equal(osg,m):
-				return True
-		return False
-
-	def equal(self,osg, mapping):
-		"""using the mapping list, check if the nodes and edges have the same labels
+        out = str(len(list(self.nodes)))
+        for k in list(self.nodes):
+            out = out + "," + str(k) + "," + str(self.nodes[k])
+        out = out + "," + str(len(list(self.edges)))
+        for (e1, e2) in list(self.edges):
+            out = out + "," + str(e1) + "," + str(e2) + "," + str(self.edges[(e1, e2)])
+        return out
+
+    def fromStr(self, inStr):
+        tab = inStr.split(",")
+        nnode = int(tab[0])
+        i = nnode * 2 + 1
+        for n in range(1, i, 2):
+            self.nodes[str(tab[n])] = str(tab[n + 1])
+        nedg = int(tab[i])
+        for n in range(i + 1, i + 1 + nedg * 3, 3):
+            a = str(tab[n])
+            b = str(tab[n + 1])
+            self.edges[(a, b)] = str(tab[n + 2])
+
+    def iso(self, osg):
+        """true if the two graphs are isomorphisms"""
+        if len(list(self.nodes)) != len(
+            list(osg.nodes)
+        ):  # or \ # problem with '_' edges which exist but should be ignored
+            # len(list(self.edges)) != len(list(osg.edges))):
+            return False
+        myLabels = (
+            list(self.nodes.values()) + list(self.edges.values()) + ["_"]
+        )  # add no edge label
+        hisLabels = (
+            list(osg.nodes.values()) + list(osg.edges.values()) + ["_"]
+        )  # add no edge label
+        # myLabels.sort()
+        # hisLabels.sort()
+        # if(myLabels != hisLabels):
+        # print myLabels
+        myLabelsFlat = [item for sublist in myLabels for item in sublist]
+        hisLabelsFlat = [item for sublist in hisLabels for item in sublist]
+        if cmpNodes(myLabelsFlat, hisLabelsFlat) != (0, []):
+            return False
+        # So they seems to be isomorphims (same label counts)
+        # let's try all permutation of nodes
+        for m in itertools.permutations(list(self.nodes)):
+            if self.equal(osg, m):
+                return True
+        return False
+
+    def equal(self, osg, mapping):
+        """using the mapping list, check if the nodes and edges have the same labels
 		The mapping is a list of self.nodes keys, the order give the mapping
 		the number of nodes have to be same"""
-		mynodes = list(self.nodes)
-		nb = len(mynodes)
-		onodes = list(osg.nodes)		
-		if(nb != len(onodes) and nb != len(mapping)):
-			return False
-		hisNode = dict(zip(mapping, onodes))
-		#print "Map : " + str(hisNode)
-		#first check the node labels
-		for (my,his) in hisNode.items():
-			#if(self.nodes[my] != osg.nodes[his]):
-			if(cmpNodes(self.nodes[my] ,osg.nodes[his]) != (0,[])):
-				#print str((self.nodes[my] ,osg.nodes[his])) + ' are diff'
-				return False
-		#then check the edges, from self to other and reverse
-		checkedEdg = set()
-		#from self to other
-		for (a,b) in self.edges.keys():
-			#id from the other through the mapping
-			(oa,ob) = (str(hisNode[a]),str(hisNode[b]))
-			checkedEdg.add((oa,ob))
-			#print str((a,b)) + " <=> " + str((oa,ob))
-			#if the edge does not exist or has a different label => missmatch
-			if not (oa,ob) in list(osg.edges):
-				if cmpEdges(self.edges[(a,b)], {'_' : 1.0})!= (0,[]):
-					#print str((oa,ob)) + " not in osg"
-					return False
-			else:
-				#if self.edges[(a,b)] != osg.edges[(oa,ob)]:
-				if cmpEdges(self.edges[(a,b)], osg.edges[(oa,ob)])!= (0,[]):
-					#print self.edges[(a,b)] + " != " + osg.edges[(oa,ob)]	
-					return False
-		#from other to self except checkedEdg, normaly, only '_' edges are remaining
-		for (oa,ob) in (set(osg.edges.keys()) - checkedEdg):
-			if cmpEdges(osg.edges[(oa,ob)], {'_' : 1.0})!= (0,[]):
-				return False
-			
-		return True
-
-	def __eq__(self,o):
-		return self.iso(o)
-	def toSVG(self, size = 200, withDef = True, nodeShape='circle'):
-		""" Generate a SVG XML string which draw the nodes (spread on a circle)
+        mynodes = list(self.nodes)
+        nb = len(mynodes)
+        onodes = list(osg.nodes)
+        if nb != len(onodes) and nb != len(mapping):
+            return False
+        hisNode = dict(zip(mapping, onodes))
+        # print "Map : " + str(hisNode)
+        # first check the node labels
+        for (my, his) in hisNode.items():
+            # if(self.nodes[my] != osg.nodes[his]):
+            if cmpNodes(self.nodes[my], osg.nodes[his]) != (0, []):
+                # print str((self.nodes[my] ,osg.nodes[his])) + ' are diff'
+                return False
+        # then check the edges, from self to other and reverse
+        checkedEdg = set()
+        # from self to other
+        for (a, b) in self.edges.keys():
+            # id from the other through the mapping
+            (oa, ob) = (str(hisNode[a]), str(hisNode[b]))
+            checkedEdg.add((oa, ob))
+            # print str((a,b)) + " <=> " + str((oa,ob))
+            # if the edge does not exist or has a different label => missmatch
+            if not (oa, ob) in list(osg.edges):
+                if cmpEdges(self.edges[(a, b)], {"_": 1.0}) != (0, []):
+                    # print str((oa,ob)) + " not in osg"
+                    return False
+            else:
+                # if self.edges[(a,b)] != osg.edges[(oa,ob)]:
+                if cmpEdges(self.edges[(a, b)], osg.edges[(oa, ob)]) != (0, []):
+                    # print self.edges[(a,b)] + " != " + osg.edges[(oa,ob)]
+                    return False
+        # from other to self except checkedEdg, normaly, only '_' edges are remaining
+        for (oa, ob) in set(osg.edges.keys()) - checkedEdg:
+            if cmpEdges(osg.edges[(oa, ob)], {"_": 1.0}) != (0, []):
+                return False
+
+        return True
+
+    def __eq__(self, o):
+        return self.iso(o)
+
+    def toSVG(self, size=200, withDef=True, nodeShape="circle"):
+        """ Generate a SVG XML string which draw the nodes (spread on a circle)
 		and edges with all label. 
 		Param size : the size of svg image (square) 
 		Param withDef : if True generate the definition of the arrow (needed only once in a HTML file)"""
 
-		svg = '<svg xmlns="http://www.w3.org/2000/svg" width="'+str(size)+'" height="'+str(size)+'">\n'
-		
-		n = len(self.nodes)
-		r = size / 10
-		R = (size - 2*r) /2
-		
-		if withDef:
-			svg = svg + '<defs><marker id="Triangle"\
+        svg = (
+            '<svg xmlns="http://www.w3.org/2000/svg" width="'
+            + str(size)
+            + '" height="'
+            + str(size)
+            + '">\n'
+        )
+
+        n = len(self.nodes)
+        r = size / 10
+        R = (size - 2 * r) / 2
+
+        if withDef:
+            svg = (
+                svg
+                + '<defs><marker id="Triangle"\
       			viewBox="0 0 10 10" refX="10" refY="5" \
       			markerUnits="strokeWidth"\
 				fill="lightgray" \
-      			stroke="black" markerWidth="'+str(r/2.75)+'" markerHeight="'+str(r/2.5)+'"\
+      			stroke="black" markerWidth="'
+                + str(r / 2.75)
+                + '" markerHeight="'
+                + str(r / 2.5)
+                + '"\
       			orient="auto">\
 				<path d="M 0 0 L 10 5 L 0 10 z" /> </marker>  </defs>'
-		
-		# Draw nodes on a circle.
-		xy = [  (cmath.rect(R,2 * x* cmath.pi/n).real + size/2,cmath.rect(R,2 * x* cmath.pi/n).imag + size/2) for x in range(n)]
-		i = 0
-		parentCount = {}
-		childCount = {}
-		findXY = {}
-
-		# Determine the number of times each node is a parent or child.
-		for (a,b) in list(self.edges):
-			if a in list(parentCount):
-				parentCount[a] += 1
-			else:
-				parentCount[a] = 1
-
-			if b in list(childCount):
-				childCount[b] += 1
-			else:
-				childCount[b] = 1
-
-		# Construct list of parent nodes (in order of parent role freq.),
-		# add any missing nodes.
-		childPairs = childCount.items()		
-		sortedPairs = sorted(childPairs, key=lambda tuple: tuple[1])
-		nodes = list(self.nodes)
-		if len(sortedPairs) > 0:
-			[nodes, counts] = zip(*sortedPairs)
-		nodeList = list(nodes)
-		for selfNode in list(self.nodes):
-			if not selfNode in nodeList:
-				nodeList.append(selfNode)
-
-		#for k in list(self.nodes):
-		for k in nodeList:
-			color = 'blue'
-			fillcolor= 'yellow'
-			if(k in self.rednodes):
-				color = 'red'
-				fillcolor='pink'
-
-			if nodeShape == 'circle':
-				svg = svg + '<circle cx="'+str(xy[i][0]) + '" cy="'+str(xy[i][1]) + '"r="'+str(r)+'" fill=' + fillcolor + ' stroke-width="2" stroke="'+color+'"/>\n'
-			else:
-				svg = svg + '<rect x="'+str(xy[i][0] - r) + '" y="'+str(xy[i][1] - r) + '"width="'+str(2*r)+'" height="' + str(2*r) + '" fill=' + fillcolor + ' stroke-width="2" stroke="'+color+'"/>\n'
-
-			#lab = ",".join(self.nodes[k])
-			lab = "".join(self.nodes[k])
-			svg = svg + '<text 	x="'+str(xy[i][0]-0.5*r) + '" y="'+str(xy[i][1]+r/2) + '"	font-family="Times"'+'font-size="'+str(1.5*r / sqrt(max([len(lab),1])))+'"'+'>' 
-			svg = svg + lab + '</text>\n'
-			findXY[k] = i
-			i = i +1
-
-		# Draw edges on a (smaller) circle
-		R = R - r 
-		xy = [  (cmath.rect(R,2 * x* cmath.pi/n).real + size/2,cmath.rect(R,2 * x* cmath.pi/n).imag + size/2) for x in range(n)]
-		for (a,b) in list(self.edges):
-			ai = findXY[a]			
-			bi = findXY[b]
-			color = 'blue'
-			swidth='1.5'
-			useMarker=True
-			if((a,b) in self.rededges):
-				color = 'red'
-				swidth='2'
-			if((a,b) in self.edges and (b,a) in self.edges):
-				useMarker=False
-			
-			# Avoid using errors for bi-directional edges (segment edges)
-			if useMarker:
-				svg = svg + '<line stroke-width=' + swidth + ' x1="'+str(xy[ai][0]) + '" y1="'+str(xy[ai][1]) + '" x2="'+str(xy[bi][0]) + '" y2="'+str(xy[bi][1]) + '" stroke="'+color+'" marker-end="url(#Triangle)" />\n'
-			else:
-				svg = svg + '<line stroke-width=' + swidth + ' x1="'+str(xy[ai][0]) + '" y1="'+str(xy[ai][1]) + '" x2="'+str(xy[bi][0]) + '" y2="'+str(xy[bi][1]) + '" stroke="'+color+'" stroke-dasharray="1,5" />\n'
-			
-			lab = ",".join(self.edges[(a,b)])
-			svg = svg + '<text x="'+str((xy[ai][0] + xy[bi][0] + 4)/2) + '" y="'+str((xy[ai][1]+ xy[bi][1])/2 - 4) + '"	font-family="Times"'+'font-size="'+str(1.5*r / sqrt(max([int(0.6 * len(lab)),1])))+'"'+'>' 
-			svg = svg + lab + '</text>\n'
-		
-		return svg + '</svg>\n'
-		
-		
+            )
+
+        # Draw nodes on a circle.
+        xy = [
+            (
+                cmath.rect(R, 2 * x * cmath.pi / n).real + size / 2,
+                cmath.rect(R, 2 * x * cmath.pi / n).imag + size / 2,
+            )
+            for x in range(n)
+        ]
+        i = 0
+        parentCount = {}
+        childCount = {}
+        findXY = {}
+
+        # Determine the number of times each node is a parent or child.
+        for (a, b) in list(self.edges):
+            if a in list(parentCount):
+                parentCount[a] += 1
+            else:
+                parentCount[a] = 1
+
+            if b in list(childCount):
+                childCount[b] += 1
+            else:
+                childCount[b] = 1
+
+        # Construct list of parent nodes (in order of parent role freq.),
+        # add any missing nodes.
+        childPairs = childCount.items()
+        sortedPairs = sorted(childPairs, key=lambda tuple: tuple[1])
+        nodes = list(self.nodes)
+        if len(sortedPairs) > 0:
+            [nodes, counts] = zip(*sortedPairs)
+        nodeList = list(nodes)
+        for selfNode in list(self.nodes):
+            if not selfNode in nodeList:
+                nodeList.append(selfNode)
+
+        # for k in list(self.nodes):
+        for k in nodeList:
+            color = "blue"
+            fillcolor = "yellow"
+            if k in self.rednodes:
+                color = "red"
+                fillcolor = "pink"
+
+            if nodeShape == "circle":
+                svg = (
+                    svg
+                    + '<circle cx="'
+                    + str(xy[i][0])
+                    + '" cy="'
+                    + str(xy[i][1])
+                    + '"r="'
+                    + str(r)
+                    + '" fill='
+                    + fillcolor
+                    + ' stroke-width="2" stroke="'
+                    + color
+                    + '"/>\n'
+                )
+            else:
+                svg = (
+                    svg
+                    + '<rect x="'
+                    + str(xy[i][0] - r)
+                    + '" y="'
+                    + str(xy[i][1] - r)
+                    + '"width="'
+                    + str(2 * r)
+                    + '" height="'
+                    + str(2 * r)
+                    + '" fill='
+                    + fillcolor
+                    + ' stroke-width="2" stroke="'
+                    + color
+                    + '"/>\n'
+                )
+
+            # lab = ",".join(self.nodes[k])
+            lab = "".join(self.nodes[k])
+            svg = (
+                svg
+                + '<text 	x="'
+                + str(xy[i][0] - 0.5 * r)
+                + '" y="'
+                + str(xy[i][1] + r / 2)
+                + '"	font-family="Times"'
+                + 'font-size="'
+                + str(1.5 * r / sqrt(max([len(lab), 1])))
+                + '"'
+                + ">"
+            )
+            svg = svg + lab + "</text>\n"
+            findXY[k] = i
+            i = i + 1
+
+        # Draw edges on a (smaller) circle
+        R = R - r
+        xy = [
+            (
+                cmath.rect(R, 2 * x * cmath.pi / n).real + size / 2,
+                cmath.rect(R, 2 * x * cmath.pi / n).imag + size / 2,
+            )
+            for x in range(n)
+        ]
+        for (a, b) in list(self.edges):
+            ai = findXY[a]
+            bi = findXY[b]
+            color = "blue"
+            swidth = "1.5"
+            useMarker = True
+            if (a, b) in self.rededges:
+                color = "red"
+                swidth = "2"
+            if (a, b) in self.edges and (b, a) in self.edges:
+                useMarker = False
+
+            # Avoid using errors for bi-directional edges (segment edges)
+            if useMarker:
+                svg = (
+                    svg
+                    + "<line stroke-width="
+                    + swidth
+                    + ' x1="'
+                    + str(xy[ai][0])
+                    + '" y1="'
+                    + str(xy[ai][1])
+                    + '" x2="'
+                    + str(xy[bi][0])
+                    + '" y2="'
+                    + str(xy[bi][1])
+                    + '" stroke="'
+                    + color
+                    + '" marker-end="url(#Triangle)" />\n'
+                )
+            else:
+                svg = (
+                    svg
+                    + "<line stroke-width="
+                    + swidth
+                    + ' x1="'
+                    + str(xy[ai][0])
+                    + '" y1="'
+                    + str(xy[ai][1])
+                    + '" x2="'
+                    + str(xy[bi][0])
+                    + '" y2="'
+                    + str(xy[bi][1])
+                    + '" stroke="'
+                    + color
+                    + '" stroke-dasharray="1,5" />\n'
+                )
+
+            lab = ",".join(self.edges[(a, b)])
+            svg = (
+                svg
+                + '<text x="'
+                + str((xy[ai][0] + xy[bi][0] + 4) / 2)
+                + '" y="'
+                + str((xy[ai][1] + xy[bi][1]) / 2 - 4)
+                + '"	font-family="Times"'
+                + 'font-size="'
+                + str(1.5 * r / sqrt(max([int(0.6 * len(lab)), 1])))
+                + '"'
+                + ">"
+            )
+            svg = svg + lab + "</text>\n"
+
+        return svg + "</svg>\n"
+
+
 def test():
-	sg=SmallGraph()
-	sg.nodes["1"] = "A"
-	sg.nodes["2"] = "B"
-	sg.nodes["3"] = "C"
-	sg.edges[("1","2")] = "R"
-	sg.edges[("1","3")] = "U"
-	sg.printLG()
-	line = str(sg)
-	print (line)
-	sg2 = SmallGraph()
-	sg2.fromStr(line)
-	sg2.printLG()
-	print ("Are they Iso (Y) : " + (str(sg == sg2)))
-	sg2.edges[('2','3')] = 'R'
-	print ("Add an edge (2,3,R) on right side ")
-	print ("Are they Iso (N) : " + (str(sg == sg2)))
-	sg.edges[('2','3')] = 'U'
-	print ("Add an edge (2,3,U) on left side ")
-	print ("Are they Iso (N) : " + (str(sg == sg2)))
-	print ("change edge (2,3) to R on left side ")
-	sg.edges[('2','3')] = 'R'	
-	print ("Are they Iso (Y) : " + (str(sg == sg2)))
-	print ("New graph : ")
-	sg2 = SmallGraph([("1","B"),("2","C"), ("3","A")], [("3", "1", "R"), ("3", "2", "U")])
-	sg2.printLG()
-	print ("Are they Iso (N) : " + (str(sg.iso(sg2))))
-	sg2.edges[('1','2')] = 'U'
-	print ("Add an edge (2,1,U) on right side ")
-	print ("Are they Iso (N) : " + (str(sg.iso(sg2))) + (str(sg2.iso(sg))))
-	sg2.edges[('1','2')] = 'R'
-	print ("Change edge (2,1)  to R on right side ")
-	print ("Are they Iso (Y) : " + (str(sg.iso(sg2)))+ (str(sg2.iso(sg))))
-	print (" SVG test : ")
-	sg.nodes["1"] = "Test"
-	print (sg.toSVG())
-				
+    sg = SmallGraph()
+    sg.nodes["1"] = "A"
+    sg.nodes["2"] = "B"
+    sg.nodes["3"] = "C"
+    sg.edges[("1", "2")] = "R"
+    sg.edges[("1", "3")] = "U"
+    sg.printLG()
+    line = str(sg)
+    print(line)
+    sg2 = SmallGraph()
+    sg2.fromStr(line)
+    sg2.printLG()
+    print("Are they Iso (Y) : " + (str(sg == sg2)))
+    sg2.edges[("2", "3")] = "R"
+    print("Add an edge (2,3,R) on right side ")
+    print("Are they Iso (N) : " + (str(sg == sg2)))
+    sg.edges[("2", "3")] = "U"
+    print("Add an edge (2,3,U) on left side ")
+    print("Are they Iso (N) : " + (str(sg == sg2)))
+    print("change edge (2,3) to R on left side ")
+    sg.edges[("2", "3")] = "R"
+    print("Are they Iso (Y) : " + (str(sg == sg2)))
+    print("New graph : ")
+    sg2 = SmallGraph(
+        [("1", "B"), ("2", "C"), ("3", "A")], [("3", "1", "R"), ("3", "2", "U")]
+    )
+    sg2.printLG()
+    print("Are they Iso (N) : " + (str(sg.iso(sg2))))
+    sg2.edges[("1", "2")] = "U"
+    print("Add an edge (2,1,U) on right side ")
+    print("Are they Iso (N) : " + (str(sg.iso(sg2))) + (str(sg2.iso(sg))))
+    sg2.edges[("1", "2")] = "R"
+    print("Change edge (2,1)  to R on right side ")
+    print("Are they Iso (Y) : " + (str(sg.iso(sg2))) + (str(sg2.iso(sg))))
+    print(" SVG test : ")
+    sg.nodes["1"] = "Test"
+    print(sg.toSVG())
diff --git a/src/statlgdb.py b/src/statlgdb.py
index 504daf7..ee4e030 100644
--- a/src/statlgdb.py
+++ b/src/statlgdb.py
@@ -16,75 +16,82 @@ from lgeval.src.lg import *
 from lgeval.src.lgio import *
 import lgeval.src.SmGrConfMatrix as SmGrConfMatrix
 
-INKMLVIEWER="file://www.cs.rit.edu/~rlaz/inkml_viewer/index.xhtml?path=http://www.cs.rit.edu/~rlaz/testdata/&files="
+INKMLVIEWER = "file://www.cs.rit.edu/~rlaz/inkml_viewer/index.xhtml?path=http://www.cs.rit.edu/~rlaz/testdata/&files="
 MINERRTOSHOW = 1
 
+
 def getObjStruct(strkLG):
-	(sp, _, _, sre) = strkLG.segmentGraph()
-	lgObj = Lg()
-	for (sid,lprim) in sp.iteritems():
-		lgObj.nlabels[sid] = strkLG.nlabels[list(lprim[0])[0]]
-
-	for thisPair in list(sre):
-		# TODO : check if it is sp1[thisPair[0]] instead of sp1[thisPair[0]][0]
-		thisParentIds = set(sp[ thisPair[0] ][0])
-		thisChildIds = set(sp[thisPair[1] ][0])
-		lgObj.elabels[thisPair] = strkLG.elabels[ (list(thisParentIds)[0], list(thisChildIds)[0])]
-	return lgObj	
+    (sp, _, _, sre) = strkLG.segmentGraph()
+    lgObj = Lg()
+    for (sid, lprim) in sp.iteritems():
+        lgObj.nlabels[sid] = strkLG.nlabels[list(lprim[0])[0]]
+
+    for thisPair in list(sre):
+        # TODO : check if it is sp1[thisPair[0]] instead of sp1[thisPair[0]][0]
+        thisParentIds = set(sp[thisPair[0]][0])
+        thisChildIds = set(sp[thisPair[1]][0])
+        lgObj.elabels[thisPair] = strkLG.elabels[
+            (list(thisParentIds)[0], list(thisChildIds)[0])
+        ]
+    return lgObj
+
 
 def main():
-	stat = SmGrConfMatrix.SmDict()
-	statObj = SmGrConfMatrix.SmDict()
-	listName = sys.argv[1]
-
-	processStrokes = False;
-	if len(sys.argv) > 2:
-		processStrokes = True;
-		print("Computing stroke and object confusion histograms.")
-
-	print ("Computing histograms for files in: " + str(listName))
-	fileReader = csv.reader(open(listName), delimiter=' ')
-	for row in fileReader:
-		# Skip comments and empty lines.
-		if not row == [] and not row[0].strip()[0] == "#":
-			lgfile = row[0].strip() # remove leading/trailing whitespace
-			print (lgfile)
-			lg = Lg(lgfile)
-
-			# NOTE: list of integers is object neighborhood sizes
-			for s in getObjStruct(lg).subStructIterator([2,3]):
-				#print s
-				statObj.get(s,SmGrConfMatrix.Counter).incr()
-
-			#NOTE: List of integers are stroke neighborhood sizes
-			# Only process strokes if explicitly asked to do so.
-			if processStrokes:
-				for s in lg.subStructIterator([2,3]):
-					stat.get(s,SmGrConfMatrix.Counter).incr()
-			
-	out=open('CH_' + listName + '.html', 'w')
-	out.write('<html xmlns="http://www.w3.org/1999/xhtml">')
-	
-	# Style
-	out.write('<head><style type="text/css">\n')
-	out.write('</style></head>\n\n')
-	out.write("<font face=\"helvetica,arial,sans-serif\">")
-	
-	out.write("<h1>LgEval Structure Confusion Histograms</h1>")
-	out.write("\n<b>" + str(listName) + "</b><br>")
-	out.write(time.strftime("%c"))
-	out.write('')
-	out.write('<UL><LI><A HREF="#Object">Object confusion histograms</A></LI><LI><A HREF=\"#Stroke\"></UL>')
-	
-	out.write('<h2><A NAME=\"#Object\">Object Subgraph Confusion Histograms</A></h2>')
-	out.write(statObj.toHTML(MINERRTOSHOW))
-	
-	if (processStrokes):
-		out.write('<h2><A NAME=\"#Stroke\">Stroke Subgraph Confusion Histograms</A></h2>')
-		out.write(stat.toHTML(MINERRTOSHOW))
-	
-	out.write('</font>')
-	out.write('</html>')
-	out.close()
-	
+    stat = SmGrConfMatrix.SmDict()
+    statObj = SmGrConfMatrix.SmDict()
+    listName = sys.argv[1]
+
+    processStrokes = False
+    if len(sys.argv) > 2:
+        processStrokes = True
+        print("Computing stroke and object confusion histograms.")
+
+    print("Computing histograms for files in: " + str(listName))
+    fileReader = csv.reader(open(listName), delimiter=" ")
+    for row in fileReader:
+        # Skip comments and empty lines.
+        if not row == [] and not row[0].strip()[0] == "#":
+            lgfile = row[0].strip()  # remove leading/trailing whitespace
+            print(lgfile)
+            lg = Lg(lgfile)
+
+            # NOTE: list of integers is object neighborhood sizes
+            for s in getObjStruct(lg).subStructIterator([2, 3]):
+                # print s
+                statObj.get(s, SmGrConfMatrix.Counter).incr()
+
+            # NOTE: List of integers are stroke neighborhood sizes
+            # Only process strokes if explicitly asked to do so.
+            if processStrokes:
+                for s in lg.subStructIterator([2, 3]):
+                    stat.get(s, SmGrConfMatrix.Counter).incr()
+
+    out = open("CH_" + listName + ".html", "w")
+    out.write('<html xmlns="http://www.w3.org/1999/xhtml">')
+
+    # Style
+    out.write('<head><style type="text/css">\n')
+    out.write("</style></head>\n\n")
+    out.write('<font face="helvetica,arial,sans-serif">')
+
+    out.write("<h1>LgEval Structure Confusion Histograms</h1>")
+    out.write("\n<b>" + str(listName) + "</b><br>")
+    out.write(time.strftime("%c"))
+    out.write("")
+    out.write(
+        '<UL><LI><A HREF="#Object">Object confusion histograms</A></LI><LI><A HREF="#Stroke"></UL>'
+    )
+
+    out.write('<h2><A NAME="#Object">Object Subgraph Confusion Histograms</A></h2>')
+    out.write(statObj.toHTML(MINERRTOSHOW))
+
+    if processStrokes:
+        out.write('<h2><A NAME="#Stroke">Stroke Subgraph Confusion Histograms</A></h2>')
+        out.write(stat.toHTML(MINERRTOSHOW))
+
+    out.write("</font>")
+    out.write("</html>")
+    out.close()
+
+
 main()
diff --git a/src/sumDiff.py b/src/sumDiff.py
index cf1ea15..e214dd7 100644
--- a/src/sumDiff.py
+++ b/src/sumDiff.py
@@ -4,7 +4,7 @@
 # Program that reads in a .diff (CSV) file containing differences between
 # to BG, and outputs confusion matrix for symbols and spatial relations.
 # Output is in CSV or HTML formats.
-# 
+#
 # Author: H. Mouchere, June 2012
 # Copyright (c) 2012-2014, Richard Zanibbi and Harold Mouchere
 ################################################################
@@ -14,248 +14,306 @@ import collections
 import time
 import os
 
+
 def addOneError(confM, id1, id2):
-        #thanks to "defaultdict" there is nothing to do !
-        confM[id1][id2] += 1
+    # thanks to "defaultdict" there is nothing to do !
+    confM[id1][id2] += 1
+
 
 def affMat(output, allID, confM):
-        # Header
-        output.write("Output:")
-        for k in sorted(allID):
-               output.write(",'"+str(k)+"'")
+    # Header
+    output.write("Output:")
+    for k in sorted(allID):
+        output.write(",'" + str(k) + "'")
+    output.write("\n")
+
+    # Data
+    for k1 in sorted(allID):
+        output.write("'" + str(k1) + "'")
+        for k2 in sorted(allID):
+            if not confM[k1][k2] == 0:
+                output.write("," + str(confM[k1][k2]))
+            else:
+                output.write(",")
         output.write("\n")
-        
-        # Data
-        for k1 in sorted(allID):
-                output.write("'"+str(k1)+"'")
-                for k2 in sorted(allID):
-                        if not confM[k1][k2] == 0:
-                                output.write(","+str(confM[k1][k2]))
-                        else:
-                                output.write(",")
-                output.write("\n")
+
 
 def affMatHTML(output, allID, confM):
-        output.write("<table>\n<tr><th><i>(Out:Rows)</i></th>")
-        for k in sorted(allID):
-                output.write("<th>"+str(k)+"</th>")
-        output.write("</tr>\n")
-        for k1 in sorted(allID):
-                output.write("<tr><th>"+str(k1)+"</th>")
-                i = 0
-                for k2 in sorted(allID):
-                        val = str(confM[k1][k2])
-                        if val == "0":
-                                val = ""
-                        output.write('<td class="col_'+str(i)+'">'+val+"</td>")
-                        i = i+1
-                output.write("<th>"+str(k1)+"</th></tr>\n")
-        output.write("<tr><th></th>")
-        for k in sorted(allID):
-                output.write("<th>"+str(k)+"</th>")
-        output.write("</tr>\n")                
-        output.write("</table>\n")
+    output.write("<table>\n<tr><th><i>(Out:Rows)</i></th>")
+    for k in sorted(allID):
+        output.write("<th>" + str(k) + "</th>")
+    output.write("</tr>\n")
+    for k1 in sorted(allID):
+        output.write("<tr><th>" + str(k1) + "</th>")
+        i = 0
+        for k2 in sorted(allID):
+            val = str(confM[k1][k2])
+            if val == "0":
+                val = ""
+            output.write('<td class="col_' + str(i) + '">' + val + "</td>")
+            i = i + 1
+        output.write("<th>" + str(k1) + "</th></tr>\n")
+    output.write("<tr><th></th>")
+    for k in sorted(allID):
+        output.write("<th>" + str(k) + "</th>")
+    output.write("</tr>\n")
+    output.write("</table>\n")
+
 
 def writeCSS(output, allID):
-        output.write('<head><style type="text/css">\n')
-        output.write('table { border-collapse:collapse;}\n')
-        output.write('p { line-height: 125%;}\n')
-        output.write('ul { line-height: 125%;}\n')
-        output.write('th{ text-align: right; padding: 4px;}\n')
-        output.write('td { text-align: right; border: 1px solid lightgray; padding: 4px; }\n')
-        
-        #output.write('h2 {        color: red;}\n')
-        output.write('tr:hover{background-color:rgb(180,200,235);}\n ')
-        #i = 0
-        #for k1 in sorted(allID):
-        #        output.write('td.col_'+str(i)+':hover {\nbackground-color:rgb(100,100,255);\n}\n')
-        #        i = i+1
-        output.write('td:hover{background-color:yellow;} \n')
-        output.write('</style></head>\n')
+    output.write('<head><style type="text/css">\n')
+    output.write("table { border-collapse:collapse;}\n")
+    output.write("p { line-height: 125%;}\n")
+    output.write("ul { line-height: 125%;}\n")
+    output.write("th{ text-align: right; padding: 4px;}\n")
+    output.write(
+        "td { text-align: right; border: 1px solid lightgray; padding: 4px; }\n"
+    )
+
+    # output.write('h2 {        color: red;}\n')
+    output.write("tr:hover{background-color:rgb(180,200,235);}\n ")
+    # i = 0
+    # for k1 in sorted(allID):
+    #        output.write('td.col_'+str(i)+':hover {\nbackground-color:rgb(100,100,255);\n}\n')
+    #        i = i+1
+    output.write("td:hover{background-color:yellow;} \n")
+    output.write("</style></head>\n")
+
 
 def main():
-        if len(sys.argv) < 3:
-                print("Usage : [[python]] sumDiff.py <file1.diff> <labelsGT.txt> [HTML]\n")
-                print("        Merge results for each line in file1.diff into confusion Matrices.")
-                print("        By default output is sent to stdout in CSV format.")
-                print(" requires list of GT labels from labelsGT.txt.")
-                print("        [HTML] option changes output format to HTML.")
-                sys.exit(0)
-        # Read data from CSV file.
-        fileName = sys.argv[1]
-        labelFile = sys.argv[2]
-        try:
-                fileReader = csv.reader(open(fileName))
-        except:
-                sys.stderr.write('  !! IO Error (cannot open): ' + fileName)
-                sys.exit(1)
-
-        try:
-                labelfileReader = csv.reader(open(labelFile))
-        except:
-                sys.stderr.write('  !! IO Error (cannot open): ' + fileName)
-                sys.exit(1)
-
-        # Read for node and edge label sets.
-        readEdges = False
-        gtNodeLabels = set()
-        gtEdgeLabels = set()
-        for row in labelfileReader:
-                if len(row) == 0:
-                        continue
-                
-                nextEntry = row[0].strip()
-                if nextEntry == 'NODE LABELS:':
-                        continue
-                elif nextEntry == 'EDGE LABELS:':
-                        readEdges = True
-                else:
-                        if readEdges:
-                                gtEdgeLabels.add(nextEntry)
-                        else:
-                                gtNodeLabels.add(nextEntry)
-
-        withHTML = False
-        if len(sys.argv) > 3:
-                withHTML = True
-        #confusion matrix = dict->dict->int
-        labelM = collections.defaultdict(collections.defaultdict(int).copy)
-        spatRelM = collections.defaultdict(collections.defaultdict(int).copy)
-        #segRelM = collections.defaultdict(collections.defaultdict(int).copy)
-        
-        allLabel = set()
-        allSR = set()
-        rowCount = -1
-
-        # Idenfity all confused symbol labels. We will use this to
-        # present relationship and segmentation confusions separately.
-        symbolLabels = set([])
-
-        nodeErrors = 0
-        allSegErrors = 0
-        allRelErrors = 0
-        fposMerge = 0
-        fnegMerge = 0
-
-        for row in fileReader:
-                rowCount += 1
-
-                # Skip blank lines.
-                if len(row) == 0:
-                        continue
-
-                entryType = row[0].strip()
-                #skip file names
-                if entryType == "DIFF":
-                        continue
-                #process node label errors
-                elif entryType == "*N":
-                        # Capture all confused symbol (node) labels.
-                        symbolLabels.add(row[2].strip())
-                        symbolLabels.add(row[5].strip())
-
-                        addOneError(labelM,row[2].strip(),row[5].strip())
-                        allLabel.add(row[2].strip())
-                        allLabel.add(row[5].strip())
-
-                        nodeErrors += 1
-
-                #process link errors
-                elif entryType == "*E":
-                        # DEBUG
-                        if row[3].strip() == "1.0" or row[6].strip() == "1.0":
-                                print("ERROR at row: " + str(rowCount) + " for file: " + fileName)
-                                print(row)
-                        elif not len(row) == 8:
-                                print("INVALID LENGTH at row: " + str(rowCount) + " for file: " + fileName)
-                                print(row)
-                        
-                        outputLabel = row[3].strip()
-                        otherLabel = row[6].strip()
-                        addOneError(spatRelM, outputLabel, otherLabel)
-
-                        allSR.add(outputLabel)
-                        allSR.add(otherLabel)
-
-                elif entryType == "*S":
-                        # Currently ignore segmentation errors (i.e. object-level errors)
-                        continue
-                
-        # Obtain the list of edge labels that do not appear on nodes.
-        # DEBUG: need to consult all GT labels in general case (handling '*' input).
-        mergeEdgeLabel = '*'
-        relOnlyLabels = allSR.difference(symbolLabels).difference(gtNodeLabels)
-        relMergeLabels = relOnlyLabels.union(mergeEdgeLabel)
-
-        # Create a modified confusion histogram where all symbol/segmentation
-        # edge confusions are treated as being of the same type.
-        ShortEdgeMatrix = collections.defaultdict(collections.defaultdict(int).copy)
-        for output in list(spatRelM):
-                olabel = output
-                if not output in relOnlyLabels:
-                        olabel = mergeEdgeLabel
-
-                for target in list(spatRelM[output]):
-                        tlabel = target
-                        if not target in relOnlyLabels:
-                                tlabel = mergeEdgeLabel
-
-                        # Increment the entry for the appropriate matrix.
-                        ShortEdgeMatrix[olabel][tlabel] += spatRelM[output][target]
-
-                        if not olabel == output or not tlabel == target:
-                                allSegErrors += spatRelM[output][target]
-                                if not olabel == output and tlabel == target:
-                                        fposMerge += spatRelM[output][target]
-                                elif not tlabel == target and olabel == output:
-                                        fnegMerge += spatRelM[output][target]
-                        else:
-                                allRelErrors += spatRelM[output][target]
-
-        if withHTML:
-                sys.stdout.write('<html>')
-                writeCSS(sys.stdout, allLabel.union(allSR))
-                print("<font face=\"helvetica,arial,sans-serif\">")
-                print("<h2>LgEval Error Summary</h2>")
-                print(time.strftime("%c"))
-                print("<br>\n")
-                print("<b>File:</b> " + os.path.splitext( os.path.split(fileName)[1] )[0] + "<br>")
-                print("<p>All confusion matrices show only errors. In each matrix, output labels appear in the left column, and target labels in the top row.</p>")
-                print("<UL><LI><A href=\"#nodes\">Node Label Confusion Matrix</A> <LI> <A HREF=\"#ShortEdges\">Edge Label Confusion Matrix (short - ignoring object class confusions)<A> <LI> <A HREF=\"#Edges\">Edge Label Matrix (all labels)</A> </UL>")
-                print ("<hr>")
-                print ("<h2><A NAME=\"nodes\">Node Label Confusion Matrix</A></h2>")
-                print ("<p>"+str(len(allLabel)) + " unique node labels. " + str(nodeErrors) + " errors. ABSENT: a node missing in the output or target graph</p>")
-                affMatHTML(sys.stdout, allLabel, labelM)
-                print("<br><hr><br>")
-                print ("<h2><A NAME=\"ShortEdges\">Edge Label Confusion Matrix (Short)</A></h2>")
-                print ("<p>" + str(len(relOnlyLabels)) + " unique relationship labels + * representing grouping two nodes into an object (any type). " + str(allSegErrors + allRelErrors) + " errors <UL><LI>" + str(allSegErrors) + " Directed segmentation and node pair classification errors (entries in '*'-labeled row and column) <UL><LI><b>" + str(allSegErrors - fposMerge - fnegMerge) + " edges between correctly grouped nodes, but with conflicting classification (* vs. *)</b> <LI>" + str(fposMerge) + " false positive merge edges (* vs. other)<LI>" + str(fnegMerge) + " false negative merge edges (other vs. *) </UL>  <LI>" + str(allRelErrors) + " Directed relationship errors (remaining matrix entries) </UL></p></p>")
-                affMatHTML(sys.stdout, relMergeLabels, ShortEdgeMatrix)
-                #affMatHTML(sys.stdout, relOnlyLabels, spatRelM)
-                
-                print("<br><hr><br>")
-                print("<h2><A NAME=\"Edges\">Edge Label Confusion Matrix (All Errors)</A></h2>")
-                print("<p>"+str(len(allSR)) + " unique edge labels representing relationships and node groupings for specific symbol types. " + str(allSegErrors + allRelErrors) + " errors</p>")
-                affMatHTML(sys.stdout, allSR, spatRelM)
-                
-                print("</font>")
-                sys.stdout.write('</html>')
+    if len(sys.argv) < 3:
+        print("Usage : [[python]] sumDiff.py <file1.diff> <labelsGT.txt> [HTML]\n")
+        print(
+            "        Merge results for each line in file1.diff into confusion Matrices."
+        )
+        print("        By default output is sent to stdout in CSV format.")
+        print(" requires list of GT labels from labelsGT.txt.")
+        print("        [HTML] option changes output format to HTML.")
+        sys.exit(0)
+    # Read data from CSV file.
+    fileName = sys.argv[1]
+    labelFile = sys.argv[2]
+    try:
+        fileReader = csv.reader(open(fileName))
+    except:
+        sys.stderr.write("  !! IO Error (cannot open): " + fileName)
+        sys.exit(1)
+
+    try:
+        labelfileReader = csv.reader(open(labelFile))
+    except:
+        sys.stderr.write("  !! IO Error (cannot open): " + fileName)
+        sys.exit(1)
+
+    # Read for node and edge label sets.
+    readEdges = False
+    gtNodeLabels = set()
+    gtEdgeLabels = set()
+    for row in labelfileReader:
+        if len(row) == 0:
+            continue
+
+        nextEntry = row[0].strip()
+        if nextEntry == "NODE LABELS:":
+            continue
+        elif nextEntry == "EDGE LABELS:":
+            readEdges = True
         else:
-                print("LgEval Error Summary for: "+fileName)
-                print(time.strftime("%c"))
-                print("")
-                print("NOTE: This file contains 3 confusion matrices.")
-                print("")
-                print("I. Node Label Confusion Matrix: " + str(len(allLabel)) + " unique labels. ABSENT: a node missing in the output or target graph")
-                affMat(sys.stdout, allLabel, labelM)
-                
-                print("")
-                print("")
-                print("II. Edge Label Confusion Matrix (Short): " + str(len(relOnlyLabels)) + " unique relationship labels + * (merge)")
-                affMat(sys.stdout, relMergeLabels, ShortEdgeMatrix)
-                
-                print("")
-                print("")
-                print("III. Edge Label Confusion Matrix (Full): " + str(len(allSR)) + " unique labels for relationships and node groupings for specific symbol types")
-                affMat(sys.stdout, allSR, spatRelM)
+            if readEdges:
+                gtEdgeLabels.add(nextEntry)
+            else:
+                gtNodeLabels.add(nextEntry)
+
+    withHTML = False
+    if len(sys.argv) > 3:
+        withHTML = True
+    # confusion matrix = dict->dict->int
+    labelM = collections.defaultdict(collections.defaultdict(int).copy)
+    spatRelM = collections.defaultdict(collections.defaultdict(int).copy)
+    # segRelM = collections.defaultdict(collections.defaultdict(int).copy)
+
+    allLabel = set()
+    allSR = set()
+    rowCount = -1
+
+    # Idenfity all confused symbol labels. We will use this to
+    # present relationship and segmentation confusions separately.
+    symbolLabels = set([])
+
+    nodeErrors = 0
+    allSegErrors = 0
+    allRelErrors = 0
+    fposMerge = 0
+    fnegMerge = 0
+
+    for row in fileReader:
+        rowCount += 1
+
+        # Skip blank lines.
+        if len(row) == 0:
+            continue
+
+        entryType = row[0].strip()
+        # skip file names
+        if entryType == "DIFF":
+            continue
+        # process node label errors
+        elif entryType == "*N":
+            # Capture all confused symbol (node) labels.
+            symbolLabels.add(row[2].strip())
+            symbolLabels.add(row[5].strip())
+
+            addOneError(labelM, row[2].strip(), row[5].strip())
+            allLabel.add(row[2].strip())
+            allLabel.add(row[5].strip())
+
+            nodeErrors += 1
+
+        # process link errors
+        elif entryType == "*E":
+            # DEBUG
+            if row[3].strip() == "1.0" or row[6].strip() == "1.0":
+                print("ERROR at row: " + str(rowCount) + " for file: " + fileName)
+                print(row)
+            elif not len(row) == 8:
+                print(
+                    "INVALID LENGTH at row: " + str(rowCount) + " for file: " + fileName
+                )
+                print(row)
+
+            outputLabel = row[3].strip()
+            otherLabel = row[6].strip()
+            addOneError(spatRelM, outputLabel, otherLabel)
+
+            allSR.add(outputLabel)
+            allSR.add(otherLabel)
+
+        elif entryType == "*S":
+            # Currently ignore segmentation errors (i.e. object-level errors)
+            continue
+
+    # Obtain the list of edge labels that do not appear on nodes.
+    # DEBUG: need to consult all GT labels in general case (handling '*' input).
+    mergeEdgeLabel = "*"
+    relOnlyLabels = allSR.difference(symbolLabels).difference(gtNodeLabels)
+    relMergeLabels = relOnlyLabels.union(mergeEdgeLabel)
+
+    # Create a modified confusion histogram where all symbol/segmentation
+    # edge confusions are treated as being of the same type.
+    ShortEdgeMatrix = collections.defaultdict(collections.defaultdict(int).copy)
+    for output in list(spatRelM):
+        olabel = output
+        if not output in relOnlyLabels:
+            olabel = mergeEdgeLabel
+
+        for target in list(spatRelM[output]):
+            tlabel = target
+            if not target in relOnlyLabels:
+                tlabel = mergeEdgeLabel
+
+            # Increment the entry for the appropriate matrix.
+            ShortEdgeMatrix[olabel][tlabel] += spatRelM[output][target]
+
+            if not olabel == output or not tlabel == target:
+                allSegErrors += spatRelM[output][target]
+                if not olabel == output and tlabel == target:
+                    fposMerge += spatRelM[output][target]
+                elif not tlabel == target and olabel == output:
+                    fnegMerge += spatRelM[output][target]
+            else:
+                allRelErrors += spatRelM[output][target]
+
+    if withHTML:
+        sys.stdout.write("<html>")
+        writeCSS(sys.stdout, allLabel.union(allSR))
+        print('<font face="helvetica,arial,sans-serif">')
+        print("<h2>LgEval Error Summary</h2>")
+        print(time.strftime("%c"))
+        print("<br>\n")
+        print(
+            "<b>File:</b> " + os.path.splitext(os.path.split(fileName)[1])[0] + "<br>"
+        )
+        print(
+            "<p>All confusion matrices show only errors. In each matrix, output labels appear in the left column, and target labels in the top row.</p>"
+        )
+        print(
+            '<UL><LI><A href="#nodes">Node Label Confusion Matrix</A> <LI> <A HREF="#ShortEdges">Edge Label Confusion Matrix (short - ignoring object class confusions)<A> <LI> <A HREF="#Edges">Edge Label Matrix (all labels)</A> </UL>'
+        )
+        print("<hr>")
+        print('<h2><A NAME="nodes">Node Label Confusion Matrix</A></h2>')
+        print(
+            "<p>"
+            + str(len(allLabel))
+            + " unique node labels. "
+            + str(nodeErrors)
+            + " errors. ABSENT: a node missing in the output or target graph</p>"
+        )
+        affMatHTML(sys.stdout, allLabel, labelM)
+        print("<br><hr><br>")
+        print('<h2><A NAME="ShortEdges">Edge Label Confusion Matrix (Short)</A></h2>')
+        print(
+            "<p>"
+            + str(len(relOnlyLabels))
+            + " unique relationship labels + * representing grouping two nodes into an object (any type). "
+            + str(allSegErrors + allRelErrors)
+            + " errors <UL><LI>"
+            + str(allSegErrors)
+            + " Directed segmentation and node pair classification errors (entries in '*'-labeled row and column) <UL><LI><b>"
+            + str(allSegErrors - fposMerge - fnegMerge)
+            + " edges between correctly grouped nodes, but with conflicting classification (* vs. *)</b> <LI>"
+            + str(fposMerge)
+            + " false positive merge edges (* vs. other)<LI>"
+            + str(fnegMerge)
+            + " false negative merge edges (other vs. *) </UL>  <LI>"
+            + str(allRelErrors)
+            + " Directed relationship errors (remaining matrix entries) </UL></p></p>"
+        )
+        affMatHTML(sys.stdout, relMergeLabels, ShortEdgeMatrix)
+        # affMatHTML(sys.stdout, relOnlyLabels, spatRelM)
+
+        print("<br><hr><br>")
+        print('<h2><A NAME="Edges">Edge Label Confusion Matrix (All Errors)</A></h2>')
+        print(
+            "<p>"
+            + str(len(allSR))
+            + " unique edge labels representing relationships and node groupings for specific symbol types. "
+            + str(allSegErrors + allRelErrors)
+            + " errors</p>"
+        )
+        affMatHTML(sys.stdout, allSR, spatRelM)
+
+        print("</font>")
+        sys.stdout.write("</html>")
+    else:
+        print("LgEval Error Summary for: " + fileName)
+        print(time.strftime("%c"))
+        print("")
+        print("NOTE: This file contains 3 confusion matrices.")
+        print("")
+        print(
+            "I. Node Label Confusion Matrix: "
+            + str(len(allLabel))
+            + " unique labels. ABSENT: a node missing in the output or target graph"
+        )
+        affMat(sys.stdout, allLabel, labelM)
+
+        print("")
+        print("")
+        print(
+            "II. Edge Label Confusion Matrix (Short): "
+            + str(len(relOnlyLabels))
+            + " unique relationship labels + * (merge)"
+        )
+        affMat(sys.stdout, relMergeLabels, ShortEdgeMatrix)
+
+        print("")
+        print("")
+        print(
+            "III. Edge Label Confusion Matrix (Full): "
+            + str(len(allSR))
+            + " unique labels for relationships and node groupings for specific symbol types"
+        )
+        affMat(sys.stdout, allSR, spatRelM)
+
 
 main()
diff --git a/src/sumMetric.py b/src/sumMetric.py
index ef49b10..323a66d 100644
--- a/src/sumMetric.py
+++ b/src/sumMetric.py
@@ -3,7 +3,7 @@
 #
 # Program that reads in a .m (CSV) file containing metrics,
 # and outputs summary statistics and global performance metrics.
-# 
+#
 # Author: H. Mouchere and R. Zanibbi, June 2012
 # Copyright (c) 2012-2014, Richard Zanibbi and Harold Mouchere
 ################################################################
@@ -13,418 +13,651 @@ import math
 import time
 import os
 
-def fmeasure(R,P):
-        """Harmonic mean of recall and precision."""
-        value = 0
-        if R > 0 or P > 0:
-                value = (2 * R * P)/(R+P)
-        return value
-
-def meanStdDev( valueList, scale ):
-        """Compute the mean and standard deviation of a *non-empty* list of numbers."""
-        numElements = len(valueList)
-        if numElements == 0:
-                return(None, 0.0)
-        
-        mean = float(sum(valueList)) / numElements
-        variance = 0
-        for value in valueList:
-                variance += math.pow( value - mean, 2 )
-        variance = variance / float(numElements)
-        return (scale * mean, scale * math.sqrt(variance))
-        
-def weightedMeanStdDev( valueList, weights, scale ):
-        """Compute the weighted mean and standard deviation of a *non-empty* list of numbers."""
-
-        numElements = sum(weights)
-        if len(valueList) < 1 or numElements == 0:
-                return(None, 0.0)
-
-        mean = 0
-        for i in range(len(valueList)):
-                mean += float(valueList[i]*weights[i])
-        mean = mean / numElements
-        variance = 0
-        for  i in range(len(valueList)):
-                variance += math.pow( valueList[i] - mean, 2 )*weights[i]
-        variance = variance / float(numElements)
-        
-        return (scale * mean, scale * math.sqrt(variance))
-
-def reportMeanStdDev(formatWidth, label, valueList, scale ):
-        (m,s) = meanStdDev(valueList,scale)
-        printTable(formatWidth,[label,m,s])
-
-def reportWMeanStdDev(formatWidth, label, valueList, weights, scale ):
-        (m,s) =  weightedMeanStdDev(valueList,weights,scale)
-        printTable(formatWidth,[label,m,s])
-        #print(label + " (mean, stdev) = " + str(weightedMeanStdDev(valueList,weights,scale)))
-        
-def reportCoupleCSV(sep,c ):
-        (mean,stdev) = c
-        sys.stdout.write(sep + str(mean) + "," + str(stdev))
-
-def intMetric( dictionary, label ):
-        return str(int(dictionary[label]))
-
-def floatMetric( dictionary, label ):
-        return str(dictionary[label])
-
-def printTable( field_width, entries):
-        """Makes it easier to format output for evaluation metrics."""
-        cols = len(entries) 
-        labelFormat = ''
-        for i in range(0,cols):
-                extra = ''
-                if type(entries[i]) is float:
-                        labelFormat += '{0[' + str(i) + ']:>{width}.2f' + '}'
-                else:
-                        labelFormat += '{0[' + str(i) + ']:>{width}}'
-        print (labelFormat.format( entries, width=field_width))
+
+def fmeasure(R, P):
+    """Harmonic mean of recall and precision."""
+    value = 0
+    if R > 0 or P > 0:
+        value = (2 * R * P) / (R + P)
+    return value
+
+
+def meanStdDev(valueList, scale):
+    """Compute the mean and standard deviation of a *non-empty* list of numbers."""
+    numElements = len(valueList)
+    if numElements == 0:
+        return (None, 0.0)
+
+    mean = float(sum(valueList)) / numElements
+    variance = 0
+    for value in valueList:
+        variance += math.pow(value - mean, 2)
+    variance = variance / float(numElements)
+    return (scale * mean, scale * math.sqrt(variance))
+
+
+def weightedMeanStdDev(valueList, weights, scale):
+    """Compute the weighted mean and standard deviation of a *non-empty* list of numbers."""
+
+    numElements = sum(weights)
+    if len(valueList) < 1 or numElements == 0:
+        return (None, 0.0)
+
+    mean = 0
+    for i in range(len(valueList)):
+        mean += float(valueList[i] * weights[i])
+    mean = mean / numElements
+    variance = 0
+    for i in range(len(valueList)):
+        variance += math.pow(valueList[i] - mean, 2) * weights[i]
+    variance = variance / float(numElements)
+
+    return (scale * mean, scale * math.sqrt(variance))
+
+
+def reportMeanStdDev(formatWidth, label, valueList, scale):
+    (m, s) = meanStdDev(valueList, scale)
+    printTable(formatWidth, [label, m, s])
+
+
+def reportWMeanStdDev(formatWidth, label, valueList, weights, scale):
+    (m, s) = weightedMeanStdDev(valueList, weights, scale)
+    printTable(formatWidth, [label, m, s])
+    # print(label + " (mean, stdev) = " + str(weightedMeanStdDev(valueList,weights,scale)))
+
+
+def reportCoupleCSV(sep, c):
+    (mean, stdev) = c
+    sys.stdout.write(sep + str(mean) + "," + str(stdev))
+
+
+def intMetric(dictionary, label):
+    return str(int(dictionary[label]))
+
+
+def floatMetric(dictionary, label):
+    return str(dictionary[label])
+
+
+def printTable(field_width, entries):
+    """Makes it easier to format output for evaluation metrics."""
+    cols = len(entries)
+    labelFormat = ""
+    for i in range(0, cols):
+        extra = ""
+        if type(entries[i]) is float:
+            labelFormat += "{0[" + str(i) + "]:>{width}.2f" + "}"
+        else:
+            labelFormat += "{0[" + str(i) + "]:>{width}}"
+    print(labelFormat.format(entries, width=field_width))
+
+
 def histogramm(values):
-        """Compute the histogramm of all values: a dictionnary dict[v]=count"""
-        hist = {}
-        for v in values:
-                if v in hist:
-                        hist[v]+=1
-                else:
-                        hist[v]=1
-        return hist
-
-def printHist(hist,N,field_width):
-        vals = []
-        cumulVals = []
-        cum = 0
-        for i in range(0,N):
-                if i in hist:
-                        vals.append(hist[i])
-                else:
-                        vals.append(0)
-                cum += vals[-1]
-                cumulVals.append(cum)
-                
-                # RZ: Inefficient but simple - sum all values, substracted accumulated ones.
-                total = 0
-                for key in list(hist):
-                        total += hist[key]
+    """Compute the histogramm of all values: a dictionnary dict[v]=count"""
+    hist = {}
+    for v in values:
+        if v in hist:
+            hist[v] += 1
+        else:
+            hist[v] = 1
+    return hist
+
 
-                remaining = total - cum
+def printHist(hist, N, field_width):
+    vals = []
+    cumulVals = []
+    cum = 0
+    for i in range(0, N):
+        if i in hist:
+            vals.append(hist[i])
+        else:
+            vals.append(0)
+        cum += vals[-1]
+        cumulVals.append(cum)
+
+        # RZ: Inefficient but simple - sum all values, substracted accumulated ones.
+        total = 0
+        for key in list(hist):
+            total += hist[key]
 
-        printTable(field_width, [ '' ] +  list(range(0,N)) + ['>' + str(N-1)])
-        print('----------------------------------------------------------------------------------')
-        printTable(field_width, [ 'Num. Files' ] + vals + [ remaining ])
-        printTable(field_width, [ 'Cum. Files' ] + cumulVals + [ total ])
+        remaining = total - cum
 
+    printTable(field_width, [""] + list(range(0, N)) + [">" + str(N - 1)])
+    print(
+        "----------------------------------------------------------------------------------"
+    )
+    printTable(field_width, ["Num. Files"] + vals + [remaining])
+    printTable(field_width, ["Cum. Files"] + cumulVals + [total])
 
 
 def main():
-        if len(sys.argv) < 2:
-                print("Usage : [[python]] sumMetric.py <label> <file1.m> [CSV] \n")
-                print("    [CSV] : print all results in one line \n")
-                print("")
-                print("'label' is a string used to identify data used for comparison.")
-                sys.exit(0)
-
-        showCSV = False
-        if len(sys.argv) > 3:
-                showCSV = True
-        # Read data from CSV file.
-        dataSourceLabel = sys.argv[1]
-        fileName = sys.argv[2]
-        try:
-                fileReader = csv.reader(open(fileName))
-        except:
-                sys.stderr.write('  !! IO Error (cannot open): ' + fileName)
-                sys.exit(0)
-
-        # Compile distributions for all metrics.
-        allValues = {}
-        nbEM = 0;
-        fileList = []
-        for row in fileReader:
-                # Skip blank lines.
-                if len(row) == 0:
-                        continue
-
-                entryType = row[0].strip()
-                if entryType == "*M":
-                        fileList = fileList + [row[1]]
-                        continue
-                for i in range(0,len(row),2):
-                        vName =  row[i].strip()
-                        if vName not in allValues:
-                                allValues[vName] = []
-                        allValues[vName] = allValues[vName] \
-                                        + [float(row[i+1].strip())]
-                nbEM+=1
-        
-        # Compile and display the sum for each metric.
-        allSum = {}
-        allZeroCount = {}
-        zeroFileList = {}
-        allHist = {}
-        for v in list(allValues):
-                allSum[v] = sum(allValues[v])
-                #print(str(v) + " = " + str(allSum[v]))
-                allHist[v] = histogramm(allValues[v])
-                allZeroCount[v] = 0
-                for s in range(len(allValues[v])):
-                        if allValues[v][s] == 0:
-                                allZeroCount[v] += 1
-                                if v in list(zeroFileList):
-                                        zeroFileList[v] += '\n' + str(s)
-                                else:
-                                        zeroFileList[v] = str(s)
-                                #print fileList[s]
-                #print ('    Correct expressions: ' + str(nbZ))
-
-        # Report input counts.
-        correctExps = int(allZeroCount["D_B"])
-        #sys.stderr.write( str( zeroFileList[ "D_B" ] ) )
-        correctExps2 = int(allZeroCount["D_E(%)"])
-        #sys.stdout.write( str( zeroFileList[ "D_E" ] ) )
-        if(not correctExps == correctExps2):
-                sys.stderr.write( "Warning : correctExps != correctExps2 (" + str(correctExps) + " vs " + str(correctExps2)+")")
-
-        nodes = int(allSum["nNodes"])
-        dcTotal = int(allSum["D_C"])
-        edges = int(allSum["nEdges"])
-        dlTotal = int(allSum["D_L"])
-        dbTotal = int(allSum["D_B"])
-        duTotal = int(allSum["dPairs"])
-        dsTotal = int(allSum["D_S"])  
-        dEdgeClassConflicts = int(allSum["edgeDiffClassCount"])
-
-        if showCSV:
-                print("D_C,D_L,D_S,D_B,D_B(%),var,D_E(%),var,wD_E(%),var")
-                sys.stdout.write(intMetric(allSum,"D_C") + "," +intMetric(allSum, "D_L") \
-                         + "," + str(dsTotal) + "," \
-                         + intMetric(allSum, "D_B"))
-                reportCoupleCSV(',',meanStdDev(allValues["D_B(%)"],100))
-                reportCoupleCSV(',',meanStdDev(allValues["D_E"],100))
-                reportCoupleCSV(',',weightedMeanStdDev(allValues["D_E"],allValues["nNodes"],100))
-                print("")
-        else:
-                fieldWidth = 10
-                
-                # Add file name and date.
-                print("LgEval Evaluation Summary")
-                print(time.strftime("%c"))
-                print("")
-                print(dataSourceLabel)
-                #print(os.path.splitext( os.path.split(fileName)[1]) )[0]
-                print('')
-
-                print("****  PRIMITIVES   **************************************************************")
-                print('')
-                printTable(fieldWidth,['Directed','Rate(%)','Total','Correct','Errors','SegErr','ClErr','RelErr'])
-                print("---------------------------------------------------------------------------------")
-                nodeRate = 100.0
-                if nodes > 0:
-                        nodeRate = 100 * float(nodes-dcTotal)/nodes
-                printTable(fieldWidth,['Nodes', nodeRate, int(allSum["nNodes"]), nodes - dcTotal, dcTotal ])
-
-        
-                edgeRate = 100.0
-                if edges > 0:
-                        edgeRate = 100 * float(edges - dlTotal) / edges
-
-                # RZ DEBUG: For relation conflicts, need to subtract segmentation and class label
-                #           edges from total errors.
-                printTable(fieldWidth,['Edges', edgeRate, edges, edges - dlTotal, dlTotal,\
-                                dsTotal, dEdgeClassConflicts, dlTotal -dsTotal -dEdgeClassConflicts])
-
-                labelRate = 100.0
-                if nodes + edges > 0:
-                        labelRate =  100 *(nodes + edges - dbTotal)/float(nodes + edges)
-                print('')
-                printTable(fieldWidth,['Total', labelRate, nodes + edges, nodes + edges - dbTotal, dbTotal])
-
-        
-                print('\n')
-                printTable(fieldWidth,['Undirected','Rate(%)','Total','Correct','Errors','SegErr','ClErr','RelErr'])
-                print("---------------------------------------------------------------------------------")
-                printTable(fieldWidth,['Nodes', nodeRate, int(allSum["nNodes"]), nodes - dcTotal, dcTotal ])
-
-                undirNodeRel = 100.0
-                if edges > 0:
-                        undirNodeRel = 100 * (float(edges)/2 - duTotal)/(edges/2)
-                mergeClassErrors = int(allSum["undirDiffClassCount"])
-                segPairErrors = int(allSum["segPairErrors"])
-
-                # RZ DEBUG: As above for directed edges, reporting of segmentation and relationship count
-                #           errors was wrong, despite being correct in the .csv and .diff output.
-                printTable(fieldWidth,['Node Pairs', undirNodeRel, int(edges/2), int(edges/2) - duTotal, duTotal, \
-                                segPairErrors, mergeClassErrors, duTotal - segPairErrors - mergeClassErrors ])
-
-                nodeAllRate = 100.0
-                nodeAllCorrect = 100.0
-                nodePairCorrect = 100.0
-
-                correctNodesAndPairs = nodes - dcTotal + float(edges)/2 - duTotal
-                pairCount = edges/2 
-                if nodes > 0:
-                        nodeAllCorrect = int(allSum["nodeCorrect"])
-                        nodeAllRate = 100 * float(nodeAllCorrect)/nodes
-                        nodePairCorrect = 100 * float(correctNodesAndPairs)/(nodes + pairCount)
-                
-                print('')
-                printTable(fieldWidth,['Total', nodePairCorrect, int(nodes + pairCount), int(correctNodesAndPairs), int(dcTotal + duTotal) ])
-
-                print('')
-                print('     SegErr: merge/split   ClErr: valid merge class error   RelErr: relation error')
-                
-                print("\n")
-
-                print("****  OBJECTS   **************************************************************************") 
-                print('')
-                printTable(fieldWidth,['','Recall(%)','Prec(%)','2RP/(R+P)','Targets','Correct','FalseNeg','*Detected','*FalsePos'])
-                print("------------------------------------------------------------------------------------------")
-                
-                # Compute segmentation and classification errors.
-                numSegments = int(allSum["nSeg"])
-                detectedSegs = int(allSum["detectedSeg"])
-                correctSegments = int(allSum["CorrectSegments"]) 
-                classErrors = int(allSum["ClassError"])
-                correctClass = int(allSum["CorrectSegmentsAndClass"]) 
-
-                # DEBUG: now explicitly record the number of correct segment rel. edges.
-                numSegRelEdges = int(allSum["nSegRelEdges"])
-                detectedSegRelEdges = int(allSum["dSegRelEdges"])
-                segRelErrors = int(allSum["SegRelErrors"])
-                correctSegRelEdges = int(allSum["CorrectSegRels"])
-                correctSegRelLocations = int(allSum["CorrectSegRelLocations"])
-
-                segRelRecall = 100.0
-                if numSegRelEdges > 0:
-                        segRelRecall = 100*float(correctSegRelEdges)/numSegRelEdges
-                segRelPrecision = 100.0
-                if detectedSegRelEdges > 0:
-                        segRelPrecision = float(100*float(correctSegRelEdges)/detectedSegRelEdges)
-                relFalsePositive = 0
-                if detectedSegRelEdges > 0:
-                        relFalsePositive = segRelErrors
-
-
-                segRate = 100.0
-                segClassRate = 100.0
-                if numSegments > 0:
-                        segRate = 100 * float(correctSegments)/numSegments
-                        segClassRate = 100*float(correctClass)/numSegments
-                segPrec = 100.0
-                segClassPrec = 100.0
-                if detectedSegs > 0:
-                        segPrec = 100 * float(correctSegments)/detectedSegs
-                        segClassPrec = 100*float(correctClass)/detectedSegs
-
-
-                segRelLocRecall = 100.0
-                if numSegRelEdges > 0:
-                        segRelLocRecall = 100 * float (correctSegRelLocations) / numSegRelEdges
-                segRelLocPrecision = 100.0
-                if detectedSegRelEdges > 0:
-                        segRelLocPrecision = 100 * float(correctSegRelLocations) / detectedSegRelEdges
-                segRelLocFalsePositive = 0
-                if detectedSegRelEdges > 0:
-                        segRelLocFalsePositive = detectedSegRelEdges - correctSegRelLocations 
-
-                segRelativeRecall = '(Empty)'
-                if correctSegments > 0:
-                        segRelativeRecall = 100 * float(correctClass)/correctSegments
-
-
-                segRelativeRelRecall = '(Empty)' 
-                if correctSegRelLocations > 0:
-                        segRelativeRelRecall = 100 * float(correctSegRelEdges)/correctSegRelLocations
-
-                printTable(fieldWidth,['Objects', segRate, \
-                                segPrec, fmeasure(segRate,segPrec),
-                                numSegments, correctSegments, numSegments - correctSegments,\
-                                                detectedSegs, detectedSegs - correctSegments ])
-
-                
-                printTable(fieldWidth,['+ Classes', segClassRate, \
-                                segClassPrec, fmeasure(segClassRate, segClassPrec), \
-                                numSegments, correctClass, numSegments - correctClass,\
-                                detectedSegs, detectedSegs - correctClass])
-
-                printTable(fieldWidth,['Class/Det', segRelativeRecall,'','', \
-                                correctSegments, correctClass])
-
-                print('')
-                printTable(fieldWidth,['Relations',\
-                                segRelLocRecall, \
-                                segRelLocPrecision, \
-                                fmeasure(segRelLocRecall, segRelLocPrecision), \
-                                numSegRelEdges,\
-                                correctSegRelLocations,\
-                                numSegRelEdges - correctSegRelLocations, \
-                                intMetric(allSum, "dSegRelEdges"),\
-                                segRelLocFalsePositive])
-
-
-                printTable(fieldWidth,['+ Classes',\
-                                segRelRecall, \
-                                segRelPrecision, \
-                                fmeasure(segRelRecall, segRelPrecision), \
-                                numSegRelEdges,\
-                                correctSegRelEdges,\
-                                numSegRelEdges - correctSegRelEdges, \
-                                intMetric(allSum, "dSegRelEdges"),\
-                                relFalsePositive])
-
-                printTable(fieldWidth,['Class/Det', segRelativeRelRecall, '', '', \
-                                correctSegRelLocations, correctSegRelEdges])
-
-                print("\n     2RP/(R+P): harmonic mean (f-measure) for (R)ecall and (P)recision")
-                print("     Class/Det: (correct detection and classification) / correct detection") 
-                print("\n")
-
-                print("****  FILES  ***************************************")
-                print('')
-                printTable(fieldWidth,['','Rate(%)','Total','Correct','Errors'])
-                print('---------------------------------------------------')
-                correctSegments = 0
-                if 1 in allHist['hasCorrectSegments']:
-                        correctSegments = allHist['hasCorrectSegments'][1]
-                correctRelLocs = 0
-                if 1 in allHist['hasCorrectRelationLocations']:
-                        correctRelLocs = allHist['hasCorrectRelationLocations'][1]
-                correctSegAndClass = 0
-                if 1 in allHist['hasCorrectSegLab']:
-                        correctSegAndClass = allHist['hasCorrectSegLab'][1]
-                correctRelAndClass = 0
-                if 1 in allHist['hasCorrectRelLab']:
-                        correctRelAndClass = allHist['hasCorrectRelLab'][1]
-                correctStructure = 0
-                if 1 in allHist['hasCorrectStructure']:
-                        correctStructure = allHist['hasCorrectStructure'][1]
-                
-
-                objRelative = '(Empty)' if correctSegments <  1 else 100 * float(correctSegAndClass)/correctSegments 
-                printTable(fieldWidth,['Objects',100 * float(correctSegments)/nbEM,nbEM,correctSegments,nbEM-correctSegments])
-                printTable(fieldWidth,['+ Classes',100 * float(correctSegAndClass)/nbEM, nbEM, correctSegAndClass, nbEM - correctSegAndClass])
-                printTable(fieldWidth,['Class/Det',objRelative,correctSegments,correctSegAndClass,'']) #correctSegments-correctSegAndClass])
-
-                print('')
-                relRelative = '(Empty)' if correctRelLocs < 1 else 100 * float(correctRelAndClass)/correctRelLocs
-                printTable(fieldWidth,['Relations',100 * float(correctRelLocs)/nbEM,nbEM,correctRelLocs,nbEM-correctRelLocs])
-                printTable(fieldWidth,['+ Classes',100 * float(correctRelAndClass)/nbEM, nbEM, correctRelAndClass, nbEM - correctRelAndClass])
-                printTable(fieldWidth,['Class/Det',relRelative, correctRelLocs,correctRelAndClass,'']) #correctRelLocs - correctRelAndClass])
-
-                print('')
-                expRelative = '(Empty)' if correctStructure < 1 else 100 * float(correctExps)/correctStructure
-                printTable(fieldWidth,['Structure',100 * float(correctStructure)/nbEM, nbEM, correctStructure, nbEM - correctStructure])
-
-                printTable(fieldWidth,['+ Classes',100 * float(correctExps)/nbEM,nbEM,correctExps,nbEM-correctExps,'*Final'])
-                printTable(fieldWidth,['Class/Det',expRelative,correctStructure,correctExps,'']) #correctStructure-correctExps])
-                print('')
-                
-                print('')
-                print("****  LABEL ERROR HISTOGRAM (Dir. Edges, D_B)  ****")
-                print('')
-                printHist(allHist['D_B'],6,fieldWidth)
-                print('')
+    if len(sys.argv) < 2:
+        print("Usage : [[python]] sumMetric.py <label> <file1.m> [CSV] \n")
+        print("    [CSV] : print all results in one line \n")
+        print("")
+        print("'label' is a string used to identify data used for comparison.")
+        sys.exit(0)
+
+    showCSV = False
+    if len(sys.argv) > 3:
+        showCSV = True
+    # Read data from CSV file.
+    dataSourceLabel = sys.argv[1]
+    fileName = sys.argv[2]
+    try:
+        fileReader = csv.reader(open(fileName))
+    except:
+        sys.stderr.write("  !! IO Error (cannot open): " + fileName)
+        sys.exit(0)
+
+    # Compile distributions for all metrics.
+    allValues = {}
+    nbEM = 0
+    fileList = []
+    for row in fileReader:
+        # Skip blank lines.
+        if len(row) == 0:
+            continue
+
+        entryType = row[0].strip()
+        if entryType == "*M":
+            fileList = fileList + [row[1]]
+            continue
+        for i in range(0, len(row), 2):
+            vName = row[i].strip()
+            if vName not in allValues:
+                allValues[vName] = []
+            allValues[vName] = allValues[vName] + [float(row[i + 1].strip())]
+        nbEM += 1
+
+    # Compile and display the sum for each metric.
+    allSum = {}
+    allZeroCount = {}
+    zeroFileList = {}
+    allHist = {}
+    for v in list(allValues):
+        allSum[v] = sum(allValues[v])
+        # print(str(v) + " = " + str(allSum[v]))
+        allHist[v] = histogramm(allValues[v])
+        allZeroCount[v] = 0
+        for s in range(len(allValues[v])):
+            if allValues[v][s] == 0:
+                allZeroCount[v] += 1
+                if v in list(zeroFileList):
+                    zeroFileList[v] += "\n" + str(s)
+                else:
+                    zeroFileList[v] = str(s)
+                # print fileList[s]
+        # print ('    Correct expressions: ' + str(nbZ))
+
+    # Report input counts.
+    correctExps = int(allZeroCount["D_B"])
+    # sys.stderr.write( str( zeroFileList[ "D_B" ] ) )
+    correctExps2 = int(allZeroCount["D_E(%)"])
+    # sys.stdout.write( str( zeroFileList[ "D_E" ] ) )
+    if not correctExps == correctExps2:
+        sys.stderr.write(
+            "Warning : correctExps != correctExps2 ("
+            + str(correctExps)
+            + " vs "
+            + str(correctExps2)
+            + ")"
+        )
+
+    nodes = int(allSum["nNodes"])
+    dcTotal = int(allSum["D_C"])
+    edges = int(allSum["nEdges"])
+    dlTotal = int(allSum["D_L"])
+    dbTotal = int(allSum["D_B"])
+    duTotal = int(allSum["dPairs"])
+    dsTotal = int(allSum["D_S"])
+    dEdgeClassConflicts = int(allSum["edgeDiffClassCount"])
+
+    if showCSV:
+        print("D_C,D_L,D_S,D_B,D_B(%),var,D_E(%),var,wD_E(%),var")
+        sys.stdout.write(
+            intMetric(allSum, "D_C")
+            + ","
+            + intMetric(allSum, "D_L")
+            + ","
+            + str(dsTotal)
+            + ","
+            + intMetric(allSum, "D_B")
+        )
+        reportCoupleCSV(",", meanStdDev(allValues["D_B(%)"], 100))
+        reportCoupleCSV(",", meanStdDev(allValues["D_E"], 100))
+        reportCoupleCSV(
+            ",", weightedMeanStdDev(allValues["D_E"], allValues["nNodes"], 100)
+        )
+        print("")
+    else:
+        fieldWidth = 10
+
+        # Add file name and date.
+        print("LgEval Evaluation Summary")
+        print(time.strftime("%c"))
+        print("")
+        print(dataSourceLabel)
+        # print(os.path.splitext( os.path.split(fileName)[1]) )[0]
+        print("")
+
+        print(
+            "****  PRIMITIVES   **************************************************************"
+        )
+        print("")
+        printTable(
+            fieldWidth,
+            [
+                "Directed",
+                "Rate(%)",
+                "Total",
+                "Correct",
+                "Errors",
+                "SegErr",
+                "ClErr",
+                "RelErr",
+            ],
+        )
+        print(
+            "---------------------------------------------------------------------------------"
+        )
+        nodeRate = 100.0
+        if nodes > 0:
+            nodeRate = 100 * float(nodes - dcTotal) / nodes
+        printTable(
+            fieldWidth,
+            ["Nodes", nodeRate, int(allSum["nNodes"]), nodes - dcTotal, dcTotal],
+        )
+
+        edgeRate = 100.0
+        if edges > 0:
+            edgeRate = 100 * float(edges - dlTotal) / edges
+
+        # RZ DEBUG: For relation conflicts, need to subtract segmentation and class label
+        #           edges from total errors.
+        printTable(
+            fieldWidth,
+            [
+                "Edges",
+                edgeRate,
+                edges,
+                edges - dlTotal,
+                dlTotal,
+                dsTotal,
+                dEdgeClassConflicts,
+                dlTotal - dsTotal - dEdgeClassConflicts,
+            ],
+        )
+
+        labelRate = 100.0
+        if nodes + edges > 0:
+            labelRate = 100 * (nodes + edges - dbTotal) / float(nodes + edges)
+        print("")
+        printTable(
+            fieldWidth,
+            ["Total", labelRate, nodes + edges, nodes + edges - dbTotal, dbTotal],
+        )
+
+        print("\n")
+        printTable(
+            fieldWidth,
+            [
+                "Undirected",
+                "Rate(%)",
+                "Total",
+                "Correct",
+                "Errors",
+                "SegErr",
+                "ClErr",
+                "RelErr",
+            ],
+        )
+        print(
+            "---------------------------------------------------------------------------------"
+        )
+        printTable(
+            fieldWidth,
+            ["Nodes", nodeRate, int(allSum["nNodes"]), nodes - dcTotal, dcTotal],
+        )
+
+        undirNodeRel = 100.0
+        if edges > 0:
+            undirNodeRel = 100 * (float(edges) / 2 - duTotal) / (edges / 2)
+        mergeClassErrors = int(allSum["undirDiffClassCount"])
+        segPairErrors = int(allSum["segPairErrors"])
+
+        # RZ DEBUG: As above for directed edges, reporting of segmentation and relationship count
+        #           errors was wrong, despite being correct in the .csv and .diff output.
+        printTable(
+            fieldWidth,
+            [
+                "Node Pairs",
+                undirNodeRel,
+                int(edges / 2),
+                int(edges / 2) - duTotal,
+                duTotal,
+                segPairErrors,
+                mergeClassErrors,
+                duTotal - segPairErrors - mergeClassErrors,
+            ],
+        )
+
+        nodeAllRate = 100.0
+        nodeAllCorrect = 100.0
+        nodePairCorrect = 100.0
+
+        correctNodesAndPairs = nodes - dcTotal + float(edges) / 2 - duTotal
+        pairCount = edges / 2
+        if nodes > 0:
+            nodeAllCorrect = int(allSum["nodeCorrect"])
+            nodeAllRate = 100 * float(nodeAllCorrect) / nodes
+            nodePairCorrect = 100 * float(correctNodesAndPairs) / (nodes + pairCount)
+
+        print("")
+        printTable(
+            fieldWidth,
+            [
+                "Total",
+                nodePairCorrect,
+                int(nodes + pairCount),
+                int(correctNodesAndPairs),
+                int(dcTotal + duTotal),
+            ],
+        )
+
+        print("")
+        print(
+            "     SegErr: merge/split   ClErr: valid merge class error   RelErr: relation error"
+        )
+
+        print("\n")
+
+        print(
+            "****  OBJECTS   **************************************************************************"
+        )
+        print("")
+        printTable(
+            fieldWidth,
+            [
+                "",
+                "Recall(%)",
+                "Prec(%)",
+                "2RP/(R+P)",
+                "Targets",
+                "Correct",
+                "FalseNeg",
+                "*Detected",
+                "*FalsePos",
+            ],
+        )
+        print(
+            "------------------------------------------------------------------------------------------"
+        )
+
+        # Compute segmentation and classification errors.
+        numSegments = int(allSum["nSeg"])
+        detectedSegs = int(allSum["detectedSeg"])
+        correctSegments = int(allSum["CorrectSegments"])
+        classErrors = int(allSum["ClassError"])
+        correctClass = int(allSum["CorrectSegmentsAndClass"])
+
+        # DEBUG: now explicitly record the number of correct segment rel. edges.
+        numSegRelEdges = int(allSum["nSegRelEdges"])
+        detectedSegRelEdges = int(allSum["dSegRelEdges"])
+        segRelErrors = int(allSum["SegRelErrors"])
+        correctSegRelEdges = int(allSum["CorrectSegRels"])
+        correctSegRelLocations = int(allSum["CorrectSegRelLocations"])
+
+        segRelRecall = 100.0
+        if numSegRelEdges > 0:
+            segRelRecall = 100 * float(correctSegRelEdges) / numSegRelEdges
+        segRelPrecision = 100.0
+        if detectedSegRelEdges > 0:
+            segRelPrecision = float(
+                100 * float(correctSegRelEdges) / detectedSegRelEdges
+            )
+        relFalsePositive = 0
+        if detectedSegRelEdges > 0:
+            relFalsePositive = segRelErrors
+
+        segRate = 100.0
+        segClassRate = 100.0
+        if numSegments > 0:
+            segRate = 100 * float(correctSegments) / numSegments
+            segClassRate = 100 * float(correctClass) / numSegments
+        segPrec = 100.0
+        segClassPrec = 100.0
+        if detectedSegs > 0:
+            segPrec = 100 * float(correctSegments) / detectedSegs
+            segClassPrec = 100 * float(correctClass) / detectedSegs
+
+        segRelLocRecall = 100.0
+        if numSegRelEdges > 0:
+            segRelLocRecall = 100 * float(correctSegRelLocations) / numSegRelEdges
+        segRelLocPrecision = 100.0
+        if detectedSegRelEdges > 0:
+            segRelLocPrecision = (
+                100 * float(correctSegRelLocations) / detectedSegRelEdges
+            )
+        segRelLocFalsePositive = 0
+        if detectedSegRelEdges > 0:
+            segRelLocFalsePositive = detectedSegRelEdges - correctSegRelLocations
+
+        segRelativeRecall = "(Empty)"
+        if correctSegments > 0:
+            segRelativeRecall = 100 * float(correctClass) / correctSegments
+
+        segRelativeRelRecall = "(Empty)"
+        if correctSegRelLocations > 0:
+            segRelativeRelRecall = (
+                100 * float(correctSegRelEdges) / correctSegRelLocations
+            )
+
+        printTable(
+            fieldWidth,
+            [
+                "Objects",
+                segRate,
+                segPrec,
+                fmeasure(segRate, segPrec),
+                numSegments,
+                correctSegments,
+                numSegments - correctSegments,
+                detectedSegs,
+                detectedSegs - correctSegments,
+            ],
+        )
+
+        printTable(
+            fieldWidth,
+            [
+                "+ Classes",
+                segClassRate,
+                segClassPrec,
+                fmeasure(segClassRate, segClassPrec),
+                numSegments,
+                correctClass,
+                numSegments - correctClass,
+                detectedSegs,
+                detectedSegs - correctClass,
+            ],
+        )
+
+        printTable(
+            fieldWidth,
+            ["Class/Det", segRelativeRecall, "", "", correctSegments, correctClass],
+        )
+
+        print("")
+        printTable(
+            fieldWidth,
+            [
+                "Relations",
+                segRelLocRecall,
+                segRelLocPrecision,
+                fmeasure(segRelLocRecall, segRelLocPrecision),
+                numSegRelEdges,
+                correctSegRelLocations,
+                numSegRelEdges - correctSegRelLocations,
+                intMetric(allSum, "dSegRelEdges"),
+                segRelLocFalsePositive,
+            ],
+        )
+
+        printTable(
+            fieldWidth,
+            [
+                "+ Classes",
+                segRelRecall,
+                segRelPrecision,
+                fmeasure(segRelRecall, segRelPrecision),
+                numSegRelEdges,
+                correctSegRelEdges,
+                numSegRelEdges - correctSegRelEdges,
+                intMetric(allSum, "dSegRelEdges"),
+                relFalsePositive,
+            ],
+        )
+
+        printTable(
+            fieldWidth,
+            [
+                "Class/Det",
+                segRelativeRelRecall,
+                "",
+                "",
+                correctSegRelLocations,
+                correctSegRelEdges,
+            ],
+        )
+
+        print(
+            "\n     2RP/(R+P): harmonic mean (f-measure) for (R)ecall and (P)recision"
+        )
+        print(
+            "     Class/Det: (correct detection and classification) / correct detection"
+        )
+        print("\n")
+
+        print("****  FILES  ***************************************")
+        print("")
+        printTable(fieldWidth, ["", "Rate(%)", "Total", "Correct", "Errors"])
+        print("---------------------------------------------------")
+        correctSegments = 0
+        if 1 in allHist["hasCorrectSegments"]:
+            correctSegments = allHist["hasCorrectSegments"][1]
+        correctRelLocs = 0
+        if 1 in allHist["hasCorrectRelationLocations"]:
+            correctRelLocs = allHist["hasCorrectRelationLocations"][1]
+        correctSegAndClass = 0
+        if 1 in allHist["hasCorrectSegLab"]:
+            correctSegAndClass = allHist["hasCorrectSegLab"][1]
+        correctRelAndClass = 0
+        if 1 in allHist["hasCorrectRelLab"]:
+            correctRelAndClass = allHist["hasCorrectRelLab"][1]
+        correctStructure = 0
+        if 1 in allHist["hasCorrectStructure"]:
+            correctStructure = allHist["hasCorrectStructure"][1]
+
+        objRelative = (
+            "(Empty)"
+            if correctSegments < 1
+            else 100 * float(correctSegAndClass) / correctSegments
+        )
+        printTable(
+            fieldWidth,
+            [
+                "Objects",
+                100 * float(correctSegments) / nbEM,
+                nbEM,
+                correctSegments,
+                nbEM - correctSegments,
+            ],
+        )
+        printTable(
+            fieldWidth,
+            [
+                "+ Classes",
+                100 * float(correctSegAndClass) / nbEM,
+                nbEM,
+                correctSegAndClass,
+                nbEM - correctSegAndClass,
+            ],
+        )
+        printTable(
+            fieldWidth,
+            ["Class/Det", objRelative, correctSegments, correctSegAndClass, ""],
+        )  # correctSegments-correctSegAndClass])
+
+        print("")
+        relRelative = (
+            "(Empty)"
+            if correctRelLocs < 1
+            else 100 * float(correctRelAndClass) / correctRelLocs
+        )
+        printTable(
+            fieldWidth,
+            [
+                "Relations",
+                100 * float(correctRelLocs) / nbEM,
+                nbEM,
+                correctRelLocs,
+                nbEM - correctRelLocs,
+            ],
+        )
+        printTable(
+            fieldWidth,
+            [
+                "+ Classes",
+                100 * float(correctRelAndClass) / nbEM,
+                nbEM,
+                correctRelAndClass,
+                nbEM - correctRelAndClass,
+            ],
+        )
+        printTable(
+            fieldWidth,
+            ["Class/Det", relRelative, correctRelLocs, correctRelAndClass, ""],
+        )  # correctRelLocs - correctRelAndClass])
+
+        print("")
+        expRelative = (
+            "(Empty)"
+            if correctStructure < 1
+            else 100 * float(correctExps) / correctStructure
+        )
+        printTable(
+            fieldWidth,
+            [
+                "Structure",
+                100 * float(correctStructure) / nbEM,
+                nbEM,
+                correctStructure,
+                nbEM - correctStructure,
+            ],
+        )
+
+        printTable(
+            fieldWidth,
+            [
+                "+ Classes",
+                100 * float(correctExps) / nbEM,
+                nbEM,
+                correctExps,
+                nbEM - correctExps,
+                "*Final",
+            ],
+        )
+        printTable(
+            fieldWidth, ["Class/Det", expRelative, correctStructure, correctExps, ""]
+        )  # correctStructure-correctExps])
+        print("")
+
+        print("")
+        print("****  LABEL ERROR HISTOGRAM (Dir. Edges, D_B)  ****")
+        print("")
+        printHist(allHist["D_B"], 6, fieldWidth)
+        print("")
 
 
 main()
diff --git a/src/testNewSeg.py b/src/testNewSeg.py
index 27cab93..e05095f 100644
--- a/src/testNewSeg.py
+++ b/src/testNewSeg.py
@@ -4,7 +4,7 @@
 # Test program for new segmentation for label graphs.
 #
 # Authors: R. Zanibbi, H. Mouchere
-#	June-August 2012
+# 	June-August 2012
 # Copyright (c) 2012-2014, Richard Zanibbi and Harold Mouchere
 ################################################################
 from bg import *
@@ -12,46 +12,46 @@ from bestBG import *
 
 
 def main():
-	nodeLabels = {}
-	nodeLabels['n1'] = { '2' : 1.0, '5' : False }
-	nodeLabels['n2'] = { '+' : 1.0 }
-	nodeLabels['n3'] = { '+' : 1.0 }
-	nodeLabels['n4'] = { '2' : 1.0 }
-
-	edgeLabels = {}
-	edgeLabels[('n1','n2')] = { 'R' : 1.0, 'S' : 1.0 }
-	edgeLabels[('n1','n3')] = { 'R' : 1.0 }
-	edgeLabels[('n1','n4')] = { 'R' : 1.0 }
-	edgeLabels[('n2','n3')] = { '*' : 1.0 }
-	edgeLabels[('n2','n4')] = { 'R' : 1.0 }
-	edgeLabels[('n3','n2')] = { '*' : 1.0 }
-	edgeLabels[('n3','n4')] = { 'R' : 1.0 }
-	#edgeLabels[('nAnon1','nAnon2')] = { 'Anon' : 'b'}
-
-	bg = Bg(nodeLabels,edgeLabels)
-	print(bg)
-	print(bg.csv())
-	
-	(psM, spM, rootSegs, segEdges) = bg.segmentGraph()
-	print('Primitives to Segments')
-	print(psM)
-	print('Segments to Primitives')
-	print(spM)
-	print('Root objects/segments')
-	print(rootSegs)
-	print('Edges between objects/segments')
-	print(segEdges)
-
-	bg = Bg('crohmeTest.bg')
-	(psM, spM, rootSegs, segEdges) = bg.segmentGraph()
-	print('Primitives to Segments')
-	print(psM)
-	print('Segments to Primitives')
-	print(spM)
-	print('Root objects/segments')
-	print(rootSegs)
-	print('Edges between objects/segments')
-	print(segEdges)
+    nodeLabels = {}
+    nodeLabels["n1"] = {"2": 1.0, "5": False}
+    nodeLabels["n2"] = {"+": 1.0}
+    nodeLabels["n3"] = {"+": 1.0}
+    nodeLabels["n4"] = {"2": 1.0}
+
+    edgeLabels = {}
+    edgeLabels[("n1", "n2")] = {"R": 1.0, "S": 1.0}
+    edgeLabels[("n1", "n3")] = {"R": 1.0}
+    edgeLabels[("n1", "n4")] = {"R": 1.0}
+    edgeLabels[("n2", "n3")] = {"*": 1.0}
+    edgeLabels[("n2", "n4")] = {"R": 1.0}
+    edgeLabels[("n3", "n2")] = {"*": 1.0}
+    edgeLabels[("n3", "n4")] = {"R": 1.0}
+    # edgeLabels[('nAnon1','nAnon2')] = { 'Anon' : 'b'}
+
+    bg = Bg(nodeLabels, edgeLabels)
+    print(bg)
+    print(bg.csv())
+
+    (psM, spM, rootSegs, segEdges) = bg.segmentGraph()
+    print("Primitives to Segments")
+    print(psM)
+    print("Segments to Primitives")
+    print(spM)
+    print("Root objects/segments")
+    print(rootSegs)
+    print("Edges between objects/segments")
+    print(segEdges)
+
+    bg = Bg("crohmeTest.bg")
+    (psM, spM, rootSegs, segEdges) = bg.segmentGraph()
+    print("Primitives to Segments")
+    print(psM)
+    print("Segments to Primitives")
+    print(spM)
+    print("Root objects/segments")
+    print(rootSegs)
+    print("Edges between objects/segments")
+    print(segEdges)
 
 
 main()
diff --git a/src/testlg.py b/src/testlg.py
index 5c9b609..0e96651 100644
--- a/src/testlg.py
+++ b/src/testlg.py
@@ -7,342 +7,374 @@
 # Copyright (c) 2012-2014, Richard Zanibbi and Harold Mouchere
 ################################################################
 from lgeval.src.lg import Lg
-#import smallGraph
+
+# import smallGraph
 import lgeval.src.SmGrConfMatrix as SmGrConfMatrix
 
-#from bestBG import *
+# from bestBG import *
+
 
 def loadFiles(testfiles):
-	for nextFile in testfiles:
-		print('[ FILE: ' + nextFile + ' ]')
-		n = Lg(nextFile)
-		print(n)
-		print(n.csv())
+    for nextFile in testfiles:
+        print("[ FILE: " + nextFile + " ]")
+        n = Lg(nextFile)
+        print(n)
+        print(n.csv())
+
 
 def testInvalidFiles(testfiles):
-	print("--TESTING INVALID FILE INPUT")
-	loadFiles(testfiles)
+    print("--TESTING INVALID FILE INPUT")
+    loadFiles(testfiles)
+
 
 def testInput(testfiles):
-	print("-- TESTING FILE INPUT")
-	loadFiles(testfiles)
+    print("-- TESTING FILE INPUT")
+    loadFiles(testfiles)
+
 
 def testshortCuts(compareFiles):
-	print('\n--TESTING SHORTCUTS')
-	for next in compareFiles:
-		n1 = Lg(next[0])
-		n2 = Lg(next[1])
-		print('>> ' + next[0] + ' vs. ' + next[1])
-		out1 = n1.compare(n2)
-		if out1[0][8][1] == 0:
-			print ("\tOK ")
-		else:
-			print (str(out1))
-
-def labelComparison(f1,f2, check):
-	print('\n[ Comparing Labels for FILE: ' + f1 + ' and ' + f2 + ' ]')
-	for (file1,file2) in [(f1,f2),(f2,f1)]:
-		n1 = Lg(file1)
-		n2 = Lg(file2)
-		print('>> ' + file1 + ' vs. ' + file2)
-		out1 = n1.compare(n2)
-		met = dict(out1[0])
-		detail = False
-		for k in list(check):
-			if k == "corrSeg":
-				if check[k] != len(out1[4]):
-					print ('Problem with ' + k)
-					detail = True
-			elif check[k] != met[k]:
-				print ('Problem with ' + k)
-				detail = True
-		if detail:
-			for el in out1[0]:
-				print('  ' + str(el))
-			print('  Node diffs: ' + str(out1[1]))
-			print('  Edge diffs: ' + str(out1[2]))
-			print('  SegEdge diffs: ' + str(out1[3]))
-			print('  Correct Segments: ' + str(out1[4]))
-		else:
-			print ('\tOK '+str(check))
+    print("\n--TESTING SHORTCUTS")
+    for next in compareFiles:
+        n1 = Lg(next[0])
+        n2 = Lg(next[1])
+        print(">> " + next[0] + " vs. " + next[1])
+        out1 = n1.compare(n2)
+        if out1[0][8][1] == 0:
+            print("\tOK ")
+        else:
+            print(str(out1))
+
+
+def labelComparison(f1, f2, check):
+    print("\n[ Comparing Labels for FILE: " + f1 + " and " + f2 + " ]")
+    for (file1, file2) in [(f1, f2), (f2, f1)]:
+        n1 = Lg(file1)
+        n2 = Lg(file2)
+        print(">> " + file1 + " vs. " + file2)
+        out1 = n1.compare(n2)
+        met = dict(out1[0])
+        detail = False
+        for k in list(check):
+            if k == "corrSeg":
+                if check[k] != len(out1[4]):
+                    print("Problem with " + k)
+                    detail = True
+            elif check[k] != met[k]:
+                print("Problem with " + k)
+                detail = True
+        if detail:
+            for el in out1[0]:
+                print("  " + str(el))
+            print("  Node diffs: " + str(out1[1]))
+            print("  Edge diffs: " + str(out1[2]))
+            print("  SegEdge diffs: " + str(out1[3]))
+            print("  Correct Segments: " + str(out1[4]))
+        else:
+            print("\tOK " + str(check))
 
 
 def testLabelComparisons(compareFiles):
-	print('\n--TESTING METRICS AND ERROR LOCALIZATON')
-	for next in compareFiles:
-		labelComparison(next[0],next[1], next[2])
+    print("\n--TESTING METRICS AND ERROR LOCALIZATON")
+    for next in compareFiles:
+        labelComparison(next[0], next[1], next[2])
+
 
 def testEmpty(emptyFiles):
-	print('\n--TESTING EMPTY FILES')
-	for next in emptyFiles:
-		print("* " + next[0] + " vs. " + next[1])
-		labelComparison(next[0],next[1])
+    print("\n--TESTING EMPTY FILES")
+    for next in emptyFiles:
+        print("* " + next[0] + " vs. " + next[1])
+        labelComparison(next[0], next[1])
 
-	print('\n--TEST NON-EXISTENT FILE')
-	notAFile = Lg('thisfiledoesnotexist')
-	print("\nError flag set for missing file:" + str(notAFile.error))
-	print(notAFile)
+    print("\n--TEST NON-EXISTENT FILE")
+    notAFile = Lg("thisfiledoesnotexist")
+    print("\nError flag set for missing file:" + str(notAFile.error))
+    print(notAFile)
 
 
 def testSegments(segFiles):
-	print('\n--TESTING SEGMENTATION')
-	for file in segFiles:
-		print('\n[ Segmentation for FILE: ' + file[0] + ' ]')
-		n = Lg(file[0])
-		(segmentPrimitiveMap, primitiveSegmentMap, noparentSegments, segmentEdges) = \
-				n.segmentGraph()
-		if(len(segmentPrimitiveMap) == file[1]["nbSeg"]) and (len(segmentEdges) == file[1]["nbSegEd"]):
-			print("\tOK :"+str(file[1]))
-		else:
-			print('  SEGMENTS -> PRIMITIVES:\n\t' + str(segmentPrimitiveMap))
-			print('  PRIMITIVES -> SEGMENTS:\n\t' + str(primitiveSegmentMap))
-			print('  NON-PARENT SEGMENTS: ' + str(noparentSegments))
-			print('  SEGMENT EDGES:\n\t' + str(segmentEdges))
+    print("\n--TESTING SEGMENTATION")
+    for file in segFiles:
+        print("\n[ Segmentation for FILE: " + file[0] + " ]")
+        n = Lg(file[0])
+        (
+            segmentPrimitiveMap,
+            primitiveSegmentMap,
+            noparentSegments,
+            segmentEdges,
+        ) = n.segmentGraph()
+        if (len(segmentPrimitiveMap) == file[1]["nbSeg"]) and (
+            len(segmentEdges) == file[1]["nbSegEd"]
+        ):
+            print("\tOK :" + str(file[1]))
+        else:
+            print("  SEGMENTS -> PRIMITIVES:\n\t" + str(segmentPrimitiveMap))
+            print("  PRIMITIVES -> SEGMENTS:\n\t" + str(primitiveSegmentMap))
+            print("  NON-PARENT SEGMENTS: " + str(noparentSegments))
+            print("  SEGMENT EDGES:\n\t" + str(segmentEdges))
+
 
 def testTreeEdges(treeFiles):
-	print('\n--TESTING TREE EDGE/LAYOUT TREE EXTRACTION')
-	for file in treeFiles:
-		print('\n[ Tree Edges for FILE: ' + file + ' ]')
-		n = Lg(file)
-		(rootNodes,tEdges,oEdges) = n.separateTreeEdges()
-		print('  ROOT NODES: ' + str(rootNodes))
-		print('  TREE EDGES: ' + str(tEdges))
-		print('  NON-TREE EDGES:' + str(oEdges))
+    print("\n--TESTING TREE EDGE/LAYOUT TREE EXTRACTION")
+    for file in treeFiles:
+        print("\n[ Tree Edges for FILE: " + file + " ]")
+        n = Lg(file)
+        (rootNodes, tEdges, oEdges) = n.separateTreeEdges()
+        print("  ROOT NODES: " + str(rootNodes))
+        print("  TREE EDGES: " + str(tEdges))
+        print("  NON-TREE EDGES:" + str(oEdges))
+
 
 def testSummingGraphs(mergeFiles):
-	print('\n--TESTS FOR ADDING NODE/EDGE LABEL VALUES')
-	for ( file1, file2 ) in mergeFiles:
-		print('\n[ Merging ' + file1 + ' and ' + file2 + ']')
-		lg1 = Lg(file1)
-		lg2 = Lg(file2)
-		lg1.addWeightedLabelValues(lg2)
-		print(lg1)
-		print(lg1.csv())
-
-		print('-- with graph weights 0.25 and 0.75')
-		lg1 = Lg(file1)
-		lg1.gweight = 0.25
-		lg2 = Lg(file2)
-		lg2.gweight = 0.75
-		lg1.addWeightedLabelValues(lg2)
-		print(lg1)
-		print(lg1.csv())
+    print("\n--TESTS FOR ADDING NODE/EDGE LABEL VALUES")
+    for (file1, file2) in mergeFiles:
+        print("\n[ Merging " + file1 + " and " + file2 + "]")
+        lg1 = Lg(file1)
+        lg2 = Lg(file2)
+        lg1.addWeightedLabelValues(lg2)
+        print(lg1)
+        print(lg1.csv())
+
+        print("-- with graph weights 0.25 and 0.75")
+        lg1 = Lg(file1)
+        lg1.gweight = 0.25
+        lg2 = Lg(file2)
+        lg2.gweight = 0.75
+        lg1.addWeightedLabelValues(lg2)
+        print(lg1)
+        print(lg1.csv())
+
 
 def testMaxLabel(mergeFiles):
-	print('\n--TESTS FOR SELECTING MAX. VALUE LABELS')
-	for ( file1, file2 ) in mergeFiles:
-		print('\n[ Selecting max labels from combined ' + file1 + \
-				' and ' + file2 + ']')
-		lg1 = Lg(file1)
-		lg2 = Lg(file2)
-		lg1.addWeightedLabelValues(lg2)
-		lg1.selectMaxLabels()
-		print(lg1)
-		print(lg1.csv())
-
-		print('-- with graph weights 0.25 and 0.75')
-		lg1 = Lg(file1)
-		lg1.gweight = 0.25
-		lg2 = Lg(file2)
-		lg2.gweight = 0.75
-		lg1.addWeightedLabelValues(lg2)
-		lg1.selectMaxLabels()
-		print(lg1)
-		print(lg1.csv())
+    print("\n--TESTS FOR SELECTING MAX. VALUE LABELS")
+    for (file1, file2) in mergeFiles:
+        print("\n[ Selecting max labels from combined " + file1 + " and " + file2 + "]")
+        lg1 = Lg(file1)
+        lg2 = Lg(file2)
+        lg1.addWeightedLabelValues(lg2)
+        lg1.selectMaxLabels()
+        print(lg1)
+        print(lg1.csv())
+
+        print("-- with graph weights 0.25 and 0.75")
+        lg1 = Lg(file1)
+        lg1.gweight = 0.25
+        lg2 = Lg(file2)
+        lg2.gweight = 0.75
+        lg1.addWeightedLabelValues(lg2)
+        lg1.selectMaxLabels()
+        print(lg1)
+        print(lg1.csv())
+
 
 def testGenAllBG(files):
-	print('\n--TESTING GENERATION OF K BEST BG')
-	for file in files:
-		print('\n[ FILE: ' + file + ' ]')
-		lg1 = Lg(file)
-		blg = BestBG(lg1,5)
-		blg.afficheDP()
-		for i in range(5):
-			print("BG top "+str(i))
-			print(blg.getBG(i).csv())
-		
-	print ("END")
-		
+    print("\n--TESTING GENERATION OF K BEST BG")
+    for file in files:
+        print("\n[ FILE: " + file + " ]")
+        lg1 = Lg(file)
+        blg = BestBG(lg1, 5)
+        blg.afficheDP()
+        for i in range(5):
+            print("BG top " + str(i))
+            print(blg.getBG(i).csv())
+
+    print("END")
+
+
 def testInvertValues(files):
-	print('\n--TESTING INVERTING LABEL VALUES')
-	for file in files:
-		print('\n[ FILE: ' + file + ' ]')
-		lg1 = Lg(file)
-		print(lg1)
-		print(lg1.csv())
-
-		# Invert values.
-		lg1.invertValues()
-		print(lg1)
-		print(lg1.csv())
-		
-		# And back to original values.
-		lg1.invertValues()
-		print(lg1)
-		print(lg1.csv())
+    print("\n--TESTING INVERTING LABEL VALUES")
+    for file in files:
+        print("\n[ FILE: " + file + " ]")
+        lg1 = Lg(file)
+        print(lg1)
+        print(lg1.csv())
+
+        # Invert values.
+        lg1.invertValues()
+        print(lg1)
+        print(lg1.csv())
+
+        # And back to original values.
+        lg1.invertValues()
+        print(lg1)
+        print(lg1.csv())
+
 
 def testStructCompare(files):
-	print('\n--TESTING STRUCT COMPARE')
-	print (" sub iterator (sizes 1 and 3): ")
-	g1 = Lg(files[0][0])
-	for s in g1.subStructIterator([1,3]):
-		print(s)
-	print (" sub iterator (sizes 2): ")
-	for s in g1.subStructIterator(2):
-		print(s)
-	print (" sub comparision :")
-	for ( file1, file2 ) in files:		
-		g1 = Lg(file1) # ground-truth
-		g2 = Lg(file2) # output
-		print (str(g2.compareSubStruct(g1,4)))
+    print("\n--TESTING STRUCT COMPARE")
+    print(" sub iterator (sizes 1 and 3): ")
+    g1 = Lg(files[0][0])
+    for s in g1.subStructIterator([1, 3]):
+        print(s)
+    print(" sub iterator (sizes 2): ")
+    for s in g1.subStructIterator(2):
+        print(s)
+    print(" sub comparision :")
+    for (file1, file2) in files:
+        g1 = Lg(file1)  # ground-truth
+        g2 = Lg(file2)  # output
+        print(str(g2.compareSubStruct(g1, 4)))
+
 
 def testSubGraphCounting(files):
-	stat = SmGrConfMatrix.SmDict()
-	mat = SmGrConfMatrix.ConfMatrix()
-	segMat = SmGrConfMatrix.ConfMatrixObject()
-	for ( fileGT, fileOUT,_ ) in files:		
-		gGT = Lg(fileGT)
-		for s in gGT.subStructIterator([1,2,3,4]):
-			stat.get(s,SmGrConfMatrix.Counter).incr()
-		gOUT = Lg(fileOUT)
-		for (gt,er) in gOUT.compareSubStruct(gGT,[2,3]):
-			mat.incr(gt,er,("../"+fileOUT))
-		for (seg,gt,er) in gOUT.compareSegmentsStruct(gGT,[2,3]):
-			segMat.incr(seg,gt,er,("../"+fileOUT))
-	print ("stat from left side expressions:")
-	#print stat
-	print ("generate HTML in test.html" )
-	out=open('Tests/test.html','w')
-	out.write('<html xmlns="http://www.w3.org/1999/xhtml">')
-	out.write('<h1> Substructure Stat </h1>')
-	out.write(stat.toHTML())
-	print ("Confusion matrix when compared with right side ME")
-	print (mat)
-	out.write('<h1> Substructure Confusion </h1>')
-	mat.toHTML(out)
-	out.write('<h1> Substructure Confusion with at least 1 error </h1>')
-	mat.toHTML(out,1)
-	out.write('<h1> Substructure Confusion at oject level with 1 error or more </h1>')
-	segMat.toHTML(out)
-	out.write('</html>')
-	out.close()
+    stat = SmGrConfMatrix.SmDict()
+    mat = SmGrConfMatrix.ConfMatrix()
+    segMat = SmGrConfMatrix.ConfMatrixObject()
+    for (fileGT, fileOUT, _) in files:
+        gGT = Lg(fileGT)
+        for s in gGT.subStructIterator([1, 2, 3, 4]):
+            stat.get(s, SmGrConfMatrix.Counter).incr()
+        gOUT = Lg(fileOUT)
+        for (gt, er) in gOUT.compareSubStruct(gGT, [2, 3]):
+            mat.incr(gt, er, ("../" + fileOUT))
+        for (seg, gt, er) in gOUT.compareSegmentsStruct(gGT, [2, 3]):
+            segMat.incr(seg, gt, er, ("../" + fileOUT))
+    print("stat from left side expressions:")
+    # print stat
+    print("generate HTML in test.html")
+    out = open("Tests/test.html", "w")
+    out.write('<html xmlns="http://www.w3.org/1999/xhtml">')
+    out.write("<h1> Substructure Stat </h1>")
+    out.write(stat.toHTML())
+    print("Confusion matrix when compared with right side ME")
+    print(mat)
+    out.write("<h1> Substructure Confusion </h1>")
+    mat.toHTML(out)
+    out.write("<h1> Substructure Confusion with at least 1 error </h1>")
+    mat.toHTML(out, 1)
+    out.write("<h1> Substructure Confusion at oject level with 1 error or more </h1>")
+    segMat.toHTML(out)
+    out.write("</html>")
+    out.close()
 
 
 def main():
-	validfiles = [ \
-			'Tests/infile1', \
-			'Tests/infile2', \
-			'Tests/infile3', \
-			'Tests/infile4', \
-			'Tests/infile5', \
-			'Tests/infile10'
-		]
-
-	shortCutFiles = [ \
-			('Tests/segment3','Tests/segment3sc'), \
-			('Tests/segment3old','Tests/segment3sc'), \
-			('Tests/segment1','Tests/segment1sc'), \
-			('Tests/segment1old','Tests/segment1sc'), \
-			('Tests/segment2','Tests/segment2sc')
-		]
-
-	invalidfiles = [ \
-			'Tests/infile6', \
-			'Tests/infile7', \
-			'Tests/infile8', \
-			'Tests/infile9'
-		]
-
-	compareFiles = [ \
-		#only errors with labels
-			('Tests/infile1','Tests/infile1a', {'D_C':1, 'D_S':0, 'D_L':0}), \
-			('Tests/infile4','Tests/infile4a', {'D_C':1, 'D_S':0, 'D_L':1})
-			#('Tests/infile4','Tests/infile4b', {'D_C':2, 'D_S':0, 'D_L':0})
-			# only errors with seg and layout
-			#('Tests/segment6','Tests/segment6erra'),\
-			#('Tests/segment6','Tests/segment6errb'),\
-			#('Tests/segment5','Tests/segment5erra'),\
-			#('Tests/segment5','Tests/segment5errb')
-			#('Tests/res_001-equation006.lg','Tests/001-equation006.lg')
-		]
-	compareFilesMulti = [ \
-		#only errors with labels
-			('Tests/multiLab0','Tests/multiLab0a', {'D_C':1, 'D_S':0, 'D_L':0,'corrSeg':1}), \
-			('Tests/multiLab1','Tests/multiLab1a', {'D_C':0, 'D_S':2, 'D_L':2,'corrSeg':2}),\
-			('Tests/multiLab2','Tests/multiLab2a', {'D_C':0, 'D_S':4, 'D_L':4,'corrSeg':2}),\
-			('Tests/multiLab2','Tests/multiLab2b', {'D_C':2, 'D_S':6, 'D_L':7,'corrSeg':1}),\
-			('Tests/102_em_39_VO.lg','Tests/102_em_39_GT.lg', {'D_C':3, 'D_S':14, 'D_L':18,'corrSeg':4}),\
-			('Tests/6_em_5_UPV.lg','Tests/6_em_5_GT.lg', {'D_C':0, 'D_S':0, 'D_L':0,'corrSeg':11})\
-				]
-
-
-	segFiles = [ \
-			('Tests/infile1',{'nbSeg':1, 'nbSegEd':0}), \
-			('Tests/infile4',{'nbSeg':4, 'nbSegEd':6}), \
-			('Tests/infile5',{'nbSeg':4, 'nbSegEd':6}), \
-			('Tests/segment1',{'nbSeg':2, 'nbSegEd':1}), \
-			('Tests/segment2',{'nbSeg':3, 'nbSegEd':3}), \
-			('Tests/segment3',{'nbSeg':2, 'nbSegEd':0}), \
-			('Tests/segment4',{'nbSeg':2, 'nbSegEd':0}), \
-			('Tests/segment5',{'nbSeg':3, 'nbSegEd':2}), \
-			('Tests/segment6',{'nbSeg':3, 'nbSegEd':2}), \
-			('Tests/multiLab0',{'nbSeg':1, 'nbSegEd':0}), \
-			('Tests/multiLab1',{'nbSeg':2, 'nbSegEd':1}), \
-			('Tests/multiLab2',{'nbSeg':3, 'nbSegEd':1})
-		]
-
-	compareFilespaper = [ \
-			('Tests/paperExampleGT','Tests/paperExampleErrA'), \
-			('Tests/paperExampleGT','Tests/paperExampleErrB'), \
-			('Tests/paperExampleGT','Tests/paperExampleErrC'), \
-			('Tests/paperExampleGT','Tests/paperExampleErrD')
-		]
-
-	compareEmpty = [ \
-			('Tests/infile1','Tests/emptyfile'),
-			('Tests/infile11','Tests/emptyfile'),
-			('Tests/infile1','Tests/infile11'),
-			('Tests/infile1','Tests/infile3'),
-			('Tests/emptyfile','Tests/paperExampleGT'),
-		]
-
-	mergeFiles = [ \
-			('Tests/infile1','Tests/infile1'),
-			('Tests/infile1','Tests/infile11'),
-			('Tests/infile4','Tests/infile4a'),
-			('Tests/infile4','Tests/infile4b'),
-			('Tests/infile1', 'invalidfile')
-		]
-		
-	filesForBestBG = [ \
-			'Tests/infile4', \
-			'Tests/infile5', \
-			'Tests/infile5b'
-		]
-	# Input file tests.	
-	testInput(validfiles)
-	#testInvalidFiles(invalidfiles)
-
-	# Segmentation tests.
-	#testSegments(segFiles)
-	testshortCuts(shortCutFiles)
-	#Comparison tests.
-	#testLabelComparisons(compareFiles)
-	testLabelComparisons(compareFilesMulti)
-	#testLabelComparisons(compareFilespaper)
-	#testEmpty(compareEmpty)
-	#testStructCompare([('Tests/2p2.lg','Tests/2p2a.lg')])
-	#testSubGraphCounting(compareFiles) #[('Tests/2p2.lg','Tests/2p2a.lg')])
-	# Extracting trees (layout trees)
-	# testTreeEdges(segFiles)
-
-	# Merging label graphs
-	#testSummingGraphs( mergeFiles )
-	#testMaxLabel( mergeFiles )
-	
-	# generate all best BG
-	#testGenAllBG(filesForBestBG)
-
-	#testInvertValues( validfiles + [ 'Tests/invalidEdgeValue' ] )
+    validfiles = [
+        "Tests/infile1",
+        "Tests/infile2",
+        "Tests/infile3",
+        "Tests/infile4",
+        "Tests/infile5",
+        "Tests/infile10",
+    ]
+
+    shortCutFiles = [
+        ("Tests/segment3", "Tests/segment3sc"),
+        ("Tests/segment3old", "Tests/segment3sc"),
+        ("Tests/segment1", "Tests/segment1sc"),
+        ("Tests/segment1old", "Tests/segment1sc"),
+        ("Tests/segment2", "Tests/segment2sc"),
+    ]
+
+    invalidfiles = ["Tests/infile6", "Tests/infile7", "Tests/infile8", "Tests/infile9"]
+
+    compareFiles = [  # only errors with labels
+        ("Tests/infile1", "Tests/infile1a", {"D_C": 1, "D_S": 0, "D_L": 0}),
+        ("Tests/infile4", "Tests/infile4a", {"D_C": 1, "D_S": 0, "D_L": 1})
+        # ('Tests/infile4','Tests/infile4b', {'D_C':2, 'D_S':0, 'D_L':0})
+        # only errors with seg and layout
+        # ('Tests/segment6','Tests/segment6erra'),\
+        # ('Tests/segment6','Tests/segment6errb'),\
+        # ('Tests/segment5','Tests/segment5erra'),\
+        # ('Tests/segment5','Tests/segment5errb')
+        # ('Tests/res_001-equation006.lg','Tests/001-equation006.lg')
+    ]
+    compareFilesMulti = [  # only errors with labels
+        (
+            "Tests/multiLab0",
+            "Tests/multiLab0a",
+            {"D_C": 1, "D_S": 0, "D_L": 0, "corrSeg": 1},
+        ),
+        (
+            "Tests/multiLab1",
+            "Tests/multiLab1a",
+            {"D_C": 0, "D_S": 2, "D_L": 2, "corrSeg": 2},
+        ),
+        (
+            "Tests/multiLab2",
+            "Tests/multiLab2a",
+            {"D_C": 0, "D_S": 4, "D_L": 4, "corrSeg": 2},
+        ),
+        (
+            "Tests/multiLab2",
+            "Tests/multiLab2b",
+            {"D_C": 2, "D_S": 6, "D_L": 7, "corrSeg": 1},
+        ),
+        (
+            "Tests/102_em_39_VO.lg",
+            "Tests/102_em_39_GT.lg",
+            {"D_C": 3, "D_S": 14, "D_L": 18, "corrSeg": 4},
+        ),
+        (
+            "Tests/6_em_5_UPV.lg",
+            "Tests/6_em_5_GT.lg",
+            {"D_C": 0, "D_S": 0, "D_L": 0, "corrSeg": 11},
+        ),
+    ]
+
+    segFiles = [
+        ("Tests/infile1", {"nbSeg": 1, "nbSegEd": 0}),
+        ("Tests/infile4", {"nbSeg": 4, "nbSegEd": 6}),
+        ("Tests/infile5", {"nbSeg": 4, "nbSegEd": 6}),
+        ("Tests/segment1", {"nbSeg": 2, "nbSegEd": 1}),
+        ("Tests/segment2", {"nbSeg": 3, "nbSegEd": 3}),
+        ("Tests/segment3", {"nbSeg": 2, "nbSegEd": 0}),
+        ("Tests/segment4", {"nbSeg": 2, "nbSegEd": 0}),
+        ("Tests/segment5", {"nbSeg": 3, "nbSegEd": 2}),
+        ("Tests/segment6", {"nbSeg": 3, "nbSegEd": 2}),
+        ("Tests/multiLab0", {"nbSeg": 1, "nbSegEd": 0}),
+        ("Tests/multiLab1", {"nbSeg": 2, "nbSegEd": 1}),
+        ("Tests/multiLab2", {"nbSeg": 3, "nbSegEd": 1}),
+    ]
+
+    compareFilespaper = [
+        ("Tests/paperExampleGT", "Tests/paperExampleErrA"),
+        ("Tests/paperExampleGT", "Tests/paperExampleErrB"),
+        ("Tests/paperExampleGT", "Tests/paperExampleErrC"),
+        ("Tests/paperExampleGT", "Tests/paperExampleErrD"),
+    ]
+
+    compareEmpty = [
+        ("Tests/infile1", "Tests/emptyfile"),
+        ("Tests/infile11", "Tests/emptyfile"),
+        ("Tests/infile1", "Tests/infile11"),
+        ("Tests/infile1", "Tests/infile3"),
+        ("Tests/emptyfile", "Tests/paperExampleGT"),
+    ]
+
+    mergeFiles = [
+        ("Tests/infile1", "Tests/infile1"),
+        ("Tests/infile1", "Tests/infile11"),
+        ("Tests/infile4", "Tests/infile4a"),
+        ("Tests/infile4", "Tests/infile4b"),
+        ("Tests/infile1", "invalidfile"),
+    ]
+
+    filesForBestBG = ["Tests/infile4", "Tests/infile5", "Tests/infile5b"]
+    # Input file tests.
+    testInput(validfiles)
+    # testInvalidFiles(invalidfiles)
+
+    # Segmentation tests.
+    # testSegments(segFiles)
+    testshortCuts(shortCutFiles)
+    # Comparison tests.
+    # testLabelComparisons(compareFiles)
+    testLabelComparisons(compareFilesMulti)
+    # testLabelComparisons(compareFilespaper)
+    # testEmpty(compareEmpty)
+    # testStructCompare([('Tests/2p2.lg','Tests/2p2a.lg')])
+    # testSubGraphCounting(compareFiles) #[('Tests/2p2.lg','Tests/2p2a.lg')])
+    # Extracting trees (layout trees)
+    # testTreeEdges(segFiles)
+
+    # Merging label graphs
+    # testSummingGraphs( mergeFiles )
+    # testMaxLabel( mergeFiles )
+
+    # generate all best BG
+    # testGenAllBG(filesForBestBG)
+
+    # testInvertValues( validfiles + [ 'Tests/invalidEdgeValue' ] )
+
 
 main()
-- 
GitLab