OUTPUT_EXPRESSION_PENALTY = 1
ORPHAN_GROB_PENALTY = 1
-THRESHOLD = 1.0
-inspect_max_count = 0
+options = None
+
+def shorten_string (s):
+ threshold = 15
+ if len (s) > 2*threshold:
+ s = s[:threshold] + '..' + s[-threshold:]
+ return s
def max_distance (x1, x2):
dist = 0.0
def read_signature_file (name):
print 'reading', name
- exp_str = ("[%s]" % open (name).read ())
- entries = safeeval.safe_eval (exp_str)
+
+ 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 ('#')]
grob_sigs = [GrobSignature (e) for e in entries]
sig = SystemSignature (grob_sigs)
################################################################
# different systems of a .ly file.
+def read_pipe (c):
+ print 'pipe' , c
+ return os.popen (c).read ()
+
+def system (c):
+ print 'system' , c
+ s = os.system (c)
+ if s :
+ raise Exception ("failed")
+ return
+
+def compare_png_images (old, new, dir):
+ def png_dims (f):
+ m = re.search ('([0-9]+) x ([0-9]+)', read_pipe ('file %s' % f))
+
+ return tuple (map (int, m.groups ()))
+
+ dest = os.path.join (dir, new.replace ('.png', '.compare.jpeg'))
+ try:
+ dims1 = png_dims (old)
+ dims2 = png_dims (new)
+ except AttributeError:
+ ## hmmm. what to do?
+ system ('touch %(dest)s' % locals ())
+ return
+
+ dims = (min (dims1[0], dims2[0]),
+ min (dims1[1], dims2[1]))
+
+ system ('convert -depth 8 -crop %dx%d+0+0 %s crop1.png' % (dims + (old,)))
+ system ('convert -depth 8 -crop %dx%d+0+0 %s crop2.png' % (dims + (new,)))
+
+ system ('compare -depth 8 crop1.png crop2.png diff.png')
+
+ system ("convert -depth 8 diff.png -blur 0x3 -negate -channel alpha,blue -type TrueColorMatte -fx 'intensity' matte.png")
+
+ system ("composite -quality 65 matte.png %(new)s %(dest)s" % locals ())
class FileLink:
+ def text_record_string (self):
+ return '%-30f %-20s\n' % (self.distance (),
+ self.name ())
+ def distance (self):
+ return 0.0
+
+ def name (self):
+ return ''
+
+ def link_files_for_html (self, old_dir, new_dir, dest_dir):
+ pass
+
+ def write_html_system_details (self, dir1, dir2, dest_dir):
+ pass
+
+ def html_record_string (self, old_dir, new_dir):
+ return ''
+
+class MidiFileLink (FileLink):
+ def get_midi (self, f):
+ s = open (f).read ()
+ s = re.sub ('LilyPond [0-9.]+', '', s)
+ return s
+
+ def __init__ (self, f1, f2):
+ self.files = (f1, f2)
+
+ s1 = self.get_midi (self.files[0])
+ s2 = self.get_midi (self.files[1])
+
+ self.same = (s1 == s2)
+
+ def name (self):
+ name = os.path.split (self.files[0])[1]
+ name = re.sub ('.midi', '', name)
+ return name
+
+ def distance (self):
+ ## todo: could use import MIDI to pinpoint
+ ## what & where changed.
+ if self.same:
+ return 0
+ else:
+ return 100;
+ def html_record_string (self, d1, d2):
+ return '''<tr>
+<td>
+%f
+</td>
+<td><tt>%s</tt></td>
+<td><tt>%s</tt></td>
+</tr>''' % ((self.distance(),) + self.files)
+
+class SignatureFileLink (FileLink):
def __init__ (self):
self.original_name = ''
self.base_names = ('','')
self.system_links = {}
self._distance = None
-
+ def name (self):
+ return self.original_name
+
def add_system_link (self, link, number):
self.system_links[number] = link
def calc_distance (self):
d = 0.0
+
+ orphan_distance = 0.0
for l in self.system_links.values ():
d = max (d, l.geometric_distance ())
- return d
+ orphan_distance += l.orphan_count ()
+
+ return d + orphan_distance
def distance (self):
if type (self._distance) != type (0.0):
return self._distance
- def text_record_string (self):
- return '%-30f %-20s\n' % (self.distance (),
- self.original_name)
-
def source_file (self):
for ext in ('.ly', '.ly.txt'):
if os.path.exists (self.base_names[1] + ext):
self.add_system_link (link, system_index[0])
def link_files_for_html (self, old_dir, new_dir, dest_dir):
- for ext in ('.png', '.ly'):
+ png_linked = [[], []]
+ for ext in ('.png', '.ly', '-page*png'):
+
for oldnew in (0,1):
- link_file (self.base_names[oldnew] + ext,
- dest_dir + '/' + self.base_names[oldnew] + ext)
-
+ for f in glob.glob (self.base_names[oldnew] + ext):
+ dst = dest_dir + '/' + f
+ link_file (f, dst)
+
+ if f.endswith ('.png'):
+ png_linked[oldnew].append (f)
+
+ if options.compare_images:
+ for (old,new) in zip (png_linked[0], png_linked[1]):
+ compare_png_images (old, new, dest_dir)
+
def html_record_string (self, old_dir, new_dir):
def img_cell (ly, img, name):
if not name:
</font>
</td>
''' % locals ()
-
- img_1 = self.base_names[0] + '.png'
- ly_1 = self.base_names[0] + '.ly'
- img_2 = self.base_names[1] + '.png'
- ly_2 = self.base_names[1] + '.ly'
+ def multi_img_cell (ly, imgs, name):
+ if not name:
+ name = 'source'
+ else:
+ name = '<tt>%s</tt>' % name
+
+ imgs_str = '\n'.join (['''<a href="%s">
+<img src="%s" style="border-style: none; max-width: 500px;">
+</a><br>''' % (img, img)
+ for img in imgs])
+
+
+ return '''
+<td align="center">
+%(imgs_str)s
+<font size="-2">(<a href="%(ly)s">%(name)s</a>)
+</font>
+</td>
+''' % locals ()
+
+
+
+ def cell (base, name):
+ pat = base + '-page*.png'
+ pages = glob.glob (pat)
+
+ if pages:
+ return multi_img_cell (base + '.ly', sorted (pages), name)
+ else:
+ return img_cell (base + '.ly', base + '.png', name)
+
+
html_2 = self.base_names[1] + '.html'
name = self.original_name
+
+ cell_1 = cell (self.base_names[0], name)
+ cell_2 = cell (self.base_names[1], name)
+ if options.compare_images:
+ cell_2 = cell_2.replace ('.png', '.compare.jpeg')
html_entry = '''
<tr>
%s
%s
</tr>
-''' % (self.distance (), html_2,
- img_cell (ly_1, img_1, name), img_cell (ly_2, img_2, name))
-
+''' % (self.distance (), html_2, cell_1, cell_2)
return html_entry
self.missing = []
self.added = []
self.file_links = {}
+
def compare_trees (self, dir1, dir2):
self.compare_directories (dir1, dir2)
self.compare_trees (d1, d2)
def compare_directories (self, dir1, dir2):
+ for ext in ['signature', 'midi']:
+ (paired, m1, m2) = paired_files (dir1, dir2, '*.' + ext)
- (paired, m1, m2) = paired_files (dir1, dir2, '*.signature')
-
- self.missing += [(dir1, m) for m in m1]
- self.added += [(dir2, m) for m in m2]
+ self.missing += [(dir1, m) for m in m1]
+ self.added += [(dir2, m) for m in m2]
- for p in paired:
- if (inspect_max_count
- and len (self.file_links) > inspect_max_count):
+ for p in paired:
+ if (options.max_count
+ and len (self.file_links) > options.max_count):
+
+ continue
- continue
-
- f2 = dir2 + '/' + p
- f1 = dir1 + '/' + p
- self.compare_files (f1, f2)
+ f2 = dir2 + '/' + p
+ f1 = dir1 + '/' + p
+ self.compare_files (f1, f2)
def compare_files (self, f1, f2):
+ if f1.endswith ('signature'):
+ self.compare_signature_files (f1, f2)
+ elif f1.endswith ('midi'):
+ self.compare_midi_files (f1, f2)
+
+ def compare_midi_files (self, f1, f2):
+ name = os.path.split (f1)[1]
+
+ file_link = MidiFileLink (f1, f2)
+ self.file_links[name] = file_link
+
+ def compare_signature_files (self, f1, f2):
name = os.path.split (f1)[1]
name = re.sub ('-[0-9]+.signature', '', name)
try:
file_link = self.file_links[name]
except KeyError:
- file_link = FileLink ()
+ file_link = SignatureFileLink ()
self.file_links[name] = file_link
- file_link.add_file_compare (f1,f2)
+ file_link.add_file_compare (f1, f2)
- def write_text_result_page (self, filename):
- print 'writing "%s"' % filename
+ def write_text_result_page (self, filename, threshold):
out = None
if filename == '':
out = sys.stdout
else:
+ print 'writing "%s"' % filename
out = open_write_file (filename)
## todo: support more scores.
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):
- self.write_text_result_page (dest_dir + '/index.txt')
+ def create_text_result_page (self, dir1, dir2, dest_dir, threshold):
+ self.write_text_result_page (dest_dir + '/index.txt', threshold)
- def create_html_result_page (self, dir1, dir2, dest_dir):
+ def create_html_result_page (self, dir1, dir2, dest_dir, threshold):
dir1 = dir1.replace ('//', '/')
dir2 = dir2.replace ('//', '/')
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)
link.link_files_for_html (dir1, dir2, dest_dir)
+ link.write_html_system_details (dir1, dir2, dest_dir)
+
html += link.html_record_string (dir1, dir2)
+ short_dir1 = shorten_string (dir1)
+ short_dir2 = shorten_string (dir2)
html = '''<html>
<table rules="rows" border bordercolor="blue">
<tr>
<th>distance</th>
-<th>old</th>
-<th>new</th>
+<th>%(short_dir1)s</th>
+<th>%(short_dir2)s</th>
</tr>
%(html)s
</table>
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)
dest_file = dest_dir + '/index.html'
open_write_file (dest_file).write (html)
- def print_results (self):
- self.write_text_result_page ('')
-
+ def print_results (self, threshold):
+ self.write_text_result_page ('', threshold)
-def compare_trees (dir1, dir2, dest_dir):
+def compare_trees (dir1, dir2, dest_dir, threshold):
data = ComparisonData ()
data.compare_trees (dir1, dir2)
- data.print_results ()
+ data.print_results (threshold)
if os.path.isdir (dest_dir):
system ('rm -rf %s '% dest_dir)
- data.create_html_result_page (dir1, dir2, dest_dir)
- data.create_text_result_page (dir1, dir2, dest_dir)
+ data.create_html_result_page (dir1, dir2, dest_dir, threshold)
+ data.create_text_result_page (dir1, dir2, dest_dir, threshold)
################################################################
# TESTING
def link_file (x, y):
mkdir (os.path.split (y)[0])
- os.link (x, y)
+ try:
+ os.link (x, y)
+ except OSError, z:
+ print 'OSError', x, y, z
+ raise OSError
def open_write_file (x):
d = os.path.split (x)[0]
system ('cp 19-1.signature 19-sub-1.signature')
system ('cp 19.ly 19-sub.ly')
system ('cp 19.png 19-sub.png')
+
+ system ('cp 20multipage* dir1')
+ system ('cp 20multipage* dir2')
+ system ('cp 19multipage-1.signature dir2/20multipage-1.signature')
+
system ('mkdir -p dir1/subdir/ dir2/subdir/')
system ('cp 19-sub{-*.signature,.ly,.png} dir1/subdir/')
## introduce differences
system ('cp 19-1.signature dir2/20-1.signature')
+ system ('cp 19.png dir2/20.png')
+ system ('cp 19multipage-page1.png dir2/20multipage-page1.png')
system ('cp 20-1.signature dir2/subdir/19-sub-1.signature')
+ system ('cp 20.png dir2/subdir/19-sub.png')
## radical diffs.
system ('cp 19-1.signature dir2/20grob-1.signature')
system ('cp 19-1.signature dir2/20grob-2.signature')
+ system ('cp 19multipage.midi dir1/midi-differ.midi')
+ system ('cp 20multipage.midi dir2/midi-differ.midi')
- compare_trees ('dir1', 'dir2', 'compare-dir1dir2')
+ compare_trees ('dir1', 'dir2', 'compare-dir1dir2', 0.5)
def test_basic_compare ():
- ly_template = r"""#(set! toplevel-score-handler print-score-with-defaults)
-#(set! toplevel-music-handler
- (lambda (p m)
- (if (not (eq? (ly:music-property m 'void) #t))
- (print-score-with-defaults
- p (scorify-music m p)))))
+ ly_template = r"""
-\sourcefilename "my-source.ly"
+\version "2.10.0"
+#(set! toplevel-score-handler print-score-with-defaults)
+ #(set! toplevel-music-handler
+ (lambda (p m)
+ (if (not (eq? (ly:music-property m 'void) #t))
+ (print-score-with-defaults
+ p (scorify-music m p)))))
+\sourcefilename "my-source.ly"
+
%(papermod)s
+\header { tagline = ##f }
+\score {
<<
\new Staff \relative c {
c4^"%(userstring)s" %(extragrob)s
c4^"%(userstring)s" %(extragrob)s
}
>>
+\layout{}
+}
+
"""
dicts = [{ 'papermod' : '',
{ 'papermod' : '',
'name' : '20grob',
'extragrob': 'r2. \\break c1',
- 'userstring': 'test' }
-
+ 'userstring': 'test' },
]
for d in dicts:
system ('lilypond -ddump-signatures --png -b eps ' + ' '.join (names))
- sigs = dict ((n, read_signature_file ('%s-1.signature' % n)) for n in names)
+
+ multipage_str = r'''
+ #(set-default-paper-size "a6")
+ \score {
+ \relative {c1 \pageBreak c1 }
+ \layout {}
+ \midi {}
+ }
+ '''
+
+ open ('20multipage', 'w').write (multipage_str.replace ('c1', 'd1'))
+ open ('19multipage', 'w').write ('#(set-global-staff-size 19.5)\n' + multipage_str)
+ system ('lilypond -ddump-signatures --png 19multipage 20multipage ')
+
+ 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
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 ()
def run_tests ():
- dir = 'output-distance-test'
+ dir = 'test-output-distance'
do_clean = not os.path.exists (dir)
p = optparse.OptionParser ("output-distance - compare LilyPond formatting runs")
p.usage = 'output-distance.py [options] tree1 tree2'
- p.add_option ('', '--test',
+ p.add_option ('', '--test-self',
dest="run_test",
action="store_true",
help='run test method')
+
p.add_option ('--max-count',
dest="max_count",
metavar="COUNT",
default=0,
action="store",
help='only analyze COUNT signature pairs')
-
- (o,a) = p.parse_args ()
- if o.run_test:
+ p.add_option ('', '--threshold',
+ dest="threshold",
+ default=0.3,
+ action="store",
+ type="float",
+ help='threshold for geometric distance')
+
+ p.add_option ('--no-compare-images',
+ dest="compare_images",
+ default=True,
+ action="store_false",
+ help="Don't run graphical comparisons")
+
+ p.add_option ('-o', '--output-dir',
+ dest="output_dir",
+ default=None,
+ action="store",
+ type="string",
+ help='where to put the test results [tree2/compare-tree1tree2]')
+
+ global options
+ (options, a) = p.parse_args ()
+
+ if options.run_test:
run_tests ()
sys.exit (0)
p.print_usage()
sys.exit (2)
- global inspect_max_count
- inspect_max_count = o.max_count
-
- compare_trees (a[0], a[1], a[1] + '/' + a[0])
+ name = options.output_dir
+ if not name:
+ name = a[0].replace ('/', '')
+ name = os.path.join (a[1], 'compare-' + shorten_string (name))
+
+ compare_trees (a[0], a[1], name, options.threshold)
if __name__ == '__main__':
main()