]> git.donarmstrong.com Git - lilypond.git/blobdiff - buildscripts/output-distance.py
Fix some bugs in the dynamic engraver and PostScript backend
[lilypond.git] / buildscripts / output-distance.py
index 4a851a65ed007f38d2a6f04754ea1870518778a7..a19380f1f28b4c9cfbd4aad84749d89e3c733954 100644 (file)
@@ -1,12 +1,6 @@
 #!@TARGET_PYTHON@
 import sys
 import optparse
-import os
-
-## so we can call directly as buildscripts/output-distance.py
-me_path = os.path.abspath (os.path.split (sys.argv[0])[0])
-sys.path.insert (0, me_path + '/../python/')
-
 
 import safeeval
 
@@ -30,9 +24,6 @@ def max_distance (x1, x2):
 empty_interval = (INFTY, -INFTY)
 empty_bbox = (empty_interval, empty_interval)
 
-def interval_is_empty (i):
-    return i[0] > i[1]
-
 def interval_length (i):
     return max (i[1]-i[0], 0) 
     
@@ -44,10 +35,6 @@ def interval_intersect (i1, i2):
     return (max (i1[0], i2[0]),
             min (i1[1], i2[1]))
 
-def bbox_is_empty (b):
-    return (interval_is_empty (b[0])
-            or interval_is_empty (b[1]))
-
 def bbox_union (b1, b2):
     return (interval_union (b1[X_AXIS], b2[X_AXIS]),
             interval_union (b2[Y_AXIS], b2[Y_AXIS]))
@@ -103,6 +90,11 @@ class GrobSignature:
         else:
             return OUTPUT_EXPRESSION_PENALTY
 
+    def distance(self, other, max_distance):
+        return (self.expression_distance (other)
+                + self.centroid_distance (other, max_distance)
+                + self.bbox_distance (other))
+            
 class SystemSignature:
     def __init__ (self, grob_sigs):
         d = {}
@@ -147,46 +139,30 @@ class SystemLink:
         self.back_link_dict = {}
 
         for g in system1.grobs ():
-
-            ## skip empty bboxes.
-            if bbox_is_empty (g.bbox):
-                continue
-            
             closest = system2.closest (g.name, g.centroid)
             
             self.link_list_dict.setdefault (closest, [])
             self.link_list_dict[closest].append (g)
             self.back_link_dict[g] = closest
 
-    def geometric_distance (self):
+    def distance (self):
         d = 0.0
-        for (g1,g2) in self.back_link_dict.items ():
-            if g2:
-                # , scale
-                d += g1.bbox_distance (g2)
 
-        return d
-    
-    def orphan_distance (self):
-        d = 0.0
+        scale = max (bbox_diameter (self.system1.bbox),
+                     bbox_diameter (self.system2.bbox))
+                                      
         for (g1,g2) in self.back_link_dict.items ():
             if g2 == None:
                 d += ORPHAN_GROB_PENALTY
-        return d
-    
-    def output_exp_distance (self):
-        d = 0.0
-        for (g1,g2) in self.back_link_dict.items ():
-            if g2:
-                d += g1.expression_distance (g2)
+            else:
+                d += g1.distance (g2, scale)
+
+        for (g1,g2s) in self.link_list_dict.items ():
+            if len (g2s) != 1:
+                d += ORPHAN_GROB_PENALTY
 
         return d
 
-    def distance (self):
-        return (self.output_exp_distance (),
-                self.orphan_distance (),
-                self.geometric_distance ())
-    
 ################################################################
 # Files/directories
 
@@ -241,13 +217,10 @@ class ComparisonData:
     def compare_trees (self, dir1, dir2):
         self.compare_directories (dir1, dir2)
         
-        (root, dirs, files) = os.walk (dir1).next ()
+        (root, files, dirs) = os.walk (dir1).next ()
         for d in dirs:
             d1 = os.path.join (dir1, d)
             d2 = os.path.join (dir2, d)
-
-            if os.path.islink (d1) or os.path.islink (d2):
-                continue
             
             if os.path.isdir (d2):
                 self.compare_trees (d1, d2)
@@ -264,7 +237,7 @@ class ComparisonData:
             f1 = dir1 +  '/' + p
             distance = compare_signature_files (f1, f2)
             self.result_dict[f2] = (distance, f1)
-
+    
     def create_text_result_page (self, dir1, dir2):
         self.write_text_result_page (dir2 + '/' + os.path.split (dir1)[1] + '.txt')
         
@@ -275,13 +248,8 @@ class ComparisonData:
             out = sys.stdout
         else:
             out = open (filename, 'w')
-
-        ## todo: support more scores.
-        results = [(geo_score, oldfile, file)
-                   for (file, ((exp_score, orphan_score,
-                                geo_score), oldfile))
-                   in self.result_dict.items ()]
         
+        results = [(score, oldfile, file) for (file, (score, oldfile)) in self.result_dict.items ()]  
         results.sort ()
         results.reverse ()
 
@@ -302,35 +270,24 @@ class ComparisonData:
         
         threshold = 1.0
         
-        results = [(geo_score, oldfile, file) for (file, ((exp_score, orphan_score,
-                                                           geo_score), oldfile)) in self.result_dict.items ()
-                   if geo_score > threshold]
+        results = [(score, oldfile, file) for (file, (score, oldfile)) in self.result_dict.items ()
+                   if score > threshold]
 
         results.sort ()
         results.reverse ()
-        
+
         html = ''
         old_prefix = os.path.split (dir1)[1]
-
-        dest_dir = os.path.join (dir2, old_prefix)
-        shutil.rmtree  (dest_dir, ignore_errors=True)
-        os.mkdir (dest_dir)
-        for (score, oldfile, newfile) in results:
-            
-            old_base = re.sub ("-[0-9]+.signature", '', oldfile)
-            old_name = os.path.split (old_base)[1]
+        os.mkdir (dir2 + '/' + old_prefix)
+        for (score, oldfile, newfile) in  results:
+            old_base = re.sub ("-[0-9]+.signature", '', os.path.split (oldfile)[1])
             new_base = re.sub ("-[0-9]+.signature", '', newfile)
             
             for ext in 'png', 'ly':
-                src_file = old_base + '.' + ext
-                
-                if os.path.exists (src_file):
-                    shutil.copy2 (src_file, dest_dir)
-                else:
-                    print "warning: can't find", src_file
+                shutil.copy2 (old_base + '.' + ext, dir2 + '/' + old_prefix)
 
-            img_1 = os.path.join (old_prefix, old_name + '.png')
-            ly_1 = os.path.join (old_prefix, old_name + '.ly')
+            img_1 = os.path.join (old_prefix, old_base + '.png')
+            ly_1 = os.path.join (old_prefix, old_base + '.ly')
 
             img_2 = new_base.replace (dir2, '') + '.png'
             img_2 = re.sub ("^/*", '', img_2)
@@ -363,7 +320,7 @@ class ComparisonData:
             html += html_entry
 
         html = '''<html>
-<table rules="rows" border bordercolor="blue">
+<table>
 <tr>
 <th>distance</th>
 <th>old</th>
@@ -378,11 +335,11 @@ class ComparisonData:
         
 
 def compare_trees (dir1, dir2):
-    data = ComparisonData ()
+    data =  ComparisonData ()
     data.compare_trees (dir1, dir2)
     data.print_results ()
     data.create_html_result_page (dir1, dir2)
-#    data.create_text_result_page (dir1, dir2)
+    data.create_text_result_page (dir1, dir2)
     
 ################################################################
 # TESTING
@@ -425,14 +382,10 @@ def test_basic_compare ():
     p (scorify-music m p)))))
 
 %(papermod)s
-<<
-\new Staff \relative c {
-  c^"%(userstring)s" %(extragrob)s
-  }
-\new Staff \relative c {
+
+\relative c {
   c^"%(userstring)s" %(extragrob)s
   }
->>
 """
 
     dicts = [{ 'papermod' : '',