{"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":"# Tutorial: Play Bird Audio on Map with Folium\nReally enjoying playing with audio data for the first time, thought I'd share a cool way I found to play the sounds of the birds based on their locations directly on a map!\n\nThis tutorial uses [Folium](https://python-visualization.github.io/folium/), a Python Library for plotting geospatial data. It's Python wrapper for Leaflet.js (a leading open-source JavaScript library for plotting interactive maps). It has the power of Leaflet.js but you get to use Python, so it's perfect for this use case!","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"# Setup","metadata":{}},{"cell_type":"code","source":"#imports\nimport pandas as pd\nimport numpy as np\nimport random\nimport time\n\nimport folium","metadata":{"execution":{"iopub.status.busy":"2022-02-17T23:07:41.847087Z","iopub.execute_input":"2022-02-17T23:07:41.847419Z","iopub.status.idle":"2022-02-17T23:07:42.251061Z","shell.execute_reply.started":"2022-02-17T23:07:41.847324Z","shell.execute_reply":"2022-02-17T23:07:42.250339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#get data\npath = '/kaggle/input/birdclef-2022/'\ntrain_metadata = pd.read_csv(path + 'train_metadata.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-17T23:07:42.252259Z","iopub.execute_input":"2022-02-17T23:07:42.252465Z","iopub.status.idle":"2022-02-17T23:07:42.348317Z","shell.execute_reply.started":"2022-02-17T23:07:42.25244Z","shell.execute_reply":"2022-02-17T23:07:42.347435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#because we have some duplicate latitutde/longitutde points, let's fuzzy them up a bit to avoid map overlap\ndef fuzzy_lat_long(num):\n    \"\"\"Add random 4 digit float to latitude and longitude betwen 0-0.00009. Assumes random is imported.  \n    \n    Args:    num (float): the latitude or longitude value to 4 decimal places\n    \n    Returns:    new_num (float): updated latitude or longitude value to 8 decimal places\"\"\"\n\n    random_value = round(random.uniform(0, 0.00009),8)\n    new_num = num + random_value\n    \n    return new_num","metadata":{"execution":{"iopub.status.busy":"2022-02-17T23:07:42.349621Z","iopub.execute_input":"2022-02-17T23:07:42.350031Z","iopub.status.idle":"2022-02-17T23:07:42.354942Z","shell.execute_reply.started":"2022-02-17T23:07:42.349999Z","shell.execute_reply":"2022-02-17T23:07:42.353928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#fuzzy it up\ntrain_metadata['newlat'] = train_metadata.apply(lambda x: fuzzy_lat_long(x['latitude']), axis=1)\ntrain_metadata['newlong'] = train_metadata.apply(lambda x: fuzzy_lat_long(x['longitude']), axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-02-17T23:07:42.356316Z","iopub.execute_input":"2022-02-17T23:07:42.356579Z","iopub.status.idle":"2022-02-17T23:07:42.694891Z","shell.execute_reply.started":"2022-02-17T23:07:42.35655Z","shell.execute_reply":"2022-02-17T23:07:42.694166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Let's also visualize only the scored_birds in the map to see what it looks like\nscored_birds = ['akiapo', 'aniani', 'apapan', 'barpet', 'crehon', 'elepai', 'ercfra', 'hawama', 'hawcre', 'hawgoo', 'hawhaw', 'hawpet1', 'houfin', 'iiwi', 'jabwar', 'maupar', 'omao', 'puaioh', 'skylar', 'warwhe1', 'yefcan']\ntrain = train_metadata[train_metadata['primary_label'].isin(scored_birds)]","metadata":{"execution":{"iopub.status.busy":"2022-02-17T23:07:42.696331Z","iopub.execute_input":"2022-02-17T23:07:42.696515Z","iopub.status.idle":"2022-02-17T23:07:42.708329Z","shell.execute_reply.started":"2022-02-17T23:07:42.696491Z","shell.execute_reply":"2022-02-17T23:07:42.707357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-17T23:07:42.709594Z","iopub.execute_input":"2022-02-17T23:07:42.709817Z","iopub.status.idle":"2022-02-17T23:07:42.735009Z","shell.execute_reply.started":"2022-02-17T23:07:42.709788Z","shell.execute_reply":"2022-02-17T23:07:42.734027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-17T23:21:34.044644Z","iopub.execute_input":"2022-02-17T23:21:34.044907Z","iopub.status.idle":"2022-02-17T23:21:34.050273Z","shell.execute_reply.started":"2022-02-17T23:21:34.044878Z","shell.execute_reply":"2022-02-17T23:21:34.049341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Fun with Folium\n* In just a few lines of code, create a useful/exciting tool to listen to bird audio by location!","metadata":{}},{"cell_type":"code","source":"#construct folium birdmap\nbirdmap = folium.Map(\n    location = [19.629425,-155.361479], #this is just a reference point to start the map --> for reference it's in Hawaii.\n    zoom_start = 8 #this is how zoomed in you are on that start point. \n    #zoom_start = 3 #change the zoom_start to 3 to see how it changes the reference frame of the map below!\n) ","metadata":{"execution":{"iopub.status.busy":"2022-02-17T23:21:35.030401Z","iopub.execute_input":"2022-02-17T23:21:35.031287Z","iopub.status.idle":"2022-02-17T23:21:35.03788Z","shell.execute_reply.started":"2022-02-17T23:21:35.031247Z","shell.execute_reply":"2022-02-17T23:21:35.037005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* We now have the map started up. Next is to populate it.\n* We populate the map by going row by row through out train dataframe.\n* The folium popup is html so you can do anything you can think of to do with html code. \n * I embeded the sounds from the urls provided, but you can do whatever you want in there!\n * Also because we're going in a loop I just pulled in some values like common_name, location (latitude, longitude), and primary_label.","metadata":{}},{"cell_type":"code","source":"for row in train.iterrows():\n    #base values\n    row_values = row[1] #this gets the \n    soundsrc = str(row_values['url']) + '/embed?simple=1' #this is the string part of the embed code...\n    location = [row_values['newlat'], row_values['newlong']] \n    \n    #html popup\n    popup = '</iframe>' \\\n            f'<strong>  {row_values[\"common_name\"]}  </strong> \\\n            <br><br> \\\n            <strong> Location: </strong> {location}  \\\n            <br><br> \\\n            <strong> Rating: </strong> {row_values[\"rating\"]} \\\n            <br><br> \\\n            This is a: <strong> {row_values[\"primary_label\"]} </strong>... Listen to it below. \\\n            <br><br> \\\n            <iframe src={soundsrc} scrolling=\"no\" frameborder=\"0\" width=\"340\" height=\"115\"></iframe>' \\\n            '</iframe>'\n    marker = folium.Marker(location=location, popup = popup) #defines the marker\n    marker.add_to(birdmap) #adds the marker to the map","metadata":{"execution":{"iopub.status.busy":"2022-02-17T23:24:34.424438Z","iopub.execute_input":"2022-02-17T23:24:34.425131Z","iopub.status.idle":"2022-02-17T23:24:34.727531Z","shell.execute_reply.started":"2022-02-17T23:24:34.425092Z","shell.execute_reply":"2022-02-17T23:24:34.726881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Finished Product: Birdmap !\n\n### 1. Zoom in and Out\n### 2. Click on Blue Icon to find out more about the recording and play the audio of that recording!","metadata":{}},{"cell_type":"code","source":"#click on a blue marker to see the common name, location of sound recording, primary_label, rating, and play audio!\ndisplay(birdmap)","metadata":{"execution":{"iopub.status.busy":"2022-02-17T23:24:35.198866Z","iopub.execute_input":"2022-02-17T23:24:35.199133Z","iopub.status.idle":"2022-02-17T23:24:41.960619Z","shell.execute_reply.started":"2022-02-17T23:24:35.199102Z","shell.execute_reply":"2022-02-17T23:24:41.959649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Hope you enjoyed this tutorial!\n\n### For another visualization technique, check out: [Tutorial: BirdCLEF Visualizations with Plotly](https://www.kaggle.com/alexteboul/tutorial-birdclef-visualizations-with-plotly)\n\n","metadata":{}}]}