I am working on the file "ROC Curve and the UCI German Credit Data Set.ipynb".
The first task is:
Create a function to plot the confusion matrix.  Code from:
http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html#sphx-glr-auto-examples-model-selection-plot-confusion-matrix-py
Can you write Python code for it?

def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    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]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, 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()
    plt.close()


I am working on the file "ROC Curve and the UCI German Credit Data Set.ipynb".
The first task is:
For multi-level features, make a function to convert to dummy variables.
Can you write Python code for it?

def get_dummies(source_df, dest_df, col):
    dummies = pd.get_dummies(source_df[col], prefix=col)

    print 'Quantities for %s column' % col
    for col in dummies:
        print '%s: %d' % (col, np.sum(dummies[col]))
    print

    dest_df = dest_df.join(dummies)
    return dest_df


I am working on the file "ROC Curve and the UCI German Credit Data Set.ipynb".
The first task is:
Import the data.  The data file is from:
https://archive.ics.uci.edu/ml/datasets/Statlog+%28German+Credit+Data%29
Can you write Python code for it?

column_names = ['CkgAcct', 'Duration', 'CreditHistory', 'Purpose', 'CreditAmt', 'Savings', 'EmploymentTime',
               'PctIncome', 'PersStatus', 'OtherDebts', 'ResidenceSince', 'Property',
               'AgeYrs', 'OtherLoan', 'Housing', 'NumCredits', 'Job', 'FamSize', 'Telephone',
               'Foriegn', 'LoanGood']

df = pd.read_table('german.data.txt', names=column_names, sep=' ')
print df.head()


I am working on the file "ROC Curve and the UCI German Credit Data Set.ipynb".
The first task is:
Make binary factors into 1/0.
Can you write Python code for it?

df['Foriegn'] = df['Foriegn'].eq('A202').mul(1)
df['Telephone'] = df['Telephone'].eq('A192').mul(1)


I am working on the file "ROC Curve and the UCI German Credit Data Set.ipynb".
The first task is:
Plot the ROC curve.  The area under the curve is not bad at 0.815, but a bank would probably want much better predictions of loans that will eventually default.  The data in this data set is likely to be inadequate for that, and either more data, or more data features are needed.
Can you write Python code for it?

plt.figure()
lw = 2
plt.plot(fpr[0], tpr[0], color='darkorange',
         lw=lw, label='ROC curve (area = %0.3f)' % roc_auc[0])
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic example')
plt.legend(loc="lower right")
plt.show()


I am working on the file "ROC Curve and the UCI German Credit Data Set.ipynb".
The first task is:
Plot the confusion matrix.
Can you write Python code for it?

confusion = metrics.confusion_matrix(y_test, pred)
class_names = ['Loan Good', 'Loan Default']
plot_confusion_matrix(confusion, classes=class_names, title='Confusion matrix')


I am working on the file "ROC Curve and the UCI German Credit Data Set.ipynb".
The first task is:
Set up for machine learning. 'X' is the data and 'y' is the true classifications from the data set. X_train and y_train are for model training, X_test and y_test are for model testing - proving the model on data unseen during training.
Can you write Python code for it?

y = df['LoanGood']
X = df.drop('LoanGood', axis=1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=245)


I am working on the file "ROME_NAF/referentiels/referentiels.ipynb".
The first task is:
Charge les labels ROME
Can you write Python code for it?

familles = pd.read_csv('referentiel_ROME/famille_2015_09.csv', sep='|', encoding="utf-8", header=None, dtype=str, index_col=0)
domaines = pd.read_csv('referentiel_ROME/domaine_2015_09.csv', sep='|', encoding="utf-8", header=None, dtype=str)

ROME_df = pd.read_csv('referentiel_ROME/20150921_arboprincipale28427_ROME_tmp.csv', sep='|', encoding="utf-8", header=None, dtype=str)

ROME_df.columns = ['famille', 'domaine', 'metier', 'label']
ROME_df['ROME'] = ROME_df['famille'] + ROME_df['domaine'] + ROME_df['metier']
ROME_df.set_index('ROME', inplace=True)

ROME_df['slugs'] = [slugify(r) for r in ROME_df.label]

ROME_df['famille_label'] = ROME_df.famille.map(lambda code: familles.loc[code][1])

def find_domaine(famille, domaine):
    famille_match = domaines[domaines[0]==famille]
    domaine_match = famille_match[famille_match[1]==domaine]
    return domaine_match.iloc[0][2]

ROME_df['domaine_label'] = ROME_df.apply(lambda row: find_domaine(row.famille, row.domaine), axis=1)

ROME_df.to_csv('referentiel_ROME/20150921_arboprincipale28427_ROME.csv', sep='|')

ROME_df = pd.read_csv('referentiel_ROME/20150921_arboprincipale28427_ROME.csv', index_col=0, sep='|', dtype=str)
ROME_df

ROME_df.iloc[5].label, ROME_df.loc['A1205'].label, ROME_df.index.get_loc('A1205'), ROME_df.iloc[5].name

len(ROME_df)

for ROME_code in ROME_df.index:
    assert '"' not in ROME_df.loc[ROME_code].label


I am working on the file "ROME_NAF/referentiels/referentiels.ipynb".
The first task is:
Codes NAF
Can you write Python code for it?

NAF_df = pd.read_csv('referentiel_NAF/naf2008_liste_n5.csv', sep='|', encoding="utf-8").set_index(['Code2'])
NAF_df.index.names = ['NAF']
NAF_df.columns = ['NAF_alternatif', 'label']
NAF_df.to_csv('referentiel_NAF/naf2008_liste_n5_nouveau_header.csv', sep='|')

NAF_df = pd.read_csv('referentiel_NAF/naf2008_liste_n5_nouveau_header.csv', sep='|', encoding="utf-8").set_index(['NAF'])
NAF_df

for NAF_code in NAF_df.index:
    assert '"' not in NAF_df.loc[NAF_code].label


I am working on the file "ROMS/.ipynb_checkpoints/sandy_sgrid-checkpoint.ipynb".
The first task is:
(Don't be scared, you do not need the sgrid object to get the variables.  This just shows that there is a one-to-one mapping from the sgrid object to the netCDF4 object.)
Can you write Python code for it?

u_velocity = nc.variables[u_var.variable]
v_velocity = nc.variables[v_var.variable]


I am working on the file "ROMS/.ipynb_checkpoints/sandy_sgrid-checkpoint.ipynb".
The first task is:
... but we need a better way to deal with the slice of the slice!
Can you write Python code for it?

from datetime import datetime, timedelta
from netCDF4 import date2index

t_var = nc.variables['ocean_time']
start = datetime(2012, 10, 30, 0, 0)
time_idx = date2index(start, t_var, select='nearest')

v_idx = 0

# Slice of the slice!
u_data = u_velocity[time_idx, v_idx, u_var.center_slicing[-2], u_var.center_slicing[-1]]
v_data = v_velocity[time_idx, v_idx, v_var.center_slicing[-2], v_var.center_slicing[-1]]


I am working on the file "ROMS/.ipynb_checkpoints/sandy_sgrid-checkpoint.ipynb".
The first task is:
Being generic is nice!  This is an improvement up on my first design ;-) ...
Can you write Python code for it?


I am working on the file "ROMS/.ipynb_checkpoints/sandy_sgrid-checkpoint.ipynb".
The first task is:
Plotting
Can you write Python code for it?

%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

import cartopy.crs as ccrs
from cartopy.io import shapereader
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER

def make_map(projection=ccrs.PlateCarree(), figsize=(9, 9)):
    fig, ax = plt.subplots(figsize=figsize,
                           subplot_kw=dict(projection=projection))
    gl = ax.gridlines(draw_labels=True)
    gl.xlabels_top = gl.ylabels_right = False
    gl.xformatter = LONGITUDE_FORMATTER
    gl.yformatter = LATITUDE_FORMATTER
    return fig, ax

sub = 10
scale = 0.06

fig, ax = make_map()

kw = dict(scale=1.0/scale, pivot='middle', width=0.003, color='black')
q = plt.quiver(lon_data[::sub, ::sub], lat_data[::sub, ::sub],
               u_rot[::sub, ::sub], v_rot[::sub, ::sub], zorder=2, **kw)

cs = plt.pcolormesh(lon_data[::sub, ::sub],
                    lat_data[::sub, ::sub],
                    uv_vector_sum[::sub, ::sub], zorder=1, cmap=plt.cm.rainbow)

_ = ax.coastlines('10m')


I am working on the file "ROMS/.ipynb_checkpoints/sandy_sgrid-checkpoint.ipynb".
The first task is:
Rotate vectors by angles
Can you write Python code for it?

from pysgrid.processing_2d import rotate_vectors

u_rot, v_rot = rotate_vectors(u_avg, v_avg, angles)


I am working on the file "ROMS/.ipynb_checkpoints/sandy_sgrid-checkpoint.ipynb".
The first task is:
Some thing for the angle information
Can you write Python code for it?

angle = sgrid.angle

angles = nc.variables[angle.variable][angle.center_slicing]


I am working on the file "ROMS/.ipynb_checkpoints/sandy_sgrid-checkpoint.ipynb".
The first task is:
The object knows about sgrid conventions
Can you write Python code for it?

sgrid.edge1_coordinates, sgrid.edge1_dimensions, sgrid.edge1_padding

u_var = sgrid.u
u_var.center_axis, u_var.node_axis

v_var = sgrid.v
v_var.center_axis, v_var.node_axis


I am working on the file "ROMS/sandy_sgrid.ipynb".
The first task is:
(Don't be scared, you do not need the sgrid object to get the variables.  This just shows that there is a one-to-one mapping from the sgrid object to the netCDF4 object.)
Can you write Python code for it?


I am working on the file "ROMS/sandy_sgrid.ipynb".
The first task is:
... but we need a better way to deal with the slice of the slice!
Can you write Python code for it?


I am working on the file "ROMS/sandy_sgrid.ipynb".
The first task is:
Average velocity vectors to cell centers
Can you write Python code for it?

from pysgrid.processing_2d import avg_to_cell_center

u_avg = avg_to_cell_center(u_data, u_var.center_axis)
v_avg = avg_to_cell_center(v_data, v_var.center_axis)


I am working on the file "ROMS/sandy_sgrid.ipynb".
The first task is:
Being generic is nice!  This is an improvement up on my first design ;-) ...
Can you write Python code for it?


I am working on the file "ROMS/sandy_sgrid.ipynb".
The first task is:
Lon, lat of the center grid
(This is kind of clunky... or maybe I just do not get the sgrid concept beyond the ROMS world.)
Can you write Python code for it?

grid_cell_centers = sgrid.centers  # Array of lon, lat pairs.

lon_var_name, lat_var_name = sgrid.face_coordinates

sg_lon = getattr(sgrid, lon_var_name)
sg_lat = getattr(sgrid, lat_var_name)

lon_data = grid_cell_centers[..., 0][sg_lon.center_slicing]
lat_data = grid_cell_centers[..., 1][sg_lat.center_slicing]


I am working on the file "ROMS/sandy_sgrid.ipynb".
The first task is:
Plotting
Can you write Python code for it?

%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

import cartopy.crs as ccrs
from cartopy.io import shapereader
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER

def make_map(projection=ccrs.PlateCarree(), figsize=(9, 9)):
    fig, ax = plt.subplots(figsize=figsize,
                           subplot_kw=dict(projection=projection))
    gl = ax.gridlines(draw_labels=True)
    gl.xlabels_top = gl.ylabels_right = False
    gl.xformatter = LONGITUDE_FORMATTER
    gl.yformatter = LATITUDE_FORMATTER
    return fig, ax

sub = 5
scale = 0.06

fig, ax = make_map()

kw = dict(scale=1.0/scale, pivot='middle', width=0.003, color='black')
q = plt.quiver(lon_data[::sub, ::sub], lat_data[::sub, ::sub],
               u_rot[::sub, ::sub], v_rot[::sub, ::sub], zorder=2, **kw)

cs = plt.pcolormesh(lon_data[::sub, ::sub],
                    lat_data[::sub, ::sub],
                    uv_vector_sum[::sub, ::sub], zorder=1, cmap=plt.cm.rainbow)

_ = ax.coastlines('10m')


I am working on the file "ROMS/sandy_sgrid.ipynb".
The first task is:
Rotate vectors by angles
Can you write Python code for it?


I am working on the file "ROMS/sandy_sgrid.ipynb".
The first task is:
Speed
Can you write Python code for it?

from pysgrid.processing_2d import vector_sum

uv_vector_sum = vector_sum(u_rot, v_rot)


I am working on the file "ROMS/sandy_sgrid.ipynb".
The first task is:
The object knows about sgrid conventions
Can you write Python code for it?


I am working on the file "ROMS/sandy_sgrid.ipynb".
The first task is:
The sgrid object
Can you write Python code for it?

import pysgrid

# The object creation is a little bit slow.  Can we defer some of the loading/computations?
sgrid = pysgrid.from_nc_dataset(nc)

sgrid  # We need a better __repr__ and __str__ !!!


I am working on the file "RPCA_Testing-3d.ipynb".
The first task is:
Add Some Outliers to Make Life Difficult
Can you write Python code for it?

outliers = np.tile([15,-10,10], 10).reshape((-1,3))

pts = np.vstack([points.T, outliers]).T


I am working on the file "RPCA_Testing-3d.ipynb".
The first task is:
And have a look at this!
Can you write Python code for it?

plt.ylim([-20,20])
plt.xlim([-20,20])
plt.scatter(*pts)
pts0 = np.dot(U[0].reshape((2,1)), np.array([-20,20]).reshape((1,2)))
plt.plot(*pts0)
plt.scatter(*L, c='red')


I am working on the file "RPCA_Testing-3d.ipynb".
The first task is:
Compute SVD on both the clean data and the outliery data
Can you write Python code for it?

U,s,Vt = np.linalg.svd(points)
U_n,s_n,Vt_n = np.linalg.svd(pts)


I am working on the file "RPCA_Testing-3d.ipynb".
The first task is:
Factor the matrix into L (low rank) and S (sparse) parts
Can you write Python code for it?


I am working on the file "RPCA_Testing-3d.ipynb".
The first task is:
Have a look at the factored components...
Can you write Python code for it?

plt.ylim([-20,20])
plt.xlim([-20,20])
plt.scatter(*L)
plt.scatter(*S, c='red')


I am working on the file "RPCA_Testing-3d.ipynb".
The first task is:
It really does add back to the original matrix!
Can you write Python code for it?


I am working on the file "RPCA_Testing-3d.ipynb".
The first task is:
Just 10 outliers can really screw up our line fit!
Can you write Python code for it?

def randrange(n, vmin, vmax):
    return (vmax-vmin)*np.random.rand(n) + vmin

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
n = 100
for c, m, zl, zh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
    xs = randrange(n, 23, 32)
    ys = randrange(n, 0, 100)
    zs = randrange(n, zl, zh)
    ax.scatter(xs, ys, zs, c=c, marker=m)

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

plt.show()


I am working on the file "RPCA_Testing.ipynb".
The first task is:
Add Some Outliers to Make Life Difficult
Can you write Python code for it?

outliers = np.tile([15,-10], 10).reshape((-1,2))

pts = np.vstack([rotated.T, outliers]).T


I am working on the file "RPCA_Testing.ipynb".
The first task is:
And have a look at this!
Can you write Python code for it?


I am working on the file "RPCA_Testing.ipynb".
The first task is:
Compute SVD on both the clean data and the outliery data
Can you write Python code for it?

U,s,Vt = np.linalg.svd(rotated)
U_n,s_n,Vt_n = np.linalg.svd(pts)


I am working on the file "RPCA_Testing.ipynb".
The first task is:
Have a look at the factored components...
Can you write Python code for it?


I am working on the file "RPCA_Testing.ipynb".
The first task is:
It really does add back to the original matrix!
Can you write Python code for it?


I am working on the file "RPCA_Testing.ipynb".
The first task is:
Just 10 outliers can really screw up our line fit!
Can you write Python code for it?


I am working on the file "RPCA_Testing.ipynb".
The first task is:
Make Some Toy Data
Can you write Python code for it?

import matplotlib.pyplot as plt
import numpy as np

def mk_rot_mat(rad=np.pi / 4):
    rot = np.array([[np.cos(rad),-np.sin(rad)], [np.sin(rad), np.cos(rad)]])
    return rot

rot_mat = mk_rot_mat( np.pi / 4)
x = np.random.randn(100) * 5
y = np.random.randn(100)
points = np.vstack([y,x])

rotated = np.dot(points.T, rot_mat).T


I am working on the file "RPCA_Testing.ipynb".
The first task is:
Now the robust pca version!
Can you write Python code for it?

import rpca

reload(rpca)

import logging
logger = logging.getLogger(rpca.__name__)
logger.setLevel(logging.INFO)


I am working on the file "RPCA_Testing.ipynb".
The first task is:
Run SVD on the Low Rank Part
Can you write Python code for it?


I am working on the file "RP_Hajcak_Foti.ipynb".
The first task is:
However, the original author also found a more robust subsequent finding by dividing participants along median ERN and assessing correlation among those whose amplitudes were relatively high. We performed the same
Can you write Python code for it?

high_amplitude = final[final['ERN'] < -6.91]

sns.jointplot(high_amplitude['ERN'],high_amplitude['EPS'],kind="reg",stat_func=pearsonr, color = "slategray")


I am working on the file "RP_Hajcak_Foti.ipynb".
The first task is:
The purpose of this notebook is to document data analysis for a Reproducibility Study conducted in collaboration with the Center for Open Science and fulfillment of my undergraduate thesis at Reed College.
The original article was "Errors are aversive: defensive motivation and the error-related negativity."
Can you write Python code for it?

data = pd.read_csv("rp.csv")

qgrid.show_grid(data, remote_js=True)

# subset trials depending on whether participant made an error,
# made an error in the previous trial ('predict'), or
# was correct in current and previous trial ('unpred')
error_trials = data[data['startle_type'] == 'error']
pred_trials = data[data['startle_type'] == 'predict']
unpred_trials = data[data['startle_type'] == 'unpred']

# restructure dataframe for python analysis
pred_error = pd.merge(error_trials, pred_trials, how="outer", on=["participant","gender","ERN","EPS","errors"], suffixes=('_error', '_pred'))

# further restructuring
final = pd.merge(pred_error, unpred_trials, how="outer", on=["participant","gender","ERN","EPS","errors"], suffixes=('_', '_unpred'))

final.rename(columns={'startle':'startle_unpred','startle_peak':'startle_peak_unpred'},inplace=True)

# drop superfluous columns naming startle type
final.drop(['startle_type_error','startle_type_pred','startle_type'], axis=1, inplace=True)

# na in participant 21's unpred startle trial excludes it from some analyes
error_trial = error_trials[error_trials.participant != 21]
pred_trials = pred_trials[pred_trials.participant != 21]
unpred_trials = unpred_trials[unpred_trials.participant != 21]

final = final[final.participant != 21]

# mean error-potentiated startle (EPS) amplitude
round(final['EPS'].mean(),2)

#standard error of the mean
round(stats.sem(final['EPS']),2)

# mean difference between error and correct trials
(final['startle_error'] - final['startle_unpred']).mean()

round(stats.sem(final['startle_error'] - final['startle_unpred']),2)

#main finding using one trial type for appropriate DF
corr_data = data[['ERN','EPS']]
corr_data.corr(method='pearson', min_periods=1)


I am working on the file "RP_Hajcak_Foti.ipynb".
The first task is:
and also found greater significance (greater correlation, p < .05) However, to investigate whether the outlier participant 1 had any bearing, we removed that data and reran the analysis:
Can you write Python code for it?

high_amp_san_outlier = high_amplitude[high_amplitude['participant'] != 1]

sns.jointplot(high_amp_san_outlier['ERN'],high_amp_san_outlier['EPS'],kind="reg",stat_func=pearsonr, color = "slategray")


I am working on the file "RP_Hajcak_Foti.ipynb".
The first task is:
and found the correlation diminished again.
The original author was communicative and eager to compare findings, and so offered his original ERN and EPS data to pool with our own:
Can you write Python code for it?

collaboration = pd.read_csv('data/collaboration.csv')

collaboration.rename(columns={'ERN (window difference wave)':'ERN','Error-potentiated startle':'EPS'},inplace=True)

sns.jointplot(collaboration['ERN'],collaboration['EPS'],kind="reg",stat_func=pearsonr, color = "slategray")


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
4. Application Scenarios
Analyze Songs from Soundcloud
4.1. Getting Songs from Soundcloud
In this step we are going to analyze songs from Soundcloud, using the Soundcloud API.
Please get your own API key first by clicking "Register New App" on https://developers.soundcloud.com.
Then we can start using the Soundcloud API:
Can you write Python code for it?

# START SOUNDCLOUD API
import soundcloud
import urllib # for mp3 download

# To use soundcloud-python, you must first create a Client instance, passing at a minimum the client id you
# obtained when you registered your app:

# If you only need read-only access to public resources, simply provide a client id when creating a Client instance:
my_client_id= 'insert your soundcloud client id here'

client = soundcloud.Client(client_id=my_client_id)
# if there is no error after this, it should have worked


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
3. Audio Feature Extraction
Rhythm Patterns

Rhythm Patterns (also called Fluctuation Patterns) describe modulation amplitudes for a range of modulation frequencies on "critical bands" of the human auditory range, i.e. fluctuations (or rhythm) on a number of frequency bands. The feature extraction process for the Rhythm Patterns is composed of two stages:
First, the specific loudness sensation in different frequency bands is computed, by using a Short Time FFT, grouping the resulting frequency bands to psycho-acoustically motivated critical-bands, applying spreading functions to account for masking effects and successive transformation into the decibel, Phon and Sone scales. This results in a power spectrum that reflects human loudness sensation (Sonogram).
In the second step, the spectrum is transformed into a time-invariant representation based on the modulation frequency, which is achieved by applying another discrete Fourier transform, resulting in amplitude modulations of the loudness in individual critical bands. These amplitude modulations have different effects on human hearing sensation depending on their frequency, the most significant of which, referred to as fluctuation strength, is most intense at 4 Hz and decreasing towards 15 Hz. From that data, reoccurring patterns in the individual critical bands, resembling rhythm, are extracted, which – after applying Gaussian smoothing to diminish small variations – result in a time-invariant, comparable representation of the rhythmic patterns in the individual critical bands.
Can you write Python code for it?

features = rp_extract(wavedata,                            # the two-channel wave-data of the audio-file
                                samplerate,                          # the samplerate of the audio-file
                                extract_rp          = True,          # <== extract this feature!
                                transform_db        = True,          # apply psycho-accoustic transformation
                                transform_phon      = True,          # apply psycho-accoustic transformation
                                transform_sone      = True,          # apply psycho-accoustic transformation
                                fluctuation_strength_weighting=True, # apply psycho-accoustic transformation
                                skip_leadin_fadeout = 1,             # skip lead-in/fade-out. value = number of segments skipped
                                step_width          = 1)             #

plotrp(features['rp'])


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
2. Audio Processing
Feature Extraction is the core of content-based description of audio files. With feature extraction from audio, a computer is able to recognize the content of a piece of music without the need of annotated labels such as artist, song title or genre. This is the essential basis for information retrieval tasks, such as similarity based searches (query-by-example, query-by-humming, etc.), automatic classification into categories, or automatic organization and clustering of music archives.
Content-based description requires the development of feature extraction techniques that analyze the acoustic characteristics of the signal. Features extracted from the audio signal are intended to describe the stylistic content of the music, e.g. beat, presence of voice, timbre, etc.
We use methods from digital signal processing and consider psycho-acoustic models in order to extract suitable semantic information from music. We developed various feature sets, which are appropriate for different tasks.
Load Audio Files
Load audio data from wav or mp3 file
We provide a library (audiofile_read.py) that is capable of reading WAV and MP3 files (MP3 through an external decoder, see Installation Requirements above).
Take any MP3 or WAV file on your disk - or download one from e.g. freemusicarchive.org.
Can you write Python code for it?

# provide/adjust the path to your wav or mp3 file

audiofile = "music/test.wav"

samplerate, samplewidth, wavedata = audiofile_read(audiofile)


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
4.3. Finding Similar Sounding Songs
In these application scenarios we try to find similar songs or classify music into different categories.
For these Use Cases we need to import a few additional functions from the sklearn package and from mir_utils (installed from git above in parallel to rp_extract):
Can you write Python code for it?

# IMPORTING mir_utils (installed from git above in parallel to rp_extract (otherwise ajust path))
import sys
sys.path.append("../mir_utils")
from demo.NotebookUtils import *
from demo.PlottingUtils import *
from demo.Soundcloud_Demo_Dataset import SoundcloudDemodatasetHandler

# IMPORTS for NearestNeighbor Search
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import NearestNeighbors


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Note: also see source file rp_extract_files.py on how to iterate over ALL mp3 or wav files in a directory.
Look at the results
Can you write Python code for it?

# iterates over all featuers (files) we extracted

for feat in all_features:
    plotrp(feat['rp'])
    plotrh(feat['rh'])

    maxbin = feat['rh'].argmax(axis=0) + 1  # +1 because it starts from 0
    bpm = maxbin * mod_freq_res * 60
    print "roughly", round(bpm), "bpm"


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Add On: Combining different Music Descriptors
Here we merge SSD and RH features together to account for <b>both</b> timbral and rhythmic similarity:
Can you write Python code for it?

def search_similar_songs_with_combined_sets(scds, query_song_id, feature_sets, skip_query=True, n_neighbors=6):

    features = scds.getCombinedFeaturesets(feature_sets)

    sim_song_search = NearestNeighbors(n_neighbors = n_neighbors, metric='l2')

    #
    scaled_feature_space = StandardScaler().fit_transform(features)

    #
    sim_song_search.fit(scaled_feature_space);

    #
    query_track_feature_vector = scaled_feature_space[scds.getFeatureIndexByID(query_song_id, feature_sets[0])]

    #
    similar_songs = sim_song_search.kneighbors(query_track_feature_vector, return_distance=False)[0]

    if skip_query:
        similar_songs = similar_songs[1:]

    #
    similar_soundcloud_ids = scds.getIdsByIndex(similar_songs, feature_sets[0])

    return similar_soundcloud_ids

feature_sets = ['ssd','rh']

compareSimilarityResults([search_similar_songs_with_combined_sets(scds, 68687842, feature_sets=feature_sets, n_neighbors=5),
                          search_similar_songs_with_combined_sets(scds, 40439758, feature_sets=feature_sets, n_neighbors=5)],
                          width=100, height=120, visual=False,
                          columns=[scds.getNameByID(68687842),
                                   scds.getNameByID(40439758)])


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Additional Libraries
These libraries are used in the later tutorial steps, but not necessarily needed if you want to use the RP_extract library alone:

mir_utils: these are additional functions used for the Soundcloud Demo data set in the tutorial below

Soundcloud API: used to retrieve and analyze music from Soundcloud.com

urllib: for downloading content from the web (may be pre-installed already, then you can skip it)

unicsv: used in rp_extract_files.py for batch iteration over many wav or mp3 files, and storing features in CSV (only needed when you want to do batch feature extraction to CSV)

sklearn: Scikit-Learn machine learning package - used in later tutorial steps for finding similar songs and/or using machine learning / classification

Can you write Python code for it?

# in Terminal
sudo pip install soundcloud urllib unicsv scikit-learn

git clone https://github.com/tuwien-musicir/mir_utils.git


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Audio Pre-processing
For audio processing and feature extraction, we use a single channel only.
Therefore in case we have a stereo signal, we combine the separate channels:
Can you write Python code for it?

# use combine the channels by calculating their geometric mean
wavedata_mono = np.mean(wavedata, axis=1)


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Below an example waveform of a mono channel after combining the stereo channels by arithmetic mean:
Can you write Python code for it?

plot_waveform(wavedata_mono[0:max_samples_plot], 16, 3)

plotstft(wavedata_mono, samplerate, binsize=512, ignore=True);


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Compare the Results of Timbral and Rhythmic Similarity
First entry is query-track
Can you write Python code for it?

track_id = 68687842 # 40439758
results_track_1 = search_similar_songs_by_id(track_id, feature_set='ssd', skip_query=False)
results_track_2 = search_similar_songs_by_id(track_id, feature_set='rh', skip_query=False)

compareSimilarityResults([results_track_1, results_track_2],
                          width=100, height=120, visual=False,
                          columns=['Statistical Spectrum Descriptors', 'Rhythm Histograms'])


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Download Preview MP3
Can you write Python code for it?

# set the MP3 download directory
mp3_dir = './music'

mp3_file = mp3_dir + os.sep + "%s.mp3" % track.title

# Download the 128 kbit stream MP3
urllib.urlretrieve (stream.http_mp3_128_url, mp3_file)

print "Downloaded " + mp3_file


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Finding rhythmically similar songs
Can you write Python code for it?

# Initialize the similarity search object

sim_song_search = NearestNeighbors(n_neighbors = 6, metric='euclidean')


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Finding rhythmically similar songs using Rhythm Patterns
This time we define a function that performs steps analogously to the RH retrieval above:
Can you write Python code for it?

def search_similar_songs_by_id(query_song_id, feature_set, skip_query=True):

    scaled_feature_space = StandardScaler().fit_transform(scds.features[feature_set]["data"])

    sim_song_search.fit(scaled_feature_space);

    query_track_feature_vector = scaled_feature_space[scds.features[feature_set]["ids"] == query_song_id]

    similar_songs = sim_song_search.kneighbors(query_track_feature_vector, return_distance=False)[0]

    if skip_query:
        similar_songs = similar_songs[1:]

    similar_soundcloud_ids = scds.features[feature_set]["ids"][similar_songs]

    return similar_soundcloud_ids

similar_soundcloud_ids = search_similar_songs_by_id(query_track_soundcloud_id,
                                              feature_set='rp')

SoundcloudTracklist(similar_soundcloud_ids, width=90, height=120, visual=False)


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Finding songs based on Timbral Similarity
Finding songs based on timbral similarity using Statistical Spectral Descriptors
Can you write Python code for it?

similar_soundcloud_ids = search_similar_songs_by_id(query_track_soundcloud_id,
                                              feature_set='ssd')

SoundcloudTracklist(similar_soundcloud_ids, width=90, height=120, visual=False)


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Further Example: Get a list of tracks by Genre
This is an example on how to retrieve Songs from Soundcloud by genre and/or bpm.
currently this does not work ... (issue on Soundcloud side?)
Can you write Python code for it?

# currently this does not work
genre = 'Dancehall'

curr_offset = 0 # Note: the API has a limit of 50 items per response, so to get more you have to query multiple times with an offset.
tracks = client.get('/tracks', genres=genre, offset=curr_offset)
print "Retrieved", len(tracks), "track objects data"

# original Soundcloud example, searching for genre and bpm
# currently this does not work
tracks = client.get('/tracks', genres='punk', bpm={'from': 120})


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Get Track Info
Can you write Python code for it?

# GET TRACK INFO

#soundcloud_url = 'http://soundcloud.com/forss/flickermood'
soundcloud_url = 'https://soundcloud.com/majorlazer/be-together-feat-wild-belle'

track = client.get('/resolve', url=soundcloud_url)

print "TRACK ID:", track.id
print "Title:", track.title
print "Artist: ", track.user['username']
print "Genre: ", track.genre
print track.bpm, "bpm"
print track.playback_count, "times played"
print track.download_count, "times downloaded"
print "Downloadable?", track.downloadable

# if you want to see all information contained in 'track':
print vars(track)


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Get Track URLs
Can you write Python code for it?

if hasattr(track, 'download_url'):
    print track.download_url
print track.stream_url
stream = client.get('/tracks/%d/streams' % track.id)
#print vars(stream)
print stream.http_mp3_128_url


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Get rough BPM from Rhythm Histogram
By looking at the maximum peak of a Rhythm Histogram, we can determine the beats per minute (BPM) very roughly by multiplying the Index of the Rhythm Histogram bin by the modulation frequency resolution (0.168 Hz) * 60. The resolution of this is however only at +/- 10 bpm.
Can you write Python code for it?

maxbin = features['rh'].argmax(axis=0) + 1  # +1 because it starts from 0

mod_freq_res = 1.0 / (2**18/44100.0)  # resolution of modulation frequency axis (0.168 Hz) (= 1/(segment_size/samplerate))
#print mod_freq_res * 60 # resolution

bpm = maxbin * mod_freq_res * 60

print bpm


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Import + Test your Environment
If you have installed all required libraries, the follwing imports should run without errors.
Can you write Python code for it?

%pylab inline

import warnings
warnings.filterwarnings('ignore')

%load_ext autoreload
%autoreload 2

# numerical processing and scientific libraries
import numpy as np

# plotting
import matplotlib.pyplot as plt

# reading wav and mp3 files
from audiofile_read import *  # included in the rp_extract git package

# Rhythm Pattern Audio Extraction Library
from rp_extract import rp_extract
from rp_plot import *   # can be skipped if you don't want to do any plots

# misc
from urllib import urlopen
import urllib2
import gzip
import StringIO


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Iterate over a List of Soundcloud Tracks
This will take a number of Souncloud URLs and get the track info for them and download the mp3 stream if available.
Can you write Python code for it?

# use your own soundcloud urls here
soundcloud_urls = [
    'https://soundcloud.com/absencemusik/lana-del-rey-born-to-die-absence-remix',
    'https://soundcloud.com/princefoxmusic/raindrops-feat-kerli-prince-fox-remix',
    'https://soundcloud.com/octobersveryown/remyboyz-my-way-rmx-ft-drake'
    ]

mp3_dir = './music'
mp3_files = []
own_track_ids = []

for url in soundcloud_urls:
    print url
    track = client.get('/resolve', url=url)
    mp3_file = mp3_dir + os.sep + "%s.mp3" % track.title
    mp3_files.append(mp3_file)
    own_track_ids.append(track.id)

    stream = client.get('/tracks/%d/streams' % track.id)

    if hasattr(stream, 'http_mp3_128_url'):
        mp3_url = stream.http_mp3_128_url
    elif hasattr(stream, 'preview_mp3_128_url'): # if we cant get the full mp3 we take the 1:30 preview
        mp3_url = stream.preview_mp3_128_url
    else:
        print "No MP3 can be downloaded for this song."
        mp3_url = None # in this case we can't get an mp3

    if not mp3_url == None:
        urllib.urlretrieve (mp3_url, mp3_file) # Download the 128 kbit stream MP3
        print "Downloaded " + mp3_file

# show list of mp3 files we got:
# print mp3_files


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Listen to the results
Can you write Python code for it?

SoundcloudTracklist(similar_soundcloud_ids, width=90, height=120, visual=False)


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Modulation Frequency Variance Descriptor
This descriptor measures variations over the critical frequency bands for a specific modulation frequency (derived from a rhythm pattern).
Considering a rhythm pattern, i.e. a matrix representing the amplitudes of 60 modulation frequencies on 24 critical bands, an MVD vector is derived by computing statistical measures (mean, median, variance, skewness, kurtosis, min and max) for each modulation frequency over the 24 bands. A vector is computed for each of the 60 modulation frequencies. Then, an MVD descriptor for an audio file is computed by the mean of multiple MVDs from the audio file's segments, leading to a 420-dimensional vector.
Temporal Statistical Spectrum Descriptor
Feature sets are frequently computed on a per segment basis and do not incorporate time series aspects. As a consequence, TSSD features describe variations over time by including a temporal dimension. Statistical measures (mean, median, variance, skewness, kurtosis, min and max) are computed over the individual statistical spec- trum descriptors extracted from segments at different time positions within a piece of audio. This captures timbral variations and changes over time in the audio spectrum, for all the critical Bark-bands. Thus, a change of rhythmic, instruments, voices, etc. over time is reflected by this feature set. The dimension is 7 times the dimension of an SSD (i.e. 1176).
Temporal Rhythm Histograms
Statistical measures (mean, median, variance, skewness, kurtosis, min and max) are computed over the individual Rhythm Histograms extracted from various segments in a piece of audio. Thus, change and variation of rhythmic aspects in time are captured by this descriptor.
Extract All Features
To extract ALL or selected ones of the before described features, you can use this command:
Can you write Python code for it?

# adapt the fext array to your needs:
fext = ['rp','ssd','rh','mvd'] # sh, tssd, trh

features = rp_extract(wavedata,
                  samplerate,
                  extract_rp   = ('rp' in fext),          # extract Rhythm Patterns features
                  extract_ssd  = ('ssd' in fext),           # extract Statistical Spectrum Descriptor
                  extract_sh   = ('sh' in fext),          # extract Statistical Histograms
                  extract_tssd = ('tssd' in fext),          # extract temporal Statistical Spectrum Descriptor
                  extract_rh   = ('rh' in fext),           # extract Rhythm Histogram features
                  extract_trh  = ('trh' in fext),          # extract temporal Rhythm Histogram features
                  extract_mvd  = ('mvd' in fext),        # extract Modulation Frequency Variance Descriptor
                  spectral_masking=True,
                  transform_db=True,
                  transform_phon=True,
                  transform_sone=True,
                  fluctuation_strength_weighting=True,
                  skip_leadin_fadeout=1,
                  step_width=1)

# let's see what we got in our dict
print features.keys()

# list the feature type dimensions

for k in features.keys():
    print k, features[k].shape


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Our query-song:
This is a query song from the pre-analyzed data set:
Can you write Python code for it?

query_track_soundcloud_id = 68687842 # Mr. Saxobeat

HTML(scds.getPlayerHTMLForID(query_track_soundcloud_id))


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Plot Wave form
we use this to check if the WAV or MP3 file has been correctly loaded
Can you write Python code for it?

max_samples_plot = 4 * samplerate # limit number of samples to plot (to 4 sec), to avoid graphical overflow

if nsamples < max_samples_plot:
    max_samples_plot = nsamples

plot_waveform(wavedata[0:max_samples_plot], 16, 5);


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Python Libraries
RP_extract depends on the following libraries. If not already included in your Python installation,
please install these Python libraries using pip or easy_install:

Numpy: the fundamental package for scientific computing with Python. It implements a wide range of fast and powerful algebraic functions.

Scipy: Scientific Python library

matplotlib: only needed for plotting (if you skipt the plots below, you are fine without)

They can usually be installed via Python PIP installer on command line:
Can you write Python code for it?

# in Terminal
sudo pip install numpy scipy matplotlib


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
RP Extract Library
This is our mean library for rhythmic and timbral audio feature analysis:

RP_extract Rhythm Patterns Audio Feature Extraction Library (includes Wavio for reading wav files (incl. 24 bit))

download ZIP or check out from GitHub:
Can you write Python code for it?

# in Terminal
git clone https://github.com/tuwien-musicir/rp_extract.git


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Retrieve the feature vector for the query song
Can you write Python code for it?

query_track_feature_vector = scaled_feature_space[scds.features[feature_set]["ids"] == query_track_soundcloud_id]


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Rhythm Histogram
The Rhythm Histogram features we use are a descriptor for general rhythmics in an audio document. Contrary to the Rhythm Patterns and the Statistical Spectrum Descriptor, information is not stored per critical band. Rather, the magnitudes of each modulation frequency bin of all critical bands are summed up, to form a histogram of "rhythmic energy" per modulation frequency. The histogram contains 60 bins which reflect modulation frequency between 0 and 10 Hz. For a given piece of audio, the Rhythm Histogram feature set is calculated by taking the median of the histograms of every 6 second segment processed.
Can you write Python code for it?

features = rp_extract(wavedata,                            # the two-channel wave-data of the audio-file
                                samplerate,                          # the samplerate of the audio-file
                                extract_rh          = True,          # <== extract this feature!
                                transform_db        = True,          # apply psycho-accoustic transformation
                                transform_phon      = True,          # apply psycho-accoustic transformation
                                transform_sone      = True,          # apply psycho-accoustic transformation
                                fluctuation_strength_weighting=True, # apply psycho-accoustic transformation
                                skip_leadin_fadeout = 1,             # skip lead-in/fade-out. value = number of segments skipped
                                step_width          = 1)             #

plotrh(features['rh'])


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Statistical Spectrum Descriptor
The Sonogram is calculated as in the first part of the Rhythm Patterns calculation. According to the occurrence of beats or other rhythmic variation of energy on a specific critical band, statistical measures are able to describe the audio content. Our goal is to describe the rhythmic content of a piece of audio by computing the following statistical moments on the Sonogram values of each of the critical bands:

mean, median, variance, skewness, kurtosis, min- and max-value
Can you write Python code for it?

features = rp_extract(wavedata,                            # the two-channel wave-data of the audio-file
                                samplerate,                          # the samplerate of the audio-file
                                extract_ssd         = True,          # <== extract this feature!
                                transform_db        = True,          # apply psycho-accoustic transformation
                                transform_phon      = True,          # apply psycho-accoustic transformation
                                transform_sone      = True,          # apply psycho-accoustic transformation
                                fluctuation_strength_weighting=True, # apply psycho-accoustic transformation
                                skip_leadin_fadeout = 1,             # skip lead-in/fade-out. value = number of segments skipped
                                step_width          = 1)             #

plotssd(features['ssd'])


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
The Soundcloud Demo Dataset
The Soundcloud Demo Dataset is a collection of commonly known mainstream radio songs hosted on the online streaming platform Soundcloud. The Dataset is available as playlist and is intended to be used to demonstrate the performance of MIR algorithms with the help of well known songs.
<!-- not working on Mac
<iframe width="100%" height="450" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/playlists/106852365&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&visual=true"></iframe>
-->
Can you write Python code for it?

# show the data set as Souncloud playlist

iframe = '<iframe width="100%" height="450" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/playlists/106852365&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&visual=false"></iframe>'

HTML(iframe)


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
The SoundcloudDemodatasetHandler abstracts the access to the TU-Wien server. On this server the extracted features are stored as csv-files. The SoundcloudDemodatasetHandler remotely loads the features and returns them by request. The features have been extracted using the method explained in the previous sections.
Can you write Python code for it?

# first argument is local file path for downloaded MP3s and local metadata (if present, otherwise None)
scds = SoundcloudDemodatasetHandler(None, lazy=False)


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Using your Own Query Song from the self-extracted Souncloud tracks above
Can you write Python code for it?

# check which files we got
mp3_files

# select from the list above the number of the song you want to use as a query (counting from 1)
song_id = 3  # count from 1

# select the feature vector type
feat_type = 'rp' # 'rh' or 'ssd' or 'rp'

# from the all_features data structure, we get the desired feature vector belonging to that song
query_feature_vector = all_features[song_id - 1][feat_type]

# get all the feature vectors of desired feature type from the Soundcloud demo set
demo_features = scds.features[feat_type]["data"]

# Initialize Neighbour Search space with demo set features

sim_song_search.fit(demo_features)

# use our own query_feature_vector for search in the demo set
(distances, similar_songs) = sim_song_search.kneighbors(query_feature_vector, return_distance=True)

print distances
print similar_songs

# now we got the song indices for similar songs in the demo set
similar_songs = similar_songs[0]
similar_songs

# and we get the according Soundcloud Track IDs
similar_soundcloud_ids = scds.features[feat_type]["ids"][similar_songs]

similar_soundcloud_ids

# we add our own Track ID at the beginning to show the seed song below:

my_track_id = own_track_ids[song_id - 1]
print my_track_id
result = np.insert(similar_soundcloud_ids,0,my_track_id)


I am working on the file "RP_extract_Tutorial.ipynb".
The first task is:
Visual Player with the Songs most similar to our Own Song
first song is the query song
Can you write Python code for it?

print "Feature Type:", feat_type
SoundcloudTracklist(result, width=90, height=120, visual=False)


I am working on the file "RP_extract_Tutorial.v3.ipynb".
The first task is:
4. Application Scenarios
Analyze Songs from Soundcloud
4.1. Getting Songs from Soundcloud
In this step we are going to analyze songs from Soundcloud, using the Soundcloud API.
Please get your own API key first by clicking "Register New App" on https://developers.soundcloud.com.
Then we can start using the Soundcloud API:
Can you write Python code for it?

# START SOUNDCLOUD API
import soundcloud
import urllib # for mp3 download

# To use soundcloud-python, you must first create a Client instance, passing at a minimum the client id you
# obtained when you registered your app:

# If you only need read-only access to public resources, simply provide a client id when creating a Client instance:
my_client_id= 'insert your soundcloud client id here'

client = soundcloud.Client(client_id=my_client_id)
# if there is no error after this, it should have worked


I am working on the file "RP_extract_Tutorial.v3.ipynb".
The first task is:
3. Audio Feature Extraction
Rhythm Patterns

Rhythm Patterns (also called Fluctuation Patterns) describe modulation amplitudes for a range of modulation frequencies on "critical bands" of the human auditory range, i.e. fluctuations (or rhythm) on a number of frequency bands. The feature extraction process for the Rhythm Patterns is composed of two stages:
First, the specific loudness sensation in different frequency bands is computed, by using a Short Time FFT, grouping the resulting frequency bands to psycho-acoustically motivated critical-bands, applying spreading functions to account for masking effects and successive transformation into the decibel, Phon and Sone scales. This results in a power spectrum that reflects human loudness sensation (Sonogram).
In the second step, the spectrum is transformed into a time-invariant representation based on the modulation frequency, which is achieved by applying another discrete Fourier transform, resulting in amplitude modulations of the loudness in individual critical bands. These amplitude modulations have different effects on human hearing sensation depending on their frequency, the most significant of which, referred to as fluctuation strength, is most intense at 4 Hz and decreasing towards 15 Hz. From that data, reoccurring patterns in the individual critical bands, resembling rhythm, are extracted, which – after applying Gaussian smoothing to diminish small variations – result in a time-invariant, comparable representation of the rhythmic patterns in the individual critical bands.
Can you write Python code for it?

features = rp_extract(wavedata,                            # the two-channel wave-data of the audio-file
                                samplerate,                          # the samplerate of the audio-file
                                extract_rp          = True,          # <== extract this feature!
                                transform_db        = True,          # apply psycho-accoustic transformation
                                transform_phon      = True,          # apply psycho-accoustic transformation
                                transform_sone      = True,          # apply psycho-accoustic transformation
                                fluctuation_strength_weighting=True, # apply psycho-accoustic transformation
                                skip_leadin_fadeout = 1,             # skip lead-in/fade-out. value = number of segments skipped
                                step_width          = 1)             #

plotrp(features['rp'])


I am working on the file "RP_extract_Tutorial.v3.ipynb".
The first task is:
4.3. Finding Similar Sounding Songs
In these application scenarios we try to find similar songs or classify music into different categories.
For these Use Cases we need to import a few additional functions from the sklearn package and from mir_utils (installed from git above in parallel to rp_extract):
Can you write Python code for it?

# IMPORTING mir_utils (installed from git above in parallel to rp_extract (otherwise ajust path))
import sys
sys.path.append("../mir_utils")
from demo.NotebookUtils import *
from demo.PlottingUtils import *
from demo.Soundcloud_Demo_Dataset import SoundcloudDemodatasetHandler

# IMPORTS for NearestNeighbor Search
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import NearestNeighbors


I am working on the file "RP_extract_Tutorial.v3.ipynb".
The first task is:
4.2. Analyzing Songs from Soundcloud
Analyze the previously loaded Songs
Now this combines reading all the MP3s we've got and analyzing the features
Can you write Python code for it?

# mp3_files is the list of downloaded Soundcloud files as stored above (mp3_files.append())

# all_features will be a list of dict entries for all files
all_features = []

for mp3 in mp3_files:

    # Read the Audio file
    samplerate, samplewidth, wavedata = audiofile_read(mp3)
    print "Successfully read audio file:", mp3
    nsamples = wavedata.shape[0]
    nchannels = wavedata.shape[1]
    print samplerate, "Hz,", samplewidth*8, "bit,", nchannels, "channel(s),", nsamples, "samples"

    # Extract the Audio Features
    # (adapt the fext array to your needs)
    fext = ['rp','ssd','rh','mvd'] # sh, tssd, trh

    features = rp_extract(wavedata,
                      samplerate,
                      extract_rp   = ('rp' in fext),          # extract Rhythm Patterns features
                      extract_ssd  = ('ssd' in fext),           # extract Statistical Spectrum Descriptor
                      extract_sh   = ('sh' in fext),          # extract Statistical Histograms
                      extract_tssd = ('tssd' in fext),          # extract temporal Statistical Spectrum Descriptor
                      extract_rh   = ('rh' in fext),           # extract Rhythm Histogram features
                      extract_trh  = ('trh' in fext),          # extract temporal Rhythm Histogram features
                      extract_mvd  = ('mvd' in fext),        # extract Modulation Frequency Variance Descriptor
                      )

    all_features.append(features)

print "Finished analyzing", len(mp3_files), "files."


I am working on the file "RP_extract_Tutorial.v3.ipynb".
The first task is:
Add On: Combining different Music Descriptors
Here we merge SSD and RH features together to account for <b>both</b> timbral and rhythmic similarity:
Can you write Python code for it?

def search_similar_songs_with_combined_sets(scds, query_song_id, feature_sets, skip_query=True, n_neighbors=6):

    features = scds.getCombinedFeaturesets(feature_sets)

    sim_song_search = NearestNeighbors(n_neighbors = n_neighbors, metric='l2')

    #
    scaled_feature_space = StandardScaler().fit_transform(features)

    #
    sim_song_search.fit(scaled_feature_space);

    #
    query_track_feature_vector = scaled_feature_space[scds.getFeatureIndexByID(query_song_id, feature_sets[0])]

    #
    similar_songs = sim_song_search.kneighbors(query_track_feature_vector, return_distance=False)[0]

    if skip_query:
        similar_songs = similar_songs[1:]

    #
    similar_soundcloud_ids = scds.getIdsByIndex(similar_songs, feature_sets[0])

    return similar_soundcloud_ids

feature_sets = ['ssd','rh']

compareSimilarityResults([search_similar_songs_with_combined_sets(scds, 68687842, feature_sets=feature_sets, n_neighbors=5),
                          search_similar_songs_with_combined_sets(scds, 40439758, feature_sets=feature_sets, n_neighbors=5)],
                          width=100, height=120, visual=False,
                          columns=[scds.getNameByID(68687842),
                                   scds.getNameByID(40439758)])


I am working on the file "RP_extract_Tutorial.v3.ipynb".
The first task is:
Additional Libraries
These libraries are used in the later tutorial steps, but not necessarily needed if you want to use the RP_extract library alone:

mir_utils: these are additional functions used for the Soundcloud Demo data set in the tutorial below

Soundcloud API: used to retrieve and analyze music from Soundcloud.com

urllib: for downloading content from the web (may be pre-installed already, then you can skip it)

unicsv: used in rp_extract_files.py for batch iteration over many wav or mp3 files, and storing features in CSV (only needed when you want to do batch feature extraction to CSV)

sklearn: Scikit-Learn machine learning package - used in later tutorial steps for finding similar songs and/or using machine learning / classification

Can you write Python code for it?

# in Terminal
sudo pip install soundcloud urllib unicsv scikit-learn

git clone https://github.com/tuwien-musicir/mir_utils.git


I am working on the file "RP_extract_Tutorial.v3.ipynb".
The first task is:
Audio Pre-processing
For audio processing and feature extraction, we use a single channel only.
Therefore in case we have a stereo signal, we combine the separate channels:
Can you write Python code for it?

# use combine the channels by calculating their geometric mean
wavedata_mono = np.mean(wavedata, axis=1)


I am working on the file "RP_extract_Tutorial.v3.ipynb".
The first task is:
Below an example waveform of a mono channel after combining the stereo channels by arithmetic mean:
Can you write Python code for it?

plot_waveform(wavedata_mono[0:max_samples_plot], 16, 3)

plotstft(wavedata_mono, samplerate, binsize=512, ignore=True);


I am working on the file "RP_extract_Tutorial.v3.ipynb".
The first task is:
Compare the Results of Timbral and Rhythmic Similarity
First entry is query-track
Can you write Python code for it?

track_id = 68687842 # 40439758
results_track_1 = search_similar_songs_by_id(track_id, feature_set='ssd', skip_query=False)
results_track_2 = search_similar_songs_by_id(track_id, feature_set='rh', skip_query=False)

compareSimilarityResults([results_track_1, results_track_2],
                          width=100, height=120, visual=False,
                          columns=['Statistical Spectrum Descriptors', 'Rhythm Histograms'])


I am working on the file "RP_extract_Tutorial.v3.ipynb".
The first task is:
Finding rhythmically similar songs
Can you write Python code for it?

# Initialize the similarity search object

sim_song_search = NearestNeighbors(n_neighbors = 6, metric='euclidean')


I am working on the file "RP_extract_Tutorial.v3.ipynb".
The first task is:
Finding songs based on Timbral Similarity
Finding songs based on timbral similarity using Statistical Spectral Descriptors
Can you write Python code for it?

similar_soundcloud_ids = search_similar_songs_by_id(query_track_soundcloud_id,
                                              feature_set='ssd')

SoundcloudTracklist(similar_soundcloud_ids, width=90, height=120, visual=False)


I am working on the file "RP_extract_Tutorial.v3.ipynb".
The first task is:
Get Track Info
Can you write Python code for it?

# GET TRACK INFO

#soundcloud_url = 'http://soundcloud.com/forss/flickermood'
soundcloud_url = 'https://soundcloud.com/majorlazer/be-together-feat-wild-belle'

track = client.get('/resolve', url=soundcloud_url)

print "TRACK ID:", track.id
print "Title:", track.title
print "Artist: ", track.user['username']
print "Genre: ", track.genre
print track.bpm, "bpm"
print track.playback_count, "times played"
print track.download_count, "times downloaded"
print "Downloadable?", track.downloadable

# if you want to see all information contained in 'track':
print vars(track)


I am working on the file "RP_extract_Tutorial.v3.ipynb".
The first task is:
Import + Test your Environment
If you have installed all required libraries, the follwing imports should run without errors.
Can you write Python code for it?

%pylab inline

import warnings
warnings.filterwarnings('ignore')

%load_ext autoreload
%autoreload 2

# numerical processing and scientific libraries
import numpy as np

# plotting
import matplotlib.pyplot as plt

# reading wav and mp3 files
from audiofile_read import *  # included in the rp_extract git package

# Rhythm Pattern Audio Extraction Library
from rp_extract_python import rp_extract
from rp_plot import *   # can be skipped if you don't want to do any plots

# misc
from urllib import urlopen
import urllib2
import gzip
import StringIO


I am working on the file "RP_extract_Tutorial.v3.ipynb".
The first task is:
MP3 Decoder
If you want to use MP3 files as input, you need to have one of the following MP3 decoders installed in your system:

Windows: FFMpeg (ffmpeg.exe is included in RP_extract library on Github above, nothing to install)

Mac: Lame for Mac or FFMPeg for Mac

Linux: please install mpg123, lame or ffmpeg from your Software Install Center or Package Repository

Note: If you don't install it to a path which can be found by the operating system, use this to add path where you installed the MP3 decoder binary to your system PATH so Python can call it:
Can you write Python code for it?

import os
path = '/path/to/ffmpeg/'
os.environ['PATH'] += os.pathsep + path


I am working on the file "RP_extract_Tutorial.v3.ipynb".
The first task is:
Modulation Frequency Variance Descriptor
This descriptor measures variations over the critical frequency bands for a specific modulation frequency (derived from a rhythm pattern).
Considering a rhythm pattern, i.e. a matrix representing the amplitudes of 60 modulation frequencies on 24 critical bands, an MVD vector is derived by computing statistical measures (mean, median, variance, skewness, kurtosis, min and max) for each modulation frequency over the 24 bands. A vector is computed for each of the 60 modulation frequencies. Then, an MVD descriptor for an audio file is computed by the mean of multiple MVDs from the audio file's segments, leading to a 420-dimensional vector.
Temporal Statistical Spectrum Descriptor
Feature sets are frequently computed on a per segment basis and do not incorporate time series aspects. As a consequence, TSSD features describe variations over time by including a temporal dimension. Statistical measures (mean, median, variance, skewness, kurtosis, min and max) are computed over the individual statistical spec- trum descriptors extracted from segments at different time positions within a piece of audio. This captures timbral variations and changes over time in the audio spectrum, for all the critical Bark-bands. Thus, a change of rhythmic, instruments, voices, etc. over time is reflected by this feature set. The dimension is 7 times the dimension of an SSD (i.e. 1176).
Temporal Rhythm Histograms
Statistical measures (mean, median, variance, skewness, kurtosis, min and max) are computed over the individual Rhythm Histograms extracted from various segments in a piece of audio. Thus, change and variation of rhythmic aspects in time are captured by this descriptor.
Extract All Features
To extract ALL or selected ones of the before described features, you can use this command:
Can you write Python code for it?

# adapt the fext array to your needs:
fext = ['rp','ssd','rh','mvd'] # sh, tssd, trh

features = rp_extract(wavedata,
                  samplerate,
                  extract_rp   = ('rp' in fext),          # extract Rhythm Patterns features
                  extract_ssd  = ('ssd' in fext),           # extract Statistical Spectrum Descriptor
                  extract_sh   = ('sh' in fext),          # extract Statistical Histograms
                  extract_tssd = ('tssd' in fext),          # extract temporal Statistical Spectrum Descriptor
                  extract_rh   = ('rh' in fext),           # extract Rhythm Histogram features
                  extract_trh  = ('trh' in fext),          # extract temporal Rhythm Histogram features
                  extract_mvd  = ('mvd' in fext),        # extract Modulation Frequency Variance Descriptor
                  spectral_masking=True,
                  transform_db=True,
                  transform_phon=True,
                  transform_sone=True,
                  fluctuation_strength_weighting=True,
                  skip_leadin_fadeout=1,
                  step_width=1)

# let's see what we got in our dict
print features.keys()

# list the feature type dimensions

for k in features.keys():
    print k, features[k].shape


I am working on the file "RP_extract_Tutorial.v3.ipynb".
The first task is:
Our query-song:
This is a query song from the pre-analyzed data set:
Can you write Python code for it?

query_track_soundcloud_id = 68687842 # Mr. Saxobeat

HTML(scds.getPlayerHTMLForID(query_track_soundcloud_id))


I am working on the file "RP_extract_Tutorial.v3.ipynb".
The first task is:
Plot Wave form
we use this to check if the WAV or MP3 file has been correctly loaded
Can you write Python code for it?

max_samples_plot = 4 * samplerate # limit number of samples to plot (to 4 sec), to avoid graphical overflow

if nsamples < max_samples_plot:
    max_samples_plot = nsamples

plot_waveform(wavedata[0:max_samples_plot], 16, 5);


I am working on the file "RP_extract_Tutorial.v3.ipynb".
The first task is:
Python Libraries
RP_extract depends on the following libraries. If not already included in your Python installation,
please install these Python libraries using pip or easy_install:

Numpy: the fundamental package for scientific computing with Python. It implements a wide range of fast and powerful algebraic functions.

Scipy: Scientific Python library

matplotlib: only needed for plotting (if you skipt the plots below, you are fine without)

They can usually be installed via Python PIP installer on command line:
Can you write Python code for it?

# in Terminal
sudo pip install numpy scipy matplotlib


I am working on the file "RP_extract_Tutorial.v3.ipynb".
The first task is:
RP Extract Library
This is our mean library for rhythmic and timbral audio feature analysis:

RP_extract Rhythm Patterns Audio Feature Extraction Library (includes Wavio for reading wav files (incl. 24 bit))

download ZIP or check out from GitHub:
Can you write Python code for it?

# in Terminal
git clone https://github.com/tuwien-musicir/rp_extract.git


I am working on the file "RP_extract_Tutorial.v3.ipynb".
The first task is:
Search the nearest neighbors of the query-feature-vector
This retrieves the most similar song indices and their distance:
Can you write Python code for it?

(distances, similar_songs) = sim_song_search.kneighbors(query_track_feature_vector, return_distance=True)

print distances
print similar_songs

# For now we use only the song indices without distances
similar_songs = sim_song_search.kneighbors(query_track_feature_vector, return_distance=False)[0]

# because we are searching in the entire collection, the top-most result is the query song itself. Thus, we can skip it.
similar_songs = similar_songs[1:]


I am working on the file "RP_extract_Tutorial.v3.ipynb".
The first task is:
The Soundcloud Demo Dataset
The Soundcloud Demo Dataset is a collection of commonly known mainstream radio songs hosted on the online streaming platform Soundcloud. The Dataset is available as playlist and is intended to be used to demonstrate the performance of MIR algorithms with the help of well known songs.
<!-- not working on Mac
<iframe width="100%" height="450" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/playlists/106852365&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&visual=true"></iframe>
-->
Can you write Python code for it?

# show the data set as Souncloud playlist

iframe = '<iframe width="100%" height="450" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/playlists/106852365&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&visual=false"></iframe>'

HTML(iframe)


I am working on the file "RP_extract_Tutorial.v3.ipynb".
The first task is:
Using your Own Query Song from the self-extracted Souncloud tracks above
Can you write Python code for it?

# check which files we got
mp3_files

# select from the list above the number of the song you want to use as a query (counting from 1)
song_id = 3  # count from 1

# select the feature vector type
feat_type = 'rp' # 'rh' or 'ssd' or 'rp'

# from the all_features data structure, we get the desired feature vector belonging to that song
query_feature_vector = all_features[song_id - 1][feat_type]

# get all the feature vectors of desired feature type from the Soundcloud demo set
demo_features = scds.features[feat_type]["data"]

# Initialize Neighbour Search space with demo set features

sim_song_search.fit(demo_features)

# use our own query_feature_vector for search in the demo set
(distances, similar_songs) = sim_song_search.kneighbors(query_feature_vector, return_distance=True)

print distances
print similar_songs

# now we got the song indices for similar songs in the demo set
similar_songs = similar_songs[0]
similar_songs

# and we get the according Soundcloud Track IDs
similar_soundcloud_ids = scds.features[feat_type]["ids"][similar_songs]

similar_soundcloud_ids

# we add our own Track ID at the beginning to show the seed song below:

my_track_id = own_track_ids[song_id - 1]
print my_track_id
result = np.insert(similar_soundcloud_ids,0,my_track_id)


I am working on the file "RP_extract_Tutorial.v3.ipynb".
The first task is:
Visual Player with the Songs most similar to our Own Song
first song is the query song
Can you write Python code for it?

print "Feature Type:", feat_type
SoundcloudTracklist(result, width=90, height=120, visual=False)


I am working on the file "RP_extract_Tutorial.v4.ipynb".
The first task is:
3. Audio Feature Extraction
Rhythm Patterns

Rhythm Patterns (also called Fluctuation Patterns) describe modulation amplitudes for a range of modulation frequencies on "critical bands" of the human auditory range, i.e. fluctuations (or rhythm) on a number of frequency bands. The feature extraction process for the Rhythm Patterns is composed of two stages:
First, the specific loudness sensation in different frequency bands is computed, by using a Short Time FFT, grouping the resulting frequency bands to psycho-acoustically motivated critical-bands, applying spreading functions to account for masking effects and successive transformation into the decibel, Phon and Sone scales. This results in a power spectrum that reflects human loudness sensation (Sonogram).
In the second step, the spectrum is transformed into a time-invariant representation based on the modulation frequency, which is achieved by applying another discrete Fourier transform, resulting in amplitude modulations of the loudness in individual critical bands. These amplitude modulations have different effects on human hearing sensation depending on their frequency, the most significant of which, referred to as fluctuation strength, is most intense at 4 Hz and decreasing towards 15 Hz. From that data, reoccurring patterns in the individual critical bands, resembling rhythm, are extracted, which – after applying Gaussian smoothing to diminish small variations – result in a time-invariant, comparable representation of the rhythmic patterns in the individual critical bands.
Can you write Python code for it?

features = rp_extract(wavedata,                            # the two-channel wave-data of the audio-file
                                samplerate,                          # the samplerate of the audio-file
                                extract_rp          = True,          # <== extract this feature!
                                transform_db        = True,          # apply psycho-accoustic transformation
                                transform_phon      = True,          # apply psycho-accoustic transformation
                                transform_sone      = True,          # apply psycho-accoustic transformation
                                fluctuation_strength_weighting=True, # apply psycho-accoustic transformation
                                skip_leadin_fadeout = 1,             # skip lead-in/fade-out. value = number of segments skipped
                                step_width          = 1)             #

plotrp(features['rp'])


I am working on the file "RP_extract_Tutorial.v4.ipynb".
The first task is:
4.3. Finding Similar Sounding Songs
In these application scenarios we try to find similar songs or classify music into different categories.
For these Use Cases we need to import a few additional functions from the sklearn package and from mir_utils (installed from git above in parallel to rp_extract):
Can you write Python code for it?

# IMPORTING mir_utils (installed from git above in parallel to rp_extract (otherwise ajust path))
import sys
sys.path.append("../mir_utils")
from demo.NotebookUtils import *
from demo.PlottingUtils import *
from demo.Soundcloud_Demo_Dataset import SoundcloudDemodatasetHandler

# IMPORTS for NearestNeighbor Search
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import NearestNeighbors


I am working on the file "RP_extract_Tutorial.v4.ipynb".
The first task is:
<b>Note about Normalization:</b> Normalization is automatically done by audiofile_read() above.
Usually, an audio file stores integer values for the samples. However, for audio processing we need float values that's why the audiofile_read library already converts the input data to float values in the range of (-1,1).
This is taken care of by audiofile_read. In the rare case you don't want to normalize, use this line instead of the one above:
samplerate, samplewidth, wavedata = audiofile_read(audiofile, normalize=False)
In case you use another library to read in WAV files (such as scipy.io.wavfile.read) please have a look into audiofile_read code to do the normalization in the same way. Note that scipy.io.wavfile.read does not correctly read 24bit WAV files.
Audio Information
Let's print some information about the audio file just read:
Can you write Python code for it?

nsamples = wavedata.shape[0]
nchannels = wavedata.shape[1]

print "Successfully read audio file:", audiofile
print samplerate, "Hz,", samplewidth*8, "bit,", nchannels, "channel(s),", nsamples, "samples"


I am working on the file "RP_extract_Tutorial.v4.ipynb".
The first task is:
Note: also see source file rp_extract_files.py on how to iterate over ALL mp3 or wav files in a directory.
Look at the results
Can you write Python code for it?

# iterates over all featuers (files) we extracted

for feat in all_features:
    plotrp(feat['rp'])
    plotrh(feat['rh'])

    maxbin = feat['rh'].argmax(axis=0) + 1  # +1 because it starts from 0
    bpm = maxbin * mod_freq_res * 60
    print "roughly", round(bpm), "bpm"


I am working on the file "RP_extract_Tutorial.v4.ipynb".
The first task is:
Add On: Combining different Music Descriptors
Here we merge SSD and RH features together to account for <b>both</b> timbral and rhythmic similarity:
Can you write Python code for it?

def search_similar_songs_with_combined_sets(scds, query_song_id, feature_sets, skip_query=True, n_neighbors=6):

    features = scds.getCombinedFeaturesets(feature_sets)

    sim_song_search = NearestNeighbors(n_neighbors = n_neighbors, metric='l2')

    #
    scaled_feature_space = StandardScaler().fit_transform(features)

    #
    sim_song_search.fit(scaled_feature_space);

    #
    query_track_feature_vector = scaled_feature_space[scds.getFeatureIndexByID(query_song_id, feature_sets[0])]

    #
    similar_songs = sim_song_search.kneighbors(query_track_feature_vector, return_distance=False)[0]

    if skip_query:
        similar_songs = similar_songs[1:]

    #
    similar_soundcloud_ids = scds.getIdsByIndex(similar_songs, feature_sets[0])

    return similar_soundcloud_ids

feature_sets = ['ssd','rh']

compareSimilarityResults([search_similar_songs_with_combined_sets(scds, 68687842, feature_sets=feature_sets, n_neighbors=5),
                          search_similar_songs_with_combined_sets(scds, 40439758, feature_sets=feature_sets, n_neighbors=5)],
                          width=100, height=120, visual=False,
                          columns=[scds.getNameByID(68687842),
                                   scds.getNameByID(40439758)])


I am working on the file "RP_extract_Tutorial.v4.ipynb".
The first task is:
Additional Libraries
These libraries are used in the later tutorial steps, but not necessarily needed if you want to use the RP_extract library alone:

mir_utils: these are additional functions used for the Soundcloud Demo data set in the tutorial below

Soundcloud API: used to retrieve and analyze music from Soundcloud.com

urllib: for downloading content from the web (may be pre-installed already, then you can skip it)

unicsv: used in rp_extract_files.py for batch iteration over many wav or mp3 files, and storing features in CSV (only needed when you want to do batch feature extraction to CSV)

sklearn: Scikit-Learn machine learning package - used in later tutorial steps for finding similar songs and/or using machine learning / classification

Can you write Python code for it?

# in Terminal
sudo pip install soundcloud urllib unicsv scikit-learn

git clone https://github.com/tuwien-musicir/mir_utils.git


I am working on the file "RP_extract_Tutorial.v4.ipynb".
The first task is:
Compare the Results of Timbral and Rhythmic Similarity
First entry is query-track
Can you write Python code for it?

track_id = 68687842 # 40439758
results_track_1 = search_similar_songs_by_id(track_id, feature_set='ssd', skip_query=False)
results_track_2 = search_similar_songs_by_id(track_id, feature_set='rh', skip_query=False)

compareSimilarityResults([results_track_1, results_track_2],
                          width=100, height=120, visual=False,
                          columns=['Statistical Spectrum Descriptors', 'Rhythm Histograms'])


I am working on the file "RP_extract_Tutorial.v4.ipynb".
The first task is:
Download Preview MP3
Can you write Python code for it?

# set the MP3 download directory
mp3_dir = './music'

mp3_file = mp3_dir + os.sep + "%s.mp3" % track.title

# Download the 128 kbit stream MP3
urllib.urlretrieve (stream.http_mp3_128_url, mp3_file)

print "Downloaded " + mp3_file


I am working on the file "RP_extract_Tutorial.v4.ipynb".
The first task is:
Finding rhythmically similar songs using Rhythm Patterns
This time we define a function that performs steps analogously to the RH retrieval above:
Can you write Python code for it?

def search_similar_songs_by_id(query_song_id, feature_set, skip_query=True):

    scaled_feature_space = StandardScaler().fit_transform(scds.features[feature_set]["data"])

    sim_song_search.fit(scaled_feature_space);

    query_track_feature_vector = scaled_feature_space[scds.features[feature_set]["ids"] == query_song_id]

    similar_songs = sim_song_search.kneighbors(query_track_feature_vector, return_distance=False)[0]

    if skip_query:
        similar_songs = similar_songs[1:]

    similar_soundcloud_ids = scds.features[feature_set]["ids"][similar_songs]

    return similar_soundcloud_ids

similar_soundcloud_ids = search_similar_songs_by_id(query_track_soundcloud_id,
                                              feature_set='rp')

SoundcloudTracklist(similar_soundcloud_ids, width=90, height=120, visual=False)


I am working on the file "RP_extract_Tutorial.v4.ipynb".
The first task is:
Finding songs based on Timbral Similarity
Finding songs based on timbral similarity using Statistical Spectral Descriptors
Can you write Python code for it?

similar_soundcloud_ids = search_similar_songs_by_id(query_track_soundcloud_id,
                                              feature_set='ssd')

SoundcloudTracklist(similar_soundcloud_ids, width=90, height=120, visual=False)


I am working on the file "RP_extract_Tutorial.v4.ipynb".
The first task is:
Further Example: Get a list of tracks by Genre
This is an example on how to retrieve Songs from Soundcloud by genre and/or bpm.
currently this does not work ... (issue on Soundcloud side?)
Can you write Python code for it?

# currently this does not work
genre = 'Dancehall'

curr_offset = 0 # Note: the API has a limit of 50 items per response, so to get more you have to query multiple times with an offset.
tracks = client.get('/tracks', genres=genre, offset=curr_offset)
print "Retrieved", len(tracks), "track objects data"

# original Soundcloud example, searching for genre and bpm
# currently this does not work
tracks = client.get('/tracks', genres='punk', bpm={'from': 120})


I am working on the file "RP_extract_Tutorial.v4.ipynb".
The first task is:
Listen to the results
Can you write Python code for it?

SoundcloudTracklist(similar_soundcloud_ids, width=90, height=120, visual=False)


I am working on the file "RP_extract_Tutorial.v4.ipynb".
The first task is:
Plot Wave form
we use this to check if the WAV or MP3 file has been correctly loaded
Can you write Python code for it?

max_samples_plot = 4 * samplerate # limit number of samples to plot (to 4 sec), to avoid graphical overflow

if nsamples < max_samples_plot:
    max_samples_plot = nsamples

plot_waveform(wavedata[0:max_samples_plot], 16, 5);


I am working on the file "RP_extract_Tutorial.v4.ipynb".
The first task is:
Retrieve the feature vector for the query song
Can you write Python code for it?

query_track_feature_vector = scaled_feature_space[scds.features[feature_set]["ids"] == query_track_soundcloud_id]


I am working on the file "RP_extract_Tutorial.v4.ipynb".
The first task is:
Rhythm Histogram
The Rhythm Histogram features we use are a descriptor for general rhythmics in an audio document. Contrary to the Rhythm Patterns and the Statistical Spectrum Descriptor, information is not stored per critical band. Rather, the magnitudes of each modulation frequency bin of all critical bands are summed up, to form a histogram of "rhythmic energy" per modulation frequency. The histogram contains 60 bins which reflect modulation frequency between 0 and 10 Hz. For a given piece of audio, the Rhythm Histogram feature set is calculated by taking the median of the histograms of every 6 second segment processed.
Can you write Python code for it?

features = rp_extract(wavedata,                            # the two-channel wave-data of the audio-file
                                samplerate,                          # the samplerate of the audio-file
                                extract_rh          = True,          # <== extract this feature!
                                transform_db        = True,          # apply psycho-accoustic transformation
                                transform_phon      = True,          # apply psycho-accoustic transformation
                                transform_sone      = True,          # apply psycho-accoustic transformation
                                fluctuation_strength_weighting=True, # apply psycho-accoustic transformation
                                skip_leadin_fadeout = 1,             # skip lead-in/fade-out. value = number of segments skipped
                                step_width          = 1)             #

plotrh(features['rh'])


I am working on the file "RP_extract_Tutorial.v4.ipynb".
The first task is:
Search the nearest neighbors of the query-feature-vector
This retrieves the most similar song indices and their distance:
Can you write Python code for it?

(distances, similar_songs) = sim_song_search.kneighbors(query_track_feature_vector, return_distance=True)

print distances
print similar_songs

# For now we use only the song indices without distances
similar_songs = sim_song_search.kneighbors(query_track_feature_vector, return_distance=False)[0]

# because we are searching in the entire collection, the top-most result is the query song itself. Thus, we can skip it.
similar_songs = similar_songs[1:]


I am working on the file "RP_extract_Tutorial.v4.ipynb".
The first task is:
The Soundcloud Demo Dataset
The Soundcloud Demo Dataset is a collection of commonly known mainstream radio songs hosted on the online streaming platform Soundcloud. The Dataset is available as playlist and is intended to be used to demonstrate the performance of MIR algorithms with the help of well known songs.
<!-- not working on Mac
<iframe width="100%" height="450" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/playlists/106852365&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&visual=true"></iframe>
-->
Can you write Python code for it?

# show the data set as Souncloud playlist

iframe = '<iframe width="100%" height="450" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/playlists/106852365&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&visual=false"></iframe>'

HTML(iframe)


I am working on the file "RP_extract_Tutorial.v4.ipynb".
The first task is:
Using your Own Query Song from the self-extracted Souncloud tracks above
Can you write Python code for it?

# check which files we got
mp3_files

# select from the list above the number of the song you want to use as a query (counting from 1)
song_id = 3  # count from 1

# select the feature vector type
feat_type = 'rp' # 'rh' or 'ssd' or 'rp'

# from the all_features data structure, we get the desired feature vector belonging to that song
query_feature_vector = all_features[song_id - 1][feat_type]

# get all the feature vectors of desired feature type from the Soundcloud demo set
demo_features = scds.features[feat_type]["data"]

# Initialize Neighbour Search space with demo set features

sim_song_search.fit(demo_features)

# use our own query_feature_vector for search in the demo set
(distances, similar_songs) = sim_song_search.kneighbors(query_feature_vector, return_distance=True)

print distances
print similar_songs

# now we got the song indices for similar songs in the demo set
similar_songs = similar_songs[0]
similar_songs

# and we get the according Soundcloud Track IDs
similar_soundcloud_ids = scds.features[feat_type]["ids"][similar_songs]

similar_soundcloud_ids

# we add our own Track ID at the beginning to show the seed song below:

my_track_id = own_track_ids[song_id - 1]
print my_track_id
result = np.insert(similar_soundcloud_ids,0,my_track_id)


I am working on the file "RP_extract_Tutorial.v4.ipynb".
The first task is:
Visual Player with the Songs most similar to our Own Song
first song is the query song
Can you write Python code for it?

print "Feature Type:", feat_type
SoundcloudTracklist(result, width=90, height=120, visual=False)


I am working on the file "RP_extract_Tutorial1.ipynb".
The first task is:
3. Audio Feature Extraction
Rhythm Patterns

Rhythm Patterns (also called Fluctuation Patterns) describe modulation amplitudes for a range of modulation frequencies on "critical bands" of the human auditory range, i.e. fluctuations (or rhythm) on a number of frequency bands. The feature extraction process for the Rhythm Patterns is composed of two stages:
First, the specific loudness sensation in different frequency bands is computed, by using a Short Time FFT, grouping the resulting frequency bands to psycho-acoustically motivated critical-bands, applying spreading functions to account for masking effects and successive transformation into the decibel, Phon and Sone scales. This results in a power spectrum that reflects human loudness sensation (Sonogram).
In the second step, the spectrum is transformed into a time-invariant representation based on the modulation frequency, which is achieved by applying another discrete Fourier transform, resulting in amplitude modulations of the loudness in individual critical bands. These amplitude modulations have different effects on human hearing sensation depending on their frequency, the most significant of which, referred to as fluctuation strength, is most intense at 4 Hz and decreasing towards 15 Hz. From that data, reoccurring patterns in the individual critical bands, resembling rhythm, are extracted, which – after applying Gaussian smoothing to diminish small variations – result in a time-invariant, comparable representation of the rhythmic patterns in the individual critical bands.
Can you write Python code for it?

extracted_features = rp_extract(wavedata,                            # the two-channel wave-data of the audio-file
                                samplerate,                          # the samplerate of the audio-file
                                extract_rp          = True,          # <== extract this feature!
                                transform_db        = True,          # apply psycho-accoustic transformation
                                transform_phon      = True,          # apply psycho-accoustic transformation
                                transform_sone      = True,          # apply psycho-accoustic transformation
                                fluctuation_strength_weighting=True, # apply psycho-accoustic transformation
                                skip_leadin_fadeout = 1,             # skip lead-in/fade-out. value = number of segments skipped
                                step_width          = 1)             #

plotrp(extracted_features['rp'])


I am working on the file "RP_extract_Tutorial1.ipynb".
The first task is:
2. Audio Processing
Feature Extraction is the core of content-based description of audio files. With feature extraction from audio, a computer is able to recognize the content of a piece of music without the need of annotated labels such as artist, song title or genre. This is the essential basis for information retrieval tasks, such as similarity based searches (query-by-example, query-by-humming, etc.), automatic classification into categories, or automatic organization and clustering of music archives.
Content-based description requires the development of feature extraction techniques that analyze the acoustic characteristics of the signal. Features extracted from the audio signal are intended to describe the stylistic content of the music, e.g. beat, presence of voice, timbre, etc.
We use methods from digital signal processing and consider psycho-acoustic models in order to extract suitable semantic information from music. We developed various feature sets, which are appropriate for different tasks.
Load Audio Files
Load audio data from wav or mp3 file
We provide a library (audiofile_read.py) that is capable of reading WAV and MP3 files (MP3 through an external decoder, see Installation Requirements above)
Can you write Python code for it?

# provide/adjust the path to your wav or mp3 file

audiofile = "music/myaudio.wav"
#audiofile = "Acrassicauda_-_02_-_Garden_Of_Stones.wav"

samplerate, samplewidth, wavedata = audiofile_read(audiofile)


I am working on the file "RP_extract_Tutorial1.ipynb".
The first task is:
Audio Pre-processing
For audio processing and feature extraction, we use a single channel only.
Therefore in case we have a stereo signal, we combine the separate channels:
Can you write Python code for it?

# use combine the channels by calculating their geometric mean
wavedata_mono = np.mean(wavedata, axis=1)


I am working on the file "RP_extract_Tutorial1.ipynb".
The first task is:
Because we are searching in the entire collection, the top-most result is the query song itself. Thus, we can skip it.
Can you write Python code for it?


I am working on the file "RP_extract_Tutorial1.ipynb".
The first task is:
Finding rhythmical similar songs
Finding rhythmical similar songs using Rhythm Histograms
Can you write Python code for it?


I am working on the file "RP_extract_Tutorial1.ipynb".
The first task is:
Finding rhythmical similar songs using Rhythm Patterns
Can you write Python code for it?

def search_similar_songs(query_song_id, feature_set, skip_query=True):

    #
    scaled_feature_space = StandardScaler().fit_transform(rp_features[feature_set]["data"])

    #
    sim_song_search.fit(scaled_feature_space);

    #
    query_track_feature_vector = scaled_feature_space[rp_features[feature_set]["soundcloudids"] == query_song_id]

    #
    similar_songs = sim_song_search.kneighbors(query_track_feature_vector, return_distance=False)[0]

    if skip_query:
        similar_songs = similar_songs[1:]

    #
    similar_soundcloud_ids = rp_features[feature_set]["soundcloudids"][similar_songs]

    return similar_soundcloud_ids

similar_soundcloud_ids = search_similar_songs(query_track_soundcloud_id,
                                              feature_set='rp')

SoundcloudTracklist(similar_soundcloud_ids, width=90, height=120, visual=False)


I am working on the file "RP_extract_Tutorial1.ipynb".
The first task is:
Finding songs based on timbral similarity
Finding songs based on timbral similarity using Statistical Spectral Descriptors
Can you write Python code for it?

similar_soundcloud_ids = search_similar_songs(query_track_soundcloud_id,
                                              feature_set='ssd')

SoundcloudTracklist(similar_soundcloud_ids, width=90, height=120, visual=False)


I am working on the file "RP_extract_Tutorial1.ipynb".
The first task is:
First entry is query-track (TODO: change CSS style of query table cells!)
Can you write Python code for it?

results_track_1 = search_similar_songs(68687842, feature_set='ssd', skip_query=False)
results_track_2 = search_similar_songs(40439758, feature_set='rh', skip_query=False)

compareSimilarityResults([results_track_1, results_track_2],
                          width=100, height=120, visual=False,
                          columns=['Statistical Spectrum Descriptors', 'Rhythm Histograms'])


I am working on the file "RP_extract_Tutorial1.ipynb".
The first task is:
Fitting the siilarity search object
Can you write Python code for it?

sim_song_search = NearestNeighbors(n_neighbors = 6, metric='euclidean')


I am working on the file "RP_extract_Tutorial1.ipynb".
The first task is:
Import + Test your Environment
If you have installed all required libraries, the follwing imports should run without errors.
Can you write Python code for it?

%pylab inline

import warnings
warnings.filterwarnings('ignore')

%load_ext autoreload
%autoreload 2

# numerical processing and scientific libraries
import numpy as np
import pandas as pd

# plotting
import matplotlib.pyplot as plt

# Rhythm Pattern Audio Extraction Library
## edit the path here where you checked out and stored the rp_extract package
#import sys
#sys.path.append("./rp_extract")
from rp_plot import *
from rp_extract import rp_extract

# reading wav and mp3 files
from audiofile_read import *

# misc
from urllib import urlopen
import urllib2
import gzip
import StringIO


I am working on the file "RP_extract_Tutorial1.ipynb".
The first task is:
MP3 Decoder
If you want to use MP3 files as input, you need to have one of the following MP3 decoders installed in your system:

Windows: FFMpeg (ffmpeg.exe is included in RP_extract library on Github above, nothing to install)

Mac: Lame for Mac or FFMPeg for Mac

Linux: please install mpg123, lame or ffmpeg from your Software Install Center or Package Repository

Note: If you don't install it to a path which can be found by the operating system, use this to add path where you installed the MP3 decoder binary to your system PATH so Python can call it:
Can you write Python code for it?

import os
path = '/path/to/ffmpeg/'
os.environ['PATH'] += os.pathsep + path