Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions impectPy/iteration_averages.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ def getPlayerIterationAveragesFromHost(
method="GET"
).process_response(
endpoint="Players"
)[["id", "commonname", "firstname", "lastname", "birthdate", "birthplace", "leg", "idMappings"]]
)[["id", "commonname", "firstname", "lastname", "birthdate", "birthplace", "leg", "countryIds", "idMappings"]]

# only keep first country id for each player
country_series = players["countryIds"].explode().groupby(level=0).first()
players["countryIds"] = players.index.to_series().map(country_series).astype("float").astype("Int64")
players = players.rename(columns={"countryIds": "countryId"})

# unnest mappings
players = unnest_mappings_df(players, "idMappings").drop(["idMappings"], axis=1).drop_duplicates()
Expand All @@ -65,6 +70,14 @@ def getPlayerIterationAveragesFromHost(
# get iterations
iterations = getIterationsFromHost(connection=connection, host=host)

# get country data
countries = connection.make_api_request_limited(
url=f"{host}/v5/customerapi/countries",
method="GET"
).process_response(
endpoint="KPIs"
)

# create empty df to store averages
averages = pd.DataFrame()

Expand Down Expand Up @@ -162,14 +175,20 @@ def getPlayerIterationAveragesFromHost(
).merge(
players[[
"id", "wyscoutId", "heimSpielId", "skillCornerId", "commonname",
"firstname", "lastname", "birthdate", "birthplace", "leg"
"firstname", "lastname", "birthdate", "birthplace", "countryId", "leg"
]].rename(
columns={"commonname": "playerName"}
),
left_on="playerId",
right_on="id",
how="left",
suffixes=("", "_right")
).merge(
countries.rename(columns={"fifaName": "playerCountry"}),
left_on="countryId",
right_on="id",
how="left",
suffixes=("", "_right")
)

# remove NA rows
Expand Down Expand Up @@ -199,6 +218,7 @@ def getPlayerIterationAveragesFromHost(
"lastname",
"birthdate",
"birthplace",
"playerCountry",
"leg",
"position",
"matchShare",
Expand Down
36 changes: 36 additions & 0 deletions impectPy/iterations.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,44 @@ def getIterationsFromHost(connection: RateLimitedAPI, host: str) -> pd.DataFrame
df.heimSpielId = df.heimSpielId.apply(lambda x: x[0] if x else None)
df.wyscoutId = df.wyscoutId.apply(lambda x: x[0] if x else None)

# get country data
countries = connection.make_api_request_limited(
url=f"{host}/v5/customerapi/countries",
method="GET"
).process_response(
endpoint="KPIs"
)

df = df.merge(
countries[["id", "fifaName"]].rename(
columns={"id": "competitionCountryId", "fifaName": "competitionCountryName"}
),
how="left",
on="competitionCountryId"
)

# sort iterations
df = df.sort_values(by="id")

# define column order
order = [
"id",
"competitionId",
"competitionName",
"season",
"competitionType",
"competitionCountryId",
"competitionCountryName",
"competitionGender",
"dataVersion",
"lastChangeTimestamp",
"wyscoutId",
"heimSpielId",
"skillCornerId",
]

# select columns
df = df[order]

# return dataframe
return df
26 changes: 23 additions & 3 deletions impectPy/matchsums.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ def getPlayerMatchsumsFromHost(matches: list, connection: RateLimitedAPI, host:
),
iterations),
ignore_index=True
)[["id", "commonname", "firstname", "lastname", "birthdate", "birthplace", "leg", "idMappings"]]
)[["id", "commonname", "firstname", "lastname", "birthdate", "birthplace", "leg", "countryIds", "idMappings"]]

# only keep first country id for each player
country_series = players["countryIds"].explode().groupby(level=0).first()
players["countryIds"] = players.index.to_series().map(country_series).astype("float").astype("Int64")
players = players.rename(columns={"countryIds": "countryId"})

# unnest mappings
players = unnest_mappings_df(players, "idMappings").drop(["idMappings"], axis=1).drop_duplicates()
Expand Down Expand Up @@ -117,6 +122,14 @@ def getPlayerMatchsumsFromHost(matches: list, connection: RateLimitedAPI, host:
# get iterations
iterations = getIterationsFromHost(connection=connection, host=host)

# get country data
countries = connection.make_api_request_limited(
url=f"{host}/v5/customerapi/countries",
method="GET"
).process_response(
endpoint="KPIs"
)

# create empty df to store matchsums
matchsums = pd.DataFrame()

Expand Down Expand Up @@ -181,7 +194,7 @@ def getPlayerMatchsumsFromHost(matches: list, connection: RateLimitedAPI, host:

# append to matchsums
matchsums = pd.concat([matchsums, temp])

# merge with other data
matchsums = matchsums.merge(
matchplan[["id", "scheduledDate", "matchDayIndex", "matchDayName", "iterationId"]],
Expand All @@ -206,14 +219,20 @@ def getPlayerMatchsumsFromHost(matches: list, connection: RateLimitedAPI, host:
).merge(
players[[
"id", "wyscoutId", "heimSpielId", "skillCornerId", "commonname",
"firstname", "lastname", "birthdate", "birthplace", "leg"
"firstname", "lastname", "birthdate", "birthplace", "countryId", "leg"
]].rename(
columns={"commonname": "playerName"}
),
left_on="id",
right_on="id",
how="left",
suffixes=("", "_right")
).merge(
countries.rename(columns={"fifaName": "playerCountry"}),
left_on="countryId",
right_on="id",
how="left",
suffixes=("", "_right")
)

# rename some columns
Expand Down Expand Up @@ -244,6 +263,7 @@ def getPlayerMatchsumsFromHost(matches: list, connection: RateLimitedAPI, host:
"lastname",
"birthdate",
"birthplace",
"playerCountry",
"leg",
"position",
"matchShare",
Expand Down
72 changes: 46 additions & 26 deletions impectPy/player_profile_scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,37 +41,37 @@ def getPlayerProfileScores(
def getPlayerProfileScoresFromHost(
iteration: int, positions: list, connection: RateLimitedAPI, host: str
) -> pd.DataFrame:

# check input for iteration argument
if not isinstance(iteration, int):
raise Exception("Input for iteration argument must be an integer")

# check input for positions argument
if not isinstance(positions, list):
raise Exception("Input for positions argument must be a list")

# check if the input positions are valid
invalid_positions = [position for position in positions if position not in allowed_positions]
if len(invalid_positions) > 0:
raise Exception(
f"Invalid position(s): {', '.join(invalid_positions)}."
f"\nChoose one or more of: {', '.join(allowed_positions)}"
)

# get squads
squads = connection.make_api_request_limited(
url=f"{host}/v5/customerapi/iterations/{iteration}/squads",
method="GET"
).process_response(
endpoint="Squads"
)

# get squadIds
squad_ids = squads[squads.access].id.to_list()

# compile position string
position_string = ",".join(positions)

# get player profile scores per squad
profile_scores_raw = pd.concat(
map(lambda squadId: connection.make_api_request_limited(
Expand All @@ -97,38 +97,51 @@ def getPlayerProfileScoresFromHost(
error_list = [str(squadId) for squadId in squad_ids if squadId not in profile_scores_raw.squadId.to_list()]
if len(error_list) > 0:
print(f"No players played at positions {positions} for iteration {iteration} for following squads:\n\t{', '.join(error_list)}")

# get players
players = connection.make_api_request_limited(
url=f"{host}/v5/customerapi/iterations/{iteration}/players",
method="GET"
).process_response(
endpoint="Players"
)[["id", "commonname", "firstname", "lastname", "birthdate", "birthplace", "leg", "idMappings"]]
)[["id", "commonname", "firstname", "lastname", "birthdate", "birthplace", "leg", "countryIds", "idMappings"]]

# only keep first country id for each player
country_series = players["countryIds"].explode().groupby(level=0).first()
players["countryIds"] = players.index.to_series().map(country_series).astype("float").astype("Int64")
players = players.rename(columns={"countryIds": "countryId"})

# unnest mappings
players = unnest_mappings_df(players, "idMappings").drop(["idMappings"], axis=1).drop_duplicates()

# get scores
scores = connection.make_api_request_limited(
url=f"{host}/v5/customerapi/player-profiles",
method="GET"
).process_response(
endpoint="playerProfiles"
)[["name"]]

# get iterations
iterations = getIterationsFromHost(connection=connection, host=host)


# get country data
countries = connection.make_api_request_limited(
url=f"{host}/v5/customerapi/countries",
method="GET"
).process_response(
endpoint="KPIs"
)

# unnest scorings
profile_scores = profile_scores_raw.explode("profileScores").reset_index(drop=True)

# unnest dictionary in kpis column
profile_scores = pd.concat(
[profile_scores.drop(["profileScores"], axis=1), pd.json_normalize(profile_scores["profileScores"])],
axis=1
)

# merge with player scores to ensure all kpis are present
profile_scores = profile_scores.merge(
scores,
Expand All @@ -137,15 +150,15 @@ def getPlayerProfileScoresFromHost(
how="outer",
suffixes=("", "_right")
)

# get matchShares
match_shares = profile_scores[
["iterationId", "squadId", "playerId", "positions", "playDuration", "matchShare"]].drop_duplicates()

# fill missing values in the "name" column with a default value to ensure players without scorings don't get lost
if len(profile_scores["name"][profile_scores["name"].isnull()]) > 0:
profile_scores["name"] = profile_scores["name"].fillna("-1")

# pivot kpi values
profile_scores = pd.pivot_table(
profile_scores,
Expand All @@ -156,11 +169,11 @@ def getPlayerProfileScoresFromHost(
fill_value=0,
dropna=False
).reset_index()

# drop "-1" column
if "-1" in profile_scores.columns:
profile_scores.drop(["-1"], inplace=True, axis=1)

# merge with playDuration and matchShare
profile_scores = profile_scores.merge(
match_shares,
Expand All @@ -187,27 +200,33 @@ def getPlayerProfileScoresFromHost(
).merge(
players[[
"id", "wyscoutId", "heimSpielId", "skillCornerId", "commonname",
"firstname", "lastname", "birthdate", "birthplace", "leg"
"firstname", "lastname", "birthdate", "birthplace", "countryId", "leg"
]].rename(
columns={"commonname": "playerName"}
),
left_on="playerId",
right_on="id",
how="left",
suffixes=("", "_right")
).merge(
countries.rename(columns={"fifaName": "playerCountry"}),
left_on="countryId",
right_on="id",
how="left",
suffixes=("", "_right")
)

# remove NA rows
profile_scores = profile_scores[profile_scores.iterationId.notnull()]

# fix column types
profile_scores["squadId"] = profile_scores["squadId"].astype("Int64")
profile_scores["playerId"] = profile_scores["playerId"].astype("Int64")
profile_scores["iterationId"] = profile_scores["iterationId"].astype("Int64")
profile_scores["wyscoutId"] = profile_scores["wyscoutId"].astype("Int64")
profile_scores["heimSpielId"] = profile_scores["heimSpielId"].astype("Int64")
profile_scores["skillCornerId"] = profile_scores["skillCornerId"].astype("Int64")

# define column order
order = [
"iterationId",
Expand All @@ -224,17 +243,18 @@ def getPlayerProfileScoresFromHost(
"lastname",
"birthdate",
"birthplace",
"playerCountry",
"leg",
"positions",
"matchShare",
"playDuration"
]

# add kpiNames to order
order = order + scores.name.to_list()

# select columns
profile_scores = profile_scores[order]

# return result
return profile_scores
Loading