<jupyter_start><jupyter_text>Friends of Tracking - Lesson 2> Solutions of the challenges- toc: true - badges: true- comments: true- categories: [jupyter]<jupyter_code>#hide
from google.colab import drive
drive.mount('/content/drive', force_remount=True)
#hide
%cd /content/drive/My Drive/Colab Notebooks/Futebol
#hide
!ls<jupyter_output>1LoadInData.py		    'FoT lesson2.ipynb'   lib_util.py
 2PlotShotsAndPasses.py      lib_colab.py	  __pycache__
 artigos		     lib_FCPython.py	  PyFootballPitch
 campos_varias_fontes.jfif   Lib_football.ipynb   statsbombpy
 Data			     lib_probability.py
 football-crunching	     lib_statsbomb.py<jupyter_text>Import<jupyter_code>#from lib_util import display_all, flatten_json
from lib_util import *
from lib_statsbomb import find_entry_1stlevel, find_entry_2ndlevel, find_entry_3rdlevel, find_not_empty
from lib_FCPython import createPitch

import pandas as pd
import numpy as np
from numpy import nan

import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import seaborn as sns

import math

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"<jupyter_output>/usr/local/lib/python3.6/dist-packages/statsmodels/tools/_testing.py:19: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.
  import pandas.util.testing as tm<jupyter_text>Visualizations   Competition<jupyter_code>#hide
path = 'drive/My Drive/Colab Notebooks/Futebol/Data/statsbomb/open-data/data/'
path = 'Data/statsbomb/open-data/data/'
df_comp = pd.read_json(path + 'competitions.json')

df_comp.shape
display_all(df_comp,20,8)
competition_id = 72<jupyter_output><empty_output><jupyter_text>Matches<jupyter_code>df_matches =  pd.read_json(path + f'matches/{str(competition_id)}/30.json')
df_matches.shape
df_matches.head(2)
for i in range(df_matches.shape[0]):
  home_team_name = df_matches['home_team'][i]['home_team_name']
  away_team_name = df_matches['away_team'][i]['away_team_name']
  home_score = df_matches['home_score'][i]
  away_score = df_matches['away_score'][i]
  competition_stage = df_matches['competition_stage'][i]['name']

  print(f'{i} {home_team_name} {home_score} - {away_score} {away_team_name}     ......   {competition_stage}')
df_matches.loc[48]['away_team']
match_id = 69301
home_team_id_required = 865 # England
home_team_required = "England Women's" # England

away_team_id_required = 858 # Sweden
away_team_required = "Sweden Women's" # Sweden<jupyter_output><empty_output><jupyter_text>Events<jupyter_code>df_events =  pd.read_json(path + f'events/{str(match_id)}.json')

df_shots_flatten =  pd.DataFrame([flatten_json(x) for x in df_events['shot']])
df_shots_flatten.shape
print()
df_events.shape<jupyter_output><empty_output><jupyter_text>Shots<jupyter_code>from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "last"
pitchLengthX= 120
pitchWidthY = 80

#lista = find_entry_3rdlevel(df_events['shot'],'outcome','name','Goal')
#print(lista)

lista = find_not_empty(df_events['shot'])

fig=plt.figure(figsize=(15, 10))
ax=fig.add_subplot(1,1,1)

createPitch(ax, pitchLengthX, pitchWidthY,'yards')

for i in lista:
  #location
  x = df_events['location'].loc[i][0]
  y = df_events['location'].loc[i][1]

  #COLOR = 'red'
  #if df_shots_flatten.loc[i]['outcome_name'] == 'Goal': COLOR = 'blue'

  #circleSize = 2
  circleSize = np.sqrt(df_shots_flatten.loc[i]['statsbomb_xg']*45)

  if df_events.loc[i]['team']['id'] == home_team_id_required: 
    COLOR = 'red'
    shotCircle = plt.Circle( ( x, pitchWidthY-y ), circleSize, facecolor = COLOR)
    shotCircle.set_alpha(0.2)
    if df_shots_flatten.loc[i]['outcome_name'] == 'Goal': 
      shotCircle.set_alpha(0.8)
      plt.text((x+1), (pitchWidthY-y+1), df_events.iloc[i]['player']['name'])      

  else:
    COLOR = 'blue'
    shotCircle = plt.Circle( ( pitchLengthX-x, y ), circleSize, facecolor = COLOR)
    shotCircle.set_alpha(0.2)
    if df_shots_flatten.loc[i]['outcome_name'] == 'Goal': 
      shotCircle.set_alpha(0.8)
      plt.text((pitchLengthX-x+1), (y+1), df_events.iloc[i]['player']['name'])


  ax.add_patch(shotCircle)

plt.text( 5, 75, away_team_required + ' shots') 
plt.text(80, 75, home_team_required + ' shots')<jupyter_output>% not empty: 0.58%<jupyter_text>Passes<jupyter_code>pitchLengthX= 120
pitchWidthY = 80

#lista = find_entry_3rdlevel(df_events['shot'],'outcome','name','Goal')
#print(lista)

lista = find_not_empty(df_events['pass'])

fig=plt.figure(figsize=(15, 10))
ax=fig.add_subplot(1,1,1)

createPitch(ax, pitchLengthX, pitchWidthY,'yards')

for i in lista:
  #location
  x = df_events['location'].loc[i][0]
  y = df_events['location'].loc[i][1]

  circleSize = 2
  
  if df_events.loc[i]['team']['id'] == home_team_id_required: 
    COLOR = 'red'
    shotCircle = plt.Circle( ( x, pitchWidthY-y ), circleSize, facecolor = COLOR)
    shotCircle.set_alpha(0.3)

  else:
    COLOR = 'blue'
    shotCircle = plt.Circle( ( pitchLengthX-x, y ), circleSize, facecolor = COLOR)
    shotCircle.set_alpha(0.3)

  ax.add_patch(shotCircle)

plt.text( 5, 75, away_team_required + ' passes') 
plt.text(80, 75, home_team_required + ' passes')<jupyter_output>% not empty: 27.99%<jupyter_text>Sweden's passes<jupyter_code>pitchLengthX= 120
pitchWidthY = 80

lista = find_not_empty(df_events['pass'])

fig=plt.figure(figsize=(15, 10))
ax=fig.add_subplot(1,1,1)

createPitch(ax, pitchLengthX, pitchWidthY,'yards')

for i in lista:
  #location
  x = df_events['location'].loc[i][0]
  y = df_events['location'].loc[i][1]

  circleSize = 2
  
  if df_events.loc[i]['team']['id'] == away_team_id_required: 
    COLOR = 'blue'
    shotCircle = plt.Circle( ( pitchLengthX-x, y ), circleSize, facecolor = COLOR)
    shotCircle.set_alpha(0.3)

    ax.add_patch(shotCircle)

plt.text( 5, 75, away_team_required + ' passes') 
#plt.text(80, 75, home_team_required + ' passes')<jupyter_output>% not empty: 27.99%<jupyter_text><NAME>'s passes<jupyter_code>pitchLengthX= 120
pitchWidthY = 80

lista = find_entry_2ndlevel(df_events['player'],'name','<NAME>')
#lista = find_entry_2ndlevel(df_events['team'], 'name', "Sweden Women's")
lista_pass = find_not_empty(df_events['pass'])

lista = list(set(lista) & set(lista_pass)) 


fig=plt.figure(figsize=(15, 10))
ax=fig.add_subplot(1,1,1)

createPitch(ax, pitchLengthX, pitchWidthY,'yards')

COLOR = 'blue'
circleSize = 2
for i in lista:
  #location
  x = df_events['location'].loc[i][0]
  y = df_events['location'].loc[i][1]  
  
  shotCircle = plt.Circle( ( x, pitchWidthY-y ), circleSize, facecolor = COLOR)
  shotCircle.set_alpha(0.3)

  ax.add_patch(shotCircle)

plt.text( 5, 75, "<NAME>'s" + ' passes')<jupyter_output>% <NAME>: 3.31%
% not empty: 27.99%<jupyter_text><NAME>'s passes with arrows<jupyter_code>pitchLengthX= 120
pitchWidthY = 80

lista = find_entry_2ndlevel(df_events['player'],'name','<NAME>')
#lista = find_entry_2ndlevel(df_events['team'], 'name', "Sweden Women's")
lista_pass = find_not_empty(df_events['pass'])

lista = list(set(lista) & set(lista_pass))

#lista = [1921]
#print(lista)

fig=plt.figure(figsize=(15, 10))
ax=fig.add_subplot(1,1,1)

createPitch(ax, pitchLengthX, pitchWidthY,'yards')

COLOR = 'blue'
HEAD_WIDTH = 1
HEAD_LENGTH= 2
for i in lista:
  #location
  x = df_events['location'].loc[i][0]
  y = df_events['location'].loc[i][1]  

  dx = df_events['pass'].loc[i]['end_location'][0]
  #x1 = pitchLengthX-x1
  dx = dx-x
  dy = df_events['pass'].loc[i]['end_location'][1]  
  dy = dy-y 

  ax.arrow(x, pitchWidthY-y, dx, dy, facecolor = COLOR, head_width=HEAD_WIDTH, head_length=HEAD_LENGTH)

  shotCircle = plt.Circle( ( x, pitchWidthY-y ), circleSize, facecolor = COLOR)
  shotCircle.set_alpha(0.3)
  ax.add_patch(shotCircle)


plt.text( 5, 75, "<NAME>" + ' passes')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14922332569391392, 'fraction_numerical': 0.024446142093200916, 'mean_word_length': 5.806759098786828, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '385122', 'license': 'Apache-2.0', 'n_tokens_mistral': 3228, 'n_tokens_neox': 3086, 'n_words': 510, 'path': '_notebooks/2020-04-11-FoTlesson2.ipynb', 'repo_name': 'VascoMano/futebol', 'size': '7942'}