{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-20T23:09:54.821458Z","iopub.execute_input":"2021-11-20T23:09:54.821796Z","iopub.status.idle":"2021-11-20T23:09:54.834860Z","shell.execute_reply.started":"2021-11-20T23:09:54.821764Z","shell.execute_reply":"2021-11-20T23:09:54.833191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# read in scouting data, plays data, tracking 2020 data\nscout_df = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2022/PFFScoutingData.csv')\nplays_df = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2022/plays.csv')\ntrack = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2022/tracking2020.csv')","metadata":{"execution":{"iopub.status.busy":"2021-11-20T23:09:54.836908Z","iopub.execute_input":"2021-11-20T23:09:54.837939Z","iopub.status.idle":"2021-11-20T23:10:28.854082Z","shell.execute_reply.started":"2021-11-20T23:09:54.837884Z","shell.execute_reply":"2021-11-20T23:10:28.853015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# joining plays_df and scout_df on a specific game\nbuf_mia = plays_df[plays_df.gameId == 2021010300].join(scout_df[scout_df.gameId == 2021010300].set_index('playId'), \n                                                       on='playId', how='left', rsuffix='_scout')\n# focusing on punt plays\nbm_punts = buf_mia[buf_mia.specialTeamsPlayType == 'Punt']\n# creating arrays of data I will use later while plotting\npunt_ids = np.array(bm_punts['playId'])\nresults = np.array(bm_punts['specialTeamsResult'])\nkick_types = np.array(bm_punts['kickType'])\nkick_teams = np.array(bm_punts['possessionTeam'])\ntitles = np.array(list(zip(punt_ids, results, kick_types, kick_teams)))\ntitles","metadata":{"execution":{"iopub.status.busy":"2021-11-21T00:04:56.343439Z","iopub.execute_input":"2021-11-21T00:04:56.344143Z","iopub.status.idle":"2021-11-21T00:04:56.365848Z","shell.execute_reply.started":"2021-11-21T00:04:56.344091Z","shell.execute_reply":"2021-11-21T00:04:56.365048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# narrowing the tracking data down to match the game data created above\nbm_track = track[track['gameId'] == 2021010300]\nbm_track = bm_track[bm_track['playId'].isin(punt_ids)].fillna(0)\n    \nbm_track['team'] = bm_track['team'].map({'home': 'BUF', 'away': 'MIA', 'football': 'FB'})\nbm_track['jerseyNumber'] = bm_track['jerseyNumber'].apply(lambda x: int(x))\n\nbm_track.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-21T00:02:21.325566Z","iopub.execute_input":"2021-11-21T00:02:21.326069Z","iopub.status.idle":"2021-11-21T00:02:21.418735Z","shell.execute_reply.started":"2021-11-21T00:02:21.326031Z","shell.execute_reply":"2021-11-21T00:02:21.417570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Player data for this game\nplayers = np.array(pd.DataFrame(bm_track.groupby(['team', 'position', 'displayName', 'jerseyNumber']))[0])\nplayers = sorted(players, key=lambda x: (x[0], x[1], x[2]))\n\nprint('Special Teams Punt Roster for game #2021010300 -- Miami AT Buffalo\\n')\nprint('TEAM - POS - PLAYER NAME - JERSEY #\\n')\nfor player in players:\n    print(' - '.join(str(i) for i in player))","metadata":{"execution":{"iopub.status.busy":"2021-11-21T00:27:13.325725Z","iopub.execute_input":"2021-11-21T00:27:13.326110Z","iopub.status.idle":"2021-11-21T00:27:13.417990Z","shell.execute_reply.started":"2021-11-21T00:27:13.326076Z","shell.execute_reply":"2021-11-21T00:27:13.416928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# this function plots all of the plays on separate axes, highlighting the movement of the football during the play\n# the title of each plot has extra information about the play\n\ndef plot_punts(df, titles):\n    \n    fig, ax = plt.subplots(len(titles), 1, figsize=(20, 80))\n    ax = ax.flatten()\n    \n    for i, j in zip(titles, ax):\n\n        mask = (df['playId'] == int(i[0]))\n        \n        x_val = df[mask]['x'].values\n        y_val = df[mask]['y'].values\n        hue = df[mask]['team']\n        \n        sns.scatterplot(x=x_val, y=y_val, hue=hue, ax=j, alpha=0.5, palette=['darkblue', 'teal', 'brown'])\n        \n        #j.set_facecolor('darkgreen')\n        j.set_xticks(list(range(0, 121, 10)))\n        j.set_title(f'Play ID: {i[0]} -- Kicking Team: {i[3]}, Kick Type: {i[2]}, Result of Play: {i[1]}', size=16)\n        j.set_xlabel('SIDELINE')\n        j.set_ylabel('ENDZONE')\n        j.grid(axis='x')\n        j.legend(prop={'size': 20})\n        \n        xycoords = list(zip(x_val, y_val, hue))\n\n        for xy in range(0, len(xycoords) - 5, 5):\n            if xycoords[xy][2] == 'FB':\n                j.annotate('', xy=(xycoords[xy + 5][0], xycoords[xy + 5][1]), \n                            xycoords='data', \n                            xytext=(xycoords[xy][0], xycoords[xy][1]), \n                            textcoords='data',\n                            arrowprops=dict(arrowstyle='simple', color='brown'),)\n        \n    plt.tight_layout()\n    \nplot_punts(bm_track, titles)","metadata":{"execution":{"iopub.status.busy":"2021-11-21T00:13:42.258753Z","iopub.execute_input":"2021-11-21T00:13:42.259430Z","iopub.status.idle":"2021-11-21T00:13:53.484639Z","shell.execute_reply.started":"2021-11-21T00:13:42.259387Z","shell.execute_reply":"2021-11-21T00:13:53.482619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}