Team Ai
Apppublic

Ezhil24/Data_Visualization_SPOTIFY

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
visualizations.cpython-310.pyc139 linesDownload Raw Back to __pycache__
1o

2��g�R�@s�ddlZddlZddlmZddlZddl	m3ZddlZ
ddlmZddlmZdd�Zdd�Zdd�Zd	d4�Zdd�Zd
d�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�ZdS)�N)�combinationsc	Cs>t�d�t�ddg�\}}|�=tjddd�d|jvrA|�d�d����}tj	|ddd	d5gd�}|j6dd
dd�t�|�nt�d�Wd�n1sPwY|�>tjddd�d|jvr�tj
|dddddgdgd�}|j7dd
dd�t�|�n
t�d�Wd�dSWd�dS1s�wYdS)NzPopularity Trends Over TimezAverage PopularityzIndividual Songsz�<span style='color:blue'>**Average Popularity by Decade**</span>: Tracks how song popularity has <span style='color:red'>changed over time</span>. This <span style='color:green'>blue</span> line chart highlights peaks.T��unsafe_allow_html�Decade�8PopularityzAverage Popularity by Decade�blue��x�y�title�color_discrete_sequence�plotly_white� ���template�width�height�%Cannot plot: 'Decade' column missing.z�<span style='color:blue'>**Song Popularity Over Time**</span>: Highlights individual trends with <span style='color:red'>red</span> points, showing <span style='color:green'>green</span> details on hover.�YearzSong Popularity Over Time�9Track Name�Artist Name(s)�red�r	r10r�11hover_datar�#Cannot plot: 'Year' column missing.��st�header�tabs�markdown�columns�groupby�mean�reset_index�px�line�
update_layout�plotly_chart�error�scatter)�df�tab1�tab2�avg_pop_by_decade�fig1�fig2�r1�IC:\Users\User\Desktop\Python\Capstone_project\functions\visualizations.py�generate_popularity_trendssD1213���14�15��16���"�r3c	Cs�t�d�t�dgd��}t�gd��\}}}|�2tjd|�d|���d�dd	�tj||d17|��dgd�}|jd
ddd�t�	|�Wd�n1sNwY|�>tjd|�d|���d�dd	�d|j18vr�tj|d||�d�dgd�}|jd
ddd�t�	|�nt�d�Wd�n1s�wY|�$tjddd	�t
��\}}t�|gd��t�|�Wd�dS1s�wYdS)NzAudio Features AnalysiszSelect Feature)�Danceability�Energy�Tempo�Loudness)�Distribution�	By Decade�Correlationsz+<span style='color:blue'>**Distribution of z6**</span>: Shows variation in <span style='color:red'>z9</span> with <span style='color:green'>green</span> bars.TrzDistribution of �green�r	rrr
rrrz<span style='color:blue'>**z6 by Decade**</span>: Compares <span style='color:red'>zI</span> across decades with <span style='color:green'>green</span> boxes.rz Distribution by Decaderrz�<span style='color:blue'>**Feature Correlations**</span>: Explores relationships with <span style='color:red'>multi-colored</span> scatter points.)r5r4�Valencer6)rr�	selectboxrr �lowerr%�	histogramr'r(r!�boxr)�plt�subplots�sns�pairplot�pyplot)	r+�featurer,r-�tab3�fig3�fig4�fig�axr1r1r2�generate_audio_features$sD19�����2021�22��23"�rMc	Cst�d�t�gd��\}}}|�Stjddd�d|jvrX|�d��ddg���jdd	�}|�d��	d24d��jdd�}t25j|dddd
t26jj
jd�}|jdddd�t�|�nt�d�Wd�n1sgwY|�3tjddd�|�d�d����}t27j|dddt28jj
jd�}|jddd�t�|�Wd�n1s�wY|�Ltjddd�d|jvr�|�d�d����}	t29jtj|dddd��d�dd d!�}30|31jddd�t�|32�n
t�d"�Wd�dSWd�dS1s�wYdS)#NzGenre & Artist Analysis)z33Top Genres�Genre DistributionzArtist Popularityz�<span style='color:blue'>**Top Genres by Decade**</span>: Shows frequent genres with <span style='color:red'>red</span> bars, <span style='color:green'>green</span> highlights.Trr�Genres�Count)�namecSs|�dd�S)N�rP)�nlargest)r	r1r1r2�<lambda>Ksz)generate_genre_analysis.<locals>.<lambda>)�dropzTop Genres by Decade)r	r34�colorrrr
rrrrz�<span style='color:blue'>**Genre Distribution**</span>: Breaks down genres with <span style='color:red'>multi-colored</span> pie slices.�countrN)�values�namesrr�rrz�<span style='color:blue'>**Artist Popularity Heatmap**</span>: Visualizes popularity with <span style='color:red'>red</span> intensity.rrr#)rX�index�aggfuncrzArtist Popularity Heatmap�Reds�r�color_continuous_scale�-Cannot plot: 'Artist Name(s)' column missing.)rrrr r!�exploder"�sizer$�applyr%�bar�colors�qualitative�Set1r'r(r)�value_counts�pie�Set2r#�imshow�pd�pivot_table�fillna)r+r,r-rH�genre_decade�35top_genres�fig5�genre_counts�fig6�36artist_pop�fig7r1r1r2�generate_genre_analysisAsv3738�3940��41��42�43��
���4445��46�����"�rvcCs�t�d�tjddd�d|jvr>d|jvr>|�ddg������d�}tj	|dd	d47dgd�}|j48d
ddd�t�|�dSt�d�dS)NzExplicit Content Trendsz�<span style='color:blue'>**Explicit vs Non-Explicit Songs**</span>: Compares content with <span style='color:red'>stacked bars</span> in <span style='color:green'>green</span> and <span style='color:purple'>purple</span>.Trr�Explicitr�stackz(Explicit vs Non-Explicit Songs by Decader;�purple)�barmoderrr
rrrz3Cannot plot: 'Decade' or 'Explicit' column missing.)
rrr r!r"rb�unstackrnr%rdr'r(r))r+�explicit_by_decade�fig8r1r1r2�generate_explicit_trendsgs49���r~c50Csdt�d�t�ddg�\}}|�=tjddd�d|jvrA|d���d���}tj	|dd	d51dgd�}|j52d
ddd�t�|�nt�d�Wd�n1sPwY|�Qtjddd�d|jvr�d|jvr�|�
d�d�dd	g���}tj|d	dddgddgd�}|j53d
ddd�t�|�n
t�d�Wd�dSWd�dS1s�wYdS)NzAlbum & Label Insightsz54Top LabelszAlbum Popularityzw<span style='color:blue'>**Top Record Labels**</span>: Identifies labels with <span style='color:red'>blue</span> bars.Tr�Label�55rWzTop Record Labels by Song Countrrr
rrrz$Cannot plot: 'Label' column missing.zs<span style='color:blue'>**Album Popularity**</span>: Shows albums with <span style='color:red'>red</span> bubbles.z56Album Namerr#z(Albums: Song Count vs Average Popularityr)r	r57rbrrrz9Cannot plot: 'Album Name' or 'Popularity' column missing.)rrrr r!rhrSr$r%rdr'r(r)r"�aggr*)r+r,r-�58top_labels�fig9�	album_pop�fig10r1r1r2�generate_album_insightsusB596061�62��63�����"�r�c	CsPt�d�t�ddg�\}}|�Btjddd�d|jvrFd|jvrF|�d�d����}tj	|ddd	d64gd�}|j65dd
dd�t�|�nt�d�Wd�n1sUwY|�Btjddd�d|jvr�d|jvr�tj
|ddddgdgd�}|j66dd
dd�t�|�n
t�d�Wd�dSWd�dS1s�wYdS)NzTempo & Mood AnalysiszTempo TrendszMood Scatterzq<span style='color:blue'>**Tempo Trends**</span>: Tracks changes with <span style='color:red'>orange</span> line.Trrr6zAverage Tempo Over Time�orangerr
rrrz.Cannot plot: 'Year' or 'Tempo' column missing.zu<span style='color:blue'>**Valence vs Energy**</span>: Groups mood with <span style='color:red'>purple</span> points.r=r5zValence vs Energyrryrz2Cannot plot: 'Valence' or 'Energy' column missing.r)r+r,r-�
tempo_by_year�fig11�fig12r1r1r2�generate_tempo_mood�s86768�69��70���"�r�c	CsXt�d�t�ddg�\}}|�=tjddd�d|jvrA|d���d���}tj	|dd	d71dgd�}|j72d
ddd�t�|�nt�d�Wd�n1sPwY|�Ktjddd�d|jvr�d|jvr�|�dd�ddg}tj	|dddddgd�}|j73d
ddd�t�|�n
t�d�Wd�dSWd�dS1s�wYdS)NzTop Artists and SongszTop Artistsz	Top Songszx<span style='color:blue'>**Most Featured Artists**</span>: Shows artists with <span style='color:red'>green</span> bars.Trrr�rWzMost Featured Artistsr;rr
rrrr`zl<span style='color:blue'>**Top 10 Songs**</span>: Lists songs with <span style='color:red'>blue</span> bars.rr�hzTop 10 Songs by Popularityr)r74r	�orientationrrz9Cannot plot: 'Track Name' or 'Popularity' column missing.)
rrrr r!rhrSr$r%rdr'r(r))r+r,r-�top_artists�fig13�	top_songs�fig14r1r1r2�generate_top_artists_songs�sF7576��77�78���79���	"�r�cCsTt�d�t�ddg�\}}|�<tjddd�d|jvr@|d������}tj	|ddd	d80gd�}|j81dd
dd�t�|�nt�d�Wd�n1sOwY|�Jtjddd�d|jvr�d|jvr�|�
ddg������d�}tj|ddd�}|j82d
dd�t�|�n
t�d�Wd�dSWd�dS1s�wYdS)NzAlbum Release TrendszAlbums per YearzArtist-Year Heatmapzu<span style='color:blue'>**Albums per Year**</span>: Tracks releases with <span style='color:red'>purple</span> line.TrrrWz"Number of Albums Released per Yearryrr
rrrrz~<span style='color:blue'>**Songs by Artists and Years**</span>: Visualizes with <span style='color:red'>heatmap colors</span>.rrz&Songs Released by Artists Across Years�Viridisr^rZz7Cannot plot: 'Artist Name(s)' or 'Year' column missing.)rrrr r!rh�83sort_indexr$r%r&r'r(r)r"rbr{rnrk)r+r,r-�albums_per_year�fig15�artist_year�fig16r1r1r2�generate_album_release_trends�sB848586�87�88������"�r�cCs*t�d�t�ddg�\}}|�1tjddd�d|jvr5tj|ddd	gd89�}|jddd
d�t�|�nt�	d�Wd�n1sDwY|�@tjddd�d|jvrvd|jvrvtj90|ddddgd�}|jddd
d�t�|�n
t�	d�Wd�dSWd�dS1s�wYdS)NzTrack Duration Analysisr8r9z<span style='color:blue'>**Track Duration Distribution**</span>: Shows lengths with <span style='color:red'>orange</span> bars.Tr�Track Duration (ms)zDistribution of Track Durationsr�r<r
rrrz2Cannot plot: 'Track Duration (ms)' column missing.zq<span style='color:blue'>**Duration by Decade**</span>: Compares with <span style='color:red'>green</span> boxes.rzTrack Duration by Decader;rz>Cannot plot: 'Decade' or 'Track Duration (ms)' column missing.)rrrr r!r%r@r'r(r)rA)r+r,r-�fig17�fig18r1r1r2�generate_duration_analysis�s69192�93��	94���"�r�cCsLt�d�t�ddg�\}}|�7tjddd�d|jvr;d|jvr;tj|ddd	d95gd�}|jdd
dd�t�|�nt�	d�Wd�n1sJwY|�Ktjddd�d|jvr�d|jvr�|�96d�d����}tj
|ddddgd�}|jdd
dd�t�|�n
t�	d�Wd�dSWd�dS1s�wYdS)Nz!Streaming and Engagement InsightszPopularity vs DurationzTime Signaturez}<span style='color:blue'>**Popularity vs Duration**</span>: Explores trends with <span style='color:red'>blue</span> scatter.Trr�rzPopularity vs Track Durationrrr
rrrzBCannot plot: 'Track Duration (ms)' or 'Popularity' column missing.z{<span style='color:blue'>**Popularity by Time Signature**</span>: Compares with <span style='color:red'>purple</span> bars.z$Average Popularity by Time Signatureryz=Cannot plot: 'Time Signature' or 'Popularity' column missing.)rrrr r!r%r*r'r(r)r"r#r$rd)r+r,r-�fig19�pop_by_time�fig20r1r1r2�generate_streaming_insights�sD9798����99��100���"�r�c101Cslt�d�t�ddg�\}}|�Gtjddd�d|jvrK|�d�gd�����}tj	|j102dd	�dd103ddd
tjjj
d�}|jdddd�t�|�nt�d�Wd�n1sZwY|�Ktjddd�d|jvr�d|jvr�|�d�d����}tj|ddddgd�}|jdddd�t�|�n
t�d�Wd�dSWd�dS1s�wYdS)Nz"Feature Comparisons Across DecadeszFeature ComparisonzLoudness Trendsz�<span style='color:blue'>**Feature Comparison**</span>: Compares features with <span style='color:red'>multi-colored</span> bars.Trr)r4r5r=)�id_vars�value�variable�groupzFeature Comparison by Decade)r	r104rVrzrrr
rrrrzn<span style='color:blue'>**Loudness Over Time**</span>: Tracks with <span style='color:red'>green</span> line.rr7zAverage Loudness Over Timer;rz1Cannot plot: 'Year' or 'Loudness' column missing.)rrrr r!r"r#r$r%rd�meltrerf�Pastelr'r(r)r&)r+r,r-�features_by_decade�fig21�loudness_by_year�fig22r1r1r2�generate_feature_comparisonssN105106����107�����108��"�r�c
s8t�d�t�ddg�\}}|��tjddd�d|jvr�|d���t�}t�	�}|D]"}dd	�|�109d110�D�}t|�dkrNt|d�D]111\}}|�
||�qCq,|��d
kr�t�|��g}	g}112|��D]"}�|d
\}}
�|d\}}|	�||dg�|113�|
|dg�qbtj|	|114tddd�ddd�}�fdd	�|��D�}�fdd	�|��D�}tj||ddtddd�t|���dd�}tj||gtjdddtd
d
d
dd �d!d"d#�d$�}t�|�nt�d%�nt�d&�Wd�n1s�wY|�tjd'dd�t�d(�t�d)�Wd�dS1�swYdS)*NzNetwork AnalysiszArtist CollaborationszGenre Crossoverz�<span style='color:blue'>**Artist Collaborations**</span>: Visualizes connections with <span style='color:red'>interactive red nodes</span>. Hover for details.TrrcSsg|]115}|��r|���qSr1)�strip)�.0�ar1r1r2�116<listcomp>6s�z-generate_network_analysis.<locals>.<listcomp>�,��rg�?z#888)rrV�none�lines)r	r117r&�	hoverinfo�modec�g|]}�|d�qS)rr1�r��node��posr1r2r�M�cr�)r�r1r�r�r1r2r�Nr�zmarkers+text�textr�r)rbrVz118top center)r	r119r�r��markerr��textpositionF�closest�()�b�l�r�triX)r�120showlegend�	hovermode�marginrr)�data�layoutz$No artist collaborations to display.r`z�<span style='color:blue'>**Genre Crossover**</span>: Placeholder with <span style='color:red'>future multi-color</span> potential.z=To implement, install `holoviews` and use the following code:a�121        import holoviews as hv122        hv.extension('bokeh')123        genre_pairs = df.explode('Genres')[['Genres']].merge(df.explode('Genres')[['Genres']], how='cross')124        chord_data = genre_pairs.groupby(['Genres_x', 'Genres_y']).size().reset_index(name='value')125        chord = hv.Chord(chord_data).opts(title="Genre Crossover")126        st.write(hv.render(chord, backend='bokeh'))127        )rrrr r!�dropna�astype�str�nx�Graph�split�lenr�add_edge�number_of_nodes�
spring_layout�edges�extend�go�Scatter�dict�nodes�list�Figure�Layoutr(�warningr)�write�code)r+r,r-�
valid_artists�G�artists�artists_list�a1�a2�edge_x�edge_y�edge�x0�y0�x1�y1�128edge_trace�node_x�node_y�129node_tracerKr1r�r2�generate_network_analysis,sv130131132��133134�135136�137��138��4139$�r�)�	streamlitr�pandasrl�plotly.express�expressr%�seabornrD�matplotlib.pyplotrFrB�networkxr��plotly.graph_objects�
graph_objectsr��	itertoolsrr3rMrvr~r�r�r�r�r�r�r�r�r1r1r1r2�<module>s(&