{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"papermill":{"default_parameters":{},"duration":863.978739,"end_time":"2023-10-05T08:14:40.177924","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2023-10-05T08:00:16.199185","version":"2.4.0"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":70203,"databundleVersionId":8068726,"sourceType":"competition"}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<p style=\"text-align: center; color:#E1B12D; font-family: 'verdana'; font-size:28px; font-weight: bold;\">BirdCLEF 2024</p>\n\n<center><img src='https://i.pinimg.com/originals/02/e9/cf/02e9cf66d4533fee576220176cc36e8a.gif' height=100px width=450px /></center>","metadata":{}},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; padding: 12px; background-color: #; font-size:120%; text-align:left\">\n\n    \n<p style=\"color: #E1B12D; font-family: 'verdana'; font-size: 20px; font-weight: bold;\">Introduction:</p>\n    \n\nThe Western Ghats, a UNESCO World Heritage Site and one of the world's eight \"hottest hotspots\" of biological diversity, also known as the Sahyadri Mountains, extends along the western coast of India, covering an area of approximately 160,000 square kilometers.<br>\n        \nPeninsular India, primarily comprising of the Southern states of Andhra Pradesh, Maharashtra, Goa, Karnataka, Kerala & Tamil Nadu lie the Western Ghats - India's oldest mountain range and one of the most biogeographically rich places in the world. It's biomass goes toe to toe with that of the Amazon and species diversity is similar.<br>\n    \nDemarcated by the Deccan plateau of Central India on its Northern Side, the habitat of the Western Ghats consists of Tropical Rainforests, Deciduous Forests, Shola Grasslands, Scrub Forests as well as Montane Forests.This mountain range is characterized by its unique topography, diverse climate patterns, and rich biodiversity, making it a globally significant ecological hotspot. <br><br>\n    \nWhy are the Western Ghats and the Nilgiri Hills so important?<br>\n• It houses 3500 Plant species, of out which 1500 species are endemic to the area<br>\n• 75% of all Amphibians of India occur here.<br>\n• More than 50% of all Reptiles of India are found here<br>\n• More than 300 species of Butterflies of India occur here<br>\n• Over 100 species of Mammals<br>\n• Almost 600 bird species of India out of which 28 species are endemic to the area.<br>\n    \nThe altitude of the Western Ghats reaches up to 1600m above sea level.<br><br>\n        \n    \n<center><img src='https://imgur.com/jEeHqbK.jpg' height=100px width=900px /></center>\n\n    \n<br><br>\nSeveral endemic and endangere d bird species are found exclusively in the Western Ghats, including the Malabar parakeet, Nilgiri wood-pigeon, and Malabar trogon. The region's biodiversity is intricately linked to its cultural heritage, as local communities depend on the forests and natural resources for their livelihoods and cultural practices.<br>\n        \nHowever, the region is facing increasing threats from anthropogenic activities such as habitat destruction, fragmentation, and climate change, which are putting immense pressure on its biodiversity.<br>\n\nFrom an avifaunal perspective, this region is home to high levels of bird diversity, with several endemic and endangered species found nowhere else. However, this mountain range is undergoing drastic landscape and climatic changes that negatively affect biodiversity. Hence, we need conservation technologies and tools to help us assess and monitor bird diversity rapidly.<br><br>\n\nThe broader goals for this Kaggle competition include:<br>\n(1) Identify endemic bird species of the sky-islands of the Western Ghats in soundscape data.<br>\n(2) Detect/classify endangered bird species (species of conservation concern) featuring limited training data.<br>\n(3) Detect/classify nocturnal bird species which are poorly understood.<br>\n</div>\n    \n","metadata":{"papermill":{"duration":0.004664,"end_time":"2023-10-05T08:00:19.333066","exception":false,"start_time":"2023-10-05T08:00:19.328402","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"<p style=\"color: #E1B12D; font-family: 'verdana'; font-size: 20px; font-weight: bold;\">Data Description:</p>\n\n**Data Description for Train Metadata**\n\n| Feature Name      | Data Type | Description                               |\n|-------------------|-----------|-------------------------------------------|\n| primary_label     | Object    | Primary label associated with the bird species                |\n| secondary_labels  | Object    | Secondary labels associated with the bird species |\n| type              | Object    | Type or category of the recording(song, call etc)        |\n| latitude          | Float64   | Latitude coordinates of recording location |\n| longitude         | Float64   | Longitude coordinates of recording location|\n| scientific_name   | Object    | Scientific name of the bird species      |\n| common_name       | Object    | Common name of the bird species          |\n| author            | Object    | User who provided the recording          |\n| license           | Object    | Licensing terms associated with the recording |\n| rating            | Float64   | Rating assigned to the recording         |\n| url               | Object    | URL associated with the recording        |\n| filename          | Object    | Filename of the audio file               |\n\n**Data Description for Bird Taxonomy**\n\n| Feature Name      | Data Type | Description                               |\n|-------------------|-----------|-------------------------------------------|\n| TAXON_ORDER       | Int64     | Taxonomic order # of the bird species       |\n| CATEGORY          | Object    | Category of the bird species              |\n| SPECIES_CODE      | Object    | Unique code assigned to the bird species  |\n| PRIMARY_COM_NAME  | Object    | Primary common name of the bird species   |\n| SCI_NAME          | Object    | Scientific name of the bird species       |\n| ORDER1            | Object    | Taxonomic order of the bird species       |\n| FAMILY            | Object    | Taxonomic family of the bird species      |\n| SPECIES_GROUP     | Object    | Species group or category                 |\n| REPORT_AS         | Object    | Additional information about the bird species |\n","metadata":{}},{"cell_type":"markdown","source":"<p style=\"color: #E1B12D; font-family: 'verdana'; font-size: 22px; font-weight: bold;\"> Import Libraries & Load Data:</p>\n","metadata":{}},{"cell_type":"code","source":"#Importing the Libraries\nimport os\nimport pandas as pd\nimport numpy as np\nimport plotly.express as px\nimport plotly.graph_objects as go\nfrom plotly.subplots import make_subplots\nimport matplotlib.pyplot as plt\nimport librosa\nimport seaborn as sns\nplt.style.use('ggplot')\n\nimport librosa\nimport librosa.display\nimport matplotlib.pyplot as plt\nfrom matplotlib.colorbar import Colorbar\nimport IPython.display as ipd\n\nfrom keras.models import Sequential\nfrom keras import layers, optimizers, callbacks\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.metrics import confusion_matrix\n\nfrom IPython.display import Audio\nfrom pydub import AudioSegment, effects\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":9.446621,"end_time":"2023-10-05T08:00:28.784261","exception":false,"start_time":"2023-10-05T08:00:19.337640","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-04-15T09:31:16.953654Z","iopub.execute_input":"2024-04-15T09:31:16.954186Z","iopub.status.idle":"2024-04-15T09:31:16.966624Z","shell.execute_reply.started":"2024-04-15T09:31:16.954145Z","shell.execute_reply":"2024-04-15T09:31:16.965306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Let's load the data\ntrain_meta = pd.read_csv('/kaggle/input/birdclef-2024/train_metadata.csv')\neBird_Taxonomy = pd.read_csv('/kaggle/input/birdclef-2024/eBird_Taxonomy_v2021.csv')","metadata":{"papermill":{"duration":0.013116,"end_time":"2023-10-05T08:00:28.812853","exception":false,"start_time":"2023-10-05T08:00:28.799737","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-04-15T09:31:16.968923Z","iopub.execute_input":"2024-04-15T09:31:16.970190Z","iopub.status.idle":"2024-04-15T09:31:17.217651Z","shell.execute_reply.started":"2024-04-15T09:31:16.970143Z","shell.execute_reply":"2024-04-15T09:31:17.216204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def summary(df):\n    summry = pd.DataFrame(df.dtypes, columns=['data type'])\n    summry['#missing'] = df.isnull().sum().values\n    summry['Duplicate'] = df.duplicated().sum()\n    summry['#unique'] = df.nunique().values\n    desc = pd.DataFrame(df.describe(include='all').transpose())\n    summry['min'] = desc['min'].values\n    summry['max'] = desc['max'].values\n    summry['avg'] = desc['mean'].values\n    summry['std dev'] = desc['std'].values\n    summry['top value'] = desc['top'].values\n    summry['Freq'] = desc['freq'].values\n\n    return summry","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-04-15T09:31:17.218934Z","iopub.execute_input":"2024-04-15T09:31:17.219438Z","iopub.status.idle":"2024-04-15T09:31:17.230927Z","shell.execute_reply.started":"2024-04-15T09:31:17.219394Z","shell.execute_reply":"2024-04-15T09:31:17.229330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"color: #E1B12D; font-family: 'verdana'; font-size: 22px; font-weight: bold;\">Let's Explore the Train Metadata:</p>\n","metadata":{}},{"cell_type":"code","source":"train_meta.head().style.set_caption(\"\").set_properties(**{'border': '1.3px dotted', 'color': ''})","metadata":{"execution":{"iopub.status.busy":"2024-04-15T09:31:17.234250Z","iopub.execute_input":"2024-04-15T09:31:17.234713Z","iopub.status.idle":"2024-04-15T09:31:17.258182Z","shell.execute_reply.started":"2024-04-15T09:31:17.234678Z","shell.execute_reply":"2024-04-15T09:31:17.256717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"summary(train_meta).style.set_caption(\"**Summary of the Train Data**\").\\\nbackground_gradient(cmap='Pastel2_r', axis=0). \\\nset_properties(**{'border': '1.3px dotted', 'color': '', 'caption-side': 'left'})","metadata":{"execution":{"iopub.status.busy":"2024-04-15T09:31:17.259850Z","iopub.execute_input":"2024-04-15T09:31:17.260349Z","iopub.status.idle":"2024-04-15T09:31:17.509830Z","shell.execute_reply.started":"2024-04-15T09:31:17.260302Z","shell.execute_reply":"2024-04-15T09:31:17.508717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cat_feature_dist(data, feature):\n    # Count unique values\n    value_counts = data[feature].value_counts().sort_values(ascending=False)\n    \n    # Plot\n    fig = px.bar(y=value_counts.index[::-1], x=value_counts[::-1], orientation='h')\n    fig.update_yaxes(title='')\n    fig.update_xaxes(title_text='Count')\n    fig.update_layout(\n        showlegend=False, \n        plot_bgcolor='#1C1D20', \n        paper_bgcolor='#1C1D20',\n        font=dict(size=16, color='#E1B12D'),\n        title_font=dict(size=20, color='#222'),\n        barmode='group',  \n        title=f\"Distribution of '{feature}'\"\n    )\n    fig.show()\n    print(f\"\\nTotal unique values in '{feature}'are:\",data[feature].nunique())\n    print(\"\\nTop 5 values:\", value_counts.head())\n    print(\"\\nBottom 5 values:\", value_counts.tail())\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-04-15T09:31:17.511792Z","iopub.execute_input":"2024-04-15T09:31:17.512751Z","iopub.status.idle":"2024-04-15T09:31:17.522270Z","shell.execute_reply.started":"2024-04-15T09:31:17.512713Z","shell.execute_reply":"2024-04-15T09:31:17.520871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_feature_dist(train_meta,'primary_label')","metadata":{"execution":{"iopub.status.busy":"2024-04-15T09:31:17.524374Z","iopub.execute_input":"2024-04-15T09:31:17.524981Z","iopub.status.idle":"2024-04-15T09:31:17.629883Z","shell.execute_reply.started":"2024-04-15T09:31:17.524948Z","shell.execute_reply":"2024-04-15T09:31:17.628890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_feature_dist(train_meta,'secondary_labels')","metadata":{"execution":{"iopub.status.busy":"2024-04-15T09:31:17.631093Z","iopub.execute_input":"2024-04-15T09:31:17.631437Z","iopub.status.idle":"2024-04-15T09:31:17.729318Z","shell.execute_reply.started":"2024-04-15T09:31:17.631408Z","shell.execute_reply":"2024-04-15T09:31:17.728067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_feature_dist(train_meta,'scientific_name')","metadata":{"execution":{"iopub.status.busy":"2024-04-15T09:31:17.734077Z","iopub.execute_input":"2024-04-15T09:31:17.734766Z","iopub.status.idle":"2024-04-15T09:31:17.829750Z","shell.execute_reply.started":"2024-04-15T09:31:17.734724Z","shell.execute_reply":"2024-04-15T09:31:17.828476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_feature_dist(train_meta,'common_name')","metadata":{"execution":{"iopub.status.busy":"2024-04-15T09:31:17.831349Z","iopub.execute_input":"2024-04-15T09:31:17.831707Z","iopub.status.idle":"2024-04-15T09:31:17.927138Z","shell.execute_reply.started":"2024-04-15T09:31:17.831677Z","shell.execute_reply":"2024-04-15T09:31:17.926047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_feature_dist(train_meta,'author')","metadata":{"execution":{"iopub.status.busy":"2024-04-15T09:31:17.928513Z","iopub.execute_input":"2024-04-15T09:31:17.928840Z","iopub.status.idle":"2024-04-15T09:31:18.030476Z","shell.execute_reply.started":"2024-04-15T09:31:17.928813Z","shell.execute_reply":"2024-04-15T09:31:18.029306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_distribution(feature):\n    feature_distribution = train_meta[feature].value_counts().sort_index(ascending=False)\n    colors = ['#2E8B57', '#3CB371', '#58D68D', '#74F9A4', '#9EF8C0',\n              '#C7F8DB', '#E6F5D0', '#F9E79F', '#F7C677', '#F7A54A', '#F58231']\n    rating_colors = {rating: color for rating, color in zip(feature_distribution.index, colors)}\n    # Bar plot\n    bar_data = go.Bar(x=feature_distribution.values, y=feature_distribution.index,\n                      orientation='h', marker=dict(color=[rating_colors[rating] for rating in feature_distribution.index]), showlegend=False, name='')\n    # Donut chart\n    labels = feature_distribution.index.tolist()\n    values = feature_distribution.values.tolist()\n    total_count = sum(values)\n    percentages = [(value / total_count) * 100 for value in values]\n    donut_data = go.Pie(labels=labels, values=values, marker=dict(colors=[rating_colors[rating] for rating in feature_distribution.index], line=dict(color='#ffffff', width=0.5)), hole=0.55,\n                        showlegend=False, name='', textinfo='label+percent', hoverinfo='label+percent', textposition='inside')\n    fig = make_subplots(rows=1, cols=2, specs=[[{\"type\": \"bar\"}, {\"type\": \"pie\"}]])\n    fig.add_trace(bar_data, row=1, col=1)\n    fig.add_trace(donut_data, row=1, col=2)\n    fig.update_layout(title=f'{feature} Distribution', plot_bgcolor='#1C1D20', paper_bgcolor='#1C1D20',\n                      title_font=dict(size=20, family=\"Lato, sans-serif\"), font=dict(color='#E1B12D'))\n    fig.show()\nplot_distribution(\"rating\")","metadata":{"execution":{"iopub.status.busy":"2024-04-15T09:31:18.033700Z","iopub.execute_input":"2024-04-15T09:31:18.034141Z","iopub.status.idle":"2024-04-15T09:31:18.084341Z","shell.execute_reply.started":"2024-04-15T09:31:18.034107Z","shell.execute_reply":"2024-04-15T09:31:18.082847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.scatter_mapbox(train_meta, lat=\"latitude\",lon=\"longitude\", color=\"common_name\",zoom=3)  \nfig.update_layout(title=\"Distribution by Location\", plot_bgcolor=None, paper_bgcolor=None,\n    title_font=dict(size=20, family=\"Lato, sans-serif\", color='#E1B12D'),font=dict(color='#E1B12D'),\n    mapbox_style=\"carto-positron\",margin=dict(t=50, r=50, b=50, l=50),hovermode='closest',\n    showlegend=True, legend=dict(title='Bird Species'), legend_title_font=dict(color='#E1B12D'))\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-15T09:31:18.086187Z","iopub.execute_input":"2024-04-15T09:31:18.086641Z","iopub.status.idle":"2024-04-15T09:31:18.718575Z","shell.execute_reply.started":"2024-04-15T09:31:18.086606Z","shell.execute_reply":"2024-04-15T09:31:18.717712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets plot for western ghat\n# boundaries for the Western Ghats - as per Google search\nlower_latitude = 8\nupper_latitude = 22\nlower_longitude = 73\nupper_longitude = 79\nfiltered_data = train_meta[(train_meta['latitude'] >= lower_latitude) & \n                           (train_meta['latitude'] <= upper_latitude) &\n                           (train_meta['longitude'] >= lower_longitude) &\n                           (train_meta['longitude'] <= upper_longitude)]\n\nfig = px.scatter_mapbox(filtered_data, lat=\"latitude\", lon=\"longitude\", color=\"common_name\",\n                        zoom=5, center={\"lat\": (lower_latitude + upper_latitude) / 2, \"lon\": (lower_longitude + upper_longitude) / 2})\nfig.update_layout( title=\"Distribution of Bird Species in the Western Ghats\", plot_bgcolor=None, paper_bgcolor=None,\n    title_font=dict(size=20, family=\"Lato, sans-serif\", color='#E1B12D'),  font=dict(color='#E1B12D'),\n    mapbox_style=\"carto-positron\", margin=dict(t=50, r=50, b=50, l=50), hovermode='closest',\n    showlegend=True, legend=dict(title='Bird Species'), legend_title_font=dict(color='#E1B12D'))\nfig.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-04-15T09:31:18.719724Z","iopub.execute_input":"2024-04-15T09:31:18.720448Z","iopub.status.idle":"2024-04-15T09:31:19.300760Z","shell.execute_reply.started":"2024-04-15T09:31:18.720355Z","shell.execute_reply":"2024-04-15T09:31:19.299458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"color: #E1B12D; font-family: 'verdana'; font-size: 22px; font-weight: bold;\">Let's Explore the Bird Texonomy:</p>\n","metadata":{}},{"cell_type":"code","source":"eBird_Taxonomy.head().style.set_caption(\"\").set_properties(**{'border': '1.3px dotted', 'color': ''})","metadata":{"execution":{"iopub.status.busy":"2024-04-15T09:31:19.302348Z","iopub.execute_input":"2024-04-15T09:31:19.302716Z","iopub.status.idle":"2024-04-15T09:31:19.318920Z","shell.execute_reply.started":"2024-04-15T09:31:19.302686Z","shell.execute_reply":"2024-04-15T09:31:19.318050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"summary(eBird_Taxonomy).style.set_caption(\"**Summary of the Train Data**\").\\\nbackground_gradient(cmap='Pastel2_r', axis=0). \\\nset_properties(**{'border': '1.3px dotted', 'color': '', 'caption-side': 'left'})","metadata":{"execution":{"iopub.status.busy":"2024-04-15T09:31:19.320578Z","iopub.execute_input":"2024-04-15T09:31:19.321538Z","iopub.status.idle":"2024-04-15T09:31:19.490857Z","shell.execute_reply.started":"2024-04-15T09:31:19.321496Z","shell.execute_reply":"2024-04-15T09:31:19.489692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_feature_dist(eBird_Taxonomy,'CATEGORY')","metadata":{"execution":{"iopub.status.busy":"2024-04-15T09:31:19.492479Z","iopub.execute_input":"2024-04-15T09:31:19.492880Z","iopub.status.idle":"2024-04-15T09:31:19.588002Z","shell.execute_reply.started":"2024-04-15T09:31:19.492848Z","shell.execute_reply":"2024-04-15T09:31:19.587085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_feature_dist(eBird_Taxonomy,'ORDER1')","metadata":{"execution":{"iopub.status.busy":"2024-04-15T09:31:19.588976Z","iopub.execute_input":"2024-04-15T09:31:19.589321Z","iopub.status.idle":"2024-04-15T09:31:19.685062Z","shell.execute_reply.started":"2024-04-15T09:31:19.589293Z","shell.execute_reply":"2024-04-15T09:31:19.683874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_feature_dist(eBird_Taxonomy,'FAMILY')","metadata":{"execution":{"iopub.status.busy":"2024-04-15T09:31:19.687009Z","iopub.execute_input":"2024-04-15T09:31:19.687507Z","iopub.status.idle":"2024-04-15T09:31:19.778920Z","shell.execute_reply.started":"2024-04-15T09:31:19.687464Z","shell.execute_reply":"2024-04-15T09:31:19.777620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_feature_dist(eBird_Taxonomy,'REPORT_AS')","metadata":{"execution":{"iopub.status.busy":"2024-04-15T09:31:19.780758Z","iopub.execute_input":"2024-04-15T09:31:19.781338Z","iopub.status.idle":"2024-04-15T09:31:19.873493Z","shell.execute_reply.started":"2024-04-15T09:31:19.781306Z","shell.execute_reply":"2024-04-15T09:31:19.872376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"color: #E1B12D; font-family: 'verdana'; font-size: 22px; font-weight: bold;\">Let's Explore the Audio Files:</p>\n","metadata":{}},{"cell_type":"markdown","source":"<div style=\"border-radius: 10px; padding: 12px; font-size: 110%; text-align: left;\"> Instructions on how to interpret each of the audio visualizations ahead:</div>\n\n<p style=\"color: #E1B12D; font-family: 'verdana'; font-size: 18px; font-weight: bold;\">Waveform</p>\n\n<div class=\"image\"> \n    <img style=\"float: right; margin: 5px 20px 5px 2px;\" src=\"https://cdn.pixabay.com/animation/2023/10/24/13/50/13-50-26-112_512.gif\" width=\"400\" height=\"500\">\n</div>\n\n<div style=\"border-radius: 10px; padding: 12px; font-size: 110%; text-align: left;\">\nThe waveform represents the amplitude of the audio signal over time. It's a visual representation of the sound pressure variations.<br><br>\n\n» The x-axis represents time, and the y-axis represents the amplitude of the audio signal.<br>\n» Loud sounds will have larger peaks and valleys, while quieter sounds will have smaller peaks and valleys.<br>\n» You can identify patterns, repetitions, or anomalies in the waveform that may correspond to different sounds or events in the audio.<br>\n» Periods of silence or low amplitude can also be easily identified in the waveform.<br> </div>\n\n<p style=\"color: #E1B12D; font-family: 'verdana'; font-size: 18px; font-weight: bold;\">Spectrogram</p>\n\n<div class=\"image\"> \n    <img style=\"float: right; margin: 5px 20px 5px 2px;\" src=\"https://i0.wp.com/cramdvoicelessons.blog/wp-content/uploads/2020/09/spec.gif?resize=450%2C220&ssl=1\" width=\"400\" height=\"500\">\n</div>\n\n<div style=\"border-radius: 10px; padding: 12px; font-size: 110%; text-align: left;\">\nThe spectrogram shows the frequency content of the audio signal over time, represented as a 2D image.<br><br>\n\n» The x-axis represents time, and the y-axis represents frequency.<br>\n» The intensity or brightness of the image at a particular time and frequency represents the energy or amplitude of that frequency component.<br>\n» Horizontal patterns represent sustained sounds at specific frequencies, while vertical patterns represent transient sounds or changes in frequency over time.<br>\n» You can identify the presence of different sounds or events based on their unique frequency patterns and energy distributions.<br> </div>\n\n<p style=\"color: #E1B12D; font-family: 'verdana'; font-size: 18px; font-weight: bold;\">Mel spectrogram</p>\n\n<div class=\"image\"> \n    <img style=\"float: right; margin: 5px 20px 5px 2px;\" src=\"https://ketanhdoshi.github.io/assets/images/AudioMel/Spectro-2.png\" width=\"400\" height=\"500\">\n</div>\n\n<div style=\"border-radius: 10px; padding: 12px; font-size: 110%; text-align: left;\">\nThe Mel spectrogram is similar to the regular spectrogram, but the frequency scales are transformed to the Mel scale, which better represents how humans perceive sound frequencies.<br><br>\n    \n» The x-axis represents time, and the y-axis represents the Mel-frequency bands.<br>\n» The Mel spectrogram can be helpful in identifying speech or music patterns that are more closely aligned with human auditory perception.<br>\n» Patterns and energy distributions in the Mel spectrogram may be easier to interpret for tasks related to speech or music analysis.<br></div>\n\n<br><br>\n<br><p style=\"color: #E1B12D; font-family: 'verdana'; font-size: 18px; font-weight: bold;\">Chromagram</p>\n\n<div class=\"image\"> \n    <img style=\"float: right; margin: 5px 20px 5px 2px;\" src=\"https://i.stack.imgur.com/CaFOM.png\" width=\"400\" height=\"500\">\n</div>\n\n<div style=\"border-radius: 10px; padding: 12px; font-size: 110%; text-align: left;\">\nThe chromagram represents the distribution of energy across the 12 pitch classes (notes) in the audio signal over time.<br><br>\n\n» The x-axis represents time, and the y-axis represents the 12 pitch classes (notes).<br>\n» Brighter regions indicate higher energy or presence of a particular pitch class at that time.<br>\n» The chromagram can be useful for analyzing musical content, such as chord progressions, key changes, or the presence of specific notes or chords.<br></div>\n\n\n<br>\n<p style=\"color: #E1B12D; font-family: 'verdana'; font-size: 18px; font-weight: bold;\">Mel-Frequency Cepstral Coefficients (MFCCs)</p>\n\n<div class=\"image\"> \n    <img style=\"float: right; margin: 5px 20px 5px 2px;\" src=\"https://i.stack.imgur.com/q8YfI.png\" width=\"400\" height=\"500\">\n</div>\n\n<div style=\"border-radius: 10px; padding: 12px; font-size: 110%; text-align: left;\">\nMFCCs are a compact representation of the audio signal's spectral envelope, which captures important characteristics of the audio signal.<br><br>\n    \n» The x-axis represents time, and the y-axis represents the MFCC coefficients.<br>\n» Each row in the image represents the MFCC coefficients at a particular time frame.<br>\n» The patterns and variations in the MFCC coefficients over time can be useful for tasks like speech recognition or audio classification.<br>\n» MFCCs are widely used as input features for machine learning models in audio-related tasks.</div>\n\n\n\n<p style=\"color: #E1B12D; font-family: 'verdana'; font-size: 18px; font-weight: bold;\">Spectral Contrast</p>\n\n<div class=\"image\"> \n    <img style=\"float: right; margin: 5px 20px 5px 2px;\" src=\"https://miro.medium.com/v2/resize:fit:1024/1*GY1WNl2Aa_Zz4vQV7UfoRQ.png\" width=\"400\" height=\"500\">\n</div>\n\n<div style=\"border-radius: 10px; padding: 12px; font-size: 110%; text-align: left;\">\nThe Spectral Contrast visualization represents the differences in the spectral characteristics of the audio signal across different frequency bands.<br><br>\n\n» The x-axis represents time, and the y-axis represents the spectral contrast coefficients.<br>\n» Each row in the image represents the spectral contrast coefficients at a particular time frame.<br>\n» The spectral contrast can be useful for identifying and differentiating between different types of sounds or audio events based on their spectral characteristics.<br>\n» This representation can provide additional information beyond the traditional spectrogram or MFCCs, potentially aiding in audio classification tasks.</div>\n\n","metadata":{}},{"cell_type":"code","source":"# Check if the coordinates are within Western Ghats\ndef is_within_western_ghats(latitude, longitude):\n    lower_latitude = 8.0\n    upper_latitude = 21.5\n    lower_longitude = 73.0\n    upper_longitude = 77.5\n    return lower_latitude <= latitude <= upper_latitude and lower_longitude <= longitude <= upper_longitude\n\n#Function to plot graphs & print audio file\ndef visualize_audio_by_bird(common_name, train_meta, audio_path):\n    metadata = train_meta[train_meta['common_name'] == common_name]\n    if metadata.empty:\n        print(f\"No data found for the bird species '{common_name}'.\")\n        return\n    \n    audio_file = metadata['filename'].iloc[0]\n    audio_file_path = f\"{audio_path}/{audio_file}\"\n    latitude = metadata['latitude'].iloc[0]\n    longitude = metadata['longitude'].iloc[0]\n    rating = metadata['rating'].iloc[0]\n    author = metadata['author'].iloc[0]\n    primary_label = metadata['primary_label'].iloc[0]\n    type = metadata['type'].iloc[0]\n\n    # Check if the coordinates are within the Western Ghats\n    if not is_within_western_ghats(latitude, longitude):\n        print(f\"The bird species '{common_name}' does not have recordings in the Western Ghats.\")\n        return\n\n    try:\n        audio_data, sample_rate = librosa.load(audio_file_path)\n    except FileNotFoundError:\n        print(f\"Audio file not found for the bird species '{common_name}'.\")\n        return\n\n    # Play the audio\n    print(f\"Playing audio: {common_name}\")\n    ipd.display(ipd.Audio(audio_data, rate=sample_rate))\n\n    # Create subplots for audio visualizations\n    fig, axs = plt.subplots(nrows=3, ncols=2, figsize=(16, 12))\n\n    # Waveform\n    axs[0, 0].plot(audio_data)\n    axs[0, 0].set_title('Waveform')\n\n    # Spectrogram\n    spectrogram = librosa.amplitude_to_db(librosa.stft(audio_data), ref=np.max)\n    librosa.display.specshow(spectrogram, sr=sample_rate, x_axis='time', y_axis='hz', ax=axs[0, 1])\n    axs[0, 1].set_title('Spectrogram')\n\n    # Mel Spectrogram\n    mel_spectrogram = librosa.feature.melspectrogram(y=audio_data, sr=sample_rate)\n    librosa.display.specshow(mel_spectrogram, x_axis='time', y_axis='mel', sr=sample_rate, ax=axs[1, 0])\n    axs[1, 0].set_title('Mel Spectrogram')\n\n    # Chromagram\n    chromagram = librosa.feature.chroma_stft(y=audio_data, sr=sample_rate)\n    librosa.display.specshow(chromagram, x_axis='time', y_axis='chroma', sr=sample_rate, ax=axs[1, 1])\n    axs[1, 1].set_title('Chromagram')\n\n    # MFCCs\n    mfccs = librosa.feature.mfcc(y=audio_data, sr=sample_rate)\n    librosa.display.specshow(mfccs, x_axis='time', sr=sample_rate, ax=axs[2, 0])\n    axs[2, 0].set_title('MFCCs')\n\n    # Spectral Contrast\n    spectral_contrast = librosa.feature.spectral_contrast(y=audio_data, sr=sample_rate)\n    librosa.display.specshow(spectral_contrast, x_axis='time', sr=sample_rate, ax=axs[2, 1])\n    axs[2, 1].set_title('Spectral Contrast')\n\n    # Add color bar to the spectrogram plot\n    cbar = fig.colorbar(axs[0, 1].collections[0], ax=axs[0, 1], format='%+2.0f dB')\n\n    # Print metadata\n    print(f\"Common Name: {common_name}\")\n    print(f\"Rating: {rating}\")\n    print(f\"Primary Label: {primary_label}\")\n    print(f\"Type: {type}\")\n    print(f\"Author: {author}\")\n    print(f\"Latitude: {latitude}\")\n    print(f\"Longitude: {longitude}\")\n\n    # Plot recordings on map\n    fig_map = px.scatter_mapbox(metadata, lat=\"latitude\", lon=\"longitude\", zoom=5, title=\"Recordings Map\",\n                                hover_name=\"filename\")\n    fig_map.update_layout(mapbox_style=\"open-street-map\")\n    fig_map.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-04-15T09:31:19.875238Z","iopub.execute_input":"2024-04-15T09:31:19.875628Z","iopub.status.idle":"2024-04-15T09:31:19.898444Z","shell.execute_reply.started":"2024-04-15T09:31:19.875598Z","shell.execute_reply":"2024-04-15T09:31:19.897047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Check if any specified does not have recording at lat & long of Western Ghat\nvisualize_audio_by_bird('Little Grebe', train_meta, '/kaggle/input/birdclef-2024/train_audio')","metadata":{"execution":{"iopub.status.busy":"2024-04-15T09:31:19.900517Z","iopub.execute_input":"2024-04-15T09:31:19.901223Z","iopub.status.idle":"2024-04-15T09:31:19.918792Z","shell.execute_reply.started":"2024-04-15T09:31:19.901183Z","shell.execute_reply":"2024-04-15T09:31:19.917447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize_audio_by_bird('Black Eagle', train_meta, '/kaggle/input/birdclef-2024/train_audio')","metadata":{"execution":{"iopub.status.busy":"2024-04-15T09:31:19.920864Z","iopub.execute_input":"2024-04-15T09:31:19.921625Z","iopub.status.idle":"2024-04-15T09:31:19.934980Z","shell.execute_reply.started":"2024-04-15T09:31:19.921581Z","shell.execute_reply":"2024-04-15T09:31:19.933759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize_audio_by_bird('Nilgiri Wood-Pigeon', train_meta, '/kaggle/input/birdclef-2024/train_audio')","metadata":{"execution":{"iopub.status.busy":"2024-04-15T09:31:19.941161Z","iopub.execute_input":"2024-04-15T09:31:19.941609Z","iopub.status.idle":"2024-04-15T09:31:24.610614Z","shell.execute_reply.started":"2024-04-15T09:31:19.941569Z","shell.execute_reply":"2024-04-15T09:31:24.609367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"color: #E1B12D; font-family: 'verdana'; font-size: 18px; font-weight: bold;\">Nilgiri Wood-Pigeon</p>\n\n<div class=\"image\"> \n    <img style=\"float: left; margin: 5px 20px 5px 2px;\" src=\"https://cdn.download.ams.birds.cornell.edu/api/v1/asset/141943751/900\" width=\"400\" height=\"500\">\n</div>\n\n<div style=\"border-radius: 10px; padding: 12px; font-size: 120%; text-align: left;\">\nThe Nilgiri wood pigeon (Columba elphinstonii) is large pigeon found in the moist deciduous forests and sholas of the Western Ghats in southwestern India. They are mainly frugivorous and forage in the canopy of dense hill forests.<br>\n\nThey are best identified in the field by their large size, dark colours and the distinctive checkerboard pattern on their nape. Nilgiri wood pigeons are usually seen singly, in pairs or in small groups, feeding almost entirely in the trees but sometimes descending to the ground to forage on fallen fruits. Although feeding mainly on fruits they have been recorded taking small snails and other invertebrates.<br>\n\nThey feed on large fruits and may play an important role in dispersal of the seeds of many forest trees. Fruits of the family Lauraceae are particularly favoured and most of their food is gathered by gleaning on the outer twigs of the middle and upper canopy. They often make movements within the forest according to the fruiting seasons of their favourite trees. \n    \nTheir call is a loud langur-like low-frequency hooting \"who\" followed by a series of deep \"who-who-who\" notes.\n</div>\n\nInformation source: Wikipedia\n","metadata":{}},{"cell_type":"code","source":"visualize_audio_by_bird('Malabar Gray Hornbill', train_meta, '/kaggle/input/birdclef-2024/train_audio')","metadata":{"execution":{"iopub.status.busy":"2024-04-15T09:31:24.612087Z","iopub.execute_input":"2024-04-15T09:31:24.612470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"</div>\n<p style=\"color: #E1B12D; font-family: 'verdana'; font-size: 18px; font-weight: bold;\">Malabar Grey Hornbill</p>\n\n<div class=\"image\"> \n    <img style=\"float: left; margin: 5px 20px 5px 2px;\" src=\"https://i0.wp.com/shiftingradius.com/wp-content/uploads/2022/04/IMG_1028edit-standard.jpg?fit=1400%2C933&ssl=1\" width=\"400\" height=\"500\">\n</div>\n\n<div style=\"border-radius: 10px; padding: 12px; font-size: 120%; text-align: left;\">\nThe Malabar grey hornbill is a large bird, but at 45 to 58 cm (18 to 23 in) in length it is still the smallest of the Asian hornbills. It has a 23 cm (9.1 in) tail and pale or yellowish to orange bill. Males have a reddish bill with a yellow tip, while the females have a plain yellow bill with black at the base of the lower mandible and a black stripe along the culmen.<br><br>\n    \nThey show a broad whitish superciliary band above the eye, running down to the neck. They fly with a strong flap and glide flight and hop around heavily on the outer branches of large fruiting trees. They have brown-grey wings, a white carpal patch and black primary flight feathers tipped with white. The Indian grey hornbill, which is found mainly on the adjoining plains, is easily told apart by its prominent casque, and in flight by the white trailing edge of the entire wing.<br>\n    \nThe Malabar grey hornbill has a grey back and a cinnamon vent. The long tail is blackish with a white tip, and the underparts are grey with white streaks. The long curved bill has no casque. Immature birds have browner upperparts and a yellow bill. Young birds have a dull white or yellow iris.<br>\n\nTheir loud calls are distinctive and include \"hysterical cackling\", \"laughing\" and \"screeching\" calls.\n</div>\n\nInformation source: Wikipedia\n","metadata":{}},{"cell_type":"markdown","source":"Next-\n- We will dive deeper & Explore more\n- Summerize insights from the EDA so far\n","metadata":{}},{"cell_type":"markdown","source":"\n\n<center><img src='https://imgur.com/sC6Lyzz.gif' height=100px width=500px /></center>","metadata":{}},{"cell_type":"markdown","source":"<a id=\"toc\"></a>\n\n<a href=\"#toc\" style=\"background-color: #E1B12D; color: #ffffff; padding: 7px 10px; text-decoration: none; border-radius: 50px;\">Back to top</a><a id=\"toc\"></a>\n\n---\n<p style=\"font-size:20px; font-family:verdana; line-height: 1.7em; color:#E1B12D;\">\n    <em>Appreciate your time exploring my work. Feel free to drop comment / feedback to help enhance the notebook.<br>\n        Happy Learning!</em>\n</p>\n\n\n","metadata":{}},{"cell_type":"markdown","source":"\n<center><img src='https://imgur.com/lw0AB53.jpg' height=100px width=1000px /></center>","metadata":{}},{"cell_type":"markdown","source":"Refrences:<br>\n- Article: https://www.indiabirdwatching.com/birding-areas/western-ghats/<br>\n- Image source : Google search- credits in image <br>\n- EDA by @burhanuddinlatsaheb @BirdCLEF 2023 https://www.kaggle.com/code/ddosad/eda-visualizations-audio-exploration<br>","metadata":{}}]}