Skip to content

Commit ad0ffe1

Browse files
committedMay 12, 2015
Init commit
1 parent a6d5a82 commit ad0ffe1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+10257830
-0
lines changed
 

‎README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Required python 2.7 modules:
2+
- IPython
3+
- numpy
4+
- matplotlib
5+
- sklearn
6+
- skimage
7+
8+
HOW TO RUN:
9+
-----------
10+
11+
Run cmm.py
12+
>> python cmm.py
13+
14+
15+
Configuring: (scroll towards the end of cmm.py)
16+
Edit pieces in cmm.py to select source pieces:
17+
pieces = ["mid/hilarity.mid", "mid/froglegs.mid", "mid/easywinners.mid"]
18+
19+
Toggle between two modes: mixture or segmentation (I haven't had a chance to figure out a way to combine the two):
20+
segmentation = True
21+
all_keys = False
22+
23+
24+
NOTES:
25+
------
26+
27+
- The cached folder holds cached computations. Delete them if they are costing you problems. Note that recalculating stuff will take a while.
28+
29+
- All midi files in mid are properly quantized (i.e. each note type has a regular number of ticks). The music generation system requires that additional midi files must also be properly quantized to function properly.
30+

‎analyze.py

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
'''
2+
analyze.py
3+
4+
This file sets up infrastructure for the similarity measure using a classifier
5+
that compares two music segments and gives a score between 0 and 1
6+
7+
'''
8+
9+
from sklearn import datasets, neighbors, linear_model, svm
10+
from sklearn.metrics import confusion_matrix
11+
import numpy as np
12+
import random
13+
from similar_sections import ss
14+
import sys
15+
16+
class train_set(object):
17+
def __init__(self, data, target):
18+
self.data = data
19+
self.target = target
20+
21+
def generate():
22+
gen = ss.generate_targets_subset()
23+
random.shuffle(gen)
24+
target = np.array([ v[2] for v in gen ])
25+
data = np.array([ f[0].compare_with(f[1]) for f in gen ])
26+
return train_set(data, target)
27+
28+
def train_classifier(sdata, classifier=None):
29+
digits = sdata
30+
31+
X_digits = digits.data
32+
y_digits = digits.target
33+
34+
n_samples = len(X_digits)
35+
36+
# data
37+
X_train = X_digits[:]
38+
y_train = y_digits[:]
39+
40+
if not classifier:
41+
#classifier = svm.NuSVC(nu=0.01, probability=True)
42+
#classifier = linear_model.RidgeClassifierCV()
43+
classifier = linear_model.LogisticRegression(C=3.0)
44+
45+
classifier_fit = classifier.fit(X_train, y_train)
46+
return classifier_fit
47+
48+
49+
def test(sdata, classifier=None, verbose=True, verboseverbose=False):
50+
digits = sdata
51+
52+
X_digits = digits.data
53+
y_digits = digits.target
54+
55+
n_samples = len(X_digits)
56+
57+
# data
58+
X_train = X_digits[:.85 * n_samples]
59+
y_train = y_digits[:.85 * n_samples]
60+
61+
# truths/target
62+
X_test = X_digits[.85 * n_samples:]
63+
y_test = y_digits[.85 * n_samples:]
64+
65+
if not classifier:
66+
classifier = linear_model.RidgeClassifierCV()
67+
68+
classifier_fit = classifier.fit(X_train, y_train)
69+
70+
pred = classifier_fit.predict(X_test)
71+
score = classifier_fit.score(X_test, y_test)
72+
73+
if verboseverbose:
74+
# print the matrix of feature scores
75+
big_matrix = np.array([ np.hstack((X_test[i], y_test[i])) for i in xrange(len(X_test)) ])
76+
print ['Tr0Rhyt','Tr0TopL','Tr1Rhyt','Tr1TopL','Truth']
77+
print big_matrix
78+
if verbose:
79+
print 'TRUTH:', y_test
80+
print 'PREDN:', pred
81+
print ('Classifier score: %f' % score)
82+
83+
return score, pred, y_test
84+
85+
def evaluate_n(n, sdata, classifier):
86+
avg_score = 0.0
87+
pred_overall, y_test_overall = np.array([]), np.array([])
88+
for i in xrange(n):
89+
score, pred, y_test = test(sdata, classifier, verbose=False if n > 1 else True)
90+
avg_score += score / n
91+
pred_overall = np.hstack((pred_overall, pred))
92+
y_test_overall = np.hstack((y_test_overall, y_test))
93+
94+
sys.stdout.write("\r(Progress: %d/%d)" % (i, n))
95+
sys.stdout.flush()
96+
else:
97+
sys.stdout.write("\r")
98+
sys.stdout.flush()
99+
100+
print "---- Num of Repetitions:", n
101+
print "---- Average Score:", avg_score
102+
np.set_printoptions(linewidth=999999)
103+
print confusion_matrix(y_test_overall, pred_overall)
104+
105+
if __name__ == '__main__':
106+
107+
# three classifiers to choose from omgz
108+
svm = svm.NuSVC(nu=0.02)
109+
ridge = linear_model.RidgeClassifierCV()
110+
knn = neighbors.KNeighborsClassifier()
111+
lr = linear_model.LogisticRegression(C=10.0)
112+
113+
n = 40
114+
115+
if len(sys.argv) == 2:
116+
n = int(sys.argv[1])
117+
118+
evaluate_n(n, generate(), lr)

0 commit comments

Comments
 (0)