{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport copy\n\n# Input data files are available in the \"../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\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.\n\n#\n# --- Read inputs ---\n#\n\ninjury = pd.read_csv(\"../input/nfl-playing-surface-analytics/InjuryRecord.csv\")\nplay = pd.read_csv(\"../input/nfl-playing-surface-analytics/PlayList.csv\")\n#- track = pd.read_csv(\"../input/nfl-playing-surface-analytics/PlayerTrackData.csv\")\n\nprint(\"A Preliminary Study on Possible Injuries Caused by Synthetic Surfaces\")\nprint()\n\n#\n# --- Number of injury cases between two surface types: Natural and Synthetic ---\n#\n\ninjury_Natural = injury.loc[injury['Surface'] == 'Natural']\ninjury_Synthetic = injury.loc[injury['Surface'] == 'Synthetic']\nn_Natural = len(injury_Natural)\nn_Synthetic = len(injury_Synthetic)\ndel injury_Natural\ndel injury_Synthetic\n\nprint(\"ANALYSIS 1. Sample distribution of injury cases between the two surface types: Natual and Synthetic\")\nprint(\"  + Number of cases of injuries on the two surface types are fairly close, which is good.\")\nprint(\"  + These counts will be used to \\\"normalize\\\" results throughout the comparison analyses.\")\nprint(\"------------------------------------------------\")\nprint(\"Number of injury cases on Natural surface = 48\")\nprint(\"Number of injury cases on Synthetic surface = 57\")\nprint(\"------------------------------------------------\")\nprint(\"\")\n\ninjury_norm = copy.deepcopy(injury)\ninjury_norm['DM_M1'] = np.where(injury_norm['Surface'] == 'Natural', injury_norm['DM_M1']/float(n_Natural), injury_norm['DM_M1']/float(n_Synthetic))\ninjury_norm['DM_M7'] = np.where(injury_norm['Surface'] == 'Natural', injury_norm['DM_M7']/float(n_Natural), injury_norm['DM_M7']/float(n_Synthetic))\ninjury_norm['DM_M28'] = np.where(injury_norm['Surface'] == 'Natural', injury_norm['DM_M28']/float(n_Natural), injury_norm['DM_M28']/float(n_Synthetic))\ninjury_norm['DM_M42'] = np.where(injury_norm['Surface'] == 'Natural', injury_norm['DM_M42']/float(n_Natural), injury_norm['DM_M42']/float(n_Synthetic))\n\n#\n# --- Overall injuries by surface type ---\n#\n\ninjury_bySurface = injury_norm.groupby(['Surface'])[['DM_M1','DM_M7','DM_M28','DM_M42']].sum()\ninjury_bySurface['DM_all'] = injury_bySurface[['DM_M1','DM_M7','DM_M28','DM_M42']].sum(axis=1)\n\nprint(\"ANALYSIS 2. Overall injuries between Natural and Synthetic surface type\")\nprint(\"  + DM_all = DM_M1 + DM_M7 + DM_M28 + DM_M42, represents all injury types together.\")\nprint(\"  + Normalization is done by dividing the raw sums by respective total injury cases.\")\nprint(\"  + Overall, it is about even for injuries to happen on Natural and Synthetic surfaces.\")\nprint(\"  + A noticeable difference is that dm_m28 type injury is more likely to happen with \")\nprint(\"  +   Synthetic surface (55.3% on Synthetic surface versus 44.7% on Natural surface).\")\nprint(\"  + All together, the Synthetic surface is slightly more like to cause injuries,\")\nprint(\"  +   50.8% vs 49.2% on Natural surface.\")\nprint(\"--------------------------------------------------------\")\ninjury_bySurface['DM_all'] = injury_bySurface[['DM_M1','DM_M7','DM_M28','DM_M42']].sum(axis=1)\nprint(injury_bySurface)\nprint(\"--------------------------------------------------------\")\nprint()\n\n#\n# --- Comparing injuries to different body parts between two surface types ---\n#\n\nprint(\"ANALYSIS 3. Comparing injuries to different body parts between two surface types\")\nprint(\"  + Ankle and Toe injuries are much more likely to happen on Synthetic surface.\")\nprint(\"  + While dm_m28 injury is overall more likely on Synthetic surface, the knee-related dm_m28 injury\")\nprint(\"    were mostly occurred on the Natural surface.\")\nprint(\"--------------------------------------------------------\")\ninjury_bodypart_bySurface = injury_norm.groupby(['BodyPart','Surface'])[['DM_M1','DM_M7','DM_M28','DM_M42']].sum()\ninjury_bodypart_bySurface['DM_all'] = injury_bodypart_bySurface[['DM_M1','DM_M7','DM_M28','DM_M42']].sum(axis=1)\nprint(injury_bodypart_bySurface)\nprint(\"--------------------------------------------------------------------\")\nprint(\"\")\n\n#\n# Comparing injuries by rosterposition between the two surfaces\n#\n\nprint(\"ANALYSIS 4. Comparing injuries by rosterposition between the two surface types\")\nprint(\"  + Most likely injuries on synthetic surface: Cornerback and Wide Receiver.\")\nprint(\"  + Least likely injuries on synthetic surface: Offeneive Lineman.\")\nprint(\"  + Most type of injuries: Cornerback: dm_m1; Wide Receiver: dm_m7, dm_m28, dm_m42.\")\nprint(\"-----------------------------------------------------------------------------\")\ninjury_play=injury_norm.merge(play,on=['PlayerKey','GameID','PlayKey'])\ninjury_roster = injury_play.groupby(['RosterPosition','Surface'])[['DM_M1','DM_M7','DM_M28','DM_M42']].sum()\ninjury_roster['DM_all'] = injury_roster[['DM_M1','DM_M7','DM_M28','DM_M42']].sum(axis=1)\nprint(injury_roster)\nprint(\"-----------------------------------------------------------------------------\")\nprint()\ndel injury_roster\n\n#\n# Comparing injuries by playtype between the two surfaces\n#\n\ninjury_play['PlayType'] = np.where(injury_play['PlayType']=='Kickoff Not Returned','Kickoff',injury_play['PlayType'])\ninjury_play['PlayType'] = np.where(injury_play['PlayType']=='Kickoff Returned','Kickoff',injury_play['PlayType'])\ninjury_play['PlayType'] = np.where(injury_play['PlayType']=='Punt Returned','Punt',injury_play['PlayType'])\ninjury_play['PlayType'] = np.where(injury_play['PlayType']=='Punt Not Returned','Punt',injury_play['PlayType'])\ninjury_playtype = injury_play.groupby(['PlayType','Surface'])[['DM_M1','DM_M7','DM_M28','DM_M42']].sum()\ninjury_playtype['DM_all'] = injury_playtype[['DM_M1','DM_M7','DM_M28','DM_M42']].sum(axis=1)\n\nprint(\"ANALYSIS 5. Comparing injuries by playtype between the two surface types\")\nprint(\"  + Both returned and not returned kickoffs are consolidated to kickoff\")\nprint(\"  + Both returned and not returned punts are consolidated to punt\")\nprint(\"  + Kickoff is significantly more likely on the synthetic for injuries of all types\")\nprint(\"-----------------------------------------------------------------------------\")\nprint(injury_playtype)\nprint(\"-----------------------------------------------------------------------------\")\nprint()\ndel injury_playtype\n\nprint(\"ANALYSIS 6. Drilldown analysis\")\nprint(\"  + So far we can conclud that ankle and toe injuries are most likely to happen on the \")\nprint(\"    synthetic surface to wide receivers during kickoffs.\")\nprint(\"  + Now we do some drilldown analyses on the position and type of plays during injuries.\")\nprint(\"  + We start by restricting results to kickoff plays as they are more likely for injuries.\")\nprint()\n\n#\n# 6a\n#\n\nprint(\"ANALYSIS 6a. Find position played during the kickoff injuries\")\nprint(\"  + Results show WR much more likely to be injured during kickoff on the synthetic surface.\")\nprint(\"    WR are probably playing as returners during kickoffs.\")\nprint(\"  + Noticed slightly higher injuries on the natural surface during kickoff at other positions, \")\nprint(\"    which will not be the scope of this analysis.\")\ninjury_play_Kickoff = injury_play.loc[injury_play['PlayType'] == 'Kickoff']\ninjury_position = injury_play_Kickoff.groupby(['Position','Surface'])[['DM_M1','DM_M7','DM_M28','DM_M42']].sum()\ninjury_position['DM_all'] = injury_position[['DM_M1','DM_M7','DM_M28','DM_M42']].sum(axis=1)\nprint(\"-----------------------------------------------------------------------------\")\nprint(injury_position)\nprint(\"-----------------------------------------------------------------------------\")\nprint()\ndel injury_position\n\n#\n# 6b\n#\n\nprint(\"ANALYSIS 6b. Find play types related to the CB injuries\")\nprint(\"  + CB is also a high risk position according to ANALYSIS 4.\")\nprint(\"  + CB injuries most likely happen during pass plays on the synthetic surface\")\nprint(\"  + Injuries also occur with punt and rush plays on the synthetic surface, but \")\nprint(\"    no data on the natural surface, which could also mean injuries are more \")\nprint(\"    likely with those plays on the synthetic surface.\")\ninjury_play_CB = injury_play.loc[injury_play['Position'] == 'CB']\ninjury_playtype = injury_play_CB.groupby(['PlayType','Surface'])[['DM_M1','DM_M7','DM_M28','DM_M42']].sum()\ninjury_playtype['DM_all'] = injury_playtype[['DM_M1','DM_M7','DM_M28','DM_M42']].sum(axis=1)\nprint(\"-----------------------------------------------------------------------------\")\nprint(injury_playtype)\nprint(\"-----------------------------------------------------------------------------\")\nprint()\ndel injury_playtype\n\n#\n# 6c\n#\n\nprint(\"ANALYSIS 6c. Combined findings of WR and CB plays\")\nprint(\"  + We have determined that most likely synthetic surface injurues occur with WR at kickoff \")\nprint(\"    CB at pass plays. We combine the results for these positions and play types.\")\nprint(\"  + The combined results show the comparison values of injuries for kickoff and pass plays \")\nprint(\"    by CB and WR.\")\ninjury_play_combine = injury_play.loc[((injury_play['Position'] == 'CB')|(injury_play['Position'] == 'WR')) & \\\n                            ((injury_play['PlayType'] == 'Kickoff')|(injury_play['PlayType'] == 'Pass'))]\ninjury_combine = injury_play_combine.groupby(['Position','PlayType','Surface'])[['DM_M1','DM_M7','DM_M28','DM_M42']].sum()\ninjury_combine['DM_all'] = injury_combine[['DM_M1','DM_M7','DM_M28','DM_M42']].sum(axis=1)\nprint(\"-----------------------------------------------------------------------------\")\nprint(injury_combine)\nprint(\"-----------------------------------------------------------------------------\")\nprint()\n\n#\n# 6d Weather a factor\n#\n\nprint(\"ANALYSIS 6d. Weather a factor?\")\nprint(\"  + We did not have conclusive finding about the weather factor for the analyses that \")\nprint(\"    we focused on, that is, wide receivers at kickoff plays and corner back at pass plays.\")\nprint(\"  + The results below is an offline analysis on the weather and no striking evidence to \")\nprint(\"    show the weather factor (values are raw counts).\")\nprint(\"-----------------------------------------------------------------------------\")\nprint(\"Position  PlayType  Surface    Weather       DM_M1 DM_M7 DM_M28 DM_M42 DM_all\")\nprint(\"CB        Pass      Natural    Rainy            1    1     0     0      2\")\nprint(\"CB        Pass      Synthetic  Indoor           4    3     1     1      9\")\nprint(\"WR        Kickoff   Natural    Partly Cloudy    1    1     0     0      2\")\nprint(\"WR        Kickoff   Synthetic  Cloudy           2    2     2     2      8\")\nprint(\"WR        Kickoff   Synthetic  Partly Cloudy    1    1     1     1      4\")\nprint(\"WR        Pass      Natural    Clear            1    1     0     0      2\")\nprint(\"WR        Pass      Natural    Cloudy           1    0     0     0      1\")\nprint(\"WR        Pass      Natural    Partly Cloudy    1    0     0     0      1\")\nprint(\"WR        Pass      Natural    Rainy            1    0     0     0      1\")\nprint(\"WR        Pass      Synthetic  Clear            2    1     0     0      3\")\nprint(\"WR        Pass      Synthetic  Indoor           1    1     0     0      2\")\nprint(\"WR        Pass      Synthetic  Partly Cloudy    1    1     1     0      3\")\nprint(\"-----------------------------------------------------------------------------\")\nprint()\nprint(\"==========\")\nprint(\"CONCLUSION\")\nprint(\"==========\")\nprint(\"\"\"\nThis is a very basic and limited comparison analysis between Natural vs Synthetic surfaces based\non the injury cases provided. There is no attempt to study the appropriateness of the sampling.\nA quick evaluation was made to determine the importance of factors such as weather and temperature,\nbut we did not reach any conclusive results for out analysis. These factors should certainly be\nexamined through case studies, which we did not do here. The results here are statistical and without\ninvestigation into individual cases. In general, we conclude\n(1) In general, injuries on the Synthetic surface is only slightly more likely in most cases.\n(2) The most striking injury cases on the Synthetic surfaces are from wide receivers at kickoff plays\n    and corner backs at pass plays.\n(3) The most synthetic surface injuries occurred to toes and ankles.\n(4) The most corner back injuries are DM_M1, and the wide receiver injuries can be all types with\n    DM_M1 the least.\n(5) The study did not find significant results from factors such as weather and temperature.\n\"\"\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}