{"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 2023 - Data Inspection\n\nHere we do a first inspection on the competition data.\n\n**All comments welcome!**\n\n## Table of Contents\n- [Config](#Config)\n- [Submission](#Submission)\n- [Training data](#Training-data)\n- [Taxonomy](#Taxonomy)","metadata":{}},{"cell_type":"code","source":"import os\nimport collections\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport librosa\nprint(\"librosa:\", librosa.__version__)","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:13.045007Z","iopub.execute_input":"2023-03-12T19:03:13.045457Z","iopub.status.idle":"2023-03-12T19:03:13.082177Z","shell.execute_reply.started":"2023-03-12T19:03:13.045419Z","shell.execute_reply":"2023-03-12T19:03:13.080833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Config","metadata":{"execution":{"iopub.status.busy":"2023-03-11T11:21:21.919495Z","iopub.execute_input":"2023-03-11T11:21:21.919899Z","iopub.status.idle":"2023-03-11T11:21:21.929101Z","shell.execute_reply.started":"2023-03-11T11:21:21.919870Z","shell.execute_reply":"2023-03-11T11:21:21.927850Z"}}},{"cell_type":"code","source":"base_dir = \"/kaggle/input/birdclef-2023/\"\ntrain_sound_dir = \"/kaggle/input/birdclef-2023/train_audio/\"\nsample_rate = 32_000","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:13.087079Z","iopub.execute_input":"2023-03-12T19:03:13.088161Z","iopub.status.idle":"2023-03-12T19:03:13.093498Z","shell.execute_reply.started":"2023-03-12T19:03:13.088107Z","shell.execute_reply":"2023-03-12T19:03:13.092546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(base_dir)","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:13.185495Z","iopub.execute_input":"2023-03-12T19:03:13.185896Z","iopub.status.idle":"2023-03-12T19:03:13.194662Z","shell.execute_reply.started":"2023-03-12T19:03:13.185859Z","shell.execute_reply":"2023-03-12T19:03:13.193670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission\n- Task: Predict probability of bird being present in 5 second time intervals (suffix is end time).\n- Multilabel task.\n- In total we have 264 birds.\n- Test soundscapes are 10 minutes long (only 3 submission rows provided here).","metadata":{}},{"cell_type":"code","source":"path_submission = base_dir + \"sample_submission.csv\"\nsubmission = pd.read_csv(path_submission)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:13.239173Z","iopub.execute_input":"2023-03-12T19:03:13.240114Z","iopub.status.idle":"2023-03-12T19:03:13.293148Z","shell.execute_reply.started":"2023-03-12T19:03:13.240074Z","shell.execute_reply":"2023-03-12T19:03:13.291977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_sound_dir = base_dir + \"test_soundscapes/\"\nos.listdir(test_sound_dir)","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:13.294944Z","iopub.execute_input":"2023-03-12T19:03:13.295375Z","iopub.status.idle":"2023-03-12T19:03:13.305417Z","shell.execute_reply.started":"2023-03-12T19:03:13.295343Z","shell.execute_reply":"2023-03-12T19:03:13.304251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_test_file = test_sound_dir + os.listdir(test_sound_dir)[0]\nprint(path_test_file)\nsig, sr = librosa.load(path_test_file, sr=sample_rate)\nsig, sr","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:13.319462Z","iopub.execute_input":"2023-03-12T19:03:13.320524Z","iopub.status.idle":"2023-03-12T19:03:26.296640Z","shell.execute_reply.started":"2023-03-12T19:03:13.320478Z","shell.execute_reply":"2023-03-12T19:03:26.295439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(sig) / sample_rate / 60","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:26.299194Z","iopub.execute_input":"2023-03-12T19:03:26.299863Z","iopub.status.idle":"2023-03-12T19:03:26.307876Z","shell.execute_reply.started":"2023-03-12T19:03:26.299826Z","shell.execute_reply":"2023-03-12T19:03:26.306631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training data\n- Total of 264 birds, same as in submission file.\n- Very unbalanced training set: 500 files max, but some birds are having only 1 file (might be very rare cases).\n- Training set does not contain any multilabel task; differecne to final prediction task.\n- Scientific name and common name are just 1-to-1 mapping of primary label.\n- Author, license, url, and filename is clear.\n- Latitude and longitude are clear, but some are missing.\n- Ratings are spread between 0.0 and 5.0; can this be used as a confidence measure?\n- 13% records have secondary labels; this might be another source of uncertainty to consider. \n    - Unclear about duplicates there.\n- Weird list columns can be casted directly using ``eval``.\n- Type give extra information (90%), but too many different labels 370 and clear how to utilize.","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(base_dir + \"train_metadata.csv\")\nfor col in [\"type\", \"secondary_labels\"]:\n    train[col] = train[col].apply(eval)\n    # remove '' elements in list (for type)\n    train[col] = train[col].apply(lambda l: [x for x in l if x])\n#train[\"path_ogg\"] = train_sound_dir + train[\"filename\"]\ntrain","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:26.310216Z","iopub.execute_input":"2023-03-12T19:03:26.311099Z","iopub.status.idle":"2023-03-12T19:03:26.867451Z","shell.execute_reply.started":"2023-03-12T19:03:26.311050Z","shell.execute_reply":"2023-03-12T19:03:26.866152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = train[\"primary_label\"].unique()\nlen(labels), labels[:10], set(submission.columns[1:]) == set(labels)","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:26.871222Z","iopub.execute_input":"2023-03-12T19:03:26.871715Z","iopub.status.idle":"2023-03-12T19:03:26.885111Z","shell.execute_reply.started":"2023-03-12T19:03:26.871664Z","shell.execute_reply":"2023-03-12T19:03:26.883543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"counts_labels = train[\"primary_label\"].value_counts().to_frame(\"counts\")\ncounts_labels.index.name = \"primary_label\"\ncounts_labels","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:26.886981Z","iopub.execute_input":"2023-03-12T19:03:26.887471Z","iopub.status.idle":"2023-03-12T19:03:26.906220Z","shell.execute_reply.started":"2023-03-12T19:03:26.887412Z","shell.execute_reply":"2023-03-12T19:03:26.904825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"counts_labels.plot(kind=\"line\", marker=\"x\", figsize=(12, 4))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:26.909392Z","iopub.execute_input":"2023-03-12T19:03:26.909718Z","iopub.status.idle":"2023-03-12T19:03:27.222795Z","shell.execute_reply.started":"2023-03-12T19:03:26.909686Z","shell.execute_reply":"2023-03-12T19:03:27.221597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(counts_labels.head(20).T.style.set_caption(\"Head\"))\ndisplay(counts_labels.tail(20).T.style.set_caption(\"Tail\"))","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:27.224474Z","iopub.execute_input":"2023-03-12T19:03:27.224996Z","iopub.status.idle":"2023-03-12T19:03:27.319340Z","shell.execute_reply.started":"2023-03-12T19:03:27.224949Z","shell.execute_reply":"2023-03-12T19:03:27.318308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"1-to-1 primary label vs scientific name:\", (train.groupby(\"primary_label\")[\"scientific_name\"].nunique() == 1).all())\nprint(\"1-to-1 primary label vs common name:\", (train.groupby(\"primary_label\")[\"common_name\"].nunique() == 1).all())\nprint(\"duplicated URL:\", train[\"url\"].duplicated().any())\nprint(\"duplicated filename:\", train[\"filename\"].duplicated().any())","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:27.320818Z","iopub.execute_input":"2023-03-12T19:03:27.321169Z","iopub.status.idle":"2023-03-12T19:03:27.344570Z","shell.execute_reply.started":"2023-03-12T19:03:27.321136Z","shell.execute_reply":"2023-03-12T19:03:27.343085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:27.346657Z","iopub.execute_input":"2023-03-12T19:03:27.347167Z","iopub.status.idle":"2023-03-12T19:03:27.367347Z","shell.execute_reply.started":"2023-03-12T19:03:27.347119Z","shell.execute_reply":"2023-03-12T19:03:27.366038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sec_labels = train[\"secondary_labels\"]\nsum(sec_labels.str.len() > 0), sum(sec_labels.str.len() > 0) / len(sec_labels)","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:27.371333Z","iopub.execute_input":"2023-03-12T19:03:27.371687Z","iopub.status.idle":"2023-03-12T19:03:27.399438Z","shell.execute_reply.started":"2023-03-12T19:03:27.371654Z","shell.execute_reply":"2023-03-12T19:03:27.398309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# duplicates?\nsec_labels[sec_labels.apply(lambda x: len(set(x)) != len(x))]","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:27.400753Z","iopub.execute_input":"2023-03-12T19:03:27.401052Z","iopub.status.idle":"2023-03-12T19:03:27.421401Z","shell.execute_reply.started":"2023-03-12T19:03:27.401023Z","shell.execute_reply":"2023-03-12T19:03:27.420144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"all known secondary labels:\", sec_labels.apply(lambda x: set(x).issubset(labels)).all())","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:27.422924Z","iopub.execute_input":"2023-03-12T19:03:27.423404Z","iopub.status.idle":"2023-03-12T19:03:27.615191Z","shell.execute_reply.started":"2023-03-12T19:03:27.423357Z","shell.execute_reply":"2023-03-12T19:03:27.614353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sec_labels.apply(lambda x: set(x)).str.len().value_counts().sort_index()","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:27.616679Z","iopub.execute_input":"2023-03-12T19:03:27.617030Z","iopub.status.idle":"2023-03-12T19:03:27.645504Z","shell.execute_reply.started":"2023-03-12T19:03:27.616998Z","shell.execute_reply":"2023-03-12T19:03:27.644370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"song_types = train[\"type\"]\nsum(song_types.str.len() > 0), sum(song_types.str.len() > 0) / len(sec_labels)","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:27.648242Z","iopub.execute_input":"2023-03-12T19:03:27.648948Z","iopub.status.idle":"2023-03-12T19:03:27.677439Z","shell.execute_reply.started":"2023-03-12T19:03:27.648896Z","shell.execute_reply":"2023-03-12T19:03:27.676260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# duplicates?\nsong_types[song_types.apply(lambda x: len(set(x)) != len(x))]","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:27.679149Z","iopub.execute_input":"2023-03-12T19:03:27.679842Z","iopub.status.idle":"2023-03-12T19:03:27.700416Z","shell.execute_reply.started":"2023-03-12T19:03:27.679790Z","shell.execute_reply":"2023-03-12T19:03:27.699530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c = collections.Counter()\nfor v in song_types:\n    c.update([x.strip().lower() for x in v])\nlen(c), c.most_common(20)","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:27.701669Z","iopub.execute_input":"2023-03-12T19:03:27.702186Z","iopub.status.idle":"2023-03-12T19:03:27.752441Z","shell.execute_reply.started":"2023-03-12T19:03:27.702153Z","shell.execute_reply":"2023-03-12T19:03:27.751250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Taxonomy\n- Taxonomy includes many entries.\n- From 264 revelant birds only one is missing, ``gnbcam2`` (if lookup acts on scientifc name).\n- Still unclear about how to use in modelling; prediction on higher aggregation?","metadata":{}},{"cell_type":"code","source":"path_taxonomy = base_dir + \"eBird_Taxonomy_v2021.csv\"\ntaxonomy = pd.read_csv(path_taxonomy)\ntaxonomy","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:27.754053Z","iopub.execute_input":"2023-03-12T19:03:27.754435Z","iopub.status.idle":"2023-03-12T19:03:27.840465Z","shell.execute_reply.started":"2023-03-12T19:03:27.754399Z","shell.execute_reply":"2023-03-12T19:03:27.839235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"taxonomy[\"label\"] = taxonomy[\"SCI_NAME\"].map(train.groupby(\"scientific_name\")[\"primary_label\"].first())\ntaxonomy = taxonomy.dropna(subset=[\"label\"])\ntaxonomy","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:27.842051Z","iopub.execute_input":"2023-03-12T19:03:27.842904Z","iopub.status.idle":"2023-03-12T19:03:27.880950Z","shell.execute_reply.started":"2023-03-12T19:03:27.842857Z","shell.execute_reply":"2023-03-12T19:03:27.879574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"missing:\", set(labels) - set(taxonomy[\"label\"]))","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:27.882447Z","iopub.execute_input":"2023-03-12T19:03:27.882828Z","iopub.status.idle":"2023-03-12T19:03:27.889510Z","shell.execute_reply.started":"2023-03-12T19:03:27.882791Z","shell.execute_reply":"2023-03-12T19:03:27.888189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"counts_family = taxonomy[\"FAMILY\"].value_counts().to_frame(\"counts\")\ncounts_family.index.name = \"family_short\"\ncounts_family.index = counts_family.index.str.split(\"(\", n=0).str[0]\nassert ~counts_family.index.has_duplicates, \"duplicates in shortend index\"\ncounts_family","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:27.891007Z","iopub.execute_input":"2023-03-12T19:03:27.891523Z","iopub.status.idle":"2023-03-12T19:03:27.909175Z","shell.execute_reply.started":"2023-03-12T19:03:27.891374Z","shell.execute_reply":"2023-03-12T19:03:27.908097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"counts_family.plot(kind='bar', rot=90, figsize=(14, 4))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-12T19:03:27.911139Z","iopub.execute_input":"2023-03-12T19:03:27.911582Z","iopub.status.idle":"2023-03-12T19:03:28.938799Z","shell.execute_reply.started":"2023-03-12T19:03:27.911536Z","shell.execute_reply":"2023-03-12T19:03:28.937890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}