]> git.donarmstrong.com Git - lilypond.git/blobdiff - buildscripts/output-distance.py
*** empty log message ***
[lilypond.git] / buildscripts / output-distance.py
index fcb8e3731c5bc1d7f931fa14bee65c5cc852184e..1ce5dca38f5c3b222c768130efe12c563acfc27a 100644 (file)
@@ -17,7 +17,7 @@ INFTY = 1e6
 
 OUTPUT_EXPRESSION_PENALTY = 1
 ORPHAN_GROB_PENALTY = 1
-inspect_max_count = 0
+THRESHOLD = 1.0
 
 def max_distance (x1, x2):
     dist = 0.0
@@ -78,10 +78,10 @@ class GrobSignature:
 
     def __repr__ (self):
         return '%s: (%.2f,%.2f), (%.2f,%.2f)\n' % (self.name,
-                                                   self.bbox[0][0],
-                                                   self.bbox[0][1],
-                                                   self.bbox[1][0],
-                                                   self.bbox[1][1])
+                                                 self.bbox[0][0],
+                                                 self.bbox[0][1],
+                                                 self.bbox[1][0],
+                                                 self.bbox[1][1])
                                                  
     def axis_centroid (self, axis):
         return apply (sum, self.bbox[axis])  / 2 
@@ -100,12 +100,9 @@ class GrobSignature:
         
     def expression_distance (self, other):
         if self.output_expression == other.output_expression:
-            return 0
+            return 0.0
         else:
-            return 1
-
-################################################################
-# single System.
+            return OUTPUT_EXPRESSION_PENALTY
 
 class SystemSignature:
     def __init__ (self, grob_sigs):
@@ -142,9 +139,6 @@ class SystemSignature:
     def grobs (self):
         return reduce (lambda x,y: x+y, self.grob_dict.values(), [])
 
-################################################################
-## comparison of systems.
-
 class SystemLink:
     def __init__ (self, system1, system2):
         self.system1 = system1
@@ -153,20 +147,6 @@ class SystemLink:
         self.link_list_dict = {}
         self.back_link_dict = {}
 
-
-        ## pairs
-        self.orphans = []
-
-        ## pair -> distance
-        self.geo_distances = {}
-
-        ## pairs
-        self.expression_changed = []
-
-        self._geometric_distance = None
-        self._expression_change_count = None
-        self._orphan_count = None
-        
         for g in system1.grobs ():
 
             ## skip empty bboxes.
@@ -179,96 +159,45 @@ class SystemLink:
             self.link_list_dict[closest].append (g)
             self.back_link_dict[g] = closest
 
-
-    def calc_geometric_distance (self):
-        total = 0.0
+    def geometric_distance (self):
+        d = 0.0
         for (g1,g2) in self.back_link_dict.items ():
             if g2:
-                d = g1.bbox_distance (g2)
-                if d:
-                    self.geo_distances[(g1,g2)] = d
-
-                total += d
+                # , scale
+                d += g1.bbox_distance (g2)
 
-        self._geometric_distance = total
+        return d
     
-    def calc_orphan_count (self):
-        count = 0
-        for (g1, g2) in self.back_link_dict.items ():
+    def orphan_distance (self):
+        d = 0
+        for (g1,g2) in self.back_link_dict.items ():
             if g2 == None:
-                self.orphans.append ((g1, None))
-                
-                count += 1
-
-        self._orphan_count = count
+                d += ORPHAN_GROB_PENALTY
+        return d
     
-    def calc_output_exp_distance (self):
+    def output_exp_distance (self):
         d = 0
         for (g1,g2) in self.back_link_dict.items ():
             if g2:
                 d += g1.expression_distance (g2)
 
-        self._expression_change_count = d
-
-    def output_expression_details_string (self):
-        return ', '.join ([g1.name for g1 in self.expression_changed])
-    
-    def geo_details_string (self):
-        results = [(d, g1,g2) for ((g1, g2), d) in self.geo_distances.items()]
-        results.sort ()
-        results.reverse ()
-        
-        return ', '.join (['%s: %f' % (g1.name, d) for (d, g1, g2) in results])
-
-    def orphan_details_string (self):
-        return ', '.join (['%s-None' % g1.name for (g1,g2) in self.orphans if g2==None])
+        return d
 
-    def geometric_distance (self):
-        if self._geometric_distance == None:
-            self.calc_geometric_distance ()
-        return self._geometric_distance
-    
-    def orphan_count (self):
-        if self._orphan_count == None:
-            self.calc_orphan_count ()
-            
-        return self._orphan_count
-    
-    def output_expression_change_count (self):
-        if self._expression_change_count == None:
-            self.calc_output_exp_distance ()
-        return self._expression_change_count
-        
     def distance (self):
-        return (self.output_expression_change_count (),
-                self.orphan_count (),
+        return (self.output_exp_distance (),
+                self.orphan_distance (),
                 self.geometric_distance ())
     
 def read_signature_file (name):
     print 'reading', name
-    
-    entries = open (name).read ().split ('\n')
-    def string_to_tup (s):
-        return tuple (map (float, s.split (' '))) 
-
-    def string_to_entry (s):
-        fields = s.split('@')
-        fields[2] = string_to_tup (fields[2])
-        fields[3] = string_to_tup (fields[3])
-
-        return tuple (fields)
-    
-    entries = [string_to_entry (e) for e in entries
-               if e and not e.startswith ('#')]
+    exp_str = ("[%s]" % open (name).read ())
+    entries = safeeval.safe_eval (exp_str)
 
     grob_sigs = [GrobSignature (e) for e in entries]
     sig = SystemSignature (grob_sigs)
     return sig
 
 
-################################################################
-# different systems of a .ly file.
-
 class FileLink:
     def __init__ (self):
         self.original_name = ''
@@ -395,19 +324,8 @@ class FileLink:
                 e += '<td>%f</td>' % d
             
             e = '<tr>%s</tr>' % e
-
             html += e
 
-            e = '<td>%d</td>' % c
-            for s in (link.output_expression_details_string (),
-                      link.orphan_details_string (),
-                      link.geo_details_string ()):
-                e += "<td>%s</td>" % s
-
-            
-            e = '<tr>%s</tr>' % e
-            html += e
-            
         original = self.original_name
         html = '''<html>
 <head>
@@ -478,7 +396,6 @@ class ComparisonData:
         self.missing = []
         self.added = []
         self.file_links = {}
-
     def compare_trees (self, dir1, dir2):
         self.compare_directories (dir1, dir2)
         
@@ -501,9 +418,7 @@ class ComparisonData:
         self.added += [(dir2, m) for m in m2] 
 
         for p in paired:
-            if (inspect_max_count
-                and len (self.file_links) > inspect_max_count):
-                
+            if len (self.file_links) > 10:
                 continue
             
             f2 = dir2 +  '/' + p
@@ -523,7 +438,7 @@ class ComparisonData:
 
         file_link.add_file_compare (f1,f2)
 
-    def write_text_result_page (self, filename, threshold):
+    def write_text_result_page (self, filename):
         print 'writing "%s"' % filename
         out = None
         if filename == '':
@@ -539,18 +454,18 @@ class ComparisonData:
 
         
         for (score, link) in results:
-            if score > threshold:
+            if score > THRESHOLD:
                 out.write (link.text_record_string ())
 
         out.write ('\n\n')
         out.write ('%d below threshold\n' % len ([1 for s,l  in results
-                                                    if threshold >=  s > 0.0]))
+                                                    if THRESHOLD >=  s > 0.0]))
         out.write ('%d unchanged\n' % len ([1 for (s,l) in results if s == 0.0]))
         
-    def create_text_result_page (self, dir1, dir2, dest_dir, threshold):
-        self.write_text_result_page (dest_dir + '/index.txt', threshold)
+    def create_text_result_page (self, dir1, dir2, dest_dir):
+        self.write_text_result_page (dest_dir + '/index.txt')
         
-    def create_html_result_page (self, dir1, dir2, dest_dir, threshold):
+    def create_html_result_page (self, dir1, dir2, dest_dir):
         dir1 = dir1.replace ('//', '/')
         dir2 = dir2.replace ('//', '/')
         
@@ -562,7 +477,7 @@ class ComparisonData:
         html = ''
         old_prefix = os.path.split (dir1)[1]
         for (score, link) in results:
-            if score <= threshold:
+            if score <= THRESHOLD:
                 continue
 
             link.write_html_system_details (dir1, dir2, dest_dir)
@@ -574,8 +489,8 @@ class ComparisonData:
 <table rules="rows" border bordercolor="blue">
 <tr>
 <th>distance</th>
-<th>%(dir1)s</th>
-<th>%(dir2)s</th>
+<th>old</th>
+<th>new</th>
 </tr>
 %(html)s
 </table>
@@ -583,7 +498,7 @@ class ComparisonData:
 
         html += ('<p>')
         below_count  =len ([1 for s,l  in results
-                            if threshold >=  s > 0.0])
+                         if THRESHOLD >=  s > 0.0])
 
         if below_count:
             html += ('<p>%d below threshold</p>' % below_count)
@@ -595,19 +510,20 @@ class ComparisonData:
         dest_file = dest_dir + '/index.html'
         open_write_file (dest_file).write (html)
         
-    def print_results (self, threshold):
-        self.write_text_result_page ('', threshold)
+    def print_results (self):
+        self.write_text_result_page ('')
+        
 
-def compare_trees (dir1, dir2, dest_dir, threshold):
+def compare_trees (dir1, dir2, dest_dir):
     data = ComparisonData ()
     data.compare_trees (dir1, dir2)
-    data.print_results (threshold)
+    data.print_results ()
 
     if os.path.isdir (dest_dir):
         system ('rm -rf %s '% dest_dir)
 
-    data.create_html_result_page (dir1, dir2, dest_dir, threshold)
-    data.create_text_result_page (dir1, dir2, dest_dir, threshold)
+    data.create_html_result_page (dir1, dir2, dest_dir)
+    data.create_text_result_page (dir1, dir2, dest_dir)
     
 ################################################################
 # TESTING
@@ -659,13 +575,12 @@ def test_compare_trees ():
 
     ## introduce differences
     system ('cp 19-1.signature dir2/20-1.signature')
-    system ('cp 20-1.signature dir2/subdir/19-sub-1.signature')
 
     ## radical diffs.
     system ('cp 19-1.signature dir2/20grob-1.signature')
     system ('cp 19-1.signature dir2/20grob-2.signature')
 
-    compare_trees ('dir1', 'dir2', 'compare-dir1dir2', 0.5)
+    compare_trees ('dir1', 'dir2', 'compare-dir1dir2')
 
 
 def test_basic_compare ():
@@ -714,37 +629,12 @@ def test_basic_compare ():
     names = [d['name'] for d in dicts]
     
     system ('lilypond -ddump-signatures --png -b eps ' + ' '.join (names))
-    test_compare_signatures (names)
     
-def test_compare_signatures (names, timing=False):
-
-    import time
-
-    times = 1
-    if timing:
-        times = 100
-
-    t0 = time.clock ()
-
-    count = 0
-    for t in range (0, times):
-        sigs = dict ((n, read_signature_file ('%s-1.signature' % n)) for n in names)
-        count += 1
-
-    if timing:
-        print 'elapsed', (time.clock() - t0)/count
-
-
-    t0 = time.clock ()
-    count = 0
+    sigs = dict ((n, read_signature_file ('%s-1.signature' % n)) for n in names)
     combinations = {}
     for (n1, s1) in sigs.items():
         for (n2, s2) in sigs.items():
             combinations['%s-%s' % (n1, n2)] = SystemLink (s1,s2).distance ()
-            count += 1
-
-    if timing:
-        print 'elapsed', (time.clock() - t0)/count
 
     results = combinations.items ()
     results.sort ()
@@ -758,10 +648,9 @@ def test_compare_signatures (names, timing=False):
 
 
 def run_tests ():
+    do_clean = 0
     dir = 'output-distance-test'
 
-    do_clean = not os.path.exists (dir)
-
     print 'test results in ', dir
     if do_clean:
         system ('rm -rf ' + dir)
@@ -780,25 +669,10 @@ def main ():
     p = optparse.OptionParser ("output-distance - compare LilyPond formatting runs")
     p.usage = 'output-distance.py [options] tree1 tree2'
     
-    p.add_option ('', '--test-self',
+    p.add_option ('', '--test',
                   dest="run_test",
                   action="store_true",
                   help='run test method')
-    
-    p.add_option ('--max-count',
-                  dest="max_count",
-                  metavar="COUNT",
-                  type="int",
-                  default=0, 
-                  action="store",
-                  help='only analyze COUNT signature pairs')
-
-    p.add_option ('', '--threshold',
-                  dest="threshold",
-                  default=0.3,
-                  action="store",
-                  type="float",
-                  help='threshold for geometric distance')
 
     (o,a) = p.parse_args ()
 
@@ -810,11 +684,7 @@ def main ():
         p.print_usage()
         sys.exit (2)
 
-    global inspect_max_count
-    inspect_max_count = o.max_count
-
-    compare_trees (a[0], a[1], os.path.join (a[1],  'compare-' +  a[0]),
-                   o.threshold)
+    compare_trees (a[0], a[1])
 
 if __name__ == '__main__':
     main()