# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import pandas as pd
import zipfile
import numpy as np

with zipfile.ZipFile("Soccer Data\matches.zip") as Z:
    with Z.open("matches_England.json") as f:
        df = pd.read_json(f)

point_data = list()
result = {1 : "draw", 0 : "lost", 3: "win"}
for i in range(len(df)):
    gameweek = df.iloc[i].gameweek
    label = df.iloc[i].label
    [[home_team, away_team], [home_score, away_score]] = [[o.strip() for o in s.split('-')] for s in label.split(',')]
    home_score = int(home_score)
    away_score = int(away_score)
    if home_score > away_score:
        home_point = 3
        away_point = 0
    if away_score > home_score:
        away_point = 3
        home_point = 0
    if away_score == home_score:
        home_point = 1
        away_point = 1
    
    point_data.append([gameweek, home_team, home_point, 'home', result[home_point]])
    point_data.append([gameweek, away_team, away_point, 'away', result[away_point]])
point_df = pd.DataFrame(point_data, columns=['gameweek', 'team', 'point', 'home_away', 'result'])
point_df
point_df.to_csv("point.csv")

import matplotlib.pyplot as plt
team_table = point_df.pivot(index= 'gameweek', columns='team', values=['point']).cumsum().fillna(method = 'backfill').fillna(method='ffill')
plt.figure(figsize=[20,12])
colormap = plt.cm.gist_ncar
color = [colormap(i) for i in np.linspace(0, 0.9, len(team_table.columns))]
[plt.plot(team_table.iloc[:,i], color = color[i]) for i in range(len(team_table.columns))]
plt.legend([team_table.columns[i][1] for i in range(len(team_table.columns))], fontsize=12)
plt.xticks(team_table.index)
plt.xlabel("Weeks", fontsize=16)
plt.ylabel("Points", fontsize=16)
plt.show()

teams = ['Arsenal', 'Chelsea', 'Liverpool', 'Manchester United', 'Manchester City']
point_df_selected = point_df[[t in teams for t in point_df['team']]]
tab = pd.crosstab(index=[point_df_selected['team'],point_df_selected['home_away']], columns=point_df_selected['result'])


tab

from scipy.stats import chi2_contingency
chi2_contingency(tab.iloc[4:6,:].values)



point_df_selected

teams_df = pd.read_json('soccer data/teams.json', orient = 'values')
teams_df

coaches_df = pd.read_json('soccer data/coaches.json', orient = 'values')
coaches_df

coaches_teams_df = pd.merge(left=teams_df, right=coaches_df,
                            left_on='wyId', right_on='currentTeamId',
                            how='inner')[['name', 'birthDate', 'shortName']].groupby('name').agg('max', on = 'birthDate').sort_values(by='birthDate', ascending = False)
now = pd.Timestamp('now')
age = (now - pd.to_datetime(coaches_teams_df['birthDate'], yearfirst=True)).astype('<m8[Y]')
coaches_teams_df['age'] = age
print(coaches_teams_df.head(10))
plt.hist(age, density = True, edgecolor='black', linewidth=1.2)
plt.xlabel('Age', fontsize=16)
plt.title('Histogram of Coaches Ages')

events_df = pd.DataFrame()
with zipfile.ZipFile("Soccer Data\events.zip") as Z:
    for name in Z.namelist():
        with Z.open(name) as f:
            df_temp = pd.read_json(f)#[['playerId', 'matchId', 'eventName', 'tags']]
            events_df = pd.concat([events_df, df_temp])
            print("file " + name + " is loaded")
            break

passes_df = events_df[['playerId', 'matchId', 'eventName', 'tags']]
passes_df.head()

passes_df = passes_df.loc[passes_df.eventName == 'Pass']
passes_df['pass_success'] = [str(t).find('1801') != -1 for t in passes_df.tags]
passes_df.drop(columns=['tags','eventName'], inplace = True)
passes_df.head()

passes_df = passes_df.groupby(['playerId', 'matchId'], as_index = False, group_keys = False).agg(['sum','count'] , on='pass_success').reset_index()
passes_df.columns = ['playerId', 'matchId', 'sum', 'count']
passes_df.head()

# +
#plt.hist(df['pass_success']['count'], bins=100)
# -

passes_df = passes_df.loc[passes_df['count'] > 100]
passes_df.head()

passes_df.drop(columns = ['matchId'], inplace = True)

passes_df = passes_df.groupby('playerId').agg('sum', level = 0, on = ['sum', 'count']).reset_index()
passes_df.head()

passes_df['ratio'] = passes_df['sum']/passes_df['count']*100
passes_df.head()

passes_top10 = passes_df.sort_values('ratio', ascending=False).head(10)
passes_top10

players_df = pd.read_json('soccer data\players.json')
players_df.head(3)

players_name = players_df[['firstName','middleName','lastName', 'wyId']].copy()
players_name['fullName'] = players_name['firstName'] + ' ' + players_name['middleName'] + ' ' + players_name['lastName']
players_name.head()

players_name.drop(columns = ['firstName', 'middleName', 'lastName'], inplace = True)
players_name.head()

passes_top10 = pd.merge(left=passes_top10, right=players_name, left_on='playerId', right_on='wyId', how='left')

passes_top10[['fullName','ratio']]

airduels_df = events_df[['playerId', 'matchId', 'eventName', 'subEventName', 'tags']]
airduels_df.head()

airduels_df = airduels_df.loc[airduels_df.subEventName == 'Air duel']
airduels_df = airduels_df.loc[airduels_df.eventName == 'Duel']
airduels_df['duel_success'] = [str(t).find('1801') != -1 for t in airduels_df.tags]
airduels_df.drop(columns=['tags','eventName', 'subEventName'], inplace = True)
airduels_df.head()

airduels_df = airduels_df.groupby(['playerId', 'matchId'], as_index = False, group_keys = False).agg(['sum','count'] , on='duel_success').reset_index()
airduels_df.columns = ['playerId', 'matchId', 'sum', 'count']
airduels_df.head()

airduels_df = airduels_df.loc[airduels_df['count'] > 5]
airduels_df.head()

players_height = players_df[['height', 'wyId']].copy()
players_height.head()

airduels_height = pd.merge(left=airduels_df, right=players_height, left_on='playerId', right_on='wyId', how='inner')[['height', 'sum','count']]

airduels_height = airduels_height.groupby(pd.cut(airduels_height["height"], np.arange(155, 210, 5))).sum(on = ['sum' , 'count'])
airduels_height.drop(columns='height', inplace = True)
airduels_height.reset_index()

airduels_height['ratio'] = airduels_height['sum']/airduels_height['count']*100

plt.figure(figsize=(15,7))
plt.scatter(range(len(airduels_height)), airduels_height['ratio'].values, c = range(len(airduels_height)), cmap = 'YlOrRd')
plt.xticks(range(len(airduels_height)), airduels_height.index)

# ## CQ1

events_df.head()

goals_df = events_df[['playerId', 'eventSec','teamId','tags','eventName', 'matchPeriod']]

goals_df.head()

tags101 = [str(t).find(' 101') != -1 for t in goals_df['tags']]

goals_df = goals_df.loc[tags101]

goals_df.head()

goals_df = goals_df.loc[goals_df['eventName'] != 'Save attempt']

goals_df.head()

goals_df['eventMin'] = goals_df['eventSec']//60 + 1

goals_df.head()

time_slots = [str(t) for t in pd.cut(goals_df['eventMin'], np.arange(0, 60, 9))]

goals_df['time_slot'] = time_slots

goals_df.head()

res = goals_df.groupby(['matchPeriod', 'time_slot']).count()[['playerId']]

res

res_plot = res.plot(kind='bar', legend=False)

res1 = goals_df.groupby(['teamId', 'time_slot', 'matchPeriod']).count()[['playerId']].reset_index()
res1.columns = ['teamId','time_slot','matchPeriod','scores']
res2 = res1.loc[res1['time_slot'] == '(36, 45]']
res3 = res2.loc[[str(t).find('2H') != -1 for t in res2['matchPeriod']]]

asd = pd.merge(left = res3, right=teams_df, left_on='teamId', right_on='wyId')[['time_slot','matchPeriod','scores','officialName']]

asd.max()

goals_df.head()

r0 = goals_df.groupby(['time_slot','playerId']).count().reset_index()[['time_slot','playerId','tags']]
r0.columns = ['time_slot','playerId','scores']

r0.head()

r1 = r0.groupby('playerId').count().reset_index()[['playerId','time_slot']]
r1.columns = ['playerId', 'nslot_covered']

r1.sort_values(by = 'nslot_covered', ascending=False)

events_df.head()

pd.unique(events_df['eventName'])

# ## RCQ2

with zipfile.ZipFile("Soccer Data\matches.zip") as Z:
    with Z.open('matches_Spain.json') as f:
            matches_df = pd.read_json(f)
with zipfile.ZipFile("Soccer Data\events.zip") as Z:
    with Z.open('events_Spain.json') as f:
            events_spain_df = pd.read_json(f)

events_spain_df.iloc[594533,:]

barcelona_mardrid_id = 2565907 #Barcelona - Real Madrid
CR7_id = 3359  #CR7
LM_id = 3322 #Messi


def event_coordinate(coordinate):
    [[_,y_start],[_,x_start],[_,y_end],[_,x_end]] = [i.split(': ') for i in str(coordinate).replace('[','').replace(']','').replace('{','').replace('}','').split(',')]
    return int(x_start)/100*130, int(y_start)/100*90, int(x_end)/100*130, int(y_end)/100*90


barcelona_madrid_df = events_spain_df[['eventName','matchId','positions','playerId']].loc[
    events_spain_df['eventName'].isin(['Pass', 'Duel','Free Kick','Shot']) & 
    events_spain_df['matchId'].isin([barcelona_mardrid_id]) &
    events_spain_df['playerId'].isin([CR7_id])]
xy_CR7 = barcelona_madrid_df['positions'].apply(event_coordinate)
xy_CR7 = xy_CR7.loc[[i[2] != 0 and i[3] != 0 for i in xy_CR7]]

barcelona_madrid_df = events_spain_df[['eventName','matchId','positions','playerId']].loc[
    events_spain_df['eventName'].isin(['Pass', 'Duel','Free Kick','Shot']) & 
    events_spain_df['matchId'].isin([barcelona_mardrid_id]) &
    events_spain_df['playerId'].isin([LM_id])]
xy_LM = barcelona_madrid_df['positions'].apply(event_coordinate)
xy_LM = xy_CR7.loc[[i[2] != 0 and i[3] != 0 for i in xy_LM]]

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Arc
import seaborn as sns
#Create figure
def plot_pitch():
    fig=plt.figure()
    fig.set_size_inches(7, 5)
    ax=fig.add_subplot(1,1,1)

    #Pitch Outline & Centre Line
    plt.plot([0,0],[0,90], color="black")
    plt.plot([0,130],[90,90], color="black")
    plt.plot([130,130],[90,0], color="black")
    plt.plot([130,0],[0,0], color="black")
    plt.plot([65,65],[0,90], color="black")

    #Left Penalty Area
    plt.plot([16.5,16.5],[65,25],color="black")
    plt.plot([0,16.5],[65,65],color="black")
    plt.plot([16.5,0],[25,25],color="black")

    #Right Penalty Area
    plt.plot([130,113.5],[65,65],color="black")
    plt.plot([113.5,113.5],[65,25],color="black")
    plt.plot([113.5,130],[25,25],color="black")

    #Left 6-yard Box
    plt.plot([0,5.5],[54,54],color="black")
    plt.plot([5.5,5.5],[54,36],color="black")
    plt.plot([5.5,0.5],[36,36],color="black")

    #Right 6-yard Box
    plt.plot([130,124.5],[54,54],color="black")
    plt.plot([124.5,124.5],[54,36],color="black")
    plt.plot([124.5,130],[36,36],color="black")

    #Prepare Circles
    centreCircle = plt.Circle((65,45),9.15,color="black",fill=False)
    centreSpot = plt.Circle((65,45),0.8,color="black")
    leftPenSpot = plt.Circle((11,45),0.8,color="black")
    rightPenSpot = plt.Circle((119,45),0.8,color="black")

    #Draw Circles
    ax.add_patch(centreCircle)
    ax.add_patch(centreSpot)
    ax.add_patch(leftPenSpot)
    ax.add_patch(rightPenSpot)

    #Prepare Arcs
    leftArc = Arc((11,45),height=18.3,width=18.3,angle=0,theta1=310,theta2=50,color="black")
    rightArc = Arc((119,45),height=18.3,width=18.3,angle=0,theta1=130,theta2=230,color="black")

    #Draw Arcs
    ax.add_patch(leftArc)
    ax.add_patch(rightArc)

    #Tidy Axes
    plt.axis('off')


plot_pitch()
x_coord = [i[0] for i in xy_CR7]
y_coord = [i[1] for i in xy_CR7]
sns.kdeplot(x_coord, y_coord, shade = "True", color = "green", n_levels = 30, shade_lowest = False)
ply.title('asdasd')
plt.show()

plot_pitch()
x_coord = [i[0] for i in xy_LM]
y_coord = [i[1] for i in xy_LM]
sns.kdeplot(x_coord, y_coord, shade = "True", color = "green", n_levels = 30, shade_lowest = False)
plt.show()

with zipfile.ZipFile("Soccer Data\matches.zip") as Z:
    with Z.open('matches_Italy.json') as f:
            matches_df = pd.read_json(f)
with zipfile.ZipFile("Soccer Data\events.zip") as Z:
    with Z.open('events_Italy.json') as f:
            events_italy_df = pd.read_json(f)

juventus_napoli_id = 2576295 #Barcelona - Real Madrid
Jorg_id = 21315  # Jorginho
Pjan_id = 20443 # <NAME>

juventus_napoli_df = events_italy_df[['eventName','matchId','positions','playerId']].loc[
    events_italy_df['eventName'].isin(['Pass', 'Duel','Free Kick','Shot']) & 
    events_italy_df['matchId'].isin([juventus_napoli_id]) &
    events_italy_df['playerId'].isin([Jorg_id])]
xy_Jorg = juventus_napoli_df['positions'].apply(event_coordinate)
xy_Jorg = xy_Jorg.loc[[i[2] != 0 and i[3] != 0 for i in xy_Jorg]]

plot_pitch()
x_coord = [i[0] for i in xy_Jorg]
y_coord = [i[1] for i in xy_Jorg]
sns.kdeplot(x_coord, y_coord, shade = "True", color = "green", n_levels = 30, shade_lowest = False)
for xy in xy_Jorg:
    plt.annotate(xy = [xy[2],xy[3]], arrowprops=dict(arrowstyle="->",connectionstyle="arc3", color = "blue"),s ='',
                 xytext = [xy[0],xy[1]])

juventus_napoli_df = events_italy_df[['eventName','matchId','positions','playerId']].loc[
    events_italy_df['eventName'].isin(['Pass', 'Duel','Free Kick','Shot']) & 
    events_italy_df['matchId'].isin([juventus_napoli_id]) &
    events_italy_df['playerId'].isin([Pjan_id])]
xy_Pjan = juventus_napoli_df['positions'].apply(event_coordinate)
xy_Pjan = xy_Jorg.loc[[i[2] != 0 and i[3] != 0 for i in xy_Pjan]]

plot_pitch()
#plt.title('asdasd')
x_coord = [i[0] for i in xy_Pjan]
y_coord = [i[1] for i in xy_Pjan]
sns.kdeplot(x_coord, y_coord, shade = "True", color = "green", n_levels = 30, shade_lowest = False)
for xy in xy_Pjan:
    plt.annotate(xy = [xy[2],xy[3]], arrowprops=dict(arrowstyle="->",connectionstyle="arc3", color = "blue"),s ='',
                 xytext = [xy[0],xy[1]])

# +
#events_italy_df

# +
#events_italy_df.loc[events_italy_df['eventId'] == 2]
# -



StarcoderdataJupyterScriptsDedupFiltered