Skip to content

Commit

Permalink
some trash
Browse files Browse the repository at this point in the history
  • Loading branch information
Cour committed Apr 13, 2018
1 parent 1312d9d commit e3e6681
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 12 deletions.
6 changes: 5 additions & 1 deletion audio_features.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import numpy as np
import nolds

def a(d):
return np.array(d)

def IEMG(data):
return np.sum(np.square(data.astype(np.float)))

Expand Down Expand Up @@ -108,4 +111,5 @@ def get_features_from_stream(

return a([ f(strea) for f in fncs])


def get_ff():
return ["MAV", "MMAV1", "MMAV2", "SSI", "VAR", "RMS", "WL", "ZC", "SSC", "WAMP", "STDDEV"]
60 changes: 59 additions & 1 deletion audiphil.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

from collections import defaultdict
import audio_features as fea
import numpy as np
import itertools
import file_processing as iop
import ml_au as ml
import scikits.audiolab
import matplotlib.pyplot as plt

def split_into_channels(sig, num_chan=2):
outputs = defaultdict(list)
Expand All @@ -14,6 +18,60 @@ def split_into_channels(sig, num_chan=2):

return [a(outputs[x]) for x in outputs]

def play_signal(signal, fs = 44100):
def play_signal(signal, fs = 44100, max_vol=None):
if max_vol is not None:
signal = np.array(signal).copy()
signal = signal/max(np.abs(signal))
signal = signal*max_vol
scikits.audiolab.play(signal, fs=fs)


def plot_signal(datas, show=False):

if type(datas) is tuple:
datas = [datas]
fig, ax = plt.subplots(1, facecolor='w', edgecolor='k')

for i in xrange(len(datas)):
data, lbl = datas[i]

ax.plot(data, label = lbl)

ax.grid()

if show:
plt.show()

def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues, cm_size=15):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""

fig, ax = plt.subplots(1, figsize=(cm_size, cm_size), facecolor='w', edgecolor='k')
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)

if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
if normalize:
plt.text(j, i, "%0.2f"% cm[i, j], horizontalalignment="center", color="white" if cm[i, j] > thresh else "black")
else:
plt.text(j, i, "%d"% int(cm[i, j]), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black")

plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()


1 change: 0 additions & 1 deletion file_processing.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import numpy as np


def load_16bit_pcm(path, lower_endian = True):
# Can easily abstract it away. Maybe do it later
endianness = '<' if lower_endian else '>'
Expand Down
28 changes: 19 additions & 9 deletions ml_au.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@

from sklearn.metrics import mean_squared_error, average_precision_score, confusion_matrix, accuracy_score, f1_score
from collections import defaultdict
from sklearn.model_selection import KFold
import audiphil as au
import numpy as np

def kfold(X, y, num_k, clfs_names):

def kfold(X, y, clfs_names, num_splits, cm=False, cm_size=15):
classifiers = []
d = defaultdict(list)
labels = sorted(list(set(y)))
for sm_c, name in clfs_names:

scores = []

kf = KFold(n_splits=num_splits, shuffle=True)

for train_index, test_index in kf.split(X):
X_train, X_test = a(X[train_index]), a(X[test_index])
y_train, y_test = a(y[train_index]), a(y[test_index])
X_train, X_test = np.array(X[train_index]), np.array(X[test_index])
y_train, y_test = np.array(y[train_index]), np.array(y[test_index])

cur = sm_c()
cur = cur.fit(X_train, y_train)
y_res = cur.predict(X_test)

cur_acc = f1_score(y_test, y_res, average="macro")
#cur_acc = f1_score(y_test, y_res, average="macro")
cur_acc = accuracy_score(y_test, y_res)

if cm:
mycm = confusion_matrix(y_test, y_res, labels=labels)
au.plot_confusion_matrix(mycm, classes=labels, normalize=False, title="{} {}".format(name, "%0.2f" % accuracy_score(y_test, y_res)), cm_size=cm_size)

scores.append(cur_acc)
classifiers.append(cur)

scores = np.array(scores)
d[name].append("KFOLD: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))

return d

return (d, classifiers)

0 comments on commit e3e6681

Please sign in to comment.