-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
511 lines (389 loc) · 13.5 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
"""Streamlit app code.
This Streamlit app will showcase an hypothetical use case for the
'music_size.joblib' and 'genre_prediction.joblib' models under the 'models'
folder.
"""
import numpy as np
import plotly.express as px
import polars as pl
import streamlit as st
from joblib import load
from sklearn.metrics import ConfusionMatrixDisplay
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from tensorflow.keras.models import load_model
from util.altair_charts import (
songs_by_bitrate,
songs_per_year,
top_5_genre_count_per_avg_bitrate,
top_10_avg_durations_by_genre,
top_10_genres_chart,
top_10_most_played_songs,
)
from util.data_imputation import data_imputation
from util.genre_prediction import pick_random_song
from util.on_change import update_numin, update_slider
from util.scaler import scale
# "st.session_state object:", st.session_state
st.markdown(
"""
# Data Science for Music :guitar: :notes:
## Hypothetical case study
A music streaming company approached us to help them estimate analyze their
music library looking for patterns, also to figure out how much space they
would require to store their music files if they offered them to their users
in different bit rates, and finally they also wanted a to be able to classify
new songs in their library by to their genre automatically.
They provided us an export of their music files metadata, an export of how
many times their songs are played (also called scrobbles), and a few sample
songs of the genres they have in their library.
"""
)
# Import dataset
original_df = pl.read_csv('data/music_library_export.csv',
encoding='utf8-lossy')
# Data imputation
songs_df = data_imputation(original_df)
st.markdown(
"""
### Dataset 1 - Music Files Metadata
Here we can see the details of their songs' metadata included in their export.
""")
# Display original data's columns
st.dataframe(songs_df)
st.markdown(
"""
### Visualization
Now let's see a few charts we created for them to better understand their data.
Here's a chart of the top 10 genres with most songs in their dataset, we can
see the genre with most songs is 'Indie'.
"""
)
st.altair_chart(top_10_genres_chart(songs_df), theme=None)
st.markdown(
"""
Now here's a chart of the top 10 genres with the longest average song duration,
we can see the genre with most duration is 'Hardstyle'.
"""
)
st.altair_chart(top_10_avg_durations_by_genre(songs_df), theme=None)
st.markdown(
"""
Now here's a chart of the top 10 years with most songs, we can see the year
with most songs was 2008.
"""
)
st.altair_chart(songs_per_year(songs_df), theme=None)
st.markdown(
"""
Next, here's a chart of the top 5 most used bit rates, we can see most popular
one is 320 kb/s.
"""
)
st.altair_chart(songs_by_bitrate(songs_df), theme=None)
st.markdown(
"""
Now a chart of the top 5 genre count per average bit rate, we can see most
popular one is again 320 kb/s.
"""
)
base = top_5_genre_count_per_avg_bitrate(songs_df)
c1 = base.mark_arc(innerRadius=30, strokeWidth=0)
c2 = base.mark_text(radiusOffset=10).encode(
text='Genre'
)
st.altair_chart(c1 + c2, theme=None)
# Import scrobbles dataset
scrobbles_df = pl.read_csv("data/lastfm-scrobbles-edchapa.csv")
# Data imputation
scrobbles_df.drop_in_place("uts")
scrobbles_df.drop_in_place("artist_mbid")
scrobbles_df.drop_in_place("album_mbid")
scrobbles_df.drop_in_place("track_mbid")
st.markdown(
"""
### Dataset 2 - Scrobbles
With the export of how many times their songs were played, we obtained the
following char of their top 10 most played songs.
"""
)
st.altair_chart(top_10_most_played_songs(scrobbles_df), theme=None)
st.markdown(
"""
### Storage Size Prediction
As we mentioned before, the music streaming company needed to figure out how
much space they would require to store their music files if they offered them
to their users in different bit rates.
For that, we will use their first dataset, but let's strip it down to their
songs' duration, bit rate, and size. Let's also see how their current total
storage size looks like."""
)
original_data = songs_df.select(
pl.col('Artist'),
pl.col('Name'),
(pl.col('Total Time')/1000).alias('Duration (seconds)'),
pl.col('Bit Rate').alias('Bit Rate (kb/s)')).with_columns(
songs_df.select(pl.col('Size').alias(
'Size (bytes)'))
)
st.dataframe(original_data, hide_index=True)
# Display original dataset's total size
total_original_size = songs_df.select(
pl.col('Size').sum())
st.write('Current total storage size (GBs):',
round(total_original_size.to_numpy()[0][0]/(1024*1024*1024), 2))
st.markdown(
"""
Now let's see an interactive 3D chart of the 'Size', 'Total Time' and
'Bit Rate' columns.
""")
data = songs_df.select(pl.col('Bit Rate'), pl.col('Size')).with_columns(
songs_df.select((pl.col('Total Time')/1000).alias('Duration')))
# Plot original data
fig = px.scatter_3d(
data,
x='Duration',
y='Bit Rate',
z='Size',
color='Size'
)
st.plotly_chart(fig, theme=None)
st.markdown(
"""
By manipulating the previous 3D chart, we can see that the size of a song
increases proportionally to its duration and its bit rate. The song with the
biggest size (represented by the yellow dot) also has the longest duration of
all songs, and the blue dots have the smallest size because they also have
either short durations and/or low bit rates.
This means we can predict the size of a song by fitting a linear regression
model using its duration and bit rate.
""")
st.sidebar.title("Music library storage size prediction :notes:")
# Create sidebar input and sidebar linked to each other with update functions
bit_rate = st.sidebar.number_input(
"Bit Rate (kb/s)", step=64, key='numeric', min_value=128, max_value=2816,
on_change=update_slider)
slider_val = st.sidebar.slider(
"Bit Rate (kb/s)", min_value=128, max_value=2816, step=64,
label_visibility='hidden',
key='slider',
on_change=update_numin)
st.markdown(
"""
### Modeling Part 1
After creating a linear regression model, now we can play with it and see how
the predicted total storage size will update automatically depending on the
selected bit rate by using the "Bit Rate (kb/s)" slider or the input field on
the left sidebar, so the company can make the best decision according to their
budget.
"""
)
# Loading the model we created before
model = load('models/music_size.joblib')
# Separate variables
songs_independent_vars = songs_df.select((pl.col('Total Time')/1000),
pl.col('Bit Rate'))
st.write("New bit rate (kb/s):", bit_rate)
new_df = songs_independent_vars.with_columns(
pl.col('Bit Rate').map_elements(lambda x: bit_rate)
)
# Scale dataset
scaled_new_df = scale(new_df)
# Predict new sizes
predicted_size = model.predict(scaled_new_df)
# Display predicted total size
st.write("Predicted total storage size (GBs): ",
round(predicted_size.sum()/(1024*1024*1024), 2))
# Add results to new_df
size_series = pl.Series("Size", np.concatenate(predicted_size))
new_df = new_df.with_columns(
size_series
).rename({"Total Time": "Duration"})
# Remove negative sizes
new_df = new_df.select(
pl.col('Duration').filter(pl.col('Size') > 0),
pl.col('Bit Rate').filter(pl.col('Size') > 0),
pl.col('Size').filter(pl.col('Size') > 0)
)
# Plot results
fig_2 = px.scatter_3d(
new_df,
x='Duration',
y='Bit Rate',
z='Size',
color='Size'
)
st.plotly_chart(fig_2, theme=None)
st.markdown(
"""
###
And now, here's how our model predicts all possible values within the ranges of
our dataset, from the minimum to the maximum values of the durations and bit
rates.
"""
)
# Generate 100 values between minimum and maximum durations and bitrates
min_bit_rate = songs_df.select(pl.col('Bit Rate').min()).to_numpy()[0][0]
max_bit_rate = songs_df.select(pl.col('Bit Rate').max()).to_numpy()[0][0]
min_duration = int(
songs_df.select(pl.col('Total Time').min()).to_numpy()[0][0]/1000)
max_duration = int(
songs_df.select(pl.col('Total Time').max()).to_numpy()[0][0]/1000)
bitrate_vals = np.linspace(min_bit_rate, max_bit_rate, 100)
duration_vals = np.linspace(min_duration, max_duration, 100)
# Create a grid combining all values
xy = np.meshgrid(duration_vals, bitrate_vals)
zz = np.array(list(zip(*(x.flat for x in xy))))
# Create dataframe with new values
plot_values = pl.DataFrame(zz).rename({"column_0": "Total Time", "column_1": "Bit Rate"})
# Scale Total Time
scaled_plot_values = scale(plot_values)
# predict new Size values and store them in a new DataFrame
predicted_y = model.predict(scaled_plot_values)
prediction_y_df = pl.DataFrame({"Size": np.concatenate(predicted_y)}).select(
pl.col("Size").cast(pl.Int64)
)
# Remove negative Sizes
data = plot_values.with_columns(
prediction_y_df).rename({'Total Time': 'Duration'}).select(
pl.col('Duration').filter(pl.col('Size') > 0),
pl.col('Bit Rate').filter(pl.col('Size') > 0),
pl.col('Size').filter(pl.col('Size') > 0)
)
# Plot result
fig_3 = px.scatter_3d(
data,
x='Duration',
y='Bit Rate',
z='Size',
color='Size'
)
fig_3.update_layout(scene=dict(camera=dict(eye=dict(x=1.5, y=1, z=1.5))))
st.plotly_chart(fig_3, theme=None)
# Music Genre Classification
st.markdown(
"""
### Dataset 3 - Music Genre Classification Model
Finally, the music streaming company wanted to automatically classify new
songs in their library according to their genre.
Our first step was extracting the audio features of the sample songs, here's
how they look like after extraction.
"""
)
# Import dataset
genres_df = pl.read_csv('data/features_3_sec.csv')
st.dataframe(genres_df)
# Export labels into a list
labels = genres_df[['label']].unique(maintain_order=True).to_series().to_list()
# Data imputation
genres_df.drop_in_place('length')
genres_df.drop_in_place('filename')
# Separate variables
labels_ds = genres_df.select(
pl.col('label').cast(pl.Categorical).to_physical()).to_numpy().squeeze()
_ = genres_df.drop_in_place('label')
# Create StandardScaler
standard_scaler = StandardScaler()
scaled_features_ds = standard_scaler.fit_transform(genres_df.to_numpy())
# Split our dataset
total_song_count = len(scaled_features_ds)
training_count = int(total_song_count * .60)
test_count = int(total_song_count * .20)
validation_count = int(total_song_count * .20)
training_x, rest_x, training_y, rest_y = train_test_split(
scaled_features_ds,
labels_ds,
train_size=training_count)
testing_x, validation_x, testing_y, validation_y = train_test_split(
rest_x, rest_y, train_size=test_count)
# Load our model and its training history
history = np.load('models/training_history.npy', allow_pickle='TRUE').item()
model = load_model('models/genre_prediction.h5')
st.markdown(
"""
### Modeling Part 2
We built a genre classification neural network model and trained it using the
sample songs of each genre.
Our model has 1 Dense input layer, 1 hidden Dense layer, and 1 Dense output
layer, with 2 hidden Dropout layers in-between to prevent overfitting.
"""
)
model.summary(print_fn=lambda x: st.text(x))
st.markdown(
"""
We trained our model for 20 epochs, here's the model's Training History chart.
"""
)
# Plot our model's training history
accuracy = history.history['accuracy']
epochs = np.arange(len(accuracy))
fig = px.line(
x=epochs,
y=accuracy,
title='Training History',
labels={'x': 'Epochs', 'y': 'Accuracy'}
)
st.plotly_chart(fig)
# Plot the Loss History
st.markdown(
"""
And here's the model's Loss History chart.
"""
)
loss = history.history['loss']
epochs2 = np.arange(len(loss))
fig2 = px.line(
x=epochs2,
y=loss,
title='Loss',
labels={'x': 'Epochs', 'y': 'Loss'}
)
st.plotly_chart(fig2)
st.markdown(
"""
### Testing our model
After building our model, we wanted to see how well it was predicting the
sample songs' genre.
Here we can see a confusion matrix of the predicted genres vs the true genres
of the validation sample songs.
"""
)
# Evaluate vs validation dataset
model.evaluate(validation_x, validation_y, batch_size=128)
predictions_y = np.argmax(model.predict(validation_x, batch_size=128), axis=1)
validation_y_labels = []
predictions_y_labels = []
for val in validation_y:
validation_y_labels.append(labels[val])
for val in predictions_y:
predictions_y_labels.append(labels[val])
disp = ConfusionMatrixDisplay.from_predictions(validation_y_labels,
predictions_y_labels)
disp.ax_.set_xticklabels(disp.ax_.get_xticklabels(), rotation=45)
st.pyplot(disp.figure_)
st.markdown(
"""
And here's the one for the testing sample songs.
"""
)
# Evaluate vs testing dataset
model.evaluate(testing_x, testing_y, batch_size=128)
predictions_y = np.argmax(model.predict(testing_x, batch_size=128), axis=1)
testing_y_labels = []
predictions_y_labels = []
for val in testing_y:
testing_y_labels.append(labels[val])
for val in predictions_y:
predictions_y_labels.append(labels[val])
disp = ConfusionMatrixDisplay.from_predictions(testing_y_labels,
predictions_y_labels)
disp.ax_.set_xticklabels(disp.ax_.get_xticklabels(), rotation=45)
st.pyplot(disp.figure_)
st.markdown(
"""
Finally, by pressing the Predict button on the left sidebar we can test our
model and predict the genre of a randomly selected song.
""")
st.sidebar.title("Music genre prediction :guitar:")
st.sidebar.button("Predict", on_click=pick_random_song(model, standard_scaler,
labels))