{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import librosa\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport plotly.express as px\nimport plotly.graph_objs as go\nfrom sklearn import preprocessing\nimport matplotlib.pyplot as plt\nfrom IPython.display import Audio","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-06T06:53:28.683760Z","iopub.execute_input":"2023-04-06T06:53:28.684258Z","iopub.status.idle":"2023-04-06T06:53:28.731386Z","shell.execute_reply.started":"2023-04-06T06:53:28.684204Z","shell.execute_reply":"2023-04-06T06:53:28.730181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Reference: [BURHANUDDIN LATSAHEB](https://www.kaggle.com/code/burhanuddinlatsaheb/eda-visualizations-audio-exploration/notebook)\n\n#### What is New\n* Audio EDA\n    * Spectral Centroids\n    * Spectral Bandwiths","metadata":{}},{"cell_type":"code","source":"meta_path = \"/kaggle/input/birdclef-2023/train_metadata.csv\"\n\nmeta_df = pd.read_csv(meta_path)\nmeta_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-06T06:25:47.948535Z","iopub.execute_input":"2023-04-06T06:25:47.950161Z","iopub.status.idle":"2023-04-06T06:25:48.123931Z","shell.execute_reply.started":"2023-04-06T06:25:47.950076Z","shell.execute_reply":"2023-04-06T06:25:48.122935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta_df.info()","metadata":{"execution":{"iopub.status.busy":"2023-04-06T06:26:39.553203Z","iopub.execute_input":"2023-04-06T06:26:39.553646Z","iopub.status.idle":"2023-04-06T06:26:39.589786Z","shell.execute_reply.started":"2023-04-06T06:26:39.553608Z","shell.execute_reply":"2023-04-06T06:26:39.588814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta_df.describe().T","metadata":{"execution":{"iopub.status.busy":"2023-04-06T06:26:55.582342Z","iopub.execute_input":"2023-04-06T06:26:55.583144Z","iopub.status.idle":"2023-04-06T06:26:55.609942Z","shell.execute_reply.started":"2023-04-06T06:26:55.583089Z","shell.execute_reply":"2023-04-06T06:26:55.608637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.histogram(meta_df, x=\"primary_label\", nbins=len(meta_df[\"primary_label\"].unique()))\nfig.update_layout(title_text=\"Distribution of Primary Labels\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-06T06:27:13.688363Z","iopub.execute_input":"2023-04-06T06:27:13.688791Z","iopub.status.idle":"2023-04-06T06:27:15.454397Z","shell.execute_reply.started":"2023-04-06T06:27:13.688751Z","shell.execute_reply":"2023-04-06T06:27:15.453470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.histogram(meta_df, x=\"secondary_labels\", nbins=len(meta_df[\"primary_label\"].unique()))\nfig.update_layout(title_text=\"Distribution of Primary Labels\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-06T06:27:45.192433Z","iopub.execute_input":"2023-04-06T06:27:45.193197Z","iopub.status.idle":"2023-04-06T06:27:45.586721Z","shell.execute_reply.started":"2023-04-06T06:27:45.193154Z","shell.execute_reply":"2023-04-06T06:27:45.585252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.histogram(meta_df, x=\"scientific_name\", nbins=len(meta_df[\"scientific_name\"].unique()))\nfig.update_layout(title_text=\"Distribution of Scientific Names\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-06T06:28:37.251155Z","iopub.execute_input":"2023-04-06T06:28:37.251547Z","iopub.status.idle":"2023-04-06T06:28:37.382292Z","shell.execute_reply.started":"2023-04-06T06:28:37.251495Z","shell.execute_reply":"2023-04-06T06:28:37.380892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.histogram(meta_df, x=\"common_name\", nbins=len(meta_df[\"scientific_name\"].unique()))\nfig.update_layout(title_text=\"Distribution of Common Names\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-06T06:29:11.403578Z","iopub.execute_input":"2023-04-06T06:29:11.404738Z","iopub.status.idle":"2023-04-06T06:29:11.538270Z","shell.execute_reply.started":"2023-04-06T06:29:11.404676Z","shell.execute_reply":"2023-04-06T06:29:11.536974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.histogram(meta_df, x=\"rating\", nbins=len(meta_df[\"rating\"].unique()) , color_discrete_sequence=['red'])\nfig.update_layout(title_text=\"Distribution of Ratings\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-06T06:29:29.448076Z","iopub.execute_input":"2023-04-06T06:29:29.448508Z","iopub.status.idle":"2023-04-06T06:29:29.515475Z","shell.execute_reply.started":"2023-04-06T06:29:29.448467Z","shell.execute_reply":"2023-04-06T06:29:29.514234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# drop columns from correlation matrix\ncorr = meta_df.corr()\n\n# create correlation heatmap\nfig = px.imshow(corr,\n                labels=dict(x=\"Columns\", y=\"Columns\", color=\"Correlation\"),\n                x=corr.columns,\n                y=corr.columns,\n                color_continuous_scale='RdBU',\n                zmin=-1,\n                zmax=1,\n                title=\"Correlation Heatmap\")\n                \n# add text annotations\nannotations = []\nfor i, row in enumerate(corr.values):\n    for j, value in enumerate(row):\n        text = '{:.2f}'.format(value)\n        annotations.append(dict(x=corr.columns[j], y=corr.columns[i], text=text, showarrow=False))\n\nfig.update_layout(width=800, height=800)\nfig.update_traces(showscale=True, colorbar_thickness=25, colorbar_len=0.75)\nfig.update_layout(margin=dict(l=50, r=50, b=100, t=100, pad=4))\nfig.update_layout(annotations=annotations)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-06T06:29:54.831339Z","iopub.execute_input":"2023-04-06T06:29:54.831805Z","iopub.status.idle":"2023-04-06T06:29:54.952112Z","shell.execute_reply.started":"2023-04-06T06:29:54.831764Z","shell.execute_reply":"2023-04-06T06:29:54.950859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.scatter(meta_df, x=\"longitude\", y=\"latitude\", color=\"common_name\")\nfig.update_layout(title=\"Distribution of Recordings by Location\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-06T06:30:17.364182Z","iopub.execute_input":"2023-04-06T06:30:17.364589Z","iopub.status.idle":"2023-04-06T06:30:18.298227Z","shell.execute_reply.started":"2023-04-06T06:30:17.364549Z","shell.execute_reply":"2023-04-06T06:30:18.297307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.scatter_mapbox(meta_df, lat=\"latitude\", lon=\"longitude\", color=\"common_name\",\n                        hover_name=\"filename\", hover_data=[\"common_name\", \"author\", \"rating\"],\n                        zoom=3, height=500)\nfig.update_layout(mapbox_style=\"open-street-map\")\nfig.update_layout(margin={\"r\":0,\"t\":0,\"l\":0,\"b\":0})\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-06T06:30:45.229521Z","iopub.execute_input":"2023-04-06T06:30:45.229959Z","iopub.status.idle":"2023-04-06T06:30:46.680520Z","shell.execute_reply.started":"2023-04-06T06:30:45.229913Z","shell.execute_reply":"2023-04-06T06:30:46.679228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.density_mapbox(meta_df, lat='latitude', lon='longitude', radius=10,\n                        center=dict(lat=47.6, lon=-122.3), zoom=7,\n                        mapbox_style=\"stamen-terrain\")\nfig.update_layout(title_text=\"Distribution of Bird Sightings\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-06T06:31:11.605022Z","iopub.execute_input":"2023-04-06T06:31:11.605444Z","iopub.status.idle":"2023-04-06T06:31:11.690640Z","shell.execute_reply.started":"2023-04-06T06:31:11.605410Z","shell.execute_reply":"2023-04-06T06:31:11.689406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = \"/kaggle/input/birdclef-2023/eBird_Taxonomy_v2021.csv\"\ntrain_species_df = pd.read_csv(train_path)\ntrain_species_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-06T06:31:53.868342Z","iopub.execute_input":"2023-04-06T06:31:53.868810Z","iopub.status.idle":"2023-04-06T06:31:53.960234Z","shell.execute_reply.started":"2023-04-06T06:31:53.868769Z","shell.execute_reply":"2023-04-06T06:31:53.959358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Histogram of the taxonomic order counts\nfig1 = px.histogram(train_species_df, x=\"TAXON_ORDER\", color_discrete_sequence=['aquamarine'])\nfig1.update_layout(title_text=\"Distribution of Taxonomic Orders\")\n\n# Bar plot of the species group counts\n# Box plot of the taxonomic order counts by category\nfig3 = px.box(train_species_df, x=\"CATEGORY\", y=\"TAXON_ORDER\", color_discrete_sequence=['red'])\nfig3.update_layout(title_text=\"Taxonomic Order Distribution by Category\")\n\n# Scatter plot of the taxonomic order counts by family\nfig4 = px.scatter(train_species_df, x=\"FAMILY\", y=\"TAXON_ORDER\")\nfig4.update_layout(title_text=\"Taxonomic Order Distribution by Family\")\n\n# Show the plots\nfig1.show()\nfig3.show()\nfig4.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-06T06:32:05.923300Z","iopub.execute_input":"2023-04-06T06:32:05.923688Z","iopub.status.idle":"2023-04-06T06:32:06.259539Z","shell.execute_reply.started":"2023-04-06T06:32:05.923652Z","shell.execute_reply":"2023-04-06T06:32:06.258545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def audio_eda(audio_path):\n   \n    # Load an audio file\n    samples, sample_rate = librosa.load(audio_path)\n\n    # Visualize the waveform\n    plt.figure(figsize=(14, 5))\n    librosa.display.waveshow(samples, sr=sample_rate)\n    plt.title('Waveform')\n    \n    # compute spectral centroids\n    spectral_centroids = librosa.feature.spectral_centroid(y=samples, sr=sample_rate)[0]\n    plt.figure(figsize=(14, 5))\n    frames = range(len(spectral_centroids))\n    t = librosa.frames_to_time(frames)\n    def normalize(x, axis=0):\n        return preprocessing.minmax_scale(x, axis=axis)\n    #Plotting the Spectral Centroid along the waveform\n    librosa.display.waveshow(samples, sr=sample_rate, alpha=0.4)\n    plt.plot(t, normalize(spectral_centroids), color='b')\n    plt.title('Spectral Centroids')\n    \n    # spectral bandwidths\n    spectral_bandwidth_2 = librosa.feature.spectral_bandwidth(y=samples+0.01, sr=sample_rate)[0]\n    spectral_bandwidth_3 = librosa.feature.spectral_bandwidth(y=samples+0.01, sr=sample_rate, p=3)[0]\n    spectral_bandwidth_4 = librosa.feature.spectral_bandwidth(y=samples+0.01, sr=sample_rate, p=4)[0]\n    plt.figure(figsize=(15, 9))\n    librosa.display.waveshow(samples, sr=sample_rate, alpha=0.4)\n    plt.plot(t, normalize(spectral_bandwidth_2), color='r')\n    plt.plot(t, normalize(spectral_bandwidth_3), color='g')\n    plt.plot(t, normalize(spectral_bandwidth_4), color='y')\n    plt.legend(('p = 2', 'p = 3', 'p = 4'))\n    plt.title('Spectral Bandwidths')\n\n    # Compute the spectrogram\n    spectrogram = librosa.stft(samples)\n    spectrogram_db = librosa.amplitude_to_db(abs(spectrogram))\n\n    # Visualize the spectrogram\n    plt.figure(figsize=(14, 5))\n    librosa.display.specshow(spectrogram_db, sr=sample_rate, x_axis='time', y_axis='log')\n    plt.colorbar(format='%+2.0f dB')\n    plt.title('Spectrogram (dB)')\n\n    # Compute the mel spectrogram\n\n\n    # Visualize the mel spectrogram\n    S = librosa.feature.melspectrogram(y=samples, sr=sample_rate)\n\n    # Visualize mel spectrogram\n    plt.figure(figsize=(10, 4))\n    librosa.display.specshow(librosa.power_to_db(S, ref=np.max), y_axis='mel', fmax=8000, x_axis='time')\n    plt.colorbar(format='%+2.0f dB')\n    plt.title('Mel spectrogram')\n    plt.tight_layout()\n\n\n    # Compute the chromagram\n    chromagram = librosa.feature.chroma_stft( y = samples , sr = sample_rate)\n\n    # Visualize the chromagram\n    plt.figure(figsize=(14, 5))\n    librosa.display.specshow(chromagram, sr=sample_rate, x_axis='time', y_axis='chroma')\n    plt.colorbar()\n    plt.title('Chromagram')\n\n    # Compute the MFCCs\n    mfccs = librosa.feature.mfcc(y=samples, sr=sample_rate, n_mfcc=13)\n\n    # Visualize the MFCCs\n    plt.figure(figsize=(14, 5))\n    librosa.display.specshow(mfccs, sr=sample_rate, x_axis='time')\n    plt.colorbar()\n    plt.title('MFCCs')\n\n    # Show the plots\n    display(Audio(samples, rate=sample_rate))\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-06T06:56:15.308563Z","iopub.execute_input":"2023-04-06T06:56:15.309034Z","iopub.status.idle":"2023-04-06T06:56:15.330059Z","shell.execute_reply.started":"2023-04-06T06:56:15.308995Z","shell.execute_reply":"2023-04-06T06:56:15.328756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio_eda(\"/kaggle/input/birdclef-2023/train_audio/bawman1/XC115075.ogg\")","metadata":{"execution":{"iopub.status.busy":"2023-04-06T06:56:15.547057Z","iopub.execute_input":"2023-04-06T06:56:15.547817Z","iopub.status.idle":"2023-04-06T06:56:19.654680Z","shell.execute_reply.started":"2023-04-06T06:56:15.547763Z","shell.execute_reply":"2023-04-06T06:56:19.653388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio_eda(\"/kaggle/input/birdclef-2023/train_audio/abethr1/XC128013.ogg\")","metadata":{"execution":{"iopub.status.busy":"2023-04-06T06:58:05.377163Z","iopub.execute_input":"2023-04-06T06:58:05.377599Z","iopub.status.idle":"2023-04-06T06:58:11.289224Z","shell.execute_reply.started":"2023-04-06T06:58:05.377561Z","shell.execute_reply":"2023-04-06T06:58:11.288030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}