-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompute.py
More file actions
234 lines (149 loc) · 6.25 KB
/
compute.py
File metadata and controls
234 lines (149 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from scipy.stats import entropy
from sklearn.metrics import accuracy_score
import numpy as np
import pandas as pd
from itertools import chain
import plotly
import json
import plotly.graph_objs as go
import itertools
from utils import getPicklefile, read_OnlyTestData,read_OnlyTrainData,savePickle,mapping
currentCount = 0
def updateGraph(scores):
traces = []
colors = {'LogisticRegression': 'rgb(31, 119, 180)',
'SVC': 'rgb(255, 127, 14)',
'RandomForestClassifier': 'rgb(44, 160, 44)'}
for name,score in scores.iteritems():
prev_scores = getPicklefile(name+'_scores')
trace = go.Scatter(
x=[j for j in range(0, 420, 20)],
y=prev_scores,
mode='lines+markers',
name=name,
line=dict(
color=colors[name]
),
showlegend=True
)
traces.append(trace)
data = traces
graphJSON = json.dumps(data, cls=plotly.utils.PlotlyJSONEncoder)
return graphJSON
def generateOracleData(df_samples):
oracleSamples = []
savePickle(df_samples,'finalOracleSamples15')
for df in df_samples:
clfname = df['classifier'][0]
for clip_name in df['sample']:
instance = {'name': clip_name[-1], 'clip': 'static/datafiles/audiofiles/'+clip_name[-1]+'.mp3'}
oracleSamples.append(instance)
return oracleSamples
def setCurrentCount(value):
global currentCount
currentCount = value
def getCurrentCount():
return currentCount
def computeOracle(stepSampleCount):
# get test data of 50 instances
X_test_set, y_test_set = read_OnlyTestData(dropFileName=True, returnXy=True)
# get unlabelled samples total 430
unlabelled_samples_data = read_OnlyTrainData(dropFileName=False)
# get pickle files
LogRegression = getPicklefile('LogisticRegression')
RFClassifier = getPicklefile('RandomForestClassifier')
Svc = getPicklefile('SVC')
classifiers = {}
classifiers[type(LogRegression).__name__] = LogRegression
classifiers[type(RFClassifier).__name__] = RFClassifier
classifiers[type(Svc).__name__] = Svc
setCurrentCount(stepSampleCount)
print ("COUNT",stepSampleCount)
prevStepCount = 0
if(stepSampleCount == 40):
prevStepCount = 20
else:
prevStepCount = stepSampleCount - 20
samples = unlabelled_samples_data[prevStepCount:stepSampleCount]
samples_no_labels_song_name = samples.drop(['label','song name'], axis=1).values
samples_with_songnames = samples.drop('label', axis=1).values
scores = {}
dfs = []
for clfname, clf in classifiers.iteritems():
# test accuracy on constant test set
y_pred = clf.predict(X_test_set)
score = accuracy_score(y_pred, y_test_set)
# get probabilites on unlabelled samples for entropy
pred_probs = clf.predict_proba(samples_no_labels_song_name)
entropies_with_samples = []
for indx in range(len(samples_no_labels_song_name)):
entr = entropy(pred_probs[indx])
withEntropy = [clfname,samples_with_songnames[indx], entr]
entropies_with_samples.append(withEntropy)
dataf = pd.DataFrame(columns=['classifier','sample', 'entropy'],data=entropies_with_samples)
finalOracledf = dataf.sort_values(by='entropy',ascending=False)
dfs.append(finalOracledf)
scores[clfname] = score
# save scores in pickle
# for name, score in scores.iteritems():
# prev_scores = getPicklefile(name+"_scores")
# prev_scores.append(score)
# savePickle(prev_scores,name+'_scores')
# update the graph with old scores and new scores
# send samples to gui for labelling
return generateOracleData(dfs)
def trainModels(trainingData):
# get test data of 50 instances
X_test_set, y_test_set = read_OnlyTestData(dropFileName=True, returnXy=True)
# get pickle files
LogRegression = getPicklefile('LogisticRegression')
Svc = getPicklefile('SVC')
RFClassifier = getPicklefile('RandomForestClassifier')
classifiers = [LogRegression,Svc,RFClassifier]
scores = {}
for execNo in range(len(trainingData)):
tdata = trainingData[execNo]
tdata = mapping(tdata)
X = tdata.drop(['label','audio_name'],axis=1).values
y = tdata['label']
clf = classifiers[execNo]
clfname = type(clf).__name__
# retrain the model
clf.fit(X,y)
y_pred = clf.predict(X_test_set)
score = accuracy_score(y_pred, y_test_set)
scores[clfname] = score
# save models to pickle
savePickle(clf, clfname)
#get previous score and save it
oldscores = getPicklefile(clfname+'_scores')
oldscores.append(score)
#print "OLD",oldscores
# save scores to pickle
savePickle(oldscores, clfname + '_scores')
return scores
def train_withLabelledSamples(labelledSamples):
finalOracleSamples15 = getPicklefile('finalOracleSamples15')
addedLabelsDFs = []
for inNum in range(len(finalOracleSamples15)):
Modsamples = finalOracleSamples15[inNum]
clfname = Modsamples['classifier'][0]
labSamples = labelledSamples[inNum]
for samp in labSamples:
aname,value = samp.items()[0]
for num in range(len(Modsamples['sample'])):
if(aname in Modsamples['sample'][num]):
Modsamples.loc[num,'label'] = value
Modsamples = Modsamples.replace(to_replace='None', value=np.nan).dropna()
Modsamples = Modsamples.drop(['classifier', 'entropy'], axis=1).values
flatArray = []
for sample,label in Modsamples:
sample = np.append(sample,label)
flatArray.append(sample)
trainingDF = pd.DataFrame(columns=['meanfreq', 'sd', 'median', 'Q25', 'Q75', 'IQR', 'skew', 'kurt', 'sp.ent',
'sfm', 'mode', 'centroid', 'meanfun', 'minfun', 'maxfun',
'meandom', 'mindom', 'maxdom',
'dfrange', 'modindx', 'audio_name','label'],data=flatArray)
addedLabelsDFs.append(trainingDF)
# finally send data to models for training
return trainModels(addedLabelsDFs)