def generate_pos_stats_df(player_id_list, year): lst_of_dicts = [] for id in player_id_list: print id stats = player.PlayerGeneralSplits(player_id=id, per_mode='PerPossession', season=year).overall() dreb_pos = stats['DREB'].sum() oreb_pos = stats['OREB'].sum() blk_pos = stats['BLK'].sum() stl_pos = stats['STL'].sum() temp_dict = { 'player_id': str(id), 'dreb_pos': float(dreb_pos), 'oreb_pos': float(oreb_pos), 'blk_pos': float(blk_pos), 'stl_pos': float(stl_pos) } lst_of_dicts.append(temp_dict) reb_pos_df = pd.DataFrame(lst_of_dicts) reb_pos_df.set_index('player_id', inplace=True, drop=True) return reb_pos_df
def get_deck_ratings(_deck, _nb_games, _date, _plot_trends, _season, _season_type): last_n_games_fp_avg = [] last_n_games_ttfl_avg = [] # --- print "Fetching deck ratings..." # iterate through deck and fetch first and last name pids = _deck['PLAYER_ID'].values names = _deck['PLAYER_NAME'].values for pid, name in zip(pids, names): # fetch player splits for last N games (passed as parameter) last_splits = nba_player.PlayerGeneralSplits( pid, last_n_games=_nb_games, season_type=_season_type).overall() # fetch nba fp from splits try: last_n_games_fp_avg.append( last_splits['NBA_FANTASY_PTS'].values[0]) except IndexError: last_n_games_fp_avg.append(0.0) # calculate ttfl score from splits last_n_games_ttfl_avg.append(score.get_ttfl_score(last_splits)) _deck['LAST_N_GAMES_NBA_FANTASY_PTS'] = pandas.Series(last_n_games_fp_avg, index=_deck.index) _deck['LAST_N_GAMES_TTFL_SCORE'] = pandas.Series(last_n_games_ttfl_avg, index=_deck.index) print "Deck ratings fetched..." print "----------" # --- print "Tracing deck ratings..." plot_deck_ratings(_deck, _nb_games, _date) print "Deck ratings traced..." print "----------" # --- if _plot_trends: # iterate through player deck for index, row in _deck.iterrows(): player.get_player_trend(row, _nb_games, _season, _season_type)
def nets_player_plus_location(player_id, location): id = player_id player_location_info = player.PlayerGeneralSplits(id).location() plus_minus = "N/A" for info in player_location_info: if info["GROUP_VALUE"] == location: plus_minus = info["PLUS_MINUS"] else: continue return plus_minus
def query_data(case): ## General Splits if str(case) == "generalsplits": for name in my_own_namelist.keys(): player_id = my_own_namelist[name] print(name, player_id) season = '2018-19' stats = player.PlayerGeneralSplits(player_id=player_id, season=season).json #print(json.dumps(stats,indent=4,sort_keys=True)) # So for now I don't care about the parameters in the dictionary. I just save them down first. target_dir = str(season) + '\\generalsplits\\' + str( player_id) + '_' + str(name) with open(target_dir, 'w') as f: json.dump(stats, f)
def get_3pt_impact_and_free_throw(): global free_throw, three_point, blocks, steals, long_range my_player = player.PlayerGeneralSplits(player_id) career_stats = my_player.overall() three_pointers_made = career_stats["FG3M"] three_pointers_att = career_stats["FG3A"] blocks = career_stats['BLK'].item() steals = career_stats['STL'].item() three_point_score_series = ((three_pointers_made/three_pointers_att) * three_pointers_made) free_throw_pct_series = career_stats["FT_PCT"] free_throw_pct = free_throw_pct_series.item() three_point_score = three_point_score_series.item() free_throw = get_free_throw_score(free_throw_pct) three_point = get_3pt_score(three_point_score) long_range = mid_range + three_point + free_throw
def testPlayerGeneralSplits(self): playergeneralsplits = player.PlayerGeneralSplits( self.playerId, self.teamId, self.measureType, self.perMode, self.plusMinus, self.paceAdjust, self.rank, self.leagueId, self.season, self.seasonType, self.poRound, self.outcome, self.location, self.month, self.sessionSegment, self.dateFrom, self.dateTo, self.opponentTeamId, self.vsConference, self.vsDivision, self.gameSegment, self.lastNGames) self.assertTrue((1, 62) == playergeneralsplits.overall().shape) self.assertTrue((31 == playergeneralsplits.overall()[0:1].GP).all()) self.assertTrue((1 == playergeneralsplits.days_rest()[0:1].GP).all()) self.assertTrue((31 == playergeneralsplits.location()[0:1].GP).all()) self.assertTrue((1 == playergeneralsplits.month()[0:1].GP).all()) self.assertTrue( (20 == playergeneralsplits.pre_post_all_star()[0:1].GP).all()) self.assertTrue( (30 == playergeneralsplits.starting_position()[0:1].GP).all()) self.assertTrue((31 == playergeneralsplits.win_losses()[0:1].GP).all())
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 save_or_update_players(): players = Player.objects.all() print("updating players stats") playerList = player.PlayerList() for val in playerList.info(): playerApp = Player() playerApp.id_nba = val['PERSON_ID'] try: playerApp = Player.objects.get(id_nba=playerApp.id_nba) except Player.DoesNotExist: playerApp.id_nba = val['PERSON_ID'] player_summary = player.PlayerSummary(playerApp.id_nba) playerApp.name = player_summary.info().__getitem__( 0)['DISPLAY_FIRST_LAST'] playerApp.position = player_summary.info().__getitem__( 0)['POSITION'] playerApp.number = player_summary.info().__getitem__(0)['JERSEY'] playerApp.height = player_summary.info().__getitem__(0)['HEIGHT'] playerApp.weight = player_summary.info().__getitem__(0)['WEIGHT'] birth_date = player_summary.info().__getitem__(0)['BIRTHDATE'] playerApp.birth_date = birth_date[:10] playerApp.college = player_summary.info().__getitem__(0)['SCHOOL'] playerApp.draft = player_summary.info().__getitem__( 0)['DRAFT_YEAR'] playerApp.experience = player_summary.info().__getitem__( 0)['SEASON_EXP'] try: teamApp = Team.objects.get( name=player_summary.info().__getitem__(0)['TEAM_NAME']) playerApp.team = teamApp except Team.DoesNotExist: pass try: playerApp.save() player_general_splits = player.PlayerGeneralSplits( playerApp.id_nba) if player_general_splits.overall(): statistic1 = PlayerStatistic() try: statistic1 = PlayerStatistic.objects.get(player=playerApp) except PlayerStatistic.DoesNotExist: statistic1.season = Season.objects.get(year="2017/18") statistic1.player = playerApp statistic1.playoff = False statistic1.ppg = player_general_splits.overall().__getitem__( 0)['PTS'] statistic1.rpg = player_general_splits.overall().__getitem__( 0)['REB'] statistic1.apg = player_general_splits.overall().__getitem__( 0)['AST'] statistic1.bpg = player_general_splits.overall().__getitem__( 0)['BLK'] statistic1.spg = player_general_splits.overall().__getitem__( 0)['STL'] statistic1.tpg = player_general_splits.overall().__getitem__( 0)['TOV'] statistic1.mpg = player_general_splits.overall().__getitem__( 0)['MIN'] statistic1.ttp = player_general_splits.overall().__getitem__( 0)['FG3_PCT'] statistic1.fgp = player_general_splits.overall().__getitem__( 0)['FG_PCT'] statistic1.ftp = player_general_splits.overall().__getitem__( 0)['FT_PCT'] statistic1.ftm = player_general_splits.overall().__getitem__( 0)['FTM'] statistic1.fgm = player_general_splits.overall().__getitem__( 0)['FGM'] statistic1.ttm = player_general_splits.overall().__getitem__( 0)['FG3M'] statistic1.save() print("salvou ou atualizou jogador:" + playerApp.name) except ValueError: pass