zmiany server.py, ogarniecie xgboosta

This commit is contained in:
Maciej Chmielarz 2024-01-09 15:26:35 +01:00
parent fc85d2f8f6
commit 0da7ccae42

View File

@ -1,6 +1,8 @@
from flask import Flask, request, jsonify
from flask_cors import CORS
from modele.modele import LogisticRegression_predict_proba, xgboost_predict_proba, xgboost_predict_proba_v2
import numpy as np
import math
app = Flask(__name__)
@ -24,37 +26,168 @@ def get_model():
# Elementy z list: czesc ciała,rodzaj uderzenia,pozycja strzelca oraz typ akcji
position_name = request.args.get('shooter')
body_part = request.args.get('bodyPart')
technique = request.args.get('technique')
acionType = request.args.get('actionType')
minute = request.args.get('gameMinute')
position_name = request.args.get('shooterPossition')
print("pozycja strzelca: " + position_name)
shooter_x, shooter_y = shooter.split(',')
shooter_x = float(shooter_x)
shooter_y = float(shooter_y)
print("minuta: ", minute)
print("pozycja x1 strzelca: ", shooter_x)
print("pozycja y1 strzelca: ", shooter_y)
print("czesc ciala: " + body_part)
print("technika: " + technique)
print("typ akcji: " + acionType)
# lista wspolzawodnikow oraz przeciwnikow
# lista atakujacy oraz obroncy
teamMatesList = request.args.get('defenders')
opponentsList = request.args.get('strickers')
def konwertujDoListy(listaString):
listaString = listaString.split(",")
listaFloat = []
for elem in listaString:
listaFloat.append(float(elem))
return listaFloat
def zmienWMaciez(lista):
maciez = []
for i in range(0,len(lista),2):
player = [float(lista[i]),float(lista[i+1])]
maciez.append(player)
if(len(maciez) < 10):
for i in range(len(maciez),11,1):
maciez.append([np.nan,np.nan])
return maciez
obroncy = request.args.get('defenders')
atakujacy = request.args.get('strickers')
print("wspolrzedne obroncow: ", obroncy)
if obroncy == "":
obroncy_macierz = [[np.nan, np.nan]]
else:
obroncy_lista = konwertujDoListy(obroncy)
obroncy_macierz = zmienWMaciez(obroncy_lista)
print("wspolrzedne obroncow: " + teamMatesList)
print("wspolrzedne przeciwnikow: " + opponentsList)
if atakujacy == "":
atakujacy_macierz = [[np.nan, np.nan]]
else:
atakujacy_lista = konwertujDoListy(atakujacy)
atakujacy_macierz = zmienWMaciez(atakujacy_lista)
print("wspolrzedne obroncow: ", obroncy)
print("wspolrzedne atakujacych: ", atakujacy)
# Bramkarz
goalkepper = "2,2"
#goalkepper = request.args.get('goalkeeper')
def euclidean_distance(point1, point2):
"""Calculate the Euclidean distance between two points."""
return math.sqrt((point1[0] - point2[0])**2 + (point1[1] - point2[1])**2)
def sort_coordinates_by_distance(coordinates, reference_point):
"""Sort a list of coordinates based on Euclidean distance from a reference point.
Coordinates with NaN values are placed at the end of the list.
"""
def distance_or_inf(coord):
"""Helper function to handle coordinates with NaN values."""
if hasattr(coord, '__iter__') and len(coord) >= 2:
if np.isnan(coord[0]) or np.isnan(coord[1]):
return float('inf') # Set distance to infinity for NaN coordinates
return euclidean_distance(coord, reference_point)
return float('inf') # Set distance to infinity for non-iterable coordinates
sorted_coordinates = sorted(coordinates, key=distance_or_inf)
if(len(sorted_coordinates) < 10):
for i in range(len(sorted_coordinates),11,1):
sorted_coordinates.append([np.nan,np.nan])
return sorted_coordinates
print("bramkarz: " + str(goalkepper) )
# POSORTOWANI OBRONCY I ATAKUJACY
posortowani_obroncy = sort_coordinates_by_distance(obroncy_macierz, [shooter_x, shooter_y])
posortowani_atakujacy = sort_coordinates_by_distance(atakujacy_macierz, [shooter_x, shooter_y])
response = xgboost_predict_proba_v2(shooter = shooter,
opponentsList = opponentsList, teamMatesList = teamMatesList, minute = 20, position_name = position_name, shot_body_part_name = body_part, shot_technique_name = technique,
shot_type_name = acionType, shot_first_time = False,
shot_aerial_won = False, shot_deflected = False, shot_open_goal = False,
shot_follows_dribble = False, shot_redirect = False, shot_kick_off = False, goalkeeper= goalkepper
)
print("Posortowani obroncy: ", posortowani_obroncy)
print("Posortowani atakujacy: ", posortowani_atakujacy)
## add angle, match minutes and number of players
def loc2angle(x, y):
rads = math.atan(7.32 * x / (x**2 + (y - 34)**2 - (7.32/2)**2))
rads = math.pi + rads if rads < 0 else rads
deg = math.degrees(rads)
return deg
def loc2distance(x, y):
return math.sqrt(x**2 + (y - 34)**2)
angle = loc2angle(x = shooter_x, y = shooter_y)
dist = loc2distance(x = shooter_x, y = shooter_y)
# BRAMKARZ
goalkepper = request.args.get('goalkeeper')
if goalkepper == "null":
goalkepper_x, goalkepper_y = np.nan, np.nan
else:
goalkepper_x, goalkepper_y = goalkepper.split(',')
goalkepper_x = float(goalkepper_x)
goalkepper_y = float(goalkepper_y)
print("bramkarz:", goalkepper_x, goalkepper_y)
# MODEL XGBOOST
response = xgboost_predict_proba(minute=1,
position_name=position_name,
shot_body_part_name=body_part,
shot_technique_name=technique,
shot_type_name=acionType,
shot_first_time=False,
shot_one_on_one=False,
shot_aerial_won=False,
shot_deflected=False,
shot_open_goal=False,
shot_follows_dribble=False,
shot_redirect=False,
shot_kick_off=False,
x1=shooter_x, y1=shooter_y,
number_of_players_opponents=0,
number_of_players_teammates=0,
angle=angle, distance=dist,
x_player_opponent_Goalkeeper=goalkepper_x, y_player_opponent_Goalkeeper=goalkepper_y,
x_player_opponent_1=posortowani_obroncy[0][0], y_player_opponent_1=posortowani_obroncy[0][1],
x_player_opponent_2=posortowani_obroncy[1][0], y_player_opponent_2=posortowani_obroncy[1][1],
x_player_opponent_3=posortowani_obroncy[2][0], y_player_opponent_3=posortowani_obroncy[2][1],
x_player_opponent_4=posortowani_obroncy[3][0], y_player_opponent_4=posortowani_obroncy[3][1],
x_player_opponent_5=posortowani_obroncy[4][0], y_player_opponent_5=posortowani_obroncy[4][1],
x_player_opponent_6=posortowani_obroncy[5][0], y_player_opponent_6=posortowani_obroncy[5][1],
x_player_opponent_7=posortowani_obroncy[6][0], y_player_opponent_7=posortowani_obroncy[6][1],
x_player_opponent_8=posortowani_obroncy[7][0], y_player_opponent_8=posortowani_obroncy[7][1],
x_player_opponent_9=posortowani_obroncy[8][0], y_player_opponent_9=posortowani_obroncy[8][1],
x_player_opponent_10=posortowani_obroncy[9][0], y_player_opponent_10=posortowani_obroncy[9][1],
x_player_teammate_Goalkeeper=np.nan, y_player_teammate_Goalkeeper=np.nan,
x_player_teammate_1=posortowani_atakujacy[0][0], y_player_teammate_1=posortowani_atakujacy[0][1],
x_player_teammate_2=posortowani_atakujacy[1][0], y_player_teammate_2=posortowani_atakujacy[1][1],
x_player_teammate_3=posortowani_atakujacy[2][0], y_player_teammate_3=posortowani_atakujacy[2][1],
x_player_teammate_4=posortowani_atakujacy[3][0], y_player_teammate_4=posortowani_atakujacy[3][1],
x_player_teammate_5=posortowani_atakujacy[4][0], y_player_teammate_5=posortowani_atakujacy[4][1],
x_player_teammate_6=posortowani_atakujacy[5][0], y_player_teammate_6=posortowani_atakujacy[5][1],
x_player_teammate_7=posortowani_atakujacy[6][0], y_player_teammate_7=posortowani_atakujacy[6][1],
x_player_teammate_8=posortowani_atakujacy[7][0], y_player_teammate_8=posortowani_atakujacy[7][1],
x_player_teammate_9=posortowani_atakujacy[8][0], y_player_teammate_9=posortowani_atakujacy[8][1],
x_player_teammate_10=posortowani_atakujacy[9][0], y_player_teammate_10=posortowani_atakujacy[9][1])
# response = xgboost_predict_proba(shooter = shooter,
# opponentsList = opponentsList, teamMatesList = teamMatesList, minute = 20, position_name = position_name, shot_body_part_name = body_part, shot_technique_name = technique,
# shot_type_name = acionType, shot_first_time = False,
# shot_aerial_won = False, shot_deflected = False, shot_open_goal = False,
# shot_follows_dribble = False, shot_redirect = False, shot_kick_off = False, goalkeeper= goalkepper
# )
#print(response)
res = str(response)