forked from josedosr/Spytify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheda_funciones.py
More file actions
332 lines (241 loc) · 15.5 KB
/
Copy patheda_funciones.py
File metadata and controls
332 lines (241 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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
import streamlit as st
import pandas as pd
import plotly.express as px
from sklearn.preprocessing import MinMaxScaler
# Definir funciones para análisis y visualización
def popularidad_artista(df, n_artistas=20):
df_pop_art = df.copy()
#df_pop_art["track_artists"] = df_pop_art["track_artists"].apply(lambda x : eval(x) if pd.notna(x) else [])
df_pop_art = df_pop_art.explode("track_artists", ignore_index=True)
df_pop_art = df_pop_art.groupby(by = "track_artists", as_index = False).agg({"track_popularity" : "mean"})\
.sort_values("track_popularity", ascending = False)\
.reset_index(drop = True)
fig = px.bar(data_frame = df_pop_art.iloc[:n_artistas],
x = "track_artists",
y = "track_popularity",
color_discrete_sequence=px.colors.qualitative.Dark2)
fig.update_layout(title="Artist popularity average",
xaxis_title="Artist",
yaxis_title="Popularity average")
fig.update_xaxes(title_font=dict(size=18, family='Arial', color='#e5383b'),
tickfont=dict(size=14, family='Arial', color='#939393'))
fig.update_yaxes(title_font=dict(size=18, family='Arial', color='#e5383b'),
tickfont=dict(size=14, family='Arial', color='#939393'))
return fig
def popularidad_genero(df, n_generos=20):
df = df.copy()
df_pop_gen = df.groupby(by = "playlist_genre", as_index = False).agg({"track_popularity" : "mean"})\
.sort_values("track_popularity", ascending = False)\
.reset_index(drop = True)
fig = px.pie(data_frame = df_pop_gen.iloc[:n_generos],
names = "playlist_genre",
values = "track_popularity",
color_discrete_sequence=px.colors.qualitative.Plotly)
fig.update_layout(title="Average popularity by genre",
legend_title="Genre",
legend=dict(title_font=dict(size=18, family='Arial', color='#e5383b'),
font=dict(size=14, color='#939393')))
return fig
def genero_count(df, n_generos=20):
df_genero_count = df.groupby(by = ["playlist_genre"], as_index = False)\
.agg({"track_id" : "count"})\
.sort_values("track_id", ascending = False)\
.reset_index(drop = True)
fig = px.pie(data_frame = df_genero_count.iloc[:n_generos],
names = "playlist_genre",
values = "track_id",
color_discrete_sequence=px.colors.qualitative.Plotly)
fig.update_layout(title="Songs obtained per genre distribution",
legend_title="Genre",
legend=dict(title_font=dict(size=18, family='Arial', color='#e5383b'),
font=dict(size=14, color='#939393')))
return fig
def popularidad_fecha_genero(df):
df["year"] = df["track_album_release_date"].apply(lambda x: int(x[:4]))
# Filtra el DF para incluir solo las filas con años mayores a 1800
df = df[df["year"] > 1800]
df_funcion = df.groupby(["year", "playlist_genre"], as_index = False).agg({"track_popularity" : "mean"})
fig = px.area(data_frame = df_funcion,
x = "year",
y = "track_popularity",
color = "playlist_genre",
color_discrete_sequence=px.colors.qualitative.Plotly)
fig.update_layout(title="Popularity timeline per genre",
xaxis_title="Year",
yaxis_title="Popularity average")
fig.update_xaxes(title_font=dict(size=18, family='Arial', color='#e5383b'),
tickfont=dict(size=14, family='Arial', color='#939393'))
fig.update_yaxes(title_font=dict(size=18, family='Arial', color='#e5383b'),
tickfont=dict(size=14, family='Arial', color='#939393'))
return fig
def popularidad_playlist(df, n_playlist=20):
"""Media de popularidad por playlist"""
df_funcion = df.groupby("playlist_name", as_index = False)\
.agg({"track_popularity" : "mean"})\
.sort_values("track_popularity", ascending = False)\
.reset_index(drop = True)
fig = px.bar(data_frame = df_funcion.iloc[:n_playlist],
x = "playlist_name",
y = "track_popularity",
color_discrete_sequence=px.colors.qualitative.Set1)
fig.update_layout(title="Popularity per playlist",
xaxis_title="Playlist",
yaxis_title="Popularidad average")
fig.update_traces(marker_color='rgb(105,250,225)', marker_line_color='rgb(8,48,107)',
marker_line_width=1.5, opacity=0.6)
fig.update_layout(bargap=0.1)
fig.update_xaxes(title_font=dict(size=14, family='Arial', color='#e5383b'),
tickfont=dict(size=9.5, family='Arial', color='#939393'))
fig.update_yaxes(title_font=dict(size=14, family='Arial', color='#e5383b'),
tickfont=dict(size=9.5, family='Arial', color='#939393'))
return fig
def histograma_duration_ms(df):
df["duration_ms"] = df["duration_ms"]/(1000*60)
fig = px.histogram(data_frame = df,
x = "duration_ms",
marginal = "box",
color = "playlist_genre",
color_discrete_sequence=px.colors.qualitative.Plotly) #aquí podemos agregar nbins = 20 si queremos
fig.update_layout(title="Track length per genre",
xaxis_title="Track length (minutes)",
yaxis_title="Frequency")
fig.update_xaxes(title_font=dict(size=14, family='Arial', color='#e5383b'),
tickfont=dict(size=9.5, family='Arial', color='#939393'))
fig.update_yaxes(title_font=dict(size=14, family='Arial', color='#e5383b'),
tickfont=dict(size=9.5, family='Arial', color='#939393'))
return fig
def top_artists_plot(df, top_n=10):
"""Generar un gráfico de los artistas más repetidos en la playlist"""
# Explode la columna 'track_artists' para manejar listas en celdas
df_expanded = df.explode("track_artists", ignore_index=True)
# Obtener los artistas más repetidos
top_artists = df_expanded["track_artists"].value_counts().head(top_n)
# Crear un DataFrame para el gráfico
data = pd.DataFrame({
"Artist": top_artists.index,
"Count": top_artists.values
})
# Crear el gráfico de barras
fig = px.bar(data_frame=data,
x="Artist",
y="Count",
labels={"Count": "Tracks"},
title=f"Top {top_n} most repeated artists in the playlist",
height=500,
color_discrete_sequence=px.colors.qualitative.Dark2)
fig.update_xaxes(title_font=dict(size=18, family='Arial', color='#e5383b'),
tickfont=dict(size=14, family='Arial', color='#939393'))
fig.update_yaxes(title_font=dict(size=18, family='Arial', color='#e5383b'),
tickfont=dict(size=14, family='Arial', color='#939393'))
return fig
def line_polar_playlist(df, playlist_names, audio_features):
scaler = MinMaxScaler()
df_normalized = df[['playlist_name'] + audio_features]
df_normalized.iloc[:, 1:] = scaler.fit_transform(df_normalized.iloc[:, 1:])
fig = px.line_polar(title="Playlist Audio Features Average")
for i, playlist_name in enumerate(playlist_names):
df_polar = df_normalized[df_normalized["playlist_name"] == playlist_name]
df_polar = df_polar.groupby("playlist_name", as_index=False).agg({col: "mean" for col in df_polar.columns[1:]})
# Seleccionar un color de las paletas de Plotly
color = px.colors.qualitative.Dark2[i % len(px.colors.qualitative.Dark2)]
fig.add_trace(px.line_polar(data_frame=df_polar.iloc[:, 1:],
r=df_polar.iloc[:, 1:].values[0],
theta=df_polar.columns[1:],
line_close=True,
color_discrete_sequence=[color]).update_traces(name=playlist_name, showlegend=True).data[0])
fig.update_layout(polar=dict(radialaxis=dict(title='Average',
title_font_color='black',
tickfont_color='black',
categoryarray=df_polar.columns[1:])), legend_title='Playlists')
return fig
def line_polar_artists(df, artists_names, audio_features):
scaler = MinMaxScaler()
df_normalized = df[['track_artists'] + audio_features]
df_normalized.iloc[:, 1:] = scaler.fit_transform(df_normalized.iloc[:, 1:])
df_normalized = df_normalized.explode("track_artists", ignore_index=True)
fig = px.line_polar(title='Artist Audio Features Average')
for i, artists_name in enumerate(artists_names):
df_polar = df_normalized[df_normalized['track_artists'] == artists_name]
df_polar = df_polar.groupby('track_artists', as_index=False).agg({col: 'mean' for col in df_polar.columns[1:]})
# Seleccionar un color de las paletas de Plotly
color = px.colors.qualitative.Dark2[i % len(px.colors.qualitative.Dark2)]
fig.add_trace(px.line_polar(data_frame=df_polar.iloc[:, 1:],
r=df_polar.iloc[:, 1:].values[0],
theta=df_polar.columns[1:],
line_close=True,
color_discrete_sequence=[color]).update_traces(name=artists_name, showlegend=True).data[0])
fig.update_layout(polar=dict(radialaxis=dict(title='Average',
title_font_color='black',
tickfont_color='black',
categoryarray=df_polar.columns[1:])), legend_title='Artists')
return fig
# Agregar Streamlit UI
def eda_ui(df):
df_copy = df.copy()
st.plotly_chart(popularidad_artista(df_copy), use_container_width= True)
col1, col2 = st.columns([1, 1])
col1.plotly_chart(popularidad_genero(df_copy), use_container_width= True)
col2.plotly_chart(genero_count(df_copy), use_container_width= True)
st.plotly_chart(popularidad_fecha_genero(df_copy), use_container_width= True)
col1, col2 = st.columns([1, 1])
col1.plotly_chart(popularidad_playlist(df_copy), use_container_width= True)
col2.plotly_chart(histograma_duration_ms(df_copy), use_container_width= True)
st.plotly_chart(top_artists_plot(df_copy), use_container_width= True)
col1, col2 = st.columns([2, 8])
playlists = df_copy["playlist_name"].unique()
#playlist_names = col1.selectbox("Selecciona una playlist", songs_to_recomend["playlist_name"].unique())
playlist_names = col1.multiselect(label="Select playlists you want to analyze",
options = playlists,
default = playlists[:3])
audio_features = ['track_popularity', 'danceability', 'energy', 'key', 'loudness', 'mode','speechiness', 'acousticness', 'instrumentalness', 'liveness', 'valence', 'tempo', 'duration_ms', 'time_signature']
audio_features_graph = col1.multiselect(label="Select the audio features you want to use in the playlist analysis",
options=audio_features,
default=audio_features)
col2.plotly_chart(line_polar_playlist(df_copy, playlist_names, audio_features_graph), use_container_width= True)
st.markdown('''### Audio Features Descriptions
- **Danceability**: indicates how suitable a track is for dancing (values range from 0.0 - 1.0).
- **Energy**: represents the perceptual intensity and activity of a track (values range from 0.0 - 1.0).
- **Key:** estimated overall key of the track mapped to pitches using standards (values range -1 - 11).
- **Loudness:** measures the overall loudness of a track in decibels (dB) (values range -60- 0 dB).
- **Mode:** indicates the modality (major or minor) of a track.
- **Speechiness:** detects the presence of spoken words in a track.
- **Acousticness:** confidence measure from 0.0 - 1.0 indicating whether the track is acoustic.
- **Instrumentalness:** predicts whether a track contains no vocals.
- **Liveness:** detects the presence of an audience in the recording.
- **Valence:** describes the musical positiveness conveyed by a track (values range 0.0 - 1.0).
- **Tempo:** overall estimated tempo of a track in beats per minute (BPM).
- **Duration:** duration of the song in milliseconds.
If you want to review more information about the audio features review the [Spotify API Documentation - Audio Features](https://developer.spotify.com/documentation/web-api/reference/get-audio-features)''')
if __name__ == "__eda_ui__":
eda_ui()
# Agregar Streamlit UI
def eda_playlist_query(df):
df_copy = df.copy()
st.plotly_chart(popularidad_artista(df_copy), use_container_width= True)
st.plotly_chart(top_artists_plot(df_copy), use_container_width= True)
col1, col2 = st.columns([2, 8])
artists = df_copy.explode('track_artists', ignore_index=True)['track_artists'].sort_values().unique()
artists_names = col1.multiselect(label="Select playlists you want to analyze",
options = artists,
default = artists[:3])
audio_features = ['track_popularity', 'danceability', 'energy', 'key', 'loudness', 'mode','speechiness', 'acousticness', 'instrumentalness', 'liveness', 'valence', 'tempo', 'duration_ms', 'time_signature']
audio_features_graph = col1.multiselect(label="Select the audio features you want to use in the playlist analysis",
options=audio_features,
default=audio_features)
col2.plotly_chart(line_polar_artists(df_copy, artists_names, audio_features_graph), use_container_width= True)
st.markdown('''### Audio Features Descriptions
- **Danceability**: indicates how suitable a track is for dancing (values range from 0.0 - 1.0).
- **Energy**: represents the perceptual intensity and activity of a track (values range from 0.0 - 1.0).
- **Key:** estimated overall key of the track mapped to pitches using standards (values range -1 - 11).
- **Loudness:** measures the overall loudness of a track in decibels (dB) (values range -60- 0 dB).
- **Mode:** indicates the modality (major or minor) of a track.
- **Speechiness:** detects the presence of spoken words in a track.
- **Acousticness:** confidence measure from 0.0 - 1.0 indicating whether the track is acoustic.
- **Instrumentalness:** predicts whether a track contains no vocals.
- **Liveness:** detects the presence of an audience in the recording.
- **Valence:** describes the musical positiveness conveyed by a track (values range 0.0 - 1.0).
- **Tempo:** overall estimated tempo of a track in beats per minute (BPM).
- **Duration:** duration of the song in milliseconds.
If you want to review more information about the audio features review the [Spotify API Documentation - Audio Features](https://developer.spotify.com/documentation/web-api/reference/get-audio-features)''')
if __name__ == "__eda_playlist_query__":
eda_playlist_query()