{"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":"!pip install nb-black > /dev/null\n!pip install mutagen > /dev/null","metadata":{"execution":{"iopub.status.busy":"2022-03-11T09:02:37.987001Z","iopub.execute_input":"2022-03-11T09:02:37.987277Z","iopub.status.idle":"2022-03-11T09:02:54.386021Z","shell.execute_reply.started":"2022-03-11T09:02:37.987251Z","shell.execute_reply":"2022-03-11T09:02:54.385194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport mutagen\nfrom mutagen.ogg import OggFileType\nfrom tqdm.notebook import tqdm\n\nplt.style.use(\"ggplot\")\n\n%load_ext lab_black","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-11T09:26:29.933845Z","iopub.execute_input":"2022-03-11T09:26:29.934357Z","iopub.status.idle":"2022-03-11T09:26:30.866028Z","shell.execute_reply.started":"2022-03-11T09:26:29.934311Z","shell.execute_reply":"2022-03-11T09:26:30.864959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/birdclef-2022/train_metadata.csv\")\nscored = pd.read_json(\"../input/birdclef-2022/scored_birds.json\")\n\n# is_scored\nscored[\"is_scored\"] = True\nscored.rename({0: \"label\"}, axis=1, inplace=True)\ntrain = (\n    pd.merge(train, scored, left_on=\"primary_label\", right_on=\"label\", how=\"left\")\n    .fillna(False)\n    .drop(\"label\", axis=1)\n)\n\n# num_secondary_labels\ntrain[\"num_secondary_labels\"] = train[\"secondary_labels\"].apply(lambda x: len(eval(x)))","metadata":{"execution":{"iopub.status.busy":"2022-03-11T09:03:11.966079Z","iopub.execute_input":"2022-03-11T09:03:11.966402Z","iopub.status.idle":"2022-03-11T09:03:12.142165Z","shell.execute_reply.started":"2022-03-11T09:03:11.966368Z","shell.execute_reply":"2022-03-11T09:03:12.141318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = {\n    \"length\": [],\n    \"channels\": [],\n    \"bitrate\": [],\n    \"sample_rate\": [],\n}\n\nfor item in tqdm(train.itertuples(), total=len(train)):\n    file_path = item.filename\n    f = mutagen.File(f\"../input/birdclef-2022/train_audio/{file_path}\")\n    for key in data.keys():\n        data[key].append(getattr(f.info, key))\naudio_meta = pd.DataFrame(data)\naudio_meta.to_csv(\"audio_metadata.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-11T09:20:35.561128Z","iopub.execute_input":"2022-03-11T09:20:35.561945Z","iopub.status.idle":"2022-03-11T09:22:23.562008Z","shell.execute_reply.started":"2022-03-11T09:20:35.561902Z","shell.execute_reply":"2022-03-11T09:22:23.561231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio_meta.describe()","metadata":{"execution":{"iopub.status.busy":"2022-03-11T09:29:59.388813Z","iopub.execute_input":"2022-03-11T09:29:59.389091Z","iopub.status.idle":"2022-03-11T09:29:59.413987Z","shell.execute_reply.started":"2022-03-11T09:29:59.389064Z","shell.execute_reply":"2022-03-11T09:29:59.413203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(audio_meta[[\"length\"]], log_scale=True)\nplt.title(\"Histogram of Audio Length\")","metadata":{"execution":{"iopub.status.busy":"2022-03-11T09:34:35.484823Z","iopub.execute_input":"2022-03-11T09:34:35.485329Z","iopub.status.idle":"2022-03-11T09:34:36.307797Z","shell.execute_reply.started":"2022-03-11T09:34:35.485291Z","shell.execute_reply":"2022-03-11T09:34:36.306903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio_meta[\"channels\"].value_counts().plot(kind=\"bar\")\nplt.xlabel(\"Channels\")\nplt.ylabel(\"Count\")\nplt.title(\"Histogram of Audio Channels\")","metadata":{"execution":{"iopub.status.busy":"2022-03-11T09:34:41.880623Z","iopub.execute_input":"2022-03-11T09:34:41.880949Z","iopub.status.idle":"2022-03-11T09:34:42.061172Z","shell.execute_reply.started":"2022-03-11T09:34:41.880915Z","shell.execute_reply":"2022-03-11T09:34:42.060548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio_meta[\"bitrate\"].value_counts().plot(kind=\"bar\")\nplt.xlabel(\"BitRate\")\nplt.ylabel(\"Count\")\nplt.title(\"Histogram of Audio Bitrates\")","metadata":{"execution":{"iopub.status.busy":"2022-03-11T09:34:44.67215Z","iopub.execute_input":"2022-03-11T09:34:44.672686Z","iopub.status.idle":"2022-03-11T09:34:44.856256Z","shell.execute_reply.started":"2022-03-11T09:34:44.672646Z","shell.execute_reply":"2022-03-11T09:34:44.855285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Output Extended Train Metadata","metadata":{}},{"cell_type":"code","source":"train_ext = pd.concat([train, audio_meta], axis=1)\ntrain_ext.to_csv(\"train_ext.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-11T09:29:39.561666Z","iopub.execute_input":"2022-03-11T09:29:39.562261Z","iopub.status.idle":"2022-03-11T09:29:39.720113Z","shell.execute_reply.started":"2022-03-11T09:29:39.562211Z","shell.execute_reply":"2022-03-11T09:29:39.719507Z"},"trusted":true},"execution_count":null,"outputs":[]}]}