{"cells":[{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"b055a1d2694473f5fe1399824cc7b6aca8725732"},"cell_type":"code","source":"import pandas as pd\nimport glob\nfrom plotly import offline\nimport plotly.graph_objs as go\n\n\npd.set_option('max.columns', None)\noffline.init_notebook_mode()\nconfig = dict(showLink=False)\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"dd49e94c47df9472a6e0cccb75e598c8d5f7fbcb"},"cell_type":"markdown","source":"# Brief EDA\n\nThis is a starter kernel for the NFL Analytics Competition! I'm not eligible to compete and it doesn't go into much depth but I do get to revel in the fact that I was the first! Maybe it will spur some inspiration. If you find it helpful, that's great! If you find it completely unhelpful, awesome! I challenge you to make something better and win $20,000 :)\n\nI start with the Video Review data because it seems fairly high-level. After a quick look, I find a particular game that was of interest because it has two punt plays where players recieved concussons. \n\n"},{"metadata":{"trusted":true,"_uuid":"40f7a4c3af95135821656bffb9fb829f229d7009"},"cell_type":"code","source":"# EDA of concussion plays\nvideo_review = pd.read_csv('../input/video_review.csv')\nvideo_review.head()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"97d08fddd67f39465cb5428ffc6e774184d7a4e0"},"cell_type":"code","source":"def plot_count_category(df, column):\n    x = df[column].value_counts().index\n    y = df[column].value_counts()\n    trace = go.Bar(\n        x=x,\n        y=y\n    )\n    data = [trace]\n    offline.iplot(data, config=config)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"54883ae3cb49cdd1f966da8961658e9d76566682"},"cell_type":"code","source":"# Players involved in Tackling seem to have the brunt of concussions. I'm shocked.\nplot_count_category(video_review, 'Player_Activity_Derived')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1a7673bf09f23500a254378ac7302630b6871da6"},"cell_type":"code","source":"# Helmet-to-helmet and heltmet-to-body impacts result in the most coccussions\nplot_count_category(video_review, 'Primary_Impact_Type')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4145f0980cad08024689cef645a4575dc160ffcb"},"cell_type":"code","source":"# Which games have multiple plays with concussions?\nvideo_review[video_review.duplicated(['GameKey'], keep=False)]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"bf457d9cb3c33f6337dc856926649a838e39dc32"},"cell_type":"markdown","source":"# Quick plots\n\n\nThis is just a little a starter kernel to show you a thing or two about the data and I only make couple of plots for GameKey 280. From the video review data, we know there were two punt plays during GameKey 280 that resulted in concusions. These plots show the players' movements on the field during those plays. \n\nThese GSISIDs are unique to each player and we're most concerned with these GSISIDs player/partner pairs: \n\n- PlayID 2918: `32120 / 32725` \n- PlayID 3746: `27654 / 33127`\n"},{"metadata":{"_uuid":"9ede7e6bf2e6983682d318f12ab054e0c6ea297d"},"cell_type":"markdown","source":"Data loading and Plotting functions\n\n---"},{"metadata":{"hide_input":true,"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true,"_uuid":"467bebb25381ec19657962d0b617633acb076eec"},"cell_type":"code","source":"def load_layout():\n    \"\"\"\n    Returns a dict for a Football themed Plot.ly layout \n    \"\"\"\n    layout = dict(\n        title = \"Player Activity\",\n        plot_bgcolor='darkseagreen',\n        showlegend=True,\n        xaxis=dict(\n            autorange=False,\n            range=[0, 120],\n            showgrid=False,\n            zeroline=False,\n            showline=True,\n            linecolor='black',\n            linewidth=1,\n            mirror=True,\n            ticks='',\n            tickmode='array',\n            tickvals=[10,20, 30, 40, 50, 60, 70, 80, 90, 100, 110],\n            ticktext=['Goal', 10, 20, 30, 40, 50, 40, 30, 20, 10, 'Goal'],\n            showticklabels=True\n        ),\n        yaxis=dict(\n            title='',\n            autorange=False,\n            range=[-3.3,56.3],\n            showgrid=False,\n            zeroline=False,\n            showline=True,\n            linecolor='black',\n            linewidth=1,\n            mirror=True,\n            ticks='',\n            showticklabels=False\n        ),\n        shapes=[\n            dict(\n                type='line',\n                layer='below',\n                x0=0,\n                y0=0,\n                x1=120,\n                y1=0,\n                line=dict(\n                    color='white',\n                    width=2\n                )\n            ),\n            dict(\n                type='line',\n                layer='below',\n                x0=0,\n                y0=53.3,\n                x1=120,\n                y1=53.3,\n                line=dict(\n                    color='white',\n                    width=2\n                )\n            ),\n            dict(\n                type='line',\n                layer='below',\n                x0=10,\n                y0=0,\n                x1=10,\n                y1=53.3,\n                line=dict(\n                    color='white',\n                    width=10\n                )\n            ),\n            dict(\n                type='line',\n                layer='below',\n                x0=20,\n                y0=0,\n                x1=20,\n                y1=53.3,\n                line=dict(\n                    color='white'\n                )\n            ),\n            dict(\n                type='line',\n                layer='below',\n                x0=30,\n                y0=0,\n                x1=30,\n                y1=53.3,\n                line=dict(\n                    color='white'\n                )\n            ),\n            dict(\n                type='line',\n                layer='below',\n                x0=40,\n                y0=0,\n                x1=40,\n                y1=53.3,\n                line=dict(\n                    color='white'\n                )\n            ),\n            dict(\n                type='line',\n                layer='below',\n                x0=50,\n                y0=0,\n                x1=50,\n                y1=53.3,\n                line=dict(\n                    color='white'\n                )\n            ),\n            dict(\n                type='line',\n                layer='below',\n                x0=60,\n                y0=0,\n                x1=60,\n                y1=53.3,\n                line=dict(\n                    color='white'\n                )\n            ),dict(\n                type='line',\n                layer='below',\n                x0=70,\n                y0=0,\n                x1=70,\n                y1=53.3,\n                line=dict(\n                    color='white'\n                )\n            ),dict(\n                type='line',\n                layer='below',\n                x0=80,\n                y0=0,\n                x1=80,\n                y1=53.3,\n                line=dict(\n                    color='white'\n                )\n            ),\n            dict(\n                type='line',\n                layer='below',\n                x0=90,\n                y0=0,\n                x1=90,\n                y1=53.3,\n                line=dict(\n                    color='white'\n                )\n            ),dict(\n                type='line',\n                layer='below',\n                x0=100,\n                y0=0,\n                x1=100,\n                y1=53.3,\n                line=dict(\n                    color='white'\n                )\n            ),\n            dict(\n                type='line',\n                layer='below',\n                x0=110,\n                y0=0,\n                x1=110,\n                y1=53.3,\n                line=dict(\n                    color='white',\n                    width=10\n                )\n            )\n        ]\n    )\n    return layout\n\nlayout = load_layout()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cc8dc7fbe48e5e0b6a1838780678dac1682e87cd","_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"# Loading and plotting functions\n\ndef load_plays_for_game(GameKey):\n    \"\"\"\n    Returns a dataframe of play data for a given game (GameKey)\n    \"\"\"\n    play_information = pd.read_csv('../input/play_information.csv')\n    play_information = play_information[play_information['GameKey'] == GameKey]\n    return play_information\n\n\ndef load_game_and_ngs(ngs_file=None, GameKey=None):\n    \"\"\"\n    Returns a dataframe of player movements (NGS data) for a given game\n    \"\"\"\n    if ngs_file is None:\n        print(\"Specifiy an NGS file.\")\n        return None\n    if GameKey is None:\n        print('Specify a GameKey')\n        return None\n    # Merge play data with NGS data    \n    plays = load_plays_for_game(GameKey)\n    ngs = pd.read_csv(ngs_file, low_memory=False)\n    merged = pd.merge(ngs, plays, how=\"inner\", on=[\"GameKey\", \"PlayID\", \"Season_Year\"])\n    return merged\n\n\ndef plot_play(game_df, PlayID, player1=None, player2=None, custom_layout=False):\n    \"\"\"\n    Plots player movements on the field for a given game, play, and two players\n    \"\"\"\n    game_df = game_df[game_df.PlayID==PlayID]\n    \n    GameKey=str(pd.unique(game_df.GameKey)[0])\n    HomeTeam = pd.unique(game_df.Home_Team_Visit_Team)[0].split(\"-\")[0]\n    VisitingTeam = pd.unique(game_df.Home_Team_Visit_Team)[0].split(\"-\")[1]\n    YardLine = game_df[(game_df.PlayID==PlayID) & (game_df.GSISID==player1)]['YardLine'].iloc[0]\n    \n    traces=[]   \n    if (player1 is not None) & (player2 is not None):\n        game_df = game_df[ (game_df['GSISID']==player1) | (game_df['GSISID']==player2)]\n        for player in pd.unique(game_df.GSISID):\n            player = int(player)\n            trace = go.Scatter(\n                x = game_df[game_df.GSISID==player].x,\n                y = game_df[game_df.GSISID==player].y,\n                name='GSISID '+str(player),\n                mode='markers'\n            )\n            traces.append(trace)\n    else:\n        print(\"Specify GSISIDs for player1 and player2\")\n        return None\n    \n    if custom_layout is not True:\n        layout = load_layout()\n        layout['title'] =  HomeTeam + \\\n        ' vs. ' + VisitingTeam + \\\n        '<br>Possession: ' + \\\n        YardLine.split(\" \")[0] +'@'+YardLine.split(\" \")[1]\n    data = traces\n    fig = dict(data=data, layout=layout)\n    play_description = game_df[(game_df.PlayID==PlayID) & (game_df.GSISID==player1)].iloc[0][\"PlayDescription\"]\n    print(\"\\n\\n\\t\",play_description)\n    offline.iplot(fig, config=config)\n    \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f61881d037ef67430a79ce0edf58bbf3cc925878"},"cell_type":"code","source":"# Load the movements of players in GameKey 280. \ngame280 = load_game_and_ngs('../input/NGS-2016-reg-wk13-17.csv',GameKey=280)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"55e31fae058c8a6f509b1011ae55c38c84ba05d8"},"cell_type":"code","source":"# Plot a single play, with two players\nplot_play(game_df=game280, PlayID=2918, player1=32120, player2=32725)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fd0c735905875e67960970a754bd6655a201c70a"},"cell_type":"code","source":"plot_play(game280,PlayID=3746, player1=27654, player2=33127)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0a86c7429fb0a3d497ba042a30f28cbfe6e164b8"},"cell_type":"markdown","source":"The next thing I might do is take a look at the video footage for this game and see what the play looked like...\n\nHope you enjoyed this kernel, see you on the field!"}],"metadata":{"hide_input":false,"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.6"}},"nbformat":4,"nbformat_minor":1}