{"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":"markdown","source":"# BirdCLEF 2022 Data Exploration\n\n![img](https://academy.allaboutbirds.org/wp-content/uploads/ARTICLE-SONG-1440X8004.png)\n\nThis notebook was created on a live coding stream. [Follow here for future streams or to watch the video.](https://www.twitch.tv/medallionstallion_)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"!pip install nb_black > /dev/null","metadata":{"execution":{"iopub.status.busy":"2022-02-16T03:16:40.002505Z","iopub.execute_input":"2022-02-16T03:16:40.002930Z","iopub.status.idle":"2022-02-16T03:16:49.299550Z","shell.execute_reply.started":"2022-02-16T03:16:40.002877Z","shell.execute_reply":"2022-02-16T03:16:49.298553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%load_ext lab_black","metadata":{"execution":{"iopub.status.busy":"2022-02-16T03:16:49.301625Z","iopub.execute_input":"2022-02-16T03:16:49.302133Z","iopub.status.idle":"2022-02-16T03:16:49.524635Z","shell.execute_reply.started":"2022-02-16T03:16:49.302095Z","shell.execute_reply":"2022-02-16T03:16:49.523539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nimport matplotlib.pylab as plt\nimport seaborn as sns\nimport plotly.express as px\n\n# For exploring audio files\nimport librosa\nimport librosa.display\nimport IPython.display as ipd\n\nsns.set_theme(style=\"white\", palette=None)\ncolor_pal = plt.rcParams[\"axes.prop_cycle\"].by_key()[\"color\"]\n\nfrom itertools import cycle\n\ncolor_cycle = cycle(plt.rcParams[\"axes.prop_cycle\"].by_key()[\"color\"])","metadata":{"execution":{"iopub.status.busy":"2022-02-16T03:51:34.400507Z","iopub.execute_input":"2022-02-16T03:51:34.400815Z","iopub.status.idle":"2022-02-16T03:51:34.417733Z","shell.execute_reply.started":"2022-02-16T03:51:34.400784Z","shell.execute_reply":"2022-02-16T03:51:34.416785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Files\n\nWe are provided with a number of files for this competition. \n\nCSV Files:\n- `train_metadata.csv` - A wide range of metadata is provided for the training data.\n- `test.csv` - Metadata for the test set.\n- `sample_submission.csv` - A valid sample submission.\n- `scored_birds.json` - The subset of the species in the dataset that are scored.\n- `eBird_Taxonomy_v2021.csv` - Data on the relationships between different species.\n\nFolders with Audio Files:\n\n- `train_audio/` - The bulk of the training data consists of short recordings of individual bird calls generously uploaded by users of xenocanto.org.\n- `test_soundscapes/` - We have 1 example file but in the true test there will be 5,500 recordings to be used for scoring. These are each ~ 1 minute long.\n","metadata":{}},{"cell_type":"code","source":"!ls -GFlash --color ../input/birdclef-2022/","metadata":{"execution":{"iopub.status.busy":"2022-02-16T03:04:05.297916Z","iopub.execute_input":"2022-02-16T03:04:05.298473Z","iopub.status.idle":"2022-02-16T03:04:06.060060Z","shell.execute_reply.started":"2022-02-16T03:04:05.298419Z","shell.execute_reply":"2022-02-16T03:04:06.058871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read in the CSV files.\nBASE_DIR = '../input/birdclef-2022/'\ntrain = pd.read_csv(f'{BASE_DIR}/train_metadata.csv')\ntest = pd.read_csv(f'{BASE_DIR}/test.csv')\nebird = pd.read_csv(f'{BASE_DIR}/eBird_Taxonomy_v2021.csv')\nss = pd.read_csv(f'{BASE_DIR}/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-16T03:12:54.220302Z","iopub.execute_input":"2022-02-16T03:12:54.221019Z","iopub.status.idle":"2022-02-16T03:12:54.375386Z","shell.execute_reply.started":"2022-02-16T03:12:54.220983Z","shell.execute_reply":"2022-02-16T03:12:54.374324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Explore Metadata\n\n- We see that there are varying counts of examples for each bird type.\n- Some birds have 500 labels while others have less than 10","metadata":{}},{"cell_type":"code","source":"fig, axs = plt.subplots(1, 2, figsize=(12, 5))\n# See the frequency of labels in the training dataset\ntrain[\"common_name\"].value_counts().head(20).plot(\n    kind=\"bar\", ax=axs[0], width=1, color=color_pal[0]\n)\n\naxs[0].set_title(\"Top 20 Birds with Labels\", fontsize=20)\n\n# See the frequency of labels in the training dataset\nax = (\n    train[\"common_name\"]\n    .value_counts()\n    .tail(20)\n    .plot(kind=\"bar\", ax=axs[1], width=1, color=color_pal[1])\n)\naxs[1].set_title(\"Bottom 20 Birds with Labels\", fontsize=20)","metadata":{"execution":{"iopub.status.busy":"2022-02-16T03:30:14.608002Z","iopub.execute_input":"2022-02-16T03:30:14.608784Z","iopub.status.idle":"2022-02-16T03:30:15.526876Z","shell.execute_reply.started":"2022-02-16T03:30:14.608730Z","shell.execute_reply":"2022-02-16T03:30:15.525687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We are given lat/long locations. Lets try to plot this!","metadata":{"execution":{"iopub.status.busy":"2022-02-16T03:27:18.413971Z","iopub.execute_input":"2022-02-16T03:27:18.414313Z","iopub.status.idle":"2022-02-16T03:27:18.445307Z","shell.execute_reply.started":"2022-02-16T03:27:18.414274Z","shell.execute_reply":"2022-02-16T03:27:18.444386Z"}}},{"cell_type":"code","source":"fig = px.scatter_geo(\n    train,\n    lat=\"latitude\",\n    lon=\"longitude\",\n    color=\"common_name\",\n    width=1_000,\n    height=500,\n    title=\"BirdCLEF 2022 Training Data\",\n)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-16T03:31:19.629918Z","iopub.execute_input":"2022-02-16T03:31:19.630242Z","iopub.status.idle":"2022-02-16T03:31:21.522609Z","shell.execute_reply.started":"2022-02-16T03:31:19.630207Z","shell.execute_reply":"2022-02-16T03:31:21.521657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training Data by Author\n\nThere are 1356 different authors in the training dataset. The number of observations per author varies from 1 to 947!\n- 540 of the 1356 authors only have labeled one audio file.","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 1, figsize=(10, 5))\n# See the frequency of labels in the training dataset\ntrain[\"author\"].value_counts().head(50).plot(\n    kind=\"bar\", ax=ax, width=1, color=color_pal[2]\n)\n\nax.set_title(\"Top 50 Authors\", fontsize=20)","metadata":{"execution":{"iopub.status.busy":"2022-02-16T03:38:05.999517Z","iopub.execute_input":"2022-02-16T03:38:05.999848Z","iopub.status.idle":"2022-02-16T03:38:08.112399Z","shell.execute_reply.started":"2022-02-16T03:38:05.999812Z","shell.execute_reply":"2022-02-16T03:38:08.111264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Example Training Audio File","metadata":{}},{"cell_type":"code","source":"# Listen to the audio for the first training example\nfn = train[\"filename\"].values[0]\nipd.Audio(f\"{BASE_DIR}train_audio/{fn}\")","metadata":{"execution":{"iopub.status.busy":"2022-02-16T03:44:38.113733Z","iopub.execute_input":"2022-02-16T03:44:38.114227Z","iopub.status.idle":"2022-02-16T03:44:38.129378Z","shell.execute_reply.started":"2022-02-16T03:44:38.114171Z","shell.execute_reply":"2022-02-16T03:44:38.128134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Barn Owl Example - (WARNING IT'S CREEPY SOUNDING)\nfn = train.loc[train[\"common_name\"] == \"Barn Owl\"][\"filename\"].values[0]\nipd.Audio(f\"{BASE_DIR}train_audio/{fn}\")","metadata":{"execution":{"iopub.status.busy":"2022-02-16T03:46:37.069174Z","iopub.execute_input":"2022-02-16T03:46:37.070324Z","iopub.status.idle":"2022-02-16T03:46:37.102913Z","shell.execute_reply.started":"2022-02-16T03:46:37.070257Z","shell.execute_reply":"2022-02-16T03:46:37.102119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load in the audio file as a numpy array","metadata":{}},{"cell_type":"code","source":"y, sr = librosa.load(f\"{BASE_DIR}train_audio/{fn}\")\nprint(f\"Numpy array of the audio loaded of shape {y.shape} and sample rate {sr}\")","metadata":{"execution":{"iopub.status.busy":"2022-02-16T03:48:01.777648Z","iopub.execute_input":"2022-02-16T03:48:01.778433Z","iopub.status.idle":"2022-02-16T03:48:03.148435Z","shell.execute_reply.started":"2022-02-16T03:48:01.778384Z","shell.execute_reply":"2022-02-16T03:48:03.147528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plot 10 Random Audio Files from the training dataset","metadata":{}},{"cell_type":"code","source":"# Plot The Audio File\ndef plot_raw_audio(filename, birdtype, color):\n    y, sr = librosa.load(f\"{BASE_DIR}train_audio/{filename}\")\n    ax = pd.DataFrame(y).plot(\n        figsize=(10, 3), title=f\"{birdtype} Raw Audio\", lw=0.1, color=color\n    )\n    plt.legend().remove()\n    plt.show()\n\n\nfor i, d in train.sample(10, random_state=529).iterrows():\n    plot_raw_audio(d[\"filename\"], d[\"common_name\"], next(color_cycle))","metadata":{"execution":{"iopub.status.busy":"2022-02-16T03:54:49.640448Z","iopub.execute_input":"2022-02-16T03:54:49.640815Z","iopub.status.idle":"2022-02-16T03:54:59.282529Z","shell.execute_reply.started":"2022-02-16T03:54:49.640778Z","shell.execute_reply":"2022-02-16T03:54:59.281541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create Spectograms of Birds","metadata":{}},{"cell_type":"code","source":"def plot_audio_melspec(filename, birdtype):\n    y, sr = librosa.load(f\"{BASE_DIR}train_audio/{filename}\")\n    S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128, fmax=8000)\n\n    fig, ax = plt.subplots(figsize=(10, 3))\n    S_dB = librosa.power_to_db(S, ref=np.max)\n    img = librosa.display.specshow(\n        S_dB, x_axis=\"time\", y_axis=\"mel\", sr=sr, fmax=8000, ax=ax\n    )\n    fig.colorbar(img, ax=ax, format=\"%+2.0f dB\")\n    ax.set(title=f\"Mel-frequency for bird {birdtype}\")\n    plt.show()\n\n\nfor i, d in train.sample(10, random_state=529).iterrows():\n    plot_audio_melspec(d[\"filename\"], d[\"common_name\"])","metadata":{"execution":{"iopub.status.busy":"2022-02-16T04:04:55.747066Z","iopub.execute_input":"2022-02-16T04:04:55.747598Z","iopub.status.idle":"2022-02-16T04:05:06.360671Z","shell.execute_reply.started":"2022-02-16T04:04:55.747544Z","shell.execute_reply":"2022-02-16T04:05:06.359767Z"},"trusted":true},"execution_count":null,"outputs":[]}]}