{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"markdown","source":"### Play details as a function of time during the play\n\n- line_set\n- punt\n- ball_snap\n- punt_received\n- punt_play\n- tackle\n- penalty_flag\n- play_submit "},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd\ndf = pd.read_csv('../input/NGS-2016-pre.csv')\nvr = pd.read_csv('../input/video_review.csv')\n\ndf.Time = pd.to_datetime(df.Time, format ='%Y-%m-%d %H:%M:%S.%f')\ndf_list = ['Season_Year', 'GameKey', 'PlayID', 'GSISID', 'Time']\ndf.sort_values(df_list, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e8ef60ec04e0905460349eefc1bcb58246fb68ec","_kg_hide-input":true},"cell_type":"code","source":"def calculate_speeds(df, dt=None, SI=False):\n    data_selected = df[['Time', 'x','y']]\n    if SI==True:\n        data_selected.x = data_selected.x / 1.0936132983 # 1 m/s == 1.0936132983  yd/s\n        data_selected.y = data_selected.y / 1.0936132983 # 1.0936132983 = 1 m/s equivale 1.0936132983  yd/s\n    # Might have used shift pd function ?\n    data_selected_diff = data_selected.diff()\n    if dt==None:\n        # Time is now a timedelta and need to be converted\n        data_selected_diff.Time = data_selected_diff.Time.apply(lambda x: (x.total_seconds()))\n        data_selected_diff['Speed'] = (data_selected_diff.x **2 + data_selected_diff.y **2).astype(np.float64).apply(np.sqrt) / data_selected_diff.Time\n    else:\n        # Need to be sure about the time step...\n        data_selected_diff['Speed'] = (data_selected_diff.x **2 + data_selected_diff.y **2).astype(np.float64).apply(np.sqrt) / dt\n    #data_selected_diff.rename(columns={'Time':'TimeDelta'}, inplace=True)\n    #return data_selected_diff\n    df['TimeDelta'] = data_selected_diff.Time\n    df['Speed'] = data_selected_diff.Speed\n    return df[1:]\n\ndef remove_wrong_values(df, tested_columns=['Season_Year', 'GameKey', 'PlayID', 'GSISID', 'TimeDelta'], cutspeed=None):\n    dump = df.copy()\n    colums = dump.columns\n    mask = []\n    for col in tested_columns:\n        dump['shift_'+col] = dump[col].shift(-1)\n        mask.append(\"( dump['shift_\"+col+\"'] == dump['\"+col+\"'])\")\n    mask =eval(\" & \".join(mask))\n    # Keep results where next rows is equally space\n    dump = dump[mask]\n    dump = dump[colums]\n    if cutspeed!=None:\n        dump = dump[dump.Speed < cutspeed]\n    return dump","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4e578a50ef7ca2682faa0cf177bb2689cf3524ee","_kg_hide-input":true},"cell_type":"code","source":"df_speed = calculate_speeds(df, SI=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4d40f71d9aab09d79470f1703a9b9c47716be009","_kg_hide-input":true},"cell_type":"code","source":"df_speed.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2b707efaab931a0f9ddcd679db1bd4cce4668435","_kg_hide-input":true},"cell_type":"code","source":"vr_GameKey = vr['GameKey']\nvr_PlayID = vr['PlayID']\nvr_GSISID = vr['GSISID']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b804f93bc2627bed04cb7015337a7740082ce2cc","_kg_hide-input":true},"cell_type":"code","source":"df_GameKey = df_speed.loc[df_speed['GameKey'] == 5]\ndf_GameKey_PlayID = df_GameKey.loc[df_GameKey['PlayID'] == 3129]\ndf_GameKey_PlayID_GSISID = df_GameKey_PlayID.loc[df_GameKey_PlayID['GSISID'] == 31057]\ndf_GameKey_PlayID_GSISID","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"114682b6e959c5297aa32c70ec2d3b4c0ebe330d"},"cell_type":"code","source":"df_speed_max = df_GameKey_PlayID_GSISID.loc[df_GameKey_PlayID_GSISID['Speed'] == max(df_GameKey_PlayID_GSISID['Speed'])]\ndf_speed_max \n# yds per seg","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"82f5ba48f9f90977c86f36ea2dc1fc127ac5c911"},"cell_type":"code","source":"df_speed_line_set = df_GameKey_PlayID_GSISID.loc[df_GameKey_PlayID_GSISID['Event'] == 'line_set']\ndf_speed_line_set","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"95330ff47f473077f6dd955fd208fdff46a7db4e","_kg_hide-input":true},"cell_type":"code","source":"df_speed_punt = df_GameKey_PlayID_GSISID.loc[df_GameKey_PlayID_GSISID['Event'] == 'punt']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"3c06de546dd3df9f896fa115323462ff4d126ad7"},"cell_type":"code","source":"df_speed_punt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"72b75618146ec1dd88f7745fbf5d99b2d344d75a"},"cell_type":"code","source":"df_speed_ball_snap = df_GameKey_PlayID_GSISID.loc[df_GameKey_PlayID_GSISID['Event'] == 'ball_snap']\ndf_speed_ball_snap","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c1d7614a1de4fb7d8f47fcaadd551238e5473ab7"},"cell_type":"code","source":"df_speed_punt_received = df_GameKey_PlayID_GSISID.loc[df_GameKey_PlayID_GSISID['Event'] == 'punt_received']\ndf_speed_punt_received","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6c3a5ad77c5da6ac8073f62e0e58bd778a8be1e0"},"cell_type":"code","source":"df_speed_punt_play = df_GameKey_PlayID_GSISID.loc[df_GameKey_PlayID_GSISID['Event'] == 'punt_play']\ndf_speed_punt_play","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"862882153866efbf887da8b2a064cb4d47742b59"},"cell_type":"code","source":"df_speed_penalty_flag = df_GameKey_PlayID_GSISID.loc[df_GameKey_PlayID_GSISID['Event'] == 'penalty_flag']\ndf_speed_penalty_flag","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"76f77b5fd6a37046c640c594c8bff407fc16fa10"},"cell_type":"code","source":"df_speed_play_submit = df_GameKey_PlayID_GSISID.loc[df_GameKey_PlayID_GSISID['Event'] == 'play_submit']\ndf_speed_play_submit","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}