def get_rim_defense(playerID): num = randint(20, 40) print num player_defense = nba.PlayerDefenseTracking(playerID) player_defense.json.pop('parameters') player_defense.json.pop('resource') sleep(num) return player_defense.json['resultSets'][0]['rowSet'][3][9]
def test(): pid = get_player('Tim', 'Duncan') vs_pid = get_player('Stephen', 'Curry') assert player.PlayerList() assert player.PlayerSummary(pid) # assert player.PlayerGeneralSplits(pid) # assert player.PlayerOpponentSplits(pid) assert player.PlayerLastNGamesSplits(pid) assert player.PlayerInGameSplits(pid) assert player.PlayerClutchSplits(pid) # assert player.PlayerShootingSplits(pid) assert player.PlayerPerformanceSplits(pid) assert player.PlayerYearOverYearSplits(pid) assert player.PlayerCareer(pid) assert player.PlayerProfile(pid) assert player.PlayerGameLogs(pid) assert player.PlayerShotTracking(pid) assert player.PlayerReboundTracking(pid) assert player.PlayerPassTracking(pid) assert player.PlayerDefenseTracking(pid) # assert player.PlayerShotLogTracking(pid) # assert player.PlayerReboundLogTracking(pid) assert player.PlayerVsPlayer(pid, vs_pid)
def testAll(self): assert player.PlayerList() assert player.PlayerSummary(self.playerId) # assert player.PlayerGeneralSplits(self.playerId) # assert player.PlayerOpponentSplits(self.playerId) assert player.PlayerLastNGamesSplits(self.playerId) assert player.PlayerInGameSplits(self.playerId) assert player.PlayerClutchSplits(self.playerId) # assert player.PlayerShootingSplits(self.playerId) assert player.PlayerPerformanceSplits(self.playerId) assert player.PlayerYearOverYearSplits(self.playerId) assert player.PlayerCareer(self.playerId) assert player.PlayerProfile(self.playerId) assert player.PlayerGameLogs(self.playerId) assert player.PlayerShotTracking(self.playerId) assert player.PlayerReboundTracking(self.playerId) assert player.PlayerPassTracking(self.playerId) assert player.PlayerDefenseTracking(self.playerId) # assert player.PlayerShotLogTracking(self.playerId) # assert player.PlayerReboundLogTracking(self.playerId) assert player.PlayerVsPlayer(self.playerId, self.vs_playerId)
def _get_query_results(params={}, flatten_keys=False): if 'module' not in params or 'sub_module' not in params or 'function' not in params: return [] if not flatten_keys and 'value_query' not in params: return [] if params['module'] == 'player': if 'first_name' not in params: return [] first = params['first_name'] last = params['last_name'] if 'last_name' in params else None team_id = params['team_id'] if 'team_id' in params else 0 measure_type = params[ 'measure_type'] if 'measure_type' in params else 'Base' per_mode = params['per_mode'] if 'per_mode' in params else 'PerGame' plus_minus = params['plus_minus'] if 'plus_minus' in params else 'N' pace_adjust = params['pace_adjust'] if 'pace_adjust' in params else 'N' rank = params['rank'] if 'rank' in params else 'N' league_id = params['league_id'] if 'league_id' in params else '00' season = params['season'] if 'season' in params else '2016-17' season_type = params[ 'season_type'] if 'season_type' in params else 'Regular Season' po_round = params['po_round'] if 'po_round' in params else '0' outcome = params['outcome'] if 'outcome' in params else '' location = params['location'] if 'location' in params else '' month = params['month'] if 'month' in params else '0' season_segment = params[ 'season_segment'] if 'season_segment' in params else '' date_from = params['date_from'] if 'date_from' in params else '' date_to = params['date_to'] if 'date_to' in params else '' opponent_team_id = params[ 'opponent_team_id'] if 'opponent_team_id' in params else '0' vs_conference = params[ 'vs_conference'] if 'vs_conference' in params else '' vs_division = params['vs_division'] if 'vs_division' in params else '' game_segment = params[ 'game_segment'] if 'game_segment' in params else '' period = params['period'] if 'period' in params else '0' shot_clock_range = params[ 'shot_clock_range'] if 'shot_clock_range' in params else '' last_n_games = params[ 'last_n_games'] if 'last_n_games' in params else '0' only_current = params['only_current'] if 'only_current' in params else 0 just_id = params['just_id'] if 'just_id' in params else True player_id = player.get_player(first, last_name=last, season=season, only_current=only_current, just_id=just_id) if params['sub_module'] == 'player_career': career = player.PlayerCareer(player_id) if params['function'] == 'all_star_season_totals': temp = career.all_star_season_totals() elif params['function'] == 'career_all_star_season_totals': temp = career.career_all_star_season_totals() elif params['function'] == 'college_season_career_totals': temp = career.college_season_career_totals() elif params['function'] == 'college_season_totals': temp = career.college_season_totals() elif params['function'] == 'post_season_career_totals': temp = career.post_season_career_totals() elif params['function'] == 'post_season_rankings': temp = career.post_season_rankings() elif params['function'] == 'post_season_totals': temp = career.post_season_totals() elif params['function'] == 'preseason_career_totals': temp = career.preseason_career_totals() elif params['function'] == 'preseason_season_totals': temp = career.preseason_season_totals() elif params['function'] == 'regular_season_career_totals': temp = career.regular_season_career_totals() elif params['function'] == 'regular_season_rankings': temp = career.regular_season_rankings() elif params['function'] == 'regular_season_totals': temp = career.regular_season_totals() else: return [] elif params['sub_module'] == 'player_clutch_splits': clutch = player.PlayerClutchSplits(player_id, team_id=team_id, measure_type=measure_type, per_mode=per_mode, plus_minus=plus_minus, \ pace_adjust=pace_adjust, rank=rank, league_id=league_id, season=season, season_type=season_type, po_round=po_round, outcome=outcome, \ location=location, month=month, season_segment=season_segment, date_from=date_from, date_to=date_to, opponent_team_id=opponent_team_id, \ vs_conference=vs_conference, vs_division=vs_division, game_segment=game_segment, period=period, shot_clock_range=shot_clock_range, \ last_n_games=last_n_games ) if params['function'] == 'last10sec_deficit_3point': temp = clutch.last10sec_deficit_3point() elif params['function'] == 'last1min_deficit_5point': temp = clutch.last1min_deficit_5point() elif params['function'] == 'last1min_plusminus_5point': temp = clutch.last1min_plusminus_5point() elif params['function'] == 'last30sec_deficit_3point': temp = clutch.last30sec_deficit_3point() elif params['function'] == 'last30sec_plusminus_5point': temp = clutch.last30sec_plusminus_5point() elif params['function'] == 'last3min_deficit_5point': temp = clutch.last3min_deficit_5point() elif params['function'] == 'last3min_plusminus_5point': temp = clutch.last3min_plusminus_5point() elif params['function'] == 'last5min_deficit_5point': temp = clutch.last5min_deficit_5point() elif params['function'] == 'last5min_plusminus_5point': temp = clutch.last5min_plusminus_5point() else: return [] elif params['sub_module'] == 'player_defense_tracking': temp = player.PlayerDefenseTracking(player_id, team_id=team_id, measure_type=measure_type, per_mode=per_mode, plus_minus=plus_minus, \ pace_adjust=pace_adjust, rank=rank, league_id=league_id, season=season, season_type=season_type, po_round=po_round, outcome=outcome, \ location=location, month=month, season_segment=season_segment, date_from=date_from, date_to=date_to, opponent_team_id=opponent_team_id, \ vs_conference=vs_conference, vs_division=vs_division, game_segment=game_segment, period=period, shot_clock_range=shot_clock_range, \ last_n_games=last_n_games ) elif params['sub_module'] == 'player_game_logs': if params['function'] == 'info': temp = player.PlayerGameLogs(player_id, league_id=league_id, season=season, season_type=season_type) else: return [] elif params['sub_module'] == 'player_general_splits': splits = player.PlayerGeneralSplits(player_id, team_id=team_id, measure_type=measure_type, per_mode=per_mode, plus_minus=plus_minus, \ pace_adjust=pace_adjust, rank=rank, league_id=league_id, season=season, season_type=season_type, po_round=po_round, outcome=outcome, \ location=location, month=month, season_segment=season_segment, date_from=date_from, date_to=date_to, opponent_team_id=opponent_team_id, \ vs_conference=vs_conference, vs_division=vs_division, game_segment=game_segment, period=period, shot_clock_range=shot_clock_range, \ last_n_games=last_n_games ) if params['function'] == 'days_rest': temp = splits.days_rest() elif params['function'] == 'location': temp = splits.location() elif params['function'] == 'month': temp = splits.month() elif params['function'] == 'pre_post_all_star': temp = splits.pre_post_all_star() elif params['function'] == 'starting_position': temp = splits.starting_position() elif params['function'] == 'win_losses': temp = splits.win_losses() else: return [] elif params['sub_module'] == 'player_in_game_splits': if params['function'] == 'by_actual_margin': temp = splits.by_actual_margin() elif params['function'] == 'by_half': temp = splits.by_half() elif params['function'] == 'by_period': temp = splits.by_period() elif params['function'] == 'by_score_margin': temp = splits.by_score_margin() else: return [] elif params['sub_module'] == 'player_last_n_games_splits': n_splits = player.PlayerLastNGamesSplits(player_id, team_id=team_id, measure_type=measure_type, per_mode=per_mode, plus_minus=plus_minus, \ pace_adjust=pace_adjust, rank=rank, league_id=league_id, season=season, season_type=season_type, po_round=po_round, outcome=outcome, \ location=location, month=month, season_segment=season_segment, date_from=date_from, date_to=date_to, opponent_team_id=opponent_team_id, \ vs_conference=vs_conference, vs_division=vs_division, game_segment=game_segment, period=period, shot_clock_range=shot_clock_range, \ last_n_games=last_n_games ) if params['function'] == 'game_number': temp = n_splits.gamenumber() elif params['function'] == 'last10': temp = n_splits.last10() elif params['function'] == 'last15': temp = n_splits.last15() elif params['function'] == 'last20': temp = n_splits.last20() elif params['function'] == 'last5': temp = n_splits.last5() else: return [] elif params['sub_module'] == 'player_list': player_list = player.PlayerList(league_id=league_id, season=season, only_current=only_current) if params['function'] == 'info': temp = player_list.info() else: return [] elif params['sub_module'] == 'player_opponent_splits': op_splits = player.PlayerOpponentSplits(player_id, team_id=team_id, measure_type=measure_type, per_mode=per_mode, plus_minus=plus_minus, \ pace_adjust=pace_adjust, rank=rank, league_id=league_id, season=season, season_type=season_type, po_round=po_round, outcome=outcome, \ location=location, month=month, season_segment=season_segment, date_from=date_from, date_to=date_to, opponent_team_id=opponent_team_id, \ vs_conference=vs_conference, vs_division=vs_division, game_segment=game_segment, period=period, shot_clock_range=shot_clock_range, \ last_n_games=last_n_games ) if params['function'] == 'by_conference': temp = op_splits.by_conference() elif params['function'] == 'by_division': temp = op_splits.by_division() elif params['function'] == 'by_opponent': temp = op_splits.by_opponent() else: return [] elif params['sub_module'] == 'player_pass_tracking': pass_tracking = player.PlayerPassTracking(player_id, team_id=team_id, measure_type=measure_type, per_mode=per_mode, plus_minus=plus_minus, \ pace_adjust=pace_adjust, rank=rank, league_id=league_id, season=season, season_type=season_type, po_round=po_round, outcome=outcome, \ location=location, month=month, season_segment=season_segment, date_from=date_from, date_to=date_to, opponent_team_id=opponent_team_id, \ vs_conference=vs_conference, vs_division=vs_division, game_segment=game_segment, period=period, shot_clock_range=shot_clock_range, \ last_n_games=last_n_games ) if params['function'] == 'passes_made': temp = pass_tracking.passes_made() elif params['function'] == 'passes_received': temp = pass_tracking.passes_received() else: return [] elif params['sub_module'] == 'player_performance_splits': performance_splits = player.PlayerPerformanceSplits(player_id, team_id=team_id, measure_type=measure_type, per_mode=per_mode, plus_minus=plus_minus, \ pace_adjust=pace_adjust, rank=rank, league_id=league_id, season=season, season_type=season_type, po_round=po_round, outcome=outcome, \ location=location, month=month, season_segment=season_segment, date_from=date_from, date_to=date_to, opponent_team_id=opponent_team_id, \ vs_conference=vs_conference, vs_division=vs_division, game_segment=game_segment, period=period, shot_clock_range=shot_clock_range, \ last_n_games=last_n_games ) if params['function'] == 'points_against': temp = performance_splits.points_against() elif params['function'] == 'points_scored': temp = performance_splits.points_scored() elif params['function'] == 'score_differential': temp = performance_splits.score_differential() else: return [] elif params['sub_module'] == 'player_profile': prof = player.PlayerProfile(per_mode=per_mode, league_id=league_id) if params['function'] == 'career_highs': temp = prof.career_highs() elif params['function'] == 'next_game': temp = prof.next_game() elif params['function'] == 'season_highs': temp = prof.season_highs() else: return [] elif params['sub_module'] == 'player_rebound_log_tracking': temp = player.PlayerReboundLogTracking(player_id, team_id=team_id, measure_type=measure_type, per_mode=per_mode, plus_minus=plus_minus, \ pace_adjust=pace_adjust, rank=rank, league_id=league_id, season=season, season_type=season_type, po_round=po_round, outcome=outcome, \ location=location, month=month, season_segment=season_segment, date_from=date_from, date_to=date_to, opponent_team_id=opponent_team_id, \ vs_conference=vs_conference, vs_division=vs_division, game_segment=game_segment, period=period, shot_clock_range=shot_clock_range, \ last_n_games=last_n_games ) elif params['sub_module'] == 'player_rebound_tracking': rb_tracking = player.PlayerReboundTracking(player_id, team_id=team_id, measure_type=measure_type, per_mode=per_mode, plus_minus=plus_minus, \ pace_adjust=pace_adjust, rank=rank, league_id=league_id, season=season, season_type=season_type, po_round=po_round, outcome=outcome, \ location=location, month=month, season_segment=season_segment, date_from=date_from, date_to=date_to, opponent_team_id=opponent_team_id, \ vs_conference=vs_conference, vs_division=vs_division, game_segment=game_segment, period=period, shot_clock_range=shot_clock_range, \ last_n_games=last_n_games ) if params['function'] == 'num_contested_rebounding': temp = rb_tracking.num_contested_rebounding() elif params['function'] == 'rebound_distance_rebounding': temp = rb_tracking.rebound_distance_rebounding() elif params['function'] == 'shot_distance_rebounding': temp = rb_tracking.shot_distance_rebounding() elif params['function'] == 'shot_type_rebounding': temp = rb_tracking.shot_type_rebounding() else: return [] elif params['sub_module'] == 'player_shooting_splits': if params['function'] == 'assisted_by': temp = rb_tracking.assisted_by() elif params['function'] == 'assisted_shots': temp = rb_tracking.assisted_shots() elif params['function'] == 'shot_5ft': temp = rb_tracking.shot_5ft() elif params['function'] == 'shot_8ft': temp = rb_tracking.shot_8ft() elif params['function'] == 'shot_areas': temp = rb_tracking.shot_areas() elif params['function'] == 'shot_types_detail': temp = rb_tracking.shot_types_detail() elif params['function'] == 'shot_types_summary': temp = rb_tracking.shot_types_summary() else: return [] elif params['sub_module'] == 'player_shot_log_tracking': temp = player.PlayerShotLogTracking(player_id, team_id=team_id, measure_type=measure_type, per_mode=per_mode, plus_minus=plus_minus, \ pace_adjust=pace_adjust, rank=rank, league_id=league_id, season=season, season_type=season_type, po_round=po_round, outcome=outcome, \ location=location, month=month, season_segment=season_segment, date_from=date_from, date_to=date_to, opponent_team_id=opponent_team_id, \ vs_conference=vs_conference, vs_division=vs_division, game_segment=game_segment, period=period, shot_clock_range=shot_clock_range, \ last_n_games=last_n_games ) if params['function'] == 'closest_defender_shooting': temp = rb_tracking.closest_defender_shooting() elif params['function'] == 'closest_defender_shooting_long': temp = rb_tracking.closest_defender_shooting_long() elif params['function'] == 'dribble_shooting': temp = rb_tracking.dribble_shooting() elif params['function'] == 'general_shooting': temp = rb_tracking.general_shooting() elif params['function'] == 'shot_clock_shooting': temp = rb_tracking.shot_clock_shooting() elif params['function'] == 'touch_time_shooting': temp = rb_tracking.touch_time_shooting() else: return [] # elif params['sub_module'] == 'player_shot_tracking': # elif params['sub_module'] == 'player_summary': # elif params['sub_module'] == 'player_vs_player': # elif params['sub_module'] == 'player_year_over_year_splits': elif params['module'] == 'game': pass elif params['module'] == 'team': pass else: # Failure case. pass if flatten_keys: return [ l for l in list(set([el for o in temp for el in o.keys()])) if l not in to_remove ] else: return { 'data': [o[params['value_query']] for o in temp], 'labels': [o[get_x_axis_key(params['function'])] for o in temp] }
def generate_defense_df(player_id_lst, year): lst_of_dicts = [] for id in player_id_lst: print id player_defense = player.PlayerDefenseTracking(id, season=year).overall() if not player_defense.empty: d_fgm_overall = float(player_defense.D_FGM[ player_defense.DEFENSE_CATEGORY == 'Overall']) d_fga_overall = float(player_defense.D_FGA[ player_defense.DEFENSE_CATEGORY == 'Overall']) d_fgm_paint = float(player_defense.D_FGM[ player_defense.DEFENSE_CATEGORY == 'Less Than 6 Ft']) d_fga_paint = float(player_defense.D_FGA[ player_defense.DEFENSE_CATEGORY == 'Less Than 6 Ft']) d_freq_paint = float(player_defense.FREQ[ player_defense.DEFENSE_CATEGORY == 'Less Than 6 Ft']) d_fgm_perim = float(player_defense.D_FGM[ player_defense.DEFENSE_CATEGORY == 'Greater Than 15 Ft']) d_fga_perim = float(player_defense.D_FGA[ player_defense.DEFENSE_CATEGORY == 'Greater Than 15 Ft']) d_freq_perim = float(player_defense.FREQ[ player_defense.DEFENSE_CATEGORY == 'Greater Than 15 Ft']) d_fgm_mid = d_fgm_overall - d_fgm_perim - d_fgm_paint d_fga_mid = d_fga_overall - d_fga_perim - d_fga_paint d_freq_mid = 1 - d_freq_perim - d_freq_paint d_fgm_threes = float(player_defense.D_FGM[ player_defense.DEFENSE_CATEGORY == '3 Pointers']) d_fga_threes = float(player_defense.D_FGA[ player_defense.DEFENSE_CATEGORY == '3 Pointers']) d_freq_threes = float(player_defense.FREQ[ player_defense.DEFENSE_CATEGORY == '3 Pointers']) lst_of_dicts.append({ 'player_id': str(id), 'd_fgm_overall': d_fgm_overall, 'd_fga_overall': d_fga_overall, 'd_fgm_paint': d_fgm_paint, 'd_fga_paint': d_fga_paint, 'd_fgm_mid': d_fgm_mid, 'd_fga_mid': d_fga_mid, 'd_fgm_perim': d_fgm_perim, 'd_fga_perim': d_fga_perim, 'd_fgm_threes': d_fgm_threes, 'd_fga_threes': d_fga_threes, 'd_freq_paint': d_freq_paint, 'd_freq_perim': d_freq_perim, 'd_freq_mid': d_freq_mid, 'd_freq_threes': d_freq_threes }) # time.sleep(1) else: lst_of_dicts.append({ 'player_id': str(id), 'd_fgm_overall': 0, 'd_fga_overall': 0, 'd_fgm_paint': 0, 'd_fga_paint': 0, 'd_fgm_mid': 0, 'd_fga_mid': 0, 'd_fgm_perim': 0, 'd_fga_perim': 0, 'd_fgm_threes': 0, 'd_fga_threes': 0, 'd_freq_paint': 0, 'd_freq_perim': 0, 'd_freq_mid': 0, 'd_freq_threes': 0 }) defense_df = pd.DataFrame(lst_of_dicts) defense_df.set_index('player_id', inplace=True, drop=True) return defense_df
def getData(name, type, season=''): season = str(season) if type.lower() == 'shotlog': id = getPlayerId(name) year = season + '-' + str(int(season) + 1)[-2:] shot_data = shotchart.ShotChart(id, season=year).json data = shot_data['resultSets'][0]['rowSet'] indices = range(0, len(data)) colnames = shot_data['resultSets'][0]['headers'] df = pd.DataFrame(data, index=indices, columns=colnames) df = df.sort_values( ['GAME_DATE', 'PERIOD', 'MINUTES_REMAINING', 'SECONDS_REMAINING'], ascending=[1, 1, 0, 0]) return df elif type.lower() == 'pergame': id = getPlayerId(name) df = pd.DataFrame( player.PlayerCareer(id, 'PerGame').regular_season_totals()) df.columns = list(map(lambda x: x.upper(), df)) if season: year = season + '-' + str(int(season) + 1)[-2:] df = df.loc[df['SEASON_ID'] == year] return df elif type.lower() == 'gamelog': id = getPlayerId(name) try: season + '-' + str(int(season) + 1)[-2:] except: return 'Season is required. Please fill.' year = season + '-' + str(int(season) + 1)[-2:] df = player.PlayerGameLogs(id, '00', year, 'Regular Season').info() df['GAME_DATE'] = pd.to_datetime(pd.to_datetime( df['GAME_DATE'], infer_datetime_format=True), format='%Y%m%d') return df.sort_values(['GAME_DATE']) elif type.lower() == 'defense': id = getPlayerId(name) year = season + '-' + str(int(season) + 1)[-2:] player.PlayerDefenseTracking(id, 0) json = player.PlayerDefenseTracking(getPlayerId(name), 0, measure_type='Base', per_mode='PerGame', season=year).json data = json['resultSets'][0]['rowSet'] indices = [x[3] for x in data] # Set DEFENSE_CATEGORY as index colnames = json['resultSets'][0]['headers'] df = pd.DataFrame(data, index=indices, columns=colnames) df = df.drop(['CLOSE_DEF_PERSON_ID', 'DEFENSE_CATEGORY'], axis=1) return 'PlayerName: ' + name.upper(), 'Season: ' + year, df elif type.lower() == 'shotmap': id = getPlayerId(name) year = season + '-' + str(int(season) + 1)[-2:] shot_data = shotchart.ShotChart(id, season=year).json data = shot_data['resultSets'][0]['rowSet'] indices = range(0, len(data)) colnames = shot_data['resultSets'][0]['headers'] df = pd.DataFrame(data, index=indices, columns=colnames) df = df.sort_values( ['GAME_DATE', 'PERIOD', 'MINUTES_REMAINING', 'SECONDS_REMAINING'], ascending=[1, 1, 0, 0]) df_size = df.groupby(['SHOT_ZONE_AREA', 'SHOT_ZONE_RANGE']).size() df_made = df.loc[df['SHOT_MADE_FLAG'] == 1].groupby( ['SHOT_ZONE_AREA', 'SHOT_ZONE_RANGE']).size() df_pct = df_made / df_size df_pct = df_pct.fillna(0) df_size = df_size.to_frame('SHOTS_ATT').reset_index() df_made = df_made.to_frame('SHOTS_MADE').reset_index() df_pct = df_pct.to_frame('SHOT_PCT').reset_index() df = df_made.merge(df_size, how='right', on=['SHOT_ZONE_AREA', 'SHOT_ZONE_RANGE']).fillna(int(0)).merge( df_pct, on=['SHOT_ZONE_AREA', 'SHOT_ZONE_RANGE']) df['SHOTS_MADE'] = df['SHOTS_MADE'].astype(int) return df