{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","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"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":70203,"databundleVersionId":8068726,"sourceType":"competition"},{"sourceId":8660079,"sourceType":"datasetVersion","datasetId":5188433}],"dockerImageVersionId":30684,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## What is it?\n\nThe provided code creates a 2 plots for the [leaderboard](https://www.kaggle.com/competitions/birdclef-2024/leaderboard) visualisation of the \"[BirdCLEF 2024](https://www.kaggle.com/competitions/birdclef-2024/)\" Competition.\n\n1. **Plot 1** - Visualization\n2. **Plot 2** - Locate your team at the Leaderboard (copy & run notebook*)\n\n\n---\n\n*This notebook created by [Samvel Kocharyan](https://www.linkedin.com/in/samvelkoch/) in 2024*","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport plotly.express as px\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.graph_objects as go\nimport ipywidgets as widgets\nfrom IPython.display import display, HTML","metadata":{"execution":{"iopub.status.busy":"2024-06-11T00:18:18.204641Z","iopub.execute_input":"2024-06-11T00:18:18.205502Z","iopub.status.idle":"2024-06-11T00:18:18.211567Z","shell.execute_reply.started":"2024-06-11T00:18:18.205465Z","shell.execute_reply":"2024-06-11T00:18:18.210324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read data\nprivate = pd.read_csv('/kaggle/input/birdclef-2024-leadeboard/birdclef-2024-privateleaderboard-2024-06-11.csv')\npublic = pd.read_csv('/kaggle/input/birdclef-2024-leadeboard/birdclef-2024-publicleaderboard-2024-06-11.csv')\n\n# Merge data\nresult = public.merge(private, on='TeamName', suffixes=('_public', '_private')).query(\"TeamName not in ['sample_submission.csv']\")","metadata":{"execution":{"iopub.status.busy":"2024-06-11T00:18:53.719508Z","iopub.execute_input":"2024-06-11T00:18:53.719944Z","iopub.status.idle":"2024-06-11T00:18:53.747046Z","shell.execute_reply.started":"2024-06-11T00:18:53.719910Z","shell.execute_reply":"2024-06-11T00:18:53.745895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plot 1 - Visualization","metadata":{}},{"cell_type":"code","source":"fig = px.scatter(result,\n                 title='Shakeup plot for BirdCLEF 2024',\n                 x='Rank_public',\n                 y='Rank_private',\n                 hover_name='TeamName',\n                 hover_data=[\n                     'Rank_public',\n                     'Rank_private',\n                 ],\n                 color='Rank_private', \n                 color_continuous_scale='Portland',  \n                )\nfig.update_traces(marker=dict(size=5))\nfig.update_layout( \n                  xaxis_title=\"Public\", \n                  yaxis_title=\"Private\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-11T00:19:06.126041Z","iopub.execute_input":"2024-06-11T00:19:06.126482Z","iopub.status.idle":"2024-06-11T00:19:06.215288Z","shell.execute_reply.started":"2024-06-11T00:19:06.126449Z","shell.execute_reply":"2024-06-11T00:19:06.213773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plot 2 - Locate your Team at the Leaderboard\n1. Run the cell bellow\n2. Input your team name in the field","metadata":{}},{"cell_type":"code","source":"# Create a text input widget\nteam_name_input = widgets.Text(\n    value='Team Kefir',\n    placeholder='Enter Team Name',\n    description='Team Name:',\n    disabled=False\n)\n\n# Create a button widget\nsubmit_button = widgets.Button(\n    description='Submit',\n    disabled=False,\n    button_style='', # 'success', 'info', 'warning', 'danger' or ''\n    tooltip='Click to submit',\n    icon='check' # (FontAwesome names without the `fa-` prefix)\n)\n\n# Display the widgets side by side\ncontainer = widgets.HBox([team_name_input, submit_button])\n\n# Create output widget for displaying the graph\noutput = widgets.Output()\n\n# Display the widgets\ndisplay(container, output)\n\ndef on_button_clicked(b):\n    # This function will be called when the button is clicked\n    team_name = team_name_input.value.strip()\n    \n    # Filter data based on the entered team name\n    filtered_data = result[result['TeamName'] == team_name]\n    \n    # Create scatter plot\n    with output:\n        output.clear_output(wait=True)\n        fig = px.scatter(result,\n                         title='Shakeup plot for BirdCLEF 2024',\n                         x='Rank_public',\n                         y='Rank_private',\n                         hover_name='TeamName',\n                         hover_data=[\n                             'Rank_public',\n                             'Rank_private',\n                         ],\n                         color='Rank_private', \n                         color_continuous_scale='Portland',\n                         width=1180, \n                         height=750,\n                        )\n        \n        fig.update_traces(marker=dict(size=5))\n        fig.update_layout(xaxis_title=\"Public\", \n                          yaxis_title=\"Private\")\n        \n        hover_template = '<b>Team:</b> %{text}<br>' + \\\n                         '<b>Public Rank:</b> %{x}<br>' + \\\n                         '<b>Private Rank:</b> %{y}<extra></extra>'\n        fig.update_traces(hovertemplate=hover_template)\n        \n        if not filtered_data.empty:\n            # Highlight selected team's point\n            fig.add_trace(go.Scatter(x=filtered_data['Rank_public'], \n                                     y=filtered_data['Rank_private'], \n                                     mode='markers', marker=dict(size=15, color='red'), \n                                     name=f'Selected Team: {team_name}', \n                                     showlegend=False))  # Don't show in legend\n            \n            # Construct annotation text with team's ranks\n            annotation_text = f\"Team: {team_name}<br>\" \\\n                              f\"Private Rank: {filtered_data['Rank_private'].iloc[0]}<br>\" \\\n                              f\"Public Rank: {filtered_data['Rank_public'].iloc[0]}\"\n            \n            # Adding annotation to the selected team's point\n            fig.add_annotation(x=filtered_data['Rank_public'].iloc[0], \n                               y=filtered_data['Rank_private'].iloc[0], \n                               text=annotation_text, \n                               showarrow=True, \n                               xshift=-2, yshift=10,\n                               arrowhead=2, arrowsize=2, arrowwidth=1, arrowcolor='red',\n                               bgcolor='rgba(255,255,255,0.8)', bordercolor='red', borderwidth=1, borderpad=4)\n            \n        fig.show()\n        \n# Attach the function to the button\nsubmit_button.on_click(on_button_clicked)\n\n# Call the function initially with the default team name\non_button_clicked(None)","metadata":{"execution":{"iopub.status.busy":"2024-06-11T00:19:21.816610Z","iopub.execute_input":"2024-06-11T00:19:21.817018Z","iopub.status.idle":"2024-06-11T00:19:21.937803Z","shell.execute_reply.started":"2024-06-11T00:19:21.816990Z","shell.execute_reply":"2024-06-11T00:19:21.936572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"That's all folks! Thanks 🙏\n\n---\n\n*Created by [Samvel Kocharyan](https://www.linkedin.com/in/samvelkoch/) 2024*","metadata":{}}]}