def shotstats_get(logger, match_list): """" gets shots per match """ logger.debug('stats_get()') g_shotstat_dict = {} g_goal_dic_ = {} for match_id_ in match_list: # get match information match_dic = match_info_get(LOGGER, match_id_, None, ['date', 'home_team_id', 'home_team__shortcut', 'visitor_team_id', 'visitor_team__shortcut', 'result']) # get list of shots vlist = ['shot_id', 'match_id', 'match_shot_resutl_id', 'player_id', 'player__first_name', 'player__last_name', 'player__jersey', 'player__stick', 'team_id', 'coordinate_x', 'coordinate_y', 'match__home_team_id', 'match__visitor_team_id', 'timestamp', 'real_date'] shot_list = shot_list_get(LOGGER, 'match_id', match_id_, vlist) shift_list = shift_get(logger, 'match_id', match_id, ['shift']) periodevent_list = periodevent_get(logger, 'match_id', match_id, ['period_event']) # get corsi statistics (shots_for_5v5, shots_against_5v5, shots_ongoal_for_5v5, shots_ongoal_against_5v5, shot_list_5v5) = gameshots5v5_get(logger, match_id_, match_dic, 'home', shot_list, shift_list, periodevent_list) # 5v5 goals from periodevents goals5v5_dic = goals5v5_get(logger, match_id_, match_dic) # convert shots and goals into structure we can process later on # we also need the XGMODEL_DIC to check if we have the shotcoordinates in our structure (shotstat_dic, goal_dic) = shotlist_process(LOGGER, shot_list_5v5, XGMODEL_DIC, REBOUND_INTERVAL, BREAK_INTERVAL) g_shotstat_dict[match_id_] = shotstat_dic # g_goal_dic_[match_id_] = {'home': len(goal_dic['home']), 'visitor': len(goal_dic['visitor'])} # we need to compare against 5v5 goals g_goal_dic_[match_id_] = {'home': goals5v5_dic['home'], 'visitor': goals5v5_dic['visitor']} return(g_shotstat_dict, g_goal_dic_)
def matchstatistics_get(logger, request, fkey=None, fvalue=None): """ matchstatistics grouped by days """ logger.debug('matchstatistics_get({0}:{1})'.format(fkey, fvalue)) ismobile = mobile_check(logger, request) # fkey = 'match_id' # fvalue = 1804 # we protect the REST and will not return anything without matchid if fkey: # we need some match_information matchinfo_dic = match_info_get(logger, fvalue, request.META) # get colors to be used color_dic = chart_colors_get(logger, matchinfo_dic) # pylint: disable=E0602 vs_name = _('vs.') subtitle = '{0} {2} {1}'.format(matchinfo_dic['home_team__team_name'], matchinfo_dic['visitor_team__team_name'], vs_name) # get list of shots shot_list = shot_list_get(logger, fkey, fvalue, ['timestamp', 'match_shot_resutl_id', 'real_date', 'team_id', 'player__first_name', 'player__last_name', 'zone', 'coordinate_x', 'coordinate_y', 'player__jersey']) # get list of shifts shift_list = shift_get(logger, fkey, fvalue, ['shift']) # get period events periodevent_list = periodevent_get(logger, fkey, fvalue, ['period_event']) # get rosters roster_list = roster_get(logger, fkey, fvalue, ['roster']) # create plotlines to be addedd to chart # plotbands_list = penaltyplotlines_get(logger, fkey, fvalue, color_dic['home_team_color_penalty_primary'], color_dic['visitor_team_color_penalty_secondary']) plotbands_list = penaltyplotlines_get(logger, fkey, fvalue, color_dic['visitor_team_color_penalty_secondary'], color_dic['home_team_color_penalty_primary']) (_sf_5v5, _sa_5v5, _sogf_5v5, _soga_5v5, shot_list_5v5) = gameshots5v5_get(logger, matchinfo_dic, 'foo', shot_list, shift_list, periodevent_list) result = [] # get matchstatistics result.append(matchstats_get(logger, fvalue)) # create chart for shots per match # pylint: disable=E0602 result.append(_gameshots_get(logger, _('Shots per minute'), subtitle, ismobile, request, fkey, fvalue, matchinfo_dic, shot_list, color_dic)) # create shotflowchart # pylint: disable=E0602 result.append(_gameflow_get(logger, _('Gameflow'), subtitle, ismobile, request, fkey, fvalue, matchinfo_dic, shot_list, color_dic)) # create chart for shotstatus # pylint: disable=E0602 result.append(_gameshootstatus_get(logger, _('Shots by Result'), subtitle, ismobile, request, fkey, fvalue, matchinfo_dic, shot_list)) # create shotzone chart # pylint: disable=E0602 result.append(_gamezoneshots_get(logger, _('Shots per Zone'), subtitle, ismobile, request, matchinfo_dic, shot_list)) # shotmap # pylint: disable=E0602 result.append(_gameheatmap_get(logger, _('Shot attempts 5v5'), subtitle, ismobile, request, fkey, fvalue, matchinfo_dic, shot_list_5v5)) # shotmap # pylint: disable=E0602 result.append(_gameshotmap_get(logger, _('Game Shotmap'), subtitle, ismobile, request, fkey, fvalue, matchinfo_dic, shot_list)) # player corsi # pylint: disable=E0602 result.extend(_gamecorsi_get(logger, subtitle, ismobile, request, matchinfo_dic, shot_list, shot_list_5v5, shift_list, periodevent_list, roster_list, plotbands_list, color_dic)) # puck possession # pylint: disable=E0602 result.append(_gamepuckpossession_get(logger, _('Puck possession'), subtitle, ismobile, request, fkey, fvalue, matchinfo_dic, shot_list, color_dic)) # time on ice per player # pylint: disable=E0602 result.extend(_gametoi_get(logger, _('Time on Ice per Player'), subtitle, ismobile, request, fkey, fvalue, matchinfo_dic, shift_list)) # pylint: disable=E0602 result.append(_shiftchart_get(logger, _('Shift Chart'), subtitle, ismobile, request, fkey, fvalue, matchinfo_dic, shift_list, roster_list, periodevent_list, color_dic)) # pylint: disable=E0602 result.append(_gamematchup_get(logger, _('5v5 Matchup'), subtitle, ismobile, request, fkey, fvalue, matchinfo_dic, shot_list, shift_list, roster_list, periodevent_list)) # pylint: disable=E0602 result.append(_chatterchart_get(logger, _('Real-Time Fan Reactions'), subtitle, ismobile, request, fkey, fvalue, matchinfo_dic, shot_list, periodevent_list, color_dic)) else: result = {'error': 'Please specify a matchid'} return result
# DEBUG mode on/off DEBUG = True # get season_id to analyse SEASON_ID = 1 # initialize logger LOGGER = logger_setup(DEBUG) # get list of shots from 2019/2020 season - they will be used as base for our model # MATCH_LIST = match_list_get(LOGGER, 'season_id', SEASON_ID, ['match_id']) VLIST = [ 'shot_id', 'match_id', 'match_shot_resutl_id', 'player_id', 'team_id', 'coordinate_x', 'coordinate_y', 'match__home_team_id', 'match__visitor_team_id', 'timestamp' ] SHOT_LIST = shot_list_get(LOGGER, 'match__season_id', SEASON_ID, VLIST) # get player_dictionary to determine left/right hand player PLAYER_DIC = player_dic_get(LOGGER) # build the model MODEL_DIC = xgmodel_build(LOGGER, SHOT_LIST, PLAYER_DIC, REBOUND_INTERVAL, BREAK_INTERVAL) # add percentage values to model MODEL_DIC = pctg_calculate(MODEL_DIC) # result = xgmodel_add(LOGGER, 'id', 1, {'xg_data': MODEL_DIC}) # print(result) json_store('model_data.json', MODEL_DIC)
def teammatchstat_add(logger, match_dic, xg_data_dic, xg_weights_dic): """ add team to database """ # pylint: disable=R0914 logger.debug('teammatchstat_add()') rebound_interval = 3 break_interval = 7 match_id = match_dic['match_id'] match_info_dic = match_info_get(logger, match_id, None) shot_list = shot_list_get(logger, 'match_id', match_id, [ 'real_date', 'shot_id', 'match_id', 'timestamp', 'match_shot_resutl_id', 'match__home_team_id', 'team_id', 'player__first_name', 'player_id', 'player__last_name', 'player__stick', 'zone', 'coordinate_x', 'coordinate_y', 'player__jersey' ]) shift_list = shift_get(logger, 'match_id', match_id, ['shift']) periodevent_list = periodevent_get(logger, 'match_id', match_id, ['period_event']) # rebounds and breaks rb_dic = rebound_breaks_get(logger, shot_list, match_info_dic) result_list = [] for team in ['home', 'visitor']: if team == 'home': o_team = 'visitor' team_id = match_info_dic['home_team_id'] else: o_team = 'home' team_id = match_info_dic['visitor_team_id'] # get corsi statistics (shots_for_5v5, shots_against_5v5, shots_ongoal_for_5v5, shots_ongoal_against_5v5, shot_list_5v5) = gameshots5v5_get(logger, match_info_dic, team, shot_list, shift_list, periodevent_list) # 5v5 goals from periodevents goals5v5_dic = goals5v5_get(logger, match_id, match_info_dic) # get goalipull events and goals goaliepull_dic = goaliepull_get(logger, team, periodevent_list) # xgf xga calculation xgf_dic = {} if xg_data_dic and xg_weights_dic and shot_list_5v5: # we also need the XGMODEL_DIC to check if we have the shotcoordinates in our structure (shotstat_dic, _goal_dic) = shotlist_process(logger, shot_list_5v5, xg_data_dic, rebound_interval, break_interval) # lets apply the magic algorithm to estimate xGF playerxgf_dic = xgf_calculate(logger, shotstat_dic, xg_weights_dic) xgf_dic = xgscore_get(logger, playerxgf_dic) game_header = gameheader_get(logger, 'match_id', match_id, ['gameheader']) points = points_get(logger, team, game_header) if 'lastEventTime' in game_header: lasteventtime = game_header['lastEventTime'] else: lasteventtime = 3600 data_dic = { 'match_id': match_id, 'matchduration': lasteventtime, 'team_id': team_id, 'goals_for': match_dic[team]['goals'], 'goals_for_5v5': goals5v5_dic[team], 'goals_against': match_dic[o_team]['goals'], 'goals_against_5v5': goals5v5_dic[o_team], 'goals_pp': match_dic[team]['ppGoals'], 'goals_pp_against': match_dic[o_team]['ppGoals'], 'goals_sh': match_dic[team]['shGoals'], 'shots_for': match_dic[team]['shotsAttempts'], 'shots_for_5v5': shots_for_5v5, 'shots_pctg': pctg_float_get(match_dic[team]['goals'], match_dic[team]['shotsAttempts']), 'shots_ongoal_for': match_dic[team]['shotsOnGoal'], 'shots_ongoal_for_5v5': shots_ongoal_for_5v5, 'shots_against': match_dic[o_team]['shotsAttempts'], 'shots_against_5v5': shots_against_5v5, 'shots_ongoal_against': match_dic[o_team]['shotsOnGoal'], 'shots_ongoal_against_5v5': shots_ongoal_against_5v5, 'shots_ongoal_pctg': pctg_float_get(match_dic[team]['goals'], match_dic[team]['shotsOnGoal']), 'saves': match_dic[team]['saves'], 'saves_pctg': pctg_float_get(match_dic[team]['saves'], match_dic[o_team]['shotsOnGoal']), 'faceoffswon': match_dic[team]['faceOffsWon'], 'faceoffslost': match_dic[o_team]['faceOffsWon'], 'faceoffswon_pctg': match_dic[team]['faceOffsWonPercent'], 'penaltyminutes_taken': match_dic[team]['penaltyMinutes'], 'penaltyminutes_drawn': match_dic[o_team]['penaltyMinutes'], 'powerplayseconds': match_dic[team]['powerPlaySeconds'], 'rebounds_for': rb_dic[team]['rebounds'], 'rebounds_against': rb_dic[o_team]['rebounds'], 'goals_rebound_for': rb_dic[team]['rebound_goals'], 'goals_rebound_against': rb_dic[o_team]['rebound_goals'], 'breaks_for': rb_dic[team]['breaks'], 'breaks_against': rb_dic[o_team]['breaks'], 'goals_break_for': rb_dic[team]['break_goals'], 'goals_break_against': rb_dic[o_team]['break_goals'], 'ppcount': match_dic[team]['ppCount'], 'shcount': match_dic[team]['shCount'], 'ppefficiency': match_dic[team]['ppEfficiency'], 'shefficiency': match_dic[team]['shEfficiency'], 'points': points, 'goalie_own_pull': goaliepull_dic['goalieown_pull'], 'goalie_other_pull': goaliepull_dic['goalieother_pull'], 'goalie_own_pulltime': goaliepull_dic['goaliepull_time'], 'goals_en_for': goaliepull_dic['goals_en_for'], 'goals_en_against': goaliepull_dic['goals_en_against'], 'goals_wogoalie_for': goaliepull_dic['goals_wogoalie_for'] } if xgf_dic: data_dic['xgoals_for'] = xgf_dic[team] data_dic['xgoals_against'] = xgf_dic[o_team] try: # add teammatchstat obj, _created = Teammatchstat.objects.update_or_create( match_id=match_id, team_id=team_id, defaults=data_dic) obj.save() result_list.append(obj.id) except BaseException as err_: logger.critical('ERROR in teammatchstat_add(): {0}'.format(err_)) result_list.append(None) logger.debug('teammatchstat_add() ended with: {0}'.format(result_list)) return result_list
'coordinate_y', 'match__home_team_id', 'match__visitor_team_id', 'timestamp' ] # empty output listg OUTPUT_LIST = [] for match_id in MATCH_LIST: match_dic = match_info_get(LOGGER, match_id, None, [ 'date', 'home_team_id', 'home_team__shortcut', 'visitor_team_id', 'visitor_team__shortcut', 'result' ]) # get list of shots shot_list = shot_list_get(LOGGER, 'match_id', match_id, VLIST) shift_list = shift_get(logger, 'match_id', match_id, ['shift']) periodevent_list = periodevent_get(logger, 'match_id', match_id, ['period_event']) # get corsi statistics (shots_for_5v5, shots_against_5v5, shots_ongoal_for_5v5, shots_ongoal_against_5v5, shot_list_5v5) = gameshots5v5_get(LOGGER, match_id, match_dic, 'foo', shot_list, shift_list, periodevent_list) # 5v5 goals from periodevents goals5v5_dic = goals5v5_get(LOGGER, match_id, match_dic) # convert shots and goals into structure we can process later on