|
|
|
|
|
|
|
|
""" |
|
|
Created on Mon Feb 5 14:02:31 2024 |
|
|
@author: jacquema |
|
|
|
|
|
Evaluation of the score of the Fake Real Book dataset |
|
|
""" |
|
|
|
|
|
|
|
|
import sys |
|
|
import logging |
|
|
|
|
|
sys.path.append('/Users/xavriley/Projects/pse/lib') |
|
|
import pse |
|
|
|
|
|
import os |
|
|
from pathlib import Path, PosixPath |
|
|
from datetime import datetime |
|
|
import re |
|
|
from operator import itemgetter, attrgetter |
|
|
import pandas |
|
|
import music21 as m21 |
|
|
import PSeval as ps |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_dataset_root = '/Users/xavriley/Dropbox/PhD/Datasets/FiloBass ISMIR Publication/musicxml/' |
|
|
|
|
|
|
|
|
_score_suffix = '.xml' |
|
|
|
|
|
|
|
|
_eval_root = '../../PSeval' |
|
|
|
|
|
Path(_eval_root).mkdir(parents=True, exist_ok=True) |
|
|
|
|
|
|
|
|
_output_dir = 'augASAP' |
|
|
|
|
|
timestamp = str(datetime.today().strftime('%Y%m%d-%H%M')) |
|
|
|
|
|
|
|
|
_mscore = '/Applications/MuseScore 4.app/Contents/MacOS/mscore' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def FiloBassCorpus(corpus): |
|
|
"""build a list of scores in a subdirectory of FiloBass""" |
|
|
global _dataset_root |
|
|
global _score_suffix |
|
|
dataset_path = Path(_dataset_root) |
|
|
assert isinstance(dataset_path, PosixPath) |
|
|
if not os.path.exists(dataset_path): |
|
|
print(dataset_path, 'not found') |
|
|
return |
|
|
|
|
|
dataset = dict() |
|
|
files = os.listdir(dataset_path) |
|
|
for file in files: |
|
|
filepath = dataset_path/file |
|
|
|
|
|
if os.path.isdir(filepath): |
|
|
continue |
|
|
|
|
|
if (os.path.splitext(file)[1] == _score_suffix): |
|
|
|
|
|
dataset[os.path.splitext(file)[0]] = filepath |
|
|
|
|
|
dataset = dict(sorted(dataset.items())) |
|
|
return dataset |
|
|
|
|
|
def accids(ks, notes): |
|
|
c = 0 |
|
|
for note in notes: |
|
|
if note.pitch.accidental != ks.accidentalByStep(note.name): |
|
|
c += 1 |
|
|
return c |
|
|
|
|
|
def FiloBass_table(corpus='leads'): |
|
|
assert(corpus == 'leads' or corpus == 'piano') |
|
|
table = [] |
|
|
dataset = FiloBassCorpus(corpus) |
|
|
names = sorted(list(dataset)) |
|
|
for name in names: |
|
|
if (dataset.get(name) == None): |
|
|
print(name, "not found in dataset", corpus) |
|
|
continue |
|
|
file = dataset[name] |
|
|
score = m21.converter.parse(file.as_posix()) |
|
|
assert(len(score.parts) > 0) |
|
|
part = score.parts[0] |
|
|
fpart = part.flatten() |
|
|
keys = fpart.getElementsByClass([m21.key.Key, m21.key.KeySignature]) |
|
|
notes = fpart.getElementsByClass(m21.note.Note) |
|
|
row = [] |
|
|
row.append(name) |
|
|
row.append(keys[0].sharps if len(keys) > 0 else None) |
|
|
row.append(len(part.getElementsByClass(m21.stream.Measure))) |
|
|
row.append(len(notes)) |
|
|
row.append(accids(keys[0], notes) if len(keys) > 0 else None) |
|
|
row.append(len(score.parts)) |
|
|
row.append(len(keys)) |
|
|
table.append(row) |
|
|
df = pandas.DataFrame(table) |
|
|
df.columns = ['name', 'KS','# bars', '# notes', '# accids', '# parts', '# keys'] |
|
|
df['KS'] = df['KS'].map('{:n}'.format) |
|
|
return df |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
skip = ['All-the-Things-You-Are', |
|
|
'Alone-Together', |
|
|
'Apple-Jump',] |
|
|
|
|
|
def eval_FiloBass(corpus='leads', algo=ps.pse.Algo_PSE, |
|
|
tons=104, kpre=33, kpost=23, |
|
|
output_dir='', filename='', |
|
|
debug=True, mark=True): |
|
|
global _eval_root |
|
|
assert(corpus == 'leads' or corpus == 'piano') |
|
|
timestamp = datetime.today().strftime('%Y%m%d-%H%M') |
|
|
|
|
|
if output_dir == '': |
|
|
output_dir = timestamp |
|
|
output_path = Path(_eval_root)/'evalFiloBass'/output_dir |
|
|
if not os.path.isdir(output_path): |
|
|
if not os.path.isdir(Path(_eval_root)/'evalFiloBass'): |
|
|
os.mkdir(Path(_eval_root)/'evalFiloBass') |
|
|
os.mkdir(output_path) |
|
|
else: |
|
|
print('WARNING: dir', output_path, 'exists') |
|
|
stat = ps.Stats() |
|
|
dataset = FiloBassCorpus(corpus) |
|
|
names = sorted(list(dataset)) |
|
|
print('\n', 'starting evaluation of FiloBass dataset -', len(names), 'entries\n') |
|
|
for name in names: |
|
|
if (name in skip): |
|
|
print('\n', name, 'SKIP\n') |
|
|
continue |
|
|
if (dataset.get(name) == None): |
|
|
print(name, "not found in dataset", corpus) |
|
|
continue |
|
|
file = dataset[name] |
|
|
print('\n', name, '\n') |
|
|
s = m21.converter.parse(file.as_posix()) |
|
|
(ls, lld) = ps.eval_score(score=s, stat=stat, |
|
|
sid=0, title=name, composer='', |
|
|
algo=algo, |
|
|
nbtons=tons, |
|
|
kpre=kpre, kpost=kpost, |
|
|
debug=debug, mark=mark) |
|
|
if mark and not ps.empty_difflist(lld): |
|
|
write_score(s, output_path, name) |
|
|
|
|
|
|
|
|
if filename == '': |
|
|
filename = 'FRWeval'+'_'+corpus+str(tons)+'_'+timestamp |
|
|
stat.show() |
|
|
df = stat.get_dataframe() |
|
|
df.pop('part') |
|
|
df.to_csv(output_path/(filename+'.csv') , header=True, index=False) |
|
|
stat.write_datasum(output_path/(filename+'_sum.csv')) |
|
|
|
|
|
|
|
|
def eval_FiloBassitem(name, corpus='leads', algo=ps.pse.Algo_PSE, |
|
|
tons=104, kpre=33, kpost=23, dflag=True, mflag=True): |
|
|
assert(len(name) > 0) |
|
|
assert(corpus == 'leads' or corpus == 'piano') |
|
|
dataset = FiloBassCorpus(corpus) |
|
|
if (dataset.get(name) == None): |
|
|
print(name, "not found in dataset", corpus) |
|
|
return |
|
|
file = dataset[name] |
|
|
score = m21.converter.parse(file.as_posix()) |
|
|
stat = ps.Stats() |
|
|
|
|
|
|
|
|
|
|
|
(ls, lld) = ps.eval_score(score=score, stat=stat, |
|
|
sid=0, title=name, composer='', |
|
|
algo=algo, |
|
|
nbtons=tons, |
|
|
kpre=kpre, kpost=kpost, |
|
|
debug=dflag, mark=mflag) |
|
|
stat.show() |
|
|
assert(len(lld) == 1) |
|
|
if mflag and len(lld[0]) > 0: |
|
|
score.show() |
|
|
write_score(score, Path(os.getcwd()), name) |
|
|
|
|
|
def write_score(score, output_path, outname): |
|
|
if not os.path.isdir(output_path): |
|
|
os.mkdir(output_path) |
|
|
xmlfile = output_path/(outname+'.musicxml') |
|
|
score.write('musicxml', fp=xmlfile) |
|
|
|
|
|
def write_score2(score, output_path, outname): |
|
|
assert(len(outname) > 0) |
|
|
if not os.path.isdir(output_path): |
|
|
os.mkdir(output_path) |
|
|
output_path = output_path/outname |
|
|
if not os.path.isdir(output_path): |
|
|
os.mkdir(output_path) |
|
|
xmlfile = output_path/(outname+'.musicxml') |
|
|
score.write('musicxml', fp=xmlfile) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def debug(name, corpus='leads'): |
|
|
assert(len(name) > 0) |
|
|
dataset = FiloBassCorpus(corpus) |
|
|
if (dataset.get(name) == None): |
|
|
print(name, "not found in dataset", corpus) |
|
|
return |
|
|
file = dataset[name] |
|
|
score = m21.converter.parse(file) |
|
|
lp = score.getElementsByClass(m21.stream.Part) |
|
|
ln = ps.extract_part(lp[0]) |
|
|
for (n, b, s) in ln: |
|
|
a = 'sp.add(' |
|
|
a += str(n.pitch.midi) |
|
|
a += ', ' |
|
|
a += str(b) |
|
|
a += ', ' |
|
|
a += 'true' if s else 'false' |
|
|
a += ');' |
|
|
print(a) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__=="__main__": |
|
|
eval_FiloBass() |
|
|
|
|
|
|