Пример #1
0
import sys
import csv
import hmm
import bayes_net
from collections import OrderedDict
import song_collections


models = [("Bayes Net", bayes_net.score)] + [("{0} Order HMM".format(i), hmm.get_scorer(i)) for i in range(2, 7)]
models = OrderedDict(models)

writer = csv.writer(sys.stdout)

header_row = ["File", "Type"]

for label, model in models.items():
    header_row.append(label)
    header_row.append(r"% diff from random")

writer.writerow(header_row)

random_model_values = []
for i in range(len(models)):
    random_model_values.append(list())

for song in song_collections.random_songs:
    row = [song, "random"]
    index = 0
    for model in models.values():
        value = model(song)
        random_model_values[index].append(value)
files_to_test = []
test_path = os.path.join("data", "Random Songs")
for root, dirs, files in os.walk(test_path):
	for name in [a_file for a_file in files if a_file[-4:] == ".mid"]:
		files_to_test.append(os.path.join(root,name))

log_path = "log"
model_scores = OrderedDict()

for hmm_depth in range(2,7):
	model_label = "HMM: %d Depth" % (hmm_depth,)
	print model_label

	cache = dict()
	model = hmm.get_scorer(hmm_depth, cache)
	scores_for_model = utils.score_files_with_model(files_to_test, model)
	num_scores = len(scores_for_model)
	total_scores = sum([score for score in scores_for_model.values() if score is not None])
	average_score = float(total_scores) / num_scores


	log_name = "HMM_" + str(hmm_depth) + "_random.txt"
	log_full_path = os.path.join(log_path, log_name);
	f = open(log_full_path, 'w')
	index = 1
	for score in scores_for_model.values():
		if score is not None:
			print >> f, "%d %f" % (index, score)
			index += 1
	f.close()