def __init__(self, event): moments = event['moments'] self.moments = [Moment(moment) for moment in moments] home_players = event['home']['players'] guest_players = event['visitor']['players'] players = home_players + guest_players player_ids = [player['playerid'] for player in players] player_names = [ " ".join([player['firstname'], player['lastname']]) for player in players ] player_jerseys = [player['jersey'] for player in players] player_positions = [player['position'] for player in players] player_id_position_dict = dict() for i in range(len(players)): player_id_position_dict[player_ids[i]] = player_positions[i] values = list(zip(player_names, player_jerseys)) # Example: 101108: ['Chris Paul', '3'] self.player_ids_dict = dict(zip(player_ids, values)) self.home_players = home_players self.guest_players = guest_players self.players = players # store id and position self.player_positions = player_positions self.player_id_position_dict = player_id_position_dict
def __init__(self, event, path_to_json, event_index, frame): self.frame_init = frame moments = event['moments'] self.moments = [Moment(moment) for moment in moments] home_players = event['home']['players'] guest_players = event['visitor']['players'] players = home_players + guest_players player_ids = [player['playerid'] for player in players] player_names = [ " ".join([player['firstname'], player['lastname']]) for player in players ] player_jerseys = [player['jersey'] for player in players] values = list(zip(player_names, player_jerseys)) # Example: 101108: ['Chris Paul', '3'] self.player_ids_dict = dict(zip(player_ids, values)) self.frame = 0 self.ball_handler = 0 self.screener = None self.on_ball_defender = None self.ball_radius = None self.path_to_json = path_to_json self.event_index = event_index self.test_a = 0 self.j = 0 self.handler_defender = np.zeros(34) self.screener_defender = np.zeros(34) self.handler_screener = np.zeros(34) self.handler_basket = np.zeros(34) self.screener_basket = np.zeros(34) self.defender_basket = np.zeros(34) self.features = np.zeros(30)
def __init__(self, event): moments = event['moments'] self.moments = [Moment(moment) for moment in moments] home_players = event['home']['players'] guest_players = event['visitor']['players'] players = home_players + guest_players player_ids = [player['playerid'] for player in players] player_names = [" ".join([player['firstname'], player['lastname']]) for player in players] player_jerseys = [player['jersey'] for player in players] values = list(zip(player_names, player_jerseys)) # Example: 101108: ['Chris Paul', '3'] self.player_ids_dict = dict(zip(player_ids, values))
def read_json(self): data_frame = pd.read_json(self.path_to_json) # Setup game parameters and players event = data_frame['events'][0] self.home_team = Team(event['home']['teamid']) self.guest_team = Team(event['visitor']['teamid']) self.player_ids_dict = self.get_player_dict(event) print('Setting up player dictionaries...') temp_player_ids_dicts_all = [] for event in data_frame['events']: player_dict = self.get_player_dict(event) temp_player_ids_dicts_all.extend( [player_dict for _ in range(len(event['moments']))]) print('Setting up moment dictionary...') moments_list = [m for e in data_frame['events'] for m in e['moments']] # divide moments based on each quater quater_list = [[] for _ in range(4)] quarter_player_dict_list = [[] for _ in range(4)] for moment, player_dict in zip(moments_list, temp_player_ids_dicts_all): quater_list[int(moment[0]) - 1].append(moment) quarter_player_dict_list[int(moment[0]) - 1].append(player_dict) # Create quater dict for each quater print('Creating moments...') self.moments = [] self.player_ids_dicts_all = [] for i in range(4): d_list = [(m[2], m, pl_dt) for m, pl_dt in zip( quater_list[i], quarter_player_dict_list[i])] moment_dict = {key: value for (key, value, _) in d_list} player_dict = {key: value for (key, _, value) in d_list} moment_keys = list(sorted(list(moment_dict.keys()), reverse=True)) cur_moments = [ Moment(moment_dict[mk], player_dict[mk]) for mk in moment_keys if isinstance(moment_dict[mk][3], float) ] cur_player_dicts = [ player_dict[mk] for mk in moment_keys if isinstance(moment_dict[mk][3], float) ] self.moments.extend(cur_moments) self.player_ids_dicts_all.extend(cur_player_dicts)
def __init__(self, event, analytics=""): moments = event['moments'] self.analytics = analytics self.moments = [Moment(moment) for moment in moments] home_players = event['home']['players'] guest_players = event['visitor']['players'] players = home_players + guest_players player_ids = [player['playerid'] for player in players] player_names = [ " ".join([player['firstname'], player['lastname']]) for player in players ] player_jerseys = [player['jersey'] for player in players] values = list(zip(player_names, player_jerseys)) self.player_ids_dict = dict(zip(player_ids, values)) self.pause = False
def __init__(self, event, anno): self.gameid = '00' + str(int(anno['gameid'])) self.home_team_id = event['home']['teamid'] self.away_team_id = event['visitor']['teamid'] self.moments = [] self.pbp = event['playbyplay'] self.anno = anno self.start_time = 0 self.end_time = 999 for ind, moment in enumerate(event['moments']): try: moment = Moment(moment, anno=self.anno) self.moments.append(moment) if moment.game_clock < self.end_time: self.end_time = moment.game_clock if moment.game_clock > self.start_time: self.start_time = moment.game_clock except MomentException: continue except PNRException: continue except TeamNotFoundException: raise TeamNotFoundException start_moment = self.moments[0] start_moment_ids = [player.id for player in start_moment.players] home_players = pd.DataFrame(event['home']['players']) guest_players = pd.DataFrame(event['visitor']['players']) self.home_players = home_players.loc[home_players.playerid.isin( start_moment_ids), :].T.to_dict().values() self.guest_players = guest_players.loc[guest_players.playerid.isin( start_moment_ids), :].T.to_dict().values() self.players = self.home_players + self.guest_players player_ids = [player['playerid'] for player in self.players] player_names = [ '%s %s' % (player['firstname'], player['lastname']) for player in self.players ] player_jerseys = [player['jersey'] for player in self.players] values = list(zip(player_names, player_jerseys)) self.player_ids_dict = dict(zip(player_ids, values)) self._resolve_home_basket()
def __init__(self, moments, player_info, event_description, probability_to_make, shot_time, feat_info): moments_list = [] # Create list of moments (11 rows of the dataframe) first_index_of_moment, last_index_of_moment, last_index = 0, 11, len(moments) - 1 while last_index_of_moment <= last_index: df_temp = moments.iloc[first_index_of_moment:last_index_of_moment, :] moments_list.append(df_temp) first_index_of_moment, last_index_of_moment = last_index_of_moment, last_index_of_moment + 11 self.moments = [Moment(moment) for moment in moments_list] # store the list in self.moments player_ids = player_info[0] player_names = player_info[1] player_jerseys = player_info[2] values = list(zip(player_names, player_jerseys)) # Dictionary for player ids that contains Name, Jersey Number self.player_ids_dict = dict(zip(player_ids, values)) self.event_description = event_description self.probability_to_make = probability_to_make self.shot_time = shot_time self.feat_info = feat_info # tuple with -> [0] opp_1_dist, [1] opp_2_dist, [2] opp_3_dist, [3] ts%
def __init__(self, event, positions_map=None): self.id = event['eventId'] moments = event['moments'] self.moments = [Moment(moment) for moment in moments] home_players = event['home']['players'] guest_players = event['visitor']['players'] players = home_players + guest_players player_teams = ['Home' for player in home_players ] + ['Away' for player in guest_players] player_ids = [player['playerid'] for player in players] player_names = [ " ".join([player['firstname'], player['lastname']]) for player in players ] player_jerseys = [player['jersey'] for player in players] if positions_map is not None: player_positions = [ positions_map[player_name] for player_name in player_names ] # Fall back to the positions in this dataset if the player is not present in the map. for i in xrange(len(player_positions)): if len(player_positions[i]) == 0: player_positions[i] = players[i]['position'] else: player_positions = [player['position'] for player in players] values = list(zip(player_names, player_jerseys, player_positions)) # Example: 101108: ['Chris Paul', '3', 'F-G'] self.player_ids_dict = dict(zip(player_ids, values)) # Example: 'Chris Paul': 'Home' self.player_teams_dict = dict(zip(player_names, player_teams)) self.player_names = { key: value[0] for key, value in self.player_ids_dict.iteritems() } self.player_types = { value[0]: self.player_teams_dict[value[0]] + ''.join(value[2].split('-')) for key, value in self.player_ids_dict.iteritems() }
def __init__(self, event): ''' :param event: event in list(events) ''' #根据Json结构倒入moments moments = event["moments"] #self.moments is a list for moment instances self.moments = [Moment(moment) for moment in moments] #处理队员 #handle all players players = event["visitor"]["players"] + event["home"]["players"] player_ids = [player["playerid"] for player in players] player_names = [ " ".join([player["firstname"], player["lastname"]]) for player in players ] player_jerseys = [player['jersey'] for player in players] #将球员信息组合成dict形式: {player_id: [ name, jersey]}, 定义成员变量 #Compress player information in dict self.player_ids_dict = dict( zip(player_ids, zip(player_names, player_jerseys)))
import json from Moment import Moment with open('/home/zhouh/下载/merge_json.json', 'r') as f1: data_new = json.load(f1) event = data_new['events'] moments = event['moments'] Id = [] for i in range(len(moments)): moment = Moment(moments[i]) player = [j.id for j in moment.players] Id.append(sorted(player)) total = 0 change_pos = [] for i in range(1, len(Id)): if Id[i] != Id[i - 1]: total += 1 change_pos.append(i) #print(total) #print(change_pos) #for i in range(len(moments)): # moment = Moment(moments[i])
with open('/home/zhouh/下载/merge_json.json', 'r') as f1: merge = json.load(f1) home_score_point = [] home_not_score_point = [] visitor_score_point = [] visitor_not_score_point = [] home_aver_pos = [] home_aver_pos_val = 0 visitor_aver_pos = [] visitor_aver_pos_val = 0 ################# get average position ################## for moment in merge['events']['moments']: moment = Moment(moment) for n in range(5): home_aver_pos_val += moment.players[n].x home_aver_pos_val /= 5 home_aver_pos.append(home_aver_pos_val) for m in range(5, 10): visitor_aver_pos_val += moment.players[m].x visitor_aver_pos_val /= 5 visitor_aver_pos.append(visitor_aver_pos_val) ################ home team score round ################################## for j in range(len(home)): for i in range(len(merge['events']['moments'])): if (merge['events']['moments'][i][0] == home[j][0]) and (int( merge['events']['moments'][i][2]) == home[j][1]):